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:
2020
2121Performance optimizations to look into:
2222 * astgen: pass *GenZir as the first arg, not *Module
23 - point here is to avoid the unnecessary virtual call scope.getGenZir()
2324 * don't store end index for blocks; rely on last instruction being noreturn
2425 * look into not storing the field name of field access as a string in zir
2526 instructions. or, look into introducing interning to string_bytes (local
src/astgen.zig+12-16
......@@ -1838,25 +1838,21 @@ fn arrayAccess(
18381838 rl: ResultLoc,
18391839 node: ast.Node.Index,
18401840) InnerError!zir.Inst.Ref {
1841 if (true) @panic("TODO update for zir-memory-layout");
1842 const tree = scope.tree();
1841 const gz = scope.getGenZir();
1842 const tree = gz.tree();
18431843 const main_tokens = tree.nodes.items(.main_token);
18441844 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 };
18511845 switch (rl) {
1852 .ref => return addZirInstTag(mod, scope, src, .elem_ptr, .{
1853 .array = try expr(mod, scope, .ref, node_datas[node].lhs),
1854 .index = try expr(mod, scope, index_rl, node_datas[node].rhs),
1855 }),
1856 else => return rvalue(mod, scope, rl, try addZirInstTag(mod, scope, src, .elem_val, .{
1857 .array = try expr(mod, scope, .none, node_datas[node].lhs),
1858 .index = try expr(mod, scope, index_rl, node_datas[node].rhs),
1859 })),
1846 .ref => return gz.addBin(
1847 .elem_ptr,
1848 try expr(mod, scope, .ref, node_datas[node].lhs),
1849 try expr(mod, scope, .{ .ty = .usize_type }, node_datas[node].rhs),
1850 ),
1851 else => return rvalue(mod, scope, rl, try gz.addBin(
1852 .elem_val,
1853 try expr(mod, scope, .none, node_datas[node].lhs),
1854 try expr(mod, scope, .{ .ty = .usize_type }, node_datas[node].rhs),
1855 ), node),
18601856 }
18611857}
18621858
test/stage2/test.zig+23-23
......@@ -917,29 +917,29 @@ pub fn addCases(ctx: *TestContext) !void {
917917 );
918918
919919 // Array access.
920 //case.addCompareOutput(
921 // \\export fn _start() noreturn {
922 // \\ assert("hello"[0] == 'h');
923 // \\
924 // \\ exit();
925 // \\}
926 // \\
927 // \\pub fn assert(ok: bool) void {
928 // \\ if (!ok) unreachable; // assertion failure
929 // \\}
930 // \\
931 // \\fn exit() noreturn {
932 // \\ asm volatile ("syscall"
933 // \\ :
934 // \\ : [number] "{rax}" (231),
935 // \\ [arg1] "{rdi}" (0)
936 // \\ : "rcx", "r11", "memory"
937 // \\ );
938 // \\ unreachable;
939 // \\}
940 //,
941 // "",
942 //);
920 case.addCompareOutput(
921 \\export fn _start() noreturn {
922 \\ assert("hello"[0] == 'h');
923 \\
924 \\ exit();
925 \\}
926 \\
927 \\pub fn assert(ok: bool) void {
928 \\ if (!ok) unreachable; // assertion failure
929 \\}
930 \\
931 \\fn exit() noreturn {
932 \\ asm volatile ("syscall"
933 \\ :
934 \\ : [number] "{rax}" (231),
935 \\ [arg1] "{rdi}" (0)
936 \\ : "rcx", "r11", "memory"
937 \\ );
938 \\ unreachable;
939 \\}
940 ,
941 "",
942 );
943943
944944 // 64bit set stack
945945 case.addCompareOutput(