For each integer
All checks were successful
CI / ci (ReleaseFast, ubuntu-latest) (pull_request) Successful in 2m35s
CI / ci (ReleaseSmall, ubuntu-latest) (pull_request) Successful in 2m34s
CI / ci (ReleaseSafe, ubuntu-latest) (pull_request) Successful in 2m41s
CI / ci (Debug, ubuntu-latest) (pull_request) Successful in 3m59s
CI / ci (ReleaseSmall, ubuntu-latest) (push) Successful in 2m28s
CI / ci (ReleaseFast, ubuntu-latest) (push) Successful in 2m32s
CI / ci (ReleaseSafe, ubuntu-latest) (push) Successful in 2m37s
CI / ci (Debug, ubuntu-latest) (push) Successful in 3m56s

This commit is contained in:
David Gonzalez Martin 2025-04-25 13:26:38 -06:00
parent 1983d50280
commit a8ec7caab7
5 changed files with 317 additions and 159 deletions

View File

@ -511,7 +511,7 @@ pub const Type = struct {
pub const Pointer = struct { pub const Pointer = struct {
type: *Type, type: *Type,
alignment: u32, alignment: ?u32,
}; };
pub const Function = struct { pub const Function = struct {
@ -630,6 +630,7 @@ pub const Type = struct {
.structure => |structure| structure.byte_alignment, .structure => |structure| structure.byte_alignment,
.bits => |bits| bits.backing_type.get_byte_alignment(), .bits => |bits| bits.backing_type.get_byte_alignment(),
.alias => |alias| alias.type.get_byte_alignment(), .alias => |alias| alias.type.get_byte_alignment(),
.unresolved => unreachable,
else => @trap(), else => @trap(),
}; };
return result; return result;
@ -734,7 +735,13 @@ pub const Statement = struct {
left_values: []const Value.Kind, left_values: []const Value.Kind,
right_values: []const *Value, right_values: []const *Value,
predicate: *Statement, predicate: *Statement,
kind: Kind,
scope: Scope, scope: Scope,
const Kind = enum {
slice,
range,
};
}; };
const Assignment = struct { const Assignment = struct {
@ -913,6 +920,7 @@ pub const Value = struct {
}; };
const Intrinsic = union(Id) { const Intrinsic = union(Id) {
alignof: *Type,
byte_size: *Type, byte_size: *Type,
enum_name: *Value, enum_name: *Value,
extend: *Value, extend: *Value,
@ -930,6 +938,7 @@ pub const Value = struct {
va_arg: VaArg, va_arg: VaArg,
const Id = enum { const Id = enum {
alignof,
byte_size, byte_size,
enum_name, enum_name,
extend, extend,
@ -1491,7 +1500,7 @@ pub const Module = struct {
pub fn get_pointer_type(module: *Module, pointer: Pointer) *Type { pub fn get_pointer_type(module: *Module, pointer: Pointer) *Type {
const p = Type.Pointer{ const p = Type.Pointer{
.type = pointer.type, .type = pointer.type,
.alignment = if (pointer.alignment) |a| a else pointer.type.get_byte_alignment(), .alignment = if (pointer.alignment) |a| a else if (pointer.type.bb != .unresolved) pointer.type.get_byte_alignment() else null,
}; };
const all_types = module.types.get_slice(); const all_types = module.types.get_slice();
const pointer_type = for (module.pointer_types.get_slice()) |pointer_type_index| { const pointer_type = for (module.pointer_types.get_slice()) |pointer_type_index| {
@ -2615,6 +2624,7 @@ pub const Module = struct {
.kind = .for_each, .kind = .for_each,
.parent = scope, .parent = scope,
}, },
.kind = undefined,
}, },
}, },
}; };
@ -2671,26 +2681,61 @@ pub const Module = struct {
var right_value_buffer: [64]*Value = undefined; var right_value_buffer: [64]*Value = undefined;
var right_value_count: u64 = 0; var right_value_count: u64 = 0;
while (true) {
module.skip_space();
const identifier = module.parse_value(scope, .{ right_value_buffer[right_value_count] = module.parse_value(scope, .{
.kind = .left, .kind = .left,
}); });
right_value_buffer[right_value_count] = identifier;
right_value_count += 1; right_value_count += 1;
module.skip_space(); module.skip_space();
if (!module.consume_character_if_match(',')) { const token = module.tokenize();
const kind: Statement.ForEach.Kind = switch (token) {
.@")" => b: {
module.offset += 1;
break :b .slice;
},
.@".." => b: {
if (left_value_count != 1) {
module.report_error();
}
right_value_buffer[0].kind = .right;
right_value_buffer[right_value_count] = module.parse_value(scope, .{
.kind = .right,
});
right_value_count += 1;
module.expect_character(right_parenthesis); module.expect_character(right_parenthesis);
break; break :b .range;
} },
} else => @trap(),
};
statement.bb.for_each.kind = kind;
module.skip_space(); module.skip_space();
// if (!module.consume_character_if_match(',')) {
// module.expect_character(right_parenthesis);
// @trap();
// }
//
// while (true) {
// module.skip_space();
//
// right_value_buffer[right_value_count] = module.parse_value(scope, .{
// .kind = .left,
// });
// right_value_count += 1;
//
// module.skip_space();
//
// if (!module.consume_character_if_match(',')) {
// module.expect_character(right_parenthesis);
// break;
// }
// }
if (left_value_count != right_value_count) { if (kind == .slice and left_value_count != right_value_count) {
module.report_error(); module.report_error();
} }
@ -3122,6 +3167,20 @@ pub const Module = struct {
const value = module.values.add(); const value = module.values.add();
value.* = switch (intrinsic) { value.* = switch (intrinsic) {
.alignof => blk: {
module.skip_space();
module.expect_character(left_parenthesis);
module.skip_space();
const ty = module.parse_type();
module.expect_character(right_parenthesis);
break :blk .{
.bb = .{
.intrinsic = .{
.alignof = ty,
},
},
};
},
.byte_size => blk: { .byte_size => blk: {
module.skip_space(); module.skip_space();
module.expect_character(left_parenthesis); module.expect_character(left_parenthesis);
@ -4715,7 +4774,7 @@ pub const Module = struct {
.scalar => module.create_load(.{ .type = va_arg.type, .value = llvm_address }), .scalar => module.create_load(.{ .type = va_arg.type, .value = llvm_address }),
.aggregate => if (left_llvm) |l| b: { .aggregate => if (left_llvm) |l| b: {
uint64.resolve(module); uint64.resolve(module);
_ = module.llvm.builder.create_memcpy(l, left_type.?.bb.pointer.alignment, llvm_address, va_arg.type.get_byte_alignment(), uint64.llvm.abi.?.to_integer().get_constant(va_arg.type.get_byte_size(), 0).to_value()); _ = module.llvm.builder.create_memcpy(l, left_type.?.bb.pointer.alignment.?, llvm_address, va_arg.type.get_byte_alignment(), uint64.llvm.abi.?.to_integer().get_constant(va_arg.type.get_byte_size(), 0).to_value());
break :b l; break :b l;
} else llvm_address, } else llvm_address,
.complex => @trap(), .complex => @trap(),
@ -7446,7 +7505,7 @@ pub const Module = struct {
value_type.bb.array.element_type.resolve(module); value_type.bb.array.element_type.resolve(module);
const array_value = value_type.bb.array.element_type.llvm.abi.?.get_constant_array(llvm_values); const array_value = value_type.bb.array.element_type.llvm.memory.?.get_constant_array(llvm_values);
break :blk array_value.to_value(); break :blk array_value.to_value();
}, },
false => switch (value.kind) { false => switch (value.kind) {
@ -8221,6 +8280,14 @@ pub const Module = struct {
const index_type = module.integer_type(64, false); const index_type = module.integer_type(64, false);
index_type.resolve(module); index_type.resolve(module);
const loop_entry_block = module.llvm.context.create_basic_block("foreach.entry", llvm_function);
const loop_body_block = module.llvm.context.create_basic_block("foreach.body", llvm_function);
const loop_continue_block = module.llvm.context.create_basic_block("foreach.continue", llvm_function);
const loop_exit_block = module.llvm.context.create_basic_block("foreach.exit", llvm_function);
switch (for_loop.kind) {
.slice => {
const index_zero = index_type.llvm.abi.?.get_zero().to_value(); const index_zero = index_type.llvm.abi.?.get_zero().to_value();
for (for_loop.locals, for_loop.left_values, for_loop.right_values) |local, kind, right| { for (for_loop.locals, for_loop.left_values, for_loop.right_values) |local, kind, right| {
@ -8274,10 +8341,6 @@ pub const Module = struct {
const index_alloca = module.create_alloca(.{ .type = index_type, .name = "foreach.index" }); const index_alloca = module.create_alloca(.{ .type = index_type, .name = "foreach.index" });
_ = module.create_store(.{ .type = index_type, .source_value = index_zero, .destination_value = index_alloca }); _ = module.create_store(.{ .type = index_type, .source_value = index_zero, .destination_value = index_alloca });
const loop_entry_block = module.llvm.context.create_basic_block("foreach.entry", llvm_function);
const loop_body_block = module.llvm.context.create_basic_block("foreach.body", llvm_function);
const loop_continue_block = module.llvm.context.create_basic_block("foreach.continue", llvm_function);
const loop_exit_block = module.llvm.context.create_basic_block("foreach.exit", llvm_function);
_ = module.llvm.builder.create_branch(loop_entry_block); _ = module.llvm.builder.create_branch(loop_entry_block);
module.llvm.builder.position_at_end(loop_entry_block); module.llvm.builder.position_at_end(loop_entry_block);
@ -8355,6 +8418,71 @@ pub const Module = struct {
module.llvm.builder.position_at_end(loop_exit_block); module.llvm.builder.position_at_end(loop_exit_block);
}, },
.range => {
assert(for_loop.locals.len == 1);
assert(for_loop.left_values.len == 1);
if (for_loop.right_values.len == 2) {
const start = for_loop.right_values[0];
const end = for_loop.right_values[1];
const local_type = switch (start.bb) {
.constant_integer => |_| switch (end.bb) {
.constant_integer => |_| {
@trap();
},
else => blk: {
module.analyze_value_type(end, .{});
start.type = end.type;
break :blk end.type.?;
},
},
else => @trap(),
};
for (for_loop.right_values) |right_value| {
if (right_value.type == null) {
module.analyze_value_type(right_value, .{ .type = local_type });
}
}
const local = for_loop.locals[0];
local.variable.type = local_type;
module.emit_local_storage(local);
module.emit_value(start, .memory);
const index_alloca = local.variable.storage.?.llvm.?;
_ = module.create_store(.{ .type = local_type, .source_value = start.llvm.?, .destination_value = index_alloca });
_ = module.llvm.builder.create_branch(loop_entry_block);
module.llvm.builder.position_at_end(loop_entry_block);
const header_index_load = module.create_load(.{ .type = local_type, .value = index_alloca });
module.emit_value(end, .abi);
const length_value = end.llvm.?;
const index_compare = module.llvm.builder.create_integer_compare(.ult, header_index_load, length_value);
_ = module.llvm.builder.create_conditional_branch(index_compare, loop_body_block, loop_exit_block);
module.llvm.builder.position_at_end(loop_body_block);
module.analyze_statement(&for_loop.scope, for_loop.predicate, last_line, last_column, last_statement_debug_location);
if (module.llvm.builder.get_insert_block() != null) {
_ = module.llvm.builder.create_branch(loop_continue_block);
}
module.llvm.builder.position_at_end(loop_continue_block);
const continue_index_load = module.create_load(.{ .type = local_type, .value = index_alloca });
const add = module.llvm.builder.create_add(continue_index_load, local_type.llvm.abi.?.to_integer().get_constant(1, 0).to_value());
_ = module.create_store(.{ .type = local_type, .source_value = add, .destination_value = index_alloca });
_ = module.llvm.builder.create_branch(loop_entry_block);
module.llvm.builder.position_at_end(loop_exit_block);
} else {
// TODO: case for reverse range
@trap();
}
},
}
},
} }
} }
@ -8408,7 +8536,7 @@ pub const Module = struct {
global_variable.to_value().set_alignment(alignment); global_variable.to_value().set_alignment(alignment);
const uint64 = module.integer_type(64, false); const uint64 = module.integer_type(64, false);
uint64.resolve(module); uint64.resolve(module);
_ = module.llvm.builder.create_memcpy(left_llvm, pointer_type.bb.pointer.alignment, global_variable.to_value(), alignment, uint64.llvm.abi.?.to_integer().get_constant(array_initialization.values.len * pointer_type.bb.pointer.type.bb.array.element_type.get_byte_size(), @intFromBool(false)).to_value()); _ = module.llvm.builder.create_memcpy(left_llvm, pointer_type.bb.pointer.alignment.?, global_variable.to_value(), alignment, uint64.llvm.abi.?.to_integer().get_constant(array_initialization.values.len * pointer_type.bb.pointer.type.bb.array.element_type.get_byte_size(), @intFromBool(false)).to_value());
}, },
false => @trap(), false => @trap(),
}, },
@ -8426,7 +8554,7 @@ pub const Module = struct {
global_variable.to_value().set_alignment(alignment); global_variable.to_value().set_alignment(alignment);
const uint64 = module.integer_type(64, false); const uint64 = module.integer_type(64, false);
uint64.resolve(module); uint64.resolve(module);
_ = module.llvm.builder.create_memcpy(left_llvm, pointer_type.bb.pointer.alignment, global_variable.to_value(), alignment, uint64.llvm.abi.?.to_integer().get_constant(value_type.get_byte_size(), @intFromBool(false)).to_value()); _ = module.llvm.builder.create_memcpy(left_llvm, pointer_type.bb.pointer.alignment.?, global_variable.to_value(), alignment, uint64.llvm.abi.?.to_integer().get_constant(value_type.get_byte_size(), @intFromBool(false)).to_value());
}, },
false => { false => {
var max_field_index: u64 = 0; var max_field_index: u64 = 0;
@ -8469,7 +8597,7 @@ pub const Module = struct {
uint8.resolve(module); uint8.resolve(module);
const uint64 = module.integer_type(64, false); const uint64 = module.integer_type(64, false);
uint64.resolve(module); uint64.resolve(module);
_ = module.llvm.builder.create_memset(destination_pointer, uint8.llvm.abi.?.get_zero().to_value(), uint64.llvm.abi.?.to_integer().get_constant(memset_size, 0).to_value(), pointer_type.bb.pointer.alignment); _ = module.llvm.builder.create_memset(destination_pointer, uint8.llvm.abi.?.get_zero().to_value(), uint64.llvm.abi.?.to_integer().get_constant(memset_size, 0).to_value(), pointer_type.bb.pointer.alignment.?);
} }
}, },
}, },
@ -8614,13 +8742,13 @@ pub const Module = struct {
u8_type.resolve(module); u8_type.resolve(module);
const u64_type = module.integer_type(64, false); const u64_type = module.integer_type(64, false);
u64_type.resolve(module); u64_type.resolve(module);
_ = module.llvm.builder.create_memset(left_llvm, u8_type.llvm.abi.?.get_zero().to_value(), u64_type.llvm.abi.?.to_integer().get_constant(value_type.get_byte_size(), 0).to_value(), pointer_type.bb.pointer.alignment); _ = module.llvm.builder.create_memset(left_llvm, u8_type.llvm.abi.?.get_zero().to_value(), u64_type.llvm.abi.?.to_integer().get_constant(value_type.get_byte_size(), 0).to_value(), pointer_type.bb.pointer.alignment.?);
}, },
.variable_reference => |variable| switch (right.kind) { .variable_reference => |variable| switch (right.kind) {
.left => @trap(), .left => @trap(),
.right => { .right => {
const uint64 = module.integer_type(64, false); const uint64 = module.integer_type(64, false);
_ = module.llvm.builder.create_memcpy(left_llvm, pointer_type.bb.pointer.alignment, variable.storage.?.llvm.?, variable.storage.?.type.?.bb.pointer.alignment, uint64.llvm.abi.?.to_integer().get_constant(value_type.get_byte_size(), @intFromBool(false)).to_value()); _ = module.llvm.builder.create_memcpy(left_llvm, pointer_type.bb.pointer.alignment.?, variable.storage.?.llvm.?, variable.storage.?.type.?.bb.pointer.alignment.?, uint64.llvm.abi.?.to_integer().get_constant(value_type.get_byte_size(), @intFromBool(false)).to_value());
}, },
}, },
.field_access => |field_access| { .field_access => |field_access| {
@ -8634,7 +8762,7 @@ pub const Module = struct {
module.emit_value(field_access.aggregate, .memory); module.emit_value(field_access.aggregate, .memory);
const gep = module.llvm.builder.create_struct_gep(struct_type.llvm.abi.?.to_struct(), field_access.aggregate.llvm.?, field_index); const gep = module.llvm.builder.create_struct_gep(struct_type.llvm.abi.?.to_struct(), field_access.aggregate.llvm.?, field_index);
const uint64 = module.integer_type(64, false); const uint64 = module.integer_type(64, false);
_ = module.llvm.builder.create_memcpy(left_llvm, pointer_type.bb.pointer.alignment, gep, value_type.get_byte_alignment(), uint64.llvm.abi.?.to_integer().get_constant(value_type.get_byte_size(), @intFromBool(false)).to_value()); _ = module.llvm.builder.create_memcpy(left_llvm, pointer_type.bb.pointer.alignment.?, gep, value_type.get_byte_alignment(), uint64.llvm.abi.?.to_integer().get_constant(value_type.get_byte_size(), @intFromBool(false)).to_value());
}, },
.undefined => {}, .undefined => {},
else => @trap(), else => @trap(),

View File

@ -443,6 +443,11 @@ arena_allocate_bytes = fn (arena: &Arena, size: u64, alignment: u64) &u8
return result; return result;
} }
arena_allocate = macro [T] (arena: &Arena, count: u64) &T
{
return arena_allocate_bytes(arena, #byte_size(T) * count, #alignof(T));
}
arena_duplicate_string = fn (arena: &Arena, string: []u8) []u8 arena_duplicate_string = fn (arena: &Arena, string: []u8) []u8
{ {
>result = arena_allocate_bytes(arena, string.length + 1, 1); >result = arena_allocate_bytes(arena, string.length + 1, 1);
@ -583,6 +588,7 @@ CompileOptions = struct
compile = fn (arena: &Arena, options: CompileOptions) void compile = fn (arena: &Arena, options: CompileOptions) void
{ {
>signs: [2]u1 = [0, 1];
} }
compile_file = fn (arena: &Arena, compile_options: CompileFile) void compile_file = fn (arena: &Arena, compile_options: CompileFile) void

View File

@ -325,4 +325,6 @@ const names = &[_][]const u8{
"return_small_struct", "return_small_struct",
"basic_macro", "basic_macro",
"generic_macro", "generic_macro",
"for_each_int",
"bool_array",
}; };

12
tests/bool_array.bbb Normal file
View File

@ -0,0 +1,12 @@
[export] main = fn [cc(c)] () s32
{
>signs: [2]u1 = [0, 0];
>accumulator: s32 = 0;
for (s: signs)
{
accumulator += #extend(s);
}
return accumulator;
}

10
tests/for_each_int.bbb Normal file
View File

@ -0,0 +1,10 @@
[export] main = fn [cc(c)] () s32
{
>top: s32 = 64;
>accumulator: s32 = 0;
for (i: 0..top)
{
accumulator += 1;
}
return accumulator - top;
}