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-17 14:25:48 -06:00
|
|
|
package ssa
|
|
|
|
|
|
|
|
// Helpers for emitting SSA instructions.
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
"go/ast"
|
2013-05-17 14:25:48 -06:00
|
|
|
"go/token"
|
|
|
|
|
|
|
|
"code.google.com/p/go.tools/go/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// emitNew emits to f a new (heap Alloc) instruction allocating an
|
|
|
|
// object of type typ. pos is the optional source location.
|
|
|
|
//
|
2013-08-01 12:06:10 -06:00
|
|
|
func emitNew(f *Function, typ types.Type, pos token.Pos) *Alloc {
|
|
|
|
v := &Alloc{Heap: true}
|
|
|
|
v.setType(types.NewPointer(typ))
|
|
|
|
v.setPos(pos)
|
|
|
|
f.emit(v)
|
|
|
|
return v
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// emitLoad emits to f an instruction to load the address addr into a
|
|
|
|
// new temporary, and returns the value so defined.
|
|
|
|
//
|
2013-05-30 07:59:17 -06:00
|
|
|
func emitLoad(f *Function, addr Value) *UnOp {
|
2013-05-17 14:25:48 -06:00
|
|
|
v := &UnOp{Op: token.MUL, X: addr}
|
2013-07-12 22:09:33 -06:00
|
|
|
v.setType(deref(addr.Type()))
|
2013-05-30 07:59:17 -06:00
|
|
|
f.emit(v)
|
|
|
|
return v
|
2013-05-17 14:25:48 -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
|
|
|
// emitDebugRef emits to f a DebugRef pseudo-instruction associating
|
2013-07-31 11:13:05 -06:00
|
|
|
// expression e with value v.
|
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
|
|
|
//
|
go.tools/ssa: record lvalue/rvalue distinction precisely in DebugRef.
A DebugRef associates a source expression E with an ssa.Value
V, but until now did not record whether V was the value or the
address of E. So, we would guess from the "pointerness" of
the Value, leading to confusion in some cases, e.g.
type N *N
var n N
n = &n // lvalue and rvalue are both pointers
Now we explicitly record 'IsAddress bool' in DebugRef, and
plumb this everywhere: through (*Function).ValueForExpr and
(*Program).VarValue, all the way to forming the pointer
analysis query.
Also:
- VarValue now treats each reference to a global distinctly,
just like it does for other vars. So:
var g int
func f() {
g = 1 // VarValue(g) == Const(1:int), !isAddress
print(g) // VarValue(g) == Global(g), isAddress
}
- DebugRefs are not emitted for references to predeclared
identifiers (nil, built-in).
- DebugRefs no longer prevent lifting of an Alloc var into a
register; now we update or discard the debug info.
- TestValueForExpr: improve coverage of ssa.EnclosingFunction
by putting expectations in methods and init funcs, not just
normal funcs.
- oracle: fix golden file broken by recent
(*types.Var).IsField change.
R=gri
CC=golang-dev
https://golang.org/cl/16610045
2013-10-24 16:31:50 -06:00
|
|
|
func emitDebugRef(f *Function, e ast.Expr, v Value, isAddr bool) {
|
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 !f.debugInfo() {
|
|
|
|
return // debugging not enabled
|
|
|
|
}
|
2013-07-31 11:13:05 -06:00
|
|
|
if v == nil || e == nil {
|
|
|
|
panic("nil")
|
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-31 11:13:05 -06:00
|
|
|
var obj types.Object
|
2013-10-29 09:07:09 -06:00
|
|
|
e = unparen(e)
|
2013-07-31 11:13:05 -06:00
|
|
|
if id, ok := e.(*ast.Ident); ok {
|
|
|
|
if isBlankIdent(id) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj = f.Pkg.objectOf(id)
|
go.tools/ssa: record lvalue/rvalue distinction precisely in DebugRef.
A DebugRef associates a source expression E with an ssa.Value
V, but until now did not record whether V was the value or the
address of E. So, we would guess from the "pointerness" of
the Value, leading to confusion in some cases, e.g.
type N *N
var n N
n = &n // lvalue and rvalue are both pointers
Now we explicitly record 'IsAddress bool' in DebugRef, and
plumb this everywhere: through (*Function).ValueForExpr and
(*Program).VarValue, all the way to forming the pointer
analysis query.
Also:
- VarValue now treats each reference to a global distinctly,
just like it does for other vars. So:
var g int
func f() {
g = 1 // VarValue(g) == Const(1:int), !isAddress
print(g) // VarValue(g) == Global(g), isAddress
}
- DebugRefs are not emitted for references to predeclared
identifiers (nil, built-in).
- DebugRefs no longer prevent lifting of an Alloc var into a
register; now we update or discard the debug info.
- TestValueForExpr: improve coverage of ssa.EnclosingFunction
by putting expectations in methods and init funcs, not just
normal funcs.
- oracle: fix golden file broken by recent
(*types.Var).IsField change.
R=gri
CC=golang-dev
https://golang.org/cl/16610045
2013-10-24 16:31:50 -06:00
|
|
|
switch obj.(type) {
|
|
|
|
case *types.Nil, *types.Const, *types.Builtin:
|
2013-07-31 11:13:05 -06:00
|
|
|
return
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
f.emit(&DebugRef{
|
|
|
|
X: v,
|
2013-10-29 09:07:09 -06:00
|
|
|
Expr: e,
|
go.tools/ssa: record lvalue/rvalue distinction precisely in DebugRef.
A DebugRef associates a source expression E with an ssa.Value
V, but until now did not record whether V was the value or the
address of E. So, we would guess from the "pointerness" of
the Value, leading to confusion in some cases, e.g.
type N *N
var n N
n = &n // lvalue and rvalue are both pointers
Now we explicitly record 'IsAddress bool' in DebugRef, and
plumb this everywhere: through (*Function).ValueForExpr and
(*Program).VarValue, all the way to forming the pointer
analysis query.
Also:
- VarValue now treats each reference to a global distinctly,
just like it does for other vars. So:
var g int
func f() {
g = 1 // VarValue(g) == Const(1:int), !isAddress
print(g) // VarValue(g) == Global(g), isAddress
}
- DebugRefs are not emitted for references to predeclared
identifiers (nil, built-in).
- DebugRefs no longer prevent lifting of an Alloc var into a
register; now we update or discard the debug info.
- TestValueForExpr: improve coverage of ssa.EnclosingFunction
by putting expectations in methods and init funcs, not just
normal funcs.
- oracle: fix golden file broken by recent
(*types.Var).IsField change.
R=gri
CC=golang-dev
https://golang.org/cl/16610045
2013-10-24 16:31:50 -06:00
|
|
|
IsAddr: isAddr,
|
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
|
|
|
object: obj,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
// emitArith emits to f code to compute the binary operation op(x, y)
|
|
|
|
// where op is an eager shift, logical or arithmetic operation.
|
|
|
|
// (Use emitCompare() for comparisons and Builder.logicalBinop() for
|
|
|
|
// non-eager operations.)
|
|
|
|
//
|
2013-05-30 07:59:17 -06:00
|
|
|
func emitArith(f *Function, op token.Token, x, y Value, t types.Type, pos token.Pos) Value {
|
2013-05-17 14:25:48 -06:00
|
|
|
switch op {
|
|
|
|
case token.SHL, token.SHR:
|
|
|
|
x = emitConv(f, x, t)
|
2013-07-09 08:21:25 -06:00
|
|
|
// y may be signed or an 'untyped' constant.
|
|
|
|
// TODO(adonovan): whence signed values?
|
|
|
|
if b, ok := y.Type().Underlying().(*types.Basic); ok && b.Info()&types.IsUnsigned == 0 {
|
|
|
|
y = emitConv(f, y, types.Typ[types.Uint64])
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
case token.ADD, token.SUB, token.MUL, token.QUO, token.REM, token.AND, token.OR, token.XOR, token.AND_NOT:
|
|
|
|
x = emitConv(f, x, t)
|
|
|
|
y = emitConv(f, y, t)
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("illegal op in emitArith: " + op.String())
|
|
|
|
|
|
|
|
}
|
|
|
|
v := &BinOp{
|
|
|
|
Op: op,
|
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
}
|
2013-05-30 07:59:17 -06:00
|
|
|
v.setPos(pos)
|
2013-05-17 14:25:48 -06:00
|
|
|
v.setType(t)
|
|
|
|
return f.emit(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// emitCompare emits to f code compute the boolean result of
|
|
|
|
// comparison comparison 'x op y'.
|
|
|
|
//
|
2013-05-30 07:59:17 -06:00
|
|
|
func emitCompare(f *Function, op token.Token, x, y Value, pos token.Pos) Value {
|
2013-05-17 15:02:47 -06:00
|
|
|
xt := x.Type().Underlying()
|
|
|
|
yt := y.Type().Underlying()
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
// Special case to optimise a tagless SwitchStmt so that
|
|
|
|
// these are equivalent
|
|
|
|
// switch { case e: ...}
|
|
|
|
// switch true { case e: ... }
|
|
|
|
// if e==true { ... }
|
|
|
|
// even in the case when e's type is an interface.
|
|
|
|
// TODO(adonovan): opt: generalise to x==true, false!=y, etc.
|
|
|
|
if x == vTrue && op == token.EQL {
|
2013-05-17 15:02:47 -06:00
|
|
|
if yt, ok := yt.(*types.Basic); ok && yt.Info()&types.IsBoolean != 0 {
|
2013-05-17 14:25:48 -06:00
|
|
|
return y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if types.IsIdentical(xt, yt) {
|
|
|
|
// no conversion necessary
|
|
|
|
} else if _, ok := xt.(*types.Interface); ok {
|
|
|
|
y = emitConv(f, y, x.Type())
|
|
|
|
} else if _, ok := yt.(*types.Interface); ok {
|
|
|
|
x = emitConv(f, x, y.Type())
|
2013-07-16 11:50:08 -06:00
|
|
|
} else if _, ok := x.(*Const); ok {
|
2013-05-17 14:25:48 -06:00
|
|
|
x = emitConv(f, x, y.Type())
|
2013-07-16 11:50:08 -06:00
|
|
|
} else if _, ok := y.(*Const); ok {
|
2013-05-17 14:25:48 -06:00
|
|
|
y = emitConv(f, y, x.Type())
|
|
|
|
} else {
|
|
|
|
// other cases, e.g. channels. No-op.
|
|
|
|
}
|
|
|
|
|
|
|
|
v := &BinOp{
|
|
|
|
Op: op,
|
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
}
|
2013-05-30 07:59:17 -06:00
|
|
|
v.setPos(pos)
|
2013-05-17 14:25:48 -06:00
|
|
|
v.setType(tBool)
|
|
|
|
return f.emit(v)
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// isValuePreserving returns true if a conversion from ut_src to
|
|
|
|
// ut_dst is value-preserving, i.e. just a change of type.
|
|
|
|
// Precondition: neither argument is a named type.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
func isValuePreserving(ut_src, ut_dst types.Type) bool {
|
|
|
|
// Identical underlying types?
|
|
|
|
if types.IsIdentical(ut_dst, ut_src) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ut_dst.(type) {
|
|
|
|
case *types.Chan:
|
|
|
|
// Conversion between channel types?
|
|
|
|
_, ok := ut_src.(*types.Chan)
|
|
|
|
return ok
|
|
|
|
|
|
|
|
case *types.Pointer:
|
|
|
|
// Conversion between pointers with identical base types?
|
|
|
|
_, ok := ut_src.(*types.Pointer)
|
|
|
|
return ok
|
|
|
|
|
|
|
|
case *types.Signature:
|
2013-06-26 11:18:31 -06:00
|
|
|
// Conversion from (T) func f() method to f(T) function?
|
2013-05-17 15:02:47 -06:00
|
|
|
_, ok := ut_src.(*types.Signature)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// emitConv emits to f code to convert Value val to exactly type typ,
|
|
|
|
// and returns the converted value. Implicit conversions are required
|
|
|
|
// by language assignability rules in assignments, parameter passing,
|
2013-07-26 19:49:27 -06:00
|
|
|
// etc. Conversions cannot fail dynamically.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
func emitConv(f *Function, val Value, typ types.Type) Value {
|
2013-05-17 15:02:47 -06:00
|
|
|
t_src := val.Type()
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
// Identical types? Conversion is a no-op.
|
2013-05-17 15:02:47 -06:00
|
|
|
if types.IsIdentical(t_src, typ) {
|
2013-05-17 14:25:48 -06:00
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
ut_dst := typ.Underlying()
|
|
|
|
ut_src := t_src.Underlying()
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// Just a change of type, but not value or representation?
|
|
|
|
if isValuePreserving(ut_src, ut_dst) {
|
|
|
|
c := &ChangeType{X: val}
|
2013-05-17 14:25:48 -06:00
|
|
|
c.setType(typ)
|
|
|
|
return f.emit(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conversion to, or construction of a value of, an interface type?
|
|
|
|
if _, ok := ut_dst.(*types.Interface); ok {
|
|
|
|
// Assignment from one interface type to another?
|
|
|
|
if _, ok := ut_src.(*types.Interface); ok {
|
2013-07-26 19:49:27 -06:00
|
|
|
c := &ChangeInterface{X: val}
|
|
|
|
c.setType(typ)
|
|
|
|
return f.emit(c)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-07-16 11:50:08 -06:00
|
|
|
// Untyped nil constant? Return interface-typed nil constant.
|
2013-05-17 14:25:48 -06:00
|
|
|
if ut_src == tUntypedNil {
|
2013-07-16 11:50:08 -06:00
|
|
|
return nilConst(typ)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert (non-nil) "untyped" literals to their default type.
|
2013-05-17 15:02:47 -06:00
|
|
|
if t, ok := ut_src.(*types.Basic); ok && t.Info()&types.IsUntyped != 0 {
|
2013-05-17 14:25:48 -06:00
|
|
|
val = emitConv(f, val, DefaultType(ut_src))
|
|
|
|
}
|
|
|
|
|
go.tools/ssa: fix computation of set of types requiring method sets.
Motivation:
Previously, we assumed that the set of types for which a
complete method set (containing all synthesized wrapper
functions) is required at runtime was the set of types
used as operands to some *ssa.MakeInterface instruction.
In fact, this is an underapproximation because types can
be derived from other ones via reflection, and some of
these may need methods. The reflect.Type API allows *T to
be derived from T, and these may have different method
sets. Reflection also allows almost any subcomponent of a
type to be accessed (with one exception: given T, defined
'type T struct{S}', you can reach S but not struct{S}).
As a result, the pointer analysis was unable to generate
all necessary constraints before running the solver,
causing a crash when reflection derives types whose
methods are unavailable. (A similar problem would afflict
an ahead-of-time compiler based on ssa. The ssa/interp
interpreter was immune only because it does not require
all wrapper methods to be created before execution
begins.)
Description:
This change causes the SSA builder to record, for each
package, the set of all types with non-empty method sets that
are referenced within that package. This set is accessed via
Packages.TypesWithMethodSets(). Program.TypesWithMethodSets()
returns its union across all packages.
The set of references that matter are:
- types of operands to some MakeInterface instruction (as before)
- types of all exported package members
- all subcomponents of the above, recursively.
This is a conservative approximation to the set of types
whose methods may be called dynamically.
We define the owning package of a type as follows:
- the owner of a named type is the package in which it is defined;
- the owner of a pointer-to-named type is the owner of that named type;
- the owner of all other types is nil.
A package must include the method sets for all types that it
owns, and all subcomponents of that type that are not owned by
another package, recursively. Types with an owner appear in
exactly one package; types with no owner (such as struct{T})
may appear within multiple packages.
(A typical Go compiler would emit multiple copies of these
methods as weak symbols; a typical linker would eliminate
duplicates.)
Also:
- go/types/typemap: implement hash function for *Tuple.
- pointer: generate nodes/constraints for all of
ssa.Program.TypesWithMethodSets().
Add rtti.go regression test.
- Add API test of Package.TypesWithMethodSets().
- Set Function.Pkg to nil (again) for wrapper functions,
since these may be shared by many packages.
- Remove a redundant logging statement.
- Document that ssa CREATE phase is in fact sequential.
Fixes golang/go#6605
R=gri
CC=golang-dev
https://golang.org/cl/14920056
2013-10-23 15:07:52 -06:00
|
|
|
f.Pkg.needMethodsOf(val.Type())
|
2013-06-13 12:43:35 -06:00
|
|
|
mi := &MakeInterface{X: val}
|
2013-05-17 14:25:48 -06:00
|
|
|
mi.setType(typ)
|
|
|
|
return f.emit(mi)
|
|
|
|
}
|
|
|
|
|
2013-07-16 11:50:08 -06:00
|
|
|
// Conversion of a constant to a non-interface type results in
|
|
|
|
// a new constant of the destination type and (initially) the
|
2013-05-17 14:25:48 -06:00
|
|
|
// same abstract value. We don't compute the representation
|
|
|
|
// change yet; this defers the point at which the number of
|
|
|
|
// possible representations explodes.
|
2013-07-16 11:50:08 -06:00
|
|
|
if c, ok := val.(*Const); ok {
|
|
|
|
return NewConst(c.Value, typ)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A representation-changing conversion.
|
2013-05-17 15:02:47 -06:00
|
|
|
c := &Convert{X: val}
|
2013-05-17 14:25:48 -06:00
|
|
|
c.setType(typ)
|
|
|
|
return f.emit(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// emitStore emits to f an instruction to store value val at location
|
|
|
|
// addr, applying implicit conversions as required by assignabilty rules.
|
|
|
|
//
|
2013-05-30 07:59:17 -06:00
|
|
|
func emitStore(f *Function, addr, val Value) *Store {
|
|
|
|
s := &Store{
|
2013-05-17 14:25:48 -06:00
|
|
|
Addr: addr,
|
2013-07-12 22:09:33 -06:00
|
|
|
Val: emitConv(f, val, deref(addr.Type())),
|
2013-05-30 07:59:17 -06:00
|
|
|
}
|
|
|
|
f.emit(s)
|
|
|
|
return s
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// emitJump emits to f a jump to target, and updates the control-flow graph.
|
|
|
|
// Postcondition: f.currentBlock is nil.
|
|
|
|
//
|
|
|
|
func emitJump(f *Function, target *BasicBlock) {
|
|
|
|
b := f.currentBlock
|
|
|
|
b.emit(new(Jump))
|
|
|
|
addEdge(b, target)
|
|
|
|
f.currentBlock = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// emitIf emits to f a conditional jump to tblock or fblock based on
|
|
|
|
// cond, and updates the control-flow graph.
|
|
|
|
// Postcondition: f.currentBlock is nil.
|
|
|
|
//
|
|
|
|
func emitIf(f *Function, cond Value, tblock, fblock *BasicBlock) {
|
|
|
|
b := f.currentBlock
|
|
|
|
b.emit(&If{Cond: cond})
|
|
|
|
addEdge(b, tblock)
|
|
|
|
addEdge(b, fblock)
|
|
|
|
f.currentBlock = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// emitExtract emits to f an instruction to extract the index'th
|
|
|
|
// component of tuple, ascribing it type typ. It returns the
|
|
|
|
// extracted value.
|
|
|
|
//
|
|
|
|
func emitExtract(f *Function, tuple Value, index int, typ types.Type) Value {
|
|
|
|
e := &Extract{Tuple: tuple, Index: index}
|
|
|
|
// In all cases but one (tSelect's recv), typ is redundant w.r.t.
|
2013-05-30 07:59:17 -06:00
|
|
|
// tuple.Type().(*types.Tuple).Values[index].Type.
|
2013-05-17 14:25:48 -06:00
|
|
|
e.setType(typ)
|
|
|
|
return f.emit(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// emitTypeAssert emits to f a type assertion value := x.(t) and
|
|
|
|
// returns the value. x.Type() must be an interface.
|
|
|
|
//
|
2013-06-13 12:43:35 -06:00
|
|
|
func emitTypeAssert(f *Function, x Value, t types.Type, pos token.Pos) Value {
|
2013-05-17 14:25:48 -06:00
|
|
|
a := &TypeAssert{X: x, AssertedType: t}
|
2013-06-13 12:43:35 -06:00
|
|
|
a.setPos(pos)
|
2013-05-17 14:25:48 -06:00
|
|
|
a.setType(t)
|
|
|
|
return f.emit(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// emitTypeTest emits to f a type test value,ok := x.(t) and returns
|
|
|
|
// a (value, ok) tuple. x.Type() must be an interface.
|
|
|
|
//
|
2013-06-13 12:43:35 -06:00
|
|
|
func emitTypeTest(f *Function, x Value, t types.Type, pos token.Pos) Value {
|
2013-05-17 14:25:48 -06:00
|
|
|
a := &TypeAssert{
|
|
|
|
X: x,
|
|
|
|
AssertedType: t,
|
|
|
|
CommaOk: true,
|
|
|
|
}
|
2013-06-13 12:43:35 -06:00
|
|
|
a.setPos(pos)
|
2013-05-17 15:02:47 -06:00
|
|
|
a.setType(types.NewTuple(
|
2013-06-04 13:15:41 -06:00
|
|
|
types.NewVar(token.NoPos, nil, "value", t),
|
2013-05-17 14:25:48 -06:00
|
|
|
varOk,
|
2013-05-17 15:02:47 -06:00
|
|
|
))
|
2013-05-17 14:25:48 -06:00
|
|
|
return f.emit(a)
|
|
|
|
}
|
|
|
|
|
2013-05-22 15:56:18 -06:00
|
|
|
// emitTailCall emits to f a function call in tail position. The
|
|
|
|
// caller is responsible for all fields of 'call' except its type.
|
2013-06-14 13:50:37 -06:00
|
|
|
// Intended for wrapper methods.
|
2013-05-17 14:25:48 -06:00
|
|
|
// Precondition: f does/will not use deferred procedure calls.
|
|
|
|
// Postcondition: f.currentBlock is nil.
|
|
|
|
//
|
|
|
|
func emitTailCall(f *Function, call *Call) {
|
2013-05-17 15:02:47 -06:00
|
|
|
tresults := f.Signature.Results()
|
|
|
|
nr := tresults.Len()
|
2013-05-17 14:25:48 -06:00
|
|
|
if nr == 1 {
|
2013-05-30 07:59:17 -06:00
|
|
|
call.typ = tresults.At(0).Type()
|
2013-05-17 14:25:48 -06:00
|
|
|
} else {
|
2013-05-30 07:59:17 -06:00
|
|
|
call.typ = tresults
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
tuple := f.emit(call)
|
2013-10-08 10:31:39 -06:00
|
|
|
var ret Return
|
2013-05-17 14:25:48 -06:00
|
|
|
switch nr {
|
|
|
|
case 0:
|
|
|
|
// no-op
|
|
|
|
case 1:
|
|
|
|
ret.Results = []Value{tuple}
|
|
|
|
default:
|
2013-05-17 15:02:47 -06:00
|
|
|
for i := 0; i < nr; i++ {
|
|
|
|
v := emitExtract(f, tuple, i, tresults.At(i).Type())
|
2013-05-17 14:25:48 -06:00
|
|
|
// TODO(adonovan): in principle, this is required:
|
|
|
|
// v = emitConv(f, o.Type, f.Signature.Results[i].Type)
|
|
|
|
// but in practice emitTailCall is only used when
|
|
|
|
// the types exactly match.
|
|
|
|
ret.Results = append(ret.Results, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.emit(&ret)
|
|
|
|
f.currentBlock = nil
|
|
|
|
}
|
2013-07-19 15:35:29 -06:00
|
|
|
|
|
|
|
// emitImplicitSelections emits to f code to apply the sequence of
|
|
|
|
// implicit field selections specified by indices to base value v, and
|
|
|
|
// returns the selected value.
|
|
|
|
//
|
2013-07-26 09:22:34 -06:00
|
|
|
// If v is the address of a struct, the result will be the address of
|
|
|
|
// a field; if it is the value of a struct, the result will be the
|
|
|
|
// value of a field.
|
|
|
|
//
|
2013-07-19 15:35:29 -06:00
|
|
|
func emitImplicitSelections(f *Function, v Value, indices []int) Value {
|
|
|
|
for _, index := range indices {
|
|
|
|
fld := deref(v.Type()).Underlying().(*types.Struct).Field(index)
|
|
|
|
|
|
|
|
if isPointer(v.Type()) {
|
|
|
|
instr := &FieldAddr{
|
|
|
|
X: v,
|
|
|
|
Field: index,
|
|
|
|
}
|
|
|
|
instr.setType(types.NewPointer(fld.Type()))
|
|
|
|
v = f.emit(instr)
|
|
|
|
// Load the field's value iff indirectly embedded.
|
|
|
|
if isPointer(fld.Type()) {
|
|
|
|
v = emitLoad(f, v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
instr := &Field{
|
|
|
|
X: v,
|
|
|
|
Field: index,
|
|
|
|
}
|
|
|
|
instr.setType(fld.Type())
|
|
|
|
v = f.emit(instr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2013-07-26 09:22:34 -06:00
|
|
|
|
|
|
|
// emitFieldSelection emits to f code to select the index'th field of v.
|
|
|
|
//
|
|
|
|
// If wantAddr, the input must be a pointer-to-struct and the result
|
|
|
|
// will be the field's address; otherwise the result will be the
|
|
|
|
// field's value.
|
|
|
|
//
|
|
|
|
func emitFieldSelection(f *Function, v Value, index int, wantAddr bool, pos token.Pos) Value {
|
|
|
|
fld := deref(v.Type()).Underlying().(*types.Struct).Field(index)
|
|
|
|
if isPointer(v.Type()) {
|
|
|
|
instr := &FieldAddr{
|
|
|
|
X: v,
|
|
|
|
Field: index,
|
|
|
|
}
|
|
|
|
instr.setPos(pos)
|
|
|
|
instr.setType(types.NewPointer(fld.Type()))
|
|
|
|
v = f.emit(instr)
|
|
|
|
// Load the field's value iff we don't want its address.
|
|
|
|
if !wantAddr {
|
|
|
|
v = emitLoad(f, v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
instr := &Field{
|
|
|
|
X: v,
|
|
|
|
Field: index,
|
|
|
|
}
|
|
|
|
instr.setPos(pos)
|
|
|
|
instr.setType(fld.Type())
|
|
|
|
v = f.emit(instr)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
|
|
|
|
// createRecoverBlock emits to f a block of code to return after a
|
|
|
|
// recovered panic, and sets f.Recover to it.
|
|
|
|
//
|
|
|
|
// If f's result parameters are named, the code loads and returns
|
|
|
|
// their current values, otherwise it returns the zero values of their
|
|
|
|
// type.
|
|
|
|
//
|
|
|
|
// Idempotent.
|
|
|
|
//
|
|
|
|
func createRecoverBlock(f *Function) {
|
|
|
|
if f.Recover != nil {
|
|
|
|
return // already created
|
|
|
|
}
|
|
|
|
saved := f.currentBlock
|
|
|
|
|
|
|
|
f.Recover = f.newBasicBlock("recover")
|
|
|
|
f.currentBlock = f.Recover
|
|
|
|
|
|
|
|
var results []Value
|
|
|
|
if f.namedResults != nil {
|
|
|
|
// Reload NRPs to form value tuple.
|
|
|
|
for _, r := range f.namedResults {
|
|
|
|
results = append(results, emitLoad(f, r))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
R := f.Signature.Results()
|
|
|
|
for i, n := 0, R.Len(); i < n; i++ {
|
|
|
|
T := R.At(i).Type()
|
|
|
|
var v Value
|
|
|
|
|
|
|
|
// Return zero value of each result type.
|
|
|
|
switch T.Underlying().(type) {
|
|
|
|
case *types.Struct, *types.Array:
|
|
|
|
v = emitLoad(f, f.addLocal(T, token.NoPos))
|
|
|
|
default:
|
|
|
|
v = zeroConst(T)
|
|
|
|
}
|
|
|
|
results = append(results, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.emit(&Return{Results: results})
|
|
|
|
|
|
|
|
f.currentBlock = saved
|
|
|
|
}
|