racket/src/expander: add -B

adding -B option into the expander to write the bytecode of the extracted linklet
This commit is contained in:
Caner Derici 2018-05-18 21:25:51 -04:00 committed by Matthew Flatt
parent 54989bddec
commit 55011ce282
5 changed files with 37 additions and 17 deletions

View File

@ -75,6 +75,11 @@ run-no-cache:
# Writes the extracted, compiled, decompiled expander to compiled/exp.rkt
decompile:
$(RACO) make bootstrap-run.rkt
$(RACKET) $(RKT_ARGS) bootstrap-run.rkt -c compiled/cache-src $(KNOT) -s -x -D -o compiled/exp.rkt
$(RACKET) $(RKT_ARGS) bootstrap-run.rkt -c compiled/cache-src $(KNOT) $(PURE) -s -x -D -o compiled/exp.rkt
# Writes the extracted, compiled expander to compiled/exp.zo
bytecode:
$(RACO) make bootstrap-run.rkt
$(RACKET) $(RKT_ARGS) bootstrap-run.rkt -c compiled/cache-src $(KNOT) $(PURE) -s -x -B -o compiled/exp.zo
.PHONY: expander expander-src expander-src-generate demo run run-no-cache

View File

@ -75,6 +75,16 @@ Running:
Expands and extracts <file-path> as a single linklet to
<outfile-path>.
% racket bootstrap-run.rkt -c <dir> -sx -D -t <file-path> -o <outfile-path>
Expands and extracts <file-path> as a single linklet, compiles and
decompiles it, then writes the s-expression into <outfile-path>.
% racket bootstrap-run.rkt -c <dir> -sx -B -t <file-path> -o <outfile-path>
Expands and extracts <file-path> as a single linklet, compiles it
and then writes the bytecode into <outfile-path>.
% racket bootstrap-run.rkt -c <dir> -O <checkout-dir>/racket
Compiles the expander to source files in <dir> --- note that

View File

@ -5,29 +5,29 @@
(provide compile-and-decompile)
(define (compile-and-decompile linklet-expr print-extracted-to)
(define (compile-and-decompile linklet-expr print-extracted-to #:as-bytecode? as-bytecode?)
(unless compile-linklet
(error "Host Racket does not support linklet compilation"))
(log-status "Compiling and decompiling linklet to ~a" print-extracted-to)
(define linklet (compile-linklet linklet-expr))
(define o (open-output-bytes))
(write (hash->linklet-bundle (hasheq 0 linklet)) o)
(define i (open-input-bytes (get-output-bytes o)))
;; Dynamically load decompiler, so that it's not otherwise a
;; dependency for running the expander-flattener
(define zo ((dynamic-require 'compiler/zo-parse 'zo-parse) i))
(define decompiled-expr ((dynamic-require 'compiler/decompile 'decompile) zo))
(define out (open-output-bytes))
(write (hash->linklet-bundle (hasheq 0 linklet)) out)
(call-with-output-file*
print-extracted-to
#:exists 'truncate/replace
(lambda (o)
(pretty-write decompiled-expr o))))
(if as-bytecode?
(write-bytes (get-output-bytes out) o)
(let* ([i (open-input-bytes (get-output-bytes out))]
;; Dynamically load decompiler, so that it's not otherwise a
;; dependency for running the expander-flattener
[zo ((dynamic-require 'compiler/zo-parse 'zo-parse) i)]
[decompiled-expr ((dynamic-require 'compiler/decompile 'decompile) zo)])
(pretty-write decompiled-expr o))))))
(define compile-linklet
(hash-ref (primitive-table '#%linklet) 'compile-linklet #f))

View File

@ -25,6 +25,7 @@
#:print-extracted-to print-extracted-to
#:as-c? as-c?
#:as-decompiled? as-decompiled?
#:as-bytecode? as-bytecode?
;; Table of symbol -> (listof knot-spec),
;; to redirect a remaining import back to
;; an implementation that is defined in the
@ -111,7 +112,7 @@
(get-module-export-variables start-link
#:compiled-modules compiled-modules
#:cache cache))
;; Generate the flattened linklet
(define flattened-linklet-expr
(flatten! start-link
@ -124,7 +125,7 @@
(define simplified-expr
(simplify-definitions flattened-linklet-expr))
;; Remove unreferenced definitions
(define gced-linklet-expr
(garbage-collect-definitions simplified-expr))
@ -140,8 +141,8 @@
(prune-names re-renamed-linklet-expr))
(cond
[as-decompiled?
(compile-and-decompile pruned-linklet-expr print-extracted-to)]
[(or as-decompiled? as-bytecode?)
(compile-and-decompile pruned-linklet-expr print-extracted-to #:as-bytecode? as-bytecode?)]
[else
(save-and-report-flattened! pruned-linklet-expr print-extracted-to
#:as-c? as-c?)])))

View File

@ -45,6 +45,7 @@
(define makefile-dependencies-file #f)
(define extract-to-c? #f)
(define extract-to-decompiled? #f)
(define extract-to-bytecode? #f)
(define instance-knot-ties (make-hasheq))
(define primitive-table-directs (make-hasheq))
(define side-effect-free-modules (make-hash))
@ -104,6 +105,8 @@
(set! extract-to-c? #t)]
[("-D") "Print extracted bootstrap as a decompiled"
(set! extract-to-decompiled? #t)]
[("-B") "Print extracted bootstrap as bytecode"
(set! extract-to-bytecode? #t)]
#:multi
[("++knot") sym path "Redirect imports from <sym> to flattened from <path>"
(hash-update! instance-knot-ties
@ -308,6 +311,7 @@
#:print-extracted-to print-extracted-to
#:as-c? extract-to-c?
#:as-decompiled? extract-to-decompiled?
#:as-bytecode? extract-to-bytecode?
#:instance-knot-ties instance-knot-ties
#:primitive-table-directs primitive-table-directs
#:side-effect-free-modules side-effect-free-modules))