Commit c8f2449e authored by Alan Donovan's avatar Alan Donovan

exp/ssa: (#5 of 5): the SSA interpreter and 'ssadump' tool.

R=gri, iant
CC=golang-dev
https://golang.org/cl/7226065
parent 0a9f1ab8
This diff is collapsed.
This diff is collapsed.
package interp
// Custom hashtable atop map.
// For use when the key's equivalence relation is not consistent with ==.
// The Go specification doesn't address the atomicity of map operations.
// The FAQ states that an implementation is permitted to crash on
// concurrent map access.
import (
"go/types"
)
type hashable interface {
hash() int
eq(x interface{}) bool
}
type entry struct {
key hashable
value value
next *entry
}
// A hashtable atop the built-in map. Since each bucket contains
// exactly one hash value, there's no need to perform hash-equality
// tests when walking the linked list. Rehashing is done by the
// underlying map.
type hashmap struct {
table map[int]*entry
length int // number of entries in map
}
// makeMap returns an empty initialized map of key type kt,
// preallocating space for reserve elements.
func makeMap(kt types.Type, reserve int) value {
if usesBuiltinMap(kt) {
return make(map[value]value, reserve)
}
return &hashmap{table: make(map[int]*entry, reserve)}
}
// delete removes the association for key k, if any.
func (m *hashmap) delete(k hashable) {
hash := k.hash()
head := m.table[hash]
if head != nil {
if k.eq(head.key) {
m.table[hash] = head.next
m.length--
return
}
prev := head
for e := head.next; e != nil; e = e.next {
if k.eq(e.key) {
prev.next = e.next
m.length--
return
}
prev = e
}
}
}
// lookup returns the value associated with key k, if present, or
// value(nil) otherwise.
func (m *hashmap) lookup(k hashable) value {
hash := k.hash()
for e := m.table[hash]; e != nil; e = e.next {
if k.eq(e.key) {
return e.value
}
}
return nil
}
// insert updates the map to associate key k with value v. If there
// was already an association for an eq() (though not necessarily ==)
// k, the previous key remains in the map and its associated value is
// updated.
func (m *hashmap) insert(k hashable, v value) {
hash := k.hash()
head := m.table[hash]
for e := head; e != nil; e = e.next {
if k.eq(e.key) {
e.value = v
return
}
}
m.table[hash] = &entry{
key: k,
value: v,
next: head,
}
m.length++
}
// len returns the number of key/value associations in the map.
func (m *hashmap) len() int {
return m.length
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
// +build ignore
package main
// ssadump: a tool for displaying and interpreting the SSA form of Go programs.
import (
"exp/ssa"
"exp/ssa/interp"
"flag"
"fmt"
"log"
"os"
"strings"
)
// TODO(adonovan): perhaps these should each be separate flags?
var buildFlag = flag.String("build", "", `Options controlling the SSA builder.
The value is a sequence of zero or more of these letters:
C perform sanity [C]hecking of the SSA form.
P log [P]ackage inventory.
F log [F]unction SSA code.
S log [S]ource locations as SSA builder progresses.
G use binary object files from gc to provide imports (no code).
`)
var runFlag = flag.Bool("run", false, "Invokes the SSA interpreter on the program.")
var interpFlag = flag.String("interp", "", `Options controlling the SSA test interpreter.
The value is a sequence of zero or more more of these letters:
R disable [R]ecover() from panic; show interpreter crash instead.
T [T]race execution of the program. Best for single-threaded programs!
`)
const usage = `SSA builder and interpreter.
Usage: ssadump [<flag> ...] <file.go> ...
Use -help flag to display options.
Examples:
% ssadump -run -interp=T hello.go # interpret a program, with tracing
% ssadump -build=FPG hello.go # quickly dump SSA form of a single package
`
func main() {
flag.Parse()
args := flag.Args()
// TODO(adonovan): perhaps we need a more extensible option
// API than a bitset, e.g. a struct with a sane zero value?
var mode ssa.BuilderMode
for _, c := range *buildFlag {
switch c {
case 'P':
mode |= ssa.LogPackages
case 'F':
mode |= ssa.LogFunctions
case 'S':
mode |= ssa.LogSource
case 'C':
mode |= ssa.SanityCheckFunctions
case 'G':
mode |= ssa.UseGCImporter
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)
os.Exit(1)
}
// Treat all leading consecutive "*.go" arguments as a single package.
//
// TODO(gri): make it a typechecker error for there to be
// duplicate (e.g.) main functions in the same package.
var gofiles []string
for len(args) > 0 && strings.HasSuffix(args[0], ".go") {
gofiles = append(gofiles, args[0])
args = args[1:]
}
if gofiles == nil {
log.Fatal("No *.go source files specified.")
}
// TODO(adonovan): permit naming a package directly instead of
// a list of .go files.
// TODO(adonovan/gri): the cascade of errors is confusing due
// to reentrant control flow. Disable for now and re-think.
var errh func(error)
// errh = func(err error) { fmt.Println(err.Error()) }
b := ssa.NewBuilder(mode, ssa.GorootLoader, errh)
files, err := ssa.ParseFiles(b.Prog.Files, ".", gofiles...)
if err != nil {
log.Fatalf(err.Error())
}
mainpkg, err := b.CreatePackage("main", files)
if err != nil {
log.Fatalf(err.Error())
}
b.BuildPackage(mainpkg)
b = nil // discard Builder
if *runFlag {
interp.Interpret(mainpkg, interpMode, gofiles[0], args)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment