about summary refs log tree commit diff
path: root/src/bootstrap/codegen.pry
diff options
context:
space:
mode:
authorBaitinq <[email protected]>2025-07-13 11:26:30 +0200
committerBaitinq <[email protected]>2025-07-13 11:26:30 +0200
commit792f56587f1594c96c12c7a3446c9c245295a0a7 (patch)
treead527e244b5658f32449b51f5940f00cb239f8ad /src/bootstrap/codegen.pry
parentBoostrap: Support assigning to ptrs (diff)
downloadpry-lang-792f56587f1594c96c12c7a3446c9c245295a0a7.tar.gz
pry-lang-792f56587f1594c96c12c7a3446c9c245295a0a7.tar.bz2
pry-lang-792f56587f1594c96c12c7a3446c9c245295a0a7.zip
Boostrap: Support sizeof builtin
Diffstat (limited to 'src/bootstrap/codegen.pry')
-rw-r--r--src/bootstrap/codegen.pry27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/bootstrap/codegen.pry b/src/bootstrap/codegen.pry
index 57f5472..be2ee5c 100644
--- a/src/bootstrap/codegen.pry
+++ b/src/bootstrap/codegen.pry
@@ -95,6 +95,7 @@ let codegen = struct {
 	whil_block: LLVMBasicBlockRef,
 	current_function: LLVMValueRef,
 	current_function_retur_type: *Node,
+	llvm_target_data: LLVMTargetDataRef,
 };
 
 let codegen_init = (alloc: *arena) => *codegen {
@@ -111,6 +112,7 @@ let codegen_init = (alloc: *arena) => *codegen {
 	let c = cast(*codegen, arena_alloc(alloc, sizeof(codegen)));
 	
 	(*c).llvm_module = module;
+	(*c).llvm_target_data = LLVMGetModuleDataLayout(module);
 	(*c).llvm_context = context;
 	(*c).builder = builder;
 	(*c).arena = alloc;
@@ -693,7 +695,6 @@ let codegen_generate_expression_value = (c: *codegen, expression: *Node, name: *
 		v.stack_level = cast(*i64, null);
 		v.node = expression;
 		v.node_type = expression;
-		printf("RET\n");
 		return codegen_create_variable(c, v);
 	};
 	
@@ -713,6 +714,30 @@ let codegen_generate_expression_value = (c: *codegen, expression: *Node, name: *
 		v.node_type = exp.typ;
 		return codegen_create_variable(c, v);
 	};
+	
+	if ((*expression).type == NODE_SIZEOF_STATEMENT) {
+		let exp = *cast(*NODE_SIZEOF_STATEMENT_DATA, (*expression).data);
+		let typ = codegen_get_llvm_type(c, exp.typ);
+		assert(typ != cast(*LLVMTypeRef, null));
+		let size_in_bytes = LLVMStoreSizeOfType((*c).llvm_target_data, *typ);
+		let size_val = LLVMConstInt(LLVMInt64Type(), size_in_bytes, 0);
+
+		let node_type = Node{};
+		node_type.type = NODE_TYPE_SIMPLE_TYPE;
+
+		let d = cast(*NODE_TYPE_SIMPLE_TYPE_DATA, arena_alloc((*c).arena, sizeof(NODE_TYPE_SIMPLE_TYPE_DATA)));
+		(*d).name = "i64";
+		(*d).underlying_type = cast(*Node, null);
+		node_type.data = cast(*void, d);
+
+		let v = Variable{};
+		v.value = size_val;
+		v.type = cast(LLVMTypeRef, null);
+		v.stack_level = cast(*i64, null);
+		v.node = expression;
+		v.node_type = create_node(c, node_type);
+		return codegen_create_variable(c, v);
+	};
 
 	printf("ASSERT 1: %d\n", (*expression).type);
 	assert(false);