Skip to content

Add --ir-hook to hand each stage of the program to a consumer's Scheme file - #18

Closed
RomarQ wants to merge 1 commit into
mainfrom
rp/ir-hook
Closed

RomarQ wants to merge 1 commit into
mainfrom
rp/ir-hook

Conversation

@RomarQ

@RomarQ RomarQ commented Aug 17, 2026 •

Copy link
Copy Markdown
Owner

Adds --ir-hook. The compiler calls a procedure in a Scheme file with the program at each stage of compilation. The compiler writes no file of its own. The hook decides what to emit.

(hook stage* proof-circuit-name* compiler target-directory)

stage* = ((lsrc     <program> <unparse>)
          (frontend <program> <unparse>)
          (analyzed <program> <unparse>)
          (circuit  <program> <unparse>))

Each stage carries the unparser of its own language, taken from the pass that made it. compiler holds what a file cannot reach on its own: expand-vm-code, an align constructor, and the three version strings.

Example

Write a file that defines hook. This one takes the analyzed stage, and prints it with the unparser that stage carried in:

;; analyzed.ss
(define hook
  (lambda (stage* proof-circuit-name* compiler target-directory)
    (let* ([stage (assq 'analyzed stage*)]
           [program (cadr stage)]
           [unparse (caddr stage)])
      (call-with-output-file (string-append target-directory "/compiler/analyzed.sexp")
        (lambda (op)
          (parameterize ([print-brackets #f]) (pretty-print (unparse program) op)))
        'replace))))

Run the compiler with it:

compactc --skip-zk --ir-hook analyzed.ss counter.compact out/

It writes out/compiler/analyzed.sexp:

(program
  (kernel-declaration (%kernel.0 () (Kernel)))
  (public-ledger-declaration
    ((%round.1 (0) (Counter)))
    (constructor () (tuple)))
  (circuit
    %increment.2
    ()
    (ttuple)
    (seq (let* (((%tmp.3 (tunsigned 65535)) (safe-cast
                                              (tunsigned 65535)
                                              (tunsigned 1)
                                              1)))
           (public-ledger %round.1 (0) increment %tmp.3))
         (tuple))))

Take (assq 'lsrc stage*), 'frontend or 'circuit for the other stages. Each one prints with its own unparser.

The unparse templates drop some fields. The public-ledger form above prints without its operation class, its type, and its expanded VM instructions. A tadt prints without its vm-expr and its two operation tables. A Program prints without the export table and the contract types. A hook that needs those walks the tree itself, and calls the unparser for the parts it covers.

A 366-line hook writes the same compiler/analyzed-ir.sexp as LFDT-Minokawa#722. All 215 example contracts that compile give byte-identical files.

@github-actions

github-actions Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Plugin Test Summary

 1 files   3 suites   1s ⏱️
21 tests 21 ✅ 0 💤 0 ❌
23 runs  23 ✅ 0 💤 0 ❌

Results for commit 38cb11f.

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Compactc E2E Test Summary

    1 files  ±    0      1 suites   - 52   6m 41s ⏱️ + 4m 54s
3 106 tests +2 629  3 106 ✅ +2 629  0 💤 ±0  0 ❌ ±0 
3 106 runs  +2 618  3 106 ✅ +2 618  0 💤 ±0  0 ❌ ±0 

Results for commit 38cb11f. ± Comparison against base commit 56c3b77.

This pull request removes 477 and adds 3106 tests. Note that renamed tests count towards both.
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[MFG-413] should compile and not thro…'
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[PM-12371] should compile and not thr…'
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[PM-15405] should compile and not thr…'
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[PM-15733] should compile and not thr…'
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[PM-15826] should compile and not thr…'
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[PM-16040] should compile and not thr…'
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[PM-16059] should compile and not thr…'
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[PM-16447] should compile and not thr…'
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[PM-16853] should compile and not thr…'
src/tests/bugs/compiler.bugs.e2e.test.ts ‑ [Bugs] Compiler > '[PM-16999] should return an error if …'
…
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_0.compact'
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_1.compact'
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_10.compact'
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_100.compact'
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_1000.compact'
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_1001.compact'
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_1002.compact'
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_1003.compact'
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_1004.compact'
src/tests/extracted.e2e.test.ts ‑ [E2E] Extracted unit tests for compiler > should be able to compile extracted contract: '/home/runner/work/_temp/nix-shell.law6Ay/temp-test-VZkqBA/contract_1005.compact'
…

♻️ This comment has been updated with latest results.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a lightweight Scheme hook for consumers to process analyzed compiler IR without exposing compiler libraries.

Changes:

  • Adds and documents --ir-hook.
  • Stores the loaded hook in compiler configuration.
  • Invokes the hook with IR, proof names, VM expander, and output directory.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
compiler/compactc.ss Parses, loads, and validates the hook.
compiler/config-params.ss Defines the hook parameter.
compiler/passes.ss Invokes the hook after analysis.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread compiler/compactc.ss Resolved
Comment thread compiler/compactc.ss Resolved
Comment thread compiler/passes.ss Resolved
Comment thread compiler/compactc.ss Resolved
@RomarQ RomarQ changed the title Add --ir-hook to hand the analyzed program to a consumer's Scheme file Add --analyzed-ir-hook to hand the analyzed program to a consumer's Scheme file Aug 17, 2026
@RomarQ RomarQ changed the title Add --analyzed-ir-hook to hand the analyzed program to a consumer's Scheme file Add --ir-hook to hand each stage of the program to a consumer's Scheme file Aug 18, 2026
@RomarQ
RomarQ force-pushed the rp/ir-hook branch 3 times, most recently from 1cafa41 to 1368d2a Compare August 18, 2026 19:44
…e file

Signed-off-by: Rodrigo Quelhas <rodrigo_quelhas@outlook.pt>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants