Select
Some checks failed
CI / ci (Debug, ubuntu-latest) (push) Has been cancelled
CI / ci (ReleaseSmall, ubuntu-latest) (push) Has been cancelled
CI / ci (ReleaseFast, ubuntu-latest) (push) Has been cancelled
CI / ci (ReleaseSafe, ubuntu-latest) (push) Has been cancelled

This commit is contained in:
David Gonzalez Martin 2025-04-13 11:45:34 -06:00
parent c59a77e7a0
commit d61a71c07d
2 changed files with 53 additions and 1 deletions

View File

@ -721,7 +721,7 @@ pub const Value = struct {
int_from_enum: *Value,
int_from_pointer: *Value,
pointer_cast: *Value,
select,
select: Select,
trap,
truncate: *Value,
va_start,
@ -746,6 +746,13 @@ pub const Value = struct {
va_copy,
va_arg,
};
const Select = struct {
condition: *Value,
true_value: *Value,
false_value: *Value,
};
const VaArg = struct {
list: *Value,
type: *Type,
@ -2556,6 +2563,31 @@ pub const Module = struct {
},
};
},
.select => blk: {
module.skip_space();
module.expect_character(left_parenthesis);
module.skip_space();
const condition = module.parse_value(function, .{});
module.expect_character(',');
module.skip_space();
const true_value = module.parse_value(function, .{});
module.expect_character(',');
module.skip_space();
const false_value = module.parse_value(function, .{});
module.skip_space();
module.expect_character(right_parenthesis);
break :blk .{
.bb = .{
.intrinsic = .{
.select = .{
.condition = condition,
.true_value = true_value,
.false_value = false_value,
},
},
}
};
},
.trap => blk: {
module.skip_space();
module.expect_character(left_parenthesis);
@ -4848,6 +4880,11 @@ pub const Module = struct {
module.report_error();
}
},
.select => |select| {
module.analyze_value_type(function, select.condition, .{});
module.analyze_value_type(function, select.true_value, .{ .type = expected_type });
module.analyze_value_type(function, select.false_value, .{ .type = expected_type });
},
.truncate => |value_to_truncate| {
module.analyze_value_type(function, value_to_truncate, .{});
if (expected_type.get_bit_size() >= value_to_truncate.type.?.get_bit_size()) {
@ -5415,6 +5452,20 @@ pub const Module = struct {
module.emit_value(function, pointer_value);
break :blk pointer_value.llvm.?;
},
.select => |select| blk: {
module.emit_value(function, select.condition);
const condition = switch (select.condition.type.?.bb) {
.integer => |integer| switch (integer.bit_count) {
1 => select.condition.llvm.?,
else => @trap(),
},
else => @trap(),
};
module.emit_value(function, select.true_value);
module.emit_value(function, select.false_value);
const result = module.llvm.builder.create_select(condition, select.true_value.llvm.?, select.false_value.llvm.?);
break :blk result;
},
.trap => blk: {
// TODO: lookup in advance
const intrinsic_id = module.llvm.intrinsic_table.trap;

View File

@ -227,4 +227,5 @@ const names = &[_][]const u8{
"ret_c_bool",
"return_type_builtin",
"return_u64_u64",
"select",
};