evmcompiler

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: 0BSD Imports: 29 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package evmcompiler compiles sequential Go 1.23 to EVM bytecode. Only supplied package declarations are visible; it never imports native Go packages or uses reflection. Unsupported syntax is a compile error.

Index

Constants

View Source
const (
	// MaxTypeNesting bounds type expressions such as [][]*map[string]T: building
	// storage shapes is worse than cubic in their depth.
	MaxTypeNesting = 32
	// MaxExprNesting bounds one expression; a+b+c nests two levels. The checker,
	// SSA builder and printer all recurse once per level.
	MaxExprNesting = 500
	// MaxStmtNesting bounds blocks, branches and loops inside a function, else-if
	// chains included: each level is a scope every inner name lookup walks.
	MaxStmtNesting = 100
	// MaxFuncNesting bounds function literals inside function literals: each
	// level captures the variables of all levels around it.
	MaxFuncNesting = 32
	// MaxConstStringSize bounds a constant string expression: the checker folds
	// a+a at compile time, so a few short lines can build gigabytes.
	MaxConstStringSize = 1 << 20
	// MaxBranchWeight bounds the functions of a package taken together, by
	// the costs of the SSA form that grow with the square of a function: a
	// quarter of the square of its basic blocks, its blocks times its local
	// variables, twice the square of the edges into each join, and 64 for each
	// phi operand. A function reaches it alone with about 4,000 if statements
	// in a row, a switch of 2,300 cases, or a switch of 500 cases that each
	// assign a variable of their own. A copy that keeps a borrowed value past
	// its scope counts 256: a function can need one for each borrowed value
	// at each branch that ends a scope.
	MaxBranchWeight = 16 << 20
)

Guest source reaches the compiler inside a running host. Type checking and compilation cost grows faster than the source with nesting depth and with folded constants, and Go can neither stop a goroutine nor cap its heap. The limits below reject such source from its syntax alone, before the type checker runs.

View Source
const MaxSourceSize = 1 << 20

Variables

This section is empty.

Functions

func Analyze

func Analyze(files *token.FileSet, file *ast.File, packages ...evm.Package) (*types.Package, *types.Info, error)

Analyze checks a frontend's AST against the same virtual packages as Compile. It returns partial type information with diagnostics so a frontend can resolve its own syntax, then require a successful check after lowering to ordinary Go.

func AnalyzeFiles

func AnalyzeFiles(files *token.FileSet, syntax []*ast.File, packages ...evm.Package) (*types.Package, *types.Info, error)

AnalyzeFiles checks a package while preserving each file's import scope. Syntax past the limits returns diagnostics and no type information.

func CatchInternalError

func CatchInternalError(err *error)

CatchInternalError is deferred by entry points that accept guest source.

func Compile

func Compile(filename, source string, packages ...evm.Package) (*evm.Module, error)

Compile type-checks Go 1.23 source against explicitly imported virtual packages and lowers supported operations to fixed64 EVM bytecode. Unsupported operations return diagnostics; no partial module is returned.

func CompileFiles

func CompileFiles(sources map[string]string, packages ...evm.Package) (*evm.Module, error)

CompileFiles compiles one package from named source files. Files share declarations; imports keep ordinary Go file scope. Input order is deterministic.

func PackagePaths

func PackagePaths(packages ...evm.Package) []string

PackagePaths lists every import, including virtual standard packages.

func PackageSource

func PackageSource(path string, packages ...evm.Package) (string, error)

PackageSource prints the importable package as Go declarations, using the same types as Compile. KVO property names retain their schema spelling.

func PackagesSource

func PackagesSource(packages ...evm.Package) (string, error)

PackagesSource prints all packages with their full import paths.

func SourceImporter

func SourceImporter(files *token.FileSet, packages ...evm.Package) types.Importer

SourceImporter exposes EVM standard packages and supplied vetted packages when checking host declarations.

Types

type DeclarationOptions

type DeclarationOptions struct {
	// Annotations selects comment lines for a function from its raw declaration comments.
	Annotations func(path, name, doc string) []string
}

DeclarationOptions controls annotations in printed package declarations.

func (DeclarationOptions) PackageSource

func (options DeclarationOptions) PackageSource(path string, packages ...evm.Package) (string, error)

type Diagnostic

type Diagnostic struct {
	Position evm.SourcePosition
	Message  string
}

func (Diagnostic) Error

func (d Diagnostic) Error() string

type Diagnostics

type Diagnostics []Diagnostic

func (Diagnostics) Error

func (d Diagnostics) Error() string

