From ab8a9a6605900fc0b064c321504ac4e7dc1ca6f4 Mon Sep 17 00:00:00 2001 From: Vexu Date: Wed, 19 Aug 2020 14:25:47 +0300 Subject: [PATCH] stage2: fix astgen of decl ref, add test for global consts --- src-self-hosted/astgen.zig | 14 ++++++++------ src-self-hosted/type.zig | 3 ++- src-self-hosted/zir_sema.zig | 3 +-- test/stage2/compare_output.zig | 31 +++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src-self-hosted/astgen.zig b/src-self-hosted/astgen.zig index 9d40c7899a..9f8afa6225 100644 --- a/src-self-hosted/astgen.zig +++ b/src-self-hosted/astgen.zig @@ -1223,9 +1223,10 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo } if (mod.lookupDeclName(scope, ident_name)) |decl| { - // TODO handle lvalues const result = try addZIRInst(mod, scope, src, zir.Inst.DeclValInModule, .{ .decl = decl }, .{}); - return rlWrap(mod, scope, rl, result); + if (rl == .lvalue or rl == .ref) + return result; + return rlWrap(mod, scope, rl, try addZIRUnOp(mod, scope, src, .deref, result)); } 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 // line lengths and new lines var len = lines.len - 1; for (lines) |line| { - len += tree.tokenSlice(line).len - 2; + // 2 for the '//' + 1 for '\n' + len += tree.tokenSlice(line).len - 3; } const bytes = try scope.arena().alloc(u8, len); @@ -1268,9 +1270,9 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr bytes[i] = '\n'; i += 1; } - const slice = tree.tokenSlice(line)[2..]; - mem.copy(u8, bytes[i..], slice); - i += slice.len; + const slice = tree.tokenSlice(line); + mem.copy(u8, bytes[i..], slice[2..slice.len - 1]); + i += slice.len - 3; } return addZIRInst(mod, scope, src, zir.Inst.Str, .{ .bytes = bytes }, .{}); diff --git a/src-self-hosted/type.zig b/src-self-hosted/type.zig index 82671609d0..c2762e1f8a 100644 --- a/src-self-hosted/type.zig +++ b/src-self-hosted/type.zig @@ -457,7 +457,8 @@ pub const Type = extern union { try param_type.format("", .{}, out_stream); } try out_stream.writeAll(") "); - try payload.return_type.format("", .{}, out_stream); + ty = payload.return_type; + continue; }, .array_u8 => { diff --git a/src-self-hosted/zir_sema.zig b/src-self-hosted/zir_sema.zig index 644b73d643..cb1ed08d6d 100644 --- a/src-self-hosted/zir_sema.zig +++ b/src-self-hosted/zir_sema.zig @@ -581,8 +581,7 @@ fn analyzeInstDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) Inne fn analyzeInstDeclValInModule(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclValInModule) InnerError!*Inst { const decl = inst.positionals.decl; - const ptr = try mod.analyzeDeclRef(scope, inst.base.src, decl); - return mod.analyzeDeref(scope, inst.base.src, ptr, inst.base.src); + return mod.analyzeDeclRef(scope, inst.base.src, decl); } fn analyzeInstCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst { diff --git a/test/stage2/compare_output.zig b/test/stage2/compare_output.zig index 3513605602..2b8c8e94cc 100644 --- a/test/stage2/compare_output.zig +++ b/test/stage2/compare_output.zig @@ -617,6 +617,37 @@ pub fn addCases(ctx: *TestContext) !void { , "", ); + + case.addCompareOutput( + \\export fn _start() noreturn { + \\ add(aa, bb); + \\ + \\ exit(); + \\} + \\ + \\const aa = 'ぁ'; + \\const bb = '\x03'; + \\ + \\fn add(a: u32, b: u32) void { + \\ assert(a + b == 12356); + \\} + \\ + \\pub fn assert(ok: bool) void { + \\ if (!ok) unreachable; // assertion failure + \\} + \\ + \\fn exit() noreturn { + \\ asm volatile ("syscall" + \\ : + \\ : [number] "{rax}" (231), + \\ [arg1] "{rdi}" (0) + \\ : "rcx", "r11", "memory" + \\ ); + \\ unreachable; + \\} + , + "", + ); } {