Pass struct test

This commit is contained in:
David Gonzalez Martin 2025-06-12 20:47:12 -06:00
parent 8e477e3c8d
commit 640ddf6a75
3 changed files with 213 additions and 4 deletions

View File

@ -6070,7 +6070,7 @@ parse = fn (module: &Module) void
}
>type_it = module.scope.types.first;
>forward_declaration: &Type = zero;
>type_forward_declaration: &Type = zero;
while (type_it)
{
@ -6078,7 +6078,7 @@ parse = fn (module: &Module) void
{
if (type_it.id == .forward_declaration)
{
forward_declaration = type_it;
type_forward_declaration = type_it;
break;
}
else
@ -6461,7 +6461,102 @@ parse = fn (module: &Module) void
},
.struct =>
{
#trap();
skip_space(module);
>struct_type: &Type = undefined;
if (type_forward_declaration)
{
struct_type = type_forward_declaration;
}
else
{
struct_type = new_type(module, {
.id = .forward_declaration,
.name = global_name,
.scope = &module.scope,
zero,
});
}
if (consume_character_if_match(module, left_brace))
{
>field_buffer: [256]Field = undefined;
>byte_size: u64 = 0;
>byte_alignment: u32 = 1;
>field_count: u64 = 0;
while (1)
{
skip_space(module);
if (consume_character_if_match(module, right_brace))
{
break;
}
>field_index = field_count;
>field_line = get_line(module);
>field_name = parse_identifier(module);
skip_space(module);
expect_character(module, ':');
skip_space(module);
>field_type = parse_type(module, scope);
>field_byte_size = get_byte_size(field_type);
>field_byte_alignment = get_byte_alignment(field_type);
// Align struct size by field alignment
>field_byte_offset = align_forward(byte_size, #extend(field_byte_alignment));
field_buffer[field_index] = {
.name = field_name,
.type = field_type,
.offset = field_byte_offset,
.line = field_line,
};
byte_size = field_byte_offset + field_byte_size;
byte_alignment = #max(byte_alignment, field_byte_alignment);
skip_space(module);
consume_character_if_match(module, ',');
field_count += 1;
}
byte_size = align_forward(byte_size, #extend(byte_alignment));
assert(byte_size % #extend(byte_alignment) == 0);
skip_space(module);
consume_character_if_match(module, ';');
>fields = arena_allocate_slice[Field](module.arena, field_count);
memcpy(#pointer_cast(fields.pointer), #pointer_cast(&field_buffer), field_count * #byte_size(Field));
struct_type.content = {
.struct = {
.fields = fields,
.byte_size = byte_size,
.byte_alignment = byte_alignment,
.line = global_line,
.is_slice = 0,
.next = zero,
},
};
struct_type.id = .struct;
}
else
{
if (type_forward_declaration)
{
report_error();
}
expect_character(module, ';');
}
},
.typealias =>
{
@ -8840,7 +8935,69 @@ analyze_type = fn (module: &Module, value: &Value, expected_type: &Type, analysi
{
.struct =>
{
#trap();
>fields = aggregate_type.content.struct.fields;
assert(fields.length <= 64);
>is_ordered: u1 = 1;
>same_values_as_field = fields.length == elements.length;
>is_properly_initialized = same_values_as_field or zero;
if (zero and same_values_as_field)
{
report_error();
}
if (!is_properly_initialized)
{
report_error();
}
assert(elements.length <= fields.length);
for (initialization_index: 0..elements.length)
{
>element = &elements[initialization_index];
>name = element.name;
>value = element.value;
>declaration_index: u64 = 0;
while (declaration_index < fields.length)
{
>field = &fields[declaration_index];
if (string_equal(name, field.name))
{
break;
}
declaration_index += 1;
}
if (declaration_index == fields.length)
{
report_error();
}
>mask = 1 << declaration_index;
>current_mask = field_mask;
if (current_mask & mask)
{
// Repeated field
report_error();
}
field_mask = current_mask | mask;
is_ordered = is_ordered and declaration_index == initialization_index;
>field = &fields[declaration_index];
>declaration_type = field.type;
analyze_type(module, value, declaration_type, { .must_be_constant = analysis.must_be_constant, zero });
is_constant = is_constant and value_is_constant(value);
}
value.content.aggregate_initialization.is_constant = is_constant and is_ordered;
},
.bits =>
{
@ -12469,6 +12626,8 @@ names: [_][]u8 = [
"bits_no_backing_type",
"bits_return_u1",
"bits_zero",
"comparison",
"global_struct",
];
[export] main = fn [cc(c)] (argument_count: u32, argv: &&u8, envp: &&u8) s32

View File

@ -853,6 +853,7 @@ struct ValueConstantInteger
struct FunctionLLVM
{
LLVMValueRef alloca_insertion_point;
LLVMBasicBlockRef return_block;
LLVMValueRef return_alloca;
};

View File

@ -1949,6 +1949,25 @@ struct AllocaOptions
u32 alignment;
};
fn Global* get_current_function(Module* module)
{
Global* parent_function_global;
if (module->current_function)
{
parent_function_global = module->current_function;
}
else if (module->current_macro_instantiation)
{
parent_function_global = module->current_macro_instantiation->instantiation_function;
}
else
{
report_error();
}
return parent_function_global;
}
fn LLVMValueRef create_alloca(Module* module, AllocaOptions options)
{
auto abi_type = options.type;
@ -1964,7 +1983,15 @@ fn LLVMValueRef create_alloca(Module* module, AllocaOptions options)
alignment = get_byte_alignment(abi_type);
}
auto original_block = LLVMGetInsertBlock(module->llvm.builder);
auto function = get_current_function(module);
auto debug_location = LLVMGetCurrentDebugLocation2(module->llvm.builder);
LLVMPositionBuilderBefore(module->llvm.builder, function->variable.storage->function.llvm.alloca_insertion_point);
LLVMSetCurrentDebugLocation2(module->llvm.builder, 0);
auto alloca = llvm_builder_create_alloca(module->llvm.builder, abi_type->llvm.memory, alignment, options.name);
LLVMPositionBuilderAtEnd(module->llvm.builder, original_block);
LLVMSetCurrentDebugLocation2(module->llvm.builder, debug_location);
return alloca;
}
@ -3070,6 +3097,12 @@ fn void analyze_type(Module* module, Value* value, Type* expected_type, TypeAnal
auto* entry_block = LLVMAppendBasicBlockInContext(module->llvm.context, llvm_function, "entry");
LLVMPositionBuilderAtEnd(module->llvm.builder, entry_block);
auto u32_type = uint32(module);
resolve_type_in_place(module, u32_type);
auto current_function = get_current_function(module);
auto old_alloca_insertion_point = current_function->variable.storage->function.llvm.alloca_insertion_point;
current_function->variable.storage->function.llvm.alloca_insertion_point = LLVMBuildAlloca(module->llvm.builder, u32_type->llvm.abi, "alloca_insert_point");
auto alloca = create_alloca(module, {
.type = string_type,
.name = string_literal("retval"),
@ -3137,6 +3170,8 @@ fn void analyze_type(Module* module, Value* value, Type* expected_type, TypeAnal
enum_to_string = llvm_function;
enum_type->enumerator.enum_to_string_function = enum_to_string;
current_function->variable.storage->function.llvm.alloca_insertion_point = old_alloca_insertion_point;
}
assert(enum_to_string);
@ -4260,6 +4295,12 @@ fn void analyze_type(Module* module, Value* value, Type* expected_type, TypeAnal
LLVMPositionBuilderAtEnd(module->llvm.builder, entry_block);
auto current_function = get_current_function(module);
auto old_alloca_insertion_point = current_function->variable.storage->function.llvm.alloca_insertion_point;
auto u32_type = uint32(module);
resolve_type_in_place(module, u32_type);
current_function->variable.storage->function.llvm.alloca_insertion_point = LLVMBuildAlloca(module->llvm.builder, u32_type->llvm.abi, "alloca_insert_point");
LLVMValueRef arguments[2];
LLVMGetParams(llvm_function, arguments);
@ -4468,6 +4509,8 @@ fn void analyze_type(Module* module, Value* value, Type* expected_type, TypeAnal
enum_type->enumerator.string_to_enum_function = llvm_function;
enum_type->enumerator.string_to_enum_struct_type = struct_type;
current_function->variable.storage->function.llvm.alloca_insertion_point = old_alloca_insertion_point;
}
auto struct_type = enum_type->enumerator.string_to_enum_struct_type;
@ -9507,6 +9550,10 @@ void emit(Module* module)
LLVMPositionBuilderAtEnd(module->llvm.builder, entry_block);
LLVMSetCurrentDebugLocation2(module->llvm.builder, 0);
auto u32_type = uint32(module);
resolve_type_in_place(module, u32_type);
global->variable.storage->function.llvm.alloca_insertion_point = LLVMBuildAlloca(module->llvm.builder, u32_type->llvm.abi, "alloca_insert_point");
auto return_abi_kind = function_type->abi.return_abi.flags.kind;
switch (return_abi_kind)
{
@ -9893,6 +9940,8 @@ void emit(Module* module)
LLVMBuildRet(module->llvm.builder, return_value);
}
LLVMInstructionEraseFromParent(global->variable.storage->function.llvm.alloca_insertion_point);
// END OF SCOPE
module->current_function = 0;
}