authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-06 06:10:44+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-06 06:10:44+00:00
log8be8ebd698aac447db2babf95def4725d9ddd05f
tree9a63bcd593efed5358244bd922e7dec4b0778a92
parentad2ed457dd925c65d7d2bcac9208cee5619c523d

stage2: skeleton codegen for x64 ADD

also rework Module to take advantage of the new hash map implementation.

6 files changed, 392 insertions(+), 281 deletions(-)

src-self-hosted/Module.zig+184-205
...@@ -20,8 +20,8 @@ const ast = std.zig.ast;...@@ -20,8 +20,8 @@ const ast = std.zig.ast;
20const trace = @import("tracy.zig").trace;20const trace = @import("tracy.zig").trace;
21const liveness = @import("liveness.zig");21const liveness = @import("liveness.zig");
2222
23/// General-purpose allocator.23/// General-purpose allocator. Used for both temporary and long-term storage.
24allocator: *Allocator,24gpa: *Allocator,
25/// Pointer to externally managed resource.25/// Pointer to externally managed resource.
26root_pkg: *Package,26root_pkg: *Package,
27/// Module owns this resource.27/// Module owns this resource.
...@@ -33,7 +33,7 @@ bin_file_path: []const u8,...@@ -33,7 +33,7 @@ bin_file_path: []const u8,
33/// It's rare for a decl to be exported, so we save memory by having a sparse map of33/// It's rare for a decl to be exported, so we save memory by having a sparse map of
34/// Decl pointers to details about them being exported.34/// Decl pointers to details about them being exported.
35/// The Export memory is owned by the `export_owners` table; the slice itself is owned by this table.35/// The Export memory is owned by the `export_owners` table; the slice itself is owned by this table.
36decl_exports: std.AutoHashMap(*Decl, []*Export),36decl_exports: std.AutoHashMapUnmanaged(*Decl, []*Export) = .{},
37/// We track which export is associated with the given symbol name for quick37/// We track which export is associated with the given symbol name for quick
38/// detection of symbol collisions.38/// detection of symbol collisions.
39symbol_exports: std.StringHashMap(*Export),39symbol_exports: std.StringHashMap(*Export),
...@@ -41,9 +41,9 @@ symbol_exports: std.StringHashMap(*Export),...@@ -41,9 +41,9 @@ symbol_exports: std.StringHashMap(*Export),
41/// is modified. Note that the key of this table is not the Decl being exported, but the Decl that41/// is modified. Note that the key of this table is not the Decl being exported, but the Decl that
42/// is performing the export of another Decl.42/// is performing the export of another Decl.
43/// This table owns the Export memory.43/// This table owns the Export memory.
44export_owners: std.AutoHashMap(*Decl, []*Export),44export_owners: std.AutoHashMapUnmanaged(*Decl, []*Export) = .{},
45/// Maps fully qualified namespaced names to the Decl struct for them.45/// Maps fully qualified namespaced names to the Decl struct for them.
46decl_table: DeclTable,46decl_table: std.HashMapUnmanaged(Scope.NameHash, *Decl, Scope.name_hash_hash, Scope.name_hash_eql, false) = .{},
4747
48optimize_mode: std.builtin.Mode,48optimize_mode: std.builtin.Mode,
49link_error_flags: link.ElfFile.ErrorFlags = .{},49link_error_flags: link.ElfFile.ErrorFlags = .{},
...@@ -55,13 +55,13 @@ work_queue: std.fifo.LinearFifo(WorkItem, .Dynamic),...@@ -55,13 +55,13 @@ work_queue: std.fifo.LinearFifo(WorkItem, .Dynamic),
55/// The ErrorMsg memory is owned by the decl, using Module's allocator.55/// The ErrorMsg memory is owned by the decl, using Module's allocator.
56/// Note that a Decl can succeed but the Fn it represents can fail. In this case,56/// Note that a Decl can succeed but the Fn it represents can fail. In this case,
57/// a Decl can have a failed_decls entry but have analysis status of success.57/// a Decl can have a failed_decls entry but have analysis status of success.
58failed_decls: std.AutoHashMap(*Decl, *ErrorMsg),58failed_decls: std.AutoHashMapUnmanaged(*Decl, *ErrorMsg) = .{},
59/// Using a map here for consistency with the other fields here.59/// Using a map here for consistency with the other fields here.
60/// The ErrorMsg memory is owned by the `Scope`, using Module's allocator.60/// The ErrorMsg memory is owned by the `Scope`, using Module's allocator.
61failed_files: std.AutoHashMap(*Scope, *ErrorMsg),61failed_files: std.AutoHashMapUnmanaged(*Scope, *ErrorMsg) = .{},
62/// Using a map here for consistency with the other fields here.62/// Using a map here for consistency with the other fields here.
63/// The ErrorMsg memory is owned by the `Export`, using Module's allocator.63/// The ErrorMsg memory is owned by the `Export`, using Module's allocator.
64failed_exports: std.AutoHashMap(*Export, *ErrorMsg),64failed_exports: std.AutoHashMapUnmanaged(*Export, *ErrorMsg) = .{},
6565
66/// Incrementing integer used to compare against the corresponding Decl66/// Incrementing integer used to compare against the corresponding Decl
67/// field to determine whether a Decl's status applies to an ongoing update, or a67/// field to determine whether a Decl's status applies to an ongoing update, or a
...@@ -76,8 +76,6 @@ deletion_set: std.ArrayListUnmanaged(*Decl) = .{},...@@ -76,8 +76,6 @@ deletion_set: std.ArrayListUnmanaged(*Decl) = .{},
7676
77keep_source_files_loaded: bool,77keep_source_files_loaded: bool,
7878
79const DeclTable = std.HashMap(Scope.NameHash, *Decl, Scope.name_hash_hash, Scope.name_hash_eql, false);
80
81const WorkItem = union(enum) {79const WorkItem = union(enum) {
82 /// Write the machine code for a Decl to the output file.80 /// Write the machine code for a Decl to the output file.
83 codegen_decl: *Decl,81 codegen_decl: *Decl,
...@@ -176,19 +174,23 @@ pub const Decl = struct {...@@ -176,19 +174,23 @@ pub const Decl = struct {
176174
177 /// The shallow set of other decls whose typed_value could possibly change if this Decl's175 /// The shallow set of other decls whose typed_value could possibly change if this Decl's
178 /// typed_value is modified.176 /// typed_value is modified.
179 dependants: ArrayListUnmanaged(*Decl) = ArrayListUnmanaged(*Decl){},177 dependants: DepsTable = .{},
180 /// The shallow set of other decls whose typed_value changing indicates that this Decl's178 /// The shallow set of other decls whose typed_value changing indicates that this Decl's
181 /// typed_value may need to be regenerated.179 /// typed_value may need to be regenerated.
182 dependencies: ArrayListUnmanaged(*Decl) = ArrayListUnmanaged(*Decl){},180 dependencies: DepsTable = .{},
181
182 /// The reason this is not `std.AutoHashMapUnmanaged` is a workaround for
183 /// stage1 compiler giving me: `error: struct 'Module.Decl' depends on itself`
184 pub const DepsTable = std.HashMapUnmanaged(*Decl, void, std.hash_map.getAutoHashFn(*Decl), std.hash_map.getAutoEqlFn(*Decl), false);
183185
184 pub fn destroy(self: *Decl, allocator: *Allocator) void {186 pub fn destroy(self: *Decl, gpa: *Allocator) void {
185 allocator.free(mem.spanZ(self.name));187 gpa.free(mem.spanZ(self.name));
186 if (self.typedValueManaged()) |tvm| {188 if (self.typedValueManaged()) |tvm| {
187 tvm.deinit(allocator);189 tvm.deinit(gpa);
188 }190 }
189 self.dependants.deinit(allocator);191 self.dependants.deinit(gpa);
190 self.dependencies.deinit(allocator);192 self.dependencies.deinit(gpa);
191 allocator.destroy(self);193 gpa.destroy(self);
192 }194 }
193195
194 pub fn src(self: Decl) usize {196 pub fn src(self: Decl) usize {
...@@ -247,23 +249,11 @@ pub const Decl = struct {...@@ -247,23 +249,11 @@ pub const Decl = struct {
247 }249 }
248250
249 fn removeDependant(self: *Decl, other: *Decl) void {251 fn removeDependant(self: *Decl, other: *Decl) void {
250 for (self.dependants.items) |item, i| {252 self.dependants.removeAssertDiscard(other);
251 if (item == other) {
252 _ = self.dependants.swapRemove(i);
253 return;
254 }
255 }
256 unreachable;
257 }253 }
258254
259 fn removeDependency(self: *Decl, other: *Decl) void {255 fn removeDependency(self: *Decl, other: *Decl) void {
260 for (self.dependencies.items) |item, i| {256 self.dependencies.removeAssertDiscard(other);
261 if (item == other) {
262 _ = self.dependencies.swapRemove(i);
263 return;
264 }
265 }
266 unreachable;
267 }257 }
268};258};
269259
...@@ -390,10 +380,10 @@ pub const Scope = struct {...@@ -390,10 +380,10 @@ pub const Scope = struct {
390 }380 }
391 }381 }
392382
393 pub fn unload(base: *Scope, allocator: *Allocator) void {383 pub fn unload(base: *Scope, gpa: *Allocator) void {
394 switch (base.tag) {384 switch (base.tag) {
395 .file => return @fieldParentPtr(File, "base", base).unload(allocator),385 .file => return @fieldParentPtr(File, "base", base).unload(gpa),
396 .zir_module => return @fieldParentPtr(ZIRModule, "base", base).unload(allocator),386 .zir_module => return @fieldParentPtr(ZIRModule, "base", base).unload(gpa),
397 .block => unreachable,387 .block => unreachable,
398 .gen_zir => unreachable,388 .gen_zir => unreachable,
399 .decl => unreachable,389 .decl => unreachable,
...@@ -422,17 +412,17 @@ pub const Scope = struct {...@@ -422,17 +412,17 @@ pub const Scope = struct {
422 }412 }
423413
424 /// Asserts the scope is a File or ZIRModule and deinitializes it, then deallocates it.414 /// Asserts the scope is a File or ZIRModule and deinitializes it, then deallocates it.
425 pub fn destroy(base: *Scope, allocator: *Allocator) void {415 pub fn destroy(base: *Scope, gpa: *Allocator) void {
426 switch (base.tag) {416 switch (base.tag) {
427 .file => {417 .file => {
428 const scope_file = @fieldParentPtr(File, "base", base);418 const scope_file = @fieldParentPtr(File, "base", base);
429 scope_file.deinit(allocator);419 scope_file.deinit(gpa);
430 allocator.destroy(scope_file);420 gpa.destroy(scope_file);
431 },421 },
432 .zir_module => {422 .zir_module => {
433 const scope_zir_module = @fieldParentPtr(ZIRModule, "base", base);423 const scope_zir_module = @fieldParentPtr(ZIRModule, "base", base);
434 scope_zir_module.deinit(allocator);424 scope_zir_module.deinit(gpa);
435 allocator.destroy(scope_zir_module);425 gpa.destroy(scope_zir_module);
436 },426 },
437 .block => unreachable,427 .block => unreachable,
438 .gen_zir => unreachable,428 .gen_zir => unreachable,
...@@ -483,7 +473,7 @@ pub const Scope = struct {...@@ -483,7 +473,7 @@ pub const Scope = struct {
483 /// Direct children of the file.473 /// Direct children of the file.
484 decls: ArrayListUnmanaged(*Decl),474 decls: ArrayListUnmanaged(*Decl),
485475
486 pub fn unload(self: *File, allocator: *Allocator) void {476 pub fn unload(self: *File, gpa: *Allocator) void {
487 switch (self.status) {477 switch (self.status) {
488 .never_loaded,478 .never_loaded,
489 .unloaded_parse_failure,479 .unloaded_parse_failure,
...@@ -497,16 +487,16 @@ pub const Scope = struct {...@@ -497,16 +487,16 @@ pub const Scope = struct {
497 }487 }
498 switch (self.source) {488 switch (self.source) {
499 .bytes => |bytes| {489 .bytes => |bytes| {
500 allocator.free(bytes);490 gpa.free(bytes);
501 self.source = .{ .unloaded = {} };491 self.source = .{ .unloaded = {} };
502 },492 },
503 .unloaded => {},493 .unloaded => {},
504 }494 }
505 }495 }
506496
507 pub fn deinit(self: *File, allocator: *Allocator) void {497 pub fn deinit(self: *File, gpa: *Allocator) void {
508 self.decls.deinit(allocator);498 self.decls.deinit(gpa);
509 self.unload(allocator);499 self.unload(gpa);
510 self.* = undefined;500 self.* = undefined;
511 }501 }
512502
...@@ -528,7 +518,7 @@ pub const Scope = struct {...@@ -528,7 +518,7 @@ pub const Scope = struct {
528 switch (self.source) {518 switch (self.source) {
529 .unloaded => {519 .unloaded => {
530 const source = try module.root_pkg.root_src_dir.readFileAllocOptions(520 const source = try module.root_pkg.root_src_dir.readFileAllocOptions(
531 module.allocator,521 module.gpa,
532 self.sub_file_path,522 self.sub_file_path,
533 std.math.maxInt(u32),523 std.math.maxInt(u32),
534 1,524 1,
...@@ -576,7 +566,7 @@ pub const Scope = struct {...@@ -576,7 +566,7 @@ pub const Scope = struct {
576 /// not this one.566 /// not this one.
577 decls: ArrayListUnmanaged(*Decl),567 decls: ArrayListUnmanaged(*Decl),
578568
579 pub fn unload(self: *ZIRModule, allocator: *Allocator) void {569 pub fn unload(self: *ZIRModule, gpa: *Allocator) void {
580 switch (self.status) {570 switch (self.status) {
581 .never_loaded,571 .never_loaded,
582 .unloaded_parse_failure,572 .unloaded_parse_failure,
...@@ -585,30 +575,30 @@ pub const Scope = struct {...@@ -585,30 +575,30 @@ pub const Scope = struct {
585 => {},575 => {},
586576
587 .loaded_success => {577 .loaded_success => {
588 self.contents.module.deinit(allocator);578 self.contents.module.deinit(gpa);
589 allocator.destroy(self.contents.module);579 gpa.destroy(self.contents.module);
590 self.contents = .{ .not_available = {} };580 self.contents = .{ .not_available = {} };
591 self.status = .unloaded_success;581 self.status = .unloaded_success;
592 },582 },
593 .loaded_sema_failure => {583 .loaded_sema_failure => {
594 self.contents.module.deinit(allocator);584 self.contents.module.deinit(gpa);
595 allocator.destroy(self.contents.module);585 gpa.destroy(self.contents.module);
596 self.contents = .{ .not_available = {} };586 self.contents = .{ .not_available = {} };
597 self.status = .unloaded_sema_failure;587 self.status = .unloaded_sema_failure;
598 },588 },
599 }589 }
600 switch (self.source) {590 switch (self.source) {
601 .bytes => |bytes| {591 .bytes => |bytes| {
602 allocator.free(bytes);592 gpa.free(bytes);
603 self.source = .{ .unloaded = {} };593 self.source = .{ .unloaded = {} };
604 },594 },
605 .unloaded => {},595 .unloaded => {},
606 }596 }
607 }597 }
608598
609 pub fn deinit(self: *ZIRModule, allocator: *Allocator) void {599 pub fn deinit(self: *ZIRModule, gpa: *Allocator) void {
610 self.decls.deinit(allocator);600 self.decls.deinit(gpa);
611 self.unload(allocator);601 self.unload(gpa);
612 self.* = undefined;602 self.* = undefined;
613 }603 }
614604
...@@ -630,7 +620,7 @@ pub const Scope = struct {...@@ -630,7 +620,7 @@ pub const Scope = struct {
630 switch (self.source) {620 switch (self.source) {
631 .unloaded => {621 .unloaded => {
632 const source = try module.root_pkg.root_src_dir.readFileAllocOptions(622 const source = try module.root_pkg.root_src_dir.readFileAllocOptions(
633 module.allocator,623 module.gpa,
634 self.sub_file_path,624 self.sub_file_path,
635 std.math.maxInt(u32),625 std.math.maxInt(u32),
636 1,626 1,
...@@ -701,8 +691,8 @@ pub const AllErrors = struct {...@@ -701,8 +691,8 @@ pub const AllErrors = struct {
701 msg: []const u8,691 msg: []const u8,
702 };692 };
703693
704 pub fn deinit(self: *AllErrors, allocator: *Allocator) void {694 pub fn deinit(self: *AllErrors, gpa: *Allocator) void {
705 self.arena.promote(allocator).deinit();695 self.arena.promote(gpa).deinit();
706 }696 }
707697
708 fn add(698 fn add(
...@@ -772,20 +762,14 @@ pub fn init(gpa: *Allocator, options: InitOptions) !Module {...@@ -772,20 +762,14 @@ pub fn init(gpa: *Allocator, options: InitOptions) !Module {
772 };762 };
773763
774 return Module{764 return Module{
775 .allocator = gpa,765 .gpa = gpa,
776 .root_pkg = options.root_pkg,766 .root_pkg = options.root_pkg,
777 .root_scope = root_scope,767 .root_scope = root_scope,
778 .bin_file_dir = bin_file_dir,768 .bin_file_dir = bin_file_dir,
779 .bin_file_path = options.bin_file_path,769 .bin_file_path = options.bin_file_path,
780 .bin_file = bin_file,770 .bin_file = bin_file,
781 .optimize_mode = options.optimize_mode,771 .optimize_mode = options.optimize_mode,
782 .decl_table = DeclTable.init(gpa),
783 .decl_exports = std.AutoHashMap(*Decl, []*Export).init(gpa),
784 .symbol_exports = std.StringHashMap(*Export).init(gpa),772 .symbol_exports = std.StringHashMap(*Export).init(gpa),
785 .export_owners = std.AutoHashMap(*Decl, []*Export).init(gpa),
786 .failed_decls = std.AutoHashMap(*Decl, *ErrorMsg).init(gpa),
787 .failed_files = std.AutoHashMap(*Scope, *ErrorMsg).init(gpa),
788 .failed_exports = std.AutoHashMap(*Export, *ErrorMsg).init(gpa),
789 .work_queue = std.fifo.LinearFifo(WorkItem, .Dynamic).init(gpa),773 .work_queue = std.fifo.LinearFifo(WorkItem, .Dynamic).init(gpa),
790 .keep_source_files_loaded = options.keep_source_files_loaded,774 .keep_source_files_loaded = options.keep_source_files_loaded,
791 };775 };
...@@ -793,51 +777,51 @@ pub fn init(gpa: *Allocator, options: InitOptions) !Module {...@@ -793,51 +777,51 @@ pub fn init(gpa: *Allocator, options: InitOptions) !Module {
793777
794pub fn deinit(self: *Module) void {778pub fn deinit(self: *Module) void {
795 self.bin_file.deinit();779 self.bin_file.deinit();
796 const allocator = self.allocator;780 const gpa = self.gpa;
797 self.deletion_set.deinit(allocator);781 self.deletion_set.deinit(gpa);
798 self.work_queue.deinit();782 self.work_queue.deinit();
799783
800 for (self.decl_table.items()) |entry| {784 for (self.decl_table.items()) |entry| {
801 entry.value.destroy(allocator);785 entry.value.destroy(gpa);
802 }786 }
803 self.decl_table.deinit();787 self.decl_table.deinit(gpa);
804788
805 for (self.failed_decls.items()) |entry| {789 for (self.failed_decls.items()) |entry| {
806 entry.value.destroy(allocator);790 entry.value.destroy(gpa);
807 }791 }
808 self.failed_decls.deinit();792 self.failed_decls.deinit(gpa);
809793
810 for (self.failed_files.items()) |entry| {794 for (self.failed_files.items()) |entry| {
811 entry.value.destroy(allocator);795 entry.value.destroy(gpa);
812 }796 }
813 self.failed_files.deinit();797 self.failed_files.deinit(gpa);
814798
815 for (self.failed_exports.items()) |entry| {799 for (self.failed_exports.items()) |entry| {
816 entry.value.destroy(allocator);800 entry.value.destroy(gpa);
817 }801 }
818 self.failed_exports.deinit();802 self.failed_exports.deinit(gpa);
819803
820 for (self.decl_exports.items()) |entry| {804 for (self.decl_exports.items()) |entry| {
821 const export_list = entry.value;805 const export_list = entry.value;
822 allocator.free(export_list);806 gpa.free(export_list);
823 }807 }
824 self.decl_exports.deinit();808 self.decl_exports.deinit(gpa);
825809
826 for (self.export_owners.items()) |entry| {810 for (self.export_owners.items()) |entry| {
827 freeExportList(allocator, entry.value);811 freeExportList(gpa, entry.value);
828 }812 }
829 self.export_owners.deinit();813 self.export_owners.deinit(gpa);
830814
831 self.symbol_exports.deinit();815 self.symbol_exports.deinit();
832 self.root_scope.destroy(allocator);816 self.root_scope.destroy(gpa);
833 self.* = undefined;817 self.* = undefined;
834}818}
835819
836fn freeExportList(allocator: *Allocator, export_list: []*Export) void {820fn freeExportList(gpa: *Allocator, export_list: []*Export) void {
837 for (export_list) |exp| {821 for (export_list) |exp| {
838 allocator.destroy(exp);822 gpa.destroy(exp);
839 }823 }
840 allocator.free(export_list);824 gpa.free(export_list);
841}825}
842826
843pub fn target(self: Module) std.Target {827pub fn target(self: Module) std.Target {
...@@ -855,7 +839,7 @@ pub fn update(self: *Module) !void {...@@ -855,7 +839,7 @@ pub fn update(self: *Module) !void {
855 // Until then we simulate a full cache miss. Source files could have been loaded for any reason;839 // Until then we simulate a full cache miss. Source files could have been loaded for any reason;
856 // to force a refresh we unload now.840 // to force a refresh we unload now.
857 if (self.root_scope.cast(Scope.File)) |zig_file| {841 if (self.root_scope.cast(Scope.File)) |zig_file| {
858 zig_file.unload(self.allocator);842 zig_file.unload(self.gpa);
859 self.analyzeRootSrcFile(zig_file) catch |err| switch (err) {843 self.analyzeRootSrcFile(zig_file) catch |err| switch (err) {
860 error.AnalysisFail => {844 error.AnalysisFail => {
861 assert(self.totalErrorCount() != 0);845 assert(self.totalErrorCount() != 0);
...@@ -863,7 +847,7 @@ pub fn update(self: *Module) !void {...@@ -863,7 +847,7 @@ pub fn update(self: *Module) !void {
863 else => |e| return e,847 else => |e| return e,
864 };848 };
865 } else if (self.root_scope.cast(Scope.ZIRModule)) |zir_module| {849 } else if (self.root_scope.cast(Scope.ZIRModule)) |zir_module| {
866 zir_module.unload(self.allocator);850 zir_module.unload(self.gpa);
867 self.analyzeRootZIRModule(zir_module) catch |err| switch (err) {851 self.analyzeRootZIRModule(zir_module) catch |err| switch (err) {
868 error.AnalysisFail => {852 error.AnalysisFail => {
869 assert(self.totalErrorCount() != 0);853 assert(self.totalErrorCount() != 0);
...@@ -876,7 +860,7 @@ pub fn update(self: *Module) !void {...@@ -876,7 +860,7 @@ pub fn update(self: *Module) !void {
876860
877 // Process the deletion set.861 // Process the deletion set.
878 while (self.deletion_set.popOrNull()) |decl| {862 while (self.deletion_set.popOrNull()) |decl| {
879 if (decl.dependants.items.len != 0) {863 if (decl.dependants.items().len != 0) {
880 decl.deletion_flag = false;864 decl.deletion_flag = false;
881 continue;865 continue;
882 }866 }
...@@ -889,7 +873,7 @@ pub fn update(self: *Module) !void {...@@ -889,7 +873,7 @@ pub fn update(self: *Module) !void {
889 // to report error messages. Otherwise we unload all source files to save memory.873 // to report error messages. Otherwise we unload all source files to save memory.
890 if (self.totalErrorCount() == 0) {874 if (self.totalErrorCount() == 0) {
891 if (!self.keep_source_files_loaded) {875 if (!self.keep_source_files_loaded) {
892 self.root_scope.unload(self.allocator);876 self.root_scope.unload(self.gpa);
893 }877 }
894 try self.bin_file.flush();878 try self.bin_file.flush();
895 }879 }
...@@ -915,10 +899,10 @@ pub fn totalErrorCount(self: *Module) usize {...@@ -915,10 +899,10 @@ pub fn totalErrorCount(self: *Module) usize {
915}899}
916900
917pub fn getAllErrorsAlloc(self: *Module) !AllErrors {901pub fn getAllErrorsAlloc(self: *Module) !AllErrors {
918 var arena = std.heap.ArenaAllocator.init(self.allocator);902 var arena = std.heap.ArenaAllocator.init(self.gpa);
919 errdefer arena.deinit();903 errdefer arena.deinit();
920904
921 var errors = std.ArrayList(AllErrors.Message).init(self.allocator);905 var errors = std.ArrayList(AllErrors.Message).init(self.gpa);
922 defer errors.deinit();906 defer errors.deinit();
923907
924 for (self.failed_files.items()) |entry| {908 for (self.failed_files.items()) |entry| {
...@@ -989,9 +973,9 @@ pub fn performAllTheWork(self: *Module) error{OutOfMemory}!void {...@@ -989,9 +973,9 @@ pub fn performAllTheWork(self: *Module) error{OutOfMemory}!void {
989 }973 }
990 // Here we tack on additional allocations to the Decl's arena. The allocations are974 // Here we tack on additional allocations to the Decl's arena. The allocations are
991 // lifetime annotations in the ZIR.975 // lifetime annotations in the ZIR.
992 var decl_arena = decl.typed_value.most_recent.arena.?.promote(self.allocator);976 var decl_arena = decl.typed_value.most_recent.arena.?.promote(self.gpa);
993 defer decl.typed_value.most_recent.arena.?.* = decl_arena.state;977 defer decl.typed_value.most_recent.arena.?.* = decl_arena.state;
994 try liveness.analyze(self.allocator, &decl_arena.allocator, payload.func.analysis.success);978 try liveness.analyze(self.gpa, &decl_arena.allocator, payload.func.analysis.success);
995 }979 }
996980
997 assert(decl.typed_value.most_recent.typed_value.ty.hasCodeGenBits());981 assert(decl.typed_value.most_recent.typed_value.ty.hasCodeGenBits());
...@@ -1002,9 +986,9 @@ pub fn performAllTheWork(self: *Module) error{OutOfMemory}!void {...@@ -1002,9 +986,9 @@ pub fn performAllTheWork(self: *Module) error{OutOfMemory}!void {
1002 decl.analysis = .dependency_failure;986 decl.analysis = .dependency_failure;
1003 },987 },
1004 else => {988 else => {
1005 try self.failed_decls.ensureCapacity(self.failed_decls.items().len + 1);989 try self.failed_decls.ensureCapacity(self.gpa, self.failed_decls.items().len + 1);
1006 self.failed_decls.putAssumeCapacityNoClobber(decl, try ErrorMsg.create(990 self.failed_decls.putAssumeCapacityNoClobber(decl, try ErrorMsg.create(
1007 self.allocator,991 self.gpa,
1008 decl.src(),992 decl.src(),
1009 "unable to codegen: {}",993 "unable to codegen: {}",
1010 .{@errorName(err)},994 .{@errorName(err)},
...@@ -1048,16 +1032,17 @@ fn ensureDeclAnalyzed(self: *Module, decl: *Decl) InnerError!void {...@@ -1048,16 +1032,17 @@ fn ensureDeclAnalyzed(self: *Module, decl: *Decl) InnerError!void {
1048 // prior to re-analysis.1032 // prior to re-analysis.
1049 self.deleteDeclExports(decl);1033 self.deleteDeclExports(decl);
1050 // Dependencies will be re-discovered, so we remove them here prior to re-analysis.1034 // Dependencies will be re-discovered, so we remove them here prior to re-analysis.
1051 for (decl.dependencies.items) |dep| {1035 for (decl.dependencies.items()) |entry| {
1036 const dep = entry.key;
1052 dep.removeDependant(decl);1037 dep.removeDependant(decl);
1053 if (dep.dependants.items.len == 0 and !dep.deletion_flag) {1038 if (dep.dependants.items().len == 0 and !dep.deletion_flag) {
1054 // We don't perform a deletion here, because this Decl or another one1039 // We don't perform a deletion here, because this Decl or another one
1055 // may end up referencing it before the update is complete.1040 // may end up referencing it before the update is complete.
1056 dep.deletion_flag = true;1041 dep.deletion_flag = true;
1057 try self.deletion_set.append(self.allocator, dep);1042 try self.deletion_set.append(self.gpa, dep);
1058 }1043 }
1059 }1044 }
1060 decl.dependencies.shrink(self.allocator, 0);1045 decl.dependencies.clearRetainingCapacity();
10611046
1062 break :blk true;1047 break :blk true;
1063 },1048 },
...@@ -1072,9 +1057,9 @@ fn ensureDeclAnalyzed(self: *Module, decl: *Decl) InnerError!void {...@@ -1072,9 +1057,9 @@ fn ensureDeclAnalyzed(self: *Module, decl: *Decl) InnerError!void {
1072 error.OutOfMemory => return error.OutOfMemory,1057 error.OutOfMemory => return error.OutOfMemory,
1073 error.AnalysisFail => return error.AnalysisFail,1058 error.AnalysisFail => return error.AnalysisFail,
1074 else => {1059 else => {
1075 try self.failed_decls.ensureCapacity(self.failed_decls.items().len + 1);1060 try self.failed_decls.ensureCapacity(self.gpa, self.failed_decls.items().len + 1);
1076 self.failed_decls.putAssumeCapacityNoClobber(decl, try ErrorMsg.create(1061 self.failed_decls.putAssumeCapacityNoClobber(decl, try ErrorMsg.create(
1077 self.allocator,1062 self.gpa,
1078 decl.src(),1063 decl.src(),
1079 "unable to analyze: {}",1064 "unable to analyze: {}",
1080 .{@errorName(err)},1065 .{@errorName(err)},
...@@ -1088,7 +1073,8 @@ fn ensureDeclAnalyzed(self: *Module, decl: *Decl) InnerError!void {...@@ -1088,7 +1073,8 @@ fn ensureDeclAnalyzed(self: *Module, decl: *Decl) InnerError!void {
1088 // We may need to chase the dependants and re-analyze them.1073 // We may need to chase the dependants and re-analyze them.
1089 // However, if the decl is a function, and the type is the same, we do not need to.1074 // However, if the decl is a function, and the type is the same, we do not need to.
1090 if (type_changed or decl.typed_value.most_recent.typed_value.val.tag() != .function) {1075 if (type_changed or decl.typed_value.most_recent.typed_value.val.tag() != .function) {
1091 for (decl.dependants.items) |dep| {1076 for (decl.dependants.items()) |entry| {
1077 const dep = entry.key;
1092 switch (dep.analysis) {1078 switch (dep.analysis) {
1093 .unreferenced => unreachable,1079 .unreferenced => unreachable,
1094 .in_progress => unreachable,1080 .in_progress => unreachable,
...@@ -1127,8 +1113,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {...@@ -1127,8 +1113,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
1127 // to complete the Decl analysis.1113 // to complete the Decl analysis.
1128 var fn_type_scope: Scope.GenZIR = .{1114 var fn_type_scope: Scope.GenZIR = .{
1129 .decl = decl,1115 .decl = decl,
1130 .arena = std.heap.ArenaAllocator.init(self.allocator),1116 .arena = std.heap.ArenaAllocator.init(self.gpa),
1131 .instructions = std.ArrayList(*zir.Inst).init(self.allocator),1117 .instructions = std.ArrayList(*zir.Inst).init(self.gpa),
1132 };1118 };
1133 defer fn_type_scope.arena.deinit();1119 defer fn_type_scope.arena.deinit();
1134 defer fn_type_scope.instructions.deinit();1120 defer fn_type_scope.instructions.deinit();
...@@ -1178,7 +1164,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {...@@ -1178,7 +1164,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
1178 _ = try self.addZIRInst(&fn_type_scope.base, fn_src, zir.Inst.Return, .{ .operand = fn_type_inst }, .{});1164 _ = try self.addZIRInst(&fn_type_scope.base, fn_src, zir.Inst.Return, .{ .operand = fn_type_inst }, .{});
11791165
1180 // We need the memory for the Type to go into the arena for the Decl1166 // We need the memory for the Type to go into the arena for the Decl
1181 var decl_arena = std.heap.ArenaAllocator.init(self.allocator);1167 var decl_arena = std.heap.ArenaAllocator.init(self.gpa);
1182 errdefer decl_arena.deinit();1168 errdefer decl_arena.deinit();
1183 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);1169 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);
11841170
...@@ -1189,7 +1175,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {...@@ -1189,7 +1175,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
1189 .instructions = .{},1175 .instructions = .{},
1190 .arena = &decl_arena.allocator,1176 .arena = &decl_arena.allocator,
1191 };1177 };
1192 defer block_scope.instructions.deinit(self.allocator);1178 defer block_scope.instructions.deinit(self.gpa);
11931179
1194 const fn_type = try self.analyzeBodyValueAsType(&block_scope, .{1180 const fn_type = try self.analyzeBodyValueAsType(&block_scope, .{
1195 .instructions = fn_type_scope.instructions.items,1181 .instructions = fn_type_scope.instructions.items,
...@@ -1202,8 +1188,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {...@@ -1202,8 +1188,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
1202 // pass completes, and semantic analysis of it completes.1188 // pass completes, and semantic analysis of it completes.
1203 var gen_scope: Scope.GenZIR = .{1189 var gen_scope: Scope.GenZIR = .{
1204 .decl = decl,1190 .decl = decl,
1205 .arena = std.heap.ArenaAllocator.init(self.allocator),1191 .arena = std.heap.ArenaAllocator.init(self.gpa),
1206 .instructions = std.ArrayList(*zir.Inst).init(self.allocator),1192 .instructions = std.ArrayList(*zir.Inst).init(self.gpa),
1207 };1193 };
1208 errdefer gen_scope.arena.deinit();1194 errdefer gen_scope.arena.deinit();
1209 defer gen_scope.instructions.deinit();1195 defer gen_scope.instructions.deinit();
...@@ -1235,7 +1221,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {...@@ -1235,7 +1221,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
1235 prev_type_has_bits = tvm.typed_value.ty.hasCodeGenBits();1221 prev_type_has_bits = tvm.typed_value.ty.hasCodeGenBits();
1236 type_changed = !tvm.typed_value.ty.eql(fn_type);1222 type_changed = !tvm.typed_value.ty.eql(fn_type);
12371223
1238 tvm.deinit(self.allocator);1224 tvm.deinit(self.gpa);
1239 }1225 }
12401226
1241 decl_arena_state.* = decl_arena.state;1227 decl_arena_state.* = decl_arena.state;
...@@ -1626,40 +1612,31 @@ fn getSimplePrimitiveValue(name: []const u8) ?TypedValue {...@@ -1626,40 +1612,31 @@ fn getSimplePrimitiveValue(name: []const u8) ?TypedValue {
1626}1612}
16271613
1628fn declareDeclDependency(self: *Module, depender: *Decl, dependee: *Decl) !void {1614fn declareDeclDependency(self: *Module, depender: *Decl, dependee: *Decl) !void {
1629 try depender.dependencies.ensureCapacity(self.allocator, depender.dependencies.items.len + 1);1615 try depender.dependencies.ensureCapacity(self.gpa, depender.dependencies.items().len + 1);
1630 try dependee.dependants.ensureCapacity(self.allocator, dependee.dependants.items.len + 1);1616 try dependee.dependants.ensureCapacity(self.gpa, dependee.dependants.items().len + 1);
1631
1632 for (depender.dependencies.items) |item| {
1633 if (item == dependee) break; // Already in the set.
1634 } else {
1635 depender.dependencies.appendAssumeCapacity(dependee);
1636 }
16371617
1638 for (dependee.dependants.items) |item| {1618 depender.dependencies.putAssumeCapacity(dependee, {});
1639 if (item == depender) break; // Already in the set.1619 dependee.dependants.putAssumeCapacity(depender, {});
1640 } else {
1641 dependee.dependants.appendAssumeCapacity(depender);
1642 }
1643}1620}
16441621
1645fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module {1622fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module {
1646 switch (root_scope.status) {1623 switch (root_scope.status) {
1647 .never_loaded, .unloaded_success => {1624 .never_loaded, .unloaded_success => {
1648 try self.failed_files.ensureCapacity(self.failed_files.items().len + 1);1625 try self.failed_files.ensureCapacity(self.gpa, self.failed_files.items().len + 1);
16491626
1650 const source = try root_scope.getSource(self);1627 const source = try root_scope.getSource(self);
16511628
1652 var keep_zir_module = false;1629 var keep_zir_module = false;
1653 const zir_module = try self.allocator.create(zir.Module);1630 const zir_module = try self.gpa.create(zir.Module);
1654 defer if (!keep_zir_module) self.allocator.destroy(zir_module);1631 defer if (!keep_zir_module) self.gpa.destroy(zir_module);
16551632
1656 zir_module.* = try zir.parse(self.allocator, source);1633 zir_module.* = try zir.parse(self.gpa, source);
1657 defer if (!keep_zir_module) zir_module.deinit(self.allocator);1634 defer if (!keep_zir_module) zir_module.deinit(self.gpa);
16581635
1659 if (zir_module.error_msg) |src_err_msg| {1636 if (zir_module.error_msg) |src_err_msg| {
1660 self.failed_files.putAssumeCapacityNoClobber(1637 self.failed_files.putAssumeCapacityNoClobber(
1661 &root_scope.base,1638 &root_scope.base,
1662 try ErrorMsg.create(self.allocator, src_err_msg.byte_offset, "{}", .{src_err_msg.msg}),1639 try ErrorMsg.create(self.gpa, src_err_msg.byte_offset, "{}", .{src_err_msg.msg}),
1663 );1640 );
1664 root_scope.status = .unloaded_parse_failure;1641 root_scope.status = .unloaded_parse_failure;
1665 return error.AnalysisFail;1642 return error.AnalysisFail;
...@@ -1686,22 +1663,22 @@ fn getAstTree(self: *Module, root_scope: *Scope.File) !*ast.Tree {...@@ -1686,22 +1663,22 @@ fn getAstTree(self: *Module, root_scope: *Scope.File) !*ast.Tree {
16861663
1687 switch (root_scope.status) {1664 switch (root_scope.status) {
1688 .never_loaded, .unloaded_success => {1665 .never_loaded, .unloaded_success => {
1689 try self.failed_files.ensureCapacity(self.failed_files.items().len + 1);1666 try self.failed_files.ensureCapacity(self.gpa, self.failed_files.items().len + 1);
16901667
1691 const source = try root_scope.getSource(self);1668 const source = try root_scope.getSource(self);
16921669
1693 var keep_tree = false;1670 var keep_tree = false;
1694 const tree = try std.zig.parse(self.allocator, source);1671 const tree = try std.zig.parse(self.gpa, source);
1695 defer if (!keep_tree) tree.deinit();1672 defer if (!keep_tree) tree.deinit();
16961673
1697 if (tree.errors.len != 0) {1674 if (tree.errors.len != 0) {
1698 const parse_err = tree.errors[0];1675 const parse_err = tree.errors[0];
16991676
1700 var msg = std.ArrayList(u8).init(self.allocator);1677 var msg = std.ArrayList(u8).init(self.gpa);
1701 defer msg.deinit();1678 defer msg.deinit();
17021679
1703 try parse_err.render(tree.token_ids, msg.outStream());1680 try parse_err.render(tree.token_ids, msg.outStream());
1704 const err_msg = try self.allocator.create(ErrorMsg);1681 const err_msg = try self.gpa.create(ErrorMsg);
1705 err_msg.* = .{1682 err_msg.* = .{
1706 .msg = msg.toOwnedSlice(),1683 .msg = msg.toOwnedSlice(),
1707 .byte_offset = tree.token_locs[parse_err.loc()].start,1684 .byte_offset = tree.token_locs[parse_err.loc()].start,
...@@ -1732,11 +1709,11 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {...@@ -1732,11 +1709,11 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
1732 const decls = tree.root_node.decls();1709 const decls = tree.root_node.decls();
17331710
1734 try self.work_queue.ensureUnusedCapacity(decls.len);1711 try self.work_queue.ensureUnusedCapacity(decls.len);
1735 try root_scope.decls.ensureCapacity(self.allocator, decls.len);1712 try root_scope.decls.ensureCapacity(self.gpa, decls.len);
17361713
1737 // Keep track of the decls that we expect to see in this file so that1714 // Keep track of the decls that we expect to see in this file so that
1738 // we know which ones have been deleted.1715 // we know which ones have been deleted.
1739 var deleted_decls = std.AutoHashMap(*Decl, void).init(self.allocator);1716 var deleted_decls = std.AutoHashMap(*Decl, void).init(self.gpa);
1740 defer deleted_decls.deinit();1717 defer deleted_decls.deinit();
1741 try deleted_decls.ensureCapacity(root_scope.decls.items.len);1718 try deleted_decls.ensureCapacity(root_scope.decls.items.len);
1742 for (root_scope.decls.items) |file_decl| {1719 for (root_scope.decls.items) |file_decl| {
...@@ -1760,9 +1737,9 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {...@@ -1760,9 +1737,9 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
1760 decl.src_index = decl_i;1737 decl.src_index = decl_i;
1761 if (deleted_decls.remove(decl) == null) {1738 if (deleted_decls.remove(decl) == null) {
1762 decl.analysis = .sema_failure;1739 decl.analysis = .sema_failure;
1763 const err_msg = try ErrorMsg.create(self.allocator, tree.token_locs[name_tok].start, "redefinition of '{}'", .{decl.name});1740 const err_msg = try ErrorMsg.create(self.gpa, tree.token_locs[name_tok].start, "redefinition of '{}'", .{decl.name});
1764 errdefer err_msg.destroy(self.allocator);1741 errdefer err_msg.destroy(self.gpa);
1765 try self.failed_decls.putNoClobber(decl, err_msg);1742 try self.failed_decls.putNoClobber(self.gpa, decl, err_msg);
1766 } else {1743 } else {
1767 if (!srcHashEql(decl.contents_hash, contents_hash)) {1744 if (!srcHashEql(decl.contents_hash, contents_hash)) {
1768 try self.markOutdatedDecl(decl);1745 try self.markOutdatedDecl(decl);
...@@ -1796,14 +1773,14 @@ fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {...@@ -1796,14 +1773,14 @@ fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {
1796 const src_module = try self.getSrcModule(root_scope);1773 const src_module = try self.getSrcModule(root_scope);
17971774
1798 try self.work_queue.ensureUnusedCapacity(src_module.decls.len);1775 try self.work_queue.ensureUnusedCapacity(src_module.decls.len);
1799 try root_scope.decls.ensureCapacity(self.allocator, src_module.decls.len);1776 try root_scope.decls.ensureCapacity(self.gpa, src_module.decls.len);
18001777
1801 var exports_to_resolve = std.ArrayList(*zir.Decl).init(self.allocator);1778 var exports_to_resolve = std.ArrayList(*zir.Decl).init(self.gpa);
1802 defer exports_to_resolve.deinit();1779 defer exports_to_resolve.deinit();
18031780
1804 // Keep track of the decls that we expect to see in this file so that1781 // Keep track of the decls that we expect to see in this file so that
1805 // we know which ones have been deleted.1782 // we know which ones have been deleted.
1806 var deleted_decls = std.AutoHashMap(*Decl, void).init(self.allocator);1783 var deleted_decls = std.AutoHashMap(*Decl, void).init(self.gpa);
1807 defer deleted_decls.deinit();1784 defer deleted_decls.deinit();
1808 try deleted_decls.ensureCapacity(self.decl_table.items().len);1785 try deleted_decls.ensureCapacity(self.decl_table.items().len);
1809 for (self.decl_table.items()) |entry| {1786 for (self.decl_table.items()) |entry| {
...@@ -1845,7 +1822,7 @@ fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {...@@ -1845,7 +1822,7 @@ fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {
1845}1822}
18461823
1847fn deleteDecl(self: *Module, decl: *Decl) !void {1824fn deleteDecl(self: *Module, decl: *Decl) !void {
1848 try self.deletion_set.ensureCapacity(self.allocator, self.deletion_set.items.len + decl.dependencies.items.len);1825 try self.deletion_set.ensureCapacity(self.gpa, self.deletion_set.items.len + decl.dependencies.items().len);
18491826
1850 // Remove from the namespace it resides in. In the case of an anonymous Decl it will1827 // Remove from the namespace it resides in. In the case of an anonymous Decl it will
1851 // not be present in the set, and this does nothing.1828 // not be present in the set, and this does nothing.
...@@ -1855,9 +1832,10 @@ fn deleteDecl(self: *Module, decl: *Decl) !void {...@@ -1855,9 +1832,10 @@ fn deleteDecl(self: *Module, decl: *Decl) !void {
1855 const name_hash = decl.fullyQualifiedNameHash();1832 const name_hash = decl.fullyQualifiedNameHash();
1856 self.decl_table.removeAssertDiscard(name_hash);1833 self.decl_table.removeAssertDiscard(name_hash);
1857 // Remove itself from its dependencies, because we are about to destroy the decl pointer.1834 // Remove itself from its dependencies, because we are about to destroy the decl pointer.
1858 for (decl.dependencies.items) |dep| {1835 for (decl.dependencies.items()) |entry| {
1836 const dep = entry.key;
1859 dep.removeDependant(decl);1837 dep.removeDependant(decl);
1860 if (dep.dependants.items.len == 0 and !dep.deletion_flag) {1838 if (dep.dependants.items().len == 0 and !dep.deletion_flag) {
1861 // We don't recursively perform a deletion here, because during the update,1839 // We don't recursively perform a deletion here, because during the update,
1862 // another reference to it may turn up.1840 // another reference to it may turn up.
1863 dep.deletion_flag = true;1841 dep.deletion_flag = true;
...@@ -1865,7 +1843,8 @@ fn deleteDecl(self: *Module, decl: *Decl) !void {...@@ -1865,7 +1843,8 @@ fn deleteDecl(self: *Module, decl: *Decl) !void {
1865 }1843 }
1866 }1844 }
1867 // Anything that depends on this deleted decl certainly needs to be re-analyzed.1845 // Anything that depends on this deleted decl certainly needs to be re-analyzed.
1868 for (decl.dependants.items) |dep| {1846 for (decl.dependants.items()) |entry| {
1847 const dep = entry.key;
1869 dep.removeDependency(decl);1848 dep.removeDependency(decl);
1870 if (dep.analysis != .outdated) {1849 if (dep.analysis != .outdated) {
1871 // TODO Move this failure possibility to the top of the function.1850 // TODO Move this failure possibility to the top of the function.
...@@ -1873,11 +1852,11 @@ fn deleteDecl(self: *Module, decl: *Decl) !void {...@@ -1873,11 +1852,11 @@ fn deleteDecl(self: *Module, decl: *Decl) !void {
1873 }1852 }
1874 }1853 }
1875 if (self.failed_decls.remove(decl)) |entry| {1854 if (self.failed_decls.remove(decl)) |entry| {
1876 entry.value.destroy(self.allocator);1855 entry.value.destroy(self.gpa);
1877 }1856 }
1878 self.deleteDeclExports(decl);1857 self.deleteDeclExports(decl);
1879 self.bin_file.freeDecl(decl);1858 self.bin_file.freeDecl(decl);
1880 decl.destroy(self.allocator);1859 decl.destroy(self.gpa);
1881}1860}
18821861
1883/// Delete all the Export objects that are caused by this Decl. Re-analysis of1862/// Delete all the Export objects that are caused by this Decl. Re-analysis of
...@@ -1899,7 +1878,7 @@ fn deleteDeclExports(self: *Module, decl: *Decl) void {...@@ -1899,7 +1878,7 @@ fn deleteDeclExports(self: *Module, decl: *Decl) void {
1899 i += 1;1878 i += 1;
1900 }1879 }
1901 }1880 }
1902 decl_exports_kv.value = self.allocator.shrink(list, new_len);1881 decl_exports_kv.value = self.gpa.shrink(list, new_len);
1903 if (new_len == 0) {1882 if (new_len == 0) {
1904 self.decl_exports.removeAssertDiscard(exp.exported_decl);1883 self.decl_exports.removeAssertDiscard(exp.exported_decl);
1905 }1884 }
...@@ -1907,12 +1886,12 @@ fn deleteDeclExports(self: *Module, decl: *Decl) void {...@@ -1907,12 +1886,12 @@ fn deleteDeclExports(self: *Module, decl: *Decl) void {
19071886
1908 self.bin_file.deleteExport(exp.link);1887 self.bin_file.deleteExport(exp.link);
1909 if (self.failed_exports.remove(exp)) |entry| {1888 if (self.failed_exports.remove(exp)) |entry| {
1910 entry.value.destroy(self.allocator);1889 entry.value.destroy(self.gpa);
1911 }1890 }
1912 _ = self.symbol_exports.remove(exp.options.name);1891 _ = self.symbol_exports.remove(exp.options.name);
1913 self.allocator.destroy(exp);1892 self.gpa.destroy(exp);
1914 }1893 }
1915 self.allocator.free(kv.value);1894 self.gpa.free(kv.value);
1916}1895}
19171896
1918fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {1897fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
...@@ -1920,7 +1899,7 @@ fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {...@@ -1920,7 +1899,7 @@ fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
1920 defer tracy.end();1899 defer tracy.end();
19211900
1922 // Use the Decl's arena for function memory.1901 // Use the Decl's arena for function memory.
1923 var arena = decl.typed_value.most_recent.arena.?.promote(self.allocator);1902 var arena = decl.typed_value.most_recent.arena.?.promote(self.gpa);
1924 defer decl.typed_value.most_recent.arena.?.* = arena.state;1903 defer decl.typed_value.most_recent.arena.?.* = arena.state;
1925 var inner_block: Scope.Block = .{1904 var inner_block: Scope.Block = .{
1926 .parent = null,1905 .parent = null,
...@@ -1929,10 +1908,10 @@ fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {...@@ -1929,10 +1908,10 @@ fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
1929 .instructions = .{},1908 .instructions = .{},
1930 .arena = &arena.allocator,1909 .arena = &arena.allocator,
1931 };1910 };
1932 defer inner_block.instructions.deinit(self.allocator);1911 defer inner_block.instructions.deinit(self.gpa);
19331912
1934 const fn_zir = func.analysis.queued;1913 const fn_zir = func.analysis.queued;
1935 defer fn_zir.arena.promote(self.allocator).deinit();1914 defer fn_zir.arena.promote(self.gpa).deinit();
1936 func.analysis = .{ .in_progress = {} };1915 func.analysis = .{ .in_progress = {} };
1937 //std.debug.warn("set {} to in_progress\n", .{decl.name});1916 //std.debug.warn("set {} to in_progress\n", .{decl.name});
19381917
...@@ -1947,7 +1926,7 @@ fn markOutdatedDecl(self: *Module, decl: *Decl) !void {...@@ -1947,7 +1926,7 @@ fn markOutdatedDecl(self: *Module, decl: *Decl) !void {
1947 //std.debug.warn("mark {} outdated\n", .{decl.name});1926 //std.debug.warn("mark {} outdated\n", .{decl.name});
1948 try self.work_queue.writeItem(.{ .analyze_decl = decl });1927 try self.work_queue.writeItem(.{ .analyze_decl = decl });
1949 if (self.failed_decls.remove(decl)) |entry| {1928 if (self.failed_decls.remove(decl)) |entry| {
1950 entry.value.destroy(self.allocator);1929 entry.value.destroy(self.gpa);
1951 }1930 }
1952 decl.analysis = .outdated;1931 decl.analysis = .outdated;
1953}1932}
...@@ -1958,7 +1937,7 @@ fn allocateNewDecl(...@@ -1958,7 +1937,7 @@ fn allocateNewDecl(
1958 src_index: usize,1937 src_index: usize,
1959 contents_hash: std.zig.SrcHash,1938 contents_hash: std.zig.SrcHash,
1960) !*Decl {1939) !*Decl {
1961 const new_decl = try self.allocator.create(Decl);1940 const new_decl = try self.gpa.create(Decl);
1962 new_decl.* = .{1941 new_decl.* = .{
1963 .name = "",1942 .name = "",
1964 .scope = scope.namespace(),1943 .scope = scope.namespace(),
...@@ -1981,10 +1960,10 @@ fn createNewDecl(...@@ -1981,10 +1960,10 @@ fn createNewDecl(
1981 name_hash: Scope.NameHash,1960 name_hash: Scope.NameHash,
1982 contents_hash: std.zig.SrcHash,1961 contents_hash: std.zig.SrcHash,
1983) !*Decl {1962) !*Decl {
1984 try self.decl_table.ensureCapacity(self.decl_table.items().len + 1);1963 try self.decl_table.ensureCapacity(self.gpa, self.decl_table.items().len + 1);
1985 const new_decl = try self.allocateNewDecl(scope, src_index, contents_hash);1964 const new_decl = try self.allocateNewDecl(scope, src_index, contents_hash);
1986 errdefer self.allocator.destroy(new_decl);1965 errdefer self.gpa.destroy(new_decl);
1987 new_decl.name = try mem.dupeZ(self.allocator, u8, decl_name);1966 new_decl.name = try mem.dupeZ(self.gpa, u8, decl_name);
1988 self.decl_table.putAssumeCapacityNoClobber(name_hash, new_decl);1967 self.decl_table.putAssumeCapacityNoClobber(name_hash, new_decl);
1989 return new_decl;1968 return new_decl;
1990}1969}
...@@ -1992,7 +1971,7 @@ fn createNewDecl(...@@ -1992,7 +1971,7 @@ fn createNewDecl(
1992fn analyzeZirDecl(self: *Module, decl: *Decl, src_decl: *zir.Decl) InnerError!bool {1971fn analyzeZirDecl(self: *Module, decl: *Decl, src_decl: *zir.Decl) InnerError!bool {
1993 var decl_scope: Scope.DeclAnalysis = .{1972 var decl_scope: Scope.DeclAnalysis = .{
1994 .decl = decl,1973 .decl = decl,
1995 .arena = std.heap.ArenaAllocator.init(self.allocator),1974 .arena = std.heap.ArenaAllocator.init(self.gpa),
1996 };1975 };
1997 errdefer decl_scope.arena.deinit();1976 errdefer decl_scope.arena.deinit();
19981977
...@@ -2008,7 +1987,7 @@ fn analyzeZirDecl(self: *Module, decl: *Decl, src_decl: *zir.Decl) InnerError!bo...@@ -2008,7 +1987,7 @@ fn analyzeZirDecl(self: *Module, decl: *Decl, src_decl: *zir.Decl) InnerError!bo
2008 prev_type_has_bits = tvm.typed_value.ty.hasCodeGenBits();1987 prev_type_has_bits = tvm.typed_value.ty.hasCodeGenBits();
2009 type_changed = !tvm.typed_value.ty.eql(typed_value.ty);1988 type_changed = !tvm.typed_value.ty.eql(typed_value.ty);
20101989
2011 tvm.deinit(self.allocator);1990 tvm.deinit(self.gpa);
2012 }1991 }
20131992
2014 arena_state.* = decl_scope.arena.state;1993 arena_state.* = decl_scope.arena.state;
...@@ -2146,11 +2125,11 @@ fn analyzeExport(self: *Module, scope: *Scope, src: usize, symbol_name: []const...@@ -2146,11 +2125,11 @@ fn analyzeExport(self: *Module, scope: *Scope, src: usize, symbol_name: []const
2146 else => return self.fail(scope, src, "unable to export type '{}'", .{typed_value.ty}),2125 else => return self.fail(scope, src, "unable to export type '{}'", .{typed_value.ty}),
2147 }2126 }
21482127
2149 try self.decl_exports.ensureCapacity(self.decl_exports.items().len + 1);2128 try self.decl_exports.ensureCapacity(self.gpa, self.decl_exports.items().len + 1);
2150 try self.export_owners.ensureCapacity(self.export_owners.items().len + 1);2129 try self.export_owners.ensureCapacity(self.gpa, self.export_owners.items().len + 1);
21512130
2152 const new_export = try self.allocator.create(Export);2131 const new_export = try self.gpa.create(Export);
2153 errdefer self.allocator.destroy(new_export);2132 errdefer self.gpa.destroy(new_export);
21542133
2155 const owner_decl = scope.decl().?;2134 const owner_decl = scope.decl().?;
21562135
...@@ -2164,27 +2143,27 @@ fn analyzeExport(self: *Module, scope: *Scope, src: usize, symbol_name: []const...@@ -2164,27 +2143,27 @@ fn analyzeExport(self: *Module, scope: *Scope, src: usize, symbol_name: []const
2164 };2143 };
21652144
2166 // Add to export_owners table.2145 // Add to export_owners table.
2167 const eo_gop = self.export_owners.getOrPut(owner_decl) catch unreachable;2146 const eo_gop = self.export_owners.getOrPut(self.gpa, owner_decl) catch unreachable;
2168 if (!eo_gop.found_existing) {2147 if (!eo_gop.found_existing) {
2169 eo_gop.entry.value = &[0]*Export{};2148 eo_gop.entry.value = &[0]*Export{};
2170 }2149 }
2171 eo_gop.entry.value = try self.allocator.realloc(eo_gop.entry.value, eo_gop.entry.value.len + 1);2150 eo_gop.entry.value = try self.gpa.realloc(eo_gop.entry.value, eo_gop.entry.value.len + 1);
2172 eo_gop.entry.value[eo_gop.entry.value.len - 1] = new_export;2151 eo_gop.entry.value[eo_gop.entry.value.len - 1] = new_export;
2173 errdefer eo_gop.entry.value = self.allocator.shrink(eo_gop.entry.value, eo_gop.entry.value.len - 1);2152 errdefer eo_gop.entry.value = self.gpa.shrink(eo_gop.entry.value, eo_gop.entry.value.len - 1);
21742153
2175 // Add to exported_decl table.2154 // Add to exported_decl table.
2176 const de_gop = self.decl_exports.getOrPut(exported_decl) catch unreachable;2155 const de_gop = self.decl_exports.getOrPut(self.gpa, exported_decl) catch unreachable;
2177 if (!de_gop.found_existing) {2156 if (!de_gop.found_existing) {
2178 de_gop.entry.value = &[0]*Export{};2157 de_gop.entry.value = &[0]*Export{};
2179 }2158 }
2180 de_gop.entry.value = try self.allocator.realloc(de_gop.entry.value, de_gop.entry.value.len + 1);2159 de_gop.entry.value = try self.gpa.realloc(de_gop.entry.value, de_gop.entry.value.len + 1);
2181 de_gop.entry.value[de_gop.entry.value.len - 1] = new_export;2160 de_gop.entry.value[de_gop.entry.value.len - 1] = new_export;
2182 errdefer de_gop.entry.value = self.allocator.shrink(de_gop.entry.value, de_gop.entry.value.len - 1);2161 errdefer de_gop.entry.value = self.gpa.shrink(de_gop.entry.value, de_gop.entry.value.len - 1);
21832162
2184 if (self.symbol_exports.get(symbol_name)) |_| {2163 if (self.symbol_exports.get(symbol_name)) |_| {
2185 try self.failed_exports.ensureCapacity(self.failed_exports.items().len + 1);2164 try self.failed_exports.ensureCapacity(self.gpa, self.failed_exports.items().len + 1);
2186 self.failed_exports.putAssumeCapacityNoClobber(new_export, try ErrorMsg.create(2165 self.failed_exports.putAssumeCapacityNoClobber(new_export, try ErrorMsg.create(
2187 self.allocator,2166 self.gpa,
2188 src,2167 src,
2189 "exported symbol collision: {}",2168 "exported symbol collision: {}",
2190 .{symbol_name},2169 .{symbol_name},
...@@ -2198,9 +2177,9 @@ fn analyzeExport(self: *Module, scope: *Scope, src: usize, symbol_name: []const...@@ -2198,9 +2177,9 @@ fn analyzeExport(self: *Module, scope: *Scope, src: usize, symbol_name: []const
2198 self.bin_file.updateDeclExports(self, exported_decl, de_gop.entry.value) catch |err| switch (err) {2177 self.bin_file.updateDeclExports(self, exported_decl, de_gop.entry.value) catch |err| switch (err) {
2199 error.OutOfMemory => return error.OutOfMemory,2178 error.OutOfMemory => return error.OutOfMemory,
2200 else => {2179 else => {
2201 try self.failed_exports.ensureCapacity(self.failed_exports.items().len + 1);2180 try self.failed_exports.ensureCapacity(self.gpa, self.failed_exports.items().len + 1);
2202 self.failed_exports.putAssumeCapacityNoClobber(new_export, try ErrorMsg.create(2181 self.failed_exports.putAssumeCapacityNoClobber(new_export, try ErrorMsg.create(
2203 self.allocator,2182 self.gpa,
2204 src,2183 src,
2205 "unable to export: {}",2184 "unable to export: {}",
2206 .{@errorName(err)},2185 .{@errorName(err)},
...@@ -2224,13 +2203,13 @@ fn addNewInstArgs(...@@ -2224,13 +2203,13 @@ fn addNewInstArgs(
2224}2203}
22252204
2226fn newZIRInst(2205fn newZIRInst(
2227 allocator: *Allocator,2206 gpa: *Allocator,
2228 src: usize,2207 src: usize,
2229 comptime T: type,2208 comptime T: type,
2230 positionals: std.meta.fieldInfo(T, "positionals").field_type,2209 positionals: std.meta.fieldInfo(T, "positionals").field_type,
2231 kw_args: std.meta.fieldInfo(T, "kw_args").field_type,2210 kw_args: std.meta.fieldInfo(T, "kw_args").field_type,
2232) !*zir.Inst {2211) !*zir.Inst {
2233 const inst = try allocator.create(T);2212 const inst = try gpa.create(T);
2234 inst.* = .{2213 inst.* = .{
2235 .base = .{2214 .base = .{
2236 .tag = T.base_tag,2215 .tag = T.base_tag,
...@@ -2273,7 +2252,7 @@ fn addNewInst(self: *Module, block: *Scope.Block, src: usize, ty: Type, comptime...@@ -2273,7 +2252,7 @@ fn addNewInst(self: *Module, block: *Scope.Block, src: usize, ty: Type, comptime
2273 },2252 },
2274 .args = undefined,2253 .args = undefined,
2275 };2254 };
2276 try block.instructions.append(self.allocator, &inst.base);2255 try block.instructions.append(self.gpa, &inst.base);
2277 return inst;2256 return inst;
2278}2257}
22792258
...@@ -2433,7 +2412,7 @@ fn analyzeInst(self: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*In...@@ -2433,7 +2412,7 @@ fn analyzeInst(self: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*In
2433fn analyzeInstStr(self: *Module, scope: *Scope, str_inst: *zir.Inst.Str) InnerError!*Inst {2412fn analyzeInstStr(self: *Module, scope: *Scope, str_inst: *zir.Inst.Str) InnerError!*Inst {
2434 // The bytes references memory inside the ZIR module, which can get deallocated2413 // The bytes references memory inside the ZIR module, which can get deallocated
2435 // after semantic analysis is complete. We need the memory to be in the new anonymous Decl's arena.2414 // after semantic analysis is complete. We need the memory to be in the new anonymous Decl's arena.
2436 var new_decl_arena = std.heap.ArenaAllocator.init(self.allocator);2415 var new_decl_arena = std.heap.ArenaAllocator.init(self.gpa);
2437 const arena_bytes = try new_decl_arena.allocator.dupe(u8, str_inst.positionals.bytes);2416 const arena_bytes = try new_decl_arena.allocator.dupe(u8, str_inst.positionals.bytes);
24382417
2439 const ty_payload = try scope.arena().create(Type.Payload.Array_u8_Sentinel0);2418 const ty_payload = try scope.arena().create(Type.Payload.Array_u8_Sentinel0);
...@@ -2457,8 +2436,8 @@ fn createAnonymousDecl(...@@ -2457,8 +2436,8 @@ fn createAnonymousDecl(
2457) !*Decl {2436) !*Decl {
2458 const name_index = self.getNextAnonNameIndex();2437 const name_index = self.getNextAnonNameIndex();
2459 const scope_decl = scope.decl().?;2438 const scope_decl = scope.decl().?;
2460 const name = try std.fmt.allocPrint(self.allocator, "{}${}", .{ scope_decl.name, name_index });2439 const name = try std.fmt.allocPrint(self.gpa, "{}${}", .{ scope_decl.name, name_index });
2461 defer self.allocator.free(name);2440 defer self.gpa.free(name);
2462 const name_hash = scope.namespace().fullyQualifiedNameHash(name);2441 const name_hash = scope.namespace().fullyQualifiedNameHash(name);
2463 const src_hash: std.zig.SrcHash = undefined;2442 const src_hash: std.zig.SrcHash = undefined;
2464 const new_decl = try self.createNewDecl(scope, name, scope_decl.src_index, name_hash, src_hash);2443 const new_decl = try self.createNewDecl(scope, name, scope_decl.src_index, name_hash, src_hash);
...@@ -2554,8 +2533,8 @@ fn analyzeInstBlock(self: *Module, scope: *Scope, inst: *zir.Inst.Block) InnerEr...@@ -2554,8 +2533,8 @@ fn analyzeInstBlock(self: *Module, scope: *Scope, inst: *zir.Inst.Block) InnerEr
2554 };2533 };
2555 const label = &child_block.label.?;2534 const label = &child_block.label.?;
25562535
2557 defer child_block.instructions.deinit(self.allocator);2536 defer child_block.instructions.deinit(self.gpa);
2558 defer label.results.deinit(self.allocator);2537 defer label.results.deinit(self.gpa);
25592538
2560 try self.analyzeBody(&child_block.base, inst.positionals.body);2539 try self.analyzeBody(&child_block.base, inst.positionals.body);
25612540
...@@ -2567,7 +2546,7 @@ fn analyzeInstBlock(self: *Module, scope: *Scope, inst: *zir.Inst.Block) InnerEr...@@ -2567,7 +2546,7 @@ fn analyzeInstBlock(self: *Module, scope: *Scope, inst: *zir.Inst.Block) InnerEr
2567 // No need to add the Block instruction; we can add the instructions to the parent block directly.2546 // No need to add the Block instruction; we can add the instructions to the parent block directly.
2568 // Blocks are terminated with a noreturn instruction which we do not want to include.2547 // Blocks are terminated with a noreturn instruction which we do not want to include.
2569 const instrs = child_block.instructions.items;2548 const instrs = child_block.instructions.items;
2570 try parent_block.instructions.appendSlice(self.allocator, instrs[0 .. instrs.len - 1]);2549 try parent_block.instructions.appendSlice(self.gpa, instrs[0 .. instrs.len - 1]);
2571 if (label.results.items.len == 1) {2550 if (label.results.items.len == 1) {
2572 return label.results.items[0];2551 return label.results.items[0];
2573 } else {2552 } else {
...@@ -2577,7 +2556,7 @@ fn analyzeInstBlock(self: *Module, scope: *Scope, inst: *zir.Inst.Block) InnerEr...@@ -2577,7 +2556,7 @@ fn analyzeInstBlock(self: *Module, scope: *Scope, inst: *zir.Inst.Block) InnerEr
25772556
2578 // Need to set the type and emit the Block instruction. This allows machine code generation2557 // Need to set the type and emit the Block instruction. This allows machine code generation
2579 // to emit a jump instruction to after the block when it encounters the break.2558 // to emit a jump instruction to after the block when it encounters the break.
2580 try parent_block.instructions.append(self.allocator, &block_inst.base);2559 try parent_block.instructions.append(self.gpa, &block_inst.base);
2581 block_inst.base.ty = try self.resolvePeerTypes(scope, label.results.items);2560 block_inst.base.ty = try self.resolvePeerTypes(scope, label.results.items);
2582 block_inst.args.body = .{ .instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items) };2561 block_inst.args.body = .{ .instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items) };
2583 return &block_inst.base;2562 return &block_inst.base;
...@@ -2596,7 +2575,7 @@ fn analyzeInstBreakVoid(self: *Module, scope: *Scope, inst: *zir.Inst.BreakVoid)...@@ -2596,7 +2575,7 @@ fn analyzeInstBreakVoid(self: *Module, scope: *Scope, inst: *zir.Inst.BreakVoid)
2596 while (opt_block) |block| {2575 while (opt_block) |block| {
2597 if (block.label) |*label| {2576 if (block.label) |*label| {
2598 if (mem.eql(u8, label.name, label_name)) {2577 if (mem.eql(u8, label.name, label_name)) {
2599 try label.results.append(self.allocator, void_inst);2578 try label.results.append(self.gpa, void_inst);
2600 return self.constNoReturn(scope, inst.base.src);2579 return self.constNoReturn(scope, inst.base.src);
2601 }2580 }
2602 }2581 }
...@@ -2719,8 +2698,8 @@ fn analyzeInstCall(self: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerErro...@@ -2719,8 +2698,8 @@ fn analyzeInstCall(self: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerErro
27192698
2720 // TODO handle function calls of generic functions2699 // TODO handle function calls of generic functions
27212700
2722 const fn_param_types = try self.allocator.alloc(Type, fn_params_len);2701 const fn_param_types = try self.gpa.alloc(Type, fn_params_len);
2723 defer self.allocator.free(fn_param_types);2702 defer self.gpa.free(fn_param_types);
2724 func.ty.fnParamTypes(fn_param_types);2703 func.ty.fnParamTypes(fn_param_types);
27252704
2726 const casted_args = try scope.arena().alloc(*Inst, fn_params_len);2705 const casted_args = try scope.arena().alloc(*Inst, fn_params_len);
...@@ -2739,7 +2718,7 @@ fn analyzeInstCall(self: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerErro...@@ -2739,7 +2718,7 @@ fn analyzeInstCall(self: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerErro
2739fn analyzeInstFn(self: *Module, scope: *Scope, fn_inst: *zir.Inst.Fn) InnerError!*Inst {2718fn analyzeInstFn(self: *Module, scope: *Scope, fn_inst: *zir.Inst.Fn) InnerError!*Inst {
2740 const fn_type = try self.resolveType(scope, fn_inst.positionals.fn_type);2719 const fn_type = try self.resolveType(scope, fn_inst.positionals.fn_type);
2741 const fn_zir = blk: {2720 const fn_zir = blk: {
2742 var fn_arena = std.heap.ArenaAllocator.init(self.allocator);2721 var fn_arena = std.heap.ArenaAllocator.init(self.gpa);
2743 errdefer fn_arena.deinit();2722 errdefer fn_arena.deinit();
27442723
2745 const fn_zir = try scope.arena().create(Fn.ZIR);2724 const fn_zir = try scope.arena().create(Fn.ZIR);
...@@ -3120,7 +3099,7 @@ fn analyzeInstCondBr(self: *Module, scope: *Scope, inst: *zir.Inst.CondBr) Inner...@@ -3120,7 +3099,7 @@ fn analyzeInstCondBr(self: *Module, scope: *Scope, inst: *zir.Inst.CondBr) Inner
3120 .instructions = .{},3099 .instructions = .{},
3121 .arena = parent_block.arena,3100 .arena = parent_block.arena,
3122 };3101 };
3123 defer true_block.instructions.deinit(self.allocator);3102 defer true_block.instructions.deinit(self.gpa);
3124 try self.analyzeBody(&true_block.base, inst.positionals.true_body);3103 try self.analyzeBody(&true_block.base, inst.positionals.true_body);
31253104
3126 var false_block: Scope.Block = .{3105 var false_block: Scope.Block = .{
...@@ -3130,7 +3109,7 @@ fn analyzeInstCondBr(self: *Module, scope: *Scope, inst: *zir.Inst.CondBr) Inner...@@ -3130,7 +3109,7 @@ fn analyzeInstCondBr(self: *Module, scope: *Scope, inst: *zir.Inst.CondBr) Inner
3130 .instructions = .{},3109 .instructions = .{},
3131 .arena = parent_block.arena,3110 .arena = parent_block.arena,
3132 };3111 };
3133 defer false_block.instructions.deinit(self.allocator);3112 defer false_block.instructions.deinit(self.gpa);
3134 try self.analyzeBody(&false_block.base, inst.positionals.false_body);3113 try self.analyzeBody(&false_block.base, inst.positionals.false_body);
31353114
3136 return self.addNewInstArgs(parent_block, inst.base.src, Type.initTag(.void), Inst.CondBr, Inst.Args(Inst.CondBr){3115 return self.addNewInstArgs(parent_block, inst.base.src, Type.initTag(.void), Inst.CondBr, Inst.Args(Inst.CondBr){
...@@ -3284,7 +3263,7 @@ fn cmpNumeric(...@@ -3284,7 +3263,7 @@ fn cmpNumeric(
3284 return self.constUndef(scope, src, Type.initTag(.bool));3263 return self.constUndef(scope, src, Type.initTag(.bool));
3285 const is_unsigned = if (lhs_is_float) x: {3264 const is_unsigned = if (lhs_is_float) x: {
3286 var bigint_space: Value.BigIntSpace = undefined;3265 var bigint_space: Value.BigIntSpace = undefined;
3287 var bigint = try lhs_val.toBigInt(&bigint_space).toManaged(self.allocator);3266 var bigint = try lhs_val.toBigInt(&bigint_space).toManaged(self.gpa);
3288 defer bigint.deinit();3267 defer bigint.deinit();
3289 const zcmp = lhs_val.orderAgainstZero();3268 const zcmp = lhs_val.orderAgainstZero();
3290 if (lhs_val.floatHasFraction()) {3269 if (lhs_val.floatHasFraction()) {
...@@ -3319,7 +3298,7 @@ fn cmpNumeric(...@@ -3319,7 +3298,7 @@ fn cmpNumeric(
3319 return self.constUndef(scope, src, Type.initTag(.bool));3298 return self.constUndef(scope, src, Type.initTag(.bool));
3320 const is_unsigned = if (rhs_is_float) x: {3299 const is_unsigned = if (rhs_is_float) x: {
3321 var bigint_space: Value.BigIntSpace = undefined;3300 var bigint_space: Value.BigIntSpace = undefined;
3322 var bigint = try rhs_val.toBigInt(&bigint_space).toManaged(self.allocator);3301 var bigint = try rhs_val.toBigInt(&bigint_space).toManaged(self.gpa);
3323 defer bigint.deinit();3302 defer bigint.deinit();
3324 const zcmp = rhs_val.orderAgainstZero();3303 const zcmp = rhs_val.orderAgainstZero();
3325 if (rhs_val.floatHasFraction()) {3304 if (rhs_val.floatHasFraction()) {
...@@ -3457,7 +3436,7 @@ fn coerceArrayPtrToSlice(self: *Module, scope: *Scope, dest_type: Type, inst: *I...@@ -3457,7 +3436,7 @@ fn coerceArrayPtrToSlice(self: *Module, scope: *Scope, dest_type: Type, inst: *I
34573436
3458fn fail(self: *Module, scope: *Scope, src: usize, comptime format: []const u8, args: var) InnerError {3437fn fail(self: *Module, scope: *Scope, src: usize, comptime format: []const u8, args: var) InnerError {
3459 @setCold(true);3438 @setCold(true);
3460 const err_msg = try ErrorMsg.create(self.allocator, src, format, args);3439 const err_msg = try ErrorMsg.create(self.gpa, src, format, args);
3461 return self.failWithOwnedErrorMsg(scope, src, err_msg);3440 return self.failWithOwnedErrorMsg(scope, src, err_msg);
3462}3441}
34633442
...@@ -3487,9 +3466,9 @@ fn failNode(...@@ -3487,9 +3466,9 @@ fn failNode(
34873466
3488fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *ErrorMsg) InnerError {3467fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *ErrorMsg) InnerError {
3489 {3468 {
3490 errdefer err_msg.destroy(self.allocator);3469 errdefer err_msg.destroy(self.gpa);
3491 try self.failed_decls.ensureCapacity(self.failed_decls.items().len + 1);3470 try self.failed_decls.ensureCapacity(self.gpa, self.failed_decls.items().len + 1);
3492 try self.failed_files.ensureCapacity(self.failed_files.items().len + 1);3471 try self.failed_files.ensureCapacity(self.gpa, self.failed_files.items().len + 1);
3493 }3472 }
3494 switch (scope.tag) {3473 switch (scope.tag) {
3495 .decl => {3474 .decl => {
...@@ -3542,28 +3521,28 @@ pub const ErrorMsg = struct {...@@ -3542,28 +3521,28 @@ pub const ErrorMsg = struct {
3542 byte_offset: usize,3521 byte_offset: usize,
3543 msg: []const u8,3522 msg: []const u8,
35443523
3545 pub fn create(allocator: *Allocator, byte_offset: usize, comptime format: []const u8, args: var) !*ErrorMsg {3524 pub fn create(gpa: *Allocator, byte_offset: usize, comptime format: []const u8, args: var) !*ErrorMsg {
3546 const self = try allocator.create(ErrorMsg);3525 const self = try gpa.create(ErrorMsg);
3547 errdefer allocator.destroy(self);3526 errdefer gpa.destroy(self);
3548 self.* = try init(allocator, byte_offset, format, args);3527 self.* = try init(gpa, byte_offset, format, args);
3549 return self;3528 return self;
3550 }3529 }
35513530
3552 /// Assumes the ErrorMsg struct and msg were both allocated with allocator.3531 /// Assumes the ErrorMsg struct and msg were both allocated with allocator.
3553 pub fn destroy(self: *ErrorMsg, allocator: *Allocator) void {3532 pub fn destroy(self: *ErrorMsg, gpa: *Allocator) void {
3554 self.deinit(allocator);3533 self.deinit(gpa);
3555 allocator.destroy(self);3534 gpa.destroy(self);
3556 }3535 }
35573536
3558 pub fn init(allocator: *Allocator, byte_offset: usize, comptime format: []const u8, args: var) !ErrorMsg {3537 pub fn init(gpa: *Allocator, byte_offset: usize, comptime format: []const u8, args: var) !ErrorMsg {
3559 return ErrorMsg{3538 return ErrorMsg{
3560 .byte_offset = byte_offset,3539 .byte_offset = byte_offset,
3561 .msg = try std.fmt.allocPrint(allocator, format, args),3540 .msg = try std.fmt.allocPrint(gpa, format, args),
3562 };3541 };
3563 }3542 }
35643543
3565 pub fn deinit(self: *ErrorMsg, allocator: *Allocator) void {3544 pub fn deinit(self: *ErrorMsg, gpa: *Allocator) void {
3566 allocator.free(self.msg);3545 gpa.free(self.msg);
3567 self.* = undefined;3546 self.* = undefined;
3568 }3547 }
3569};3548};
src-self-hosted/codegen.zig+186-65
...@@ -46,7 +46,14 @@ pub fn generateSymbol(...@@ -46,7 +46,14 @@ pub fn generateSymbol(
46 var mc_args = try std.ArrayList(Function.MCValue).initCapacity(bin_file.allocator, param_types.len);46 var mc_args = try std.ArrayList(Function.MCValue).initCapacity(bin_file.allocator, param_types.len);
47 defer mc_args.deinit();47 defer mc_args.deinit();
4848
49 var next_stack_offset: u64 = 0;49 var branch_stack = std.ArrayList(Function.Branch).init(bin_file.allocator);
50 defer {
51 assert(branch_stack.items.len == 1);
52 branch_stack.items[0].deinit(bin_file.allocator);
53 branch_stack.deinit();
54 }
55 const branch = try branch_stack.addOne();
56 branch.* = .{};
5057
51 switch (fn_type.fnCallingConvention()) {58 switch (fn_type.fnCallingConvention()) {
52 .Naked => assert(mc_args.items.len == 0),59 .Naked => assert(mc_args.items.len == 0),
...@@ -61,8 +68,8 @@ pub fn generateSymbol(...@@ -61,8 +68,8 @@ pub fn generateSymbol(
61 switch (param_type.zigTypeTag()) {68 switch (param_type.zigTypeTag()) {
62 .Bool, .Int => {69 .Bool, .Int => {
63 if (next_int_reg >= integer_registers.len) {70 if (next_int_reg >= integer_registers.len) {
64 try mc_args.append(.{ .stack_offset = next_stack_offset });71 try mc_args.append(.{ .stack_offset = branch.next_stack_offset });
65 next_stack_offset += param_type.abiSize(bin_file.options.target);72 branch.next_stack_offset += @intCast(u32, param_type.abiSize(bin_file.options.target));
66 } else {73 } else {
67 try mc_args.append(.{ .register = @enumToInt(integer_registers[next_int_reg]) });74 try mc_args.append(.{ .register = @enumToInt(integer_registers[next_int_reg]) });
68 next_int_reg += 1;75 next_int_reg += 1;
...@@ -100,23 +107,17 @@ pub fn generateSymbol(...@@ -100,23 +107,17 @@ pub fn generateSymbol(
100 }107 }
101108
102 var function = Function{109 var function = Function{
110 .gpa = bin_file.allocator,
103 .target = &bin_file.options.target,111 .target = &bin_file.options.target,
104 .bin_file = bin_file,112 .bin_file = bin_file,
105 .mod_fn = module_fn,113 .mod_fn = module_fn,
106 .code = code,114 .code = code,
107 .err_msg = null,115 .err_msg = null,
108 .args = mc_args.items,116 .args = mc_args.items,
109 .branch_stack = .{},117 .branch_stack = &branch_stack,
110 };118 };
111 defer {
112 assert(function.branch_stack.items.len == 1);
113 function.branch_stack.items[0].inst_table.deinit();
114 function.branch_stack.deinit(bin_file.allocator);
115 }
116 try function.branch_stack.append(bin_file.allocator, .{
117 .inst_table = std.AutoHashMap(*ir.Inst, Function.MCValue).init(bin_file.allocator),
118 });
119119
120 branch.max_end_stack = branch.next_stack_offset;
120 function.gen() catch |err| switch (err) {121 function.gen() catch |err| switch (err) {
121 error.CodegenFail => return Result{ .fail = function.err_msg.? },122 error.CodegenFail => return Result{ .fail = function.err_msg.? },
122 else => |e| return e,123 else => |e| return e,
...@@ -218,6 +219,7 @@ pub fn generateSymbol(...@@ -218,6 +219,7 @@ pub fn generateSymbol(
218}219}
219220
220const Function = struct {221const Function = struct {
222 gpa: *Allocator,
221 bin_file: *link.ElfFile,223 bin_file: *link.ElfFile,
222 target: *const std.Target,224 target: *const std.Target,
223 mod_fn: *const Module.Fn,225 mod_fn: *const Module.Fn,
...@@ -232,10 +234,37 @@ const Function = struct {...@@ -232,10 +234,37 @@ const Function = struct {
232 /// within different branches. Special consideration is needed when a branch234 /// within different branches. Special consideration is needed when a branch
233 /// joins with its parent, to make sure all instructions have the same MCValue235 /// joins with its parent, to make sure all instructions have the same MCValue
234 /// across each runtime branch upon joining.236 /// across each runtime branch upon joining.
235 branch_stack: std.ArrayListUnmanaged(Branch),237 branch_stack: *std.ArrayList(Branch),
236238
237 const Branch = struct {239 const Branch = struct {
238 inst_table: std.AutoHashMap(*ir.Inst, MCValue),240 inst_table: std.AutoHashMapUnmanaged(*ir.Inst, MCValue) = .{},
241
242 /// The key is an enum value of an arch-specific register.
243 registers: std.AutoHashMapUnmanaged(usize, RegisterAllocation) = .{},
244
245 /// Maps offset to what is stored there.
246 stack: std.AutoHashMapUnmanaged(usize, StackAllocation) = .{},
247 /// Offset from the stack base, representing the end of the stack frame.
248 max_end_stack: u32 = 0,
249 /// Represents the current end stack offset. If there is no existing slot
250 /// to place a new stack allocation, it goes here, and then bumps `max_end_stack`.
251 next_stack_offset: u32 = 0,
252
253 fn deinit(self: *Branch, gpa: *Allocator) void {
254 self.inst_table.deinit(gpa);
255 self.registers.deinit(gpa);
256 self.stack.deinit(gpa);
257 self.* = undefined;
258 }
259 };
260
261 const RegisterAllocation = struct {
262 inst: *ir.Inst,
263 };
264
265 const StackAllocation = struct {
266 inst: *ir.Inst,
267 size: u32,
239 };268 };
240269
241 const MCValue = union(enum) {270 const MCValue = union(enum) {
...@@ -256,6 +285,13 @@ const Function = struct {...@@ -256,6 +285,13 @@ const Function = struct {
256 memory: u64,285 memory: u64,
257 /// The value is one of the stack variables.286 /// The value is one of the stack variables.
258 stack_offset: u64,287 stack_offset: u64,
288
289 fn isMemory(mcv: MCValue) bool {
290 return switch (mcv) {
291 .embedded_in_code, .memory, .stack_offset => true,
292 else => false,
293 };
294 }
259 };295 };
260296
261 fn gen(self: *Function) !void {297 fn gen(self: *Function) !void {
...@@ -318,7 +354,7 @@ const Function = struct {...@@ -318,7 +354,7 @@ const Function = struct {
318 const inst_table = &self.branch_stack.items[0].inst_table;354 const inst_table = &self.branch_stack.items[0].inst_table;
319 for (self.mod_fn.analysis.success.instructions) |inst| {355 for (self.mod_fn.analysis.success.instructions) |inst| {
320 const new_inst = try self.genFuncInst(inst, arch);356 const new_inst = try self.genFuncInst(inst, arch);
321 try inst_table.putNoClobber(inst, new_inst);357 try inst_table.putNoClobber(self.gpa, inst, new_inst);
322 }358 }
323 }359 }
324360
...@@ -344,19 +380,99 @@ const Function = struct {...@@ -344,19 +380,99 @@ const Function = struct {
344 }380 }
345381
346 fn genAdd(self: *Function, inst: *ir.Inst.Add, comptime arch: std.Target.Cpu.Arch) !MCValue {382 fn genAdd(self: *Function, inst: *ir.Inst.Add, comptime arch: std.Target.Cpu.Arch) !MCValue {
347 const lhs = try self.resolveInst(inst.args.lhs);383 // No side effects, so if it's unreferenced, do nothing.
348 const rhs = try self.resolveInst(inst.args.rhs);384 if (inst.base.isUnused())
385 return MCValue.dead;
349 switch (arch) {386 switch (arch) {
350 .i386, .x86_64 => {387 .x86_64 => {
351 // const lhs_reg = try self.instAsReg(lhs);388 // Biggest encoding of ADD is 8 bytes.
352 // const rhs_reg = try self.instAsReg(rhs);389 try self.code.ensureCapacity(self.code.items.len + 8);
353 // const result = try self.allocateReg();390
354391 // In x86, ADD has 2 operands, destination and source.
355 // try self.code.append(??);392 // Either one, but not both, can be a memory operand.
393 // Source operand can be an immediate, 8 bits or 32 bits.
394 // So, if either one of the operands dies with this instruction, we can use it
395 // as the result MCValue.
396 var dst_mcv: MCValue = undefined;
397 var src_mcv: MCValue = undefined;
398 if (inst.base.operandDies(0)) {
399 // LHS dies; use it as the destination.
400 dst_mcv = try self.resolveInst(inst.args.lhs);
401 // Both operands cannot be memory.
402 if (dst_mcv.isMemory()) {
403 src_mcv = try self.resolveInstImmOrReg(inst.args.rhs);
404 } else {
405 src_mcv = try self.resolveInst(inst.args.rhs);
406 }
407 } else if (inst.base.operandDies(1)) {
408 // RHS dies; use it as the destination.
409 dst_mcv = try self.resolveInst(inst.args.rhs);
410 // Both operands cannot be memory.
411 if (dst_mcv.isMemory()) {
412 src_mcv = try self.resolveInstImmOrReg(inst.args.lhs);
413 } else {
414 src_mcv = try self.resolveInst(inst.args.lhs);
415 }
416 } else {
417 const lhs = try self.resolveInst(inst.args.lhs);
418 const rhs = try self.resolveInst(inst.args.rhs);
419 if (lhs.isMemory()) {
420 dst_mcv = try self.copyToNewRegister(inst.base.src, lhs);
421 src_mcv = rhs;
422 } else {
423 dst_mcv = try self.copyToNewRegister(inst.base.src, rhs);
424 src_mcv = lhs;
425 }
426 }
427 // x86 ADD supports only signed 32-bit immediates at most. If the immediate
428 // value is larger than this, we put it in a register.
429 // A potential opportunity for future optimization here would be keeping track
430 // of the fact that the instruction is available both as an immediate
431 // and as a register.
432 switch (src_mcv) {
433 .immediate => |imm| {
434 if (imm > std.math.maxInt(u31)) {
435 src_mcv = try self.copyToNewRegister(inst.base.src, src_mcv);
436 }
437 },
438 else => {},
439 }
356440
357 // lhs_reg.release();441 switch (dst_mcv) {
358 // rhs_reg.release();442 .none => unreachable,
359 return self.fail(inst.base.src, "TODO implement register allocation", .{});443 .dead, .unreach, .immediate => unreachable,
444 .register => |dst_reg_usize| {
445 const dst_reg = @intToEnum(Reg(arch), @intCast(@TagType(Reg(arch)), dst_reg_usize));
446 switch (src_mcv) {
447 .none => unreachable,
448 .dead, .unreach => unreachable,
449 .register => |src_reg_usize| {
450 const src_reg = @intToEnum(Reg(arch), @intCast(@TagType(Reg(arch)), src_reg_usize));
451 self.rex(.{ .b = dst_reg.isExtended(), .r = src_reg.isExtended(), .w = dst_reg.size() == 64 });
452 self.code.appendSliceAssumeCapacity(&[_]u8{ 0x1, 0xC0 | (@as(u8, src_reg.id() & 0b111) << 3) | @as(u8, dst_reg.id() & 0b111) });
453 },
454 .immediate => |imm| {
455 const imm32 = @intCast(u31, imm); // We handle this case above.
456 // 81 /0 id
457 if (imm32 <= std.math.maxInt(u7)) {
458 self.rex(.{ .b = dst_reg.isExtended(), .w = dst_reg.size() == 64 });
459 self.code.appendSliceAssumeCapacity(&[_]u8{ 0x83, 0xC0 | @as(u8, dst_reg.id() & 0b111), @intCast(u8, imm32)});
460 } else {
461 self.rex(.{ .r = dst_reg.isExtended(), .w = dst_reg.size() == 64 });
462 self.code.appendSliceAssumeCapacity(&[_]u8{ 0x81, 0xC0 | @as(u8, dst_reg.id() & 0b111) });
463 std.mem.writeIntLittle(u32, self.code.addManyAsArrayAssumeCapacity(4), imm32);
464 }
465 },
466 .embedded_in_code, .memory, .stack_offset => {
467 return self.fail(inst.base.src, "TODO implement x86 add source memory", .{});
468 },
469 }
470 },
471 .embedded_in_code, .memory, .stack_offset => {
472 return self.fail(inst.base.src, "TODO implement x86 add destination memory", .{});
473 },
474 }
475 return dst_mcv;
360 },476 },
361 else => return self.fail(inst.base.src, "TODO implement add for {}", .{self.target.cpu.arch}),477 else => return self.fail(inst.base.src, "TODO implement add for {}", .{self.target.cpu.arch}),
362 }478 }
...@@ -526,23 +642,23 @@ const Function = struct {...@@ -526,23 +642,23 @@ const Function = struct {
526 /// resulting REX is meaningful, but will remain the same if it is not.642 /// resulting REX is meaningful, but will remain the same if it is not.
527 /// * Deliberately inserting a "meaningless REX" requires explicit usage of643 /// * Deliberately inserting a "meaningless REX" requires explicit usage of
528 /// 0x40, and cannot be done via this function.644 /// 0x40, and cannot be done via this function.
529 fn REX(self: *Function, arg: struct { B: bool = false, W: bool = false, X: bool = false, R: bool = false }) !void {645 fn rex(self: *Function, arg: struct { b: bool = false, w: bool = false, x: bool = false, r: bool = false }) void {
530 // From section 2.2.1.2 of the manual, REX is encoded as b0100WRXB.646 // From section 2.2.1.2 of the manual, REX is encoded as b0100WRXB.
531 var value: u8 = 0x40;647 var value: u8 = 0x40;
532 if (arg.B) {648 if (arg.b) {
533 value |= 0x1;649 value |= 0x1;
534 }650 }
535 if (arg.X) {651 if (arg.x) {
536 value |= 0x2;652 value |= 0x2;
537 }653 }
538 if (arg.R) {654 if (arg.r) {
539 value |= 0x4;655 value |= 0x4;
540 }656 }
541 if (arg.W) {657 if (arg.w) {
542 value |= 0x8;658 value |= 0x8;
543 }659 }
544 if (value != 0x40) {660 if (value != 0x40) {
545 try self.code.append(value);661 self.code.appendAssumeCapacity(value);
546 }662 }
547 }663 }
548664
...@@ -570,11 +686,11 @@ const Function = struct {...@@ -570,11 +686,11 @@ const Function = struct {
570 // If we're accessing e.g. r8d, we need to use a REX prefix before the actual operation. Since686 // If we're accessing e.g. r8d, we need to use a REX prefix before the actual operation. Since
571 // this is a 32-bit operation, the W flag is set to zero. X is also zero, as we're not using a SIB.687 // this is a 32-bit operation, the W flag is set to zero. X is also zero, as we're not using a SIB.
572 // Both R and B are set, as we're extending, in effect, the register bits *and* the operand.688 // Both R and B are set, as we're extending, in effect, the register bits *and* the operand.
573 try self.REX(.{ .R = reg.isExtended(), .B = reg.isExtended() });689 try self.code.ensureCapacity(self.code.items.len + 3);
690 self.rex(.{ .r = reg.isExtended(), .b = reg.isExtended() });
574 const id = @as(u8, reg.id() & 0b111);691 const id = @as(u8, reg.id() & 0b111);
575 return self.code.appendSlice(&[_]u8{692 self.code.appendSliceAssumeCapacity(&[_]u8{ 0x31, 0xC0 | id << 3 | id });
576 0x31, 0xC0 | id << 3 | id,693 return;
577 });
578 }694 }
579 if (x <= std.math.maxInt(u32)) {695 if (x <= std.math.maxInt(u32)) {
580 // Next best case: if we set the lower four bytes, the upper four will be zeroed.696 // Next best case: if we set the lower four bytes, the upper four will be zeroed.
...@@ -607,9 +723,9 @@ const Function = struct {...@@ -607,9 +723,9 @@ const Function = struct {
607 // Since we always need a REX here, let's just check if we also need to set REX.B.723 // Since we always need a REX here, let's just check if we also need to set REX.B.
608 //724 //
609 // In this case, the encoding of the REX byte is 0b0100100B725 // In this case, the encoding of the REX byte is 0b0100100B
610726 try self.code.ensureCapacity(self.code.items.len + 10);
611 try self.REX(.{ .W = true, .B = reg.isExtended() });727 self.rex(.{ .w = true, .b = reg.isExtended() });
612 try self.code.resize(self.code.items.len + 9);728 self.code.items.len += 9;
613 self.code.items[self.code.items.len - 9] = 0xB8 | @as(u8, reg.id() & 0b111);729 self.code.items[self.code.items.len - 9] = 0xB8 | @as(u8, reg.id() & 0b111);
614 const imm_ptr = self.code.items[self.code.items.len - 8 ..][0..8];730 const imm_ptr = self.code.items[self.code.items.len - 8 ..][0..8];
615 mem.writeIntLittle(u64, imm_ptr, x);731 mem.writeIntLittle(u64, imm_ptr, x);
...@@ -620,13 +736,13 @@ const Function = struct {...@@ -620,13 +736,13 @@ const Function = struct {
620 }736 }
621 // We need the offset from RIP in a signed i32 twos complement.737 // We need the offset from RIP in a signed i32 twos complement.
622 // The instruction is 7 bytes long and RIP points to the next instruction.738 // The instruction is 7 bytes long and RIP points to the next instruction.
623 //739 try self.code.ensureCapacity(self.code.items.len + 7);
624 // 64-bit LEA is encoded as REX.W 8D /r. If the register is extended, the REX byte is modified,740 // 64-bit LEA is encoded as REX.W 8D /r. If the register is extended, the REX byte is modified,
625 // but the operation size is unchanged. Since we're using a disp32, we want mode 0 and lower three741 // but the operation size is unchanged. Since we're using a disp32, we want mode 0 and lower three
626 // bits as five.742 // bits as five.
627 // REX 0x8D 0b00RRR101, where RRR is the lower three bits of the id.743 // REX 0x8D 0b00RRR101, where RRR is the lower three bits of the id.
628 try self.REX(.{ .W = true, .B = reg.isExtended() });744 self.rex(.{ .w = true, .b = reg.isExtended() });
629 try self.code.resize(self.code.items.len + 6);745 self.code.items.len += 6;
630 const rip = self.code.items.len;746 const rip = self.code.items.len;
631 const big_offset = @intCast(i64, code_offset) - @intCast(i64, rip);747 const big_offset = @intCast(i64, code_offset) - @intCast(i64, rip);
632 const offset = @intCast(i32, big_offset);748 const offset = @intCast(i32, big_offset);
...@@ -646,9 +762,10 @@ const Function = struct {...@@ -646,9 +762,10 @@ const Function = struct {
646 // If the *source* is extended, the B field must be 1.762 // If the *source* is extended, the B field must be 1.
647 // Since the register is being accessed directly, the R/M mode is three. The reg field (the middle763 // Since the register is being accessed directly, the R/M mode is three. The reg field (the middle
648 // three bits) contain the destination, and the R/M field (the lower three bits) contain the source.764 // three bits) contain the destination, and the R/M field (the lower three bits) contain the source.
649 try self.REX(.{ .W = true, .R = reg.isExtended(), .B = src_reg.isExtended() });765 try self.code.ensureCapacity(self.code.items.len + 3);
766 self.rex(.{ .w = true, .r = reg.isExtended(), .b = src_reg.isExtended() });
650 const R = 0xC0 | (@as(u8, reg.id() & 0b111) << 3) | @as(u8, src_reg.id() & 0b111);767 const R = 0xC0 | (@as(u8, reg.id() & 0b111) << 3) | @as(u8, src_reg.id() & 0b111);
651 try self.code.appendSlice(&[_]u8{ 0x8B, R });768 self.code.appendSliceAssumeCapacity(&[_]u8{ 0x8B, R });
652 },769 },
653 .memory => |x| {770 .memory => |x| {
654 if (reg.size() != 64) {771 if (reg.size() != 64) {
...@@ -662,14 +779,14 @@ const Function = struct {...@@ -662,14 +779,14 @@ const Function = struct {
662 // The SIB must be 0x25, to indicate a disp32 with no scaled index.779 // The SIB must be 0x25, to indicate a disp32 with no scaled index.
663 // 0b00RRR100, where RRR is the lower three bits of the register ID.780 // 0b00RRR100, where RRR is the lower three bits of the register ID.
664 // The instruction is thus eight bytes; REX 0x8B 0b00RRR100 0x25 followed by a four-byte disp32.781 // The instruction is thus eight bytes; REX 0x8B 0b00RRR100 0x25 followed by a four-byte disp32.
665 try self.REX(.{ .W = true, .B = reg.isExtended() });782 try self.code.ensureCapacity(self.code.items.len + 8);
666 try self.code.resize(self.code.items.len + 7);783 self.rex(.{ .w = true, .b = reg.isExtended() });
667 const r = 0x04 | (@as(u8, reg.id() & 0b111) << 3);784 self.code.appendSliceAssumeCapacity(&[_]u8{
668 self.code.items[self.code.items.len - 7] = 0x8B;785 0x8B,
669 self.code.items[self.code.items.len - 6] = r;786 0x04 | (@as(u8, reg.id() & 0b111) << 3), // R
670 self.code.items[self.code.items.len - 5] = 0x25;787 0x25,
671 const imm_ptr = self.code.items[self.code.items.len - 4 ..][0..4];788 });
672 mem.writeIntLittle(u32, imm_ptr, @intCast(u32, x));789 mem.writeIntLittle(u32, self.code.addManyAsArrayAssumeCapacity(4), @intCast(u32, x));
673 } else {790 } else {
674 // If this is RAX, we can use a direct load; otherwise, we need to load the address, then indirectly load791 // If this is RAX, we can use a direct load; otherwise, we need to load the address, then indirectly load
675 // the value.792 // the value.
...@@ -700,15 +817,15 @@ const Function = struct {...@@ -700,15 +817,15 @@ const Function = struct {
700 // Currently, we're only allowing 64-bit registers, so we need the `REX.W 8B /r` variant.817 // Currently, we're only allowing 64-bit registers, so we need the `REX.W 8B /r` variant.
701 // TODO: determine whether to allow other sized registers, and if so, handle them properly.818 // TODO: determine whether to allow other sized registers, and if so, handle them properly.
702 // This operation requires three bytes: REX 0x8B R/M819 // This operation requires three bytes: REX 0x8B R/M
703 //820 try self.code.ensureCapacity(self.code.items.len + 3);
704 // For this operation, we want R/M mode *zero* (use register indirectly), and the two register821 // For this operation, we want R/M mode *zero* (use register indirectly), and the two register
705 // values must match. Thus, it's 00ABCABC where ABC is the lower three bits of the register ID.822 // values must match. Thus, it's 00ABCABC where ABC is the lower three bits of the register ID.
706 //823 //
707 // Furthermore, if this is an extended register, both B and R must be set in the REX byte, as *both*824 // Furthermore, if this is an extended register, both B and R must be set in the REX byte, as *both*
708 // register operands need to be marked as extended.825 // register operands need to be marked as extended.
709 try self.REX(.{ .W = true, .B = reg.isExtended(), .R = reg.isExtended() });826 self.rex(.{ .w = true, .b = reg.isExtended(), .r = reg.isExtended() });
710 const RM = (@as(u8, reg.id() & 0b111) << 3) | @truncate(u3, reg.id());827 const RM = (@as(u8, reg.id() & 0b111) << 3) | @truncate(u3, reg.id());
711 try self.code.appendSlice(&[_]u8{ 0x8B, RM });828 self.code.appendSliceAssumeCapacity(&[_]u8{ 0x8B, RM });
712 }829 }
713 }830 }
714 },831 },
...@@ -731,36 +848,40 @@ const Function = struct {...@@ -731,36 +848,40 @@ const Function = struct {
731 }848 }
732849
733 fn resolveInst(self: *Function, inst: *ir.Inst) !MCValue {850 fn resolveInst(self: *Function, inst: *ir.Inst) !MCValue {
734 if (self.inst_table.get(inst)) |mcv| {
735 return mcv;
736 }
737 // Constants have static lifetimes, so they are always memoized in the outer most table.851 // Constants have static lifetimes, so they are always memoized in the outer most table.
738 if (inst.cast(ir.Inst.Constant)) |const_inst| {852 if (inst.cast(ir.Inst.Constant)) |const_inst| {
739 const branch = &self.branch_stack.items[0];853 const branch = &self.branch_stack.items[0];
740 const gop = try branch.inst_table.getOrPut(inst);854 const gop = try branch.inst_table.getOrPut(self.gpa, inst);
741 if (!gop.found_existing) {855 if (!gop.found_existing) {
742 const mcv = try self.genTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val });856 const mcv = try self.genTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val });
743 try branch.inst_table.putNoClobber(inst, mcv);857 try branch.inst_table.putNoClobber(self.gpa, inst, mcv);
744 gop.kv.value = mcv;858 gop.entry.value = mcv;
745 return mcv;859 return mcv;
746 }860 }
747 return gop.kv.value;861 return gop.entry.value;
748 }862 }
749863
750 // Treat each stack item as a "layer" on top of the previous one.864 // Treat each stack item as a "layer" on top of the previous one.
751 var i: usize = self.branch_stack.items.len;865 var i: usize = self.branch_stack.items.len;
752 while (true) {866 while (true) {
753 i -= 1;867 i -= 1;
754 if (self.branch_stack.items[i].inst_table.getValue(inst)) |mcv| {868 if (self.branch_stack.items[i].inst_table.get(inst)) |mcv| {
755 return mcv;869 return mcv;
756 }870 }
757 }871 }
758 }872 }
759873
874 fn resolveInstImmOrReg(self: *Function, inst: *ir.Inst) !MCValue {
875 return self.fail(inst.src, "TODO implement resolveInstImmOrReg", .{});
876 }
877
878 fn copyToNewRegister(self: *Function, src: usize, mcv: MCValue) !MCValue {
879 return self.fail(src, "TODO implement copyToNewRegister", .{});
880 }
881
760 fn genTypedValue(self: *Function, src: usize, typed_value: TypedValue) !MCValue {882 fn genTypedValue(self: *Function, src: usize, typed_value: TypedValue) !MCValue {
761 const ptr_bits = self.target.cpu.arch.ptrBitWidth();883 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
762 const ptr_bytes: u64 = @divExact(ptr_bits, 8);884 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
763 const allocator = self.code.allocator;
764 switch (typed_value.ty.zigTypeTag()) {885 switch (typed_value.ty.zigTypeTag()) {
765 .Pointer => {886 .Pointer => {
766 if (typed_value.val.cast(Value.Payload.DeclRef)) |payload| {887 if (typed_value.val.cast(Value.Payload.DeclRef)) |payload| {
...@@ -787,7 +908,7 @@ const Function = struct {...@@ -787,7 +908,7 @@ const Function = struct {
787 fn fail(self: *Function, src: usize, comptime format: []const u8, args: var) error{ CodegenFail, OutOfMemory } {908 fn fail(self: *Function, src: usize, comptime format: []const u8, args: var) error{ CodegenFail, OutOfMemory } {
788 @setCold(true);909 @setCold(true);
789 assert(self.err_msg == null);910 assert(self.err_msg == null);
790 self.err_msg = try ErrorMsg.create(self.code.allocator, src, format, args);911 self.err_msg = try ErrorMsg.create(self.bin_file.allocator, src, format, args);
791 return error.CodegenFail;912 return error.CodegenFail;
792 }913 }
793};914};
src-self-hosted/ir.zig+16-5
...@@ -2,6 +2,7 @@ const std = @import("std");...@@ -2,6 +2,7 @@ const std = @import("std");
2const Value = @import("value.zig").Value;2const Value = @import("value.zig").Value;
3const Type = @import("type.zig").Type;3const Type = @import("type.zig").Type;
4const Module = @import("Module.zig");4const Module = @import("Module.zig");
5const assert = std.debug.assert;
56
6/// These are in-memory, analyzed instructions. See `zir.Inst` for the representation7/// These are in-memory, analyzed instructions. See `zir.Inst` for the representation
7/// of instructions that correspond to the ZIR text format.8/// of instructions that correspond to the ZIR text format.
...@@ -12,18 +13,28 @@ pub const Inst = struct {...@@ -12,18 +13,28 @@ pub const Inst = struct {
12 tag: Tag,13 tag: Tag,
13 /// Each bit represents the index of an `Inst` parameter in the `args` field.14 /// Each bit represents the index of an `Inst` parameter in the `args` field.
14 /// If a bit is set, it marks the end of the lifetime of the corresponding 15 /// If a bit is set, it marks the end of the lifetime of the corresponding
15 /// instruction parameter. For example, 0b00000101 means that the first and16 /// instruction parameter. For example, 0b000_00101 means that the first and
16 /// third `Inst` parameters' lifetimes end after this instruction, and will17 /// third `Inst` parameters' lifetimes end after this instruction, and will
17 /// not have any more following references.18 /// not have any more following references.
18 /// The most significant bit being set means that the instruction itself is19 /// The most significant bit being set means that the instruction itself is
19 /// never referenced, in other words its lifetime ends as soon as it finishes.20 /// never referenced, in other words its lifetime ends as soon as it finishes.
20 /// If the byte is `0xff`, it means this is a special case and this data is21 /// If bit 7 (0b1xxx_xxxx) is set, it means this instruction itself is unreferenced.
21 /// encoded elsewhere.22 /// If bit 6 (0bx1xx_xxxx) is set, it means this is a special case and the
22 deaths: u8 = 0xff,23 /// lifetimes of operands are encoded elsewhere.
24 deaths: u8 = undefined,
23 ty: Type,25 ty: Type,
24 /// Byte offset into the source.26 /// Byte offset into the source.
25 src: usize,27 src: usize,
2628
29 pub fn isUnused(self: Inst) bool {
30 return (self.deaths & 0b1000_0000) != 0;
31 }
32
33 pub fn operandDies(self: Inst, index: u3) bool {
34 assert(index < 6);
35 return @truncate(u1, self.deaths << index) != 0;
36 }
37
27 pub const Tag = enum {38 pub const Tag = enum {
28 add,39 add,
29 arg,40 arg,
...@@ -240,4 +251,4 @@ pub const Inst = struct {...@@ -240,4 +251,4 @@ pub const Inst = struct {
240251
241pub const Body = struct {252pub const Body = struct {
242 instructions: []*Inst,253 instructions: []*Inst,
243};
\ No newline at end of file
254};
src-self-hosted/link.zig+3-3
...@@ -1007,7 +1007,7 @@ pub const ElfFile = struct {...@@ -1007,7 +1007,7 @@ pub const ElfFile = struct {
1007 .appended => code_buffer.items,1007 .appended => code_buffer.items,
1008 .fail => |em| {1008 .fail => |em| {
1009 decl.analysis = .codegen_failure;1009 decl.analysis = .codegen_failure;
1010 _ = try module.failed_decls.put(decl, em);1010 _ = try module.failed_decls.put(module.gpa, decl, em);
1011 return;1011 return;
1012 },1012 },
1013 };1013 };
...@@ -1093,7 +1093,7 @@ pub const ElfFile = struct {...@@ -1093,7 +1093,7 @@ pub const ElfFile = struct {
1093 for (exports) |exp| {1093 for (exports) |exp| {
1094 if (exp.options.section) |section_name| {1094 if (exp.options.section) |section_name| {
1095 if (!mem.eql(u8, section_name, ".text")) {1095 if (!mem.eql(u8, section_name, ".text")) {
1096 try module.failed_exports.ensureCapacity(module.failed_exports.items().len + 1);1096 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
1097 module.failed_exports.putAssumeCapacityNoClobber(1097 module.failed_exports.putAssumeCapacityNoClobber(
1098 exp,1098 exp,
1099 try Module.ErrorMsg.create(self.allocator, 0, "Unimplemented: ExportOptions.section", .{}),1099 try Module.ErrorMsg.create(self.allocator, 0, "Unimplemented: ExportOptions.section", .{}),
...@@ -1111,7 +1111,7 @@ pub const ElfFile = struct {...@@ -1111,7 +1111,7 @@ pub const ElfFile = struct {
1111 },1111 },
1112 .Weak => elf.STB_WEAK,1112 .Weak => elf.STB_WEAK,
1113 .LinkOnce => {1113 .LinkOnce => {
1114 try module.failed_exports.ensureCapacity(module.failed_exports.items().len + 1);1114 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
1115 module.failed_exports.putAssumeCapacityNoClobber(1115 module.failed_exports.putAssumeCapacityNoClobber(
1116 exp,1116 exp,
1117 try Module.ErrorMsg.create(self.allocator, 0, "Unimplemented: GlobalLinkage.LinkOnce", .{}),1117 try Module.ErrorMsg.create(self.allocator, 0, "Unimplemented: GlobalLinkage.LinkOnce", .{}),
src-self-hosted/liveness.zig+2-2
...@@ -123,7 +123,7 @@ fn analyzeInst(arena: *std.mem.Allocator, table: *std.AutoHashMap(*ir.Inst, void...@@ -123,7 +123,7 @@ fn analyzeInst(arena: *std.mem.Allocator, table: *std.AutoHashMap(*ir.Inst, void
123 if (arg_index >= 6) {123 if (arg_index >= 6) {
124 @compileError("out of bits to mark deaths of operands");124 @compileError("out of bits to mark deaths of operands");
125 }125 }
126 const prev = try table.put(@field(inst.args, field.name), {});126 const prev = try table.fetchPut(@field(inst.args, field.name), {});
127 if (prev == null) {127 if (prev == null) {
128 // Death.128 // Death.
129 inst.base.deaths |= 1 << arg_index;129 inst.base.deaths |= 1 << arg_index;
...@@ -131,4 +131,4 @@ fn analyzeInst(arena: *std.mem.Allocator, table: *std.AutoHashMap(*ir.Inst, void...@@ -131,4 +131,4 @@ fn analyzeInst(arena: *std.mem.Allocator, table: *std.AutoHashMap(*ir.Inst, void
131 arg_index += 1;131 arg_index += 1;
132 }132 }
133 }133 }
134}
\ No newline at end of file
134}
src-self-hosted/main.zig+1-1
...@@ -502,7 +502,7 @@ fn updateModule(gpa: *Allocator, module: *Module, zir_out_path: ?[]const u8) !vo...@@ -502,7 +502,7 @@ fn updateModule(gpa: *Allocator, module: *Module, zir_out_path: ?[]const u8) !vo
502 const update_nanos = timer.read();502 const update_nanos = timer.read();
503503
504 var errors = try module.getAllErrorsAlloc();504 var errors = try module.getAllErrorsAlloc();
505 defer errors.deinit(module.allocator);505 defer errors.deinit(module.gpa);
506506
507 if (errors.list.len != 0) {507 if (errors.list.len != 0) {
508 for (errors.list) |full_err_msg| {508 for (errors.list) |full_err_msg| {