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 {...@@ -1124,6 +1124,17 @@ pub const Scope = struct {
1124 });1124 });
1125 }1125 }
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
1127 pub fn addNode(1138 pub fn addNode(
1128 gz: *GenZir,1139 gz: *GenZir,
1129 tag: zir.Inst.Tag,1140 tag: zir.Inst.Tag,
src/astgen.zig+11-34
...@@ -2835,23 +2835,22 @@ fn identifier(...@@ -2835,23 +2835,22 @@ fn identifier(
2835 rl: ResultLoc,2835 rl: ResultLoc,
2836 ident: ast.Node.Index,2836 ident: ast.Node.Index,
2837) InnerError!zir.Inst.Ref {2837) InnerError!zir.Inst.Ref {
2838 if (true) @panic("TODO update for zir-memory-layout");
2839 const tracy = trace(@src());2838 const tracy = trace(@src());
2840 defer tracy.end();2839 defer tracy.end();
28412840
2842 const tree = scope.tree();2841 const tree = scope.tree();
2843 const main_tokens = tree.nodes.items(.main_token);2842 const main_tokens = tree.nodes.items(.main_token);
2844 const token_starts = tree.tokens.items(.start);2843
2844 const gz = scope.getGenZir();
28452845
2846 const ident_token = main_tokens[ident];2846 const ident_token = main_tokens[ident];
2847 const ident_name = try mod.identifierTokenString(scope, ident_token);2847 const ident_name = try mod.identifierTokenString(scope, ident_token);
2848 const src = token_starts[ident_token];
2849 if (mem.eql(u8, ident_name, "_")) {2848 if (mem.eql(u8, ident_name, "_")) {
2850 return mod.failNode(scope, ident, "TODO implement '_' identifier", .{});2849 return mod.failNode(scope, ident, "TODO implement '_' identifier", .{});
2851 }2850 }
28522851
2853 if (simple_types.get(ident_name)) |zir_const_tag| {2852 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);
2855 }2854 }
28562855
2857 if (ident_name.len >= 2) integer: {2856 if (ident_name.len >= 2) integer: {
...@@ -2867,26 +2866,7 @@ fn identifier(...@@ -2867,26 +2866,7 @@ fn identifier(
2867 ),2866 ),
2868 error.InvalidCharacter => break :integer,2867 error.InvalidCharacter => break :integer,
2869 };2868 };
2870 const val = switch (bit_count) {2869 return rvalue(mod, scope, rl, try gz.addBin(.int_type, @boolToInt(is_signed), bit_count), ident);
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);
2890 }2870 }
2891 }2871 }
28922872
...@@ -2897,7 +2877,7 @@ fn identifier(...@@ -2897,7 +2877,7 @@ fn identifier(
2897 .local_val => {2877 .local_val => {
2898 const local_val = s.cast(Scope.LocalVal).?;2878 const local_val = s.cast(Scope.LocalVal).?;
2899 if (mem.eql(u8, local_val.name, ident_name)) {2879 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);
2901 }2881 }
2902 s = local_val.parent;2882 s = local_val.parent;
2903 },2883 },
...@@ -2905,8 +2885,8 @@ fn identifier(...@@ -2905,8 +2885,8 @@ fn identifier(
2905 const local_ptr = s.cast(Scope.LocalPtr).?;2885 const local_ptr = s.cast(Scope.LocalPtr).?;
2906 if (mem.eql(u8, local_ptr.name, ident_name)) {2886 if (mem.eql(u8, local_ptr.name, ident_name)) {
2907 if (rl == .ref) return local_ptr.ptr;2887 if (rl == .ref) return local_ptr.ptr;
2908 const loaded = try addZIRUnOp(mod, scope, src, .deref, local_ptr.ptr);2888 const loaded = try gz.addUnNode(.deref_node, local_ptr.ptr, ident);
2909 return rvalue(mod, scope, rl, loaded);2889 return rvalue(mod, scope, rl, loaded, ident);
2910 }2890 }
2911 s = local_ptr.parent;2891 s = local_ptr.parent;
2912 },2892 },
...@@ -2918,13 +2898,10 @@ fn identifier(...@@ -2918,13 +2898,10 @@ fn identifier(
2918 }2898 }
29192899
2920 if (mod.lookupDeclName(scope, ident_name)) |decl| {2900 if (mod.lookupDeclName(scope, ident_name)) |decl| {
2921 if (rl == .ref) {2901 return if (rl == .ref)
2922 return addZIRInst(mod, scope, src, zir.Inst.DeclRef, .{ .decl = decl }, .{});2902 gz.addDecl(.decl_ref, decl)
2923 } else {2903 else
2924 return rvalue(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.DeclVal, .{2904 rvalue(mod, scope, rl, try gz.addDecl(.decl_val, decl), ident);
2925 .decl = decl,
2926 }, .{}));
2927 }
2928 }2905 }
29292906
2930 return mod.failNode(scope, ident, "use of undeclared identifier '{s}'", .{ident_name});2907 return mod.failNode(scope, ident, "use of undeclared identifier '{s}'", .{ident_name});
src/zir.zig+1
...@@ -564,6 +564,7 @@ pub const Inst = struct {...@@ -564,6 +564,7 @@ pub const Inst = struct {
564 intcast,564 intcast,
565 /// Make an integer type out of signedness and bit count.565 /// Make an integer type out of signedness and bit count.
566 /// lhs is signedness, rhs is bit count.566 /// lhs is signedness, rhs is bit count.
567 /// Payload is `Bin`
567 int_type,568 int_type,
568 /// Return a boolean false if an optional is null. `x != null`569 /// Return a boolean false if an optional is null. `x != null`
569 /// Uses the `un_tok` field.570 /// Uses the `un_tok` field.