authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-25 13:03:54-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-25 13:03:54-07:00
log399bb2e154395f1f2372eccb10ea41d6ba5ef68f
tree91ca58a20bb65c8a404b74a130f06ab1cacbfce0
parent31023de6c4b3957ef356be01b5454426844955a9

astgen: fix array access


3 files changed, 36 insertions(+), 39 deletions(-)

BRANCH_TODO+1
...@@ -20,6 +20,7 @@ Merge TODO list:...@@ -20,6 +20,7 @@ Merge TODO list:
2020
21Performance optimizations to look into:21Performance optimizations to look into:
22 * astgen: pass *GenZir as the first arg, not *Module22 * astgen: pass *GenZir as the first arg, not *Module
23 - point here is to avoid the unnecessary virtual call scope.getGenZir()
23 * don't store end index for blocks; rely on last instruction being noreturn24 * don't store end index for blocks; rely on last instruction being noreturn
24 * look into not storing the field name of field access as a string in zir25 * look into not storing the field name of field access as a string in zir
25 instructions. or, look into introducing interning to string_bytes (local26 instructions. or, look into introducing interning to string_bytes (local
src/astgen.zig+12-16
...@@ -1838,25 +1838,21 @@ fn arrayAccess(...@@ -1838,25 +1838,21 @@ fn arrayAccess(
1838 rl: ResultLoc,1838 rl: ResultLoc,
1839 node: ast.Node.Index,1839 node: ast.Node.Index,
1840) InnerError!zir.Inst.Ref {1840) InnerError!zir.Inst.Ref {
1841 if (true) @panic("TODO update for zir-memory-layout");1841 const gz = scope.getGenZir();
1842 const tree = scope.tree();1842 const tree = gz.tree();
1843 const main_tokens = tree.nodes.items(.main_token);1843 const main_tokens = tree.nodes.items(.main_token);
1844 const node_datas = tree.nodes.items(.data);1844 const node_datas = tree.nodes.items(.data);
1845
1846 const usize_type = try addZIRInstConst(mod, scope, src, .{
1847 .ty = Type.initTag(.type),
1848 .val = Value.initTag(.usize_type),
1849 });
1850 const index_rl: ResultLoc = .{ .ty = usize_type };
1851 switch (rl) {1845 switch (rl) {
1852 .ref => return addZirInstTag(mod, scope, src, .elem_ptr, .{1846 .ref => return gz.addBin(
1853 .array = try expr(mod, scope, .ref, node_datas[node].lhs),1847 .elem_ptr,
1854 .index = try expr(mod, scope, index_rl, node_datas[node].rhs),1848 try expr(mod, scope, .ref, node_datas[node].lhs),
1855 }),1849 try expr(mod, scope, .{ .ty = .usize_type }, node_datas[node].rhs),
1856 else => return rvalue(mod, scope, rl, try addZirInstTag(mod, scope, src, .elem_val, .{1850 ),
1857 .array = try expr(mod, scope, .none, node_datas[node].lhs),1851 else => return rvalue(mod, scope, rl, try gz.addBin(
1858 .index = try expr(mod, scope, index_rl, node_datas[node].rhs),1852 .elem_val,
1859 })),1853 try expr(mod, scope, .none, node_datas[node].lhs),
1854 try expr(mod, scope, .{ .ty = .usize_type }, node_datas[node].rhs),
1855 ), node),
1860 }1856 }
1861}1857}
18621858
test/stage2/test.zig+23-23
...@@ -917,29 +917,29 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -917,29 +917,29 @@ pub fn addCases(ctx: *TestContext) !void {
917 );917 );
918918
919 // Array access.919 // Array access.
920 //case.addCompareOutput(920 case.addCompareOutput(
921 // \\export fn _start() noreturn {921 \\export fn _start() noreturn {
922 // \\ assert("hello"[0] == 'h');922 \\ assert("hello"[0] == 'h');
923 // \\923 \\
924 // \\ exit();924 \\ exit();
925 // \\}925 \\}
926 // \\926 \\
927 // \\pub fn assert(ok: bool) void {927 \\pub fn assert(ok: bool) void {
928 // \\ if (!ok) unreachable; // assertion failure928 \\ if (!ok) unreachable; // assertion failure
929 // \\}929 \\}
930 // \\930 \\
931 // \\fn exit() noreturn {931 \\fn exit() noreturn {
932 // \\ asm volatile ("syscall"932 \\ asm volatile ("syscall"
933 // \\ :933 \\ :
934 // \\ : [number] "{rax}" (231),934 \\ : [number] "{rax}" (231),
935 // \\ [arg1] "{rdi}" (0)935 \\ [arg1] "{rdi}" (0)
936 // \\ : "rcx", "r11", "memory"936 \\ : "rcx", "r11", "memory"
937 // \\ );937 \\ );
938 // \\ unreachable;938 \\ unreachable;
939 // \\}939 \\}
940 //,940 ,
941 // "",941 "",
942 //);942 );
943943
944 // 64bit set stack944 // 64bit set stack
945 case.addCompareOutput(945 case.addCompareOutput(