+ (let ([binding (assoc name (env-bindings env))])
+ (if (not binding)
+ (error #f (format "Variable ~a is not bound" name))
+ (emit "movq ~a(%rbp), %rax" (cdr binding)))))
+
+(define cur-lambda 0)
+(define (fresh-lambda)
+ (set! cur-lambda (+ 1 cur-lambda))
+ (format "_lambda~a" (- cur-lambda 1)))
+
+ ; a closure on the heap looks like:
+ ; 0 8 16 24
+ ; addr var1.... var2.... var3....
+
+(define (codegen-closure label captured si env)
+ (let* ((heap-offsets (map (lambda (i) (+ 8 (* 8 i)))
+ (range 0 (length captured))))) ; 4, 12, 20, etc.
+
+ (emit "## creating closure")
+
+ (emit "movq heap_start@GOTPCREL(%rip), %rbx")
+
+ (emit "movq (%rbx), %rax") ; %rax = heap addr of closure
+
+
+ ; point heap_start to next space
+ (emit "addq $~a, (%rbx)" (+ 8 (* 8 (length captured))))
+
+ (emit "## storing address to lambda")
+ ; store the address to the lambda code
+ (emit "movq ~a@GOTPCREL(%rip), %rbx" label)
+ (emit "movq %rbx, 0(%rax)")
+
+ (emit "## storing captives")
+ ; store the captured vars
+ (for-each
+ (lambda (var-name heap-offset)
+ (let ([stack-offset (cdr (assoc var-name (env-bindings env)))])
+ (emit "### captive ~a" var-name)
+ (if (eqv? stack-offset 'self-captive)
+ ; captive refers to this closure:
+ ; move heap addr of this closure to stack!
+ (emit "movq %rax, ~a(%rax)" heap-offset)
+ (begin
+ (emit "movq ~a(%rbp), %rbx" stack-offset)
+ (emit "movq %rbx, ~a(%rax)" heap-offset)))))
+ captured
+ heap-offsets)))
+
+ ; for now we can only call closures
+(define (codegen-call f args si env)
+ (codegen-expr f si env)
+
+ (emit "## starting call")
+
+ (emit "movq %rax, ~a(%rbp)" si) ; store address of closure first on stack
+
+ ; codegen the arguments, store them intermediately
+ (for-each
+ (lambda (e i)
+ (begin
+ (emit "## arg no. ~a" (- i 1))
+ (codegen-expr e (- si (* wordsize i)) env)
+ ; store intermediate result on stack
+ (emit "movq %rax, ~a(%rbp)" (- si (* wordsize i)))))
+
+ args (range 1 (length args)))
+
+ ; now that we have everything we need on the stack,
+ ; move them into the param registers
+
+ (emit "## moving args into place")
+ (for-each
+ (lambda (i) (emit "movq ~a(%rbp), ~a"
+ (- si (* wordsize i))
+ (param-register i)))
+ (range 1 (length args)))
+
+ ; todo: can this be made more efficient
+ (emit "movq ~a(%rbp), %rax" si) ; load back pointer to closure
+
+ (emit "## moving captives into place")
+
+ ; move captives into first argument
+ (emit "movq %rax, %rbx")
+ (emit "addq $8, %rbx")
+ (emit "movq %rbx, ~a" (param-register 0))
+
+ (emit "## performing call")
+
+ (emit "addq $~a, %rsp" si) ; adjust the stack pointer to account all the stuff we put in the env
+ (emit "callq *(%rax)") ; call closure function
+ (emit "subq $~a, %rsp" si))
+
+ ; LAMBDAS:
+ ; 1st param: pointer to captured args
+ ; 2nd param: 1st arg
+ ; 3rd param: 2nd arg, etc.
+
+(define (codegen-lambda l)
+ (let* ((label (car l))
+ (stuff (cdr l))
+ (captives (car stuff))
+ (args (cadr stuff))
+ (body (caddr stuff))
+ ; params = what actually gets passed
+ (params (append captives args))
+
+ (stack-offsets (map (lambda (i)
+ (* (- wordsize) (+ 1 i)))
+ (range 0 (length params))))
+
+ [bindings (map cons params stack-offsets)]
+ [env (make-env '() bindings)])
+ (emit "~a:" label)
+
+ (display "## lambda captives: ")
+ (display captives)
+ (newline)
+ (display "## lambda args: ")
+ (display args)
+ (newline)
+ (display "## lambda body: ")
+ (display body)
+ (newline)
+
+ (emit "push %rbp") ; preserve caller's base pointer
+
+ (emit "movq %rsp, %rbp") ; set up our own base pointer
+
+ ; load the captured vars onto the stack
+ (for-each
+ (lambda (i)
+ (begin
+ (emit "# loading captive ~a" (list-ref captives i))
+ (emit "movq ~a(~a), %rbx" (* wordsize i) (param-register 0))
+ (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) (+ 1 i)))))
+ (range 0 (length captives)))
+
+ ; load the args onto the stack
+ (for-each
+ (lambda (i)
+ (begin
+ (emit "movq ~a, %rbx" (param-register (+ 1 i)))
+ (emit "movq %rbx, ~a(%rbp)"
+ (* (- wordsize)
+ (+ 1 (length captives) i)))))
+ (range 0 (length args)))
+
+ (codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
+
+ (emit "pop %rbp") ; restore caller's base pointer
+ (emit "ret")))
+
+(define cur-label 0)
+(define (fresh-label)
+ (set! cur-label (+ 1 cur-label))
+ (format "label~a" (- cur-label 1)))
+
+(define (codegen-if cond then else si env)
+ (codegen-expr cond si env)
+ (emit "cmpq $0, %rax")
+ (let ((exit-label (fresh-label))
+ (else-label (fresh-label)))
+ (emit "je ~a" else-label)
+ (codegen-expr then si env)
+ (emit "jmp ~a" exit-label)
+ (emit "~a:" else-label)
+ (codegen-expr else si env)
+ (emit "~a:" exit-label)))
+
+(define (data-tor env e)
+ (and (list? e)
+ (assoc (car e) (flat-map data-tors (env-data-layouts env)))))
+
+(define (codegen-data-tor e si env)
+
+ (define (codegen-destructor tor)
+ (when (not (eqv? 'stack (ast-type (cadr e))))
+ (error #f "expected stack value"))
+ (let* ([stack-expr (cadr e)]
+ [stack-body (caddr stack-expr)]
+ [stack-type (cadr stack-expr)])
+
+ (codegen-expr stack-body si env)
+ (let ([index (cadr tor)]
+ [products 2]
+ [to-traverse (list-head products index)]
+ [offset (fold-left
+ (lambda (acc t) (+ acc (type-size t)))
+ wordsize ; skip tag in first word
+ to-traverse)])
+ 3
+ )))
+
+ (let* ([tor (data-tor env e)]
+ [constructor (eqv? 'constructor (cadr tor))])
+ (if constructor
+ (codegen-constructor tor)
+ (codegen-destructor tor))))