Implement byte size intrinsic

This commit is contained in:
David Gonzalez Martin 2025-03-23 12:23:27 +01:00
parent 614c3d7e35
commit 9ad86fe936
4 changed files with 46 additions and 0 deletions

View File

@ -1,3 +1,14 @@
Arena = struct
{
reserved_size: u64,
position: u64,
os_position: u64,
granularity: u64,
reserved: [32]u8,
}
minimum_position: u64 = #byte_size(Arena);
[export] main = fn [cc(c)] () s32
{
return 0;

View File

@ -2561,6 +2561,7 @@ const Converter = struct {
};
const Intrinsic = enum {
byte_size,
cast,
cast_to,
extend,
@ -2584,6 +2585,30 @@ const Converter = struct {
converter.skip_space();
switch (intrinsic_keyword) {
.byte_size => {
const ty = converter.parse_type(module);
converter.skip_space();
converter.expect_character(')');
const byte_size = ty.get_byte_size();
const destination_type = expected_type orelse converter.report_error();
if (destination_type.bb != .integer) {
converter.report_error();
}
const value = module.values.add();
value.* = .{
.llvm = destination_type.llvm.handle.to_integer().get_constant(byte_size, @intFromBool(false)).to_value(),
.bb = .{
.constant_integer = .{
.value = byte_size,
.signed = false,
},
},
.type = destination_type,
.lvalue = false,
.dereference_to_assign = false,
};
return value;
},
.cast => {
@trap();
},

View File

@ -368,3 +368,7 @@ test "indirect_varargs" {
test "varargs" {
try invsrc(@src());
}
test "byte_size" {
try invsrc(@src());
}

6
tests/byte_size.bbb Normal file
View File

@ -0,0 +1,6 @@
[export] main = fn [cc(c)] () s32
{
>a: s32 = #byte_size(s32);
>b: s32 = #byte_size(s32);
return a - b;
}