about summary refs log tree commit diff
path: root/src/bootstrap
diff options
context:
space:
mode:
authorBaitinq <[email protected]>2025-06-04 00:18:53 +0200
committerBaitinq <[email protected]>2025-06-04 00:36:54 +0200
commit7e39fd651c2dbbdd7271605b20deefb30ad2d203 (patch)
tree25b08dec9e4ccb251b986ce05f8276ab613ecf09 /src/bootstrap
parentBootstrap: Codegen: Start implementation (diff)
downloadpry-lang-7e39fd651c2dbbdd7271605b20deefb30ad2d203.tar.gz
pry-lang-7e39fd651c2dbbdd7271605b20deefb30ad2d203.tar.bz2
pry-lang-7e39fd651c2dbbdd7271605b20deefb30ad2d203.zip
Boostrap: Codegen: Generate object file
Diffstat (limited to 'src/bootstrap')
-rw-r--r--src/bootstrap/codegen.src39
-rw-r--r--src/bootstrap/llvm.src16
2 files changed, 55 insertions, 0 deletions
diff --git a/src/bootstrap/codegen.src b/src/bootstrap/codegen.src
index e4d3102..43b83e1 100644
--- a/src/bootstrap/codegen.src
+++ b/src/bootstrap/codegen.src
@@ -4,6 +4,7 @@ let codegen = struct {
 	llvm_module: *void,
 	llvm_context: *void,
 	builder: *void,
+	arena: *arena,
 };
 
 let codegen_init = (alloc: *arena) => *codegen {
@@ -22,6 +23,7 @@ let codegen_init = (alloc: *arena) => *codegen {
 	(*c).llvm_module = module;
 	(*c).llvm_context = context;
 	(*c).builder = builder;
+	(*c).arena = alloc;
 
 	return c;
 };
@@ -31,7 +33,44 @@ let codegen_generate = (c: *codegen) => i64 {
 };
 
 let codegen_compile = (c: *codegen) => i64 {
+        /* Dump module */
 	LLVMDumpModule((*c).llvm_module);
+
+	/* Generate code */
+        let triple = LLVMGetDefaultTargetTriple();
+        let target_ref = cast(**void, arena_alloc((*c).arena, sizeof(**void)));
+        let message = cast(**i8, null);
+        let result = LLVMGetTargetFromTriple(triple, target_ref, message);
+        if result != 0 {
+            println("Target output: %s", *message);
+            LLVMDisposeMessage(*message);
+        };
+        let target_machine = LLVMCreateTargetMachine(
+            *target_ref,
+            triple,
+            "",
+            "",
+            LLVMCodeGenLevelDefault,
+            LLVMRelocDefault,
+            LLVMCodeModelDefault,
+        );
+        result = LLVMVerifyModule((*c).llvm_module, LLVMAbortProcessAction, message);
+        if result != 0 {
+            println("Verification output: %s", *message);
+            LLVMDisposeMessage(*message);
+        };
+
+        /* Generate the object file */
+        let filename = "bootstrap_output.o";
+        LLVMTargetMachineEmitToFile(
+            target_machine,
+            (*c).llvm_module,
+            filename,
+            LLVMObjectFile,
+            cast(**i8, null),
+        );
+        println("Object file generated: %s", filename);
+
 	return 0;
 };
 
diff --git a/src/bootstrap/llvm.src b/src/bootstrap/llvm.src
index 70a5065..4a9c349 100644
--- a/src/bootstrap/llvm.src
+++ b/src/bootstrap/llvm.src
@@ -12,3 +12,19 @@ extern LLVMShutdown = () => void;
 extern LLVMDisposeBuilder = (*void) => void;
 
 extern LLVMDumpModule = (*void) => void;
+extern LLVMGetDefaultTargetTriple = () => *i8;
+extern LLVMGetTargetFromTriple = (*i8, **void, **i8) => i64;
+extern LLVMDisposeMessage = (*i8) => void;
+extern LLVMCreateTargetMachine = (*void, *i8, *i8, *i8, i64, i64, i64) => *void;
+
+let LLVMCodeGenLevelDefault = 2;
+let LLVMRelocDefault = 0;
+let LLVMCodeModelDefault = 0;
+
+extern LLVMVerifyModule = (*void, i64, **i8) => i64;
+
+let LLVMAbortProcessAction = 0;
+
+extern LLVMTargetMachineEmitToFile = (*void, *void, *i8, i64, **i8) => i64;
+
+let LLVMObjectFile = 1;