1
0
mirror of https://github.com/golang/go synced 2024-10-05 16:31:21 -06:00

[dev.ssa] cmd/compile: add constBool helpers

Change-Id: I1f93ea65bbdc895cd4eff7545e1688a64d85aae5
Reviewed-on: https://go-review.googlesource.com/14520
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2015-09-08 16:52:25 -07:00
parent f5c53e0deb
commit cea441427e
2 changed files with 15 additions and 8 deletions

View File

@ -371,7 +371,10 @@ func (s *state) entryNewValue2(op ssa.Op, t ssa.Type, arg0, arg1 *ssa.Value) *ss
return s.f.Entry.NewValue2(s.peekLine(), op, t, arg0, arg1)
}
// constInt* routines add a new const int value to the entry block.
// const* routines add a new const value to the entry block.
func (s *state) constBool(c bool) *ssa.Value {
return s.f.ConstBool(s.peekLine(), Types[TBOOL], c)
}
func (s *state) constInt8(t ssa.Type, c int8) *ssa.Value {
return s.f.ConstInt8(s.peekLine(), t, c)
}
@ -647,7 +650,7 @@ func (s *state) stmt(n *Node) {
if n.Left != nil {
cond = s.expr(n.Left)
} else {
cond = s.entryNewValue0I(ssa.OpConstBool, Types[TBOOL], 1) // 1 = true
cond = s.constBool(true)
}
b = s.endBlock()
b.Kind = ssa.BlockIf
@ -1223,11 +1226,7 @@ func (s *state) expr(n *Node) *ssa.Value {
case CTSTR:
return s.entryNewValue0A(ssa.OpConstString, n.Type, n.Val().U)
case CTBOOL:
if n.Val().U.(bool) {
return s.entryNewValue0I(ssa.OpConstBool, Types[TBOOL], 1) // 1 = true
} else {
return s.entryNewValue0I(ssa.OpConstBool, Types[TBOOL], 0) // 0 = false
}
return s.constBool(n.Val().U.(bool))
case CTNIL:
t := n.Type
switch {
@ -1947,7 +1946,7 @@ func (s *state) zeroVal(t *Type) *ssa.Value {
case t.IsPtr():
return s.entryNewValue0(ssa.OpConstNil, t)
case t.IsBoolean():
return s.entryNewValue0I(ssa.OpConstBool, Types[TBOOL], 0) // 0 = false
return s.constBool(false)
case t.IsInterface():
return s.entryNewValue0(ssa.OpConstInterface, t)
case t.IsSlice():

View File

@ -266,6 +266,14 @@ func (b *Block) NewValue3I(line int32, op Op, t Type, aux int64, arg0, arg1, arg
}
// ConstInt returns an int constant representing its argument.
func (f *Func) ConstBool(line int32, t Type, c bool) *Value {
// TODO: cache?
i := int64(0)
if c {
i = 1
}
return f.Entry.NewValue0I(line, OpConstBool, t, i)
}
func (f *Func) ConstInt8(line int32, t Type, c int8) *Value {
// TODO: cache?
return f.Entry.NewValue0I(line, OpConst8, t, int64(c))