authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-19 14:25:47+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-19 16:12:29-07:00
logab8a9a6605900fc0b064c321504ac4e7dc1ca6f4
tree46b5cd9da18d8df31b60756eab2347de1b62896b
parent338a495648f07a2734e012034fd301a0e77f8202

stage2: fix astgen of decl ref, add test for global consts


4 files changed, 42 insertions(+), 9 deletions(-)

src-self-hosted/astgen.zig+8-6
...@@ -1223,9 +1223,10 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo...@@ -1223,9 +1223,10 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo
1223 }1223 }
12241224
1225 if (mod.lookupDeclName(scope, ident_name)) |decl| {1225 if (mod.lookupDeclName(scope, ident_name)) |decl| {
1226 // TODO handle lvalues
1227 const result = try addZIRInst(mod, scope, src, zir.Inst.DeclValInModule, .{ .decl = decl }, .{});1226 const result = try addZIRInst(mod, scope, src, zir.Inst.DeclValInModule, .{ .decl = decl }, .{});
1228 return rlWrap(mod, scope, rl, result);1227 if (rl == .lvalue or rl == .ref)
1228 return result;
1229 return rlWrap(mod, scope, rl, try addZIRUnOp(mod, scope, src, .deref, result));
1229 }1230 }
12301231
1231 return mod.failNode(scope, &ident.base, "use of undeclared identifier '{}'", .{ident_name});1232 return mod.failNode(scope, &ident.base, "use of undeclared identifier '{}'", .{ident_name});
...@@ -1258,7 +1259,8 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr...@@ -1258,7 +1259,8 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr
1258 // line lengths and new lines1259 // line lengths and new lines
1259 var len = lines.len - 1;1260 var len = lines.len - 1;
1260 for (lines) |line| {1261 for (lines) |line| {
1261 len += tree.tokenSlice(line).len - 2;1262 // 2 for the '//' + 1 for '\n'
1263 len += tree.tokenSlice(line).len - 3;
1262 }1264 }
12631265
1264 const bytes = try scope.arena().alloc(u8, len);1266 const bytes = try scope.arena().alloc(u8, len);
...@@ -1268,9 +1270,9 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr...@@ -1268,9 +1270,9 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr
1268 bytes[i] = '\n';1270 bytes[i] = '\n';
1269 i += 1;1271 i += 1;
1270 }1272 }
1271 const slice = tree.tokenSlice(line)[2..];1273 const slice = tree.tokenSlice(line);
1272 mem.copy(u8, bytes[i..], slice);1274 mem.copy(u8, bytes[i..], slice[2..slice.len - 1]);
1273 i += slice.len;1275 i += slice.len - 3;
1274 }1276 }
12751277
1276 return addZIRInst(mod, scope, src, zir.Inst.Str, .{ .bytes = bytes }, .{});1278 return addZIRInst(mod, scope, src, zir.Inst.Str, .{ .bytes = bytes }, .{});
src-self-hosted/type.zig+2-1
...@@ -457,7 +457,8 @@ pub const Type = extern union {...@@ -457,7 +457,8 @@ pub const Type = extern union {
457 try param_type.format("", .{}, out_stream);457 try param_type.format("", .{}, out_stream);
458 }458 }
459 try out_stream.writeAll(") ");459 try out_stream.writeAll(") ");
460 try payload.return_type.format("", .{}, out_stream);460 ty = payload.return_type;
461 continue;
461 },462 },
462463
463 .array_u8 => {464 .array_u8 => {
src-self-hosted/zir_sema.zig+1-2
...@@ -581,8 +581,7 @@ fn analyzeInstDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) Inne...@@ -581,8 +581,7 @@ fn analyzeInstDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) Inne
581581
582fn analyzeInstDeclValInModule(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclValInModule) InnerError!*Inst {582fn analyzeInstDeclValInModule(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclValInModule) InnerError!*Inst {
583 const decl = inst.positionals.decl;583 const decl = inst.positionals.decl;
584 const ptr = try mod.analyzeDeclRef(scope, inst.base.src, decl);584 return mod.analyzeDeclRef(scope, inst.base.src, decl);
585 return mod.analyzeDeref(scope, inst.base.src, ptr, inst.base.src);
586}585}
587586
588fn analyzeInstCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {587fn analyzeInstCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
test/stage2/compare_output.zig+31
...@@ -617,6 +617,37 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -617,6 +617,37 @@ pub fn addCases(ctx: *TestContext) !void {
617 ,617 ,
618 "",618 "",
619 );619 );
620
621 case.addCompareOutput(
622 \\export fn _start() noreturn {
623 \\ add(aa, bb);
624 \\
625 \\ exit();
626 \\}
627 \\
628 \\const aa = 'ぁ';
629 \\const bb = '\x03';
630 \\
631 \\fn add(a: u32, b: u32) void {
632 \\ assert(a + b == 12356);
633 \\}
634 \\
635 \\pub fn assert(ok: bool) void {
636 \\ if (!ok) unreachable; // assertion failure
637 \\}
638 \\
639 \\fn exit() noreturn {
640 \\ asm volatile ("syscall"
641 \\ :
642 \\ : [number] "{rax}" (231),
643 \\ [arg1] "{rdi}" (0)
644 \\ : "rcx", "r11", "memory"
645 \\ );
646 \\ unreachable;
647 \\}
648 ,
649 "",
650 );
620 }651 }
621652
622 {653 {