作者:Karthikv
项目:15640_project
// setGlobal sets the value of a system-initialized global variable.
func setGlobal(i *interpreter, pkg *ssa.Package, name string, v value) {
if g, ok := i.globals[pkg.Var(name)]; ok {
*g = v
return
}
panic("no global variable: " + pkg.Object.Path() + "." + name)
}
作者:Karthikv
项目:15640_project
// Interpret interprets the Go program whose main package is mainpkg.
// mode specifies various interpreter options. filename and args are
// the initial values of os.Args for the target program. sizes is the
// effective type-sizing function for this program.
//
// Interpret returns the exit code of the program: 2 for panic (like
// gc does), or the argument to os.Exit for normal termination.
//
// The SSA program must include the "runtime" package.
//
func Interpret(mainpkg *ssa.Package, mode Mode, sizes types.Sizes, filename string, args []string) (exitCode int) {
i := &interpreter{
prog: mainpkg.Prog,
globals: make(map[ssa.Value]*value),
mode: mode,
sizes: sizes,
}
runtimePkg := i.prog.ImportedPackage("runtime")
if runtimePkg == nil {
panic("ssa.Program doesn't include runtime package")
}
i.runtimeErrorString = runtimePkg.Type("errorString").Object().Type()
initReflect(i)
for _, pkg := range i.prog.AllPackages() {
// Initialize global storage.
for _, m := range pkg.Members {
switch v := m.(type) {
case *ssa.Global:
cell := zero(deref(v.Type()))
i.globals[v] = &cell
}
}
// Ad-hoc initialization for magic system variables.
switch pkg.Object.Path() {
case "syscall":
var envs []value
for _, s := range os.Environ() {
envs = append(envs, s)
}
envs = append(envs, "GOSSAINTERP=1")
envs = append(envs, "GOARCH="+runtime.GOARCH)
setGlobal(i, pkg, "envs", envs)
case "runtime":
sz := sizes.Sizeof(pkg.Object.Scope().Lookup("MemStats").Type())
setGlobal(i, pkg, "sizeof_C_MStats", uintptr(sz))
case "os":
Args := []value{filename}
for _, s := range args {
Args = append(Args, s)
}
setGlobal(i, pkg, "Args", Args)
}
}
// Top-level error handler.
exitCode = 2
defer func() {
if exitCode != 2 || i.mode&DisableRecover != 0 {
return
}
switch p := recover().(type) {
case exitPanic:
exitCode = int(p)
return
case targetPanic:
fmt.Fprintln(os.Stderr, "panic:", toString(p.v))
case runtime.Error:
fmt.Fprintln(os.Stderr, "panic:", p.Error())
case string:
fmt.Fprintln(os.Stderr, "panic:", p)
default:
fmt.Fprintf(os.Stderr, "panic: unexpected type: %T\n", p)
}
// TODO(adonovan): dump panicking interpreter goroutine?
// buf := make([]byte, 0x10000)
// runtime.Stack(buf, false)
// fmt.Fprintln(os.Stderr, string(buf))
// (Or dump panicking target goroutine?)
}()
// Run!
call(i, nil, token.NoPos, mainpkg.Func("init"), nil)
if mainFn := mainpkg.Func("main"); mainFn != nil {
call(i, nil, token.NoPos, mainFn, nil)
exitCode = 0
} else {
fmt.Fprintln(os.Stderr, "No main function.")
exitCode = 1
}
return
}
作者:Karthikv
项目:15640_project
func doMain() error {
flag.Parse()
args := flag.Args()
conf := loader.Config{
Build: &build.Default,
SourceImports: true,
}
// TODO(adonovan): make go/types choose its default Sizes from
// build.Default or a specified *build.Context.
var wordSize int64 = 8
switch conf.Build.GOARCH {
case "386", "arm":
wordSize = 4
}
conf.TypeChecker.Sizes = &types.StdSizes{
MaxAlign: 8,
WordSize: wordSize,
}
var mode ssa.BuilderMode
for _, c := range *buildFlag {
switch c {
case 'D':
mode |= ssa.GlobalDebug
case 'P':
mode |= ssa.LogPackages | ssa.BuildSerially
case 'F':
mode |= ssa.LogFunctions | ssa.BuildSerially
case 'S':
mode |= ssa.LogSource | ssa.BuildSerially
case 'C':
mode |= ssa.SanityCheckFunctions
case 'N':
mode |= ssa.NaiveForm
case 'G':
conf.SourceImports = false
case 'L':
mode |= ssa.BuildSerially
default:
return fmt.Errorf("unknown -build option: '%c'", c)
}
}
var interpMode interp.Mode
for _, c := range *interpFlag {
switch c {
case 'T':
interpMode |= interp.EnableTracing
case 'R':
interpMode |= interp.DisableRecover
default:
fmt.Fprintf(os.Stderr, "ssadump: unknown -interp option: '%c'.", c)
os.Exit(1)
}
}
if len(args) == 0 {
fmt.Fprint(os.Stderr, usage)
os.Exit(1)
}
// Profiling support.
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Use the initial packages from the command line.
args, err := conf.FromArgs(args, *testFlag)
if err != nil {
return err
}
// The interpreter needs the runtime package.
if *runFlag {
conf.Import("runtime")
}
// Load, parse and type-check the whole program.
iprog, err := conf.Load()
if err != nil {
return err
}
// Create and build SSA-form program representation.
prog := ssa.Create(iprog, mode)
prog.BuildAll()
// Run the interpreter.
if *runFlag {
var main *ssa.Package
pkgs := prog.AllPackages()
if *testFlag {
// If -test, run all packages' tests.
//.........这里部分代码省略.........
作者:pombredann
项目:tardisg
func doTestable(args []string) error {
conf := loader.Config{
Build: &build.Default,
SourceImports: true,
}
// TODO(adonovan): make go/types choose its default Sizes from
// build.Default or a specified *build.Context.
var wordSize int64 = 8
switch conf.Build.GOARCH {
case "386", "arm":
wordSize = 4
}
wordSize = 4 // TARDIS Go addition to force default int size to 32 bits
//conf.Build.GOARCH = "tardisgo" // TARDIS Go addition to ensure no architecure-specific code will compile
//conf.Build.GOOS = "tardisgo" // TARDIS Go addition to ensure no OS-specific code will compile
conf.TypeChecker.Sizes = &types.StdSizes{
MaxAlign: 8,
WordSize: wordSize,
}
var mode ssa.BuilderMode
for _, c := range *buildFlag {
switch c {
case 'D':
mode |= ssa.GlobalDebug
case 'P':
mode |= ssa.LogPackages | ssa.BuildSerially
case 'F':
mode |= ssa.LogFunctions | ssa.BuildSerially
case 'S':
mode |= ssa.LogSource | ssa.BuildSerially
case 'C':
mode |= ssa.SanityCheckFunctions
case 'N':
mode |= ssa.NaiveForm
case 'G':
conf.SourceImports = false
case 'L':
mode |= ssa.BuildSerially
default:
log.Fatalf("Unknown -build option: '%c'.", c)
}
}
var interpMode interp.Mode
for _, c := range *interpFlag {
switch c {
case 'T':
interpMode |= interp.EnableTracing
case 'R':
interpMode |= interp.DisableRecover
default:
log.Fatalf("Unknown -interp option: '%c'.", c)
}
}
if len(args) == 0 {
//fmt.Fprint(os.Stderr, usage)
return fmt.Errorf("%v", usage)
}
// Profiling support.
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
return err
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// TARDIS Go TEST
// Really need to find a way to replace entire packages, this experiment did not work...
/*
conf.Fset = token.NewFileSet()
f, err := parser.ParseFile(conf.Fset, conf.Build.GOPATH+"/src/github.com/tardisgo/tardisgo/golibruntime/runtime/runtime.go", nil, 0)
if err != nil {
fmt.Println(err)
return err
}
conf.CreateFromFiles("", f)
fmt.Printf("DEBUG %s %s\n", f.Name.Name, "") //, f.Name.Obj.Name)
*/
// end TARDIS Go TEST
// Use the initial packages from the command line.
args, err := conf.FromArgs(args, *testFlag)
if err != nil {
return err
}
// The interpreter needs the runtime package.
if *runFlag {
conf.Import("runtime")
conf.Import("github.com/tardisgo/tardisgo/golibruntime/runtime") // This required for TARDIS go to run runtime
}
//.........这里部分代码省略.........
作者:bryanx
项目:go-zh.tool
// Interpret interprets the Go program whose main package is mainpkg.
// mode specifies various interpreter options. filename and args are
// the initial values of os.Args for the target program. sizes is the
// effective type-sizing function for this program.
//
// Interpret returns the exit code of the program: 2 for panic (like
// gc does), or the argument to os.Exit for normal termination.
//
// The SSA program must include the "runtime" package.
//
func Interpret(mainpkg *ssa.Package, mode Mode, sizes types.Sizes, filename string, args []string) (exitCode int) {
i := &interpreter{
prog: mainpkg.Prog,
globals: make(map[ssa.Value]*value),
mode: mode,
sizes: sizes,
}
runtimePkg := i.prog.ImportedPackage("runtime")
if runtimePkg == nil {
panic("ssa.Program doesn't include runtime package")
}
i.runtimeErrorString = runtimePkg.Type("errorString").Object().Type()
initReflect(i)
i.osArgs = append(i.osArgs, filename)
for _, arg := range args {
i.osArgs = append(i.osArgs, arg)
}
for _, pkg := range i.prog.AllPackages() {
// Initialize global storage.
for _, m := range pkg.Members {
switch v := m.(type) {
case *ssa.Global:
cell := zero(deref(v.Type()))
i.globals[v] = &cell
}
}
// Ad-hoc initialization for magic system variables.
switch pkg.Object.Path() {
case "syscall":
setGlobal(i, pkg, "envs", environ)
case "runtime":
sz := sizes.Sizeof(pkg.Object.Scope().Lookup("MemStats").Type())
setGlobal(i, pkg, "sizeof_C_MStats", uintptr(sz))
// Delete the bodies of almost all "runtime" functions since they're magic.
// A missing intrinsic leads to a very clear error.
for _, mem := range pkg.Members {
if fn, ok := mem.(*ssa.Function); ok {
switch fn.Name() {
case "GOROOT", "gogetenv":
// keep
default:
fn.Blocks = nil
}
}
}
}
}
// Top-level error handler.
exitCode = 2
defer func() {
if exitCode != 2 || i.mode&DisableRecover != 0 {
return
}
switch p := recover().(type) {
case exitPanic:
exitCode = int(p)
return
case targetPanic:
fmt.Fprintln(os.Stderr, "panic:", toString(p.v))
case runtime.Error:
fmt.Fprintln(os.Stderr, "panic:", p.Error())
case string:
fmt.Fprintln(os.Stderr, "panic:", p)
default:
fmt.Fprintf(os.Stderr, "panic: unexpected type: %T\n", p)
}
// TODO(adonovan): dump panicking interpreter goroutine?
// buf := make([]byte, 0x10000)
// runtime.Stack(buf, false)
// fmt.Fprintln(os.Stderr, string(buf))
// (Or dump panicking target goroutine?)
}()
// Run!
call(i, nil, token.NoPos, mainpkg.Func("init"), nil)
if mainFn := mainpkg.Func("main"); mainFn != nil {
call(i, nil, token.NoPos, mainFn, nil)
exitCode = 0
} else {
fmt.Fprintln(os.Stderr, "No main function.")
exitCode = 1
}
//.........这里部分代码省略.........