Skip to content

Commit

Permalink
krash
Browse files Browse the repository at this point in the history
  • Loading branch information
sangisos committed Aug 6, 2016
1 parent a68c11b commit 543189b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
4 changes: 2 additions & 2 deletions irgen/irgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ func TestGen(t *testing.T) {
}
got, want := module.String(), string(buf)
if got != want {
t.Errorf("%q: module mismatch; expected `%v`, got `%v`", g.path, want, got)
t.Errorf("%q: module mismatch;\n..........expected:..........`\n%v`\n............got:............`\n%v`", g.path, want, got)
// TODO: Remove debug output.
fmt.Println("### FAIL:", g.path)
continue
}
// TODO: Remove debug output.
fmt.Println("PASS:", g.path)
fmt.Println("\n+++ PASS:", g.path)
}
}
86 changes: 82 additions & 4 deletions irgen/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func gen(file *ast.File, info *sem.Info) *ir.Module {
default:
panic(fmt.Sprintf("support for %T not yet implemented", decl))
}
//TODO: Remove debug output:
fmt.Println(decl)
}
fmt.Println(m)
return m.Module
}

Expand Down Expand Up @@ -615,28 +618,103 @@ func (m *Module) callExpr(f *Function, callExpr *ast.CallExpr) value.Value {
// ident lowers the given identifier to LLVM IR, emitting code to f.
func (m *Module) ident(f *Function, ident *ast.Ident) value.Value {
switch typ := m.typeOf(ident).(type) {
case *irtypes.Pointer:
ptr := m.valueFromIdent(f, ident)
fmt.Println("ident Pointer type:", ptr.Type())

// loadTyp, err := irtypes.NewPointer(ptr.Type())
// if err != nil {
// panic(fmt.Sprintf("unable to create newPointer instruction for load for getelementptr; %v", err))
// }
// fmt.Println("loadTyp =", loadTyp.String())
load, err := instruction.NewLoad(typ, ptr)
if err != nil {
panic(fmt.Sprintf("unable to create load instruction for getelementptr; %v", err))
}
loadInst := f.emitInst(load)
fmt.Println("ident pointer: ident:", ident.String(), "typ:", typ, "typ.Elem:", typ.Elem(), "\nptr:", ptr.String(), "ptr.Type:", ptr.Type().String(), "\nloadInst:", loadInst.String(), "loadInst.Type:", loadInst.Type().String(), "\nbasicblock:\n", f.curBlock.String())
zero := constZero(irtypes.I64)
indices := []value.Value{zero}

gepInst, err := instruction.NewGetElementPtr(load.RetType(), loadInst, indices)
if err != nil {
panic(fmt.Sprintf("unable to create getelementptr instruction; %v", err))
}
return f.emitInst(gepInst)
case *irtypes.Array:
fmt.Println("irtypes.Array:", ident.String())
array := m.valueFromIdent(f, ident)
loadInst := array
var loadTyp irtypes.Type
loadTyp = typ

if m.isGlobal(ident) {
load, err := instruction.NewLoad(typ, array)
if err != nil {
panic(fmt.Sprintf("unable to create load instruction for getelementptr; %v", err))
}
loadInst = f.emitInst(load)
loadTyp = load.RetType()
}
if isRef(typ.Elem()) {
fmt.Println("ident array:isRef(typElem): ident:", ident.String(), "array:", array, "array.Type:", array.Type(), "\nbasicblock:\n", f.curBlock.String())
// loadTyp, err := irtypes.NewPointer(array.Type())
// if err != nil {
// panic(fmt.Sprintf("unable to create newPointer instruction for load for getelementptr; %v", err))
// }
// load, err := instruction.NewLoad(loadTyp, array)
// fmt.Println("loadTyp =", loadTyp.String())
// if err != nil {
// panic(fmt.Sprintf("unable to create load instruction for getelementptr; %v", err))
// }
// loadInst = f.emitInst(load)
}
zero := constZero(irtypes.I64)
indices := []value.Value{zero, zero}
gepInst, err := instruction.NewGetElementPtr(typ, array, indices)

gepInst, err := instruction.NewGetElementPtr(loadTyp, loadInst, indices)
if err != nil {
panic(fmt.Sprintf("unable to create getelementptr instruction; %v", err))
}
return f.emitInst(gepInst)
default:
fmt.Println("irtypes:", "ident:", ident, "typ:", typ)
return m.valueFromIdent(f, ident)
}
}

// identUse lowers the given identifier usage to LLVM IR, emitting code to f.
func (m *Module) identUse(f *Function, ident *ast.Ident) value.Value {
v := m.ident(f, ident)
typ := m.typeOf(ident)
var addr value.Value
var err error
if isRef(typ) {
return v

addr = m.ident(f, ident)

fmt.Println("identUse:isRef(typ): ident:", ident, "addr:", addr, "addr.Type:", addr.Type(), "typ:", typ, "\nbasicblock:\n", f.curBlock)

// typ.()
// if isRef(typ.)
// addr = m.valueFromIdent(f, ident)
// fmt.Println("isRef: addr:", addr.String(), "addr.Type:", addr.Type().String(), "typ:", typ.String())
// loadInst, err := instruction.NewLoad(typ, addr)
// ld := f.emitInst(loadInst)
// zero := constZero(irtypes.I64)
// indices := []value.Value{zero, zero}
// gepInst, err := instruction.NewGetElementPtr(ld.Type(), ld, indices)
// if err != nil {
// panic(fmt.Sprintf("unable to create getelementptr instruction; %v", err))
// }
// addr = f.emitInst(gepInst)
return addr
} else {
addr = m.ident(f, ident)

fmt.Println("identUse: ident:", ident.String(), "addr:", addr, "addr.Type:", addr.Type(), "typ:", typ, "\nbasicblock:\n", f.curBlock.String())

}
loadInst, err := instruction.NewLoad(typ, v)
loadInst, err := instruction.NewLoad(typ, addr)
if err != nil {
panic(fmt.Sprintf("unable to create load instruction; %v", err))
}
Expand Down
16 changes: 15 additions & 1 deletion irgen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ func (m *Module) valueFromIdent(f *Function, ident *ast.Ident) value.Value {
panic(fmt.Sprintf("unable to locate value associated with identifier %q (declared at source code position %d)", ident, pos))
}

func (m *Module) isGlobal(ident *ast.Ident) bool {
pos := ident.Decl.Name().Start()
if _, ok := m.idents[pos]; ok {
return true
}
return false
}

// setIdentValue maps the given global identifier to the associated value.
func (m *Module) setIdentValue(ident *ast.Ident, v value.Value) {
pos := ident.Decl.Name().Start()
Expand All @@ -194,7 +202,13 @@ func (f *Function) setIdentValue(ident *ast.Ident, v value.Value) {
// typeOf returns the LLVM IR type of the given expression.
func (m *Module) typeOf(expr ast.Expr) irtypes.Type {
if typ, ok := m.info.Types[expr]; ok {
return toIrType(typ)
irTyp := toIrType(typ)
if iden, ok := expr.(*ast.Ident); ok {
fmt.Println("typeOf(", expr, ") typ:", typ, "irTyp:", irTyp, "global:", m.isGlobal(iden))
} else {
fmt.Println("typeOf(", expr, ") typ:", typ, "irTyp:", irTyp)
}
return irTyp
}
panic(fmt.Sprintf("unable to locate type for expression %v", expr))
}
Expand Down

0 comments on commit 543189b

Please sign in to comment.