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...@@ -3599,11 +3599,14 @@ pub fn lookupDeclName(mod: *Module, scope: *Scope, ident_name: []const u8) ?*Dec
3599 return mod.decl_table.get(name_hash);3599 return mod.decl_table.get(name_hash);
3600}3600}
36013601
3602pub fn makeIntType(arena: *Allocator, signed: bool, bits: u16) !Type {3602pub fn makeIntType(arena: *Allocator, signedness: std.builtin.Signedness, bits: u16) !Type {
3603 const int_payload = try arena.create(Type.Payload.Bits);3603 const int_payload = try arena.create(Type.Payload.Bits);
3604 int_payload.* = .{3604 int_payload.* = .{
3605 .base = .{3605 .base = .{
3606 .tag = if (signed) .int_signed else .int_unsigned,3606 .tag = switch (signedness) {
3607 .signed => .int_signed,
3608 .unsigned => .int_unsigned,
3609 },
3607 },3610 },
3608 .data = bits,3611 .data = bits,
3609 };3612 };
src/Sema.zig+7-2
...@@ -1226,7 +1226,11 @@ fn zirIntType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError...@@ -1226,7 +1226,11 @@ fn zirIntType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
1226 const tracy = trace(@src());1226 const tracy = trace(@src());
1227 defer tracy.end();1227 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);
1230}1234}
12311235
1232fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {1236fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
...@@ -3967,7 +3971,8 @@ fn cmpNumeric(...@@ -3967,7 +3971,8 @@ fn cmpNumeric(
3967 const casted_bits = std.math.cast(u16, max_bits) catch |err| switch (err) {3971 const casted_bits = std.math.cast(u16, max_bits) catch |err| switch (err) {
3968 error.Overflow => return sema.mod.fail(&block.base, src, "{d} exceeds maximum integer bit count", .{max_bits}),3972 error.Overflow => return sema.mod.fail(&block.base, src, "{d} exceeds maximum integer bit count", .{max_bits}),
3969 };3973 };
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);
3971 };3976 };
3972 const casted_lhs = try sema.coerce(block, dest_type, lhs, lhs.src);3977 const casted_lhs = try sema.coerce(block, dest_type, lhs, lhs.src);
3973 const casted_rhs = try sema.coerce(block, dest_type, rhs, rhs.src);3978 const casted_rhs = try sema.coerce(block, dest_type, rhs, rhs.src);
src/astgen.zig+13-2
...@@ -2888,7 +2888,10 @@ fn identifier(...@@ -2888,7 +2888,10 @@ fn identifier(
2888 if (ident_name.len >= 2) integer: {2888 if (ident_name.len >= 2) integer: {
2889 const first_c = ident_name[0];2889 const first_c = ident_name[0];
2890 if (first_c == 'i' or first_c == 'u') {2890 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 };
2892 const bit_count = std.fmt.parseInt(u16, ident_name[1..], 10) catch |err| switch (err) {2895 const bit_count = std.fmt.parseInt(u16, ident_name[1..], 10) catch |err| switch (err) {
2893 error.Overflow => return mod.failNode(2896 error.Overflow => return mod.failNode(
2894 scope,2897 scope,
...@@ -2898,7 +2901,15 @@ fn identifier(...@@ -2898,7 +2901,15 @@ fn identifier(
2898 ),2901 ),
2899 error.InvalidCharacter => break :integer,2902 error.InvalidCharacter => break :integer,
2900 };2903 };
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);
2902 }2913 }
2903 }2914 }
29042915
src/value.zig+36
...@@ -30,6 +30,8 @@ pub const Value = extern union {...@@ -30,6 +30,8 @@ pub const Value = extern union {
30 i32_type,30 i32_type,
31 u64_type,31 u64_type,
32 i64_type,32 i64_type,
33 u128_type,
34 i128_type,
33 usize_type,35 usize_type,
34 isize_type,36 isize_type,
35 c_short_type,37 c_short_type,
...@@ -120,6 +122,8 @@ pub const Value = extern union {...@@ -120,6 +122,8 @@ pub const Value = extern union {
120 .i32_type,122 .i32_type,
121 .u64_type,123 .u64_type,
122 .i64_type,124 .i64_type,
125 .u128_type,
126 .i128_type,
123 .usize_type,127 .usize_type,
124 .isize_type,128 .isize_type,
125 .c_short_type,129 .c_short_type,
...@@ -274,6 +278,8 @@ pub const Value = extern union {...@@ -274,6 +278,8 @@ pub const Value = extern union {
274 .i32_type,278 .i32_type,
275 .u64_type,279 .u64_type,
276 .i64_type,280 .i64_type,
281 .u128_type,
282 .i128_type,
277 .usize_type,283 .usize_type,
278 .isize_type,284 .isize_type,
279 .c_short_type,285 .c_short_type,
...@@ -427,6 +433,8 @@ pub const Value = extern union {...@@ -427,6 +433,8 @@ pub const Value = extern union {
427 .i32_type => return out_stream.writeAll("i32"),433 .i32_type => return out_stream.writeAll("i32"),
428 .u64_type => return out_stream.writeAll("u64"),434 .u64_type => return out_stream.writeAll("u64"),
429 .i64_type => return out_stream.writeAll("i64"),435 .i64_type => return out_stream.writeAll("i64"),
436 .u128_type => return out_stream.writeAll("u128"),
437 .i128_type => return out_stream.writeAll("i128"),
430 .isize_type => return out_stream.writeAll("isize"),438 .isize_type => return out_stream.writeAll("isize"),
431 .usize_type => return out_stream.writeAll("usize"),439 .usize_type => return out_stream.writeAll("usize"),
432 .c_short_type => return out_stream.writeAll("c_short"),440 .c_short_type => return out_stream.writeAll("c_short"),
...@@ -554,6 +562,8 @@ pub const Value = extern union {...@@ -554,6 +562,8 @@ pub const Value = extern union {
554 .i32_type => Type.initTag(.i32),562 .i32_type => Type.initTag(.i32),
555 .u64_type => Type.initTag(.u64),563 .u64_type => Type.initTag(.u64),
556 .i64_type => Type.initTag(.i64),564 .i64_type => Type.initTag(.i64),
565 .u128_type => Type.initTag(.u128),
566 .i128_type => Type.initTag(.i128),
557 .usize_type => Type.initTag(.usize),567 .usize_type => Type.initTag(.usize),
558 .isize_type => Type.initTag(.isize),568 .isize_type => Type.initTag(.isize),
559 .c_short_type => Type.initTag(.c_short),569 .c_short_type => Type.initTag(.c_short),
...@@ -650,6 +660,8 @@ pub const Value = extern union {...@@ -650,6 +660,8 @@ pub const Value = extern union {
650 .i32_type,660 .i32_type,
651 .u64_type,661 .u64_type,
652 .i64_type,662 .i64_type,
663 .u128_type,
664 .i128_type,
653 .usize_type,665 .usize_type,
654 .isize_type,666 .isize_type,
655 .c_short_type,667 .c_short_type,
...@@ -736,6 +748,8 @@ pub const Value = extern union {...@@ -736,6 +748,8 @@ pub const Value = extern union {
736 .i32_type,748 .i32_type,
737 .u64_type,749 .u64_type,
738 .i64_type,750 .i64_type,
751 .u128_type,
752 .i128_type,
739 .usize_type,753 .usize_type,
740 .isize_type,754 .isize_type,
741 .c_short_type,755 .c_short_type,
...@@ -822,6 +836,8 @@ pub const Value = extern union {...@@ -822,6 +836,8 @@ pub const Value = extern union {
822 .i32_type,836 .i32_type,
823 .u64_type,837 .u64_type,
824 .i64_type,838 .i64_type,
839 .u128_type,
840 .i128_type,
825 .usize_type,841 .usize_type,
826 .isize_type,842 .isize_type,
827 .c_short_type,843 .c_short_type,
...@@ -935,6 +951,8 @@ pub const Value = extern union {...@@ -935,6 +951,8 @@ pub const Value = extern union {
935 .i32_type,951 .i32_type,
936 .u64_type,952 .u64_type,
937 .i64_type,953 .i64_type,
954 .u128_type,
955 .i128_type,
938 .usize_type,956 .usize_type,
939 .isize_type,957 .isize_type,
940 .c_short_type,958 .c_short_type,
...@@ -1026,6 +1044,8 @@ pub const Value = extern union {...@@ -1026,6 +1044,8 @@ pub const Value = extern union {
1026 .i32_type,1044 .i32_type,
1027 .u64_type,1045 .u64_type,
1028 .i64_type,1046 .i64_type,
1047 .u128_type,
1048 .i128_type,
1029 .usize_type,1049 .usize_type,
1030 .isize_type,1050 .isize_type,
1031 .c_short_type,1051 .c_short_type,
...@@ -1182,6 +1202,8 @@ pub const Value = extern union {...@@ -1182,6 +1202,8 @@ pub const Value = extern union {
1182 .i32_type,1202 .i32_type,
1183 .u64_type,1203 .u64_type,
1184 .i64_type,1204 .i64_type,
1205 .u128_type,
1206 .i128_type,
1185 .usize_type,1207 .usize_type,
1186 .isize_type,1208 .isize_type,
1187 .c_short_type,1209 .c_short_type,
...@@ -1265,6 +1287,8 @@ pub const Value = extern union {...@@ -1265,6 +1287,8 @@ pub const Value = extern union {
1265 .i32_type,1287 .i32_type,
1266 .u64_type,1288 .u64_type,
1267 .i64_type,1289 .i64_type,
1290 .u128_type,
1291 .i128_type,
1268 .usize_type,1292 .usize_type,
1269 .isize_type,1293 .isize_type,
1270 .c_short_type,1294 .c_short_type,
...@@ -1416,6 +1440,8 @@ pub const Value = extern union {...@@ -1416,6 +1440,8 @@ pub const Value = extern union {
1416 .i32_type,1440 .i32_type,
1417 .u64_type,1441 .u64_type,
1418 .i64_type,1442 .i64_type,
1443 .u128_type,
1444 .i128_type,
1419 .usize_type,1445 .usize_type,
1420 .isize_type,1446 .isize_type,
1421 .c_short_type,1447 .c_short_type,
...@@ -1573,6 +1599,8 @@ pub const Value = extern union {...@@ -1573,6 +1599,8 @@ pub const Value = extern union {
1573 .i32_type,1599 .i32_type,
1574 .u64_type,1600 .u64_type,
1575 .i64_type,1601 .i64_type,
1602 .u128_type,
1603 .i128_type,
1576 .usize_type,1604 .usize_type,
1577 .isize_type,1605 .isize_type,
1578 .c_short_type,1606 .c_short_type,
...@@ -1659,6 +1687,8 @@ pub const Value = extern union {...@@ -1659,6 +1687,8 @@ pub const Value = extern union {
1659 .i32_type,1687 .i32_type,
1660 .u64_type,1688 .u64_type,
1661 .i64_type,1689 .i64_type,
1690 .u128_type,
1691 .i128_type,
1662 .usize_type,1692 .usize_type,
1663 .isize_type,1693 .isize_type,
1664 .c_short_type,1694 .c_short_type,
...@@ -1762,6 +1792,8 @@ pub const Value = extern union {...@@ -1762,6 +1792,8 @@ pub const Value = extern union {
1762 .i32_type,1792 .i32_type,
1763 .u64_type,1793 .u64_type,
1764 .i64_type,1794 .i64_type,
1795 .u128_type,
1796 .i128_type,
1765 .usize_type,1797 .usize_type,
1766 .isize_type,1798 .isize_type,
1767 .c_short_type,1799 .c_short_type,
...@@ -1843,6 +1875,8 @@ pub const Value = extern union {...@@ -1843,6 +1875,8 @@ pub const Value = extern union {
1843 .i32_type,1875 .i32_type,
1844 .u64_type,1876 .u64_type,
1845 .i64_type,1877 .i64_type,
1878 .u128_type,
1879 .i128_type,
1846 .usize_type,1880 .usize_type,
1847 .isize_type,1881 .isize_type,
1848 .c_short_type,1882 .c_short_type,
...@@ -1944,6 +1978,8 @@ pub const Value = extern union {...@@ -1944,6 +1978,8 @@ pub const Value = extern union {
1944 .i32_type,1978 .i32_type,
1945 .u64_type,1979 .u64_type,
1946 .i64_type,1980 .i64_type,
1981 .u128_type,
1982 .i128_type,
1947 .usize_type,1983 .usize_type,
1948 .isize_type,1984 .isize_type,
1949 .c_short_type,1985 .c_short_type,
src/zir.zig+33-3
...@@ -125,6 +125,8 @@ pub const Const = enum {...@@ -125,6 +125,8 @@ pub const Const = enum {
125 i32_type,125 i32_type,
126 u64_type,126 u64_type,
127 i64_type,127 i64_type,
128 u128_type,
129 i128_type,
128 usize_type,130 usize_type,
129 isize_type,131 isize_type,
130 c_short_type,132 c_short_type,
...@@ -210,6 +212,14 @@ pub const const_inst_list = std.enums.directEnumArray(Const, TypedValue, 0, .{...@@ -210,6 +212,14 @@ pub const const_inst_list = std.enums.directEnumArray(Const, TypedValue, 0, .{
210 .ty = Type.initTag(.type),212 .ty = Type.initTag(.type),
211 .val = Value.initTag(.i64_type),213 .val = Value.initTag(.i64_type),
212 },214 },
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 },
213 .usize_type = .{223 .usize_type = .{
214 .ty = Type.initTag(.type),224 .ty = Type.initTag(.type),
215 .val = Value.initTag(.usize_type),225 .val = Value.initTag(.usize_type),
...@@ -619,8 +629,7 @@ pub const Inst = struct {...@@ -619,8 +629,7 @@ pub const Inst = struct {
619 /// Payload is `Bin` with lhs as the dest type, rhs the operand.629 /// Payload is `Bin` with lhs as the dest type, rhs the operand.
620 intcast,630 intcast,
621 /// Make an integer type out of signedness and bit count.631 /// Make an integer type out of signedness and bit count.
622 /// lhs is signedness, rhs is bit count.632 /// Payload is `int_type`
623 /// Payload is `Bin`
624 int_type,633 int_type,
625 /// Return a boolean false if an optional is null. `x != null`634 /// Return a boolean false if an optional is null. `x != null`
626 /// Uses the `un_tok` field.635 /// Uses the `un_tok` field.
...@@ -1135,6 +1144,17 @@ pub const Inst = struct {...@@ -1135,6 +1144,17 @@ pub const Inst = struct {
1135 /// For `fn_type_cc` this points to `FnTypeCc` in `extra`.1144 /// For `fn_type_cc` this points to `FnTypeCc` in `extra`.
1136 payload_index: u32,1145 payload_index: u32,
1137 },1146 },
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 },
1138 bool_br: struct {1158 bool_br: struct {
1139 lhs: Ref,1159 lhs: Ref,
1140 /// Points to a `Block`.1160 /// Points to a `Block`.
...@@ -1340,7 +1360,6 @@ const Writer = struct {...@@ -1340,7 +1360,6 @@ const Writer = struct {
1340 .elem_ptr,1360 .elem_ptr,
1341 .elem_val,1361 .elem_val,
1342 .intcast,1362 .intcast,
1343 .int_type,
1344 .merge_error_sets,1363 .merge_error_sets,
1345 => try self.writeBin(stream, inst),1364 => try self.writeBin(stream, inst),
13461365
...@@ -1405,6 +1424,7 @@ const Writer = struct {...@@ -1405,6 +1424,7 @@ const Writer = struct {
1405 .str => try self.writeStr(stream, inst),1424 .str => try self.writeStr(stream, inst),
1406 .elided => try stream.writeAll(")"),1425 .elided => try stream.writeAll(")"),
1407 .break_void_node => try self.writeBreakVoidNode(stream, inst),1426 .break_void_node => try self.writeBreakVoidNode(stream, inst),
1427 .int_type => try self.writeIntType(stream, inst),
14081428
1409 .@"asm",1429 .@"asm",
1410 .asm_volatile,1430 .asm_volatile,
...@@ -1742,6 +1762,16 @@ const Writer = struct {...@@ -1742,6 +1762,16 @@ const Writer = struct {
1742 try self.writeSrc(stream, inst_data.src());1762 try self.writeSrc(stream, inst_data.src());
1743 }1763 }
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
1745 fn writeUnreachable(self: *Writer, stream: anytype, inst: Inst.Index) !void {1775 fn writeUnreachable(self: *Writer, stream: anytype, inst: Inst.Index) !void {
1746 const inst_data = self.code.instructions.items(.data)[inst].@"unreachable";1776 const inst_data = self.code.instructions.items(.data)[inst].@"unreachable";
1747 const safety_str = if (inst_data.safety) "safe" else "unsafe";1777 const safety_str = if (inst_data.safety) "safe" else "unsafe";