diff options
| author | Baitinq <[email protected]> | 2025-01-23 23:51:17 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-01-24 00:00:40 +0100 |
| commit | d1a79f22cdf84cb14b4be5776b268a2ced86f506 (patch) | |
| tree | 90a48e08859cd2274247fe677e9ebfad3a58ab5c | |
| parent | Misc: Add example llvm code (diff) | |
| download | interpreter-d1a79f22cdf84cb14b4be5776b268a2ced86f506.tar.gz interpreter-d1a79f22cdf84cb14b4be5776b268a2ced86f506.tar.bz2 interpreter-d1a79f22cdf84cb14b4be5776b268a2ced86f506.zip | |
Misc: get llvm compilation kind of working
| -rw-r--r-- | src/codegen.zig | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index 5951141..989dd99 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -1,6 +1,7 @@ const std = @import("std"); const parser = @import("parser.zig"); const llvm = @import("llvm"); +const target_m = llvm.target_machine; const target = llvm.target; const types = llvm.types; const core = llvm.core; @@ -19,19 +20,50 @@ pub const CodeGen = struct { core.LLVMInt32Type(), }; + var exit_params: [1]types.LLVMTypeRef = [_]types.LLVMTypeRef{ + core.LLVMInt32Type(), + }; + const exit_func_type: types.LLVMTypeRef = core.LLVMFunctionType(core.LLVMInt32Type(), &exit_params, 1, 0); + const exit_func: types.LLVMValueRef = core.LLVMAddFunction(module, "exit", exit_func_type); + // Create a function that computes the sum of two integers const func_type: types.LLVMTypeRef = core.LLVMFunctionType(core.LLVMInt32Type(), ¶ms, 2, 0); - const sum_func: types.LLVMValueRef = core.LLVMAddFunction(module, "sum", func_type); + const sum_func: types.LLVMValueRef = core.LLVMAddFunction(module, "main", func_type); const entry: types.LLVMBasicBlockRef = core.LLVMAppendBasicBlock(sum_func, "entry"); const builder: types.LLVMBuilderRef = core.LLVMCreateBuilder(); core.LLVMPositionBuilderAtEnd(builder, entry); const arg1: types.LLVMValueRef = core.LLVMGetParam(sum_func, 0); const arg2: types.LLVMValueRef = core.LLVMGetParam(sum_func, 1); const sum: types.LLVMValueRef = core.LLVMBuildAdd(builder, arg1, arg2, "sum"); + const exit_status: types.LLVMValueRef = core.LLVMConstInt(core.LLVMInt32Type(), 7, 0); + var exit_args: [1]types.LLVMValueRef = undefined; + exit_args[0] = exit_status; + _ = core.LLVMBuildCall2(builder, exit_func_type, exit_func, &exit_args, 1, "exit_call"); _ = core.LLVMBuildRet(builder, sum); - // Dump the LLVM module to stdout - core.LLVMDumpModule(module); + const triple = target_m.LLVMGetDefaultTargetTriple(); + var target_ref: types.LLVMTargetRef = undefined; + _ = target_m.LLVMGetTargetFromTriple(triple, &target_ref, null); + const target_machine = target_m.LLVMCreateTargetMachine( + target_ref, + triple, + "generic", + "", + types.LLVMCodeGenOptLevel.LLVMCodeGenLevelDefault, + types.LLVMRelocMode.LLVMRelocDefault, + types.LLVMCodeModel.LLVMCodeModelDefault, + ); + + // Generate the object file + const filename = "output.o"; + _ = target_m.LLVMTargetMachineEmitToFile( + target_machine, + module, + filename, + types.LLVMCodeGenFileType.LLVMObjectFile, + null, + ); + std.debug.print("Object file generated: {s}\n", .{filename}); // Clean up LLVM resources core.LLVMDisposeBuilder(builder); |