Skip to content

Commit e779aa4

Browse files
committed
go/ssa/interp: make tests fast and robust
The go/ssa/interp tests have been a maintenance nightmare for years because the interpreter requires intrinsics for all low-level or non-Go code functions, and the set of such functions in the standard library naturally changes from day to day. This CL finally drops support for interpreting real packages (which has anyway been broken for ages) and restricts the test suite to small programs that use a handful of simple functions in packages bytes, strings, errors, runtime, reflect, and unicode. These functions are declared in a tiny fake standard libary in testdata/src, and the implementations of these functions are provided by interpreter intrinsics that delegate to the real Go implementation---all their parameters and results are basic datatypes. The test suite is now very fast and should be easy to maintain going forward. It is still possible that a change to some file in $GOROOT/test/*.go adds a dependency to a symbol not present in our standard library, but this is rare. I will either delete the test or add the intrinsic on a case-by-case basis. We no longer attempt to interpret major functionality like fmt.Sprintf or "testing". The interpreter always pretends to be in linux/amd64 mode. Happy Christmas, Brad. ;) Fixes golang/go#27292 Change-Id: I715cf63e3534e2e0dab4666a5d7c669bf1d92674 Reviewed-on: https://go-review.googlesource.com/c/tools/+/168898 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent cc8e56e commit e779aa4

24 files changed

+304
-986
lines changed

go/ssa/interp/external.go

+131-403
Large diffs are not rendered by default.

go/ssa/interp/external_darwin.go

-35
This file was deleted.

go/ssa/interp/external_unix.go

-256
This file was deleted.

go/ssa/interp/interp.go

-43
Original file line numberDiff line numberDiff line change
@@ -630,30 +630,6 @@ func setGlobal(i *interpreter, pkg *ssa.Package, name string, v value) {
630630
panic("no global variable: " + pkg.Pkg.Path() + "." + name)
631631
}
632632

633-
var environ []value
634-
635-
func init() {
636-
for _, s := range os.Environ() {
637-
environ = append(environ, s)
638-
}
639-
environ = append(environ, "GOSSAINTERP=1")
640-
environ = append(environ, "GOARCH="+runtime.GOARCH)
641-
}
642-
643-
// deleteBodies delete the bodies of all standalone functions except the
644-
// specified ones. A missing intrinsic leads to a clear runtime error.
645-
func deleteBodies(pkg *ssa.Package, except ...string) {
646-
keep := make(map[string]bool)
647-
for _, e := range except {
648-
keep[e] = true
649-
}
650-
for _, mem := range pkg.Members {
651-
if fn, ok := mem.(*ssa.Function); ok && !keep[fn.Name()] {
652-
fn.Blocks = nil
653-
}
654-
}
655-
}
656-
657633
// Interpret interprets the Go program whose main package is mainpkg.
658634
// mode specifies various interpreter options. filename and args are
659635
// the initial values of os.Args for the target program. sizes is the
@@ -665,11 +641,6 @@ func deleteBodies(pkg *ssa.Package, except ...string) {
665641
// The SSA program must include the "runtime" package.
666642
//
667643
func Interpret(mainpkg *ssa.Package, mode Mode, sizes types.Sizes, filename string, args []string) (exitCode int) {
668-
if syswrite == nil {
669-
fmt.Fprintln(os.Stderr, "Interpret: unsupported platform.")
670-
return 1
671-
}
672-
673644
i := &interpreter{
674645
prog: mainpkg.Prog,
675646
globals: make(map[ssa.Value]*value),
@@ -699,20 +670,6 @@ func Interpret(mainpkg *ssa.Package, mode Mode, sizes types.Sizes, filename stri
699670
i.globals[v] = &cell
700671
}
701672
}
702-
703-
// Ad-hoc initialization for magic system variables.
704-
switch pkg.Pkg.Path() {
705-
case "syscall":
706-
setGlobal(i, pkg, "envs", environ)
707-
708-
case "reflect":
709-
deleteBodies(pkg, "DeepEqual", "deepValueEqual")
710-
711-
case "runtime":
712-
sz := sizes.Sizeof(pkg.Pkg.Scope().Lookup("MemStats").Type())
713-
setGlobal(i, pkg, "sizeof_C_MStats", uintptr(sz))
714-
deleteBodies(pkg, "GOROOT", "gogetenv")
715-
}
716673
}
717674

718675
// Top-level error handler.

0 commit comments

Comments
 (0)