Propagate use-alloca parameter for constants

This commit is contained in:
Justin Ethier 2018-11-20 18:54:47 -05:00
parent bab9acd49e
commit 16e1600662

View file

@ -389,12 +389,12 @@
trace trace
cps?)) cps?))
; Core forms: ; Core forms:
((const? exp) (c-compile-const exp)) ((const? exp) (c-compile-const exp (alloca? ast-id)))
((prim? exp) ((prim? exp)
;; TODO: this needs to be more refined, probably w/a lookup table ;; TODO: this needs to be more refined, probably w/a lookup table
(c-code (string-append "primitive_" (mangle exp)))) (c-code (string-append "primitive_" (mangle exp))))
((ref? exp) (c-compile-ref exp)) ((ref? exp) (c-compile-ref exp))
((quote? exp) (c-compile-quote exp)) ((quote? exp) (c-compile-quote exp (alloca? ast-id)))
((if? exp) (c-compile-if exp append-preamble cont ast-id trace cps?)) ((if? exp) (c-compile-if exp append-preamble cont ast-id trace cps?))
; IR (2): ; IR (2):
@ -410,11 +410,11 @@
((app? exp) (c-compile-app exp append-preamble cont ast-id trace cps?)) ((app? exp) (c-compile-app exp append-preamble cont ast-id trace cps?))
(else (error "unknown exp in c-compile-exp: " exp)))) (else (error "unknown exp in c-compile-exp: " exp))))
(define (c-compile-quote qexp) (define (c-compile-quote qexp use-alloca)
(let ((exp (cadr qexp))) (let ((exp (cadr qexp)))
(c-compile-scalars exp))) (c-compile-scalars exp use-alloca)))
(define (c-compile-scalars args) (define (c-compile-scalars args use-alloca)
(letrec ( (letrec (
(num-args 0) (num-args 0)
(create-cons (create-cons
@ -429,12 +429,12 @@
((null? args) ((null? args)
(c-code "NULL")) (c-code "NULL"))
((not (pair? args)) ((not (pair? args))
(c-compile-const args)) (c-compile-const args use-alloca))
(else (else
(let* ((cvar-name (mangle (gensym 'c))) (let* ((cvar-name (mangle (gensym 'c)))
(cell (create-cons (cell (create-cons
cvar-name cvar-name
(c-compile-const (car args)) (c-compile-const (car args) use-alloca)
(_c-compile-scalars (cdr args))))) (_c-compile-scalars (cdr args)))))
(set! num-args (+ 1 num-args)) (set! num-args (+ 1 num-args))
(c-code/vars (c-code/vars
@ -447,7 +447,7 @@
(_c-compile-scalars args) (_c-compile-scalars args)
num-args))) num-args)))
(define (c-compile-vector exp) (define (c-compile-vector exp use-alloca)
(letrec ((cvar-name (mangle (gensym 'vec))) (letrec ((cvar-name (mangle (gensym 'vec)))
(len (vector-length exp)) (len (vector-length exp))
;; Generate code for each member of the vector ;; Generate code for each member of the vector
@ -455,7 +455,7 @@
(lambda (i code) (lambda (i code)
(if (= i len) (if (= i len)
code code
(let ((idx-code (c-compile-const (vector-ref exp i)))) (let ((idx-code (c-compile-const (vector-ref exp i) use-alloca)))
(loop (loop
(+ i 1) (+ i 1)
(c-code/vars (c-code/vars
@ -490,7 +490,7 @@
(number->string len) ");"))))) (number->string len) ");")))))
(loop 0 code)))))) (loop 0 code))))))
(define (c-compile-bytevector exp) (define (c-compile-bytevector exp use-alloca)
(letrec ((cvar-name (mangle (gensym 'vec))) (letrec ((cvar-name (mangle (gensym 'vec)))
(len (bytevector-length exp)) (len (bytevector-length exp))
;; Generate code for each member of the vector ;; Generate code for each member of the vector
@ -545,18 +545,18 @@
(string-append "" cvar-name) ; Code is just the variable name (string-append "" cvar-name) ; Code is just the variable name
(list ; Allocate integer on the C stack (list ; Allocate integer on the C stack
(string-append (string-append
"object " cvar-name ";\\n " "object " cvar-name ";\n "
"alloc_string(data," "alloc_string(data,"
cvar-name cvar-name
", " ", "
blen blen
", " ", "
(number->string (string-length exp)) (number->string (string-length exp))
");\\n" ");\n"
"char " tmp-name "[] = " "char " tmp-name "[] = "
(->cstr exp) (->cstr exp)
";\\n" ";\n"
"memcpy(((string_type *)" cvar-name ")->str, " tmp-name "," blen ");\\n" "memcpy(((string_type *)" cvar-name ")->str, " tmp-name "," blen ");\n"
"((string_type *)" cvar-name ")->str[" blen "] = '\\0';" "((string_type *)" cvar-name ")->str[" blen "] = '\\0';"
))))) )))))
(else (else
@ -579,16 +579,16 @@
;; Typically this function is used to compile constant values such as ;; Typically this function is used to compile constant values such as
;; a single number, boolean, etc. However, it can be passed a quoted ;; a single number, boolean, etc. However, it can be passed a quoted
;; item such as a list, to compile as a literal. ;; item such as a list, to compile as a literal.
(define (c-compile-const exp #;use-alloca) (define (c-compile-const exp use-alloca)
(cond (cond
((null? exp) ((null? exp)
(c-code "NULL")) (c-code "NULL"))
((pair? exp) ((pair? exp)
(c-compile-scalars exp)) (c-compile-scalars exp use-alloca))
((vector? exp) ((vector? exp)
(c-compile-vector exp)) (c-compile-vector exp use-alloca))
((bytevector? exp) ((bytevector? exp)
(c-compile-bytevector exp)) (c-compile-bytevector exp use-alloca))
((bignum? exp) ((bignum? exp)
(let ((cvar-name (mangle (gensym 'c))) (let ((cvar-name (mangle (gensym 'c)))
(num2str (cond (num2str (cond
@ -650,7 +650,7 @@
(c-code (string-append "obj_char2obj(" (c-code (string-append "obj_char2obj("
(number->string (char->integer exp)) ")"))) (number->string (char->integer exp)) ")")))
((string? exp) ((string? exp)
(c-compile-string exp #f)) (c-compile-string exp use-alloca))
;TODO: not good enough, need to store new symbols in a table so they can ;TODO: not good enough, need to store new symbols in a table so they can
;be inserted into the C program ;be inserted into the C program
((symbol? exp) ((symbol? exp)