Skip to content

Commit

Permalink
fix extern func definition with pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Sep 7, 2024
1 parent 25966b4 commit 45a122a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
15 changes: 14 additions & 1 deletion compiler/compiler/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func (c *Compiler) compileDefineFuncNode(v *parser.DefineFuncNode) value.Value {
LlvmReturnType: funcRetType,
ReturnTypes: treReturnTypes,
IsVariadic: isVariadicFunc,
IsExtern: v.IsExtern,
ArgumentTypes: treParams,
}

Expand Down Expand Up @@ -482,6 +483,7 @@ func (c *Compiler) compileCallNode(v *parser.CallNode) value.Value {
FuncType: ifaceMethod.LlvmJumpFunction.Type(),
ReturnTypes: ifaceMethod.ReturnTypes,
LlvmReturnType: returnType,
ArgumentTypes: ifaceMethod.ArgumentTypes,
}
fn = ifaceMethod.LlvmJumpFunction
} else {
Expand Down Expand Up @@ -538,7 +540,7 @@ func (c *Compiler) compileCallNode(v *parser.CallNode) value.Value {
val := internal.LoadIfVariable(c.contextBlock, v)

// Convert strings and arrays to i8* when calling external functions
if fnType.IsExternal {
if fnType.IsBuiltin {
if v.Type.Name() == "string" {
llvmArgs[i] = c.contextBlock.NewExtractValue(val, 1)
continue
Expand All @@ -549,6 +551,17 @@ func (c *Compiler) compileCallNode(v *parser.CallNode) value.Value {
continue
}
}
if fnType.IsExtern {
// Convert pointer to target type as needed
if len(fnType.ArgumentTypes) > 0 {
if _, isPointer := v.Type.(*types.Pointer); isPointer {
if arg := fnType.ArgumentTypes[i]; arg != v.Type {
val = c.contextBlock.NewBitCast(val, arg.LLVM())
}
}
}
}


llvmArgs[i] = val
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/compiler/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (p *pkg) setExternal(internalName string, fn *ir.Func, variadic bool) value
Type: &types.Function{
LlvmReturnType: types.Void,
FuncType: fn.Type(),
IsExternal: true,
IsBuiltin: true,
},
Value: fn,
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/compiler/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ type Function struct {
ReturnTypes []Type

IsVariadic bool
IsExtern bool
ArgumentTypes []Type
IsExternal bool
IsBuiltin bool

// Is used when calling an interface method
JumpFunction *ir.Func
Expand Down

0 comments on commit 45a122a

Please sign in to comment.