codegen

package
v1.2.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

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.

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.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

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.

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.

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.

SPDX-License-Identifier: Apache-2.0

Index

Constants

View Source
const DEFAULT_MAX_STATIC_DEPTH uint = 65536

DEFAULT_MAX_STATIC_DEPTH is the default maximum depth (i.e. number of rows) of static tables, used when no override is supplied. It is 2^16.

Variables

View Source
var DEFAULT_CONFIG = Config{
	// contains filtered or unexported fields
}

DEFAULT_CONFIG is the configuration used when no overrides are supplied. Vectorisation is enabled, which matches the behaviour expected by the downstream prover; callers wanting to disable individual passes (for debugging, for example) should derive a custom Config via the chainable setters below.

Functions

func BitwiseAnd

func BitwiseAnd[W vm.Word[W]](values ...W) W

BitwiseAnd computes the bitwise AND of a set of words.

func BitwiseOr

func BitwiseOr[W vm.Word[W]](values ...W) W

BitwiseOr computes the bitwise OR of a set of words.

func BitwiseShl

func BitwiseShl[W vm.Word[W]](bitwidth uint, values ...W) W

BitwiseShl computes a left-shift chain over a set of words.

func BitwiseShr

func BitwiseShr[W vm.Word[W]](values ...W) W

BitwiseShr computes a right-shift chain over a set of words.

func BitwiseXor

func BitwiseXor[W vm.Word[W]](values ...W) W

BitwiseXor computes the bitwise XOR of a set of words.

func Product

func Product[W vm.Word[W]](bitwidth uint, values ...W) (W, bool)

Product mulitplies a given set of words together.

func Quotient

func Quotient[W vm.Word[W]](values ...W) W

Quotient divides a sequence of words left-to-right.

func Remainder

func Remainder[W vm.Word[W]](values ...W) W

Remainder computes the remainder of dividing a sequence of words left-to-right.

func Subtract

func Subtract[W vm.Word[W]](bitwidth uint, values ...W) (W, bool)

Subtract a given set of words together, producing the difference and an underflow indicator.

func Sum

func Sum[W vm.Word[W]](bitwidth uint, values ...W) (W, bool)

Sum a given set of words together.

Types

type Bytecode added in v1.2.21

type Bytecode = vm.Bytecode[vm.Uint]

Bytecode provides a convenient alias for a single bytecode instruction emitted by codegen.

type BytecodeVector added in v1.2.21

type BytecodeVector = vm.BytecodeVector[vm.Uint]

BytecodeVector provides a convenient alias for a vector (trace line) of bytecode instructions.

type Compiler

type Compiler struct {
	// contains filtered or unexported fields
}

Compiler is responsible for compiling high-level programs into low-level machines which can be used (for example) to execute this program with some given inputs. A compile is configurable in certain aspects.

func NewCompiler

func NewCompiler(cfg Config, env data.ResolvedEnvironment, srcmaps source.Maps[any]) *Compiler

NewCompiler constructs a code generator parameterised by a configuration, the resolved type environment, and the source maps recorded by earlier pipeline stages. The configuration controls optional passes such as vectorisation; cfg=DEFAULT_CONFIG matches the prover-facing defaults. The environment supplies type information needed when lowering expressions (e.g. bit-widths of named types), and the source maps allow generated instructions and any errors raised during compilation to be tied back to their originating source positions.

func (*Compiler) Compile

func (p *Compiler) Compile(declarations []Declaration) (vm.Program[vm.Uint], []source.SyntaxError)

Compile attempts to compile a given high-level program into a low-level machine which can be used (for example) to execute this program with some given inputs.

type Condition

type Condition = expr.Condition[symbol.Resolved]

Condition is a convenient alias

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config captures the tunable aspects of the ZkC code generator. Instances are immutable: each setter (e.g. Vectorize) returns a new Config rather than mutating the receiver, so a Config can be safely shared between concurrent compilations.

func (Config) FastMode

func (p Config) FastMode(flag bool) Config

FastMode returns a copy of this Config with fast-mode execution enabled (flag=true) or disabled (flag=false).

