authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-24 03:46:32-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-24 03:46:32-04:00
log14aa08fcd3ca6ef32fff9422969cb684cb81b9d7
tree8a3a4048f16c81a31adb29d3b6eeab93036c5a7d
parentb1b7708cc8380ad9518715e3e285db3011f5a6a4

self-hosted: restore ZIR functionality


4 files changed, 211 insertions(+), 216 deletions(-)

src-self-hosted/Module.zig+158-136
...@@ -1060,21 +1060,24 @@ fn ensureDeclAnalyzed(self: *Module, decl: *Decl) InnerError!void {...@@ -1060,21 +1060,24 @@ fn ensureDeclAnalyzed(self: *Module, decl: *Decl) InnerError!void {
1060 .unreferenced => false,1060 .unreferenced => false,
1061 };1061 };
10621062
1063 const type_changed = self.astGenAndAnalyzeDecl(decl) catch |err| switch (err) {1063 const type_changed = if (self.root_scope.cast(Scope.ZIRModule)) |zir_module|
1064 error.OutOfMemory => return error.OutOfMemory,1064 try self.analyzeZirDecl(decl, zir_module.contents.module.decls[decl.src_index])
1065 error.AnalysisFail => return error.AnalysisFail,1065 else
1066 else => {1066 self.astGenAndAnalyzeDecl(decl) catch |err| switch (err) {
1067 try self.failed_decls.ensureCapacity(self.failed_decls.size + 1);1067 error.OutOfMemory => return error.OutOfMemory,
1068 self.failed_decls.putAssumeCapacityNoClobber(decl, try ErrorMsg.create(1068 error.AnalysisFail => return error.AnalysisFail,
1069 self.allocator,1069 else => {
1070 decl.src(),1070 try self.failed_decls.ensureCapacity(self.failed_decls.size + 1);
1071 "unable to analyze: {}",1071 self.failed_decls.putAssumeCapacityNoClobber(decl, try ErrorMsg.create(
1072 .{@errorName(err)},1072 self.allocator,
1073 ));1073 decl.src(),
1074 decl.analysis = .sema_failure_retryable;1074 "unable to analyze: {}",
1075 return error.AnalysisFail;1075 .{@errorName(err)},
1076 },1076 ));
1077 };1077 decl.analysis = .sema_failure_retryable;
1078 return error.AnalysisFail;
1079 },
1080 };
10781081
1079 if (subsequent_analysis) {1082 if (subsequent_analysis) {
1080 // We may need to chase the dependants and re-analyze them.1083 // We may need to chase the dependants and re-analyze them.
...@@ -1724,71 +1727,63 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {...@@ -1724,71 +1727,63 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
1724}1727}
17251728
1726fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {1729fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {
1727 switch (root_scope.status) {1730 // We may be analyzing it for the first time, or this may be
1728 .never_loaded => {1731 // an incremental update. This code handles both cases.
1729 const src_module = try self.getSrcModule(root_scope);1732 const src_module = try self.getSrcModule(root_scope);
17301733
1731 // Here we ensure enough queue capacity to store all the decls, so that later we can use1734 try self.work_queue.ensureUnusedCapacity(src_module.decls.len);
1732 // appendAssumeCapacity.1735 try root_scope.decls.ensureCapacity(self.allocator, src_module.decls.len);
1733 try self.work_queue.ensureUnusedCapacity(src_module.decls.len);
17341736
1735 for (src_module.decls) |src_decl| {1737 var exports_to_resolve = std.ArrayList(*zir.Decl).init(self.allocator);
1736 if (src_decl.inst.cast(zir.Inst.Export)) |export_inst| {1738 defer exports_to_resolve.deinit();
1737 _ = try self.resolveDecl(&root_scope.base, src_decl);
1738 }
1739 }
1740 },
17411739
1742 .unloaded_parse_failure,1740 // Keep track of the decls that we expect to see in this file so that
1743 .unloaded_sema_failure,1741 // we know which ones have been deleted.
1744 .unloaded_success,1742 var deleted_decls = std.AutoHashMap(*Decl, void).init(self.allocator);
1745 .loaded_sema_failure,1743 defer deleted_decls.deinit();
1746 .loaded_success,1744 try deleted_decls.ensureCapacity(self.decl_table.size);
1747 => {1745 {
1748 const src_module = try self.getSrcModule(root_scope);1746 var it = self.decl_table.iterator();
17491747 while (it.next()) |kv| {
1750 var exports_to_resolve = std.ArrayList(*zir.Decl).init(self.allocator);1748 deleted_decls.putAssumeCapacityNoClobber(kv.value, {});
1751 defer exports_to_resolve.deinit();1749 }
17521750 }
1753 // Keep track of the decls that we expect to see in this file so that
1754 // we know which ones have been deleted.
1755 var deleted_decls = std.AutoHashMap(*Decl, void).init(self.allocator);
1756 defer deleted_decls.deinit();
1757 try deleted_decls.ensureCapacity(self.decl_table.size);
1758 {
1759 var it = self.decl_table.iterator();
1760 while (it.next()) |kv| {
1761 deleted_decls.putAssumeCapacityNoClobber(kv.value, {});
1762 }
1763 }
17641751
1765 for (src_module.decls) |src_decl| {1752 for (src_module.decls) |src_decl, decl_i| {
1766 const name_hash = root_scope.fullyQualifiedNameHash(src_decl.name);1753 const name_hash = root_scope.fullyQualifiedNameHash(src_decl.name);
1767 if (self.decl_table.get(name_hash)) |kv| {1754 if (self.decl_table.get(name_hash)) |kv| {
1768 const decl = kv.value;1755 const decl = kv.value;
1769 deleted_decls.removeAssertDiscard(decl);1756 deleted_decls.removeAssertDiscard(decl);
1770 //std.debug.warn("'{}' contents: '{}'\n", .{ src_decl.name, src_decl.contents });1757 //std.debug.warn("'{}' contents: '{}'\n", .{ src_decl.name, src_decl.contents });
1771 if (!srcHashEql(src_decl.contents_hash, decl.contents_hash)) {1758 if (!srcHashEql(src_decl.contents_hash, decl.contents_hash)) {
1772 try self.markOutdatedDecl(decl);1759 try self.markOutdatedDecl(decl);
1773 decl.contents_hash = src_decl.contents_hash;1760 decl.contents_hash = src_decl.contents_hash;
1774 }
1775 } else if (src_decl.inst.cast(zir.Inst.Export)) |export_inst| {
1776 try exports_to_resolve.append(src_decl);
1777 }
1778 }1761 }
1779 {1762 } else {
1780 // Handle explicitly deleted decls from the source code. Not to be confused1763 const new_decl = try self.createNewDecl(
1781 // with when we delete decls because they are no longer referenced.1764 &root_scope.base,
1782 var it = deleted_decls.iterator();1765 src_decl.name,
1783 while (it.next()) |kv| {1766 decl_i,
1784 //std.debug.warn("noticed '{}' deleted from source\n", .{kv.key.name});1767 name_hash,
1785 try self.deleteDecl(kv.key);1768 src_decl.contents_hash,
1786 }1769 );
1787 }1770 root_scope.decls.appendAssumeCapacity(new_decl);
1788 for (exports_to_resolve.items) |export_decl| {1771 if (src_decl.inst.cast(zir.Inst.Export)) |export_inst| {
1789 _ = try self.resolveDecl(&root_scope.base, export_decl);1772 try exports_to_resolve.append(src_decl);
1790 }1773 }
1791 },1774 }
1775 }
1776 {
1777 // Handle explicitly deleted decls from the source code. Not to be confused
1778 // with when we delete decls because they are no longer referenced.
1779 var it = deleted_decls.iterator();
1780 while (it.next()) |kv| {
1781 //std.debug.warn("noticed '{}' deleted from source\n", .{kv.key.name});
1782 try self.deleteDecl(kv.key);
1783 }
1784 }
1785 for (exports_to_resolve.items) |export_decl| {
1786 _ = try self.resolveZirDecl(&root_scope.base, export_decl);
1792 }1787 }
1793}1788}
17941789
...@@ -1933,73 +1928,67 @@ fn createNewDecl(...@@ -1933,73 +1928,67 @@ fn createNewDecl(
1933 return new_decl;1928 return new_decl;
1934}1929}
19351930
1936fn analyzeNewDecl(self: *Module, new_decl: *Decl, src_decl: *zir.Decl) InnerError!void {1931fn analyzeZirDecl(self: *Module, decl: *Decl, src_decl: *zir.Decl) InnerError!bool {
1937 var decl_scope: Scope.DeclAnalysis = .{1932 var decl_scope: Scope.DeclAnalysis = .{
1938 .decl = new_decl,1933 .decl = decl,
1939 .arena = std.heap.ArenaAllocator.init(self.allocator),1934 .arena = std.heap.ArenaAllocator.init(self.allocator),
1940 };1935 };
1941 errdefer decl_scope.arena.deinit();1936 errdefer decl_scope.arena.deinit();
19421937
1943 new_decl.analysis = .in_progress;1938 decl.analysis = .in_progress;
19441939
1945 const typed_value = self.analyzeConstInst(&decl_scope.base, src_decl.inst) catch |err| switch (err) {1940 const typed_value = try self.analyzeConstInst(&decl_scope.base, src_decl.inst);
1946 error.OutOfMemory => return error.OutOfMemory,
1947 error.AnalysisFail => {
1948 switch (new_decl.analysis) {
1949 .in_progress => new_decl.analysis = .dependency_failure,
1950 else => {},
1951 }
1952 new_decl.generation = self.generation;
1953 return error.AnalysisFail;
1954 },
1955 };
1956 const arena_state = try decl_scope.arena.allocator.create(std.heap.ArenaAllocator.State);1941 const arena_state = try decl_scope.arena.allocator.create(std.heap.ArenaAllocator.State);
19571942
1958 arena_state.* = decl_scope.arena.state;1943 var prev_type_has_bits = false;
1944 var type_changed = true;
19591945
1960 new_decl.typed_value = .{1946 if (decl.typedValueManaged()) |tvm| {
1947 prev_type_has_bits = tvm.typed_value.ty.hasCodeGenBits();
1948 type_changed = !tvm.typed_value.ty.eql(typed_value.ty);
1949
1950 tvm.deinit(self.allocator);
1951 }
1952
1953 arena_state.* = decl_scope.arena.state;
1954 decl.typed_value = .{
1961 .most_recent = .{1955 .most_recent = .{
1962 .typed_value = typed_value,1956 .typed_value = typed_value,
1963 .arena = arena_state,1957 .arena = arena_state,
1964 },1958 },
1965 };1959 };
1966 new_decl.analysis = .complete;1960 decl.analysis = .complete;
1967 new_decl.generation = self.generation;1961 decl.generation = self.generation;
1968 if (typed_value.ty.hasCodeGenBits()) {1962 if (typed_value.ty.hasCodeGenBits()) {
1969 // We don't fully codegen the decl until later, but we do need to reserve a global1963 // We don't fully codegen the decl until later, but we do need to reserve a global
1970 // offset table index for it. This allows us to codegen decls out of dependency order,1964 // offset table index for it. This allows us to codegen decls out of dependency order,
1971 // increasing how many computations can be done in parallel.1965 // increasing how many computations can be done in parallel.
1972 try self.bin_file.allocateDeclIndexes(new_decl);1966 try self.bin_file.allocateDeclIndexes(decl);
1973 try self.work_queue.writeItem(.{ .codegen_decl = new_decl });1967 try self.work_queue.writeItem(.{ .codegen_decl = decl });
1968 } else if (prev_type_has_bits) {
1969 self.bin_file.freeDecl(decl);
1974 }1970 }
1971
1972 return type_changed;
1975}1973}
19761974
1977fn resolveDecl(self: *Module, scope: *Scope, src_decl: *zir.Decl) InnerError!*Decl {1975fn resolveZirDecl(self: *Module, scope: *Scope, src_decl: *zir.Decl) InnerError!*Decl {
1978 // If the name is empty, then we make this an anonymous Decl.1976 const zir_module = self.root_scope.cast(Scope.ZIRModule).?;
1979 const scope_decl = scope.decl().?;1977 const entry = zir_module.contents.module.findDecl(src_decl.name).?;
1980 const new_decl = try self.allocateNewDecl(scope, scope_decl.src_index, src_decl.contents_hash);1978 return self.resolveZirDeclHavingIndex(scope, src_decl, entry.index);
1981 try self.analyzeNewDecl(new_decl, src_decl);1979}
1982 return new_decl;1980
1983 //const name_hash = Decl.hashSimpleName(src_decl.name);1981fn resolveZirDeclHavingIndex(self: *Module, scope: *Scope, src_decl: *zir.Decl, src_index: usize) InnerError!*Decl {
1984 //if (self.decl_table.get(name_hash)) |kv| {1982 const name_hash = scope.namespace().fullyQualifiedNameHash(src_decl.name);
1985 // const decl = kv.value;1983 const decl = self.decl_table.getValue(name_hash).?;
1986 // decl.src = src_decl.src;1984 decl.src_index = src_index;
1987 // try self.reAnalyzeDecl(decl, src_decl);1985 try self.ensureDeclAnalyzed(decl);
1988 // return decl;1986 return decl;
1989 //} else if (src_decl.cast(zir.Inst.DeclVal)) |decl_val| {
1990 // // This is just a named reference to another decl.
1991 // return self.analyzeDeclVal(scope, decl_val);
1992 //} else {
1993 // const new_decl = try self.createNewDecl(scope, src_decl.name, src_decl.src, name_hash, src_decl.contents_hash);
1994 // try self.analyzeNewDecl(new_decl, src_decl);
1995
1996 // return new_decl;
1997 //}
1998}1987}
19991988
2000/// Declares a dependency on the decl.1989/// Declares a dependency on the decl.
2001fn resolveCompleteDecl(self: *Module, scope: *Scope, src_decl: *zir.Decl) InnerError!*Decl {1990fn resolveCompleteZirDecl(self: *Module, scope: *Scope, src_decl: *zir.Decl) InnerError!*Decl {
2002 const decl = try self.resolveDecl(scope, src_decl);1991 const decl = try self.resolveZirDecl(scope, src_decl);
2003 switch (decl.analysis) {1992 switch (decl.analysis) {
2004 .unreferenced => unreachable,1993 .unreferenced => unreachable,
2005 .in_progress => unreachable,1994 .in_progress => unreachable,
...@@ -2014,15 +2003,32 @@ fn resolveCompleteDecl(self: *Module, scope: *Scope, src_decl: *zir.Decl) InnerE...@@ -2014,15 +2003,32 @@ fn resolveCompleteDecl(self: *Module, scope: *Scope, src_decl: *zir.Decl) InnerE
20142003
2015 .complete => {},2004 .complete => {},
2016 }2005 }
2017 if (scope.decl()) |scope_decl| {
2018 try self.declareDeclDependency(scope_decl, decl);
2019 }
2020 return decl;2006 return decl;
2021}2007}
20222008
2023/// TODO look into removing this function2009/// TODO Look into removing this function. The body is only needed for .zir files, not .zig files.
2024fn resolveInst(self: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*Inst {2010fn resolveInst(self: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*Inst {
2025 return old_inst.analyzed_inst;2011 if (old_inst.analyzed_inst) |inst| return inst;
2012
2013 // If this assert trips, the instruction that was referenced did not get properly
2014 // analyzed before it was referenced.
2015 const zir_module = scope.namespace().cast(Scope.ZIRModule).?;
2016 const entry = if (old_inst.cast(zir.Inst.DeclVal)) |declval| blk: {
2017 const decl_name = declval.positionals.name;
2018 const entry = zir_module.contents.module.findDecl(decl_name) orelse
2019 return self.fail(scope, old_inst.src, "decl '{}' not found", .{decl_name});
2020 break :blk entry;
2021 } else blk: {
2022 // If this assert trips, the instruction that was referenced did not get
2023 // properly analyzed by a previous instruction analysis before it was
2024 // referenced by the current one.
2025 break :blk zir_module.contents.module.findInstDecl(old_inst).?;
2026 };
2027 const decl = try self.resolveCompleteZirDecl(scope, entry.decl);
2028 const decl_ref = try self.analyzeDeclRef(scope, old_inst.src, decl);
2029 const result = try self.analyzeDeref(scope, old_inst.src, decl_ref, old_inst.src);
2030 old_inst.analyzed_inst = result;
2031 return result;
2026}2032}
20272033
2028fn requireRuntimeBlock(self: *Module, scope: *Scope, src: usize) !*Scope.Block {2034fn requireRuntimeBlock(self: *Module, scope: *Scope, src: usize) !*Scope.Block {
...@@ -2071,6 +2077,7 @@ fn resolveType(self: *Module, scope: *Scope, old_inst: *zir.Inst) !Type {...@@ -2071,6 +2077,7 @@ fn resolveType(self: *Module, scope: *Scope, old_inst: *zir.Inst) !Type {
2071}2077}
20722078
2073fn analyzeExport(self: *Module, scope: *Scope, src: usize, symbol_name: []const u8, exported_decl: *Decl) !void {2079fn analyzeExport(self: *Module, scope: *Scope, src: usize, symbol_name: []const u8, exported_decl: *Decl) !void {
2080 try self.ensureDeclAnalyzed(exported_decl);
2074 const typed_value = exported_decl.typed_value.most_recent.typed_value;2081 const typed_value = exported_decl.typed_value.most_recent.typed_value;
2075 switch (typed_value.ty.zigTypeTag()) {2082 switch (typed_value.ty.zigTypeTag()) {
2076 .Fn => {},2083 .Fn => {},
...@@ -2439,7 +2446,7 @@ fn analyzeDeclVal(self: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) InnerEr...@@ -2439,7 +2446,7 @@ fn analyzeDeclVal(self: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) InnerEr
2439 const src_decl = zir_module.contents.module.findDecl(decl_name) orelse2446 const src_decl = zir_module.contents.module.findDecl(decl_name) orelse
2440 return self.fail(scope, inst.base.src, "use of undeclared identifier '{}'", .{decl_name});2447 return self.fail(scope, inst.base.src, "use of undeclared identifier '{}'", .{decl_name});
24412448
2442 const decl = try self.resolveCompleteDecl(scope, src_decl.decl);2449 const decl = try self.resolveCompleteZirDecl(scope, src_decl.decl);
24432450
2444 return decl;2451 return decl;
2445}2452}
...@@ -2555,19 +2562,31 @@ fn analyzeInstCall(self: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerErro...@@ -2555,19 +2562,31 @@ fn analyzeInstCall(self: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerErro
2555}2562}
25562563
2557fn analyzeInstFn(self: *Module, scope: *Scope, fn_inst: *zir.Inst.Fn) InnerError!*Inst {2564fn analyzeInstFn(self: *Module, scope: *Scope, fn_inst: *zir.Inst.Fn) InnerError!*Inst {
2558 return self.fail(scope, fn_inst.base.src, "TODO implement ZIR fn inst", .{});2565 const fn_type = try self.resolveType(scope, fn_inst.positionals.fn_type);
2559 //const fn_type = try self.resolveType(scope, fn_inst.positionals.fn_type);2566 const fn_zir = blk: {
2560 //const new_func = try scope.arena().create(Fn);2567 var fn_arena = std.heap.ArenaAllocator.init(self.allocator);
2561 //new_func.* = .{2568 errdefer fn_arena.deinit();
2562 // .analysis = .{ .queued = fn_inst },2569
2563 // .owner_decl = scope.decl().?,2570 const fn_zir = try scope.arena().create(Fn.ZIR);
2564 //};2571 fn_zir.* = .{
2565 //const fn_payload = try scope.arena().create(Value.Payload.Function);2572 .body = .{
2566 //fn_payload.* = .{ .func = new_func };2573 .instructions = fn_inst.positionals.body.instructions,
2567 //return self.constInst(scope, fn_inst.base.src, .{2574 },
2568 // .ty = fn_type,2575 .arena = fn_arena.state,
2569 // .val = Value.initPayload(&fn_payload.base),2576 };
2570 //});2577 break :blk fn_zir;
2578 };
2579 const new_func = try scope.arena().create(Fn);
2580 new_func.* = .{
2581 .analysis = .{ .queued = fn_zir },
2582 .owner_decl = scope.decl().?,
2583 };
2584 const fn_payload = try scope.arena().create(Value.Payload.Function);
2585 fn_payload.* = .{ .func = new_func };
2586 return self.constInst(scope, fn_inst.base.src, .{
2587 .ty = fn_type,
2588 .val = Value.initPayload(&fn_payload.base),
2589 });
2571}2590}
25722591
2573fn analyzeInstFnType(self: *Module, scope: *Scope, fntype: *zir.Inst.FnType) InnerError!*Inst {2592fn analyzeInstFnType(self: *Module, scope: *Scope, fntype: *zir.Inst.FnType) InnerError!*Inst {
...@@ -3277,6 +3296,7 @@ fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *Err...@@ -3277,6 +3296,7 @@ fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *Err
3277 .decl => {3296 .decl => {
3278 const decl = scope.cast(Scope.DeclAnalysis).?.decl;3297 const decl = scope.cast(Scope.DeclAnalysis).?.decl;
3279 decl.analysis = .sema_failure;3298 decl.analysis = .sema_failure;
3299 decl.generation = self.generation;
3280 self.failed_decls.putAssumeCapacityNoClobber(decl, err_msg);3300 self.failed_decls.putAssumeCapacityNoClobber(decl, err_msg);
3281 },3301 },
3282 .block => {3302 .block => {
...@@ -3285,12 +3305,14 @@ fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *Err...@@ -3285,12 +3305,14 @@ fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *Err
3285 func.analysis = .sema_failure;3305 func.analysis = .sema_failure;
3286 } else {3306 } else {
3287 block.decl.analysis = .sema_failure;3307 block.decl.analysis = .sema_failure;
3308 block.decl.generation = self.generation;
3288 }3309 }
3289 self.failed_decls.putAssumeCapacityNoClobber(block.decl, err_msg);3310 self.failed_decls.putAssumeCapacityNoClobber(block.decl, err_msg);
3290 },3311 },
3291 .gen_zir => {3312 .gen_zir => {
3292 const gen_zir = scope.cast(Scope.GenZIR).?;3313 const gen_zir = scope.cast(Scope.GenZIR).?;
3293 gen_zir.decl.analysis = .sema_failure;3314 gen_zir.decl.analysis = .sema_failure;
3315 gen_zir.decl.generation = self.generation;
3294 self.failed_decls.putAssumeCapacityNoClobber(gen_zir.decl, err_msg);3316 self.failed_decls.putAssumeCapacityNoClobber(gen_zir.decl, err_msg);
3295 },3317 },
3296 .zir_module => {3318 .zir_module => {
src-self-hosted/main.zig+1-1
...@@ -504,7 +504,7 @@ fn updateModule(gpa: *Allocator, module: *Module, zir_out_path: ?[]const u8) !vo...@@ -504,7 +504,7 @@ fn updateModule(gpa: *Allocator, module: *Module, zir_out_path: ?[]const u8) !vo
504 });504 });
505 }505 }
506 } else {506 } else {
507 std.debug.print("Update completed in {} ms\n", .{update_nanos / std.time.ns_per_ms});507 std.log.info(.compiler, "Update completed in {} ms\n", .{update_nanos / std.time.ns_per_ms});
508 }508 }
509509
510 if (zir_out_path) |zop| {510 if (zir_out_path) |zop| {
src-self-hosted/zir.zig+13-1
...@@ -30,7 +30,7 @@ pub const Inst = struct {...@@ -30,7 +30,7 @@ pub const Inst = struct {
30 /// Byte offset into the source.30 /// Byte offset into the source.
31 src: usize,31 src: usize,
32 /// Pre-allocated field for mapping ZIR text instructions to post-analysis instructions.32 /// Pre-allocated field for mapping ZIR text instructions to post-analysis instructions.
33 analyzed_inst: *ir.Inst = undefined,33 analyzed_inst: ?*ir.Inst = null,
3434
35 /// These names are used directly as the instruction names in the text format.35 /// These names are used directly as the instruction names in the text format.
36 pub const Tag = enum {36 pub const Tag = enum {
...@@ -545,6 +545,18 @@ pub const Module = struct {...@@ -545,6 +545,18 @@ pub const Module = struct {
545 return null;545 return null;
546 }546 }
547547
548 pub fn findInstDecl(self: Module, inst: *Inst) ?DeclAndIndex {
549 for (self.decls) |decl, i| {
550 if (decl.inst == inst) {
551 return DeclAndIndex{
552 .decl = decl,
553 .index = i,
554 };
555 }
556 }
557 return null;
558 }
559
548 /// The allocator is used for temporary storage, but this function always returns560 /// The allocator is used for temporary storage, but this function always returns
549 /// with no resources allocated.561 /// with no resources allocated.
550 pub fn writeToStream(self: Module, allocator: *Allocator, stream: var) !void {562 pub fn writeToStream(self: Module, allocator: *Allocator, stream: var) !void {
test/stage2/zir.zig+39-78
...@@ -228,17 +228,6 @@ pub fn addCases(ctx: *TestContext) void {...@@ -228,17 +228,6 @@ pub fn addCases(ctx: *TestContext) void {
228 \\@2 = int(2)228 \\@2 = int(2)
229 \\@3 = int(3)229 \\@3 = int(3)
230 \\230 \\
231 \\@syscall_array = str("syscall")
232 \\@sysoutreg_array = str("={rax}")
233 \\@rax_array = str("{rax}")
234 \\@rdi_array = str("{rdi}")
235 \\@rcx_array = str("rcx")
236 \\@r11_array = str("r11")
237 \\@rdx_array = str("{rdx}")
238 \\@rsi_array = str("{rsi}")
239 \\@memory_array = str("memory")
240 \\@len_array = str("len")
241 \\
242 \\@msg = str("Hello, world!\n")231 \\@msg = str("Hello, world!\n")
243 \\232 \\
244 \\@start_fnty = fntype([], @noreturn, cc=Naked)233 \\@start_fnty = fntype([], @noreturn, cc=Naked)
...@@ -246,24 +235,23 @@ pub fn addCases(ctx: *TestContext) void {...@@ -246,24 +235,23 @@ pub fn addCases(ctx: *TestContext) void {
246 \\ %SYS_exit_group = int(231)235 \\ %SYS_exit_group = int(231)
247 \\ %exit_code = as(@usize, @0)236 \\ %exit_code = as(@usize, @0)
248 \\237 \\
249 \\ %syscall = ref(@syscall_array)238 \\ %syscall = str("syscall")
250 \\ %sysoutreg = ref(@sysoutreg_array)239 \\ %sysoutreg = str("={rax}")
251 \\ %rax = ref(@rax_array)240 \\ %rax = str("{rax}")
252 \\ %rdi = ref(@rdi_array)241 \\ %rdi = str("{rdi}")
253 \\ %rcx = ref(@rcx_array)242 \\ %rcx = str("rcx")
254 \\ %rdx = ref(@rdx_array)243 \\ %rdx = str("{rdx}")
255 \\ %rsi = ref(@rsi_array)244 \\ %rsi = str("{rsi}")
256 \\ %r11 = ref(@r11_array)245 \\ %r11 = str("r11")
257 \\ %memory = ref(@memory_array)246 \\ %memory = str("memory")
258 \\247 \\
259 \\ %SYS_write = as(@usize, @1)248 \\ %SYS_write = as(@usize, @1)
260 \\ %STDOUT_FILENO = as(@usize, @1)249 \\ %STDOUT_FILENO = as(@usize, @1)
261 \\250 \\
262 \\ %msg_ptr = ref(@msg)251 \\ %msg_addr = ptrtoint(@msg)
263 \\ %msg_addr = ptrtoint(%msg_ptr)
264 \\252 \\
265 \\ %len_name = ref(@len_array)253 \\ %len_name = str("len")
266 \\ %msg_len_ptr = fieldptr(%msg_ptr, %len_name)254 \\ %msg_len_ptr = fieldptr(@msg, %len_name)
267 \\ %msg_len = deref(%msg_len_ptr)255 \\ %msg_len = deref(%msg_len_ptr)
268 \\ %rc_write = asm(%syscall, @usize,256 \\ %rc_write = asm(%syscall, @usize,
269 \\ volatile=1,257 \\ volatile=1,
...@@ -283,8 +271,7 @@ pub fn addCases(ctx: *TestContext) void {...@@ -283,8 +271,7 @@ pub fn addCases(ctx: *TestContext) void {
283 \\});271 \\});
284 \\272 \\
285 \\@9 = str("_start")273 \\@9 = str("_start")
286 \\@10 = ref(@9)274 \\@11 = export(@9, "start")
287 \\@11 = export(@10, @start)
288 ,275 ,
289 \\@noreturn = primitive(noreturn)276 \\@noreturn = primitive(noreturn)
290 \\@void = primitive(void)277 \\@void = primitive(void)
...@@ -294,17 +281,6 @@ pub fn addCases(ctx: *TestContext) void {...@@ -294,17 +281,6 @@ pub fn addCases(ctx: *TestContext) void {
294 \\@2 = int(2)281 \\@2 = int(2)
295 \\@3 = int(3)282 \\@3 = int(3)
296 \\283 \\
297 \\@syscall_array = str("syscall")
298 \\@sysoutreg_array = str("={rax}")
299 \\@rax_array = str("{rax}")
300 \\@rdi_array = str("{rdi}")
301 \\@rcx_array = str("rcx")
302 \\@r11_array = str("r11")
303 \\@rdx_array = str("{rdx}")
304 \\@rsi_array = str("{rsi}")
305 \\@memory_array = str("memory")
306 \\@len_array = str("len")
307 \\
308 \\@msg = str("Hello, world!\n")284 \\@msg = str("Hello, world!\n")
309 \\@msg2 = str("HELL WORLD\n")285 \\@msg2 = str("HELL WORLD\n")
310 \\286 \\
...@@ -313,24 +289,23 @@ pub fn addCases(ctx: *TestContext) void {...@@ -313,24 +289,23 @@ pub fn addCases(ctx: *TestContext) void {
313 \\ %SYS_exit_group = int(231)289 \\ %SYS_exit_group = int(231)
314 \\ %exit_code = as(@usize, @0)290 \\ %exit_code = as(@usize, @0)
315 \\291 \\
316 \\ %syscall = ref(@syscall_array)292 \\ %syscall = str("syscall")
317 \\ %sysoutreg = ref(@sysoutreg_array)293 \\ %sysoutreg = str("={rax}")
318 \\ %rax = ref(@rax_array)294 \\ %rax = str("{rax}")
319 \\ %rdi = ref(@rdi_array)295 \\ %rdi = str("{rdi}")
320 \\ %rcx = ref(@rcx_array)296 \\ %rcx = str("rcx")
321 \\ %rdx = ref(@rdx_array)297 \\ %rdx = str("{rdx}")
322 \\ %rsi = ref(@rsi_array)298 \\ %rsi = str("{rsi}")
323 \\ %r11 = ref(@r11_array)299 \\ %r11 = str("r11")
324 \\ %memory = ref(@memory_array)300 \\ %memory = str("memory")
325 \\301 \\
326 \\ %SYS_write = as(@usize, @1)302 \\ %SYS_write = as(@usize, @1)
327 \\ %STDOUT_FILENO = as(@usize, @1)303 \\ %STDOUT_FILENO = as(@usize, @1)
328 \\304 \\
329 \\ %msg_ptr = ref(@msg2)305 \\ %msg_addr = ptrtoint(@msg2)
330 \\ %msg_addr = ptrtoint(%msg_ptr)
331 \\306 \\
332 \\ %len_name = ref(@len_array)307 \\ %len_name = str("len")
333 \\ %msg_len_ptr = fieldptr(%msg_ptr, %len_name)308 \\ %msg_len_ptr = fieldptr(@msg2, %len_name)
334 \\ %msg_len = deref(%msg_len_ptr)309 \\ %msg_len = deref(%msg_len_ptr)
335 \\ %rc_write = asm(%syscall, @usize,310 \\ %rc_write = asm(%syscall, @usize,
336 \\ volatile=1,311 \\ volatile=1,
...@@ -350,8 +325,7 @@ pub fn addCases(ctx: *TestContext) void {...@@ -350,8 +325,7 @@ pub fn addCases(ctx: *TestContext) void {
350 \\});325 \\});
351 \\326 \\
352 \\@9 = str("_start")327 \\@9 = str("_start")
353 \\@10 = ref(@9)328 \\@11 = export(@9, "start")
354 \\@11 = export(@10, @start)
355 ,329 ,
356 \\@noreturn = primitive(noreturn)330 \\@noreturn = primitive(noreturn)
357 \\@void = primitive(void)331 \\@void = primitive(void)
...@@ -361,17 +335,6 @@ pub fn addCases(ctx: *TestContext) void {...@@ -361,17 +335,6 @@ pub fn addCases(ctx: *TestContext) void {
361 \\@2 = int(2)335 \\@2 = int(2)
362 \\@3 = int(3)336 \\@3 = int(3)
363 \\337 \\
364 \\@syscall_array = str("syscall")
365 \\@sysoutreg_array = str("={rax}")
366 \\@rax_array = str("{rax}")
367 \\@rdi_array = str("{rdi}")
368 \\@rcx_array = str("rcx")
369 \\@r11_array = str("r11")
370 \\@rdx_array = str("{rdx}")
371 \\@rsi_array = str("{rsi}")
372 \\@memory_array = str("memory")
373 \\@len_array = str("len")
374 \\
375 \\@msg = str("Hello, world!\n")338 \\@msg = str("Hello, world!\n")
376 \\@msg2 = str("Editing the same msg2 decl but this time with a much longer message which will\ncause the data to need to be relocated in virtual address space.\n")339 \\@msg2 = str("Editing the same msg2 decl but this time with a much longer message which will\ncause the data to need to be relocated in virtual address space.\n")
377 \\340 \\
...@@ -380,24 +343,23 @@ pub fn addCases(ctx: *TestContext) void {...@@ -380,24 +343,23 @@ pub fn addCases(ctx: *TestContext) void {
380 \\ %SYS_exit_group = int(231)343 \\ %SYS_exit_group = int(231)
381 \\ %exit_code = as(@usize, @0)344 \\ %exit_code = as(@usize, @0)
382 \\345 \\
383 \\ %syscall = ref(@syscall_array)346 \\ %syscall = str("syscall")
384 \\ %sysoutreg = ref(@sysoutreg_array)347 \\ %sysoutreg = str("={rax}")
385 \\ %rax = ref(@rax_array)348 \\ %rax = str("{rax}")
386 \\ %rdi = ref(@rdi_array)349 \\ %rdi = str("{rdi}")
387 \\ %rcx = ref(@rcx_array)350 \\ %rcx = str("rcx")
388 \\ %rdx = ref(@rdx_array)351 \\ %rdx = str("{rdx}")
389 \\ %rsi = ref(@rsi_array)352 \\ %rsi = str("{rsi}")
390 \\ %r11 = ref(@r11_array)353 \\ %r11 = str("r11")
391 \\ %memory = ref(@memory_array)354 \\ %memory = str("memory")
392 \\355 \\
393 \\ %SYS_write = as(@usize, @1)356 \\ %SYS_write = as(@usize, @1)
394 \\ %STDOUT_FILENO = as(@usize, @1)357 \\ %STDOUT_FILENO = as(@usize, @1)
395 \\358 \\
396 \\ %msg_ptr = ref(@msg2)359 \\ %msg_addr = ptrtoint(@msg2)
397 \\ %msg_addr = ptrtoint(%msg_ptr)
398 \\360 \\
399 \\ %len_name = ref(@len_array)361 \\ %len_name = str("len")
400 \\ %msg_len_ptr = fieldptr(%msg_ptr, %len_name)362 \\ %msg_len_ptr = fieldptr(@msg2, %len_name)
401 \\ %msg_len = deref(%msg_len_ptr)363 \\ %msg_len = deref(%msg_len_ptr)
402 \\ %rc_write = asm(%syscall, @usize,364 \\ %rc_write = asm(%syscall, @usize,
403 \\ volatile=1,365 \\ volatile=1,
...@@ -417,8 +379,7 @@ pub fn addCases(ctx: *TestContext) void {...@@ -417,8 +379,7 @@ pub fn addCases(ctx: *TestContext) void {
417 \\});379 \\});
418 \\380 \\
419 \\@9 = str("_start")381 \\@9 = str("_start")
420 \\@10 = ref(@9)382 \\@11 = export(@9, "start")
421 \\@11 = export(@10, @start)
422 },383 },
423 &[_][]const u8{384 &[_][]const u8{
424 \\Hello, world!385 \\Hello, world!