1 Getting Started
A minimal example for x64asm in Typed Racket would be:
#lang typed/racket (require x64asm) (define-cast ->int #:type (-> Integer) #:ctype (_fun -> _int)) (define-λ! get-1000 ->int (mov rax (imm32 1000)) (ret))
Example:
> (get-1000) - : Integer
1000
Note. For untyped racket, use x64asm/untyped instead.
Now there is a more complicated example for calculating fibonacci numbers:
#lang typed/racket (require x64asm) (define-cast int->int #:type (Integer -> Integer) #:ctype (_fun _int64 -> _int64)) (define-λ! fib int->int #:labels (start l1 l2 l3) (define arg0 (if (eq? (system-type)'windows) rcx rdi)) (:! start) (push rbp) (mov rbp rsp) (sub rsp (imm8 16)) (cmp arg0 (imm8 2)) (jg (rel8 l1)) (mov rax (imm32 1)) (leave) (ret) (:! l1) (sub arg0 (imm8 1)) (mov (mref 64 rbp - 8) arg0) (call (rel32 start)) (mov (mref 64 rbp - 16) rax) (mov arg0 (mref 64 rbp - 8)) (sub arg0 (imm8 1)) (call (rel32 start)) (add rax (mref 64 rbp - 16)) (leave) (ret) )
Example:
> (fib 40) - : Integer
102334155
An example without helper macros is
#lang typed/racket (require x64asm) (define-cast dd->d #:type (Flonum Flonum -> Flonum) #:ctype (_fun _double _double -> _double)) (define my-fl+ (parameterize ([current-context (make-context)]) (define entry (label)) (:! entry) (addsd xmm0 xmm1) (ret) (emit-code!) (dd->d (label-addr entry))))
Example:
> (my-fl+ 100.0 200.0) - : Flonum
300.0