2013-08-27 16:49:13 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-05-28 13:28:46 -06:00
|
|
|
package ssa
|
|
|
|
|
2013-07-30 12:28:14 -06:00
|
|
|
// This file defines utilities for working with source positions
|
|
|
|
// or source-level named entities ("objects").
|
2013-05-28 13:28:46 -06:00
|
|
|
|
2013-07-31 11:13:05 -06:00
|
|
|
// TODO(adonovan): test that {Value,Instruction}.Pos() positions match
|
|
|
|
// the originating syntax, as specified.
|
|
|
|
|
2013-05-28 13:28:46 -06:00
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
|
|
|
|
"code.google.com/p/go.tools/go/types"
|
2013-05-28 13:28:46 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// EnclosingFunction returns the function that contains the syntax
|
|
|
|
// node denoted by path.
|
|
|
|
//
|
|
|
|
// Syntax associated with package-level variable specifications is
|
|
|
|
// enclosed by the package's init() function.
|
|
|
|
//
|
|
|
|
// Returns nil if not found; reasons might include:
|
|
|
|
// - the node is not enclosed by any function.
|
|
|
|
// - the node is within an anonymous function (FuncLit) and
|
|
|
|
// its SSA function has not been created yet (pkg.BuildPackage()
|
|
|
|
// has not yet been called).
|
|
|
|
//
|
|
|
|
func EnclosingFunction(pkg *Package, path []ast.Node) *Function {
|
|
|
|
// Start with package-level function...
|
|
|
|
fn := findEnclosingPackageLevelFunction(pkg, path)
|
|
|
|
if fn == nil {
|
|
|
|
return nil // not in any function
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...then walk down the nested anonymous functions.
|
|
|
|
n := len(path)
|
|
|
|
outer:
|
|
|
|
for i := range path {
|
|
|
|
if lit, ok := path[n-1-i].(*ast.FuncLit); ok {
|
|
|
|
for _, anon := range fn.AnonFuncs {
|
|
|
|
if anon.Pos() == lit.Type.Func {
|
|
|
|
fn = anon
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// SSA function not found:
|
|
|
|
// - package not yet built, or maybe
|
|
|
|
// - builder skipped FuncLit in dead block
|
|
|
|
// (in principle; but currently the Builder
|
|
|
|
// generates even dead FuncLits).
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fn
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasEnclosingFunction returns true if the AST node denoted by path
|
|
|
|
// is contained within the declaration of some function or
|
|
|
|
// package-level variable.
|
|
|
|
//
|
|
|
|
// Unlike EnclosingFunction, the behaviour of this function does not
|
|
|
|
// depend on whether SSA code for pkg has been built, so it can be
|
|
|
|
// used to quickly reject check inputs that will cause
|
|
|
|
// EnclosingFunction to fail, prior to SSA building.
|
|
|
|
//
|
|
|
|
func HasEnclosingFunction(pkg *Package, path []ast.Node) bool {
|
|
|
|
return findEnclosingPackageLevelFunction(pkg, path) != nil
|
|
|
|
}
|
|
|
|
|
2013-06-24 12:15:13 -06:00
|
|
|
// findEnclosingPackageLevelFunction returns the Function
|
2013-05-28 13:28:46 -06:00
|
|
|
// corresponding to the package-level function enclosing path.
|
|
|
|
//
|
|
|
|
func findEnclosingPackageLevelFunction(pkg *Package, path []ast.Node) *Function {
|
|
|
|
if n := len(path); n >= 2 { // [... {Gen,Func}Decl File]
|
|
|
|
switch decl := path[n-2].(type) {
|
|
|
|
case *ast.GenDecl:
|
|
|
|
if decl.Tok == token.VAR && n >= 3 {
|
|
|
|
// Package-level 'var' initializer.
|
2013-07-10 16:37:52 -06:00
|
|
|
return pkg.init
|
2013-05-28 13:28:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
if decl.Recv == nil && decl.Name.Name == "init" {
|
|
|
|
// Explicit init() function.
|
2013-10-14 12:08:23 -06:00
|
|
|
for _, b := range pkg.init.Blocks {
|
|
|
|
for _, instr := range b.Instrs {
|
|
|
|
if instr, ok := instr.(*Call); ok {
|
|
|
|
if callee, ok := instr.Call.Value.(*Function); ok && callee.Pkg == pkg && callee.Pos() == decl.Name.NamePos {
|
|
|
|
return callee
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Hack: return non-nil when SSA is not yet
|
|
|
|
// built so that HasEnclosingFunction works.
|
2013-07-10 16:37:52 -06:00
|
|
|
return pkg.init
|
2013-05-28 13:28:46 -06:00
|
|
|
}
|
|
|
|
// Declared function/method.
|
|
|
|
return findNamedFunc(pkg, decl.Name.NamePos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil // not in any function
|
|
|
|
}
|
|
|
|
|
|
|
|
// findNamedFunc returns the named function whose FuncDecl.Ident is at
|
|
|
|
// position pos.
|
|
|
|
//
|
|
|
|
func findNamedFunc(pkg *Package, pos token.Pos) *Function {
|
|
|
|
// Look at all package members and method sets of named types.
|
|
|
|
// Not very efficient.
|
|
|
|
for _, mem := range pkg.Members {
|
|
|
|
switch mem := mem.(type) {
|
|
|
|
case *Function:
|
|
|
|
if mem.Pos() == pos {
|
|
|
|
return mem
|
|
|
|
}
|
|
|
|
case *Type:
|
2013-08-14 12:02:10 -06:00
|
|
|
mset := types.NewPointer(mem.Type()).MethodSet()
|
2013-07-30 12:28:14 -06:00
|
|
|
for i, n := 0, mset.Len(); i < n; i++ {
|
2013-07-30 14:36:58 -06:00
|
|
|
// Don't call Program.Method: avoid creating wrappers.
|
2013-07-30 12:28:14 -06:00
|
|
|
obj := mset.At(i).Obj().(*types.Func)
|
|
|
|
if obj.Pos() == pos {
|
|
|
|
return pkg.values[obj].(*Function)
|
2013-05-28 13:28:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-06-13 12:43:35 -06:00
|
|
|
|
2013-07-31 11:13:05 -06:00
|
|
|
// ValueForExpr returns the SSA Value that corresponds to non-constant
|
|
|
|
// expression e.
|
2013-06-13 12:43:35 -06:00
|
|
|
//
|
2013-07-31 11:13:05 -06:00
|
|
|
// It returns nil if no value was found, e.g.
|
|
|
|
// - the expression is not lexically contained within f;
|
|
|
|
// - f was not built with debug information; or
|
|
|
|
// - e is a constant expression. (For efficiency, no debug
|
|
|
|
// information is stored for constants. Use
|
|
|
|
// importer.PackageInfo.ValueOf(e) instead.)
|
|
|
|
// - the value was optimised away.
|
2013-06-13 12:43:35 -06:00
|
|
|
//
|
2013-07-31 11:13:05 -06:00
|
|
|
// The types of e and the result are equal (modulo "untyped" bools
|
|
|
|
// resulting from comparisons) and they have equal "pointerness".
|
2013-06-13 12:43:35 -06:00
|
|
|
//
|
2013-07-31 11:13:05 -06:00
|
|
|
// (Tip: to find the ssa.Value given a source position, use
|
|
|
|
// importer.PathEnclosingInterval to locate the ast.Node, then
|
|
|
|
// EnclosingFunction to locate the Function, then ValueForExpr to find
|
|
|
|
// the ssa.Value.)
|
|
|
|
//
|
|
|
|
func (f *Function) ValueForExpr(e ast.Expr) Value {
|
|
|
|
if f.debugInfo() { // (opt)
|
|
|
|
e = unparen(e)
|
|
|
|
for _, b := range f.Blocks {
|
|
|
|
for _, instr := range b.Instrs {
|
|
|
|
if ref, ok := instr.(*DebugRef); ok {
|
|
|
|
if ref.Expr == e {
|
|
|
|
return ref.X
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-13 12:43:35 -06:00
|
|
|
}
|
2013-07-31 11:13:05 -06:00
|
|
|
return nil
|
2013-06-13 12:43:35 -06:00
|
|
|
}
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
|
|
|
|
// --- Lookup functions for source-level named entities (types.Objects) ---
|
|
|
|
|
|
|
|
// Package returns the SSA Package corresponding to the specified
|
|
|
|
// type-checker package object.
|
|
|
|
// It returns nil if no such SSA package has been created.
|
|
|
|
//
|
|
|
|
func (prog *Program) Package(obj *types.Package) *Package {
|
|
|
|
return prog.packages[obj]
|
|
|
|
}
|
|
|
|
|
|
|
|
// packageLevelValue returns the package-level value corresponding to
|
|
|
|
// the specified named object, which may be a package-level const
|
2013-07-16 11:50:08 -06:00
|
|
|
// (*Const), var (*Global) or func (*Function) of some package in
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
// prog. It returns nil if the object is not found.
|
|
|
|
//
|
|
|
|
func (prog *Program) packageLevelValue(obj types.Object) Value {
|
|
|
|
if pkg, ok := prog.packages[obj.Pkg()]; ok {
|
|
|
|
return pkg.values[obj]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-23 16:18:35 -06:00
|
|
|
// FuncValue returns the Function denoted by the source-level named
|
|
|
|
// function obj.
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
//
|
2013-09-23 16:18:35 -06:00
|
|
|
func (prog *Program) FuncValue(obj *types.Func) *Function {
|
2013-07-29 12:24:09 -06:00
|
|
|
// Package-level function or declared method?
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
if v := prog.packageLevelValue(obj); v != nil {
|
2013-09-23 16:18:35 -06:00
|
|
|
return v.(*Function)
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
}
|
2013-07-29 15:10:11 -06:00
|
|
|
// Interface method wrapper?
|
2013-08-14 12:02:10 -06:00
|
|
|
meth := recvType(obj).MethodSet().Lookup(obj.Pkg(), obj.Name())
|
2013-07-30 14:36:58 -06:00
|
|
|
return prog.Method(meth)
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConstValue returns the SSA Value denoted by the source-level named
|
2013-07-16 11:50:08 -06:00
|
|
|
// constant obj. The result may be a *Const, or nil if not found.
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
//
|
2013-07-16 11:50:08 -06:00
|
|
|
func (prog *Program) ConstValue(obj *types.Const) *Const {
|
2013-07-15 14:10:08 -06:00
|
|
|
// TODO(adonovan): opt: share (don't reallocate)
|
2013-07-31 11:13:05 -06:00
|
|
|
// Consts for const objects and constant ast.Exprs.
|
2013-07-15 14:10:08 -06:00
|
|
|
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
// Universal constant? {true,false,nil}
|
|
|
|
if obj.Parent() == types.Universe {
|
2013-07-16 11:50:08 -06:00
|
|
|
return NewConst(obj.Val(), obj.Type())
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
}
|
|
|
|
// Package-level named constant?
|
|
|
|
if v := prog.packageLevelValue(obj); v != nil {
|
2013-07-16 11:50:08 -06:00
|
|
|
return v.(*Const)
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
}
|
2013-07-16 11:50:08 -06:00
|
|
|
return NewConst(obj.Val(), obj.Type())
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// VarValue returns the SSA Value that corresponds to a specific
|
|
|
|
// identifier denoting the source-level named variable obj.
|
|
|
|
//
|
|
|
|
// VarValue returns nil if a local variable was not found, perhaps
|
2013-07-31 11:13:05 -06:00
|
|
|
// because its package was not built, the debug information was not
|
|
|
|
// requested during SSA construction, or the value was optimized away.
|
2013-07-26 09:22:34 -06:00
|
|
|
//
|
2013-08-27 15:57:55 -06:00
|
|
|
// ref is the path to an ast.Ident (e.g. from PathEnclosingInterval),
|
|
|
|
// and that ident must resolve to obj.
|
|
|
|
//
|
|
|
|
// pkg is the package enclosing the reference. (A reference to a var
|
|
|
|
// may result in code, so we need to know where to find that code.)
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
//
|
|
|
|
// The Value of a defining (as opposed to referring) identifier is the
|
|
|
|
// value assigned to it in its definition.
|
|
|
|
//
|
|
|
|
// In many cases where the identifier appears in an lvalue context,
|
|
|
|
// the resulting Value is the var's address, not its value.
|
|
|
|
// For example, x in all these examples:
|
|
|
|
// x.y = 0
|
|
|
|
// x[0] = 0
|
|
|
|
// _ = x[:]
|
|
|
|
// x = X{}
|
|
|
|
// _ = &x
|
|
|
|
// x.method() (iff method is on &x)
|
|
|
|
// and all package-level vars. (This situation can be detected by
|
|
|
|
// comparing the types of the Var and Value.)
|
|
|
|
//
|
2013-08-27 15:57:55 -06:00
|
|
|
func (prog *Program) VarValue(obj *types.Var, pkg *Package, ref []ast.Node) Value {
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
id := ref[0].(*ast.Ident)
|
|
|
|
|
|
|
|
// Package-level variable?
|
|
|
|
if v := prog.packageLevelValue(obj); v != nil {
|
|
|
|
return v.(*Global)
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:57:55 -06:00
|
|
|
// Must be a function-local variable.
|
|
|
|
// (e.g. local, parameter, or field selection e.f)
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
fn := EnclosingFunction(pkg, ref)
|
|
|
|
if fn == nil {
|
|
|
|
return nil // e.g. SSA not built
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defining ident of a parameter?
|
|
|
|
if id.Pos() == obj.Pos() {
|
|
|
|
for _, param := range fn.Params {
|
|
|
|
if param.Object() == obj {
|
|
|
|
return param
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other ident?
|
|
|
|
for _, b := range fn.Blocks {
|
|
|
|
for _, instr := range b.Instrs {
|
|
|
|
if ref, ok := instr.(*DebugRef); ok {
|
|
|
|
if ref.Pos() == id.Pos() {
|
|
|
|
return ref.X
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 11:13:05 -06:00
|
|
|
return nil // e.g. debug info not requested, or var optimized away
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
}
|