Skip to content

compiler: add support for recursive function types #2175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// Version of the compiler pacakge. Must be incremented each time the compiler
// package changes in a way that affects the generated LLVM module.
// This version is independent of the TinyGo version number.
const Version = 22 // last change: check for divide by zero
const Version = 23 // last change: fix recursive function types

func init() {
llvm.InitializeAllTargets()
Expand Down Expand Up @@ -76,6 +76,7 @@ type compilerContext struct {
targetData llvm.TargetData
intType llvm.Type
i8ptrType llvm.Type // for convenience
rawVoidFuncType llvm.Type // for convenience
funcPtrAddrSpace int
uintptrType llvm.Type
program *ssa.Program
Expand Down Expand Up @@ -121,6 +122,7 @@ func newCompilerContext(moduleName string, machine llvm.TargetMachine, config *C
dummyFuncType := llvm.FunctionType(c.ctx.VoidType(), nil, false)
dummyFunc := llvm.AddFunction(c.mod, "tinygo.dummy", dummyFuncType)
c.funcPtrAddrSpace = dummyFunc.Type().PointerAddressSpace()
c.rawVoidFuncType = dummyFunc.Type()
dummyFunc.EraseFromParentAsFunction()

return c
Expand Down
9 changes: 4 additions & 5 deletions compiler/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *compilerContext) createFuncValue(builder llvm.Builder, funcPtr, context
switch c.FuncImplementation {
case "doubleword":
// Closure is: {context, function pointer}
funcValueScalar = funcPtr
funcValueScalar = llvm.ConstBitCast(funcPtr, c.rawVoidFuncType)
case "switch":
funcValueWithSignatureGlobalName := funcPtr.Name() + "$withSignature"
funcValueWithSignatureGlobal := c.mod.NamedGlobal(funcValueWithSignatureGlobalName)
Expand Down Expand Up @@ -78,11 +78,11 @@ func (b *builder) extractFuncContext(funcValue llvm.Value) llvm.Value {
// value. This may be an expensive operation.
func (b *builder) decodeFuncValue(funcValue llvm.Value, sig *types.Signature) (funcPtr, context llvm.Value) {
context = b.CreateExtractValue(funcValue, 0, "")
llvmSig := b.getRawFuncType(sig)
switch b.FuncImplementation {
case "doubleword":
funcPtr = b.CreateExtractValue(funcValue, 1, "")
funcPtr = b.CreateBitCast(b.CreateExtractValue(funcValue, 1, ""), llvmSig, "")
case "switch":
llvmSig := b.getRawFuncType(sig)
sigGlobal := b.getFuncSignatureID(sig)
funcPtr = b.createRuntimeCall("getFuncPtr", []llvm.Value{funcValue, sigGlobal}, "")
funcPtr = b.CreateIntToPtr(funcPtr, llvmSig, "")
Expand All @@ -96,8 +96,7 @@ func (b *builder) decodeFuncValue(funcValue llvm.Value, sig *types.Signature) (f
func (c *compilerContext) getFuncType(typ *types.Signature) llvm.Type {
switch c.FuncImplementation {
case "doubleword":
rawPtr := c.getRawFuncType(typ)
return c.ctx.StructType([]llvm.Type{c.i8ptrType, rawPtr}, false)
return c.ctx.StructType([]llvm.Type{c.i8ptrType, c.rawVoidFuncType}, false)
case "switch":
return c.getLLVMRuntimeType("funcValue")
default:
Expand Down
6 changes: 3 additions & 3 deletions compiler/testdata/goroutine-cortex-m-qemu.ll
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ entry:
declare void @runtime.printint32(i32, i8*, i8*)

; Function Attrs: nounwind
define hidden void @main.funcGoroutine(i8* %fn.context, void (i32, i8*, i8*)* %fn.funcptr, i8* %context, i8* %parentHandle) unnamed_addr #0 {
define hidden void @main.funcGoroutine(i8* %fn.context, void ()* %fn.funcptr, i8* %context, i8* %parentHandle) unnamed_addr #0 {
entry:
%0 = call i8* @runtime.alloc(i32 12, i8* undef, i8* null) #0
%1 = bitcast i8* %0 to i32*
Expand All @@ -112,8 +112,8 @@ entry:
%3 = bitcast i8* %2 to i8**
store i8* %fn.context, i8** %3, align 4
%4 = getelementptr inbounds i8, i8* %0, i32 8
%5 = bitcast i8* %4 to void (i32, i8*, i8*)**
store void (i32, i8*, i8*)* %fn.funcptr, void (i32, i8*, i8*)** %5, align 4
%5 = bitcast i8* %4 to void ()**
store void ()* %fn.funcptr, void ()** %5, align 4
%stacksize = call i32 @"internal/task.getGoroutineStackSize"(i32 ptrtoint (void (i8*)* @main.funcGoroutine.gowrapper to i32), i8* undef, i8* undef) #0
call void @"internal/task.start"(i32 ptrtoint (void (i8*)* @main.funcGoroutine.gowrapper to i32), i8* nonnull %0, i32 %stacksize, i8* undef, i8* null) #0
ret void
Expand Down
4 changes: 4 additions & 0 deletions testdata/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,7 @@ type issue1304 struct {
func (x issue1304) call() {
// nothing to do
}

type recursiveFuncType func(recursiveFuncType)

var recursiveFunction recursiveFuncType
5 changes: 4 additions & 1 deletion transform/interrupt.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func LowerInterrupts(mod llvm.Module, sizeLevel int) []error {
softwareVector := make(map[int64]llvm.Value)

ctx := mod.Context()
nullptr := llvm.ConstNull(llvm.PointerType(ctx.Int8Type(), 0))
i8ptrType := llvm.PointerType(ctx.Int8Type(), 0)
nullptr := llvm.ConstNull(i8ptrType)
builder := ctx.NewBuilder()
defer builder.Dispose()

Expand Down Expand Up @@ -236,6 +237,8 @@ func LowerInterrupts(mod llvm.Module, sizeLevel int) []error {
// Fill the function declaration with the forwarding call.
// In practice, the called function will often be inlined which avoids
// the extra indirection.
handlerFuncPtrType := llvm.PointerType(llvm.FunctionType(ctx.VoidType(), []llvm.Type{num.Type(), i8ptrType, i8ptrType}, false), handlerFuncPtr.Type().PointerAddressSpace())
handlerFuncPtr = llvm.ConstBitCast(handlerFuncPtr, handlerFuncPtrType)
builder.CreateCall(handlerFuncPtr, []llvm.Value{num, handlerContext, nullptr}, "")

// Replace all ptrtoint uses of the global with the interrupt constant.
Expand Down