This commit is contained in:
David Gonzalez Martin 2025-04-26 19:32:35 -06:00
parent 71707473bd
commit d26cc54714
2 changed files with 96 additions and 32 deletions

View File

@ -241,7 +241,8 @@ pub const Macro = struct {
return_alloca: *llvm.Value,
return_block: *llvm.BasicBlock,
function_scope: Scope,
block_scope: Scope,
instantiation_line: u32,
instantiation_column: u32,
};
pub const Buffer = struct {
@ -3697,23 +3698,17 @@ pub const Module = struct {
.block = undefined,
.return_alloca = undefined,
.return_block = undefined,
.block_scope = .{
.line = instantiation_line,
.column = instantiation_column,
.kind = .macro_instantiation_block,
.parent = scope,
},
.function_scope = .{
.line = macro.scope.line,
.column = macro.scope.column,
.kind = .macro_instantiation_function,
.parent = undefined,
.parent = scope,
},
.instantiation_line = instantiation_line,
.instantiation_column = instantiation_column,
},
},
};
value.bb.macro_instantiation.function_scope.parent = &value.bb.macro_instantiation.block_scope;
},
else => {
left.kind = .left;
@ -3819,22 +3814,17 @@ pub const Module = struct {
.return_type = macro.return_type,
.constant_argument_values = &.{},
.declaration_arguments = &.{},
.block_scope = .{
.line = instantiation_line,
.column = instantiation_column,
.kind = .macro_instantiation_block,
.parent = scope,
},
.function_scope = .{
.line = macro.scope.line,
.column = macro.scope.column,
.kind = .macro_instantiation_function,
.parent = undefined,
.parent = scope,
},
.instantiation_line = instantiation_line,
.instantiation_column = instantiation_column,
},
},
};
value.bb.macro_instantiation.function_scope.parent = &value.bb.macro_instantiation.block_scope;
return value;
},
@ -5998,6 +5988,31 @@ pub const Module = struct {
},
},
.variable_reference => unreachable,
.call => |call| b: {
const callable = module.clone_value(scope, call.callable);
const arguments = module.arena.allocate(*Value, call.arguments.len);
for (arguments, call.arguments) |*new_argument, old_argument| {
new_argument.* = module.clone_value(scope, old_argument);
}
break :b .{
.call = .{
.callable = callable,
.arguments = arguments,
.function_type = call.function_type,
},
};
},
.intrinsic => |intrinsic| .{
.intrinsic = switch (intrinsic) {
.alignof => |ty| .{
.alignof = module.resolve_type(ty),
},
.byte_size => |ty| .{
.byte_size = module.resolve_type(ty),
},
else => @trap(),
},
},
else => @trap(),
},
.kind = source.kind,
@ -6979,9 +6994,6 @@ pub const Module = struct {
const argument_count = macro_instantiation.declaration_arguments.len;
if (module.has_debug_info) {
const lexical_block = module.llvm.di_builder.create_lexical_block(macro_instantiation.block_scope.parent.?.llvm.?, module.llvm.file, macro_instantiation.block_scope.line, macro_instantiation.block_scope.column);
macro_instantiation.block_scope.llvm = lexical_block.to_scope();
for (macro_instantiation.instantiation_arguments, macro_instantiation.declaration_arguments) |instantiation_argument, declaration_argument| {
module.analyze_value_type(instantiation_argument, .{ .type = declaration_argument.variable.type.? });
}
@ -7198,10 +7210,8 @@ pub const Module = struct {
const slice_length = if (has_start) {
@trap();
} else if (slice_expression.end) |end| {
_ = end;
@trap();
} else llvm_integer_index_type.get_constant(array.element_count, 0).to_value();
} else if (slice_expression.end) |end| end.llvm.?
else llvm_integer_index_type.get_constant(array.element_count, 0).to_value();
return .{ slice_pointer, slice_length };
},
@ -7897,7 +7907,7 @@ pub const Module = struct {
module.emit_value(call_argument, .abi);
}
const caller_debug_location = if (module.has_debug_info) llvm.DI.create_debug_location(module.llvm.context, macro_instantiation.block_scope.line, macro_instantiation.block_scope.column, macro_instantiation.block_scope.llvm.?, null) else undefined;
const caller_debug_location = if (module.has_debug_info) llvm.DI.create_debug_location(module.llvm.context, macro_instantiation.instantiation_line, macro_instantiation.instantiation_column, macro_instantiation.function_scope.parent.?.llvm.?, null) else undefined;
defer if (module.has_debug_info) {
module.llvm.builder.set_current_debug_location(caller_debug_location);
};
@ -8466,10 +8476,8 @@ pub const Module = struct {
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();
},
.constant_integer => |start_constant_integer| switch (end.bb) {
.constant_integer => |end_constant_integer| module.integer_type(64, !(!start_constant_integer.signed and !end_constant_integer.signed)),
else => blk: {
module.analyze_value_type(end, .{});
start.type = end.type;
@ -8579,7 +8587,21 @@ pub const Module = struct {
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());
},
false => @trap(),
false => {
assert(value_type.bb == .array);
const uint64 = module.integer_type(64, false);
uint64.resolve(module);
const u64_zero = uint64.llvm.abi.?.to_integer().get_constant(0, 0).to_value();
const pointer_to_element_type = module.get_pointer_type(.{ .type = value_type.bb.array.element_type });
for (array_initialization.values, 0..) |v, i| {
const alloca_gep = module.llvm.builder.create_gep(.{
.type = value_type.llvm.memory.?,
.aggregate = left_llvm,
.indices = &.{ u64_zero, uint64.llvm.abi.?.to_integer().get_constant(i, 0).to_value() },
});
module.emit_assignment(alloca_gep, pointer_to_element_type, v);
}
},
},
.aggregate_initialization => |aggregate_initialization| switch (aggregate_initialization.is_constant) {
true => {
@ -9121,8 +9143,24 @@ pub const Module = struct {
break :blk result_type;
},
else => ty,
.pointer => |*pointer| blk: {
pointer.type = module.resolve_type(pointer.type);
break :blk ty;
},
.structure => |structure| blk: {
for (structure.fields) |*field| {
field.type = module.resolve_type(field.type);
}
break :blk ty;
},
.integer => ty,
.array => |*array| blk: {
array.element_type = module.resolve_type(array.element_type);
break :blk ty;
},
else => @trap(),
};
assert(result_type.bb != .unresolved);
return result_type;
}

View File

@ -586,9 +586,35 @@ CompileOptions = struct
silent: u1,
}
Type = struct
{
foo: u32,
}
compile = fn (arena: &Arena, options: CompileOptions) void
{
>signs: [2]u1 = [0, 1];
>type_allocation = arena_allocate[Type](arena,
64 * 2 // Basic integer types
+ 2 // u128, s128
+ 2 // void, noreturn
);
for (sign: signs)
{
for (b: 0..64)
{
>bit_count = b + 1;
>first_digit: u8 = #truncate(#select(bit_count < 10, bit_count % 10 + '0', bit_count / 10 + '0'));
>second_digit: u8 = #truncate(#select(bit_count > 9, bit_count % 10 + '0', 0));
>name_buffer: [3]u8 = [ #select(sign, 's', 'u'), first_digit, second_digit ];
>name_length: u64 = 2 + #extend(bit_count > 9);
>name = arena_duplicate_string(arena, name_buffer[..name_length]);
}
}
}
compile_file = fn (arena: &Arena, compile_options: CompileFile) void