authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-27 17:08:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-27 17:08:37-07:00
logdc88864c9742029c2980fc16cd2c9e6f04ff3568
tree2569f14d189ef238ca7a281774e09ba49374113b
parent66e5920dc3411daa4f0c84a8f4c733c1263e8523

stage2: implement `@boolToInt`

This is the first commit in which some behavior tests are passing for both stage1 and stage2.

15 files changed, 92 insertions(+), 16 deletions(-)

doc/langref.html.in+2-2
......@@ -7165,8 +7165,8 @@ fn func(y: *i32) void {
71657165 {#header_open|@boolToInt#}
71667166 <pre>{#syntax#}@boolToInt(value: bool) u1{#endsyntax#}</pre>
71677167 <p>
7168 Converts {#syntax#}true{#endsyntax#} to {#syntax#}u1(1){#endsyntax#} and {#syntax#}false{#endsyntax#} to
7169 {#syntax#}u1(0){#endsyntax#}.
7168 Converts {#syntax#}true{#endsyntax#} to {#syntax#}@as(u1, 1){#endsyntax#} and {#syntax#}false{#endsyntax#} to
7169 {#syntax#}@as(u1, 0){#endsyntax#}.
71707170 </p>
71717171 <p>
71727172 If the value is known at compile-time, the return type is {#syntax#}comptime_int{#endsyntax#}
src/Air.zig+6
......@@ -189,6 +189,10 @@ pub const Inst = struct {
189189 /// Converts a pointer to its address. Result type is always `usize`.
190190 /// Uses the `un_op` field.
191191 ptrtoint,
192 /// Given a boolean, returns 0 or 1.
193 /// Result type is always `u1`.
194 /// Uses the `un_op` field.
195 bool_to_int,
192196 /// Stores a value onto the stack and returns a pointer to it.
193197 /// TODO audit where this AIR instruction is emitted, maybe it should instead be emitting
194198 /// alloca instruction and storing to the alloca.
......@@ -490,6 +494,8 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
490494 .slice_len,
491495 => return Type.initTag(.usize),
492496
497 .bool_to_int => return Type.initTag(.u1),
498
493499 .call => {
494500 const callee_ty = air.typeOf(datas[inst].pl_op.operand);
495501 return callee_ty.fnReturnType();
src/AstGen.zig+2
......@@ -7754,6 +7754,7 @@ pub const simple_types = std.ComptimeStringMap(Zir.Inst.Ref, .{
77547754 .{ "u32", .u32_type },
77557755 .{ "u64", .u64_type },
77567756 .{ "u128", .u128_type },
7757 .{ "u1", .u1_type },
77577758 .{ "u8", .u8_type },
77587759 .{ "undefined", .undef },
77597760 .{ "usize", .usize_type },
......@@ -8400,6 +8401,7 @@ fn rvalue(
84008401 const as_usize = @as(u64, @enumToInt(Zir.Inst.Ref.usize_type)) << 32;
84018402 const as_void = @as(u64, @enumToInt(Zir.Inst.Ref.void_type)) << 32;
84028403 switch ((@as(u64, @enumToInt(ty_inst)) << 32) | @as(u64, @enumToInt(result))) {
8404 as_ty | @enumToInt(Zir.Inst.Ref.u1_type),
84038405 as_ty | @enumToInt(Zir.Inst.Ref.u8_type),
84048406 as_ty | @enumToInt(Zir.Inst.Ref.i8_type),
84058407 as_ty | @enumToInt(Zir.Inst.Ref.u16_type),
src/Liveness.zig+1
......@@ -291,6 +291,7 @@ fn analyzeInst(
291291 .is_err_ptr,
292292 .is_non_err_ptr,
293293 .ptrtoint,
294 .bool_to_int,
294295 .ret,
295296 => {
296297 const operand = inst_datas[inst].un_op;
src/Sema.zig+9-2
......@@ -5848,8 +5848,14 @@ fn zirAlignOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
58485848
58495849fn zirBoolToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
58505850 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5851 const src = inst_data.src();
5852 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBoolToInt", .{});
5851 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
5852 const operand = sema.resolveInst(inst_data.operand);
5853 if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |val| {
5854 if (val.isUndef()) return sema.addConstUndef(Type.initTag(.u1));
5855 const bool_ints = [2]Air.Inst.Ref{ .zero, .one };
5856 return bool_ints[@boolToInt(val.toBool())];
5857 }
5858 return block.addUnOp(.bool_to_int, operand);
58535859}
58545860
58555861fn zirEmbedFile(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -8252,6 +8258,7 @@ fn typeHasOnePossibleValue(
82528258 .c_longdouble,
82538259 .comptime_int,
82548260 .comptime_float,
8261 .u1,
82558262 .u8,
82568263 .i8,
82578264 .u16,
src/Zir.zig+5
......@@ -1633,6 +1633,7 @@ pub const Inst = struct {
16331633 /// value and may instead be used as a sentinel to indicate null.
16341634 none,
16351635
1636 u1_type,
16361637 u8_type,
16371638 i8_type,
16381639 u16_type,
......@@ -1719,6 +1720,10 @@ pub const Inst = struct {
17191720 pub const typed_value_map = std.enums.directEnumArray(Ref, TypedValue, 0, .{
17201721 .none = undefined,
17211722
1723 .u1_type = .{
1724 .ty = Type.initTag(.type),
1725 .val = Value.initTag(.u1_type),
1726 },
17221727 .u8_type = .{
17231728 .ty = Type.initTag(.type),
17241729 .val = Value.initTag(.u8_type),
src/codegen.zig+8
......@@ -835,6 +835,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
835835 .dbg_stmt => try self.airDbgStmt(inst),
836836 .floatcast => try self.airFloatCast(inst),
837837 .intcast => try self.airIntCast(inst),
838 .bool_to_int => try self.airBoolToInt(inst),
838839 .is_non_null => try self.airIsNonNull(inst),
839840 .is_non_null_ptr => try self.airIsNonNullPtr(inst),
840841 .is_null => try self.airIsNull(inst),
......@@ -1110,6 +1111,13 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
11101111 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
11111112 }
11121113
1114 fn airBoolToInt(self: *Self, inst: Air.Inst.Index) !void {
1115 const un_op = self.air.instructions.items(.data)[inst].un_op;
1116 const operand = try self.resolveInst(un_op);
1117 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else operand;
1118 return self.finishAir(inst, result, .{ un_op, .none, .none });
1119 }
1120
11131121 fn airNot(self: *Self, inst: Air.Inst.Index) !void {
11141122 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
11151123 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
src/codegen/c.zig+15
......@@ -925,6 +925,7 @@ fn genBody(o: *Object, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfM
925925 .call => try airCall(o, inst),
926926 .dbg_stmt => try airDbgStmt(o, inst),
927927 .intcast => try airIntCast(o, inst),
928 .bool_to_int => try airBoolToInt(o, inst),
928929 .load => try airLoad(o, inst),
929930 .ret => try airRet(o, inst),
930931 .store => try airStore(o, inst),
......@@ -1083,6 +1084,20 @@ fn airIntCast(o: *Object, inst: Air.Inst.Index) !CValue {
10831084 return local;
10841085}
10851086
1087fn airBoolToInt(o: *Object, inst: Air.Inst.Index) !CValue {
1088 if (o.liveness.isUnused(inst))
1089 return CValue.none;
1090 const un_op = o.air.instructions.items(.data)[inst].un_op;
1091 const writer = o.writer();
1092 const inst_ty = o.air.typeOfIndex(inst);
1093 const operand = try o.resolveInst(un_op);
1094 const local = try o.allocLocal(inst_ty, .Const);
1095 try writer.writeAll(" = ");
1096 try o.writeCValue(writer, operand);
1097 try writer.writeAll(";\n");
1098 return local;
1099}
1100
10861101fn airStore(o: *Object, inst: Air.Inst.Index) !CValue {
10871102 // *a = b;
10881103 const bin_op = o.air.instructions.items(.data)[inst].bin_op;
src/codegen/llvm.zig+10
......@@ -961,6 +961,7 @@ pub const FuncGen = struct {
961961 .alloc => try self.airAlloc(inst),
962962 .arg => try self.airArg(inst),
963963 .bitcast => try self.airBitCast(inst),
964 .bool_to_int=> try self.airBoolToInt(inst),
964965 .block => try self.airBlock(inst),
965966 .br => try self.airBr(inst),
966967 .switch_br => try self.airSwitchBr(inst),
......@@ -1656,6 +1657,15 @@ pub const FuncGen = struct {
16561657 return self.builder.buildBitCast(operand, dest_type, "");
16571658 }
16581659
1660 fn airBoolToInt(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
1661 if (self.liveness.isUnused(inst))
1662 return null;
1663
1664 const un_op = self.air.instructions.items(.data)[inst].un_op;
1665 const operand = try self.resolveInst(un_op);
1666 return operand;
1667 }
1668
16591669 fn airArg(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
16601670 const arg_val = self.args[self.arg_index];
16611671 self.arg_index += 1;
src/link/Coff.zig+5-2
......@@ -885,7 +885,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
885885 // Both stage1 and stage2 LLVM backend put the object file in the cache directory.
886886 if (self.base.options.use_llvm) {
887887 // Stage2 has to call flushModule since that outputs the LLVM object file.
888 if (!build_options.is_stage1) try self.flushModule(comp);
888 if (!build_options.is_stage1 or !self.base.options.use_stage1) try self.flushModule(comp);
889889
890890 const obj_basename = try std.zig.binNameAlloc(arena, .{
891891 .root_name = self.base.options.root_name,
......@@ -1269,7 +1269,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
12691269
12701270 // TODO: remove when stage2 can build compiler_rt.zig, c.zig and ssp.zig
12711271 // compiler-rt, libc and libssp
1272 if (is_exe_or_dyn_lib and !self.base.options.skip_linker_dependencies and build_options.is_stage1) {
1272 if (is_exe_or_dyn_lib and
1273 !self.base.options.skip_linker_dependencies and
1274 build_options.is_stage1 and self.base.options.use_stage1)
1275 {
12731276 if (!self.base.options.link_libc) {
12741277 try argv.append(comp.libc_static_lib.?.full_object_path);
12751278 }
src/link/Elf.zig+4-3
......@@ -1257,7 +1257,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
12571257 // Both stage1 and stage2 LLVM backend put the object file in the cache directory.
12581258 if (self.base.options.use_llvm) {
12591259 // Stage2 has to call flushModule since that outputs the LLVM object file.
1260 if (!build_options.is_stage1) try self.flushModule(comp);
1260 if (!build_options.is_stage1 or !self.base.options.use_stage1) try self.flushModule(comp);
12611261
12621262 const obj_basename = try std.zig.binNameAlloc(arena, .{
12631263 .root_name = self.base.options.root_name,
......@@ -1287,7 +1287,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
12871287 const allow_shlib_undefined = self.base.options.allow_shlib_undefined orelse !self.base.options.is_native_os;
12881288 const compiler_rt_path: ?[]const u8 = if (self.base.options.include_compiler_rt) blk: {
12891289 // TODO: remove when stage2 can build compiler_rt.zig
1290 if (!build_options.is_stage1) break :blk null;
1290 if (!build_options.is_stage1 or !self.base.options.use_stage1) break :blk null;
12911291
12921292 // In the case of build-obj we include the compiler-rt symbols directly alongside
12931293 // the symbols of the root source file, in the same compilation unit.
......@@ -1605,7 +1605,8 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16051605 if (is_exe_or_dyn_lib and
16061606 !self.base.options.skip_linker_dependencies and
16071607 !self.base.options.link_libc and
1608 build_options.is_stage1)
1608 build_options.is_stage1 and
1609 self.base.options.use_stage1)
16091610 {
16101611 try argv.append(comp.libc_static_lib.?.full_object_path);
16111612 }
src/print_air.zig+1
......@@ -137,6 +137,7 @@ const Writer = struct {
137137 .is_err_ptr,
138138 .is_non_err_ptr,
139139 .ptrtoint,
140 .bool_to_int,
140141 .ret,
141142 => try w.writeUnOp(s, inst),
142143
src/type.zig+15-2
......@@ -23,6 +23,7 @@ pub const Type = extern union {
2323
2424 pub fn zigTypeTag(self: Type) std.builtin.TypeId {
2525 switch (self.tag()) {
26 .u1,
2627 .u8,
2728 .i8,
2829 .u16,
......@@ -638,6 +639,7 @@ pub const Type = extern union {
638639 if (self.tag_if_small_enough < Tag.no_payload_count) {
639640 return Type{ .tag_if_small_enough = self.tag_if_small_enough };
640641 } else switch (self.ptr_otherwise.tag) {
642 .u1,
641643 .u8,
642644 .i8,
643645 .u16,
......@@ -819,6 +821,7 @@ pub const Type = extern union {
819821 while (true) {
820822 const t = ty.tag();
821823 switch (t) {
824 .u1,
822825 .u8,
823826 .i8,
824827 .u16,
......@@ -1082,6 +1085,7 @@ pub const Type = extern union {
10821085
10831086 pub fn toValue(self: Type, allocator: *Allocator) Allocator.Error!Value {
10841087 switch (self.tag()) {
1088 .u1 => return Value.initTag(.u1_type),
10851089 .u8 => return Value.initTag(.u8_type),
10861090 .i8 => return Value.initTag(.i8_type),
10871091 .u16 => return Value.initTag(.u16_type),
......@@ -1141,6 +1145,7 @@ pub const Type = extern union {
11411145
11421146 pub fn hasCodeGenBits(self: Type) bool {
11431147 return switch (self.tag()) {
1148 .u1,
11441149 .u8,
11451150 .i8,
11461151 .u16,
......@@ -1321,6 +1326,7 @@ pub const Type = extern union {
13211326 /// Asserts that hasCodeGenBits() is true.
13221327 pub fn abiAlignment(self: Type, target: Target) u32 {
13231328 return switch (self.tag()) {
1329 .u1,
13241330 .u8,
13251331 .i8,
13261332 .bool,
......@@ -1539,6 +1545,7 @@ pub const Type = extern union {
15391545 @panic("TODO abiSize unions");
15401546 },
15411547
1548 .u1,
15421549 .u8,
15431550 .i8,
15441551 .bool,
......@@ -1704,7 +1711,7 @@ pub const Type = extern union {
17041711
17051712 .u8, .i8 => 8,
17061713
1707 .bool => 1,
1714 .bool, .u1 => 1,
17081715
17091716 .vector => {
17101717 const payload = self.castTag(.vector).?.data;
......@@ -2217,12 +2224,13 @@ pub const Type = extern union {
22172224 pub fn isUnsignedInt(self: Type) bool {
22182225 return switch (self.tag()) {
22192226 .int_unsigned,
2220 .u8,
22212227 .usize,
22222228 .c_ushort,
22232229 .c_uint,
22242230 .c_ulong,
22252231 .c_ulonglong,
2232 .u1,
2233 .u8,
22262234 .u16,
22272235 .u32,
22282236 .u64,
......@@ -2244,6 +2252,7 @@ pub const Type = extern union {
22442252 .signedness = .signed,
22452253 .bits = self.castTag(.int_signed).?.data,
22462254 },
2255 .u1 => .{ .signedness = .unsigned, .bits = 1 },
22472256 .u8 => .{ .signedness = .unsigned, .bits = 8 },
22482257 .i8 => .{ .signedness = .signed, .bits = 8 },
22492258 .u16 => .{ .signedness = .unsigned, .bits = 16 },
......@@ -2406,6 +2415,7 @@ pub const Type = extern union {
24062415 .c_longdouble,
24072416 .comptime_int,
24082417 .comptime_float,
2418 .u1,
24092419 .u8,
24102420 .i8,
24112421 .u16,
......@@ -2446,6 +2456,7 @@ pub const Type = extern union {
24462456 .c_longdouble,
24472457 .comptime_int,
24482458 .comptime_float,
2459 .u1,
24492460 .u8,
24502461 .i8,
24512462 .u16,
......@@ -2911,6 +2922,7 @@ pub const Type = extern union {
29112922 /// See `zigTypeTag` for the function that corresponds to `std.builtin.TypeId`.
29122923 pub const Tag = enum {
29132924 // The first section of this enum are tags that require no payload.
2925 u1,
29142926 u8,
29152927 i8,
29162928 u16,
......@@ -3018,6 +3030,7 @@ pub const Type = extern union {
30183030
30193031 pub fn Type(comptime t: Tag) type {
30203032 return switch (t) {
3033 .u1,
30213034 .u8,
30223035 .i8,
30233036 .u16,
src/value.zig+7
......@@ -22,6 +22,7 @@ pub const Value = extern union {
2222
2323 pub const Tag = enum {
2424 // The first section of this enum are tags that require no payload.
25 u1_type,
2526 u8_type,
2627 i8_type,
2728 u16_type,
......@@ -138,6 +139,7 @@ pub const Value = extern union {
138139
139140 pub fn Type(comptime t: Tag) type {
140141 return switch (t) {
142 .u1_type,
141143 .u8_type,
142144 .i8_type,
143145 .u16_type,
......@@ -314,6 +316,7 @@ pub const Value = extern union {
314316 if (self.tag_if_small_enough < Tag.no_payload_count) {
315317 return Value{ .tag_if_small_enough = self.tag_if_small_enough };
316318 } else switch (self.ptr_otherwise.tag) {
319 .u1_type,
317320 .u8_type,
318321 .i8_type,
319322 .u16_type,
......@@ -520,6 +523,7 @@ pub const Value = extern union {
520523 comptime assert(fmt.len == 0);
521524 var val = start_val;
522525 while (true) switch (val.tag()) {
526 .u1_type => return out_stream.writeAll("u1"),
523527 .u8_type => return out_stream.writeAll("u8"),
524528 .i8_type => return out_stream.writeAll("i8"),
525529 .u16_type => return out_stream.writeAll("u16"),
......@@ -671,6 +675,7 @@ pub const Value = extern union {
671675 pub fn toType(self: Value, allocator: *Allocator) !Type {
672676 return switch (self.tag()) {
673677 .ty => self.castTag(.ty).?.data,
678 .u1_type => Type.initTag(.u1),
674679 .u8_type => Type.initTag(.u8),
675680 .i8_type => Type.initTag(.i8),
676681 .u16_type => Type.initTag(.u16),
......@@ -1150,6 +1155,7 @@ pub const Value = extern union {
11501155 var hasher = std.hash.Wyhash.init(0);
11511156
11521157 switch (self.tag()) {
1158 .u1_type,
11531159 .u8_type,
11541160 .i8_type,
11551161 .u16_type,
......@@ -1502,6 +1508,7 @@ pub const Value = extern union {
15021508 return switch (self.tag()) {
15031509 .ty,
15041510 .int_type,
1511 .u1_type,
15051512 .u8_type,
15061513 .i8_type,
15071514 .u16_type,
test/behavior.zig+2-5
......@@ -2,11 +2,9 @@ const builtin = @import("builtin");
22
33test {
44 // Tests that pass for both.
5 {}
5 _ = @import("behavior/bool.zig");
66
7 if (builtin.zig_is_stage2) {
8 // Tests that only pass for stage2.
9 } else {
7 if (!builtin.zig_is_stage2) {
108 // Tests that only pass for stage1.
119 _ = @import("behavior/align.zig");
1210 _ = @import("behavior/alignof.zig");
......@@ -20,7 +18,6 @@ test {
2018 _ = @import("behavior/bit_shifting.zig");
2119 _ = @import("behavior/bitcast.zig");
2220 _ = @import("behavior/bitreverse.zig");
23 _ = @import("behavior/bool.zig");
2421 _ = @import("behavior/bugs/1025.zig");
2522 _ = @import("behavior/bugs/1076.zig");
2623 _ = @import("behavior/bugs/1111.zig");