| ... | ... | @@ -12,19 +12,23 @@ const TypedValue = @import("TypedValue.zig"); |
| 12 | 12 | const ir = @import("ir.zig"); |
| 13 | 13 | const IrModule = @import("Module.zig"); |
| 14 | 14 | |
| 15 | /// This struct is relevent only for the ZIR Module text format. It is not used for |
| 16 | /// semantic analysis of Zig source code. |
| 17 | pub const Decl = struct { |
| 18 | name: []const u8, |
| 19 | |
| 20 | /// Hash of slice into the source of the part after the = and before the next instruction. |
| 21 | contents_hash: std.zig.SrcHash, |
| 22 | |
| 23 | inst: *Inst, |
| 24 | }; |
| 25 | |
| 15 | 26 | /// These are instructions that correspond to the ZIR text format. See `ir.Inst` for |
| 16 | 27 | /// in-memory, analyzed instructions with types and values. |
| 17 | | /// TODO Separate into Decl and Inst. Decl will have extra fields, and will make the |
| 18 | | /// undefined default field value of contents_hash no longer needed. |
| 19 | 28 | pub const Inst = struct { |
| 20 | 29 | tag: Tag, |
| 21 | 30 | /// Byte offset into the source. |
| 22 | 31 | src: usize, |
| 23 | | name: []const u8, |
| 24 | | |
| 25 | | /// Hash of slice into the source of the part after the = and before the next instruction. |
| 26 | | contents_hash: std.zig.SrcHash = undefined, |
| 27 | | |
| 28 | 32 | /// Pre-allocated field for mapping ZIR text instructions to post-analysis instructions. |
| 29 | 33 | analyzed_inst: *ir.Inst = undefined, |
| 30 | 34 | |
| ... | ... | @@ -37,11 +41,14 @@ pub const Inst = struct { |
| 37 | 41 | @"const", |
| 38 | 42 | /// Represents a pointer to a global decl by name. |
| 39 | 43 | declref, |
| 44 | /// Represents a pointer to a global decl by string name. |
| 45 | declref_str, |
| 40 | 46 | /// The syntax `@foo` is equivalent to `declval("foo")`. |
| 41 | 47 | /// declval is equivalent to declref followed by deref. |
| 42 | 48 | declval, |
| 43 | 49 | /// Same as declval but the parameter is a `*Module.Decl` rather than a name. |
| 44 | 50 | declval_in_module, |
| 51 | /// String Literal. Makes an anonymous Decl and then takes a pointer to it. |
| 45 | 52 | str, |
| 46 | 53 | int, |
| 47 | 54 | ptrtoint, |
| ... | ... | @@ -56,7 +63,6 @@ pub const Inst = struct { |
| 56 | 63 | fntype, |
| 57 | 64 | @"export", |
| 58 | 65 | primitive, |
| 59 | | ref, |
| 60 | 66 | intcast, |
| 61 | 67 | bitcast, |
| 62 | 68 | elemptr, |
| ... | ... | @@ -72,6 +78,7 @@ pub const Inst = struct { |
| 72 | 78 | .breakpoint => Breakpoint, |
| 73 | 79 | .call => Call, |
| 74 | 80 | .declref => DeclRef, |
| 81 | .declref_str => DeclRefStr, |
| 75 | 82 | .declval => DeclVal, |
| 76 | 83 | .declval_in_module => DeclValInModule, |
| 77 | 84 | .compileerror => CompileError, |
| ... | ... | @@ -89,7 +96,6 @@ pub const Inst = struct { |
| 89 | 96 | .@"fn" => Fn, |
| 90 | 97 | .@"export" => Export, |
| 91 | 98 | .primitive => Primitive, |
| 92 | | .ref => Ref, |
| 93 | 99 | .fntype => FnType, |
| 94 | 100 | .intcast => IntCast, |
| 95 | 101 | .bitcast => BitCast, |
| ... | ... | @@ -134,6 +140,16 @@ pub const Inst = struct { |
| 134 | 140 | pub const base_tag = Tag.declref; |
| 135 | 141 | base: Inst, |
| 136 | 142 | |
| 143 | positionals: struct { |
| 144 | name: []const u8, |
| 145 | }, |
| 146 | kw_args: struct {}, |
| 147 | }; |
| 148 | |
| 149 | pub const DeclRefStr = struct { |
| 150 | pub const base_tag = Tag.declref_str; |
| 151 | base: Inst, |
| 152 | |
| 137 | 153 | positionals: struct { |
| 138 | 154 | name: *Inst, |
| 139 | 155 | }, |
| ... | ... | @@ -316,17 +332,7 @@ pub const Inst = struct { |
| 316 | 332 | |
| 317 | 333 | positionals: struct { |
| 318 | 334 | symbol_name: *Inst, |
| 319 | | value: *Inst, |
| 320 | | }, |
| 321 | | kw_args: struct {}, |
| 322 | | }; |
| 323 | | |
| 324 | | pub const Ref = struct { |
| 325 | | pub const base_tag = Tag.ref; |
| 326 | | base: Inst, |
| 327 | | |
| 328 | | positionals: struct { |
| 329 | | operand: *Inst, |
| 335 | decl_name: []const u8, |
| 330 | 336 | }, |
| 331 | 337 | kw_args: struct {}, |
| 332 | 338 | }; |
| ... | ... | @@ -500,7 +506,7 @@ pub const ErrorMsg = struct { |
| 500 | 506 | }; |
| 501 | 507 | |
| 502 | 508 | pub const Module = struct { |
| 503 | | decls: []*Inst, |
| 509 | decls: []*Decl, |
| 504 | 510 | arena: std.heap.ArenaAllocator, |
| 505 | 511 | error_msg: ?ErrorMsg = null, |
| 506 | 512 | |
| ... | ... | @@ -519,10 +525,10 @@ pub const Module = struct { |
| 519 | 525 | self.writeToStream(std.heap.page_allocator, std.io.getStdErr().outStream()) catch {}; |
| 520 | 526 | } |
| 521 | 527 | |
| 522 | | const InstPtrTable = std.AutoHashMap(*Inst, struct { inst: *Inst, index: ?usize }); |
| 528 | const InstPtrTable = std.AutoHashMap(*Inst, struct { inst: *Inst, index: ?usize, name: []const u8 }); |
| 523 | 529 | |
| 524 | 530 | const DeclAndIndex = struct { |
| 525 | | decl: *Inst, |
| 531 | decl: *Decl, |
| 526 | 532 | index: usize, |
| 527 | 533 | }; |
| 528 | 534 | |
| ... | ... | @@ -549,18 +555,18 @@ pub const Module = struct { |
| 549 | 555 | try inst_table.ensureCapacity(self.decls.len); |
| 550 | 556 | |
| 551 | 557 | for (self.decls) |decl, decl_i| { |
| 552 | | try inst_table.putNoClobber(decl, .{ .inst = decl, .index = null }); |
| 558 | try inst_table.putNoClobber(decl.inst, .{ .inst = decl.inst, .index = null, .name = decl.name }); |
| 553 | 559 | |
| 554 | | if (decl.cast(Inst.Fn)) |fn_inst| { |
| 560 | if (decl.inst.cast(Inst.Fn)) |fn_inst| { |
| 555 | 561 | for (fn_inst.positionals.body.instructions) |inst, inst_i| { |
| 556 | | try inst_table.putNoClobber(inst, .{ .inst = inst, .index = inst_i }); |
| 562 | try inst_table.putNoClobber(inst, .{ .inst = inst, .index = inst_i, .name = undefined }); |
| 557 | 563 | } |
| 558 | 564 | } |
| 559 | 565 | } |
| 560 | 566 | |
| 561 | 567 | for (self.decls) |decl, i| { |
| 562 | 568 | try stream.print("@{} ", .{decl.name}); |
| 563 | | try self.writeInstToStream(stream, decl, &inst_table); |
| 569 | try self.writeInstToStream(stream, decl.inst, &inst_table); |
| 564 | 570 | try stream.writeByte('\n'); |
| 565 | 571 | } |
| 566 | 572 | } |
| ... | ... | @@ -568,41 +574,41 @@ pub const Module = struct { |
| 568 | 574 | fn writeInstToStream( |
| 569 | 575 | self: Module, |
| 570 | 576 | stream: var, |
| 571 | | decl: *Inst, |
| 577 | inst: *Inst, |
| 572 | 578 | inst_table: *const InstPtrTable, |
| 573 | 579 | ) @TypeOf(stream).Error!void { |
| 574 | 580 | // TODO I tried implementing this with an inline for loop and hit a compiler bug |
| 575 | | switch (decl.tag) { |
| 576 | | .breakpoint => return self.writeInstToStreamGeneric(stream, .breakpoint, decl, inst_table), |
| 577 | | .call => return self.writeInstToStreamGeneric(stream, .call, decl, inst_table), |
| 578 | | .declref => return self.writeInstToStreamGeneric(stream, .declref, decl, inst_table), |
| 579 | | .declval => return self.writeInstToStreamGeneric(stream, .declval, decl, inst_table), |
| 580 | | .declval_in_module => return self.writeInstToStreamGeneric(stream, .declval_in_module, decl, inst_table), |
| 581 | | .compileerror => return self.writeInstToStreamGeneric(stream, .compileerror, decl, inst_table), |
| 582 | | .@"const" => return self.writeInstToStreamGeneric(stream, .@"const", decl, inst_table), |
| 583 | | .str => return self.writeInstToStreamGeneric(stream, .str, decl, inst_table), |
| 584 | | .int => return self.writeInstToStreamGeneric(stream, .int, decl, inst_table), |
| 585 | | .ptrtoint => return self.writeInstToStreamGeneric(stream, .ptrtoint, decl, inst_table), |
| 586 | | .fieldptr => return self.writeInstToStreamGeneric(stream, .fieldptr, decl, inst_table), |
| 587 | | .deref => return self.writeInstToStreamGeneric(stream, .deref, decl, inst_table), |
| 588 | | .as => return self.writeInstToStreamGeneric(stream, .as, decl, inst_table), |
| 589 | | .@"asm" => return self.writeInstToStreamGeneric(stream, .@"asm", decl, inst_table), |
| 590 | | .@"unreachable" => return self.writeInstToStreamGeneric(stream, .@"unreachable", decl, inst_table), |
| 591 | | .@"return" => return self.writeInstToStreamGeneric(stream, .@"return", decl, inst_table), |
| 592 | | .returnvoid => return self.writeInstToStreamGeneric(stream, .returnvoid, decl, inst_table), |
| 593 | | .@"fn" => return self.writeInstToStreamGeneric(stream, .@"fn", decl, inst_table), |
| 594 | | .@"export" => return self.writeInstToStreamGeneric(stream, .@"export", decl, inst_table), |
| 595 | | .ref => return self.writeInstToStreamGeneric(stream, .ref, decl, inst_table), |
| 596 | | .primitive => return self.writeInstToStreamGeneric(stream, .primitive, decl, inst_table), |
| 597 | | .fntype => return self.writeInstToStreamGeneric(stream, .fntype, decl, inst_table), |
| 598 | | .intcast => return self.writeInstToStreamGeneric(stream, .intcast, decl, inst_table), |
| 599 | | .bitcast => return self.writeInstToStreamGeneric(stream, .bitcast, decl, inst_table), |
| 600 | | .elemptr => return self.writeInstToStreamGeneric(stream, .elemptr, decl, inst_table), |
| 601 | | .add => return self.writeInstToStreamGeneric(stream, .add, decl, inst_table), |
| 602 | | .cmp => return self.writeInstToStreamGeneric(stream, .cmp, decl, inst_table), |
| 603 | | .condbr => return self.writeInstToStreamGeneric(stream, .condbr, decl, inst_table), |
| 604 | | .isnull => return self.writeInstToStreamGeneric(stream, .isnull, decl, inst_table), |
| 605 | | .isnonnull => return self.writeInstToStreamGeneric(stream, .isnonnull, decl, inst_table), |
| 581 | switch (inst.tag) { |
| 582 | .breakpoint => return self.writeInstToStreamGeneric(stream, .breakpoint, inst, inst_table), |
| 583 | .call => return self.writeInstToStreamGeneric(stream, .call, inst, inst_table), |
| 584 | .declref => return self.writeInstToStreamGeneric(stream, .declref, inst, inst_table), |
| 585 | .declref_str => return self.writeInstToStreamGeneric(stream, .declref_str, inst, inst_table), |
| 586 | .declval => return self.writeInstToStreamGeneric(stream, .declval, inst, inst_table), |
| 587 | .declval_in_module => return self.writeInstToStreamGeneric(stream, .declval_in_module, inst, inst_table), |
| 588 | .compileerror => return self.writeInstToStreamGeneric(stream, .compileerror, inst, inst_table), |
| 589 | .@"const" => return self.writeInstToStreamGeneric(stream, .@"const", inst, inst_table), |
| 590 | .str => return self.writeInstToStreamGeneric(stream, .str, inst, inst_table), |
| 591 | .int => return self.writeInstToStreamGeneric(stream, .int, inst, inst_table), |
| 592 | .ptrtoint => return self.writeInstToStreamGeneric(stream, .ptrtoint, inst, inst_table), |
| 593 | .fieldptr => return self.writeInstToStreamGeneric(stream, .fieldptr, inst, inst_table), |
| 594 | .deref => return self.writeInstToStreamGeneric(stream, .deref, inst, inst_table), |
| 595 | .as => return self.writeInstToStreamGeneric(stream, .as, inst, inst_table), |
| 596 | .@"asm" => return self.writeInstToStreamGeneric(stream, .@"asm", inst, inst_table), |
| 597 | .@"unreachable" => return self.writeInstToStreamGeneric(stream, .@"unreachable", inst, inst_table), |
| 598 | .@"return" => return self.writeInstToStreamGeneric(stream, .@"return", inst, inst_table), |
| 599 | .returnvoid => return self.writeInstToStreamGeneric(stream, .returnvoid, inst, inst_table), |
| 600 | .@"fn" => return self.writeInstToStreamGeneric(stream, .@"fn", inst, inst_table), |
| 601 | .@"export" => return self.writeInstToStreamGeneric(stream, .@"export", inst, inst_table), |
| 602 | .primitive => return self.writeInstToStreamGeneric(stream, .primitive, inst, inst_table), |
| 603 | .fntype => return self.writeInstToStreamGeneric(stream, .fntype, inst, inst_table), |
| 604 | .intcast => return self.writeInstToStreamGeneric(stream, .intcast, inst, inst_table), |
| 605 | .bitcast => return self.writeInstToStreamGeneric(stream, .bitcast, inst, inst_table), |
| 606 | .elemptr => return self.writeInstToStreamGeneric(stream, .elemptr, inst, inst_table), |
| 607 | .add => return self.writeInstToStreamGeneric(stream, .add, inst, inst_table), |
| 608 | .cmp => return self.writeInstToStreamGeneric(stream, .cmp, inst, inst_table), |
| 609 | .condbr => return self.writeInstToStreamGeneric(stream, .condbr, inst, inst_table), |
| 610 | .isnull => return self.writeInstToStreamGeneric(stream, .isnull, inst, inst_table), |
| 611 | .isnonnull => return self.writeInstToStreamGeneric(stream, .isnonnull, inst, inst_table), |
| 606 | 612 | } |
| 607 | 613 | } |
| 608 | 614 | |
| ... | ... | @@ -685,7 +691,7 @@ pub const Module = struct { |
| 685 | 691 | if (info.index) |i| { |
| 686 | 692 | try stream.print("%{}", .{info.index}); |
| 687 | 693 | } else { |
| 688 | | try stream.print("@{}", .{info.inst.name}); |
| 694 | try stream.print("@{}", .{info.name}); |
| 689 | 695 | } |
| 690 | 696 | } else if (inst.cast(Inst.DeclVal)) |decl_val| { |
| 691 | 697 | try stream.print("@{}", .{decl_val.positionals.name}); |
| ... | ... | @@ -732,7 +738,7 @@ const Parser = struct { |
| 732 | 738 | arena: std.heap.ArenaAllocator, |
| 733 | 739 | i: usize, |
| 734 | 740 | source: [:0]const u8, |
| 735 | | decls: std.ArrayListUnmanaged(*Inst), |
| 741 | decls: std.ArrayListUnmanaged(*Decl), |
| 736 | 742 | global_name_map: *std.StringHashMap(usize), |
| 737 | 743 | error_msg: ?ErrorMsg = null, |
| 738 | 744 | unnamed_index: usize, |
| ... | ... | @@ -761,12 +767,12 @@ const Parser = struct { |
| 761 | 767 | skipSpace(self); |
| 762 | 768 | try requireEatBytes(self, "="); |
| 763 | 769 | skipSpace(self); |
| 764 | | const inst = try parseInstruction(self, &body_context, ident); |
| 770 | const decl = try parseInstruction(self, &body_context, ident); |
| 765 | 771 | const ident_index = body_context.instructions.items.len; |
| 766 | 772 | if (try body_context.name_map.put(ident, ident_index)) |_| { |
| 767 | 773 | return self.fail("redefinition of identifier '{}'", .{ident}); |
| 768 | 774 | } |
| 769 | | try body_context.instructions.append(inst); |
| 775 | try body_context.instructions.append(decl.inst); |
| 770 | 776 | continue; |
| 771 | 777 | }, |
| 772 | 778 | ' ', '\n' => continue, |
| ... | ... | @@ -916,7 +922,7 @@ const Parser = struct { |
| 916 | 922 | return error.ParseFailure; |
| 917 | 923 | } |
| 918 | 924 | |
| 919 | | fn parseInstruction(self: *Parser, body_ctx: ?*Body, name: []const u8) InnerError!*Inst { |
| 925 | fn parseInstruction(self: *Parser, body_ctx: ?*Body, name: []const u8) InnerError!*Decl { |
| 920 | 926 | const contents_start = self.i; |
| 921 | 927 | const fn_name = try skipToAndOver(self, '('); |
| 922 | 928 | inline for (@typeInfo(Inst.Tag).Enum.fields) |field| { |
| ... | ... | @@ -935,10 +941,9 @@ const Parser = struct { |
| 935 | 941 | body_ctx: ?*Body, |
| 936 | 942 | inst_name: []const u8, |
| 937 | 943 | contents_start: usize, |
| 938 | | ) InnerError!*Inst { |
| 944 | ) InnerError!*Decl { |
| 939 | 945 | const inst_specific = try self.arena.allocator.create(InstType); |
| 940 | 946 | inst_specific.base = .{ |
| 941 | | .name = inst_name, |
| 942 | 947 | .src = self.i, |
| 943 | 948 | .tag = InstType.base_tag, |
| 944 | 949 | }; |
| ... | ... | @@ -988,10 +993,15 @@ const Parser = struct { |
| 988 | 993 | } |
| 989 | 994 | try requireEatBytes(self, ")"); |
| 990 | 995 | |
| 991 | | inst_specific.base.contents_hash = std.zig.hashSrc(self.source[contents_start..self.i]); |
| 996 | const decl = try self.arena.allocator.create(Decl); |
| 997 | decl.* = .{ |
| 998 | .name = inst_name, |
| 999 | .contents_hash = std.zig.hashSrc(self.source[contents_start..self.i]), |
| 1000 | .inst = &inst_specific.base, |
| 1001 | }; |
| 992 | 1002 | //std.debug.warn("parsed {} = '{}'\n", .{ inst_specific.base.name, inst_specific.base.contents }); |
| 993 | 1003 | |
| 994 | | return &inst_specific.base; |
| 1004 | return decl; |
| 995 | 1005 | } |
| 996 | 1006 | |
| 997 | 1007 | fn parseParameterGeneric(self: *Parser, comptime T: type, body_ctx: ?*Body) !T { |
| ... | ... | @@ -1075,7 +1085,6 @@ const Parser = struct { |
| 1075 | 1085 | const declval = try self.arena.allocator.create(Inst.DeclVal); |
| 1076 | 1086 | declval.* = .{ |
| 1077 | 1087 | .base = .{ |
| 1078 | | .name = try self.generateName(), |
| 1079 | 1088 | .src = src, |
| 1080 | 1089 | .tag = Inst.DeclVal.base_tag, |
| 1081 | 1090 | }, |
| ... | ... | @@ -1088,7 +1097,7 @@ const Parser = struct { |
| 1088 | 1097 | if (local_ref) { |
| 1089 | 1098 | return body_ctx.?.instructions.items[kv.value]; |
| 1090 | 1099 | } else { |
| 1091 | | return self.decls.items[kv.value]; |
| 1100 | return self.decls.items[kv.value].inst; |
| 1092 | 1101 | } |
| 1093 | 1102 | } |
| 1094 | 1103 | |
| ... | ... | @@ -1107,7 +1116,7 @@ pub fn emit(allocator: *Allocator, old_module: IrModule) !Module { |
| 1107 | 1116 | .old_module = &old_module, |
| 1108 | 1117 | .next_auto_name = 0, |
| 1109 | 1118 | .names = std.StringHashMap(void).init(allocator), |
| 1110 | | .primitive_table = std.AutoHashMap(Inst.Primitive.Builtin, *Inst).init(allocator), |
| 1119 | .primitive_table = std.AutoHashMap(Inst.Primitive.Builtin, *Decl).init(allocator), |
| 1111 | 1120 | }; |
| 1112 | 1121 | defer ctx.decls.deinit(allocator); |
| 1113 | 1122 | defer ctx.names.deinit(); |
| ... | ... | @@ -1126,10 +1135,10 @@ const EmitZIR = struct { |
| 1126 | 1135 | allocator: *Allocator, |
| 1127 | 1136 | arena: std.heap.ArenaAllocator, |
| 1128 | 1137 | old_module: *const IrModule, |
| 1129 | | decls: std.ArrayListUnmanaged(*Inst), |
| 1138 | decls: std.ArrayListUnmanaged(*Decl), |
| 1130 | 1139 | names: std.StringHashMap(void), |
| 1131 | 1140 | next_auto_name: usize, |
| 1132 | | primitive_table: std.AutoHashMap(Inst.Primitive.Builtin, *Inst), |
| 1141 | primitive_table: std.AutoHashMap(Inst.Primitive.Builtin, *Decl), |
| 1133 | 1142 | |
| 1134 | 1143 | fn emit(self: *EmitZIR) !void { |
| 1135 | 1144 | // Put all the Decls in a list and sort them by name to avoid nondeterminism introduced |
| ... | ... | @@ -1156,22 +1165,20 @@ const EmitZIR = struct { |
| 1156 | 1165 | for (src_decls.items) |ir_decl| { |
| 1157 | 1166 | if (self.old_module.export_owners.getValue(ir_decl)) |exports| { |
| 1158 | 1167 | for (exports) |module_export| { |
| 1159 | | const declval = try self.emitDeclVal(ir_decl.src(), mem.spanZ(module_export.exported_decl.name)); |
| 1160 | 1168 | const symbol_name = try self.emitStringLiteral(module_export.src, module_export.options.name); |
| 1161 | 1169 | const export_inst = try self.arena.allocator.create(Inst.Export); |
| 1162 | 1170 | export_inst.* = .{ |
| 1163 | 1171 | .base = .{ |
| 1164 | | .name = try self.autoName(), |
| 1165 | 1172 | .src = module_export.src, |
| 1166 | 1173 | .tag = Inst.Export.base_tag, |
| 1167 | 1174 | }, |
| 1168 | 1175 | .positionals = .{ |
| 1169 | | .symbol_name = symbol_name, |
| 1170 | | .value = declval, |
| 1176 | .symbol_name = symbol_name.inst, |
| 1177 | .decl_name = mem.spanZ(module_export.exported_decl.name), |
| 1171 | 1178 | }, |
| 1172 | 1179 | .kw_args = .{}, |
| 1173 | 1180 | }; |
| 1174 | | try self.decls.append(self.allocator, &export_inst.base); |
| 1181 | _ = try self.emitUnnamedDecl(&export_inst.base); |
| 1175 | 1182 | } |
| 1176 | 1183 | } else { |
| 1177 | 1184 | const new_decl = try self.emitTypedValue(ir_decl.src(), ir_decl.typed_value.most_recent.typed_value); |
| ... | ... | @@ -1188,7 +1195,7 @@ const EmitZIR = struct { |
| 1188 | 1195 | } else if (const_inst.val.cast(Value.Payload.DeclRef)) |declref| blk: { |
| 1189 | 1196 | break :blk try self.emitDeclRef(inst.src, declref.decl); |
| 1190 | 1197 | } else blk: { |
| 1191 | | break :blk try self.emitTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val }); |
| 1198 | break :blk (try self.emitTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val })).inst; |
| 1192 | 1199 | }; |
| 1193 | 1200 | try inst_table.putNoClobber(inst, new_decl); |
| 1194 | 1201 | return new_decl; |
| ... | ... | @@ -1201,7 +1208,6 @@ const EmitZIR = struct { |
| 1201 | 1208 | const declval = try self.arena.allocator.create(Inst.DeclVal); |
| 1202 | 1209 | declval.* = .{ |
| 1203 | 1210 | .base = .{ |
| 1204 | | .name = try self.autoName(), |
| 1205 | 1211 | .src = src, |
| 1206 | 1212 | .tag = Inst.DeclVal.base_tag, |
| 1207 | 1213 | }, |
| ... | ... | @@ -1211,12 +1217,11 @@ const EmitZIR = struct { |
| 1211 | 1217 | return &declval.base; |
| 1212 | 1218 | } |
| 1213 | 1219 | |
| 1214 | | fn emitComptimeIntVal(self: *EmitZIR, src: usize, val: Value) !*Inst { |
| 1220 | fn emitComptimeIntVal(self: *EmitZIR, src: usize, val: Value) !*Decl { |
| 1215 | 1221 | const big_int_space = try self.arena.allocator.create(Value.BigIntSpace); |
| 1216 | 1222 | const int_inst = try self.arena.allocator.create(Inst.Int); |
| 1217 | 1223 | int_inst.* = .{ |
| 1218 | 1224 | .base = .{ |
| 1219 | | .name = try self.autoName(), |
| 1220 | 1225 | .src = src, |
| 1221 | 1226 | .tag = Inst.Int.base_tag, |
| 1222 | 1227 | }, |
| ... | ... | @@ -1225,34 +1230,29 @@ const EmitZIR = struct { |
| 1225 | 1230 | }, |
| 1226 | 1231 | .kw_args = .{}, |
| 1227 | 1232 | }; |
| 1228 | | try self.decls.append(self.allocator, &int_inst.base); |
| 1229 | | return &int_inst.base; |
| 1233 | return self.emitUnnamedDecl(&int_inst.base); |
| 1230 | 1234 | } |
| 1231 | 1235 | |
| 1232 | | fn emitDeclRef(self: *EmitZIR, src: usize, decl: *IrModule.Decl) !*Inst { |
| 1233 | | const declval = try self.emitDeclVal(src, mem.spanZ(decl.name)); |
| 1234 | | const ref_inst = try self.arena.allocator.create(Inst.Ref); |
| 1235 | | ref_inst.* = .{ |
| 1236 | fn emitDeclRef(self: *EmitZIR, src: usize, module_decl: *IrModule.Decl) !*Inst { |
| 1237 | const declref_inst = try self.arena.allocator.create(Inst.DeclRef); |
| 1238 | declref_inst.* = .{ |
| 1236 | 1239 | .base = .{ |
| 1237 | | .name = try self.autoName(), |
| 1238 | 1240 | .src = src, |
| 1239 | | .tag = Inst.Ref.base_tag, |
| 1241 | .tag = Inst.DeclRef.base_tag, |
| 1240 | 1242 | }, |
| 1241 | 1243 | .positionals = .{ |
| 1242 | | .operand = declval, |
| 1244 | .name = mem.spanZ(module_decl.name), |
| 1243 | 1245 | }, |
| 1244 | 1246 | .kw_args = .{}, |
| 1245 | 1247 | }; |
| 1246 | | try self.decls.append(self.allocator, &ref_inst.base); |
| 1247 | | |
| 1248 | | return &ref_inst.base; |
| 1248 | return &declref_inst.base; |
| 1249 | 1249 | } |
| 1250 | 1250 | |
| 1251 | | fn emitTypedValue(self: *EmitZIR, src: usize, typed_value: TypedValue) Allocator.Error!*Inst { |
| 1251 | fn emitTypedValue(self: *EmitZIR, src: usize, typed_value: TypedValue) Allocator.Error!*Decl { |
| 1252 | 1252 | const allocator = &self.arena.allocator; |
| 1253 | 1253 | if (typed_value.val.cast(Value.Payload.DeclRef)) |decl_ref| { |
| 1254 | 1254 | const decl = decl_ref.decl; |
| 1255 | | return self.emitDeclRef(src, decl); |
| 1255 | return try self.emitUnnamedDecl(try self.emitDeclRef(src, decl)); |
| 1256 | 1256 | } |
| 1257 | 1257 | switch (typed_value.ty.zigTypeTag()) { |
| 1258 | 1258 | .Pointer => { |
| ... | ... | @@ -1279,18 +1279,16 @@ const EmitZIR = struct { |
| 1279 | 1279 | const as_inst = try self.arena.allocator.create(Inst.As); |
| 1280 | 1280 | as_inst.* = .{ |
| 1281 | 1281 | .base = .{ |
| 1282 | | .name = try self.autoName(), |
| 1283 | 1282 | .src = src, |
| 1284 | 1283 | .tag = Inst.As.base_tag, |
| 1285 | 1284 | }, |
| 1286 | 1285 | .positionals = .{ |
| 1287 | | .dest_type = try self.emitType(src, typed_value.ty), |
| 1288 | | .value = try self.emitComptimeIntVal(src, typed_value.val), |
| 1286 | .dest_type = (try self.emitType(src, typed_value.ty)).inst, |
| 1287 | .value = (try self.emitComptimeIntVal(src, typed_value.val)).inst, |
| 1289 | 1288 | }, |
| 1290 | 1289 | .kw_args = .{}, |
| 1291 | 1290 | }; |
| 1292 | | |
| 1293 | | return &as_inst.base; |
| 1291 | return self.emitUnnamedDecl(&as_inst.base); |
| 1294 | 1292 | }, |
| 1295 | 1293 | .Type => { |
| 1296 | 1294 | const ty = typed_value.val.toType(); |
| ... | ... | @@ -1316,7 +1314,6 @@ const EmitZIR = struct { |
| 1316 | 1314 | const fail_inst = try self.arena.allocator.create(Inst.CompileError); |
| 1317 | 1315 | fail_inst.* = .{ |
| 1318 | 1316 | .base = .{ |
| 1319 | | .name = try self.autoName(), |
| 1320 | 1317 | .src = src, |
| 1321 | 1318 | .tag = Inst.CompileError.base_tag, |
| 1322 | 1319 | }, |
| ... | ... | @@ -1331,7 +1328,6 @@ const EmitZIR = struct { |
| 1331 | 1328 | const fail_inst = try self.arena.allocator.create(Inst.CompileError); |
| 1332 | 1329 | fail_inst.* = .{ |
| 1333 | 1330 | .base = .{ |
| 1334 | | .name = try self.autoName(), |
| 1335 | 1331 | .src = src, |
| 1336 | 1332 | .tag = Inst.CompileError.base_tag, |
| 1337 | 1333 | }, |
| ... | ... | @@ -1352,18 +1348,16 @@ const EmitZIR = struct { |
| 1352 | 1348 | const fn_inst = try self.arena.allocator.create(Inst.Fn); |
| 1353 | 1349 | fn_inst.* = .{ |
| 1354 | 1350 | .base = .{ |
| 1355 | | .name = try self.autoName(), |
| 1356 | 1351 | .src = src, |
| 1357 | 1352 | .tag = Inst.Fn.base_tag, |
| 1358 | 1353 | }, |
| 1359 | 1354 | .positionals = .{ |
| 1360 | | .fn_type = fn_type, |
| 1355 | .fn_type = fn_type.inst, |
| 1361 | 1356 | .body = .{ .instructions = arena_instrs }, |
| 1362 | 1357 | }, |
| 1363 | 1358 | .kw_args = .{}, |
| 1364 | 1359 | }; |
| 1365 | | try self.decls.append(self.allocator, &fn_inst.base); |
| 1366 | | return &fn_inst.base; |
| 1360 | return self.emitUnnamedDecl(&fn_inst.base); |
| 1367 | 1361 | }, |
| 1368 | 1362 | .Array => { |
| 1369 | 1363 | // TODO more checks to make sure this can be emitted as a string literal |
| ... | ... | @@ -1379,7 +1373,6 @@ const EmitZIR = struct { |
| 1379 | 1373 | const str_inst = try self.arena.allocator.create(Inst.Str); |
| 1380 | 1374 | str_inst.* = .{ |
| 1381 | 1375 | .base = .{ |
| 1382 | | .name = try self.autoName(), |
| 1383 | 1376 | .src = src, |
| 1384 | 1377 | .tag = Inst.Str.base_tag, |
| 1385 | 1378 | }, |
| ... | ... | @@ -1388,8 +1381,7 @@ const EmitZIR = struct { |
| 1388 | 1381 | }, |
| 1389 | 1382 | .kw_args = .{}, |
| 1390 | 1383 | }; |
| 1391 | | try self.decls.append(self.allocator, &str_inst.base); |
| 1392 | | return &str_inst.base; |
| 1384 | return self.emitUnnamedDecl(&str_inst.base); |
| 1393 | 1385 | }, |
| 1394 | 1386 | .Void => return self.emitPrimitive(src, .void_value), |
| 1395 | 1387 | else => |t| std.debug.panic("TODO implement emitTypedValue for {}", .{@tagName(t)}), |
| ... | ... | @@ -1400,7 +1392,6 @@ const EmitZIR = struct { |
| 1400 | 1392 | const new_inst = try self.arena.allocator.create(T); |
| 1401 | 1393 | new_inst.* = .{ |
| 1402 | 1394 | .base = .{ |
| 1403 | | .name = try self.autoName(), |
| 1404 | 1395 | .src = src, |
| 1405 | 1396 | .tag = T.base_tag, |
| 1406 | 1397 | }, |
| ... | ... | @@ -1429,7 +1420,6 @@ const EmitZIR = struct { |
| 1429 | 1420 | } |
| 1430 | 1421 | new_inst.* = .{ |
| 1431 | 1422 | .base = .{ |
| 1432 | | .name = try self.autoName(), |
| 1433 | 1423 | .src = inst.src, |
| 1434 | 1424 | .tag = Inst.Call.base_tag, |
| 1435 | 1425 | }, |
| ... | ... | @@ -1447,7 +1437,6 @@ const EmitZIR = struct { |
| 1447 | 1437 | const new_inst = try self.arena.allocator.create(Inst.Return); |
| 1448 | 1438 | new_inst.* = .{ |
| 1449 | 1439 | .base = .{ |
| 1450 | | .name = try self.autoName(), |
| 1451 | 1440 | .src = inst.src, |
| 1452 | 1441 | .tag = Inst.Return.base_tag, |
| 1453 | 1442 | }, |
| ... | ... | @@ -1466,12 +1455,12 @@ const EmitZIR = struct { |
| 1466 | 1455 | |
| 1467 | 1456 | const inputs = try self.arena.allocator.alloc(*Inst, old_inst.args.inputs.len); |
| 1468 | 1457 | for (inputs) |*elem, i| { |
| 1469 | | elem.* = try self.emitStringLiteral(inst.src, old_inst.args.inputs[i]); |
| 1458 | elem.* = (try self.emitStringLiteral(inst.src, old_inst.args.inputs[i])).inst; |
| 1470 | 1459 | } |
| 1471 | 1460 | |
| 1472 | 1461 | const clobbers = try self.arena.allocator.alloc(*Inst, old_inst.args.clobbers.len); |
| 1473 | 1462 | for (clobbers) |*elem, i| { |
| 1474 | | elem.* = try self.emitStringLiteral(inst.src, old_inst.args.clobbers[i]); |
| 1463 | elem.* = (try self.emitStringLiteral(inst.src, old_inst.args.clobbers[i])).inst; |
| 1475 | 1464 | } |
| 1476 | 1465 | |
| 1477 | 1466 | const args = try self.arena.allocator.alloc(*Inst, old_inst.args.args.len); |
| ... | ... | @@ -1481,18 +1470,17 @@ const EmitZIR = struct { |
| 1481 | 1470 | |
| 1482 | 1471 | new_inst.* = .{ |
| 1483 | 1472 | .base = .{ |
| 1484 | | .name = try self.autoName(), |
| 1485 | 1473 | .src = inst.src, |
| 1486 | 1474 | .tag = Inst.Asm.base_tag, |
| 1487 | 1475 | }, |
| 1488 | 1476 | .positionals = .{ |
| 1489 | | .asm_source = try self.emitStringLiteral(inst.src, old_inst.args.asm_source), |
| 1490 | | .return_type = try self.emitType(inst.src, inst.ty), |
| 1477 | .asm_source = (try self.emitStringLiteral(inst.src, old_inst.args.asm_source)).inst, |
| 1478 | .return_type = (try self.emitType(inst.src, inst.ty)).inst, |
| 1491 | 1479 | }, |
| 1492 | 1480 | .kw_args = .{ |
| 1493 | 1481 | .@"volatile" = old_inst.args.is_volatile, |
| 1494 | 1482 | .output = if (old_inst.args.output) |o| |
| 1495 | | try self.emitStringLiteral(inst.src, o) |
| 1483 | (try self.emitStringLiteral(inst.src, o)).inst |
| 1496 | 1484 | else |
| 1497 | 1485 | null, |
| 1498 | 1486 | .inputs = inputs, |
| ... | ... | @@ -1507,7 +1495,6 @@ const EmitZIR = struct { |
| 1507 | 1495 | const new_inst = try self.arena.allocator.create(Inst.PtrToInt); |
| 1508 | 1496 | new_inst.* = .{ |
| 1509 | 1497 | .base = .{ |
| 1510 | | .name = try self.autoName(), |
| 1511 | 1498 | .src = inst.src, |
| 1512 | 1499 | .tag = Inst.PtrToInt.base_tag, |
| 1513 | 1500 | }, |
| ... | ... | @@ -1523,12 +1510,11 @@ const EmitZIR = struct { |
| 1523 | 1510 | const new_inst = try self.arena.allocator.create(Inst.BitCast); |
| 1524 | 1511 | new_inst.* = .{ |
| 1525 | 1512 | .base = .{ |
| 1526 | | .name = try self.autoName(), |
| 1527 | 1513 | .src = inst.src, |
| 1528 | 1514 | .tag = Inst.BitCast.base_tag, |
| 1529 | 1515 | }, |
| 1530 | 1516 | .positionals = .{ |
| 1531 | | .dest_type = try self.emitType(inst.src, inst.ty), |
| 1517 | .dest_type = (try self.emitType(inst.src, inst.ty)).inst, |
| 1532 | 1518 | .operand = try self.resolveInst(inst_table, old_inst.args.operand), |
| 1533 | 1519 | }, |
| 1534 | 1520 | .kw_args = .{}, |
| ... | ... | @@ -1540,7 +1526,6 @@ const EmitZIR = struct { |
| 1540 | 1526 | const new_inst = try self.arena.allocator.create(Inst.Cmp); |
| 1541 | 1527 | new_inst.* = .{ |
| 1542 | 1528 | .base = .{ |
| 1543 | | .name = try self.autoName(), |
| 1544 | 1529 | .src = inst.src, |
| 1545 | 1530 | .tag = Inst.Cmp.base_tag, |
| 1546 | 1531 | }, |
| ... | ... | @@ -1568,7 +1553,6 @@ const EmitZIR = struct { |
| 1568 | 1553 | const new_inst = try self.arena.allocator.create(Inst.CondBr); |
| 1569 | 1554 | new_inst.* = .{ |
| 1570 | 1555 | .base = .{ |
| 1571 | | .name = try self.autoName(), |
| 1572 | 1556 | .src = inst.src, |
| 1573 | 1557 | .tag = Inst.CondBr.base_tag, |
| 1574 | 1558 | }, |
| ... | ... | @@ -1586,7 +1570,6 @@ const EmitZIR = struct { |
| 1586 | 1570 | const new_inst = try self.arena.allocator.create(Inst.IsNull); |
| 1587 | 1571 | new_inst.* = .{ |
| 1588 | 1572 | .base = .{ |
| 1589 | | .name = try self.autoName(), |
| 1590 | 1573 | .src = inst.src, |
| 1591 | 1574 | .tag = Inst.IsNull.base_tag, |
| 1592 | 1575 | }, |
| ... | ... | @@ -1602,7 +1585,6 @@ const EmitZIR = struct { |
| 1602 | 1585 | const new_inst = try self.arena.allocator.create(Inst.IsNonNull); |
| 1603 | 1586 | new_inst.* = .{ |
| 1604 | 1587 | .base = .{ |
| 1605 | | .name = try self.autoName(), |
| 1606 | 1588 | .src = inst.src, |
| 1607 | 1589 | .tag = Inst.IsNonNull.base_tag, |
| 1608 | 1590 | }, |
| ... | ... | @@ -1619,7 +1601,7 @@ const EmitZIR = struct { |
| 1619 | 1601 | } |
| 1620 | 1602 | } |
| 1621 | 1603 | |
| 1622 | | fn emitType(self: *EmitZIR, src: usize, ty: Type) Allocator.Error!*Inst { |
| 1604 | fn emitType(self: *EmitZIR, src: usize, ty: Type) Allocator.Error!*Decl { |
| 1623 | 1605 | switch (ty.tag()) { |
| 1624 | 1606 | .isize => return self.emitPrimitive(src, .isize), |
| 1625 | 1607 | .usize => return self.emitPrimitive(src, .usize), |
| ... | ... | @@ -1652,26 +1634,24 @@ const EmitZIR = struct { |
| 1652 | 1634 | ty.fnParamTypes(param_types); |
| 1653 | 1635 | const emitted_params = try self.arena.allocator.alloc(*Inst, param_types.len); |
| 1654 | 1636 | for (param_types) |param_type, i| { |
| 1655 | | emitted_params[i] = try self.emitType(src, param_type); |
| 1637 | emitted_params[i] = (try self.emitType(src, param_type)).inst; |
| 1656 | 1638 | } |
| 1657 | 1639 | |
| 1658 | 1640 | const fntype_inst = try self.arena.allocator.create(Inst.FnType); |
| 1659 | 1641 | fntype_inst.* = .{ |
| 1660 | 1642 | .base = .{ |
| 1661 | | .name = try self.autoName(), |
| 1662 | 1643 | .src = src, |
| 1663 | 1644 | .tag = Inst.FnType.base_tag, |
| 1664 | 1645 | }, |
| 1665 | 1646 | .positionals = .{ |
| 1666 | 1647 | .param_types = emitted_params, |
| 1667 | | .return_type = try self.emitType(src, ty.fnReturnType()), |
| 1648 | .return_type = (try self.emitType(src, ty.fnReturnType())).inst, |
| 1668 | 1649 | }, |
| 1669 | 1650 | .kw_args = .{ |
| 1670 | 1651 | .cc = ty.fnCallingConvention(), |
| 1671 | 1652 | }, |
| 1672 | 1653 | }; |
| 1673 | | try self.decls.append(self.allocator, &fntype_inst.base); |
| 1674 | | return &fntype_inst.base; |
| 1654 | return self.emitUnnamedDecl(&fntype_inst.base); |
| 1675 | 1655 | }, |
| 1676 | 1656 | else => std.debug.panic("TODO implement emitType for {}", .{ty}), |
| 1677 | 1657 | }, |
| ... | ... | @@ -1690,13 +1670,12 @@ const EmitZIR = struct { |
| 1690 | 1670 | } |
| 1691 | 1671 | } |
| 1692 | 1672 | |
| 1693 | | fn emitPrimitive(self: *EmitZIR, src: usize, tag: Inst.Primitive.Builtin) !*Inst { |
| 1673 | fn emitPrimitive(self: *EmitZIR, src: usize, tag: Inst.Primitive.Builtin) !*Decl { |
| 1694 | 1674 | const gop = try self.primitive_table.getOrPut(tag); |
| 1695 | 1675 | if (!gop.found_existing) { |
| 1696 | 1676 | const primitive_inst = try self.arena.allocator.create(Inst.Primitive); |
| 1697 | 1677 | primitive_inst.* = .{ |
| 1698 | 1678 | .base = .{ |
| 1699 | | .name = try self.autoName(), |
| 1700 | 1679 | .src = src, |
| 1701 | 1680 | .tag = Inst.Primitive.base_tag, |
| 1702 | 1681 | }, |
| ... | ... | @@ -1705,17 +1684,15 @@ const EmitZIR = struct { |
| 1705 | 1684 | }, |
| 1706 | 1685 | .kw_args = .{}, |
| 1707 | 1686 | }; |
| 1708 | | try self.decls.append(self.allocator, &primitive_inst.base); |
| 1709 | | gop.kv.value = &primitive_inst.base; |
| 1687 | gop.kv.value = try self.emitUnnamedDecl(&primitive_inst.base); |
| 1710 | 1688 | } |
| 1711 | 1689 | return gop.kv.value; |
| 1712 | 1690 | } |
| 1713 | 1691 | |
| 1714 | | fn emitStringLiteral(self: *EmitZIR, src: usize, str: []const u8) !*Inst { |
| 1692 | fn emitStringLiteral(self: *EmitZIR, src: usize, str: []const u8) !*Decl { |
| 1715 | 1693 | const str_inst = try self.arena.allocator.create(Inst.Str); |
| 1716 | 1694 | str_inst.* = .{ |
| 1717 | 1695 | .base = .{ |
| 1718 | | .name = try self.autoName(), |
| 1719 | 1696 | .src = src, |
| 1720 | 1697 | .tag = Inst.Str.base_tag, |
| 1721 | 1698 | }, |
| ... | ... | @@ -1724,22 +1701,17 @@ const EmitZIR = struct { |
| 1724 | 1701 | }, |
| 1725 | 1702 | .kw_args = .{}, |
| 1726 | 1703 | }; |
| 1727 | | try self.decls.append(self.allocator, &str_inst.base); |
| 1704 | return self.emitUnnamedDecl(&str_inst.base); |
| 1705 | } |
| 1728 | 1706 | |
| 1729 | | const ref_inst = try self.arena.allocator.create(Inst.Ref); |
| 1730 | | ref_inst.* = .{ |
| 1731 | | .base = .{ |
| 1732 | | .name = try self.autoName(), |
| 1733 | | .src = src, |
| 1734 | | .tag = Inst.Ref.base_tag, |
| 1735 | | }, |
| 1736 | | .positionals = .{ |
| 1737 | | .operand = &str_inst.base, |
| 1738 | | }, |
| 1739 | | .kw_args = .{}, |
| 1707 | fn emitUnnamedDecl(self: *EmitZIR, inst: *Inst) !*Decl { |
| 1708 | const decl = try self.arena.allocator.create(Decl); |
| 1709 | decl.* = .{ |
| 1710 | .name = try self.autoName(), |
| 1711 | .contents_hash = undefined, |
| 1712 | .inst = inst, |
| 1740 | 1713 | }; |
| 1741 | | try self.decls.append(self.allocator, &ref_inst.base); |
| 1742 | | |
| 1743 | | return &ref_inst.base; |
| 1714 | try self.decls.append(self.allocator, decl); |
| 1715 | return decl; |
| 1744 | 1716 | } |
| 1745 | 1717 | }; |