diff --git a/src/bootstrap.zig b/src/bootstrap.zig index 2457350..61140ee 100644 --- a/src/bootstrap.zig +++ b/src/bootstrap.zig @@ -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; diff --git a/src/main.zig b/src/main.zig index 6db4100..76ef9bc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -227,4 +227,5 @@ const names = &[_][]const u8{ "ret_c_bool", "return_type_builtin", "return_u64_u64", + "select", };