+(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)
+ (if (not (list? e)) #f
+ (assoc (car e) (flat-map data-tors (env-data-layouts env)))))
+
+ ; returns the internal offset in bytes of a product within an ADT
+ ; given the constructor layout
+ ; constructor-layout: (foo (Int Bool))
+(define (data-product-offset data-layouts type sum index)
+ (let* ([products (cdr (assoc sum (cdr (assoc type data-layouts))))]
+ [to-traverse (list-head products index)])
+ (fold-left
+ (lambda (acc t) (+ acc (type-size data-layouts t)))
+ wordsize ; skip the tag in the first word
+ to-traverse)))
+
+(define (data-sum-tag data-layouts type sum)
+
+ (define (go acc sums)
+ (when (null? sums) (error #f "data-sum-tag no sum for type" sum type))
+ (if (eqv? sum (car sums))
+ acc
+ (go (+ 1 acc) (cdr sums))))
+ (let* ([type-sums (cdr (assoc type data-layouts))])
+ (go 0 (map car type-sums))))
+
+(define (codegen-data-tor e si env)
+
+ (define (codegen-destructor tor)
+ (let* ([res (codegen-expr (cadr e) si env)]
+ [info (cadr tor)]
+ [index (caddr info)]
+ [type (car info)]
+ [sum (cadr info)])
+ (when (not (stack-expr? res))
+ (error #f "codegened something that wasn't a stack expression"))
+ ;TODO handle stack types
+ (emit "movq ~a(%rbp), %rax"
+ (- si (data-product-offset (env-data-layouts env) type sum index)))))
+
+ (define (codegen-constructor tor)
+ (let* ([info (cadr tor)]
+ [type (car info)]
+ [sum (cadr info)]
+ [constructor (car e)]
+
+ [args (cdr e)]
+
+ [tag (data-sum-tag (env-data-layouts env)
+ type
+ sum)]
+
+ [insert-product
+ (lambda (expr i)
+ (let ([res (codegen-expr expr si env)]
+ [stack-offset (- si (data-product-offset (env-data-layouts env)
+ type sum
+ i))])
+ (if (stack-expr? res)
+ (error #f "todo: handle stack-exprs in stack exprs")
+ (emit "movq %rax, ~a(%rbp)" stack-offset))))])
+
+ ; emit the tag
+ (emit "movq $~a, ~a(%rbp)" tag si)
+
+ (for-each insert-product args (range 0 (length args)))
+ (type-size (env-data-layouts env) type)))
+
+ (let* ([tor (data-tor env e)]
+ [constructor (eqv? 'constructor (caddr (cadr tor)))])
+ (if constructor
+ (codegen-constructor tor)
+ (codegen-destructor tor))))
+
+(define stack-expr? number?)
+
+ ; returns a number if result was stored on stack