From cd8ee23cd2c3347ecad055ad9f4044bc2869946e Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Fri, 14 Aug 2026 15:54:48 +0100 Subject: [PATCH 1/2] Add --run-hook to run a user Scheme processor over the pass outputs Signed-off-by: Rodrigo Quelhas --- compiler/compactc.ss | 24 +++++++++++++++++++++++- flake.nix | 12 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/compiler/compactc.ss b/compiler/compactc.ss index cfd6571c5..1d82aeb1e 100755 --- a/compiler/compactc.ss +++ b/compiler/compactc.ss @@ -82,6 +82,19 @@ The following flags, if present, affect the compiler's behavior as follows: --trace-passes causes the compiler to print tracing information that is generally useful only to compiler developers. + + --run-hook loads the Scheme file at and calls the + procedure it defines under the name `hook` after every compiler pass, with + the pass's name (a symbol), its unparser, its pretty formats, and the + pass's output values. The file is loaded into the interaction + environment, where the compiler's own libraries, for example (langs) and + (vm), can be imported, so a hook can process the compiler's intermediate + languages in ways the printed representation does not support. The hook + runs in process and sees internal, unversioned compiler structures: a + hook written for one compiler version can need changes for the next. + Importing the compiler's libraries requires a compactc build that keeps + them visible (the compactc-hooks package); in the default build the + hook's imports fail with a library-not-found error. ")) (usage " ... ") @@ -100,6 +113,7 @@ The following flags, if present, affect the compiler's behavior as follows: [(--compact-path) (string search-list)] [(--trace-search)] [(--trace-passes)] + [(--run-hook) (string hook-pathname)] [(--feature-zkir-v3)]) (string source-pathname) (string target-directory-pathname)) @@ -113,7 +127,15 @@ 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)))] + (generate-everything source-pathname target-directory-pathname + #f + (and ?--run-hook + (begin + (load hook-pathname) + (let ([h (eval 'hook (interaction-environment))]) + (unless (procedure? h) + (external-errorf "the file loaded by --run-hook must define a procedure named hook")) + h))))))] [((flags [(--help) $ (begin (print-help) (exit))] [(--version) $ (begin (print-compiler-version) (exit))] [(--language-version) $ (begin (print-language-version) (exit))] diff --git a/flake.nix b/flake.nix index cefd98a77..1c86d0e65 100644 --- a/flake.nix +++ b/flake.nix @@ -210,6 +210,18 @@ checkPhase = ""; }); + # compactc with its libraries kept visible for import by --run-hook + # files. Visibility defeats whole-program pruning, so this binary is + # substantially larger than the default one; that is why it is a + # separate package. + packages.compactc-hooks = packages.compactc.overrideAttrs (oldAttrs: { + name = "compactc-hooks"; + buildPhase = builtins.replaceStrings + [ ''(compile-whole-program "obj/compiler/compactc.wpo" "obj/compactc")'' ] + [ ''(compile-whole-program "obj/compiler/compactc.wpo" "obj/compactc" #t)'' ] + oldAttrs.buildPhase; + }); + packages.compactc = pkgs.stdenv.mkDerivation { name = "compactc"; version = "0.33.122"; # NB: also update compiler-version in compiler/compiler-version.ss From e706c06e46f3f9085fef1066a69add6bbf3b3d42 Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Fri, 14 Aug 2026 17:02:17 +0100 Subject: [PATCH 2/2] Keep the compiler's libraries visible in the default build Signed-off-by: Rodrigo Quelhas --- compiler/compactc.ss | 3 --- flake.nix | 14 +------------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/compiler/compactc.ss b/compiler/compactc.ss index 1d82aeb1e..df104cc5b 100755 --- a/compiler/compactc.ss +++ b/compiler/compactc.ss @@ -92,9 +92,6 @@ The following flags, if present, affect the compiler's behavior as follows: languages in ways the printed representation does not support. The hook runs in process and sees internal, unversioned compiler structures: a hook written for one compiler version can need changes for the next. - Importing the compiler's libraries requires a compactc build that keeps - them visible (the compactc-hooks package); in the default build the - hook's imports fail with a library-not-found error. ")) (usage " ... ") diff --git a/flake.nix b/flake.nix index 1c86d0e65..25cc774e2 100644 --- a/flake.nix +++ b/flake.nix @@ -210,18 +210,6 @@ checkPhase = ""; }); - # compactc with its libraries kept visible for import by --run-hook - # files. Visibility defeats whole-program pruning, so this binary is - # substantially larger than the default one; that is why it is a - # separate package. - packages.compactc-hooks = packages.compactc.overrideAttrs (oldAttrs: { - name = "compactc-hooks"; - buildPhase = builtins.replaceStrings - [ ''(compile-whole-program "obj/compiler/compactc.wpo" "obj/compactc")'' ] - [ ''(compile-whole-program "obj/compiler/compactc.wpo" "obj/compactc" #t)'' ] - oldAttrs.buildPhase; - }); - packages.compactc = pkgs.stdenv.mkDerivation { name = "compactc"; version = "0.33.122"; # NB: also update compiler-version in compiler/compiler-version.ss @@ -266,7 +254,7 @@ (compile-program "obj/compiler/compactc.ss" "obj/compiler/compactc.so") (compile-program "obj/compiler/format-compact.ss" "obj/compiler/format-compact.so") (compile-program "obj/compiler/fixup-compact.ss" "obj/compiler/fixup-compact.so") - (compile-whole-program "obj/compiler/compactc.wpo" "obj/compactc") + (compile-whole-program "obj/compiler/compactc.wpo" "obj/compactc" #t) (compile-whole-program "obj/compiler/format-compact.wpo" "obj/format-compact") (compile-whole-program "obj/compiler/fixup-compact.wpo" "obj/fixup-compact") END