about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/codegen.zig39
-rw-r--r--src/main.zig3
2 files changed, 41 insertions, 1 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index a3fc681..5951141 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -1,4 +1,41 @@
 const std = @import("std");
 const parser = @import("parser.zig");
+const llvm = @import("llvm");
+const target = llvm.target;
+const types = llvm.types;
+const core = llvm.core;
 
-pub const CodeGen = struct {};
+pub const CodeGen = struct {
+    pub fn generate() void {
+        // Initialize LLVM
+        _ = target.LLVMInitializeNativeTarget();
+        _ = target.LLVMInitializeNativeAsmPrinter();
+        _ = target.LLVMInitializeNativeAsmParser();
+
+        // Create a new LLVM module
+        const module: types.LLVMModuleRef = core.LLVMModuleCreateWithName("sum_module");
+        var params: [2]types.LLVMTypeRef = [_]types.LLVMTypeRef{
+            core.LLVMInt32Type(),
+            core.LLVMInt32Type(),
+        };
+
+        // Create a function that computes the sum of two integers
+        const func_type: types.LLVMTypeRef = core.LLVMFunctionType(core.LLVMInt32Type(), &params, 2, 0);
+        const sum_func: types.LLVMValueRef = core.LLVMAddFunction(module, "sum", 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");
+        _ = core.LLVMBuildRet(builder, sum);
+
+        // Dump the LLVM module to stdout
+        core.LLVMDumpModule(module);
+
+        // Clean up LLVM resources
+        core.LLVMDisposeBuilder(builder);
+        core.LLVMDisposeModule(module);
+        core.LLVMShutdown();
+    }
+};
diff --git a/src/main.zig b/src/main.zig
index 94105b0..713f6b8 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -2,6 +2,7 @@ const std = @import("std");
 const tokenizer = @import("tokenizer.zig");
 const parser = @import("parser.zig");
 const evaluator = @import("evaluator.zig");
+const codegen = @import("codegen.zig");
 
 pub fn main() !void {
     const stdout = std.io.getStdOut().writer();
@@ -22,6 +23,8 @@ pub fn main() !void {
 
     const source_evaluator = try evaluator.Evaluator.init(arena.allocator());
 
+    codegen.CodeGen.generate();
+
     if (std.mem.eql(u8, path, "-i")) {
         while (true) {
             try stdout.print("> ", .{});