From 692f38c2509da047e8be15fd7a932aa204f33fd3 Mon Sep 17 00:00:00 2001 From: Noam Preil Date: Thu, 13 Aug 2020 12:23:37 -0400 Subject: [PATCH 1/4] astgen: minor cleanup --- src-self-hosted/astgen.zig | 96 ++++++++++++++------------------------ 1 file changed, 36 insertions(+), 60 deletions(-) diff --git a/src-self-hosted/astgen.zig b/src-self-hosted/astgen.zig index b5f439ec2706f7204d1e407f8e10272ac3985a31..850cc5719bd3a0f8650b5c90d326f5dc4b66f256 100644 --- a/src-self-hosted/astgen.zig +++ b/src-self-hosted/astgen.zig @@ -335,76 +335,52 @@ fn varDecl( // Depending on the type of AST the initialization expression is, we may need an lvalue // or an rvalue as a result location. If it is an rvalue, we can use the instruction as // the variable, no memory location needed. - if (nodeMayNeedMemoryLocation(init_node)) { + const result_loc = if (nodeMayNeedMemoryLocation(init_node)) r: { if (node.getTrailer("type_node")) |type_node| { const type_inst = try typeExpr(mod, scope, type_node); const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst); - const result_loc: ResultLoc = .{ .ptr = alloc }; - const init_inst = try expr(mod, scope, result_loc, init_node); - const sub_scope = try block_arena.create(Scope.LocalVal); - sub_scope.* = .{ - .parent = scope, - .gen_zir = scope.getGenZIR(), - .name = ident_name, - .inst = init_inst, - }; - return &sub_scope.base; + break :r ResultLoc{ .ptr = alloc }; } else { const alloc = try addZIRNoOpT(mod, scope, name_src, .alloc_inferred); - const result_loc: ResultLoc = .{ .inferred_ptr = alloc }; - const init_inst = try expr(mod, scope, result_loc, init_node); - const sub_scope = try block_arena.create(Scope.LocalVal); - sub_scope.* = .{ - .parent = scope, - .gen_zir = scope.getGenZIR(), - .name = ident_name, - .inst = init_inst, - }; - return &sub_scope.base; + break :r ResultLoc{ .inferred_ptr = alloc }; } - } else { - const result_loc: ResultLoc = if (node.getTrailer("type_node")) |type_node| - .{ .ty = try typeExpr(mod, scope, type_node) } + } else r: { + if (node.getTrailer("type_node")) |type_node| + break :r ResultLoc{ .ty = try typeExpr(mod, scope, type_node) } else - .none; - const init_inst = try expr(mod, scope, result_loc, init_node); - const sub_scope = try block_arena.create(Scope.LocalVal); - sub_scope.* = .{ - .parent = scope, - .gen_zir = scope.getGenZIR(), - .name = ident_name, - .inst = init_inst, - }; - return &sub_scope.base; - } + break :r .none; + }; + const init_inst = try expr(mod, scope, result_loc, init_node); + const sub_scope = try block_arena.create(Scope.LocalVal); + sub_scope.* = .{ + .parent = scope, + .gen_zir = scope.getGenZIR(), + .name = ident_name, + .inst = init_inst, + }; + return &sub_scope.base; }, .Keyword_var => { - if (node.getTrailer("type_node")) |type_node| { + const alloc = if (node.getTrailer("type_node")) |type_node| a: { const type_inst = try typeExpr(mod, scope, type_node); - const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst); - const result_loc: ResultLoc = .{ .ptr = alloc }; - const init_inst = try expr(mod, scope, result_loc, init_node); - const sub_scope = try block_arena.create(Scope.LocalPtr); - sub_scope.* = .{ - .parent = scope, - .gen_zir = scope.getGenZIR(), - .name = ident_name, - .ptr = alloc, - }; - return &sub_scope.base; - } else { - const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred); - const result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred).? }; - const init_inst = try expr(mod, scope, result_loc, init_node); - const sub_scope = try block_arena.create(Scope.LocalPtr); - sub_scope.* = .{ - .parent = scope, - .gen_zir = scope.getGenZIR(), - .name = ident_name, - .ptr = alloc, - }; - return &sub_scope.base; - } + break :a try addZIRUnOp(mod, scope, name_src, .alloc, type_inst); + } else try addZIRNoOp(mod, scope, name_src, .alloc_inferred); + const result_loc = r: { + if (node.getTrailer("type_node")) |type_node| { + break :r ResultLoc{ .ptr = alloc }; + } else { + break :r ResultLoc{ .inferred_ptr = alloc.castTag(.alloc_inferred).? }; + } + }; + const init_inst = try expr(mod, scope, result_loc, init_node); + const sub_scope = try block_arena.create(Scope.LocalPtr); + sub_scope.* = .{ + .parent = scope, + .gen_zir = scope.getGenZIR(), + .name = ident_name, + .ptr = alloc, + }; + return &sub_scope.base; }, else => unreachable, } -- 2.54.0 From 34923e071e6f9f46ab8abb645929b975ef9ba7ff Mon Sep 17 00:00:00 2001 From: Noam Preil Date: Thu, 13 Aug 2020 12:25:42 -0400 Subject: [PATCH 2/4] CBE: minor doc change --- src-self-hosted/codegen/c.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src-self-hosted/codegen/c.zig b/src-self-hosted/codegen/c.zig index f0d3d8367a1d87133d9dded40b503fc732f6d7ec..9a7a4888bed1a4576f5f239b1c9fe07fecb0636e 100644 --- a/src-self-hosted/codegen/c.zig +++ b/src-self-hosted/codegen/c.zig @@ -11,8 +11,8 @@ const C = link.File.C; const Decl = Module.Decl; const mem = std.mem; -/// Maps a name from Zig source to C. This will always give the same output for -/// any given input. +/// Maps a name from Zig source to C. Currently, this will always give the same +/// output for any given input, sometimes resulting in broken identifiers. fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 { return allocator.dupe(u8, name); } -- 2.54.0 From 93619a5e4e9ad0e29e521c2c30fc1e39f47a6ba8 Mon Sep 17 00:00:00 2001 From: Noam Preil Date: Thu, 13 Aug 2020 12:35:17 -0400 Subject: [PATCH 3/4] Module: panic when encountering unimplemented node --- src-self-hosted/Module.zig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src-self-hosted/Module.zig b/src-self-hosted/Module.zig index 2fad7e7a0063a587fa075e877282f4a719efc243..6272ef6d98a2f6b48a5f3cb47a4a07fea3fde7e1 100644 --- a/src-self-hosted/Module.zig +++ b/src-self-hosted/Module.zig @@ -1580,6 +1580,8 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void { } } } + } else { + std.debug.panic("TODO: analyzeRootSrcFile {}", .{src_decl.tag}); } // TODO also look for global variable declarations // TODO also look for comptime blocks and exported globals -- 2.54.0 From 3ca8c42e7ab04886f536a7bab34c9ea563c62711 Mon Sep 17 00:00:00 2001 From: Noam Preil Date: Sun, 16 Aug 2020 20:36:33 -0400 Subject: [PATCH 4/4] Astgen: further cleanup --- src-self-hosted/astgen.zig | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src-self-hosted/astgen.zig b/src-self-hosted/astgen.zig index 850cc5719bd3a0f8650b5c90d326f5dc4b66f256..a88a178bb369f2c93522be1c4ead61ee1669059c 100644 --- a/src-self-hosted/astgen.zig +++ b/src-self-hosted/astgen.zig @@ -361,24 +361,21 @@ fn varDecl( return &sub_scope.base; }, .Keyword_var => { - const alloc = if (node.getTrailer("type_node")) |type_node| a: { + const var_data: struct { result_loc: ResultLoc, alloc: *zir.Inst } = if (node.getTrailer("type_node")) |type_node| a: { const type_inst = try typeExpr(mod, scope, type_node); - break :a try addZIRUnOp(mod, scope, name_src, .alloc, type_inst); - } else try addZIRNoOp(mod, scope, name_src, .alloc_inferred); - const result_loc = r: { - if (node.getTrailer("type_node")) |type_node| { - break :r ResultLoc{ .ptr = alloc }; - } else { - break :r ResultLoc{ .inferred_ptr = alloc.castTag(.alloc_inferred).? }; - } + const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst); + break :a .{ .alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst), .result_loc = .{ .ptr = alloc } }; + } else a: { + const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred); + break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred).? } }; }; - const init_inst = try expr(mod, scope, result_loc, init_node); + const init_inst = try expr(mod, scope, var_data.result_loc, init_node); const sub_scope = try block_arena.create(Scope.LocalPtr); sub_scope.* = .{ .parent = scope, .gen_zir = scope.getGenZIR(), .name = ident_name, - .ptr = alloc, + .ptr = var_data.alloc, }; return &sub_scope.base; }, -- 2.54.0