Implement global struct

This commit is contained in:
David Gonzalez Martin 2025-03-24 21:10:17 +01:00
parent 27e8d13748
commit aba6b2d22b
4 changed files with 1132 additions and 1070 deletions

View File

@ -151,7 +151,31 @@ arena_initialize = fn (initialization: ArenaInitialization) &Arena
return arena;
}
arena_initialize_default = fn (initial_size: u64) &Arena
{
return arena_initialize({
.reserved_size = 4 * 1024 * 1024 * 1024,
.granularity = 4 * 1024,
.initial_size = initial_size,
});
}
GlobalState = struct
{
arena: &Arena,
}
global_state: GlobalState = undefined;
global_state_initialize = fn () void
{
global_state = {
.arena = arena_initialize_default(2 * 1024 * 1024),
};
}
[export] main = fn [cc(c)] () s32
{
global_state_initialize();
return 0;
}

View File

@ -3596,7 +3596,6 @@ const Converter = struct {
const value_start_ch = converter.content[value_offset];
var value = switch (value_start_ch) {
'a'...'z', 'A'...'Z', '_' => b: {
if (module.current_function) |current_function| {
const identifier = converter.parse_identifier();
if (string_to_enum(ValueKeyword, identifier)) |value_keyword| switch (value_keyword) {
@ -3624,7 +3623,9 @@ const Converter = struct {
module.llvm.builder.clear_insertion_position();
return module.unreachable_value;
},
} else {
};
if (module.current_function) |current_function| {
const variable = if (current_function.value.bb.function.locals.find(identifier)) |local| local else if (current_function.value.bb.function.arguments.find(identifier)) |argument| argument else if (module.globals.find(identifier)) |global| global else converter.report_error();
converter.skip_space();
@ -3843,7 +3844,6 @@ const Converter = struct {
},
}
}
}
} else {
converter.report_error();
}
@ -4909,11 +4909,14 @@ pub noinline fn convert(arena: *Arena, options: ConvertOptions) void {
converter.skip_space();
var global_keyword = false;
if (is_identifier_start_ch(converter.content[converter.offset])) {
const global_string = converter.parse_identifier();
converter.skip_space();
if (string_to_enum(GlobalKind, global_string)) |global_kind| switch (global_kind) {
if (string_to_enum(GlobalKind, global_string)) |global_kind| {
global_keyword = true;
switch (global_kind) {
.@"fn" => {
var calling_convention = CallingConvention.c;
const function_attributes = Function.Attributes{};
@ -5797,10 +5800,13 @@ pub noinline fn convert(arena: *Arena, options: ConvertOptions) void {
.name = global_name,
});
},
} else {
converter.report_error();
}
} else {
converter.offset -= global_string.len;
}
}
if (!global_keyword) {
if (global_type) |expected_type| {
const value = converter.parse_value(module, expected_type, .value);

View File

@ -416,3 +416,7 @@ test "pointer_cast" {
test "struct_assignment" {
try invsrc(@src());
}
test "global_struct" {
try invsrc(@src());
}

28
tests/global_struct.bbb Normal file
View File

@ -0,0 +1,28 @@
S = struct
{
a: u32,
b: u32,
c: u32,
}
s: S = {
.a = 1,
.b = 2,
.c = 3,
};
require = fn (ok: u1) void
{
if (!ok)
{
#trap();
}
}
[export] main = fn () s32
{
require(s.a == 1);
require(s.b == 2);
require(s.c == 3);
return 0;
}