diff --git a/compiler/compactc.ss b/compiler/compactc.ss index fbc5b0f4b..99549a839 100755 --- a/compiler/compactc.ss +++ b/compiler/compactc.ss @@ -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 loads the Scheme file at 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 " ... ") (parameterize ([reset-handler abort]) @@ -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)) @@ -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))] diff --git a/compiler/config-params.ss b/compiler/config-params.ss index cc05b0f71..f34c99f0a 100644 --- a/compiler/config-params.ss +++ b/compiler/config-params.ss @@ -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) ) diff --git a/compiler/passes.ss b/compiler/passes.ss index d4aabd4fe..a07458584 100644 --- a/compiler/passes.ss +++ b/compiler/passes.ss @@ -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*))) @@ -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) diff --git a/compiler/save-analyzed-ir-passes.ss b/compiler/save-analyzed-ir-passes.ss index 72fbbcd2e..0fad5b251 100644 --- a/compiler/save-analyzed-ir-passes.ss +++ b/compiler/save-analyzed-ir-passes.ss @@ -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") diff --git a/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss b/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss index cd80359a3..ae1208b3a 100644 --- a/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss +++ b/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss @@ -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) diff --git a/tests-e2e/src/resources/compiler_man_page.txt b/tests-e2e/src/resources/compiler_man_page.txt index 6ac34a4e4..ba71373eb 100644 --- a/tests-e2e/src/resources/compiler_man_page.txt +++ b/tests-e2e/src/resources/compiler_man_page.txt @@ -69,7 +69,7 @@ The following flags, if present, affect the compiler's behavior as follows: --analyzed-ir-hook loads the Scheme file at 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.