Add support for void function calls

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
Slendi 2024-03-10 02:10:02 +02:00
parent 453689642c
commit d54f9fb2f8
2 changed files with 15 additions and 3 deletions

View File

@ -111,6 +111,9 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
switch type.kind {
case .Integer:
if type.bit_size == 0 {
return LLVMVoidTypeInContext(ctx)
}
return LLVMIntTypeInContext(ctx, uint(type.bit_size))
case .Float:
if type.bit_size == 64 {
@ -142,6 +145,9 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
) {
function_args_type := [dynamic]LLVMTypeRef{}
for arg in fn.children {
if arg == nil {
continue
}
if arg.kind != .VariableDeclaration {
continue
}
@ -257,7 +263,12 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
append(&fn_args, value)
}
call := LLVMBuildCall2(
if node.return_type == nil || (node.return_type.kind == .Integer && node.return_type.bit_size == 0) {
fmt.printf("Void function call: %s\n", name)
return LLVMBuildCall2(builder, fn_type, fn_value, raw_data(fn_args[:]), len(fn_args), cstring(""))
}
return LLVMBuildCall2(
builder,
fn_type,
fn_value,
@ -265,7 +276,6 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
len(fn_args),
cstring(raw_data(name[:])),
)
return call
}
generate_llvm_value :: proc(
@ -283,7 +293,6 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
} else if node.kind == .Identifier {
def := llvm_scope_find_definition(&node.value.([dynamic]u8))
type := llvm_scope_find_type(&node.value.([dynamic]u8))
//LLVMBuildLoad2 :: proc(Builder: LLVMBuilderRef, Ty: LLVMTypeRef, PointerVal: LLVMValueRef, Name: cstring) -> LLVMValueRef ---
def_value := LLVMBuildLoad2(builder, type, def, "loadtmp")
return def_value
}

View File

@ -111,6 +111,7 @@ foreign llvmc {
LLVMFloatTypeInContext :: proc(C: LLVMContextRef) -> LLVMTypeRef ---
LLVMVoidTypeInContext :: proc(C: LLVMContextRef) -> LLVMTypeRef ---
LLVMVoidType :: proc() -> LLVMTypeRef ---
LLVMGetTypeKind :: proc(Ty: LLVMTypeRef) -> LLVMTypeKind ---
@ -176,4 +177,6 @@ foreign llvmc {
LLVMGetParam :: proc(Fn: LLVMValueRef, Index: uint) -> LLVMValueRef ---
LLVMSetValueName2 :: proc(Val: LLVMValueRef, Name: cstring, Length: u64) ---
LLVMGetReturnType :: proc(Fn: LLVMTypeRef) -> LLVMTypeRef ---
}