From 9dea42753261834183954cd4414317853a46b63f Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Tue, 18 Aug 2026 11:23:01 +0100 Subject: [PATCH 1/5] Add --analyzed-ir to write the analyzed program as one S-expression Signed-off-by: Rodrigo Quelhas --- compiler/compactc.ss | 6 + compiler/config-params.ss | 2 + compiler/passes.ss | 5 + compiler/save-analyzed-ir-passes.ss | 36 ++ .../extract-analyzed-ir.ss | 309 ++++++++++++++++++ .../save-analyzed-ir.ss | 22 ++ tests-e2e/src/resources/compiler_man_page.txt | 4 + 7 files changed, 384 insertions(+) create mode 100644 compiler/save-analyzed-ir-passes.ss create mode 100644 compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss create mode 100644 compiler/save-analyzed-ir-passes/save-analyzed-ir.ss diff --git a/compiler/compactc.ss b/compiler/compactc.ss index cfd6571c5..b6e2cb6b8 100755 --- a/compiler/compactc.ss +++ b/compiler/compactc.ss @@ -82,6 +82,10 @@ 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. + + --analyzed-ir writes compiler/analyzed-ir.sexp in the target directory. + It contains the analyzed program, with ledger operations and emits expanded + into Impact VM instructions. Its format can change between compiler versions. ")) (usage " ... ") @@ -100,6 +104,7 @@ The following flags, if present, affect the compiler's behavior as follows: [(--compact-path) (string search-list)] [(--trace-search)] [(--trace-passes)] + [(--analyzed-ir)] [(--feature-zkir-v3)]) (string source-pathname) (string target-directory-pathname)) @@ -109,6 +114,7 @@ The following flags, if present, affect the compiler's behavior as follows: [skip-zk ?--skip-zk] [no-communications-commitment ?--no-communications-commitment] [feature-zkir-v3 ?--feature-zkir-v3] + [write-analyzed-ir ?--analyzed-ir] [compact-path (if ?--compact-path (split-search-path search-list) (compact-path))] [trace-search ?--trace-search]) (when source-root (register-source-root! source-root)) diff --git a/compiler/config-params.ss b/compiler/config-params.ss index ecae20589..cc05b0f71 100644 --- a/compiler/config-params.ss +++ b/compiler/config-params.ss @@ -41,4 +41,6 @@ ; feature flags (export-parameter feature-zkir-v3 #f) + + (export-parameter write-analyzed-ir #f) ) diff --git a/compiler/passes.ss b/compiler/passes.ss index fc6db8081..d4aabd4fe 100644 --- a/compiler/passes.ss +++ b/compiler/passes.ss @@ -33,6 +33,7 @@ (frontend-passes) (analysis-passes) (save-contract-info-passes) + (save-analyzed-ir-passes) (typescript-passes) (circuit-passes) (zkir-passes) @@ -145,6 +146,10 @@ (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) + (with-target-ports + '((analyzed-ir.sexp . "compiler/analyzed-ir.sexp")) + (run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*))) (with-target-ports (map (lambda (sym) (cons sym (format "zkir/~a.zkir" sym))) proof-circuit-name*) diff --git a/compiler/save-analyzed-ir-passes.ss b/compiler/save-analyzed-ir-passes.ss new file mode 100644 index 000000000..72fbbcd2e --- /dev/null +++ b/compiler/save-analyzed-ir-passes.ss @@ -0,0 +1,36 @@ +;;; This file is part of Compact. +;;; Copyright (C) 2025 Midnight Foundation +;;; SPDX-License-Identifier: Apache-2.0 +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +#!chezscheme + +(library (save-analyzed-ir-passes) + (export save-analyzed-ir-passes) + (import (except (chezscheme) errorf) + (utils) + (nanopass) + (langs) + (vm) + (compiler-version) + (language-version) + (runtime-version) + (pass-helpers)) + + (include "save-analyzed-ir-passes/extract-analyzed-ir.ss") + + (include "save-analyzed-ir-passes/save-analyzed-ir.ss") + + (define-passes save-analyzed-ir-passes + (save-analyzed-ir Lloweredemit)) +) diff --git a/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss b/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss new file mode 100644 index 000000000..c93871590 --- /dev/null +++ b/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss @@ -0,0 +1,309 @@ +;;; This file is part of Compact. +;;; Copyright (C) 2025 Midnight Foundation +;;; SPDX-License-Identifier: Apache-2.0 +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +#!chezscheme + +(define-pass extract-analyzed-ir : Lloweredemit (ir proof-circuit-name*) -> * (sexp) + (definitions + (define (fail what x) (internal-errorf 'save-analyzed-ir "unsupported ~a: ~s" what x)) + + (define id-number (make-eq-hashtable)) + (define next-id-number 0) + (define (id->sym i) + (let ([n (or (hashtable-ref id-number i #f) + (let ([n next-id-number]) + (hashtable-set! id-number i n) + (set! next-id-number (+ n 1)) + n))]) + (string->symbol (format "~a~s.~s" (id-prefix) (id-sym i) n)))) + + (define (rendered? v) (and (pair? v) (symbol? (car v)))) + + (define (vm-value->sexp v) + (cond + [(or (integer? v) (boolean? v) (string? v)) v] + [(and (pair? v) (not (rendered? v))) (map vm-value->sexp v)] + [(rendered? v) v] + [(null? v) '()] + [(VMop? v) + (VMop-case v + [(VMstack) '(stack)] + [(VMvoid) '(void)] + [(VMalign value bytes) `(align ,value ,bytes)] + [(VM+ x y) `(+ ,(vm-value->sexp x) ,(vm-value->sexp y))] + [(VMvalue->int x) `(value->int ,(vm-value->sexp x))] + ;; These operands are IR types, so expression serialization does not apply. + [(VMnull x) `(null ,(Type x))] + [(VMmax-sizeof x) `(max-sizeof ,(Type x))] + [(VMleaf-hash x) `(leaf-hash ,(vm-value->sexp x))] + [(VMcoin-commit coin recipient) + `(coin-commit ,(vm-value->sexp coin) ,(vm-value->sexp recipient))] + [(VMaligned-concat x*) `(aligned-concat ,@(map vm-value->sexp x*))] + [(VMstate-value-null) '(state-value null)] + [(VMstate-value-cell val) `(state-value cell ,(vm-value->sexp val))] + ;; Keep the type because it determines whether the value needs a cell. + [(VMstate-value-ADT val type) `(state-value ADT ,(vm-value->sexp val) ,(Type type))] + [(VMstate-value-array val*) `(state-value array ,@(map vm-value->sexp val*))] + [(VMstate-value-map key* val*) + `(state-value map ,@(map (lambda (k v) `(,(vm-value->sexp k) ,(vm-value->sexp v))) key* val*))] + [(VMstate-value-merkle-tree nat key* val*) + `(state-value merkle-tree ,nat + ,@(map (lambda (k v) `(,(vm-value->sexp k) ,(vm-value->sexp v))) key* val*))] + [else (fail "VM value" v)])] + [else (Expr v)])) + + (define (vm-suppressed? v) + (and (VMop? v) (VMop-case v [(VMsuppress) #t] [else #f]))) + + (define (vminstr->sexp vi) + (let ([args (vminstr-arg* vi)]) + (if (ormap (lambda (a) (vm-suppressed? (cdr a))) args) + #f + (let ([rendered + `(,(string->symbol (vminstr-op vi)) + ,@(map (lambda (a) `(,(string->symbol (car a)) ,(vm-value->sexp (cdr a)))) args))]) + ;; An ins with (void) has no runtime effect. + (if (and (eq? (car rendered) 'ins) + (member '(n (void)) (cdr rendered))) + #f + rendered))))) + + (define (instructions->sexp vminstr*) + (fold-right + (lambda (vi acc) (let ([s (vminstr->sexp vi)]) (if s (cons s acc) acc))) + '() + vminstr*)) + + (define (expand-ops src path-elt* adt-formal* adt-arg* var-name* expr* vm-code) + (instructions->sexp + (expand-vm-code src + (map (lambda (pe) + (nanopass-case (Lloweredemit Path-Element) pe + [,path-index (VMalign path-index 1)] + [(,src ,type ,expr) (Expr expr)])) + path-elt*) + #f + (append (map cons adt-formal* adt-arg*) + (map (lambda (vn ex) (cons (id-sym vn) (Expr ex))) var-name* expr*)) + (vm-code-code vm-code))))) + + (Ftype : Field-Type (ftype) -> * (sexp) + [(field-native) '(field-native)] + [(field-base ,ctype) `(field-base ,(Ctype ctype))] + [(field-scalar ,ctype) `(field-scalar ,(Ctype ctype))]) + + (Ctype : Curve-Type (ctype) -> * (sexp) + [(curve-jubjub) '(curve-jubjub)] + [(curve-secp256k1) '(curve-secp256k1)]) + + (Type : Type (type) -> * (sexp) + [(tboolean ,src) '(tboolean)] + [(tfield ,src ,ftype) `(tfield ,(Ftype ftype))] + [(tunsigned ,src ,nat) `(tunsigned ,nat)] + [(tpoint ,src ,ctype) `(tpoint ,(Ctype ctype))] + [(tbytes ,src ,len) `(tbytes ,len)] + [(topaque ,src ,opaque-type) `(topaque ,opaque-type)] + [(tvector ,src ,len ,type) `(tvector ,len ,(Type type))] + [(ttuple ,src ,type* ...) `(ttuple ,@(map Type type*))] + [(tstruct ,src ,struct-name (,elt-name* ,type*) ...) + `(tstruct ,struct-name ,@(map (lambda (n t) `(,n ,(Type t))) elt-name* type*))] + [(tenum ,src ,enum-name ,elt-name ,elt-name* ...) + `(tenum ,enum-name ,elt-name ,@elt-name*)] + [(talias ,src ,nominal? ,type-name ,type) + `(talias ,nominal? ,type-name ,(Type type))] + [(tcontract ,src ,contract-name (,elt-name* ,pure-dcl* (,type** ...) ,type*) ...) + `(tcontract ,contract-name + ,@(map (lambda (n p ts t) `(,n ,p ,(map Type ts) ,(Type t))) + elt-name* pure-dcl* type** type*))] + [(tadt ,src ,adt-name ([,adt-formal* ,adt-arg*] ...) ,vm-expr (,adt-op* ...) (,adt-rt-op* ...)) + `(,adt-name ,@(map AdtArg adt-arg*))] + [,tvar-name tvar-name] + [(tunknown) '(tunknown)] + [else (fail "type" (unparse-Lloweredemit type))]) + + (AdtArg : Public-Ledger-ADT-Arg (arg) -> * (sexp) + [,type (Type type)] + [,nat nat]) + + (TupleArg : Tuple-Argument (ta) -> * (sexp) + [(single ,src ,expr) `(single ,(Expr expr))] + [(spread ,src ,nat ,expr) `(spread ,nat ,(Expr expr))]) + + (MapArg : Map-Argument (ma) -> * (sexp) + [(,expr ,type ,type^) `(,(Expr expr) ,(Type type) ,(Type type^))]) + + (Fun : Function (fun) -> * (sexp) + [(fref ,src ,function-name) `(fref ,(id->sym function-name))] + [(circuit ,src (,arg* ...) ,type ,expr) + `(circuit ,(map Arg arg*) ,(Type type) ,(Expr expr))]) + + (Arg : Argument (arg) -> * (sexp) + [(,var-name ,type) `(,(id->sym var-name) ,(Type type))]) + + (Expr : Expression (expr) -> * (sexp) + [(quote ,src ,datum) `(quote ,datum)] + [(var-ref ,src ,var-name) `(var-ref ,(id->sym var-name))] + [(default ,src ,type) `(default ,(Type type))] + [(if ,src ,expr0 ,expr1 ,expr2) + `(if ,(Expr expr0) ,(Expr expr1) ,(Expr expr2))] + [(elt-ref ,src ,expr ,elt-name ,nat) `(elt-ref ,(Expr expr) ,elt-name ,nat)] + [(enum-ref ,src ,type ,elt-name) `(enum-ref ,(Type type) ,elt-name)] + [(tuple ,src ,tuple-arg* ...) `(tuple ,@(map TupleArg tuple-arg*))] + [(vector ,src ,tuple-arg* ...) `(vector ,@(map TupleArg tuple-arg*))] + [(tuple-ref ,src ,expr ,kindex) `(tuple-ref ,(Expr expr) ,kindex)] + [(tuple-slice ,src ,type ,expr ,kindex ,len) + `(tuple-slice ,(Type type) ,(Expr expr) ,kindex ,len)] + [(vector-ref ,src ,type ,expr ,index) + `(vector-ref ,(Type type) ,(Expr expr) ,(Expr index))] + [(vector-slice ,src ,type ,expr ,index ,len) + `(vector-slice ,(Type type) ,(Expr expr) ,(Expr index) ,len)] + [(bytes-ref ,src ,type ,expr ,index) + `(bytes-ref ,(Type type) ,(Expr expr) ,(Expr index))] + [(bytes-slice ,src ,type ,expr ,index ,len) + `(bytes-slice ,(Type type) ,(Expr expr) ,(Expr index) ,len)] + [(+ ,src ,type ,expr1 ,expr2) `(+ ,(Type type) ,(Expr expr1) ,(Expr expr2))] + [(- ,src ,type ,expr1 ,expr2) `(- ,(Type type) ,(Expr expr1) ,(Expr expr2))] + [(* ,src ,type ,expr1 ,expr2) `(* ,(Type type) ,(Expr expr1) ,(Expr expr2))] + [(< ,src ,bits ,expr1 ,expr2) `(< ,bits ,(Expr expr1) ,(Expr expr2))] + [(<= ,src ,bits ,expr1 ,expr2) `(<= ,bits ,(Expr expr1) ,(Expr expr2))] + [(> ,src ,bits ,expr1 ,expr2) `(> ,bits ,(Expr expr1) ,(Expr expr2))] + [(>= ,src ,bits ,expr1 ,expr2) `(>= ,bits ,(Expr expr1) ,(Expr expr2))] + [(== ,src ,type ,expr1 ,expr2) `(== ,(Type type) ,(Expr expr1) ,(Expr expr2))] + [(!= ,src ,type ,expr1 ,expr2) `(!= ,(Type type) ,(Expr expr1) ,(Expr expr2))] + [(map ,src ,len ,fun ,map-arg ,map-arg* ...) + `(map ,len ,(Fun fun) ,@(map MapArg (cons map-arg map-arg*)))] + [(fold ,src ,len ,fun (,expr0 ,type0) ,map-arg ,map-arg* ...) + `(fold ,len ,(Fun fun) (,(Expr expr0) ,(Type type0)) + ,@(map MapArg (cons map-arg map-arg*)))] + [(call ,src ,function-name ,expr* ...) + `(call ,(id->sym function-name) ,@(map Expr expr*))] + [(new ,src ,type ,expr* ...) + `(new ,(Type type) ,@(map Expr expr*))] + [(seq ,src ,expr* ... ,expr) + `(seq ,@(map Expr expr*) ,(Expr expr))] + [(let* ,src ([,local* ,expr*] ...) ,expr) + `(let* ,(map (lambda (l e) `(,(Arg l) ,(Expr e))) local* expr*) + ,(Expr expr))] + [(assert ,src ,expr ,mesg) `(assert ,(Expr expr) ,mesg)] + [(field->bytes ,src ,len ,ftype ,expr) + `(field->bytes ,len ,(Ftype ftype) ,(Expr expr))] + [(cast-from-bytes ,src ,type ,len ,expr) + `(cast-from-bytes ,(Type type) ,len ,(Expr expr))] + [(vector->bytes ,src ,len ,expr) `(vector->bytes ,len ,(Expr expr))] + [(bytes->vector ,src ,len ,expr) `(bytes->vector ,len ,(Expr expr))] + [(cast-from-enum ,src ,type ,type^ ,expr) + `(cast-from-enum ,(Type type) ,(Type type^) ,(Expr expr))] + [(cast-to-enum ,src ,type ,type^ ,expr) + `(cast-to-enum ,(Type type) ,(Type type^) ,(Expr expr))] + [(cast-to-field ,src ,ftype ,type ,expr) + `(cast-to-field ,(Ftype ftype) ,(Type type) ,(Expr expr))] + [(cast-from-field ,src ,nat ,ftype ,expr) + `(cast-from-field ,nat ,(Ftype ftype) ,(Expr expr))] + [(safe-cast ,src ,type ,type^ ,expr) + `(safe-cast ,(Type type) ,(Type type^) ,(Expr expr))] + [(downcast-unsigned ,src ,nat2 ,nat1 ,expr) + `(downcast-unsigned ,nat2 ,nat1 ,(Expr expr))] + [(contract-call ,src ,elt-name (,expr ,type) ,expr* ...) + `(contract-call ,elt-name (,(Expr expr) ,(Type type)) + ,@(map Expr expr*))] + [(emit ,src ,event-version ,event-tag ,len ,expr ,vm-code) + `(emit ,event-version ,event-tag ,len ,(Expr expr) + (instructions + ,@(instructions->sexp + (expand-vm-code src #f #f + `((emit-version . ,event-version) + (emit-tag . ,event-tag) + (emit-payload . ,(Expr expr))) + (vm-code-code vm-code)))))] + [(public-ledger ,src ,ledger-field-name ,sugar (,path-elt* ...) ,src^ ,adt-op ,expr* ...) + (nanopass-case (Lloweredemit ADT-Op) adt-op + [(,ledger-op ,op-class (,adt-name (,adt-formal* ,adt-arg*) ...) ((,var-name* ,type*) ...) ,type ,vm-code) + `(public-ledger ,(id->sym ledger-field-name) + ,(map (lambda (pe) + (nanopass-case (Lloweredemit Path-Element) pe + [,path-index path-index] + [(,src ,type ,expr) `(,(Type type) ,(Expr expr))])) + path-elt*) + ,ledger-op + ,(Type type) + (instructions ,@(expand-ops src path-elt* adt-formal* adt-arg* var-name* expr* vm-code)) + ,@(map Expr expr*))])] + [(return ,src ,expr) `(return ,(Expr expr))] + [else (fail "expression" (unparse-Lloweredemit expr))]) + + (Pelt : Program-Element (pelt proof-id*) -> * (sexp) + [(circuit ,src ,function-name (,arg* ...) ,type ,expr) + `(circuit ,(id->sym function-name) + (exported ,(id-exported? function-name)) + (pure ,(id-pure? function-name)) + (proof ,(and (memq function-name proof-id*) #t)) + ,(map Arg arg*) + ,(Type type) + ,(Expr expr))] + [(native ,src ,function-name ,native-entry (,arg* ...) ,type) + `(native ,(id->sym function-name) + (entry ,(native-entry-function native-entry) ,(native-entry-class native-entry)) + ,(map Arg arg*) + ,(Type type))] + [(witness ,src ,function-name (,arg* ...) ,type) + `(witness ,(id->sym function-name) ,(map Arg arg*) ,(Type type))] + [(kernel-declaration ,public-binding) + `(kernel-declaration ,(Binding public-binding))] + [(public-ledger-declaration ,pl-array ,lconstructor) + `(public-ledger-declaration + ,(PlArray pl-array) + ,(nanopass-case (Lloweredemit Ledger-Constructor) lconstructor + [(constructor ,src (,arg* ...) ,expr) + `(constructor ,(map Arg arg*) ,(Expr expr))]))] + [(export-typedef ,src ,type-name (,tvar-name* ...) ,type) + `(export-typedef ,type-name ,tvar-name* ,(Type type))] + [else (fail "program element" (unparse-Lloweredemit pelt))]) + + (Binding : Public-Ledger-Binding (pb) -> * (sexp) + [(,src ,ledger-field-name (,path-index* ...) ,type) + `(,(id->sym ledger-field-name) + ,path-index* + (exported ,(id-exported? ledger-field-name)) + ,(Type type))]) + + (PlArray : Public-Ledger-Array (pl-array) -> * (sexp) + [(public-ledger-array ,pl-array-elt* ...) + `(public-ledger-array + ,@(map (lambda (elt) + (nanopass-case (Lloweredemit Public-Ledger-Array-Element) elt + [,pl-array (PlArray pl-array)] + [,public-binding (Binding public-binding)])) + pl-array-elt*))]) + + (Program : Program (ir) -> * (sexp) + [(program ,src (,contract-type* ...) ((,export-name* ,name*) ...) ,pelt* ...) + ;; Resolve proof export names through the export table because selective exports can rename circuits. + ;; Keep the id records because id-uniq assigns numbers on first print. + (let ([proof-id* + (fold-left + (lambda (acc export-name name) + (if (memq export-name proof-circuit-name*) (cons name acc) acc)) + '() + export-name* + name*)]) + `(analyzed-ir + (compiler-version ,compiler-version-string) + (language-version ,language-version-string) + (runtime-version ,runtime-version-string) + (exports ,@(map (lambda (en n) `(,en . ,(id->sym n))) export-name* name*)) + (contract-types ,@(map Type contract-type*)) + ,@(map (lambda (pelt) (Pelt pelt proof-id*)) pelt*)))]) + + (Program ir)) diff --git a/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss b/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss new file mode 100644 index 000000000..0dbc77d64 --- /dev/null +++ b/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss @@ -0,0 +1,22 @@ +;;; This file is part of Compact. +;;; Copyright (C) 2025 Midnight Foundation +;;; SPDX-License-Identifier: Apache-2.0 +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +#!chezscheme + +(define (save-analyzed-ir ir proof-circuit-name*) + (let ([op (get-target-port 'analyzed-ir.sexp)]) + (parameterize ([print-brackets #f]) + (pretty-print (extract-analyzed-ir ir proof-circuit-name*) op))) + ir) diff --git a/tests-e2e/src/resources/compiler_man_page.txt b/tests-e2e/src/resources/compiler_man_page.txt index db59a6041..46c071e7f 100644 --- a/tests-e2e/src/resources/compiler_man_page.txt +++ b/tests-e2e/src/resources/compiler_man_page.txt @@ -56,3 +56,7 @@ 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. + + --analyzed-ir writes compiler/analyzed-ir.sexp in the target directory. + It contains the analyzed program, with ledger operations and emits expanded + into Impact VM instructions. Its format can change between compiler versions. From 512fd3275e8db812f869663fd4773e9e7514d3b1 Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Tue, 18 Aug 2026 12:39:26 +0100 Subject: [PATCH 2/5] Carry the operation class and the native type arguments Signed-off-by: Rodrigo Quelhas --- .../extract-analyzed-ir.ss | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss b/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss index c93871590..38e30ec41 100644 --- a/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss +++ b/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss @@ -29,6 +29,24 @@ n))]) (string->symbol (format "~a~s.~s" (id-prefix) (id-sym i) n)))) + ;; Scan the result because a type parameter can occur there without appearing in an argument. + (define (native-type-argument* native-entry arg* type) + (let ([seen (make-hashtable symbol-hash eq?)]) + (fold-right + (lambda (maybe-type-param type acc) + (if (and maybe-type-param (not (hashtable-contains? seen maybe-type-param))) + (begin + (hashtable-set! seen maybe-type-param #t) + (cons (Type type) acc)) + acc)) + '() + (native-entry-maybe-type-param* native-entry) + (append (map (lambda (a) + (nanopass-case (Lloweredemit Argument) a + [(,var-name ,type) type])) + arg*) + (list type))))) + (define (rendered? v) (and (pair? v) (symbol? (car v)))) (define (vm-value->sexp v) @@ -133,6 +151,14 @@ [(tunknown) '(tunknown)] [else (fail "type" (unparse-Lloweredemit type))]) + ;; A coin-check class names the argument positions the check reads, and the + ;; check is a runtime call rather than a VM instruction, so the instruction + ;; list cannot carry it. + (OpClass : ADT-Op-Class (op-class) -> * (sexp) + [,ledger-op-class ledger-op-class] + [(,ledger-op-class ,nat ,nat^) `(,ledger-op-class ,nat ,nat^)] + [else (fail "operation class" op-class)]) + (AdtArg : Public-Ledger-ADT-Arg (arg) -> * (sexp) [,type (Type type)] [,nat nat]) @@ -231,6 +257,7 @@ (nanopass-case (Lloweredemit ADT-Op) adt-op [(,ledger-op ,op-class (,adt-name (,adt-formal* ,adt-arg*) ...) ((,var-name* ,type*) ...) ,type ,vm-code) `(public-ledger ,(id->sym ledger-field-name) + ,(OpClass op-class) ,(map (lambda (pe) (nanopass-case (Lloweredemit Path-Element) pe [,path-index path-index] @@ -255,6 +282,7 @@ [(native ,src ,function-name ,native-entry (,arg* ...) ,type) `(native ,(id->sym function-name) (entry ,(native-entry-function native-entry) ,(native-entry-class native-entry)) + (type-arguments ,@(native-type-argument* native-entry arg* type)) ,(map Arg arg*) ,(Type type))] [(witness ,src ,function-name (,arg* ...) ,type) From 7ee5433c0948a9d25711ef485744448430e5aadd Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Tue, 18 Aug 2026 13:41:16 +0100 Subject: [PATCH 3/5] Cover the analyzed-IR artifact in the compiler tests Signed-off-by: Rodrigo Quelhas --- compiler/test.ss | 167 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 166 insertions(+), 1 deletion(-) diff --git a/compiler/test.ss b/compiler/test.ss index 27e5215b7..d2d56edf5 100644 --- a/compiler/test.ss +++ b/compiler/test.ss @@ -339,7 +339,8 @@ groups than for single tests. (reverse feedback*))) (define (go source-fn target-fn okay?* code) (fluid-let ([successes '()] [positive-feedback* '()] [negative-feedback* '()] [condition-recorded? #f]) - (parameterize ([id-counter 0] [warning-conditions '()]) + (parameterize ([id-counter 0] [warning-conditions '()] + [write-analyzed-ir (eq? pass-name 'save-analyzed-ir)]) (let-values ([(result pretty-formats) (parameterize ([pending-conditions '()]) (guard (c [else (values @@ -92463,5 +92464,169 @@ groups than for single tests. ) ) +(run-tests save-analyzed-ir + (test-group + ((create-file "Registry.compact" + '( + "module Registry {" + " import CompactStandardLibrary;" + "" + " export ledger members: Set;" + " export ledger count: Counter;" + "" + " export circuit add(m: T): [] {" + " members.insert(disclose(m));" + " count.increment(1);" + " }" + "" + " export pure circuit atLeast(need: Uint<8>, have: Uint<8>): Boolean {" + " return have >= need;" + " }" + "}" + )) + (succeeds)) + ((create-file "testfile.compact" + '( + "pragma language_version >= 0.23;" + "import CompactStandardLibrary;" + "import Registry> prefix reg_;" + "" + "export { reg_add, reg_atLeast };" + "" + "export ledger digest: Bytes<32>;" + "export ledger vault: QualifiedShieldedCoinInfo;" + "" + "export circuit record(x: Field): [] {" + " digest = disclose(persistentHash(x));" + "}" + "" + "export circuit stash(coin: ShieldedCoinInfo," + " r: Either): [] {" + " vault.writeCoin(disclose(coin), disclose(r));" + "}" + "" + "export circuit pause(): [] {" + " emit(Paused {});" + "}" + )) + ; Keep the version lines dynamic because version strings change. + (output-file "compiler/testdir/testfile/compiler/analyzed-ir.sexp" + `( + ,(format "(analyzed-ir (compiler-version ~s) (language-version ~s)" + compiler-version-string language-version-string) + ,(format " (runtime-version ~s)" runtime-version-string) + " (exports (digest . %digest.5) (pause . %pause.6)" + " (record . %record.3) (reg_add . %add.4)" + " (reg_atLeast . %atLeast.1) (stash . %stash.2)" + " (vault . %vault.0))" + " (contract-types)" + " (kernel-declaration (%kernel.19 () (exported #f) (Kernel)))" + " (public-ledger-declaration" + " (public-ledger-array" + " (%members.16 (0) (exported #f) (Set (tbytes 32)))" + " (%count.18 (1) (exported #f) (Counter))" + " (%digest.5 (2) (exported #t) (__compact_Cell (tbytes 32)))" + " (%vault.0" + " (3)" + " (exported #t)" + " (__compact_Cell" + " (tstruct QualifiedShieldedCoinInfo (nonce (tbytes 32))" + " (color (tbytes 32))" + " (value (tunsigned 340282366920938463463374607431768211455))" + " (mt_index (tunsigned 18446744073709551615))))))" + " (constructor () (tuple)))" + " (native %persistentHash.13" + " (entry \"__compactRuntime.persistentHash\" circuit)" + " (type-arguments (tfield (field-native)))" + " ((%value.14 (tfield (field-native)))) (tbytes 32))" + " (circuit %add.4 (exported #t) (pure #f) (proof #t)" + " ((%m.15 (tbytes 32))) (ttuple)" + " (seq (public-ledger %members.16 update (0) insert (ttuple)" + " (instructions (idx (cached #f) (pushPath #t) (path ((align 0 1))))" + " (push" + " (storage #f)" + " (value (state-value cell (var-ref %m.15))))" + " (push (storage #t) (value (state-value null)))" + " (ins (cached #f) (n 1)) (ins (cached #t) (n 1)))" + " (var-ref %m.15))" + " (let* (((%tmp.17 (tunsigned 65535)) (safe-cast" + " (tunsigned 65535)" + " (tunsigned 1)" + " '1)))" + " (public-ledger %count.18 update (1) increment (ttuple)" + " (instructions" + " (idx (cached #f) (pushPath #t) (path ((align 1 1))))" + " (addi (immediate (value->int (var-ref %tmp.17))))" + " (ins (cached #t) (n 1)))" + " (var-ref %tmp.17)))" + " (return (tuple))))" + " (circuit %atLeast.1 (exported #t) (pure #t) (proof #f)" + " ((%need.9 (tunsigned 255)) (%have.10 (tunsigned 255)))" + " (tboolean)" + " (return (>= 8 (var-ref %have.10) (var-ref %need.9))))" + " (circuit %record.3 (exported #t) (pure #f) (proof #t)" + " ((%x.11 (tfield (field-native)))) (ttuple)" + " (seq (let* (((%tmp.12 (tbytes 32)) (call" + " %persistentHash.13" + " (var-ref %x.11))))" + " (public-ledger %digest.5 write (2) write (ttuple)" + " (instructions" + " (push (storage #f) (value (state-value cell (align 2 1))))" + " (push" + " (storage #t)" + " (value (state-value cell (var-ref %tmp.12))))" + " (ins (cached #f) (n 1)))" + " (var-ref %tmp.12)))" + " (return (tuple))))" + " (circuit %stash.2 (exported #t) (pure #f) (proof #t)" + " ((%coin.7" + " (tstruct" + " ShieldedCoinInfo" + " (nonce (tbytes 32))" + " (color (tbytes 32))" + " (value" + " (tunsigned 340282366920938463463374607431768211455))))" + " (%r.8" + " (tstruct" + " Either" + " (is_left (tboolean))" + " (left (tstruct ZswapCoinPublicKey (bytes (tbytes 32))))" + " (right (tstruct ContractAddress (bytes (tbytes 32)))))))" + " (ttuple)" + " (seq (public-ledger %vault.0 (update-with-coin-check 0 1) (3) writeCoin" + " (ttuple)" + " (instructions (push (storage #f) (value (state-value cell (align 3 1))))" + " (dup (n 3))" + " (push" + " (storage #f)" + " (value" + " (state-value" + " cell" + " (coin-commit (var-ref %coin.7) (var-ref %r.8)))))" + " (idx (cached #t) (pushPath #f) (path ((align 1 1) (stack))))" + " (push" + " (storage #f)" + " (value (state-value cell (var-ref %coin.7))))" + " (swap (n 0)) (concat (cached #t) (n 91))" + " (ins (cached #f) (n 1)))" + " (var-ref %coin.7) (var-ref %r.8))" + " (return (tuple))))" + " (circuit %pause.6 (exported #t) (pure #f) (proof #t) () (ttuple)" + " (seq (emit 1 8 0 '#vu8()" + " (instructions" + " (push" + " (storage #f)" + " (value" + " (state-value" + " array" + " (state-value cell (align 1 4))" + " (state-value cell (align 8 1))" + " (state-value cell '#vu8()))))" + " (log)))" + " (return (tuple)))))" + )) + )) +) + (run-javascript) ) From 3eccccf59787417fcfb735439436f624c78ead16 Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Tue, 18 Aug 2026 15:57:48 +0100 Subject: [PATCH 4/5] Print types with the language's own unparser Signed-off-by: Rodrigo Quelhas --- .../extract-analyzed-ir.ss | 44 ++----------------- 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss b/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss index 38e30ec41..fca4876d2 100644 --- a/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss +++ b/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss @@ -47,6 +47,9 @@ arg*) (list type))))) + (define (Ftype ftype) (unparse-Lloweredemit ftype)) + (define (Type type) (unparse-Lloweredemit type)) + (define (rendered? v) (and (pair? v) (symbol? (car v)))) (define (vm-value->sexp v) @@ -117,52 +120,11 @@ (map (lambda (vn ex) (cons (id-sym vn) (Expr ex))) var-name* expr*)) (vm-code-code vm-code))))) - (Ftype : Field-Type (ftype) -> * (sexp) - [(field-native) '(field-native)] - [(field-base ,ctype) `(field-base ,(Ctype ctype))] - [(field-scalar ,ctype) `(field-scalar ,(Ctype ctype))]) - - (Ctype : Curve-Type (ctype) -> * (sexp) - [(curve-jubjub) '(curve-jubjub)] - [(curve-secp256k1) '(curve-secp256k1)]) - - (Type : Type (type) -> * (sexp) - [(tboolean ,src) '(tboolean)] - [(tfield ,src ,ftype) `(tfield ,(Ftype ftype))] - [(tunsigned ,src ,nat) `(tunsigned ,nat)] - [(tpoint ,src ,ctype) `(tpoint ,(Ctype ctype))] - [(tbytes ,src ,len) `(tbytes ,len)] - [(topaque ,src ,opaque-type) `(topaque ,opaque-type)] - [(tvector ,src ,len ,type) `(tvector ,len ,(Type type))] - [(ttuple ,src ,type* ...) `(ttuple ,@(map Type type*))] - [(tstruct ,src ,struct-name (,elt-name* ,type*) ...) - `(tstruct ,struct-name ,@(map (lambda (n t) `(,n ,(Type t))) elt-name* type*))] - [(tenum ,src ,enum-name ,elt-name ,elt-name* ...) - `(tenum ,enum-name ,elt-name ,@elt-name*)] - [(talias ,src ,nominal? ,type-name ,type) - `(talias ,nominal? ,type-name ,(Type type))] - [(tcontract ,src ,contract-name (,elt-name* ,pure-dcl* (,type** ...) ,type*) ...) - `(tcontract ,contract-name - ,@(map (lambda (n p ts t) `(,n ,p ,(map Type ts) ,(Type t))) - elt-name* pure-dcl* type** type*))] - [(tadt ,src ,adt-name ([,adt-formal* ,adt-arg*] ...) ,vm-expr (,adt-op* ...) (,adt-rt-op* ...)) - `(,adt-name ,@(map AdtArg adt-arg*))] - [,tvar-name tvar-name] - [(tunknown) '(tunknown)] - [else (fail "type" (unparse-Lloweredemit type))]) - - ;; A coin-check class names the argument positions the check reads, and the - ;; check is a runtime call rather than a VM instruction, so the instruction - ;; list cannot carry it. (OpClass : ADT-Op-Class (op-class) -> * (sexp) [,ledger-op-class ledger-op-class] [(,ledger-op-class ,nat ,nat^) `(,ledger-op-class ,nat ,nat^)] [else (fail "operation class" op-class)]) - (AdtArg : Public-Ledger-ADT-Arg (arg) -> * (sexp) - [,type (Type type)] - [,nat nat]) - (TupleArg : Tuple-Argument (ta) -> * (sexp) [(single ,src ,expr) `(single ,(Expr expr))] [(spread ,src ,nat ,expr) `(spread ,nat ,(Expr expr))]) From 39521ed3318ea675765c44f48766c2ef5d7233db Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Tue, 18 Aug 2026 16:39:35 +0100 Subject: [PATCH 5/5] Number identifiers with the compiler's own counter Signed-off-by: Rodrigo Quelhas --- .../extract-analyzed-ir.ss | 14 ++------- compiler/test.ss | 30 +++++++++---------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss b/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss index fca4876d2..c64d7d58a 100644 --- a/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss +++ b/compiler/save-analyzed-ir-passes/extract-analyzed-ir.ss @@ -19,15 +19,7 @@ (definitions (define (fail what x) (internal-errorf 'save-analyzed-ir "unsupported ~a: ~s" what x)) - (define id-number (make-eq-hashtable)) - (define next-id-number 0) - (define (id->sym i) - (let ([n (or (hashtable-ref id-number i #f) - (let ([n next-id-number]) - (hashtable-set! id-number i n) - (set! next-id-number (+ n 1)) - n))]) - (string->symbol (format "~a~s.~s" (id-prefix) (id-sym i) n)))) + (define (id->sym i) (string->symbol (format "~a" i))) ;; Scan the result because a type parameter can occur there without appearing in an argument. (define (native-type-argument* native-entry arg* type) @@ -49,6 +41,7 @@ (define (Ftype ftype) (unparse-Lloweredemit ftype)) (define (Type type) (unparse-Lloweredemit type)) + (define (Arg arg) (unparse-Lloweredemit arg)) (define (rendered? v) (and (pair? v) (symbol? (car v)))) @@ -137,9 +130,6 @@ [(circuit ,src (,arg* ...) ,type ,expr) `(circuit ,(map Arg arg*) ,(Type type) ,(Expr expr))]) - (Arg : Argument (arg) -> * (sexp) - [(,var-name ,type) `(,(id->sym var-name) ,(Type type))]) - (Expr : Expression (expr) -> * (sexp) [(quote ,src ,datum) `(quote ,datum)] [(var-ref ,src ,var-name) `(var-ref ,(id->sym var-name))] diff --git a/compiler/test.ss b/compiler/test.ss index d2d56edf5..a82a6966a 100644 --- a/compiler/test.ss +++ b/compiler/test.ss @@ -92520,11 +92520,11 @@ groups than for single tests. " (reg_atLeast . %atLeast.1) (stash . %stash.2)" " (vault . %vault.0))" " (contract-types)" - " (kernel-declaration (%kernel.19 () (exported #f) (Kernel)))" + " (kernel-declaration (%kernel.18 () (exported #f) (Kernel)))" " (public-ledger-declaration" " (public-ledger-array" - " (%members.16 (0) (exported #f) (Set (tbytes 32)))" - " (%count.18 (1) (exported #f) (Counter))" + " (%members.14 (0) (exported #f) (Set (tbytes 32)))" + " (%count.16 (1) (exported #f) (Counter))" " (%digest.5 (2) (exported #t) (__compact_Cell (tbytes 32)))" " (%vault.0" " (3)" @@ -92535,13 +92535,13 @@ groups than for single tests. " (value (tunsigned 340282366920938463463374607431768211455))" " (mt_index (tunsigned 18446744073709551615))))))" " (constructor () (tuple)))" - " (native %persistentHash.13" + " (native %persistentHash.11" " (entry \"__compactRuntime.persistentHash\" circuit)" " (type-arguments (tfield (field-native)))" - " ((%value.14 (tfield (field-native)))) (tbytes 32))" + " ((%value.19 (tfield (field-native)))) (tbytes 32))" " (circuit %add.4 (exported #t) (pure #f) (proof #t)" " ((%m.15 (tbytes 32))) (ttuple)" - " (seq (public-ledger %members.16 update (0) insert (ttuple)" + " (seq (public-ledger %members.14 update (0) insert (ttuple)" " (instructions (idx (cached #f) (pushPath #t) (path ((align 0 1))))" " (push" " (storage #f)" @@ -92553,7 +92553,7 @@ groups than for single tests. " (tunsigned 65535)" " (tunsigned 1)" " '1)))" - " (public-ledger %count.18 update (1) increment (ttuple)" + " (public-ledger %count.16 update (1) increment (ttuple)" " (instructions" " (idx (cached #f) (pushPath #t) (path ((align 1 1))))" " (addi (immediate (value->int (var-ref %tmp.17))))" @@ -92561,22 +92561,22 @@ groups than for single tests. " (var-ref %tmp.17)))" " (return (tuple))))" " (circuit %atLeast.1 (exported #t) (pure #t) (proof #f)" - " ((%need.9 (tunsigned 255)) (%have.10 (tunsigned 255)))" + " ((%need.10 (tunsigned 255)) (%have.9 (tunsigned 255)))" " (tboolean)" - " (return (>= 8 (var-ref %have.10) (var-ref %need.9))))" + " (return (>= 8 (var-ref %have.9) (var-ref %need.10))))" " (circuit %record.3 (exported #t) (pure #f) (proof #t)" - " ((%x.11 (tfield (field-native)))) (ttuple)" - " (seq (let* (((%tmp.12 (tbytes 32)) (call" - " %persistentHash.13" - " (var-ref %x.11))))" + " ((%x.12 (tfield (field-native)))) (ttuple)" + " (seq (let* (((%tmp.13 (tbytes 32)) (call" + " %persistentHash.11" + " (var-ref %x.12))))" " (public-ledger %digest.5 write (2) write (ttuple)" " (instructions" " (push (storage #f) (value (state-value cell (align 2 1))))" " (push" " (storage #t)" - " (value (state-value cell (var-ref %tmp.12))))" + " (value (state-value cell (var-ref %tmp.13))))" " (ins (cached #f) (n 1)))" - " (var-ref %tmp.12)))" + " (var-ref %tmp.13)))" " (return (tuple))))" " (circuit %stash.2 (exported #t) (pure #f) (proof #t)" " ((%coin.7"