
For a full explanation, see interp/README.md. In short, this rewrite is a redesign of the partial evaluator which improves it over the previous partial evaluator. The main functional difference is that when interpreting a function, the interpretation can be rolled back when an unsupported instruction is encountered (for example, an actual unknown instruction or a branch on a value that's only known at runtime). This also means that it is no longer necessary to scan functions to see whether they can be interpreted: instead, this package now just tries to interpret it and reverts when it can't go further. This new design has several benefits: * Most errors coming from the interp package are avoided, as it can simply skip the code it can't handle. This has long been an issue. * The memory model has been improved, which means some packages now pass all tests that previously didn't pass them. * Because of a better design, it is in fact a bit faster than the previous version. This means the following packages now pass tests with `tinygo test`: * hash/adler32: previously it would hang in an infinite loop * math/cmplx: previously it resulted in errors This also means that the math/big package can be imported. It would previously fail with a "interp: branch on a non-constant" error.
89 строки
2,7 КиБ
Go
89 строки
2,7 КиБ
Go
package interp
|
|
|
|
// This file provides useful types for errors encountered during IR evaluation.
|
|
|
|
import (
|
|
"errors"
|
|
"go/scanner"
|
|
"go/token"
|
|
"path/filepath"
|
|
|
|
"tinygo.org/x/go-llvm"
|
|
)
|
|
|
|
var errLiteralToPointer = errors.New("interp: trying to convert literal value to pointer")
|
|
|
|
// These errors are expected during normal execution and can be recovered from
|
|
// by running the affected function at runtime instead of compile time.
|
|
var (
|
|
errExpectedPointer = errors.New("interp: trying to use an integer as a pointer (memory-mapped I/O?)")
|
|
errUnsupportedInst = errors.New("interp: unsupported instruction")
|
|
errUnsupportedRuntimeInst = errors.New("interp: unsupported instruction (to be emitted at runtime)")
|
|
errMapAlreadyCreated = errors.New("interp: map already created")
|
|
)
|
|
|
|
func isRecoverableError(err error) bool {
|
|
return err == errExpectedPointer || err == errUnsupportedInst || err == errUnsupportedRuntimeInst || err == errMapAlreadyCreated
|
|
}
|
|
|
|
// ErrorLine is one line in a traceback. The position may be missing.
|
|
type ErrorLine struct {
|
|
Pos token.Position
|
|
Inst llvm.Value
|
|
}
|
|
|
|
// Error encapsulates compile-time interpretation errors with an associated
|
|
// import path. The errors may not have a precise location attached.
|
|
type Error struct {
|
|
ImportPath string
|
|
Inst llvm.Value
|
|
Pos token.Position
|
|
Err error
|
|
Traceback []ErrorLine
|
|
}
|
|
|
|
// Error returns the string of the first error in the list of errors.
|
|
func (e *Error) Error() string {
|
|
return e.Pos.String() + ": " + e.Err.Error()
|
|
}
|
|
|
|
// errorAt returns an error value for the currently interpreted package at the
|
|
// location of the instruction. The location information may not be complete as
|
|
// it depends on debug information in the IR.
|
|
func (r *runner) errorAt(inst instruction, err error) *Error {
|
|
pos := getPosition(inst.llvmInst)
|
|
return &Error{
|
|
ImportPath: r.pkgName,
|
|
Pos: pos,
|
|
Err: err,
|
|
Traceback: []ErrorLine{{pos, inst.llvmInst}},
|
|
}
|
|
}
|
|
|
|
// errorAt returns an error value at the location of the instruction.
|
|
// The location information may not be complete as it depends on debug
|
|
// information in the IR.
|
|
func errorAt(inst llvm.Value, msg string) scanner.Error {
|
|
return scanner.Error{
|
|
Pos: getPosition(inst),
|
|
Msg: msg,
|
|
}
|
|
}
|
|
|
|
// getPosition returns the position information for the given instruction, as
|
|
// far as it is available.
|
|
func getPosition(inst llvm.Value) token.Position {
|
|
if inst.IsAInstruction().IsNil() {
|
|
return token.Position{}
|
|
}
|
|
loc := inst.InstructionDebugLoc()
|
|
if loc.IsNil() {
|
|
return token.Position{}
|
|
}
|
|
file := loc.LocationScope().ScopeFile()
|
|
return token.Position{
|
|
Filename: filepath.Join(file.FileDirectory(), file.FileFilename()),
|
|
Line: int(loc.LocationLine()),
|
|
Column: int(loc.LocationColumn()),
|
|
}
|
|
}
|