| ... | @@ -8,6 +8,7 @@ const mem = std.mem; | ... | @@ -8,6 +8,7 @@ const mem = std.mem; |
| 8 | const wasm = std.wasm; | 8 | const wasm = std.wasm; |
| 9 | const log = std.log.scoped(.codegen); | 9 | const log = std.log.scoped(.codegen); |
| 10 | | 10 | |
| | 11 | const codegen = @import("../../codegen.zig"); |
| 11 | const Module = @import("../../Module.zig"); | 12 | const Module = @import("../../Module.zig"); |
| 12 | const Decl = Module.Decl; | 13 | const Decl = Module.Decl; |
| 13 | const Type = @import("../../type.zig").Type; | 14 | const Type = @import("../../type.zig").Type; |
| ... | @@ -546,7 +547,7 @@ blocks: std.AutoArrayHashMapUnmanaged(Air.Inst.Index, struct { | ... | @@ -546,7 +547,7 @@ blocks: std.AutoArrayHashMapUnmanaged(Air.Inst.Index, struct { |
| 546 | value: WValue, | 547 | value: WValue, |
| 547 | }) = .{}, | 548 | }) = .{}, |
| 548 | /// `bytes` contains the wasm bytecode belonging to the 'code' section. | 549 | /// `bytes` contains the wasm bytecode belonging to the 'code' section. |
| 549 | code: ArrayList(u8), | 550 | code: *ArrayList(u8), |
| 550 | /// The index the next local generated will have | 551 | /// The index the next local generated will have |
| 551 | /// NOTE: arguments share the index with locals therefore the first variable | 552 | /// NOTE: arguments share the index with locals therefore the first variable |
| 552 | /// will have the index that comes after the last argument's index | 553 | /// will have the index that comes after the last argument's index |
| ... | @@ -566,9 +567,6 @@ locals: std.ArrayListUnmanaged(u8), | ... | @@ -566,9 +567,6 @@ locals: std.ArrayListUnmanaged(u8), |
| 566 | target: std.Target, | 567 | target: std.Target, |
| 567 | /// Represents the wasm binary file that is being linked. | 568 | /// Represents the wasm binary file that is being linked. |
| 568 | bin_file: *link.File.Wasm, | 569 | bin_file: *link.File.Wasm, |
| 569 | /// Reference to the Module that this decl is part of. | | |
| 570 | /// Used to find the error value. | | |
| 571 | module: *Module, | | |
| 572 | /// List of MIR Instructions | 570 | /// List of MIR Instructions |
| 573 | mir_instructions: std.MultiArrayList(Mir.Inst) = .{}, | 571 | mir_instructions: std.MultiArrayList(Mir.Inst) = .{}, |
| 574 | /// Contains extra data for MIR | 572 | /// Contains extra data for MIR |
| ... | @@ -611,7 +609,6 @@ pub fn deinit(self: *Self) void { | ... | @@ -611,7 +609,6 @@ pub fn deinit(self: *Self) void { |
| 611 | self.locals.deinit(self.gpa); | 609 | self.locals.deinit(self.gpa); |
| 612 | self.mir_instructions.deinit(self.gpa); | 610 | self.mir_instructions.deinit(self.gpa); |
| 613 | self.mir_extra.deinit(self.gpa); | 611 | self.mir_extra.deinit(self.gpa); |
| 614 | self.code.deinit(); | | |
| 615 | self.* = undefined; | 612 | self.* = undefined; |
| 616 | } | 613 | } |
| 617 | | 614 | |
| ... | @@ -822,7 +819,40 @@ fn genFunctype(gpa: Allocator, fn_ty: Type, target: std.Target) !wasm.Type { | ... | @@ -822,7 +819,40 @@ fn genFunctype(gpa: Allocator, fn_ty: Type, target: std.Target) !wasm.Type { |
| 822 | }; | 819 | }; |
| 823 | } | 820 | } |
| 824 | | 821 | |
| 825 | pub fn genFunc(self: *Self) InnerError!void { | 822 | pub fn generate( |
| | 823 | bin_file: *link.File, |
| | 824 | src_loc: Module.SrcLoc, |
| | 825 | func: *Module.Fn, |
| | 826 | air: Air, |
| | 827 | liveness: Liveness, |
| | 828 | code: *std.ArrayList(u8), |
| | 829 | debug_output: codegen.DebugInfoOutput, |
| | 830 | ) codegen.GenerateSymbolError!codegen.FnResult { |
| | 831 | _ = debug_output; // TODO |
| | 832 | _ = src_loc; |
| | 833 | var code_gen: Self = .{ |
| | 834 | .gpa = bin_file.allocator, |
| | 835 | .air = air, |
| | 836 | .liveness = liveness, |
| | 837 | .values = .{}, |
| | 838 | .code = code, |
| | 839 | .decl = func.owner_decl, |
| | 840 | .err_msg = undefined, |
| | 841 | .locals = .{}, |
| | 842 | .target = bin_file.options.target, |
| | 843 | .bin_file = bin_file.cast(link.File.Wasm).?, |
| | 844 | }; |
| | 845 | defer code_gen.deinit(); |
| | 846 | |
| | 847 | genFunc(&code_gen) catch |err| switch (err) { |
| | 848 | error.CodegenFail => return codegen.FnResult{ .fail = code_gen.err_msg }, |
| | 849 | else => |e| return e, |
| | 850 | }; |
| | 851 | |
| | 852 | return codegen.FnResult{ .appended = {} }; |
| | 853 | } |
| | 854 | |
| | 855 | fn genFunc(self: *Self) InnerError!void { |
| 826 | var func_type = try genFunctype(self.gpa, self.decl.ty, self.target); | 856 | var func_type = try genFunctype(self.gpa, self.decl.ty, self.target); |
| 827 | defer func_type.deinit(self.gpa); | 857 | defer func_type.deinit(self.gpa); |
| 828 | self.decl.fn_link.wasm.type_index = try self.bin_file.putOrGetFuncType(func_type); | 858 | self.decl.fn_link.wasm.type_index = try self.bin_file.putOrGetFuncType(func_type); |
| ... | @@ -889,7 +919,7 @@ pub fn genFunc(self: *Self) InnerError!void { | ... | @@ -889,7 +919,7 @@ pub fn genFunc(self: *Self) InnerError!void { |
| 889 | var emit: Emit = .{ | 919 | var emit: Emit = .{ |
| 890 | .mir = mir, | 920 | .mir = mir, |
| 891 | .bin_file = &self.bin_file.base, | 921 | .bin_file = &self.bin_file.base, |
| 892 | .code = &self.code, | 922 | .code = self.code, |
| 893 | .locals = self.locals.items, | 923 | .locals = self.locals.items, |
| 894 | .decl = self.decl, | 924 | .decl = self.decl, |
| 895 | }; | 925 | }; |
| ... | @@ -1761,7 +1791,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { | ... | @@ -1761,7 +1791,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 1761 | }, | 1791 | }, |
| 1762 | .ErrorSet => switch (val.tag()) { | 1792 | .ErrorSet => switch (val.tag()) { |
| 1763 | .@"error" => { | 1793 | .@"error" => { |
| 1764 | const kv = try self.module.getErrorValue(val.getError().?); | 1794 | const kv = try self.bin_file.base.options.module.?.getErrorValue(val.getError().?); |
| 1765 | return WValue{ .imm32 = kv.value }; | 1795 | return WValue{ .imm32 = kv.value }; |
| 1766 | }, | 1796 | }, |
| 1767 | else => return WValue{ .imm32 = 0 }, | 1797 | else => return WValue{ .imm32 = 0 }, |
| ... | @@ -1852,7 +1882,7 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 { | ... | @@ -1852,7 +1882,7 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 { |
| 1852 | .unsigned => return @bitCast(i32, @truncate(u32, val.toUnsignedInt())), | 1882 | .unsigned => return @bitCast(i32, @truncate(u32, val.toUnsignedInt())), |
| 1853 | }, | 1883 | }, |
| 1854 | .ErrorSet => { | 1884 | .ErrorSet => { |
| 1855 | const kv = self.module.getErrorValue(val.getError().?) catch unreachable; // passed invalid `Value` to function | 1885 | const kv = self.bin_file.base.options.module.?.getErrorValue(val.getError().?) catch unreachable; // passed invalid `Value` to function |
| 1856 | return @bitCast(i32, kv.value); | 1886 | return @bitCast(i32, kv.value); |
| 1857 | }, | 1887 | }, |
| 1858 | else => unreachable, // Programmer called this function for an illegal type | 1888 | else => unreachable, // Programmer called this function for an illegal type |