Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion compiler/compactc.ss
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,31 @@ The following flags, if present, affect the compiler's behavior as follows:
execute circuits from this file with an interpreter. The file is one
S-expression datum. The format follows the intermediate language, so it
can change between compiler versions.

--analyzed-ir-hook <pathname> loads the Scheme file at <pathname> and calls
the procedure it defines under the name `hook` with the same datum
--analyzed-ir writes, and with the target directory as a string. The
compiler writes no analyzed-ir.sexp in this mode. The hook runs after
the compile succeeds, and it decides what to do with the datum, so a consumer can render it in the encoding its
own tools want. The datum is ordinary data, so the file needs no import
from the compiler.
"))

;; The loaded file sees only the bindings the boot file already provides, so a
;; hook that takes the datum needs no library from the compiler and the binary
;; keeps its libraries pruned.
(define (load-hook pathname)
(define (complain)
(external-errorf "the file loaded by --analyzed-ir-hook must define a procedure named hook"))
(check-pathname pathname)
(unless (file-exists? pathname)
(external-errorf "the file named by --analyzed-ir-hook does not exist: ~a" pathname))
(load pathname)
(unless (top-level-bound? 'hook (interaction-environment)) (complain))
(let ([hook (eval 'hook (interaction-environment))])
(unless (procedure? hook) (complain))
hook))

(usage "<flag> ... <source-pathname> <target-directory-pathname>")

(parameterize ([reset-handler abort])
Expand All @@ -110,6 +133,7 @@ The following flags, if present, affect the compiler's behavior as follows:
[(--trace-search)]
[(--trace-passes)]
[(--analyzed-ir)]
[(--analyzed-ir-hook) (string analyzed-ir-hook-pathname)]
[(--feature-zkir-v3)])
(string source-pathname)
(string target-directory-pathname))
Expand All @@ -124,7 +148,12 @@ The following flags, if present, affect the compiler's behavior as follows:
[trace-search ?--trace-search])
(when source-root (register-source-root! source-root))
(handle-exceptions ?--vscode
(generate-everything source-pathname target-directory-pathname)))]
;; Load inside the handler: a missing or malformed hook file is
;; user input, and its error belongs in the same format as the rest.
(parameterize ([analyzed-ir-hook
(and ?--analyzed-ir-hook
(load-hook analyzed-ir-hook-pathname))])
(generate-everything source-pathname target-directory-pathname))))]
[((flags [(--help) $ (begin (print-help) (exit))]
[(--version) $ (begin (print-compiler-version) (exit))]
[(--language-version) $ (begin (print-language-version) (exit))]
Expand Down
3 changes: 3 additions & 0 deletions compiler/config-params.ss
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@
(export-parameter feature-zkir-v3 #f)

(export-parameter write-analyzed-ir #f)

;; The procedure --analyzed-ir-hook loaded, or #f to write the file.
(export-parameter analyzed-ir-hook #f)
)
8 changes: 7 additions & 1 deletion compiler/passes.ss
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
(with-target-ports
'((contract-info.json . "compiler/contract-info.json"))
(run-passes save-contract-info-passes analyzed-ir proof-circuit-name*))
(when (write-analyzed-ir)
(when (and (write-analyzed-ir) (not (analyzed-ir-hook)))
(with-target-ports
'((analyzed-ir.sexp . "compiler/analyzed-ir.sexp"))
(run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*)))
Expand Down Expand Up @@ -198,6 +198,12 @@
(run-passes manifest-passes circuit-ir
output-directory-pathname
output-subdirectories)))
;; Last, so a failure anywhere above leaves no hook
;; output behind: the compiler does not track the
;; files a hook writes and cannot roll them back.
(when (analyzed-ir-hook)
;; The hook takes the datum, so open no file for it.
(run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*))
(when final-pass (internal-errorf 'generate-everything "never encountered final pass ~s" final-pass)))])))))))]))

(define-pass extract-circuit-names : Lflattened (ir) -> * (ls)
Expand Down
3 changes: 2 additions & 1 deletion compiler/save-analyzed-ir-passes.ss
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
(compiler-version)
(language-version)
(runtime-version)
(pass-helpers))
(pass-helpers)
(config-params))

(include "save-analyzed-ir-passes/extract-analyzed-ir.ss")

Expand Down
17 changes: 12 additions & 5 deletions compiler/save-analyzed-ir-passes/save-analyzed-ir.ss
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@
#!chezscheme

(define (save-analyzed-ir ir proof-circuit-name*)
(let ([op (get-target-port 'analyzed-ir.sexp)])
;; Parentheses only: brackets are a Chez pretty-printing style, and a
;; non-Scheme reader should not need to treat them as paren synonyms.
(parameterize ([print-brackets #f])
(pretty-print (extract-analyzed-ir ir proof-circuit-name*) op)))
(let ([sexp (extract-analyzed-ir ir proof-circuit-name*)])
(cond
;; The hook takes the datum and the directory as arguments, so it needs
;; no import from the compiler and runs in a binary built without
;; visible libraries.
[(analyzed-ir-hook) => (lambda (hook) (hook sexp (target-directory)))]
[else
(let ([op (get-target-port 'analyzed-ir.sexp)])
;; Parentheses only: brackets are a Chez pretty-printing style, and a
;; non-Scheme reader should not need to treat them as paren synonyms.
(parameterize ([print-brackets #f])
(pretty-print sexp op)))]))
ir)
4 changes: 2 additions & 2 deletions tests-e2e/src/resources/compiler_man_page.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ The following flags, if present, affect the compiler's behavior as follows:
--analyzed-ir-hook <pathname> loads the Scheme file at <pathname> and calls
the procedure it defines under the name `hook` with the same datum
--analyzed-ir writes, and with the target directory as a string. The
compiler writes no analyzed-ir.sexp in this mode: the hook decides what
to do with the datum, so a consumer can render it in the encoding its
compiler writes no analyzed-ir.sexp in this mode. The hook runs after
the compile succeeds, and it decides what to do with the datum, so a consumer can render it in the encoding its
own tools want. The datum is ordinary data, so the file needs no import
from the compiler.
Loading