Skip to content

Commit

Permalink
ir: add elemType to load constructor
Browse files Browse the repository at this point in the history
Updates #112.
  • Loading branch information
mewmew committed Dec 16, 2019
1 parent 082a093 commit e3e7aa1
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 26 deletions.
2 changes: 1 addition & 1 deletion asm/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func Example() {
// Insts: {
// &ir.InstLoad{
// LocalIdent: ir.LocalIdent{LocalName:"", LocalID:1},
// ElemType: &types.IntType{TypeName:"", BitSize:0x20},
// Src: &ir.Global{(CYCLIC REFERENCE)},
// Typ: &types.IntType{TypeName:"", BitSize:0x20},
// Atomic: false,
// Volatile: false,
// SyncScope: "",
Expand Down
2 changes: 1 addition & 1 deletion asm/inst_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (fgen *funcGen) newLoadInst(ident ir.LocalIdent, old *ast.LoadInst) (*ir.In
if err != nil {
return nil, errors.WithStack(err)
}
return &ir.InstLoad{LocalIdent: ident, Typ: elemType}, nil
return &ir.InstLoad{LocalIdent: ident, ElemType: elemType}, nil
}

// newCmpXchgInst returns a new IR cmpxchg instruction (without body but with
Expand Down
6 changes: 3 additions & 3 deletions ir/block_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func (block *Block) NewAlloca(elemType types.Type) *InstAlloca {
// ~~~ [ load ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// NewLoad appends a new load instruction to the basic block based on the given
// source address.
func (block *Block) NewLoad(src value.Value) *InstLoad {
inst := NewLoad(src)
// element type and source address.
func (block *Block) NewLoad(elemType types.Type, src value.Value) *InstLoad {
inst := NewLoad(elemType, src)
block.Insts = append(block.Insts, inst)
return inst
}
Expand Down
2 changes: 1 addition & 1 deletion ir/constant/expr_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ExprGetElementPtr struct {
// really the type used to compute the result type of gep.

// NewGetElementPtr returns a new getelementptr expression based on the given
// source address and element indices.
// element type, source address and element indices.
func NewGetElementPtr(elemType types.Type, src Constant, indices ...Constant) *ExprGetElementPtr {
e := &ExprGetElementPtr{ElemType: elemType, Src: src, Indices: indices}
// Compute type.
Expand Down
2 changes: 1 addition & 1 deletion ir/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Example() {
entry := rand.NewBlock("")

// Create instructions and append them to the entry basic block.
tmp1 := entry.NewLoad(seed)
tmp1 := entry.NewLoad(types.I32, seed)
tmp2 := entry.NewMul(tmp1, a)
tmp3 := entry.NewAdd(tmp2, c)
entry.NewStore(tmp3, seed)
Expand Down
25 changes: 8 additions & 17 deletions ir/inst_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ func (inst *InstAlloca) LLString() string {
type InstLoad struct {
// Name of local variable associated with the result.
LocalIdent
// Element type of src.
ElemType types.Type
// Source address.
Src value.Value

// extra.

// Type of result produced by the instruction.
Typ types.Type
// (optional) Atomic.
Atomic bool
// (optional) Volatile.
Expand All @@ -118,11 +118,10 @@ type InstLoad struct {
Metadata
}

// NewLoad returns a new load instruction based on the given source address.
func NewLoad(src value.Value) *InstLoad {
inst := &InstLoad{Src: src}
// Compute type.
inst.Type()
// NewLoad returns a new load instruction based on the given element type and
// source address.
func NewLoad(elemType types.Type, src value.Value) *InstLoad {
inst := &InstLoad{ElemType: elemType, Src: src}
return inst
}

Expand All @@ -134,15 +133,7 @@ func (inst *InstLoad) String() string {

// Type returns the type of the instruction.
func (inst *InstLoad) Type() types.Type {
// Cache type if not present.
if inst.Typ == nil {
t, ok := inst.Src.Type().(*types.PointerType)
if !ok {
panic(fmt.Errorf("invalid source type; expected *types.PointerType, got %T", inst.Src.Type()))
}
inst.Typ = t.ElemType
}
return inst.Typ
return inst.ElemType
}

// LLString returns the LLVM syntax representation of the instruction.
Expand All @@ -166,7 +157,7 @@ func (inst *InstLoad) LLString() string {
if inst.Volatile {
buf.WriteString(" volatile")
}
fmt.Fprintf(buf, " %s, %s", inst.Typ, inst.Src)
fmt.Fprintf(buf, " %s, %s", inst.ElemType, inst.Src)
if len(inst.SyncScope) > 0 {
fmt.Fprintf(buf, " syncscope(%s)", quote(inst.SyncScope))
}
Expand Down
4 changes: 2 additions & 2 deletions ir/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func Example_main() {
entry.NewStore(constant.NewInt(i32, 16), b)

// %1 = load i32, i32* %a
tmpA := entry.NewLoad(a)
tmpA := entry.NewLoad(types.I32, a)

// %2 = load i32, i32* %b
tmpB := entry.NewLoad(b)
tmpB := entry.NewLoad(types.I32, b)

// %3 = add nsw i32 %1, %2
tmpC := entry.NewAdd(tmpA, tmpB)
Expand Down

0 comments on commit e3e7aa1

Please sign in to comment.