type Environment

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

Environment is a set of host packages prepared for compiling many sources.

NewEnvironment type-checks every package and the standard library once. Each compile then checks only its own source against that shared result. An Environment never changes after NewEnvironment returns and is safe for concurrent use. Host types keep one identity across all of its compiles.

The packages must not change while the Environment is in use.

func NewEnvironment

func NewEnvironment(packages ...evm.Package) (env *Environment, err error)

NewEnvironment validates and imports the packages. It fails if any package cannot be imported, including one that no source would import.

func (*Environment) Analyze

func (env *Environment) Analyze(files *token.FileSet, file *ast.File, options Options) (*types.Package, *types.Info, error)

Analyze checks a frontend's AST. See Analyze. The file must have been parsed into a file set from NewFileSet.

func (*Environment) AnalyzeFiles

func (env *Environment) AnalyzeFiles(files *token.FileSet, syntax []*ast.File, options Options) (_ *types.Package, _ *types.Info, err error)

AnalyzeFiles checks a package while preserving each file's import scope. See AnalyzeFiles. The files must have been parsed into a file set from NewFileSet.

func (*Environment) Compile

func (env *Environment) Compile(filename, source string, options Options) (m *evm.Module, err error)

Compile compiles one source file. See Compile.

func (*Environment) CompileFiles

func (env *Environment) CompileFiles(sources map[string]string, options Options) (m *evm.Module, err error)

CompileFiles compiles one package from named source files. See CompileFiles.

func (*Environment) MethodTables

func (env *Environment) MethodTables() evm.MethodTables

MethodTables returns the method tables the environment's modules share, for evm.LinkOptions. They never change.

func (*Environment) NewFileSet

func (env *Environment) NewFileSet() *token.FileSet

NewFileSet returns a file set for syntax passed to Analyze and AnalyzeFiles. It already holds the host sources, so positions of host declarations resolve in it. The host files are shared, not copied.

func (*Environment) NewLinkEnvironment

func (env *Environment) NewLinkEnvironment(packages ...evm.Package) (*evm.LinkEnvironment, error)

NewLinkEnvironment prepares packages for linking the modules this environment compiles. The packages declare what the environment was built from and bind the implementations, which a compile does not need. Programs linked through the result share the linked form of the host shapes and the bound method tables of host types; see evm.LinkOptions.

func (*Environment) Packages

func (env *Environment) Packages() []evm.Package

Packages returns the packages the environment was built from.

func (*Environment) Shapes

func (env *Environment) Shapes() []*values.Shape

Shapes returns the storage shapes of the host types. The modules the environment compiles list a selection of them, the same shapes every time, along with shapes of their own. The shapes never change.

type InternalError

type InternalError struct {
	Value any
	Stack []byte
}

InternalError reports a panic raised while compiling or formatting. Guest source reaches the compiler in running hosts, so entry points return the failure instead of unwinding the host. Stack helps locate the defect.

func (*InternalError) Error

func (e *InternalError) Error() string

func (*InternalError) Unwrap

func (e *InternalError) Unwrap() error

type LimitError

type LimitError struct {
	Pos     token.Pos
	Message string
}

LimitError reports syntax that exceeds one of the limits.

func CheckLimits

func CheckLimits(syntax []*ast.File, packages ...evm.Package) []LimitError

CheckLimits reports syntax of one package that exceeds the limits. Compile and AnalyzeFiles apply it themselves; a frontend calls it on the source its author wrote, to report positions there before lowering changes the syntax. Packages supply the sizes of imported string constants.

type Options

type Options struct {
	NoOptimization bool
	// LoweredStmtNesting raises MaxStmtNesting for Go that a frontend generated
	// from source it has already checked with CheckLimits. It is the nesting
	// that the frontend's lowering puts around the author's statements.
	LoweredStmtNesting int
}

Options controls optional EVM code generation. The zero value enables all passes.

func (Options) AnalyzeFiles

func (o Options) AnalyzeFiles(files *token.FileSet, syntax []*ast.File, packages ...evm.Package) (*types.Package, *types.Info, error)

AnalyzeFiles checks a package with the limits these options set.

func (Options) Compile

func (o Options) Compile(filename, source string, packages ...evm.Package) (m *evm.Module, err error)

Compile emits Go SSA operations directly when NoOptimization is set. Link honors the same choice for the lifetime of the resulting program.

func (Options) CompileFiles

func (o Options) CompileFiles(sources map[string]string, packages ...evm.Package) (m *evm.Module, err error)

Jump to

Keyboard shortcuts

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