func (Config) Field

func (p Config) Field(field field.Config) Config

Field sets the target field configuration to use for this compiler.

func (Config) GetField

func (p Config) GetField() field.Config

GetField returns the specified field configuration.

func (Config) GetMaxStaticDepth added in v1.2.21

func (p Config) GetMaxStaticDepth() uint

GetMaxStaticDepth returns the maximum depth (ie nb of rows) of static tables.

func (Config) Inlining

func (p Config) Inlining(flag bool) Config

Inlining returns a copy of this Config in which function inlining is either enabled (flag=true) or disabled (flag=false). When enabled, functions marked with the #[inline] annotation are inlined at their call sites.

func (Config) IsFastMode added in v1.2.21

func (p Config) IsFastMode() bool

IsFastMode determines whether or not fast mode is enabled.

func (Config) MaxStaticDepth added in v1.2.21

func (p Config) MaxStaticDepth(depth uint) Config

MaxStaticDepth sets the maximum depth (ie nb of rows) of static tables.

func (Config) SplitRegisters

func (p Config) SplitRegisters(flag bool) Config

SplitRegisters returns a copy of this Config in which register splitting is either enabled (flag=true) or disabled (flag=false).

func (Config) Verbose added in v1.2.22

func (p Config) Verbose(flag bool) Config

Verbose returns a copy of this Config where printf statements are emitted during code generation when flag=true, and skipped otherwise.

func (Config) Word added in v1.2.21

func (p Config) Word(word vm.WordConfig) Config

Word sets the target word configuration to use for this compiler.

type ConstantEvaluator

type ConstantEvaluator struct {
	// contains filtered or unexported fields
}

ConstantEvaluator provides machinery for evaluating compile-time constant expression using the provided declaration list and type environment. It is used during function code generation and when initialising static memory contents, and also during typing (for array type size expressions). As a result of the latter, it must be robust against error. That is, it may be called on a malformed expression and, hence, it must handle this gracefully.

func NewConstantEvaluator

func NewConstantEvaluator(field field.Config, env data.ResolvedEnvironment, declarations ...Declaration,
) ConstantEvaluator

NewConstantEvaluator constructs a new constant evaluator.

func (ConstantEvaluator) Eval

func (p ConstantEvaluator) Eval(e Expr, definition bool) (res vm.Uint, errorMessage string)

Eval attempts to evaluate a given expression to a constant.

type Declaration

type Declaration = decl.Declaration[symbol.Resolved]

Declaration represents a declaration which can contain macro instructions and where external identifiers are otherwise resolved. As such, it should not be possible that such a declaration refers to unknown (or otherwise incorrect) external components.

type Expr

type Expr = expr.Expr[symbol.Resolved]

Expr is a convenient alias

type LVal

type LVal = lval.LVal[symbol.Resolved]

LVal is a convenient alias

type MultiTranslator

type MultiTranslator[T any] = func(T, []uint, []RegisterId) []Bytecode

MultiTranslator is for multi-way instructions which cannot target a vector instruction.

type RegisterId added in v1.2.21

type RegisterId = vm.RegisterId

RegisterId is the register identifier used throughout codegen. It is the bytecode-layer RegisterId, so compiled instructions need no register-type conversion.

type Stmt

type Stmt = stmt.Stmt[symbol.Resolved]

Stmt is a convenient alias

type StmtCompiler

type StmtCompiler struct {
	// contains filtered or unexported fields
}

StmtCompiler provides a working environment for compiling individual statements within a given function. For example, it provides the ability to allocate new temporary registers as required.

type UnitTranslator

type UnitTranslator[T any] = func(T, uint, []uint, RegisterId) []Bytecode

UnitTranslator is for unit instructions which cannot target a vector instruction.

type VariableDescriptor

type VariableDescriptor = variable.Descriptor[symbol.Resolved]

VariableDescriptor represents a descriptor whose external identifiers are otherwise resolved. As such, it should not be possible that such a declaration refers to unknown (or otherwise incorrect) external components.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL