authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-03-19 15:07:51-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-19 14:43:08-07:00
loge9810d9e79a1aa327d006c27c5d3098b2d29dfe7
tree35634eeb88e5fa09efeebfe62fc5b44abaca596a
parentabdbc11c7efb2f83804af64ac2fec10b972cdf2a

zir-memory-layout: astgen: fill in identifier


3 files changed, 23 insertions(+), 34 deletions(-)

src/Module.zig+11
......@@ -1124,6 +1124,17 @@ pub const Scope = struct {
11241124 });
11251125 }
11261126
1127 pub fn addDecl(
1128 gz: *GenZir,
1129 tag: zir.Inst.Tag,
1130 decl: *Decl,
1131 ) !zir.Inst.Ref {
1132 return gz.add(.{
1133 .tag = tag,
1134 .data = .{ .decl = decl },
1135 });
1136 }
1137
11271138 pub fn addNode(
11281139 gz: *GenZir,
11291140 tag: zir.Inst.Tag,
src/astgen.zig+11-34
......@@ -2835,23 +2835,22 @@ fn identifier(
28352835 rl: ResultLoc,
28362836 ident: ast.Node.Index,
28372837) InnerError!zir.Inst.Ref {
2838 if (true) @panic("TODO update for zir-memory-layout");
28392838 const tracy = trace(@src());
28402839 defer tracy.end();
28412840
28422841 const tree = scope.tree();
28432842 const main_tokens = tree.nodes.items(.main_token);
2844 const token_starts = tree.tokens.items(.start);
2843
2844 const gz = scope.getGenZir();
28452845
28462846 const ident_token = main_tokens[ident];
28472847 const ident_name = try mod.identifierTokenString(scope, ident_token);
2848 const src = token_starts[ident_token];
28492848 if (mem.eql(u8, ident_name, "_")) {
28502849 return mod.failNode(scope, ident, "TODO implement '_' identifier", .{});
28512850 }
28522851
28532852 if (simple_types.get(ident_name)) |zir_const_tag| {
2854 return rvalue(mod, scope, rl, @enumToInt(zir_const_tag));
2853 return rvalue(mod, scope, rl, @enumToInt(zir_const_tag), ident);
28552854 }
28562855
28572856 if (ident_name.len >= 2) integer: {
......@@ -2867,26 +2866,7 @@ fn identifier(
28672866 ),
28682867 error.InvalidCharacter => break :integer,
28692868 };
2870 const val = switch (bit_count) {
2871 8 => if (is_signed) Value.initTag(.i8_type) else Value.initTag(.u8_type),
2872 16 => if (is_signed) Value.initTag(.i16_type) else Value.initTag(.u16_type),
2873 32 => if (is_signed) Value.initTag(.i32_type) else Value.initTag(.u32_type),
2874 64 => if (is_signed) Value.initTag(.i64_type) else Value.initTag(.u64_type),
2875 else => {
2876 return rvalue(mod, scope, rl, try addZIRInstConst(mod, scope, src, .{
2877 .ty = Type.initTag(.type),
2878 .val = try Value.Tag.int_type.create(scope.arena(), .{
2879 .signed = is_signed,
2880 .bits = bit_count,
2881 }),
2882 }));
2883 },
2884 };
2885 const result = try addZIRInstConst(mod, scope, src, .{
2886 .ty = Type.initTag(.type),
2887 .val = val,
2888 });
2889 return rvalue(mod, scope, rl, result);
2869 return rvalue(mod, scope, rl, try gz.addBin(.int_type, @boolToInt(is_signed), bit_count), ident);
28902870 }
28912871 }
28922872
......@@ -2897,7 +2877,7 @@ fn identifier(
28972877 .local_val => {
28982878 const local_val = s.cast(Scope.LocalVal).?;
28992879 if (mem.eql(u8, local_val.name, ident_name)) {
2900 return rvalue(mod, scope, rl, local_val.inst);
2880 return rvalue(mod, scope, rl, local_val.inst, ident);
29012881 }
29022882 s = local_val.parent;
29032883 },
......@@ -2905,8 +2885,8 @@ fn identifier(
29052885 const local_ptr = s.cast(Scope.LocalPtr).?;
29062886 if (mem.eql(u8, local_ptr.name, ident_name)) {
29072887 if (rl == .ref) return local_ptr.ptr;
2908 const loaded = try addZIRUnOp(mod, scope, src, .deref, local_ptr.ptr);
2909 return rvalue(mod, scope, rl, loaded);
2888 const loaded = try gz.addUnNode(.deref_node, local_ptr.ptr, ident);
2889 return rvalue(mod, scope, rl, loaded, ident);
29102890 }
29112891 s = local_ptr.parent;
29122892 },
......@@ -2918,13 +2898,10 @@ fn identifier(
29182898 }
29192899
29202900 if (mod.lookupDeclName(scope, ident_name)) |decl| {
2921 if (rl == .ref) {
2922 return addZIRInst(mod, scope, src, zir.Inst.DeclRef, .{ .decl = decl }, .{});
2923 } else {
2924 return rvalue(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.DeclVal, .{
2925 .decl = decl,
2926 }, .{}));
2927 }
2901 return if (rl == .ref)
2902 gz.addDecl(.decl_ref, decl)
2903 else
2904 rvalue(mod, scope, rl, try gz.addDecl(.decl_val, decl), ident);
29282905 }
29292906
29302907 return mod.failNode(scope, ident, "use of undeclared identifier '{s}'", .{ident_name});
src/zir.zig+1
......@@ -564,6 +564,7 @@ pub const Inst = struct {
564564 intcast,
565565 /// Make an integer type out of signedness and bit count.
566566 /// lhs is signedness, rhs is bit count.
567 /// Payload is `Bin`
567568 int_type,
568569 /// Return a boolean false if an optional is null. `x != null`
569570 /// Uses the `un_tok` field.