lifting up closure constants

This commit is contained in:
Danny Yoo 2012-03-02 00:58:29 -05:00
parent 0ad456d380
commit cd7b8904f5
5 changed files with 49 additions and 33 deletions

View File

@ -7,10 +7,31 @@
"../compiler/il-structs.rkt" "../compiler/il-structs.rkt"
racket/string) racket/string)
(provide assemble-op-expression) (provide assemble-op-expression
current-interned-constant-closure-table
assemble-current-interned-constant-closure-table)
(: current-interned-constant-closure-table (Parameterof (HashTable Symbol MakeCompiledProcedure)))
(define current-interned-constant-closure-table
(make-parameter ((inst make-hasheq Symbol MakeCompiledProcedure))))
(: assemble-current-interned-constant-closure-table (-> String))
(define (assemble-current-interned-constant-closure-table)
(string-join (hash-map
(current-interned-constant-closure-table)
(lambda: ([a-label : Symbol] [a-shell : MakeCompiledProcedure])
(format "var ~a_c=new RT.Closure(~a,~a,void(0),~a);"
(assemble-label (make-Label (MakeCompiledProcedure-label a-shell)))
(assemble-label (make-Label (MakeCompiledProcedure-label a-shell)))
(assemble-arity (MakeCompiledProcedure-arity a-shell))
(assemble-display-name (MakeCompiledProcedure-display-name a-shell)))))
"\n"))
(: assemble-op-expression (PrimitiveOperator Blockht -> String)) (: assemble-op-expression (PrimitiveOperator Blockht -> String))
(define (assemble-op-expression op blockht) (define (assemble-op-expression op blockht)
(cond (cond
@ -20,17 +41,17 @@
[(MakeCompiledProcedure? op) [(MakeCompiledProcedure? op)
(cond (cond
;; Small optimization: try to avoid creating the array if we know up front ;; Small optimization: try to avoid creating the array if we know up front
;; that the closure has no closed values. ;; that the closure has no closed values. It's a constant that we lift up to the toplevel.
[(null? (MakeCompiledProcedure-closed-vals op)) [(null? (MakeCompiledProcedure-closed-vals op))
(format "new RT.Closure(~a,~a,void(0),~a)" (define assembled-label (assemble-label (make-Label (MakeCompiledProcedure-label op))))
(assemble-label (make-Label (MakeCompiledProcedure-label op)) (unless (hash-has-key? (current-interned-constant-closure-table) (MakeCompiledProcedure-label op))
blockht) (hash-set! (current-interned-constant-closure-table)
(assemble-arity (MakeCompiledProcedure-arity op)) (MakeCompiledProcedure-label op)
(assemble-display-name (MakeCompiledProcedure-display-name op)))] op))
(format "~a_c" assembled-label)]
[else [else
(format "new RT.Closure(~a,~a,[~a],~a)" (format "new RT.Closure(~a,~a,[~a],~a)"
(assemble-label (make-Label (MakeCompiledProcedure-label op)) (assemble-label (make-Label (MakeCompiledProcedure-label op)))
blockht)
(assemble-arity (MakeCompiledProcedure-arity op)) (assemble-arity (MakeCompiledProcedure-arity op))
(string-join (map (string-join (map
assemble-env-reference/closure-capture assemble-env-reference/closure-capture
@ -44,8 +65,7 @@
[(MakeCompiledProcedureShell? op) [(MakeCompiledProcedureShell? op)
(format "new RT.Closure(~a,~a,void(0),~a)" (format "new RT.Closure(~a,~a,void(0),~a)"
(assemble-label (make-Label (MakeCompiledProcedureShell-label op)) (assemble-label (make-Label (MakeCompiledProcedureShell-label op)))
blockht)
(assemble-arity (MakeCompiledProcedureShell-arity op)) (assemble-arity (MakeCompiledProcedureShell-arity op))
(assemble-display-name (MakeCompiledProcedureShell-display-name op)))] (assemble-display-name (MakeCompiledProcedureShell-display-name op)))]

View File

@ -49,7 +49,7 @@
[(Reg? v) [(Reg? v)
(assemble-reg v)] (assemble-reg v)]
[(Label? v) [(Label? v)
(assemble-label v blockht)] (assemble-label v)]
[(Const? v) [(Const? v)
(assemble-const v)] (assemble-const v)]
[(EnvLexicalReference? v) [(EnvLexicalReference? v)
@ -335,8 +335,8 @@
(: assemble-label (Label Blockht -> String)) (: assemble-label (Label -> String))
(define (assemble-label a-label Blockht) (define (assemble-label a-label)
(munge-label-name a-label)) (munge-label-name a-label))
@ -479,7 +479,7 @@
[(Reg? a-location) [(Reg? a-location)
(assemble-reg a-location)] (assemble-reg a-location)]
[(Label? a-location) [(Label? a-location)
(assemble-label a-location blockht)])) (assemble-label a-location)]))
(: assemble-primitive-kernel-value (PrimitiveKernelValue -> String)) (: assemble-primitive-kernel-value (PrimitiveKernelValue -> String))

View File

@ -156,8 +156,7 @@
(format "M.modules[~s]=new RT.ModuleRecord(~s,~a);" (format "M.modules[~s]=new RT.ModuleRecord(~s,~a);"
(symbol->string (ModuleLocator-name (InstallModuleEntry!-path op))) (symbol->string (ModuleLocator-name (InstallModuleEntry!-path op)))
(symbol->string (InstallModuleEntry!-name op)) (symbol->string (InstallModuleEntry!-name op))
(assemble-label (make-Label (InstallModuleEntry!-entry-point op)) (assemble-label (make-Label (InstallModuleEntry!-entry-point op))))]
blockht))]
[(MarkModuleInvoked!? op) [(MarkModuleInvoked!? op)
(format "M.modules[~s].isInvoked=true;" (format "M.modules[~s].isInvoked=true;"

View File

@ -36,7 +36,8 @@
;; What's emitted is a function expression that, when invoked, runs the ;; What's emitted is a function expression that, when invoked, runs the
;; statements. ;; statements.
(define (assemble/write-invoke stmts op) (define (assemble/write-invoke stmts op)
(parameterize ([current-interned-symbol-table ((inst make-hash Symbol Symbol))]) (parameterize ([current-interned-symbol-table ((inst make-hash Symbol Symbol))]
[current-interned-constant-closure-table ((inst make-hash Symbol MakeCompiledProcedure))])
(display "(function(M, success, fail, params) {\n" op) (display "(function(M, success, fail, params) {\n" op)
(display "var param;\n" op) (display "var param;\n" op)
(display "var RT = plt.runtime;\n" op) (display "var RT = plt.runtime;\n" op)
@ -59,6 +60,7 @@
op) op)
(write-linked-label-attributes stmts blockht op) (write-linked-label-attributes stmts blockht op)
(display (assemble-current-interned-symbol-table) op) (display (assemble-current-interned-symbol-table) op)
(display (assemble-current-interned-constant-closure-table) op)
(display "M.params.currentErrorHandler = fail;\n" op) (display "M.params.currentErrorHandler = fail;\n" op)
(display "M.params.currentSuccessHandler = success;\n" op) (display "M.params.currentSuccessHandler = success;\n" op)
@ -71,8 +73,7 @@ for (param in params) {
EOF EOF
op) op)
(fprintf op "M.trampoline(~a, true); })" (fprintf op "M.trampoline(~a, true); })"
(assemble-label (make-Label (BasicBlock-name (first basic-blocks))) (assemble-label (make-Label (BasicBlock-name (first basic-blocks)))))))
blockht))))
@ -158,7 +159,7 @@ EOF
[else [else
(fprintf op "~a.mvr=~a;\n" (fprintf op "~a.mvr=~a;\n"
(munge-label-name (make-Label (LinkedLabel-label stmt))) (munge-label-name (make-Label (LinkedLabel-label stmt)))
(assemble-label (make-Label (LinkedLabel-linked-to stmt)) blockht))]) (assemble-label (make-Label (LinkedLabel-linked-to stmt))))])
(next)] (next)]
[(DebugPrint? stmt) [(DebugPrint? stmt)
(next)] (next)]
@ -223,7 +224,7 @@ EOF
(: default-assemble-basic-block (BasicBlock Blockht (Setof Symbol) (Setof Symbol) Output-Port -> 'ok)) (: default-assemble-basic-block (BasicBlock Blockht (Setof Symbol) (Setof Symbol) Output-Port -> 'ok))
(define (default-assemble-basic-block a-basic-block blockht entry-points function-entry-and-exit-names op) (define (default-assemble-basic-block a-basic-block blockht entry-points function-entry-and-exit-names op)
(fprintf op "var ~a=function(M){" (fprintf op "var ~a=function(M){"
(assemble-label (make-Label (BasicBlock-name a-basic-block)) blockht)) (assemble-label (make-Label (BasicBlock-name a-basic-block))))
(define is-self-looping? (define is-self-looping?
(let () (let ()
(cond [(not (empty? (BasicBlock-stmts a-basic-block))) (cond [(not (empty? (BasicBlock-stmts a-basic-block)))
@ -240,7 +241,7 @@ EOF
(fprintf op "while(true){") (fprintf op "while(true){")
(when (set-contains? function-entry-and-exit-names (BasicBlock-name a-basic-block)) (when (set-contains? function-entry-and-exit-names (BasicBlock-name a-basic-block))
(fprintf op "if(--M.cbt<0){throw ~a;}\n" (fprintf op "if(--M.cbt<0){throw ~a;}\n"
(assemble-label (make-Label (BasicBlock-name a-basic-block)) blockht))) (assemble-label (make-Label (BasicBlock-name a-basic-block)))))
(assemble-block-statements (BasicBlock-name a-basic-block) (assemble-block-statements (BasicBlock-name a-basic-block)
(drop-right (BasicBlock-stmts a-basic-block) 1) (drop-right (BasicBlock-stmts a-basic-block) 1)
@ -251,7 +252,7 @@ EOF
[else [else
(when (set-contains? function-entry-and-exit-names (BasicBlock-name a-basic-block)) (when (set-contains? function-entry-and-exit-names (BasicBlock-name a-basic-block))
(fprintf op "if(--M.cbt<0){throw ~a;}\n" (fprintf op "if(--M.cbt<0){throw ~a;}\n"
(assemble-label (make-Label (BasicBlock-name a-basic-block)) blockht))) (assemble-label (make-Label (BasicBlock-name a-basic-block)))))
(assemble-block-statements (BasicBlock-name a-basic-block) (assemble-block-statements (BasicBlock-name a-basic-block)
(BasicBlock-stmts a-basic-block) (BasicBlock-stmts a-basic-block)
blockht blockht
@ -553,11 +554,9 @@ EOF
(let: ([label : (U Symbol LinkedLabel) (PushControlFrame/Call-label stmt)]) (let: ([label : (U Symbol LinkedLabel) (PushControlFrame/Call-label stmt)])
(cond (cond
[(symbol? label) [(symbol? label)
(assemble-label (make-Label label) (assemble-label (make-Label label))]
blockht)]
[(LinkedLabel? label) [(LinkedLabel? label)
(assemble-label (make-Label (LinkedLabel-label label)) (assemble-label (make-Label (LinkedLabel-label label)))])))]
blockht)])))]
[(PushControlFrame/Prompt? stmt) [(PushControlFrame/Prompt? stmt)
;; fixme: use a different frame structure ;; fixme: use a different frame structure
@ -565,11 +564,9 @@ EOF
(let: ([label : (U Symbol LinkedLabel) (PushControlFrame/Prompt-label stmt)]) (let: ([label : (U Symbol LinkedLabel) (PushControlFrame/Prompt-label stmt)])
(cond (cond
[(symbol? label) [(symbol? label)
(assemble-label (make-Label label) (assemble-label (make-Label label))]
blockht)]
[(LinkedLabel? label) [(LinkedLabel? label)
(assemble-label (make-Label (LinkedLabel-label label)) (assemble-label (make-Label (LinkedLabel-label label)))]))
blockht)]))
(let: ([tag : (U DefaultContinuationPromptTag OpArg) (let: ([tag : (U DefaultContinuationPromptTag OpArg)
(PushControlFrame/Prompt-tag stmt)]) (PushControlFrame/Prompt-tag stmt)])

View File

@ -7,4 +7,4 @@
(provide version) (provide version)
(: version String) (: version String)
(define version "1.215") (define version "1.218")