diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-02-01 17:46:13 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-02-01 17:46:13 +0100 |
commit | ed7d4ba9505c45e406da37c419cd00421d55ab3e (patch) | |
tree | cd0e25274501eef7674309a9dc11c7de3f0b0ef4 /src/codegen.zig | |
parent | Codegen: Start handling global variables (diff) | |
download | interpreter-ed7d4ba9505c45e406da37c419cd00421d55ab3e.tar.gz interpreter-ed7d4ba9505c45e406da37c419cd00421d55ab3e.tar.bz2 interpreter-ed7d4ba9505c45e406da37c419cd00421d55ab3e.zip |
Diffstat (limited to '')
-rw-r--r-- | src/codegen.zig | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index dc9c1da..c6b259e 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -11,11 +11,6 @@ pub const CodeGenError = error{ OutOfMemory, }; -const Variable = struct { - type: types.LLVMTypeRef, - value: types.LLVMValueRef, -}; - pub const CodeGen = struct { llvm_module: types.LLVMModuleRef, builder: types.LLVMBuilderRef, @@ -185,11 +180,29 @@ pub const CodeGen = struct { const builder_pos = core.LLVMGetInsertBlock(self.builder); defer core.LLVMPositionBuilderAtEnd(self.builder, builder_pos); - const function_type = core.LLVMFunctionType(core.LLVMInt64Type(), &[_]types.LLVMTypeRef{}, 0, 0) orelse return CodeGenError.CompilationError; + var paramtypes = std.ArrayList(types.LLVMTypeRef).init(self.arena); + for (function_definition.parameters) |param| { + std.debug.assert(param.PRIMARY_EXPRESSION == .IDENTIFIER); + try paramtypes.append(core.LLVMInt64Type()); + } + const function_type = core.LLVMFunctionType(core.LLVMInt64Type(), paramtypes.items.ptr, @intCast(paramtypes.items.len), 0) orelse return CodeGenError.CompilationError; const function = core.LLVMAddFunction(self.llvm_module, "", function_type) orelse return CodeGenError.CompilationError; const function_entry = core.LLVMAppendBasicBlock(function, "entrypoint") orelse return CodeGenError.CompilationError; core.LLVMPositionBuilderAtEnd(self.builder, function_entry); + const params = try self.arena.alloc(types.LLVMValueRef, function_definition.parameters.len); + core.LLVMGetParams(function, params.ptr); + + var parameters_index: usize = 0; + for (params) |p| { + const xdd = function_definition.parameters[parameters_index]; + try self.environment.add_variable(xdd.PRIMARY_EXPRESSION.IDENTIFIER.name, try self.create_variable(.{ + .value = p, + .type = core.LLVMInt64Type(), + })); + parameters_index += 1; + } + for (function_definition.statements) |stmt| { try self.generate_statement(stmt); } @@ -262,6 +275,11 @@ pub const CodeGen = struct { } }; +const Variable = struct { + type: types.LLVMTypeRef, + value: types.LLVMValueRef, +}; + const Scope = struct { variables: std.StringHashMap(*Variable), }; |