Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
- Convert function pointers to stored type when taking function value
- Handle invalid Return in unreachable blocks (Go Issue #7022)
- Use IsAFunction instead of IsGlobalConstant
- Ignore package "C" when computing dependencies in llgo-build
  • Loading branch information
axw committed Dec 28, 2013
1 parent cd336f0 commit a31e8af
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion closures.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func (c *compiler) makeClosure(fn *LLVMValue, bindings []*LLVMValue) *LLVMValue
block = c.builder.CreateBitCast(block, llvm.PointerType(llvm.Int8Type(), 0), "")
// fn is a raw function pointer; ToLLVM yields {*fn, *uint8}.
closure := llvm.Undef(c.types.ToLLVM(fn.Type()))
closure = c.builder.CreateInsertValue(closure, fn.LLVMValue(), 0, "")
fnptr := c.builder.CreateBitCast(fn.LLVMValue(), closure.Type().StructElementTypes()[0], "")
closure = c.builder.CreateInsertValue(closure, fnptr, 0, "")
closure = c.builder.CreateInsertValue(closure, block, 1, "")
return c.NewValue(closure, fn.Type())
}
3 changes: 3 additions & 0 deletions cmd/llgo-build/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func linkdeps(pkg *build.Package, output string) error {
var mkdeps func(pkg *build.Package, imports []string) error
mkdeps = func(pkg *build.Package, imports []string) error {
for _, path := range imports {
if path == "C" {
continue
}
if !deps[path] {
deps[path] = true
pkg, err := build.Import(path, "", 0)
Expand Down
2 changes: 1 addition & 1 deletion indirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *compiler) indirectFunction(fn *LLVMValue, args []*LLVMValue) *LLVMValue
if fnptr.Type().TypeKind() == llvm.StructTypeKind {
fnptr = c.builder.CreateExtractValue(fnval, 0, "")
fnctx = c.builder.CreateExtractValue(fnval, 1, "")
globalfn = fnptr.IsGlobalConstant()
globalfn = !fnptr.IsAFunction().IsNil()
if !globalfn {
nctx++
}
Expand Down
16 changes: 10 additions & 6 deletions ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ func (fr *frame) value(v ssa.Value) (result *LLVMValue) {
f = contextFunction(fr.compiler, f)
}
pair := llvm.ConstNull(fr.llvmtypes.ToLLVM(f.Type()))
pair = llvm.ConstInsertValue(pair, f.LLVMValue(), []uint32{0})
fnptr := llvm.ConstBitCast(f.LLVMValue(), pair.Type().StructElementTypes()[0])
pair = llvm.ConstInsertValue(pair, fnptr, []uint32{0})
result = fr.NewValue(pair, f.Type())
fr.funcvals[v] = result
return result
Expand Down Expand Up @@ -517,7 +518,7 @@ func (fr *frame) instruction(instr ssa.Instruction) {
fr.env[instr] = fr.makeChan(instr.Type(), fr.value(instr.Size))

case *ssa.MakeClosure:
fn := fr.value(instr.Fn)
fn := fr.resolveFunction(instr.Fn.(*ssa.Function))
bindings := make([]*LLVMValue, len(instr.Bindings))
for i, binding := range instr.Bindings {
bindings[i] = fr.value(binding)
Expand Down Expand Up @@ -609,7 +610,12 @@ func (fr *frame) instruction(instr ssa.Instruction) {
case *ssa.Return:
switch n := len(instr.Results); n {
case 0:
fr.builder.CreateRetVoid()
// https://code.google.com/p/go/issues/detail?id=7022
if r := instr.Parent().Signature.Results(); r != nil && r.Len() > 0 {
fr.builder.CreateUnreachable()
} else {
fr.builder.CreateRetVoid()
}
case 1:
fr.builder.CreateRet(fr.value(instr.Results[0]).LLVMValue())
default:
Expand Down Expand Up @@ -850,9 +856,7 @@ func contextFunction(c *compiler, f *LLVMValue) *LLVMValue {
}
c.builder.CreateAggregateRet(results)
}
fnptr = c.builder.CreateBitCast(wrapper, fnptr.Type(), "")
fnval := c.builder.CreateInsertValue(llvm.ConstNull(resultType), fnptr, 0, "")
return c.NewValue(fnval, f.Type())
return c.NewValue(wrapper, f.Type())
}

// phiValue returns a new value with the same value and type as the given Phi.
Expand Down

0 comments on commit a31e8af

Please sign in to comment.