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
- func Analyze(files *token.FileSet, file *ast.File, packages ...evm.Package) (*types.Package, *types.Info, error)
- func AnalyzeFiles(files *token.FileSet, syntax []*ast.File, packages ...evm.Package) (*types.Package, *types.Info, error)
- func CatchInternalError(err *error)
- func Compile(filename, source string, packages ...evm.Package) (*evm.Module, error)
- func CompileFiles(sources map[string]string, packages ...evm.Package) (*evm.Module, error)
- func PackagePaths(packages ...evm.Package) []string
- func PackageSource(path string, packages ...evm.Package) (string, error)
- func PackagesSource(packages ...evm.Package) (string, error)
- func SourceImporter(files *token.FileSet, packages ...evm.Package) types.Importer
- type DeclarationOptions
- type Diagnostic
- type Diagnostics
- type Environment
- func (env *Environment) Analyze(files *token.FileSet, file *ast.File, options Options) (*types.Package, *types.Info, error)
- func (env *Environment) AnalyzeFiles(files *token.FileSet, syntax []*ast.File, options Options) (_ *types.Package, _ *types.Info, err error)
- func (env *Environment) Compile(filename, source string, options Options) (m *evm.Module, err error)
- func (env *Environment) CompileFiles(sources map[string]string, options Options) (m *evm.Module, err error)
- func (env *Environment) MethodTables() evm.MethodTables
- func (env *Environment) NewFileSet() *token.FileSet
- func (env *Environment) NewLinkEnvironment(packages ...evm.Package) (*evm.LinkEnvironment, error)
- func (env *Environment) Packages() []evm.Package
- func (env *Environment) Shapes() []*values.Shape
- type InternalError
- type LimitError
- type Options
- func (o Options) AnalyzeFiles(files *token.FileSet, syntax []*ast.File, packages ...evm.Package) (*types.Package, *types.Info, error)
- func (o Options) Compile(filename, source string, packages ...evm.Package) (m *evm.Module, err error)
- func (o Options) CompileFiles(sources map[string]string, packages ...evm.Package) (m *evm.Module, err error)
Constants ¶
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.
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 ¶
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 ¶
CompileFiles compiles one package from named source files. Files share declarations; imports keep ordinary Go file scope. Input order is deterministic.
func PackagePaths ¶
PackagePaths lists every import, including virtual standard packages.
func PackageSource ¶
PackageSource prints the importable package as Go declarations, using the same types as Compile. KVO property names retain their schema spelling.
func PackagesSource ¶
PackagesSource prints all packages with their full import paths.
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 ¶
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 ¶
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 ¶
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.
Source Files
¶
- analyze.go
- cell_locals.go
- compiler.go
- declarations.go
- defer.go
- environment.go
- extended.go
- funcsize.go
- functions.go
- fusion.go
- generic_native.go
- inline.go
- interfaces.go
- internal_error.go
- invariant.go
- limits.go
- liveness.go
- lower.go
- method_tables.go
- module_difference.go
- native_shape_match.go
- native_slots.go
- native_types.go
- native_view_types.go
- operators.go
- overlay.go
- ownership.go
- ownership_local.go
- package_values.go
- packedslice.go
- positions.go
- publication.go
- record_lists.go
- records.go
- scalaropt.go
- shapes.go
- shared.go
- slots.go
- stdlib.go
- synthetic_storage.go
- typed.go
- types.go
- unroll.go