2015-03-03 13:38:14 -08:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
|
|
|
|
|
package ssa
|
|
|
|
|
|
2015-09-04 06:33:56 -05:00
|
|
|
import (
|
|
|
|
|
"math"
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
2015-06-25 23:13:57 -05:00
|
|
|
|
2015-03-03 13:38:14 -08:00
|
|
|
// A Func represents a Go func declaration (or function literal) and
|
|
|
|
|
// its body. This package compiles each Func independently.
|
|
|
|
|
type Func struct {
|
2015-07-28 10:56:39 -07:00
|
|
|
Config *Config // architecture information
|
|
|
|
|
Name string // e.g. bytes·Compare
|
|
|
|
|
Type Type // type signature of the function.
|
|
|
|
|
StaticData interface{} // associated static data, untouched by the ssa package
|
|
|
|
|
Blocks []*Block // unordered set of all basic blocks (note: not indexable by ID)
|
|
|
|
|
Entry *Block // the entry basic block
|
|
|
|
|
bid idAlloc // block ID allocator
|
|
|
|
|
vid idAlloc // value ID allocator
|
2015-03-03 13:38:14 -08:00
|
|
|
|
2015-08-03 12:33:03 -07:00
|
|
|
scheduled bool // Values in Blocks are in final order
|
|
|
|
|
|
2015-03-03 13:38:14 -08:00
|
|
|
// when register allocation is done, maps value ids to locations
|
|
|
|
|
RegAlloc []Location
|
2015-10-22 14:22:38 -07:00
|
|
|
|
2015-11-02 08:10:26 -08:00
|
|
|
// map from LocalSlot to set of Values that we want to store in that slot.
|
|
|
|
|
NamedValues map[LocalSlot][]*Value
|
2015-10-22 14:22:38 -07:00
|
|
|
// Names is a copy of NamedValues.Keys. We keep a separate list
|
|
|
|
|
// of keys to make iteration order deterministic.
|
2015-11-02 08:10:26 -08:00
|
|
|
Names []LocalSlot
|
2015-03-03 13:38:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NumBlocks returns an integer larger than the id of any Block in the Func.
|
|
|
|
|
func (f *Func) NumBlocks() int {
|
|
|
|
|
return f.bid.num()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NumValues returns an integer larger than the id of any Value in the Func.
|
|
|
|
|
func (f *Func) NumValues() int {
|
|
|
|
|
return f.vid.num()
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 23:13:57 -05:00
|
|
|
const (
|
|
|
|
|
blockSize = 100
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// blockPool provides a contiguous array of Blocks which
|
|
|
|
|
// improves the speed of traversing dominator trees.
|
|
|
|
|
type blockPool struct {
|
|
|
|
|
blocks []Block
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (bp *blockPool) newBlock() *Block {
|
|
|
|
|
bp.mu.Lock()
|
|
|
|
|
defer bp.mu.Unlock()
|
|
|
|
|
|
2015-07-11 16:30:24 +02:00
|
|
|
if len(bp.blocks) == 0 {
|
2015-06-25 23:13:57 -05:00
|
|
|
bp.blocks = make([]Block, blockSize, blockSize)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res := &bp.blocks[0]
|
|
|
|
|
bp.blocks = bp.blocks[1:]
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bp blockPool
|
|
|
|
|
|
2015-03-03 13:38:14 -08:00
|
|
|
// NewBlock returns a new block of the given kind and appends it to f.Blocks.
|
|
|
|
|
func (f *Func) NewBlock(kind BlockKind) *Block {
|
2015-06-25 23:13:57 -05:00
|
|
|
b := bp.newBlock()
|
|
|
|
|
b.ID = f.bid.get()
|
|
|
|
|
b.Kind = kind
|
|
|
|
|
b.Func = f
|
2015-03-03 13:38:14 -08:00
|
|
|
f.Blocks = append(f.Blocks, b)
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 21:29:25 -07:00
|
|
|
// NewValue0 returns a new value in the block with no arguments and zero aux values.
|
|
|
|
|
func (b *Block) NewValue0(line int32, op Op, t Type) *Value {
|
2015-03-03 13:38:14 -08:00
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
Block: b,
|
2015-06-14 09:52:13 -07:00
|
|
|
Line: line,
|
2015-03-03 13:38:14 -08:00
|
|
|
}
|
|
|
|
|
v.Args = v.argstorage[:0]
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 21:29:25 -07:00
|
|
|
// NewValue returns a new value in the block with no arguments and an auxint value.
|
|
|
|
|
func (b *Block) NewValue0I(line int32, op Op, t Type, auxint int64) *Value {
|
|
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
AuxInt: auxint,
|
|
|
|
|
Block: b,
|
2015-06-14 09:52:13 -07:00
|
|
|
Line: line,
|
2015-06-11 21:29:25 -07:00
|
|
|
}
|
|
|
|
|
v.Args = v.argstorage[:0]
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue returns a new value in the block with no arguments and an aux value.
|
|
|
|
|
func (b *Block) NewValue0A(line int32, op Op, t Type, aux interface{}) *Value {
|
|
|
|
|
if _, ok := aux.(int64); ok {
|
|
|
|
|
// Disallow int64 aux values. They should be in the auxint field instead.
|
|
|
|
|
// Maybe we want to allow this at some point, but for now we disallow it
|
|
|
|
|
// to prevent errors like using NewValue1A instead of NewValue1I.
|
2015-06-24 14:03:39 -07:00
|
|
|
b.Fatalf("aux field has int64 type op=%s type=%s aux=%v", op, t, aux)
|
2015-06-11 21:29:25 -07:00
|
|
|
}
|
2015-04-15 15:51:25 -07:00
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
Aux: aux,
|
|
|
|
|
Block: b,
|
2015-06-13 11:01:16 -07:00
|
|
|
Line: line,
|
2015-04-15 15:51:25 -07:00
|
|
|
}
|
2015-06-11 21:29:25 -07:00
|
|
|
v.Args = v.argstorage[:0]
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue returns a new value in the block with no arguments and both an auxint and aux values.
|
|
|
|
|
func (b *Block) NewValue0IA(line int32, op Op, t Type, auxint int64, aux interface{}) *Value {
|
|
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
AuxInt: auxint,
|
|
|
|
|
Aux: aux,
|
|
|
|
|
Block: b,
|
2015-06-14 09:52:13 -07:00
|
|
|
Line: line,
|
2015-06-11 21:29:25 -07:00
|
|
|
}
|
|
|
|
|
v.Args = v.argstorage[:0]
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue1 returns a new value in the block with one argument and zero aux values.
|
|
|
|
|
func (b *Block) NewValue1(line int32, op Op, t Type, arg *Value) *Value {
|
|
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
Block: b,
|
2015-06-14 09:52:13 -07:00
|
|
|
Line: line,
|
2015-06-11 21:29:25 -07:00
|
|
|
}
|
2015-04-15 15:51:25 -07:00
|
|
|
v.Args = v.argstorage[:1]
|
|
|
|
|
v.Args[0] = arg
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 21:29:25 -07:00
|
|
|
// NewValue1I returns a new value in the block with one argument and an auxint value.
|
|
|
|
|
func (b *Block) NewValue1I(line int32, op Op, t Type, auxint int64, arg *Value) *Value {
|
|
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
AuxInt: auxint,
|
|
|
|
|
Block: b,
|
2015-06-14 09:52:13 -07:00
|
|
|
Line: line,
|
2015-06-11 21:29:25 -07:00
|
|
|
}
|
|
|
|
|
v.Args = v.argstorage[:1]
|
|
|
|
|
v.Args[0] = arg
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue1A returns a new value in the block with one argument and an aux value.
|
|
|
|
|
func (b *Block) NewValue1A(line int32, op Op, t Type, aux interface{}, arg *Value) *Value {
|
2015-04-15 15:51:25 -07:00
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
Aux: aux,
|
|
|
|
|
Block: b,
|
2015-06-13 11:01:16 -07:00
|
|
|
Line: line,
|
2015-04-15 15:51:25 -07:00
|
|
|
}
|
2015-06-11 21:29:25 -07:00
|
|
|
v.Args = v.argstorage[:1]
|
|
|
|
|
v.Args[0] = arg
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue1IA returns a new value in the block with one argument and both an auxint and aux values.
|
|
|
|
|
func (b *Block) NewValue1IA(line int32, op Op, t Type, auxint int64, aux interface{}, arg *Value) *Value {
|
|
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
AuxInt: auxint,
|
|
|
|
|
Aux: aux,
|
|
|
|
|
Block: b,
|
2015-06-14 09:52:13 -07:00
|
|
|
Line: line,
|
2015-06-11 21:29:25 -07:00
|
|
|
}
|
|
|
|
|
v.Args = v.argstorage[:1]
|
|
|
|
|
v.Args[0] = arg
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewValue2 returns a new value in the block with two arguments and zero aux values.
|
|
|
|
|
func (b *Block) NewValue2(line int32, op Op, t Type, arg0, arg1 *Value) *Value {
|
|
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
Block: b,
|
2015-06-13 11:01:16 -07:00
|
|
|
Line: line,
|
2015-06-11 21:29:25 -07:00
|
|
|
}
|
2015-04-15 15:51:25 -07:00
|
|
|
v.Args = v.argstorage[:2]
|
|
|
|
|
v.Args[0] = arg0
|
|
|
|
|
v.Args[1] = arg1
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-27 15:45:20 +01:00
|
|
|
// NewValue2I returns a new value in the block with two arguments and an auxint value.
|
|
|
|
|
func (b *Block) NewValue2I(line int32, op Op, t Type, aux int64, arg0, arg1 *Value) *Value {
|
|
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
AuxInt: aux,
|
|
|
|
|
Block: b,
|
|
|
|
|
Line: line,
|
|
|
|
|
}
|
|
|
|
|
v.Args = v.argstorage[:2]
|
|
|
|
|
v.Args[0] = arg0
|
|
|
|
|
v.Args[1] = arg1
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 21:29:25 -07:00
|
|
|
// NewValue3 returns a new value in the block with three arguments and zero aux values.
|
|
|
|
|
func (b *Block) NewValue3(line int32, op Op, t Type, arg0, arg1, arg2 *Value) *Value {
|
2015-04-15 15:51:25 -07:00
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
Block: b,
|
2015-06-13 11:01:16 -07:00
|
|
|
Line: line,
|
2015-04-15 15:51:25 -07:00
|
|
|
}
|
|
|
|
|
v.Args = []*Value{arg0, arg1, arg2}
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-14 21:47:20 -07:00
|
|
|
// NewValue3I returns a new value in the block with three arguments and an auxint value.
|
|
|
|
|
func (b *Block) NewValue3I(line int32, op Op, t Type, aux int64, arg0, arg1, arg2 *Value) *Value {
|
|
|
|
|
v := &Value{
|
|
|
|
|
ID: b.Func.vid.get(),
|
|
|
|
|
Op: op,
|
|
|
|
|
Type: t,
|
|
|
|
|
AuxInt: aux,
|
|
|
|
|
Block: b,
|
|
|
|
|
Line: line,
|
|
|
|
|
}
|
|
|
|
|
v.Args = []*Value{arg0, arg1, arg2}
|
|
|
|
|
b.Values = append(b.Values, v)
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-03 13:38:14 -08:00
|
|
|
// ConstInt returns an int constant representing its argument.
|
2015-09-08 16:52:25 -07:00
|
|
|
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)
|
|
|
|
|
}
|
2015-07-28 14:19:20 -07:00
|
|
|
func (f *Func) ConstInt8(line int32, t Type, c int8) *Value {
|
2015-03-03 13:38:14 -08:00
|
|
|
// TODO: cache?
|
2015-07-28 14:19:20 -07:00
|
|
|
return f.Entry.NewValue0I(line, OpConst8, t, int64(c))
|
|
|
|
|
}
|
|
|
|
|
func (f *Func) ConstInt16(line int32, t Type, c int16) *Value {
|
|
|
|
|
// TODO: cache?
|
|
|
|
|
return f.Entry.NewValue0I(line, OpConst16, t, int64(c))
|
|
|
|
|
}
|
|
|
|
|
func (f *Func) ConstInt32(line int32, t Type, c int32) *Value {
|
|
|
|
|
// TODO: cache?
|
|
|
|
|
return f.Entry.NewValue0I(line, OpConst32, t, int64(c))
|
|
|
|
|
}
|
|
|
|
|
func (f *Func) ConstInt64(line int32, t Type, c int64) *Value {
|
|
|
|
|
// TODO: cache?
|
|
|
|
|
return f.Entry.NewValue0I(line, OpConst64, t, c)
|
|
|
|
|
}
|
2015-08-12 16:38:11 -04:00
|
|
|
func (f *Func) ConstFloat32(line int32, t Type, c float64) *Value {
|
|
|
|
|
// TODO: cache?
|
2015-09-04 06:33:56 -05:00
|
|
|
return f.Entry.NewValue0I(line, OpConst32F, t, int64(math.Float64bits(c)))
|
2015-08-12 16:38:11 -04:00
|
|
|
}
|
|
|
|
|
func (f *Func) ConstFloat64(line int32, t Type, c float64) *Value {
|
|
|
|
|
// TODO: cache?
|
2015-09-04 06:33:56 -05:00
|
|
|
return f.Entry.NewValue0I(line, OpConst64F, t, int64(math.Float64bits(c)))
|
2015-08-12 16:38:11 -04:00
|
|
|
}
|
2015-06-12 11:01:13 -07:00
|
|
|
|
2015-06-24 14:03:39 -07:00
|
|
|
func (f *Func) Logf(msg string, args ...interface{}) { f.Config.Logf(msg, args...) }
|
|
|
|
|
func (f *Func) Fatalf(msg string, args ...interface{}) { f.Config.Fatalf(msg, args...) }
|
|
|
|
|
func (f *Func) Unimplementedf(msg string, args ...interface{}) { f.Config.Unimplementedf(msg, args...) }
|