authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-23 16:12:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-23 16:12:26-07:00
logbe673e67937bbc3e2c74591f8f447f848b2a566a
treea6784230d35bbb35ea0374984d475228313ac265
parentaa46a705ad998636443e744b63471bf0864e90c9

stage2: implement inttype ZIR

also add i128 and u128 to const inst list

5 files changed, 94 insertions(+), 9 deletions(-)

src/Module.zig+5-2
......@@ -3599,11 +3599,14 @@ pub fn lookupDeclName(mod: *Module, scope: *Scope, ident_name: []const u8) ?*Dec
35993599 return mod.decl_table.get(name_hash);
36003600}
36013601
3602pub fn makeIntType(arena: *Allocator, signed: bool, bits: u16) !Type {
3602pub fn makeIntType(arena: *Allocator, signedness: std.builtin.Signedness, bits: u16) !Type {
36033603 const int_payload = try arena.create(Type.Payload.Bits);
36043604 int_payload.* = .{
36053605 .base = .{
3606 .tag = if (signed) .int_signed else .int_unsigned,
3606 .tag = switch (signedness) {
3607 .signed => .int_signed,
3608 .unsigned => .int_unsigned,
3609 },
36073610 },
36083611 .data = bits,
36093612 };
src/Sema.zig+7-2
......@@ -1226,7 +1226,11 @@ fn zirIntType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
12261226 const tracy = trace(@src());
12271227 defer tracy.end();
12281228
1229 return sema.mod.fail(&block.base, sema.src, "TODO implement inttype", .{});
1229 const int_type = sema.code.instructions.items(.data)[inst].int_type;
1230 const src = int_type.src();
1231 const ty = try Module.makeIntType(sema.arena, int_type.signedness, int_type.bit_count);
1232
1233 return sema.mod.constType(sema.arena, src, ty);
12301234}
12311235
12321236fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -3967,7 +3971,8 @@ fn cmpNumeric(
39673971 const casted_bits = std.math.cast(u16, max_bits) catch |err| switch (err) {
39683972 error.Overflow => return sema.mod.fail(&block.base, src, "{d} exceeds maximum integer bit count", .{max_bits}),
39693973 };
3970 break :blk try Module.makeIntType(sema.arena, dest_int_is_signed, casted_bits);
3974 const signedness: std.builtin.Signedness = if (dest_int_is_signed) .signed else .unsigned;
3975 break :blk try Module.makeIntType(sema.arena, signedness, casted_bits);
39713976 };
39723977 const casted_lhs = try sema.coerce(block, dest_type, lhs, lhs.src);
39733978 const casted_rhs = try sema.coerce(block, dest_type, rhs, rhs.src);
src/astgen.zig+13-2
......@@ -2888,7 +2888,10 @@ fn identifier(
28882888 if (ident_name.len >= 2) integer: {
28892889 const first_c = ident_name[0];
28902890 if (first_c == 'i' or first_c == 'u') {
2891 const is_signed = first_c == 'i';
2891 const signedness: std.builtin.Signedness = switch (first_c == 'i') {
2892 true => .signed,
2893 false => .unsigned,
2894 };
28922895 const bit_count = std.fmt.parseInt(u16, ident_name[1..], 10) catch |err| switch (err) {
28932896 error.Overflow => return mod.failNode(
28942897 scope,
......@@ -2898,7 +2901,15 @@ fn identifier(
28982901 ),
28992902 error.InvalidCharacter => break :integer,
29002903 };
2901 return rvalue(mod, scope, rl, try gz.addBin(.int_type, @boolToInt(is_signed), bit_count), ident);
2904 const result = try gz.add(.{
2905 .tag = .int_type,
2906 .data = .{ .int_type = .{
2907 .src_node = gz.zir_code.decl.nodeIndexToRelative(ident),
2908 .signedness = signedness,
2909 .bit_count = bit_count,
2910 } },
2911 });
2912 return rvalue(mod, scope, rl, result, ident);
29022913 }
29032914 }
29042915
src/value.zig+36
......@@ -30,6 +30,8 @@ pub const Value = extern union {
3030 i32_type,
3131 u64_type,
3232 i64_type,
33 u128_type,
34 i128_type,
3335 usize_type,
3436 isize_type,
3537 c_short_type,
......@@ -120,6 +122,8 @@ pub const Value = extern union {
120122 .i32_type,
121123 .u64_type,
122124 .i64_type,
125 .u128_type,
126 .i128_type,
123127 .usize_type,
124128 .isize_type,
125129 .c_short_type,
......@@ -274,6 +278,8 @@ pub const Value = extern union {
274278 .i32_type,
275279 .u64_type,
276280 .i64_type,
281 .u128_type,
282 .i128_type,
277283 .usize_type,
278284 .isize_type,
279285 .c_short_type,
......@@ -427,6 +433,8 @@ pub const Value = extern union {
427433 .i32_type => return out_stream.writeAll("i32"),
428434 .u64_type => return out_stream.writeAll("u64"),
429435 .i64_type => return out_stream.writeAll("i64"),
436 .u128_type => return out_stream.writeAll("u128"),
437 .i128_type => return out_stream.writeAll("i128"),
430438 .isize_type => return out_stream.writeAll("isize"),
431439 .usize_type => return out_stream.writeAll("usize"),
432440 .c_short_type => return out_stream.writeAll("c_short"),
......@@ -554,6 +562,8 @@ pub const Value = extern union {
554562 .i32_type => Type.initTag(.i32),
555563 .u64_type => Type.initTag(.u64),
556564 .i64_type => Type.initTag(.i64),
565 .u128_type => Type.initTag(.u128),
566 .i128_type => Type.initTag(.i128),
557567 .usize_type => Type.initTag(.usize),
558568 .isize_type => Type.initTag(.isize),
559569 .c_short_type => Type.initTag(.c_short),
......@@ -650,6 +660,8 @@ pub const Value = extern union {
650660 .i32_type,
651661 .u64_type,
652662 .i64_type,
663 .u128_type,
664 .i128_type,
653665 .usize_type,
654666 .isize_type,
655667 .c_short_type,
......@@ -736,6 +748,8 @@ pub const Value = extern union {
736748 .i32_type,
737749 .u64_type,
738750 .i64_type,
751 .u128_type,
752 .i128_type,
739753 .usize_type,
740754 .isize_type,
741755 .c_short_type,
......@@ -822,6 +836,8 @@ pub const Value = extern union {
822836 .i32_type,
823837 .u64_type,
824838 .i64_type,
839 .u128_type,
840 .i128_type,
825841 .usize_type,
826842 .isize_type,
827843 .c_short_type,
......@@ -935,6 +951,8 @@ pub const Value = extern union {
935951 .i32_type,
936952 .u64_type,
937953 .i64_type,
954 .u128_type,
955 .i128_type,
938956 .usize_type,
939957 .isize_type,
940958 .c_short_type,
......@@ -1026,6 +1044,8 @@ pub const Value = extern union {
10261044 .i32_type,
10271045 .u64_type,
10281046 .i64_type,
1047 .u128_type,
1048 .i128_type,
10291049 .usize_type,
10301050 .isize_type,
10311051 .c_short_type,
......@@ -1182,6 +1202,8 @@ pub const Value = extern union {
11821202 .i32_type,
11831203 .u64_type,
11841204 .i64_type,
1205 .u128_type,
1206 .i128_type,
11851207 .usize_type,
11861208 .isize_type,
11871209 .c_short_type,
......@@ -1265,6 +1287,8 @@ pub const Value = extern union {
12651287 .i32_type,
12661288 .u64_type,
12671289 .i64_type,
1290 .u128_type,
1291 .i128_type,
12681292 .usize_type,
12691293 .isize_type,
12701294 .c_short_type,
......@@ -1416,6 +1440,8 @@ pub const Value = extern union {
14161440 .i32_type,
14171441 .u64_type,
14181442 .i64_type,
1443 .u128_type,
1444 .i128_type,
14191445 .usize_type,
14201446 .isize_type,
14211447 .c_short_type,
......@@ -1573,6 +1599,8 @@ pub const Value = extern union {
15731599 .i32_type,
15741600 .u64_type,
15751601 .i64_type,
1602 .u128_type,
1603 .i128_type,
15761604 .usize_type,
15771605 .isize_type,
15781606 .c_short_type,
......@@ -1659,6 +1687,8 @@ pub const Value = extern union {
16591687 .i32_type,
16601688 .u64_type,
16611689 .i64_type,
1690 .u128_type,
1691 .i128_type,
16621692 .usize_type,
16631693 .isize_type,
16641694 .c_short_type,
......@@ -1762,6 +1792,8 @@ pub const Value = extern union {
17621792 .i32_type,
17631793 .u64_type,
17641794 .i64_type,
1795 .u128_type,
1796 .i128_type,
17651797 .usize_type,
17661798 .isize_type,
17671799 .c_short_type,
......@@ -1843,6 +1875,8 @@ pub const Value = extern union {
18431875 .i32_type,
18441876 .u64_type,
18451877 .i64_type,
1878 .u128_type,
1879 .i128_type,
18461880 .usize_type,
18471881 .isize_type,
18481882 .c_short_type,
......@@ -1944,6 +1978,8 @@ pub const Value = extern union {
19441978 .i32_type,
19451979 .u64_type,
19461980 .i64_type,
1981 .u128_type,
1982 .i128_type,
19471983 .usize_type,
19481984 .isize_type,
19491985 .c_short_type,
src/zir.zig+33-3
......@@ -125,6 +125,8 @@ pub const Const = enum {
125125 i32_type,
126126 u64_type,
127127 i64_type,
128 u128_type,
129 i128_type,
128130 usize_type,
129131 isize_type,
130132 c_short_type,
......@@ -210,6 +212,14 @@ pub const const_inst_list = std.enums.directEnumArray(Const, TypedValue, 0, .{
210212 .ty = Type.initTag(.type),
211213 .val = Value.initTag(.i64_type),
212214 },
215 .u128_type = .{
216 .ty = Type.initTag(.type),
217 .val = Value.initTag(.u128_type),
218 },
219 .i128_type = .{
220 .ty = Type.initTag(.type),
221 .val = Value.initTag(.i128_type),
222 },
213223 .usize_type = .{
214224 .ty = Type.initTag(.type),
215225 .val = Value.initTag(.usize_type),
......@@ -619,8 +629,7 @@ pub const Inst = struct {
619629 /// Payload is `Bin` with lhs as the dest type, rhs the operand.
620630 intcast,
621631 /// Make an integer type out of signedness and bit count.
622 /// lhs is signedness, rhs is bit count.
623 /// Payload is `Bin`
632 /// Payload is `int_type`
624633 int_type,
625634 /// Return a boolean false if an optional is null. `x != null`
626635 /// Uses the `un_tok` field.
......@@ -1135,6 +1144,17 @@ pub const Inst = struct {
11351144 /// For `fn_type_cc` this points to `FnTypeCc` in `extra`.
11361145 payload_index: u32,
11371146 },
1147 int_type: struct {
1148 /// Offset from Decl AST node index.
1149 /// `Tag` determines which kind of AST node this points to.
1150 src_node: i32,
1151 signedness: std.builtin.Signedness,
1152 bit_count: u16,
1153
1154 pub fn src(self: @This()) LazySrcLoc {
1155 return .{ .node_offset = self.src_node };
1156 }
1157 },
11381158 bool_br: struct {
11391159 lhs: Ref,
11401160 /// Points to a `Block`.
......@@ -1340,7 +1360,6 @@ const Writer = struct {
13401360 .elem_ptr,
13411361 .elem_val,
13421362 .intcast,
1343 .int_type,
13441363 .merge_error_sets,
13451364 => try self.writeBin(stream, inst),
13461365
......@@ -1405,6 +1424,7 @@ const Writer = struct {
14051424 .str => try self.writeStr(stream, inst),
14061425 .elided => try stream.writeAll(")"),
14071426 .break_void_node => try self.writeBreakVoidNode(stream, inst),
1427 .int_type => try self.writeIntType(stream, inst),
14081428
14091429 .@"asm",
14101430 .asm_volatile,
......@@ -1742,6 +1762,16 @@ const Writer = struct {
17421762 try self.writeSrc(stream, inst_data.src());
17431763 }
17441764
1765 fn writeIntType(self: *Writer, stream: anytype, inst: Inst.Index) !void {
1766 const int_type = self.code.instructions.items(.data)[inst].int_type;
1767 const prefix: u8 = switch (int_type.signedness) {
1768 .signed => 'i',
1769 .unsigned => 'u',
1770 };
1771 try stream.print("{c}{d}) ", .{ prefix, int_type.bit_count });
1772 try self.writeSrc(stream, int_type.src());
1773 }
1774
17451775 fn writeUnreachable(self: *Writer, stream: anytype, inst: Inst.Index) !void {
17461776 const inst_data = self.code.instructions.items(.data)[inst].@"unreachable";
17471777 const safety_str = if (inst_data.safety) "safe" else "unsafe";