Make the compiler work on macOS

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
Slendi 2024-03-10 01:16:25 +02:00
parent 1092c68317
commit e4289d577a
5 changed files with 677 additions and 579 deletions

5
odinfmt.json Normal file
View File

@ -0,0 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/DanielGavin/ols/master/misc/odinfmt.schema.json",
"character_width": "120"
}

View File

@ -33,12 +33,15 @@ llvm_scope_find_type :: proc(name: ^[dynamic]u8) -> LLVMTypeRef {
}
llvm_scope_enter :: proc(name: cstring, basic_block: LLVMBasicBlockRef) {
append(&g_llvm_scope_stack, LLVMScope{
append(
&g_llvm_scope_stack,
LLVMScope {
name = name,
basic_block = basic_block,
definitions = make(map[int]LLVMValueRef),
types = make(map[int]LLVMTypeRef),
})
},
)
}
llvm_scope_leave :: proc() {
@ -63,14 +66,24 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
return fmt.caprintf("scope_%d", scope_num)
}
generate_llvm_integer :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef, node: ^Node) -> LLVMValueRef {
generate_llvm_integer :: proc(
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
node: ^Node,
) -> LLVMValueRef {
bit_size := uint(node.return_type.bit_size)
value := node.value.(u64)
is_signed := node.return_type.is_signed
return LLVMConstInt(LLVMIntTypeInContext(ctx, bit_size), value, LLVMBool(is_signed))
}
generate_llvm_float :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef, node: ^Node) -> LLVMValueRef {
generate_llvm_float :: proc(
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
node: ^Node,
) -> LLVMValueRef {
bit_size := uint(node.return_type.bit_size)
value := node.value.(f64)
if bit_size == 64 {
@ -81,16 +94,17 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
append(
&g_message_list,
message_create(
.Error,
fmt.aprintf("Unsupported float bit size: %d", bit_size),
node.range,
),
message_create(.Error, fmt.aprintf("Unsupported float bit size: %d", bit_size), node.range),
)
return nil
}
generate_llvm_type_from_node :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef, type: ^Type) -> LLVMTypeRef {
generate_llvm_type_from_node :: proc(
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
type: ^Type,
) -> LLVMTypeRef {
if type == nil {
return LLVMVoidTypeInContext(ctx)
}
@ -117,9 +131,15 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
}
generate_llvm_function_extern :: proc(
ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef,
fn: ^Node, scope_number: ^i32
) -> (type: LLVMTypeRef, value: LLVMValueRef) {
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
fn: ^Node,
scope_number: ^i32,
) -> (
type: LLVMTypeRef,
value: LLVMValueRef,
) {
function_args_type := [dynamic]LLVMTypeRef{}
for arg in fn.children {
if arg.kind != .VariableDeclaration {
@ -129,13 +149,49 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
}
function_return_type := generate_llvm_type_from_node(ctx, mod, builder, fn.return_type)
type = LLVMFunctionType(function_return_type, raw_data(function_args_type[:]), len(function_args_type), LLVMBool(0))
type = LLVMFunctionType(
function_return_type,
raw_data(function_args_type[:]),
len(function_args_type),
LLVMBool(0),
)
value = LLVMAddFunction(mod, strings.clone_to_cstring(string(fn.value.([dynamic]u8)[:])), type)
return
}
generate_llvm_function :: proc(
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
fn: ^Node,
scope_number: ^i32,
) -> (
type: LLVMTypeRef,
value: LLVMValueRef,
) {
function_args_type := [dynamic]LLVMTypeRef{}
for arg in fn.children {
if arg.kind != .VariableDeclaration {
continue
}
append(&function_args_type, generate_llvm_type_from_node(ctx, mod, builder, arg.return_type))
}
function_return_type := generate_llvm_type_from_node(ctx, mod, builder, fn.return_type)
type = LLVMFunctionType(
function_return_type,
raw_data(function_args_type[:]),
len(function_args_type),
LLVMBool(0),
)
value = LLVMAddFunction(mod, strings.clone_to_cstring(string(fn.value.([dynamic]u8)[:])), type)
return
}
generate_llvm_function_call :: proc(
ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef,
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
node: ^Node,
) -> LLVMValueRef {
// FIXME: Add support for dot access
@ -143,14 +199,7 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
fn_type := llvm_scope_find_type(name)
fn_value := llvm_scope_find_definition(name)
if fn_type == nil || fn_value == nil {
append(
&g_message_list,
message_create(
.Error,
fmt.aprintf("Function '%s' not found", name),
node.range,
),
)
append(&g_message_list, message_create(.Error, fmt.aprintf("Function '%s' not found", name), node.range))
return nil
}
@ -160,12 +209,21 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
append(&fn_args, value)
}
call := LLVMBuildCall2(builder, fn_type, fn_value, raw_data(fn_args[:]), len(fn_args), cstring(raw_data(name[:])))
call := LLVMBuildCall2(
builder,
fn_type,
fn_value,
raw_data(fn_args[:]),
len(fn_args),
cstring(raw_data(name[:])),
)
return call
}
generate_llvm_value :: proc(
ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef,
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
node: ^Node,
) -> LLVMValueRef {
if node.kind == .Integer {
@ -186,7 +244,9 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
}
generate_llvm_expression :: proc(
ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef,
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
node: ^Node,
) -> LLVMValueRef {
if node.kind == .BinaryExpression {
@ -202,7 +262,9 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
}
generate_llvm_binary_expression :: proc(
ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef,
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
node: ^Node,
) -> LLVMValueRef {
op := node.value_token_kind
@ -211,7 +273,11 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
lhs := generate_llvm_expression(ctx, mod, builder, lhs_node)
rhs := generate_llvm_expression(ctx, mod, builder, rhs_node)
if lhs_node.return_type.kind != rhs_node.return_type.kind {
fmt.panicf("LLVM-C: Binary expression types do not match: {} and {}", lhs_node.return_type.kind, rhs_node.return_type.kind)
fmt.panicf(
"LLVM-C: Binary expression types do not match: {} and {}",
lhs_node.return_type.kind,
rhs_node.return_type.kind,
)
}
is_float := lhs_node.return_type.kind == .Float
is_signed := lhs_node.return_type.is_signed
@ -329,11 +395,7 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
if def == nil {
append(
&g_message_list,
message_create(
.Error,
fmt.aprintf("Variable '%s' not found", name),
node.range,
),
message_create(.Error, fmt.aprintf("Variable '%s' not found", name), node.range),
)
return nil
}
@ -344,8 +406,12 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
}
generate_llvm_if :: proc(
ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef, function: LLVMValueRef,
node: ^Node, scope_number: ^i32
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
function: LLVMValueRef,
node: ^Node,
scope_number: ^i32,
) {
condition_node := node.children[0]
true_node := node.children[1]
@ -392,8 +458,12 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
}
generate_llvm_for :: proc(
ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef, function: LLVMValueRef,
node: ^Node, scope_number: ^i32
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
function: LLVMValueRef,
node: ^Node,
scope_number: ^i32,
) {
sum_nils := 0
for i in node.children {
@ -409,8 +479,10 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
loop_type: LoopType
switch sum_nils {
case 3: loop_type = LoopType.Infinite
case 2: loop_type = LoopType.While
case 3:
loop_type = LoopType.Infinite
case 2:
loop_type = LoopType.While
case:
panic("FIXME: Implement other for loop types")
}
@ -447,7 +519,15 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
}
}
generate_llvm_scope :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuilderRef, function: LLVMValueRef, scope: ^Node, scope_number: ^i32, bb: LLVMBasicBlockRef = nil) {
generate_llvm_scope :: proc(
ctx: LLVMContextRef,
mod: LLVMModuleRef,
builder: LLVMBuilderRef,
function: LLVMValueRef,
scope: ^Node,
scope_number: ^i32,
bb: LLVMBasicBlockRef = nil,
) {
scope_name := get_basic_block_name(scope_number^)
basic_block: LLVMBasicBlockRef
if bb != nil {
@ -463,21 +543,30 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
case .Block:
generate_llvm_scope(ctx, mod, builder, function, node, scope_number)
case .Function:
panic("FIXME: Implement function delarations")
case .ExternFunction:
type, value := generate_llvm_function_extern(ctx, mod, builder, node, scope_number)
//name := LLVMGetValueName2(value, nil)
type, value := generate_llvm_function(ctx, mod, builder, node, scope_number)
llvm_top_scope().definitions[get_character_sum_of_dyn_arr(&node.value.([dynamic]u8))] = value
llvm_top_scope().types[get_character_sum_of_dyn_arr(&node.value.([dynamic]u8))] = type
case .Integer: fallthrough
case .Float: fallthrough
case .Identifier: fallthrough
case .FunctionCall: fallthrough
case .ExternFunction:
type, value := generate_llvm_function_extern(ctx, mod, builder, node, scope_number)
llvm_top_scope().definitions[get_character_sum_of_dyn_arr(&node.value.([dynamic]u8))] = value
llvm_top_scope().types[get_character_sum_of_dyn_arr(&node.value.([dynamic]u8))] = type
case .Integer:
fallthrough
case .Float:
fallthrough
case .Identifier:
fallthrough
case .FunctionCall:
fallthrough
case .BinaryExpression:
generate_llvm_expression(ctx, mod, builder, node)
case .VariableDeclaration:
type := generate_llvm_type_from_node(ctx, mod, builder, node.return_type)
var := LLVMBuildAlloca(builder, type, strings.clone_to_cstring(string(node.children[0].value.([dynamic]u8)[:])))
var := LLVMBuildAlloca(
builder,
type,
strings.clone_to_cstring(string(node.children[0].value.([dynamic]u8)[:])),
)
if node.children[2] != nil {
value := generate_llvm_expression(ctx, mod, builder, node.children[2])
LLVMBuildStore(builder, value, var)

View File

@ -1,6 +1,10 @@
package main
foreign import llvmc "LLVM-C.lib"
when ODIN_OS == .Windows {
foreign import llvmc "llvm/win64/LLVM-C.lib"
} else when ODIN_OS == .Linux || ODIN_OS == .Darwin {
foreign import llvmc "system:LLVM-C"
}
LLVMBool :: distinct int
LLVMMemoryBufferRef :: distinct rawptr
@ -75,10 +79,10 @@ LLVMRealPredicate :: enum {
LLVMRealULT,
LLVMRealULE,
LLVMRealUNE,
LLVMRealPredicateTrue
};
LLVMRealPredicateTrue,
}
@(default_calling_convention = "std")
@(default_calling_convention = "c")
foreign llvmc {
LLVMContextCreate :: proc() -> LLVMContextRef ---
LLVMModuleCreateWithName :: proc(name: cstring) -> LLVMModuleRef ---

View File

@ -2,7 +2,7 @@
- [x] Create a TODO list
- [ ] Custom Functions (not external)
- [ ] Add support for function definitions in the type checker
- [x] Add support for function definitions in the type checker
- [ ] Add support for function definitions in the LLVM emitter
- [ ] Structures
- [ ] Add support for packed in the parser