| ... | @@ -6,6 +6,7 @@ const testing = std.testing; | ... | @@ -6,6 +6,7 @@ const testing = std.testing; |
| 6 | const leb = std.leb; | 6 | const leb = std.leb; |
| 7 | const mem = std.mem; | 7 | const mem = std.mem; |
| 8 | const wasm = std.wasm; | 8 | const wasm = std.wasm; |
| | 9 | const log = std.log.scoped(.codegen); |
| 9 | | 10 | |
| 10 | const Module = @import("../../Module.zig"); | 11 | const Module = @import("../../Module.zig"); |
| 11 | const Decl = Module.Decl; | 12 | const Decl = Module.Decl; |
| ... | @@ -550,7 +551,7 @@ mir_extra: std.ArrayListUnmanaged(u32) = .{}, | ... | @@ -550,7 +551,7 @@ mir_extra: std.ArrayListUnmanaged(u32) = .{}, |
| 550 | initial_stack_value: WValue = .none, | 551 | initial_stack_value: WValue = .none, |
| 551 | /// Arguments of this function declaration | 552 | /// Arguments of this function declaration |
| 552 | /// This will be set after `resolveCallingConventionValues` | 553 | /// This will be set after `resolveCallingConventionValues` |
| 553 | args: []WValue = undefined, | 554 | args: []WValue = &.{}, |
| 554 | /// This will only be `.none` if the function returns void, or returns an immediate. | 555 | /// This will only be `.none` if the function returns void, or returns an immediate. |
| 555 | /// When it returns a pointer to the stack, the `.local` tag will be active and must be populated | 556 | /// When it returns a pointer to the stack, the `.local` tag will be active and must be populated |
| 556 | /// before this function returns its execution to the caller. | 557 | /// before this function returns its execution to the caller. |
| ... | @@ -562,8 +563,6 @@ const InnerError = error{ | ... | @@ -562,8 +563,6 @@ const InnerError = error{ |
| 562 | CodegenFail, | 563 | CodegenFail, |
| 563 | /// Can occur when dereferencing a pointer that points to a `Decl` of which the analysis has failed | 564 | /// Can occur when dereferencing a pointer that points to a `Decl` of which the analysis has failed |
| 564 | AnalysisFail, | 565 | AnalysisFail, |
| 565 | /// Failed to emit MIR instructions to binary/textual representation. | | |
| 566 | EmitFail, | | |
| 567 | /// Compiler implementation could not handle a large integer. | 566 | /// Compiler implementation could not handle a large integer. |
| 568 | Overflow, | 567 | Overflow, |
| 569 | }; | 568 | }; |
| ... | @@ -800,7 +799,7 @@ pub fn genFunc(self: *Self) InnerError!Result { | ... | @@ -800,7 +799,7 @@ pub fn genFunc(self: *Self) InnerError!Result { |
| 800 | emit.emitMir() catch |err| switch (err) { | 799 | emit.emitMir() catch |err| switch (err) { |
| 801 | error.EmitFail => { | 800 | error.EmitFail => { |
| 802 | self.err_msg = emit.error_msg.?; | 801 | self.err_msg = emit.error_msg.?; |
| 803 | return error.EmitFail; | 802 | return error.CodegenFail; |
| 804 | }, | 803 | }, |
| 805 | else => |e| return e, | 804 | else => |e| return e, |
| 806 | }; | 805 | }; |
| ... | @@ -809,8 +808,34 @@ pub fn genFunc(self: *Self) InnerError!Result { | ... | @@ -809,8 +808,34 @@ pub fn genFunc(self: *Self) InnerError!Result { |
| 809 | return Result.appended; | 808 | return Result.appended; |
| 810 | } | 809 | } |
| 811 | | 810 | |
| | 811 | pub fn genDecl(self: *Self) InnerError!Result { |
| | 812 | const decl = self.decl; |
| | 813 | assert(decl.has_tv); |
| | 814 | |
| | 815 | log.debug("gen: {s} type: {}, value: {}", .{ decl.name, decl.ty, decl.val }); |
| | 816 | |
| | 817 | if (decl.val.castTag(.function)) |func_payload| { |
| | 818 | _ = func_payload; |
| | 819 | return self.fail("TODO wasm backend genDecl function pointer", .{}); |
| | 820 | } else if (decl.val.castTag(.extern_fn)) |extern_fn| { |
| | 821 | const ext_decl = extern_fn.data; |
| | 822 | var func_type = try self.genFunctype(ext_decl.ty); |
| | 823 | func_type.deinit(self.gpa); |
| | 824 | ext_decl.fn_link.wasm.type_index = try self.bin_file.putOrGetFuncType(func_type); |
| | 825 | return Result.appended; |
| | 826 | } else { |
| | 827 | const init_val = if (decl.val.castTag(.variable)) |payload| init_val: { |
| | 828 | break :init_val payload.data.init; |
| | 829 | } else decl.val; |
| | 830 | if (init_val.tag() != .unreachable_value) { |
| | 831 | return try self.genTypedValue(decl.ty, init_val); |
| | 832 | } |
| | 833 | return Result.appended; |
| | 834 | } |
| | 835 | } |
| | 836 | |
| 812 | /// Generates the wasm bytecode for the declaration belonging to `Context` | 837 | /// Generates the wasm bytecode for the declaration belonging to `Context` |
| 813 | pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { | 838 | fn genTypedValue(self: *Self, ty: Type, val: Value) InnerError!Result { |
| 814 | if (val.isUndef()) { | 839 | if (val.isUndef()) { |
| 815 | try self.code.appendNTimes(0xaa, @intCast(usize, ty.abiSize(self.target))); | 840 | try self.code.appendNTimes(0xaa, @intCast(usize, ty.abiSize(self.target))); |
| 816 | return Result.appended; | 841 | return Result.appended; |
| ... | @@ -822,16 +847,16 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { | ... | @@ -822,16 +847,16 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { |
| 822 | .function => val.castTag(.function).?.data.owner_decl, | 847 | .function => val.castTag(.function).?.data.owner_decl, |
| 823 | else => unreachable, | 848 | else => unreachable, |
| 824 | }; | 849 | }; |
| 825 | return try self.lowerDeclRef(fn_decl); | 850 | return try self.lowerDeclRef(ty, val, fn_decl); |
| 826 | }, | 851 | }, |
| 827 | .Optional => { | 852 | .Optional => { |
| 828 | var opt_buf: Type.Payload.ElemType = undefined; | 853 | var opt_buf: Type.Payload.ElemType = undefined; |
| 829 | const payload_type = ty.optionalChild(&opt_buf); | 854 | const payload_type = ty.optionalChild(&opt_buf); |
| 830 | if (ty.isPtrLikeOptional()) { | 855 | if (ty.isPtrLikeOptional()) { |
| 831 | if (val.castTag(.opt_payload)) |payload| { | 856 | if (val.castTag(.opt_payload)) |payload| { |
| 832 | return try self.genDecl(payload_type, payload.data); | 857 | return try self.genTypedValue(payload_type, payload.data); |
| 833 | } else if (!val.isNull()) { | 858 | } else if (!val.isNull()) { |
| 834 | return try self.genDecl(payload_type, val); | 859 | return try self.genTypedValue(payload_type, val); |
| 835 | } else { | 860 | } else { |
| 836 | try self.code.appendNTimes(0, @intCast(usize, ty.abiSize(self.target))); | 861 | try self.code.appendNTimes(0, @intCast(usize, ty.abiSize(self.target))); |
| 837 | return Result.appended; | 862 | return Result.appended; |
| ... | @@ -839,7 +864,7 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { | ... | @@ -839,7 +864,7 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { |
| 839 | } | 864 | } |
| 840 | // `null-tag` byte | 865 | // `null-tag` byte |
| 841 | try self.code.appendNTimes(@boolToInt(!val.isNull()), 4); | 866 | try self.code.appendNTimes(@boolToInt(!val.isNull()), 4); |
| 842 | const pl_result = try self.genDecl( | 867 | const pl_result = try self.genTypedValue( |
| 843 | payload_type, | 868 | payload_type, |
| 844 | if (val.castTag(.opt_payload)) |pl| pl.data else Value.initTag(.undef), | 869 | if (val.castTag(.opt_payload)) |pl| pl.data else Value.initTag(.undef), |
| 845 | ); | 870 | ); |
| ... | @@ -855,7 +880,7 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { | ... | @@ -855,7 +880,7 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { |
| 855 | if (ty.sentinel()) |sentinel| { | 880 | if (ty.sentinel()) |sentinel| { |
| 856 | try self.code.appendSlice(payload.data); | 881 | try self.code.appendSlice(payload.data); |
| 857 | | 882 | |
| 858 | switch (try self.genDecl(ty.childType(), sentinel)) { | 883 | switch (try self.genTypedValue(ty.childType(), sentinel)) { |
| 859 | .appended => return Result.appended, | 884 | .appended => return Result.appended, |
| 860 | .externally_managed => |data| { | 885 | .externally_managed => |data| { |
| 861 | try self.code.appendSlice(data); | 886 | try self.code.appendSlice(data); |
| ... | @@ -869,22 +894,20 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { | ... | @@ -869,22 +894,20 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { |
| 869 | const elem_vals = val.castTag(.array).?.data; | 894 | const elem_vals = val.castTag(.array).?.data; |
| 870 | const elem_ty = ty.elemType(); | 895 | const elem_ty = ty.elemType(); |
| 871 | for (elem_vals) |elem_val| { | 896 | for (elem_vals) |elem_val| { |
| 872 | switch (try self.genDecl(elem_ty, elem_val)) { | 897 | switch (try self.genTypedValue(elem_ty, elem_val)) { |
| 873 | .appended => {}, | 898 | .appended => {}, |
| 874 | .externally_managed => |data| { | 899 | .externally_managed => |data| try self.code.appendSlice(data), |
| 875 | try self.code.appendSlice(data); | | |
| 876 | }, | | |
| 877 | } | 900 | } |
| 878 | } | 901 | } |
| 879 | return Result.appended; | 902 | return Result.appended; |
| 880 | }, | 903 | }, |
| 881 | else => return self.fail("TODO implement genDecl for array type value: {s}", .{@tagName(val.tag())}), | 904 | else => return self.fail("TODO implement genTypedValue for array type value: {s}", .{@tagName(val.tag())}), |
| 882 | }, | 905 | }, |
| 883 | .Int => { | 906 | .Int => { |
| 884 | const info = ty.intInfo(self.target); | 907 | const info = ty.intInfo(self.target); |
| 885 | const abi_size = @intCast(usize, ty.abiSize(self.target)); | 908 | const abi_size = @intCast(usize, ty.abiSize(self.target)); |
| 886 | // todo: Implement integer sizes larger than 64bits | 909 | // todo: Implement integer sizes larger than 64bits |
| 887 | if (info.bits > 64) return self.fail("TODO: Implement genDecl for integer bit size: {d}", .{info.bits}); | 910 | if (info.bits > 64) return self.fail("TODO: Implement genTypedValue for integer bit size: {d}", .{info.bits}); |
| 888 | var buf: [8]u8 = undefined; | 911 | var buf: [8]u8 = undefined; |
| 889 | if (info.signedness == .unsigned) { | 912 | if (info.signedness == .unsigned) { |
| 890 | std.mem.writeIntLittle(u64, &buf, val.toUnsignedInt()); | 913 | std.mem.writeIntLittle(u64, &buf, val.toUnsignedInt()); |
| ... | @@ -906,8 +929,7 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { | ... | @@ -906,8 +929,7 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { |
| 906 | for (field_vals) |field_val, index| { | 929 | for (field_vals) |field_val, index| { |
| 907 | const field_ty = ty.structFieldType(index); | 930 | const field_ty = ty.structFieldType(index); |
| 908 | if (!field_ty.hasCodeGenBits()) continue; | 931 | if (!field_ty.hasCodeGenBits()) continue; |
| 909 | | 932 | switch (try self.genTypedValue(field_ty, field_val)) { |
| 910 | switch (try self.genDecl(field_ty, field_val)) { | | |
| 911 | .appended => {}, | 933 | .appended => {}, |
| 912 | .externally_managed => |payload| try self.code.appendSlice(payload), | 934 | .externally_managed => |payload| try self.code.appendSlice(payload), |
| 913 | } | 935 | } |
| ... | @@ -923,21 +945,21 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { | ... | @@ -923,21 +945,21 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { |
| 923 | .Pointer => switch (val.tag()) { | 945 | .Pointer => switch (val.tag()) { |
| 924 | .variable => { | 946 | .variable => { |
| 925 | const decl = val.castTag(.variable).?.data.owner_decl; | 947 | const decl = val.castTag(.variable).?.data.owner_decl; |
| 926 | return try self.lowerDeclRef(decl); | 948 | return try self.lowerDeclRef(ty, val, decl); |
| 927 | }, | 949 | }, |
| 928 | .decl_ref => { | 950 | .decl_ref => { |
| 929 | const decl = val.castTag(.decl_ref).?.data; | 951 | const decl = val.castTag(.decl_ref).?.data; |
| 930 | return try self.lowerDeclRef(decl); | 952 | return try self.lowerDeclRef(ty, val, decl); |
| 931 | }, | 953 | }, |
| 932 | .slice => { | 954 | .slice => { |
| 933 | const slice = val.castTag(.slice).?.data; | 955 | const slice = val.castTag(.slice).?.data; |
| 934 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; | 956 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 935 | const ptr_ty = ty.slicePtrFieldType(&buf); | 957 | const ptr_ty = ty.slicePtrFieldType(&buf); |
| 936 | switch (try self.genDecl(ptr_ty, slice.ptr)) { | 958 | switch (try self.genTypedValue(ptr_ty, slice.ptr)) { |
| 937 | .externally_managed => |data| try self.code.appendSlice(data), | 959 | .externally_managed => |data| try self.code.appendSlice(data), |
| 938 | .appended => {}, | 960 | .appended => {}, |
| 939 | } | 961 | } |
| 940 | switch (try self.genDecl(Type.usize, slice.len)) { | 962 | switch (try self.genTypedValue(Type.usize, slice.len)) { |
| 941 | .externally_managed => |data| try self.code.appendSlice(data), | 963 | .externally_managed => |data| try self.code.appendSlice(data), |
| 942 | .appended => {}, | 964 | .appended => {}, |
| 943 | } | 965 | } |
| ... | @@ -949,13 +971,25 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { | ... | @@ -949,13 +971,25 @@ pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result { |
| 949 | } | 971 | } |
| 950 | } | 972 | } |
| 951 | | 973 | |
| 952 | fn lowerDeclRef(self: *Self, decl: *Module.Decl) InnerError!Result { | 974 | fn lowerDeclRef(self: *Self, ty: Type, val: Value, decl: *Module.Decl) InnerError!Result { |
| 953 | decl.alive = true; | 975 | if (ty.isSlice()) { |
| | 976 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| | 977 | const slice_ty = ty.slicePtrFieldType(&buf); |
| | 978 | switch (try self.genTypedValue(slice_ty, val)) { |
| | 979 | .appended => {}, |
| | 980 | .externally_managed => |payload| try self.code.appendSlice(payload), |
| | 981 | } |
| | 982 | var slice_len: Value.Payload.U64 = .{ |
| | 983 | .base = .{ .tag = .int_u64 }, |
| | 984 | .data = val.sliceLen(), |
| | 985 | }; |
| | 986 | return try self.genTypedValue(Type.usize, Value.initPayload(&slice_len.base)); |
| | 987 | } |
| 954 | | 988 | |
| 955 | const offset = @intCast(u32, self.code.items.len); | 989 | const offset = @intCast(u32, self.code.items.len); |
| 956 | const atom = &self.decl.link.wasm; | 990 | const atom = &self.decl.link.wasm; |
| 957 | const target_sym_index = decl.link.wasm.sym_index; | 991 | const target_sym_index = decl.link.wasm.sym_index; |
| 958 | | 992 | decl.alive = true; |
| 959 | if (decl.ty.zigTypeTag() == .Fn) { | 993 | if (decl.ty.zigTypeTag() == .Fn) { |
| 960 | // We found a function pointer, so add it to our table, | 994 | // We found a function pointer, so add it to our table, |
| 961 | // as function pointers are not allowed to be stored inside the data section, | 995 | // as function pointers are not allowed to be stored inside the data section, |
| ... | @@ -1014,7 +1048,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu | ... | @@ -1014,7 +1048,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu |
| 1014 | | 1048 | |
| 1015 | const ret_ty = fn_ty.fnReturnType(); | 1049 | const ret_ty = fn_ty.fnReturnType(); |
| 1016 | switch (ret_ty.zigTypeTag()) { | 1050 | switch (ret_ty.zigTypeTag()) { |
| 1017 | .ErrorUnion, .Optional => result.return_value = try self.allocLocal(Type.initTag(.i32)), | 1051 | .ErrorUnion, .Optional, .Pointer => result.return_value = try self.allocLocal(Type.initTag(.i32)), |
| 1018 | .Int, .Float, .Bool, .Void, .NoReturn => {}, | 1052 | .Int, .Float, .Bool, .Void, .NoReturn => {}, |
| 1019 | else => return self.fail("TODO: Implement function return type {}", .{ret_ty}), | 1053 | else => return self.fail("TODO: Implement function return type {}", .{ret_ty}), |
| 1020 | } | 1054 | } |
| ... | @@ -1028,6 +1062,11 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu | ... | @@ -1028,6 +1062,11 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu |
| 1028 | }; | 1062 | }; |
| 1029 | | 1063 | |
| 1030 | try self.moveStack(offset, result.return_value.local); | 1064 | try self.moveStack(offset, result.return_value.local); |
| | 1065 | |
| | 1066 | // We want to make sure the return value's stack value doesn't get overwritten, |
| | 1067 | // so set initial stack value to current's position instead. |
| | 1068 | try self.addLabel(.global_get, 0); |
| | 1069 | try self.addLabel(.local_set, self.initial_stack_value.local); |
| 1031 | } | 1070 | } |
| 1032 | }, | 1071 | }, |
| 1033 | else => return self.fail("TODO implement function parameters for cc '{}' on wasm", .{cc}), | 1072 | else => return self.fail("TODO implement function parameters for cc '{}' on wasm", .{cc}), |
| ... | @@ -1042,10 +1081,6 @@ fn initializeStack(self: *Self) !void { | ... | @@ -1042,10 +1081,6 @@ fn initializeStack(self: *Self) !void { |
| 1042 | assert(self.initial_stack_value == .none); | 1081 | assert(self.initial_stack_value == .none); |
| 1043 | // reserve space for immediate value | 1082 | // reserve space for immediate value |
| 1044 | // get stack pointer global | 1083 | // get stack pointer global |
| 1045 | // TODO: For now, we hardcode the stack pointer to index '0', | | |
| 1046 | // once the linker is further implemented, we can replace this by inserting | | |
| 1047 | // a relocation and have the linker resolve the correct index to the stack pointer global. | | |
| 1048 | // NOTE: relocations of the type GLOBAL_INDEX_LEB are 5-bytes big | | |
| 1049 | try self.addLabel(.global_get, 0); | 1084 | try self.addLabel(.global_get, 0); |
| 1050 | | 1085 | |
| 1051 | // Reserve a local to store the current stack pointer | 1086 | // Reserve a local to store the current stack pointer |
| ... | @@ -1074,8 +1109,6 @@ fn restoreStackPointer(self: *Self) !void { | ... | @@ -1074,8 +1109,6 @@ fn restoreStackPointer(self: *Self) !void { |
| 1074 | /// the result back into the stack pointer. | 1109 | /// the result back into the stack pointer. |
| 1075 | fn moveStack(self: *Self, offset: u32, local: u32) !void { | 1110 | fn moveStack(self: *Self, offset: u32, local: u32) !void { |
| 1076 | if (offset == 0) return; | 1111 | if (offset == 0) return; |
| 1077 | // TODO: Rather than hardcode the stack pointer to position 0, | | |
| 1078 | // have the linker resolve its relocation | | |
| 1079 | try self.addLabel(.global_get, 0); | 1112 | try self.addLabel(.global_get, 0); |
| 1080 | try self.addImm32(@bitCast(i32, offset)); | 1113 | try self.addImm32(@bitCast(i32, offset)); |
| 1081 | try self.addTag(.i32_sub); | 1114 | try self.addTag(.i32_sub); |
| ... | @@ -1083,6 +1116,13 @@ fn moveStack(self: *Self, offset: u32, local: u32) !void { | ... | @@ -1083,6 +1116,13 @@ fn moveStack(self: *Self, offset: u32, local: u32) !void { |
| 1083 | try self.addLabel(.global_set, 0); | 1116 | try self.addLabel(.global_set, 0); |
| 1084 | } | 1117 | } |
| 1085 | | 1118 | |
| | 1119 | /// From given zig bitsize, returns the wasm bitsize |
| | 1120 | fn toWasmIntBits(bits: u16) ?u16 { |
| | 1121 | return for ([_]u16{ 32, 64 }) |wasm_bits| { |
| | 1122 | if (bits <= wasm_bits) return wasm_bits; |
| | 1123 | } else null; |
| | 1124 | } |
| | 1125 | |
| 1086 | fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { | 1126 | fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { |
| 1087 | const air_tags = self.air.instructions.items(.tag); | 1127 | const air_tags = self.air.instructions.items(.tag); |
| 1088 | return switch (air_tags[inst]) { | 1128 | return switch (air_tags[inst]) { |
| ... | @@ -1129,11 +1169,15 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { | ... | @@ -1129,11 +1169,15 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { |
| 1129 | .load => self.airLoad(inst), | 1169 | .load => self.airLoad(inst), |
| 1130 | .loop => self.airLoop(inst), | 1170 | .loop => self.airLoop(inst), |
| 1131 | .not => self.airNot(inst), | 1171 | .not => self.airNot(inst), |
| | 1172 | .optional_payload => self.airOptionalPayload(inst), |
| | 1173 | .optional_payload_ptr => self.airOptionalPayload(inst), |
| | 1174 | .optional_payload_ptr_set => self.airOptionalPayloadPtrSet(inst), |
| 1132 | .ret => self.airRet(inst), | 1175 | .ret => self.airRet(inst), |
| 1133 | .ret_ptr => self.airRetPtr(inst), | 1176 | .ret_ptr => self.airRetPtr(inst), |
| 1134 | .ret_load => self.airRetLoad(inst), | 1177 | .ret_load => self.airRetLoad(inst), |
| 1135 | .slice_len => self.airSliceLen(inst), | 1178 | .slice_len => self.airSliceLen(inst), |
| 1136 | .slice_elem_val => self.airSliceElemVal(inst), | 1179 | .slice_elem_val => self.airSliceElemVal(inst), |
| | 1180 | .slice_ptr => self.airSlicePtr(inst), |
| 1137 | .store => self.airStore(inst), | 1181 | .store => self.airStore(inst), |
| 1138 | .struct_field_ptr => self.airStructFieldPtr(inst), | 1182 | .struct_field_ptr => self.airStructFieldPtr(inst), |
| 1139 | .struct_field_ptr_index_0 => self.airStructFieldPtrIndex(inst, 0), | 1183 | .struct_field_ptr_index_0 => self.airStructFieldPtrIndex(inst, 0), |
| ... | @@ -1142,16 +1186,14 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { | ... | @@ -1142,16 +1186,14 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { |
| 1142 | .struct_field_ptr_index_3 => self.airStructFieldPtrIndex(inst, 3), | 1186 | .struct_field_ptr_index_3 => self.airStructFieldPtrIndex(inst, 3), |
| 1143 | .struct_field_val => self.airStructFieldVal(inst), | 1187 | .struct_field_val => self.airStructFieldVal(inst), |
| 1144 | .switch_br => self.airSwitchBr(inst), | 1188 | .switch_br => self.airSwitchBr(inst), |
| | 1189 | .trunc => self.airTrunc(inst), |
| 1145 | .unreach => self.airUnreachable(inst), | 1190 | .unreach => self.airUnreachable(inst), |
| 1146 | .wrap_optional => self.airWrapOptional(inst), | | |
| 1147 | | 1191 | |
| | 1192 | .wrap_optional => self.airWrapOptional(inst), |
| 1148 | .unwrap_errunion_payload => self.airUnwrapErrUnionPayload(inst), | 1193 | .unwrap_errunion_payload => self.airUnwrapErrUnionPayload(inst), |
| 1149 | .unwrap_errunion_err => self.airUnwrapErrUnionError(inst), | 1194 | .unwrap_errunion_err => self.airUnwrapErrUnionError(inst), |
| 1150 | .wrap_errunion_payload => self.airWrapErrUnionPayload(inst), | 1195 | .wrap_errunion_payload => self.airWrapErrUnionPayload(inst), |
| 1151 | | 1196 | .wrap_errunion_err => self.airWrapErrUnionErr(inst), |
| 1152 | .optional_payload => self.airOptionalPayload(inst), | | |
| 1153 | .optional_payload_ptr => self.airOptionalPayload(inst), | | |
| 1154 | .optional_payload_ptr_set => self.airOptionalPayloadPtrSet(inst), | | |
| 1155 | else => |tag| self.fail("TODO: Implement wasm inst: {s}", .{@tagName(tag)}), | 1197 | else => |tag| self.fail("TODO: Implement wasm inst: {s}", .{@tagName(tag)}), |
| 1156 | }; | 1198 | }; |
| 1157 | } | 1199 | } |
| ... | @@ -1193,15 +1235,15 @@ fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -1193,15 +1235,15 @@ fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1193 | // local, containing the offset to the stack position | 1235 | // local, containing the offset to the stack position |
| 1194 | const local = try self.allocLocal(Type.initTag(.i32)); // always pointer therefore i32 | 1236 | const local = try self.allocLocal(Type.initTag(.i32)); // always pointer therefore i32 |
| 1195 | try self.moveStack(@intCast(u32, abi_size), local.local); | 1237 | try self.moveStack(@intCast(u32, abi_size), local.local); |
| 1196 | | | |
| 1197 | return local; | 1238 | return local; |
| 1198 | } | 1239 | } |
| 1199 | | 1240 | |
| 1200 | fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | 1241 | fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1201 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 1242 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 1202 | const operand = self.resolveInst(un_op); | 1243 | const operand = self.resolveInst(un_op); |
| 1203 | const result = try self.load(operand, self.air.typeOf(un_op), 0); | 1244 | |
| 1204 | try self.addLabel(.local_get, result.local); | 1245 | const result = try self.load(operand, self.air.typeOf(un_op).childType(), 0); |
| | 1246 | try self.emitWValue(result); |
| 1205 | try self.restoreStackPointer(); | 1247 | try self.restoreStackPointer(); |
| 1206 | try self.addTag(.@"return"); | 1248 | try self.addTag(.@"return"); |
| 1207 | return .none; | 1249 | return .none; |
| ... | @@ -1231,8 +1273,25 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -1231,8 +1273,25 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1231 | }; | 1273 | }; |
| 1232 | | 1274 | |
| 1233 | for (args) |arg| { | 1275 | for (args) |arg| { |
| 1234 | const arg_val = self.resolveInst(@intToEnum(Air.Inst.Ref, arg)); | 1276 | const arg_ref = @intToEnum(Air.Inst.Ref, arg); |
| 1235 | try self.emitWValue(arg_val); | 1277 | const arg_val = self.resolveInst(arg_ref); |
| | 1278 | |
| | 1279 | const arg_ty = self.air.typeOf(arg_ref); |
| | 1280 | switch (arg_ty.zigTypeTag()) { |
| | 1281 | .Struct, .Pointer, .Optional, .ErrorUnion => { |
| | 1282 | // single pointer can be passed directly |
| | 1283 | if (arg_ty.isSinglePointer() or arg_val != .constant) { |
| | 1284 | try self.emitWValue(arg_val); |
| | 1285 | continue; |
| | 1286 | } |
| | 1287 | const abi_size = arg_ty.abiSize(self.target); |
| | 1288 | const arg_local = try self.allocLocal(Type.initTag(.i32)); |
| | 1289 | try self.moveStack(@intCast(u32, abi_size), arg_local.local); |
| | 1290 | try self.store(arg_local, arg_val, arg_ty, 0); |
| | 1291 | try self.emitWValue(arg_local); |
| | 1292 | }, |
| | 1293 | else => try self.emitWValue(arg_val), |
| | 1294 | } |
| 1236 | } | 1295 | } |
| 1237 | | 1296 | |
| 1238 | if (target) |direct| { | 1297 | if (target) |direct| { |
| ... | @@ -1242,8 +1301,11 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -1242,8 +1301,11 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1242 | // so load its value onto the stack | 1301 | // so load its value onto the stack |
| 1243 | std.debug.assert(ty.zigTypeTag() == .Pointer); | 1302 | std.debug.assert(ty.zigTypeTag() == .Pointer); |
| 1244 | const operand = self.resolveInst(pl_op.operand); | 1303 | const operand = self.resolveInst(pl_op.operand); |
| 1245 | try self.emitWValue(operand); | 1304 | const offset = switch (operand) { |
| 1246 | const result = try self.load(operand, fn_ty, operand.local_with_offset.offset); | 1305 | .local_with_offset => |with_offset| with_offset.offset, |
| | 1306 | else => @as(u32, 0), |
| | 1307 | }; |
| | 1308 | const result = try self.load(operand, fn_ty, offset); |
| 1247 | try self.addLabel(.local_get, result.local); | 1309 | try self.addLabel(.local_get, result.local); |
| 1248 | | 1310 | |
| 1249 | var fn_type = try self.genFunctype(fn_ty); | 1311 | var fn_type = try self.genFunctype(fn_ty); |
| ... | @@ -1254,14 +1316,12 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -1254,14 +1316,12 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1254 | } | 1316 | } |
| 1255 | | 1317 | |
| 1256 | const ret_ty = fn_ty.fnReturnType(); | 1318 | const ret_ty = fn_ty.fnReturnType(); |
| 1257 | switch (ret_ty.zigTypeTag()) { | 1319 | if (!ret_ty.hasCodeGenBits()) return WValue.none; |
| 1258 | .Void, .NoReturn => return WValue.none, | 1320 | |
| 1259 | else => { | 1321 | const result_local = try self.allocLocal(ret_ty); |
| 1260 | const result_local = try self.allocLocal(ret_ty); | 1322 | try self.addLabel(.local_set, result_local.local); |
| 1261 | try self.addLabel(.local_set, result_local.local); | 1323 | // if the result was allocated on the virtual stack, we must load |
| 1262 | return result_local; | 1324 | return result_local; |
| 1263 | }, | | |
| 1264 | } | | |
| 1265 | } | 1325 | } |
| 1266 | | 1326 | |
| 1267 | fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | 1327 | fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| ... | @@ -1278,7 +1338,6 @@ fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -1278,7 +1338,6 @@ fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1278 | // local, containing the offset to the stack position | 1338 | // local, containing the offset to the stack position |
| 1279 | const local = try self.allocLocal(Type.initTag(.i32)); // always pointer therefore i32 | 1339 | const local = try self.allocLocal(Type.initTag(.i32)); // always pointer therefore i32 |
| 1280 | try self.moveStack(@intCast(u32, abi_size), local.local); | 1340 | try self.moveStack(@intCast(u32, abi_size), local.local); |
| 1281 | | | |
| 1282 | return local; | 1341 | return local; |
| 1283 | } | 1342 | } |
| 1284 | | 1343 | |
| ... | @@ -1330,14 +1389,16 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro | ... | @@ -1330,14 +1389,16 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro |
| 1330 | .local => { | 1389 | .local => { |
| 1331 | // Load values from `rhs` stack position and store in `lhs` instead | 1390 | // Load values from `rhs` stack position and store in `lhs` instead |
| 1332 | const tag_local = try self.load(rhs, tag_ty, 0); | 1391 | const tag_local = try self.load(rhs, tag_ty, 0); |
| 1333 | const payload_local = try self.load(rhs, payload_ty, payload_offset); | 1392 | if (payload_ty.hasCodeGenBits()) { |
| 1334 | | 1393 | const payload_local = try self.load(rhs, payload_ty, payload_offset); |
| 1335 | try self.store(lhs, tag_local, tag_ty, 0); | 1394 | try self.store(lhs, payload_local, payload_ty, payload_offset); |
| 1336 | return try self.store(lhs, payload_local, payload_ty, payload_offset); | 1395 | } |
| | 1396 | return try self.store(lhs, tag_local, tag_ty, 0); |
| 1337 | }, | 1397 | }, |
| 1338 | .local_with_offset => |with_offset| { | 1398 | .local_with_offset => |with_offset| { |
| 1339 | const tag_local = try self.allocLocal(tag_ty); | 1399 | const tag_local = try self.allocLocal(tag_ty); |
| 1340 | try self.addImm32(0); | 1400 | try self.addImm32(0); |
| | 1401 | try self.addLabel(.local_set, tag_local.local); |
| 1341 | try self.store(lhs, tag_local, tag_ty, 0); | 1402 | try self.store(lhs, tag_local, tag_ty, 0); |
| 1342 | | 1403 | |
| 1343 | return try self.store( | 1404 | return try self.store( |
| ... | @@ -1366,6 +1427,18 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro | ... | @@ -1366,6 +1427,18 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro |
| 1366 | } | 1427 | } |
| 1367 | return; | 1428 | return; |
| 1368 | }, | 1429 | }, |
| | 1430 | .Pointer => { |
| | 1431 | if (ty.isSlice() and rhs == .constant) { |
| | 1432 | try self.emitWValue(rhs); |
| | 1433 | const len_local = try self.allocLocal(Type.usize); |
| | 1434 | const ptr_local = try self.allocLocal(Type.usize); |
| | 1435 | try self.addLabel(.local_set, len_local.local); |
| | 1436 | try self.addLabel(.local_set, ptr_local.local); |
| | 1437 | try self.store(lhs, ptr_local, Type.usize, 0); |
| | 1438 | try self.store(lhs, len_local, Type.usize, self.target.cpu.arch.ptrBitWidth() / 8); |
| | 1439 | return; |
| | 1440 | } |
| | 1441 | }, |
| 1369 | else => {}, | 1442 | else => {}, |
| 1370 | } | 1443 | } |
| 1371 | try self.emitWValue(lhs); | 1444 | try self.emitWValue(lhs); |
| ... | @@ -1402,6 +1475,8 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -1402,6 +1475,8 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1402 | const operand = self.resolveInst(ty_op.operand); | 1475 | const operand = self.resolveInst(ty_op.operand); |
| 1403 | const ty = self.air.getRefType(ty_op.ty); | 1476 | const ty = self.air.getRefType(ty_op.ty); |
| 1404 | | 1477 | |
| | 1478 | if (!ty.hasCodeGenBits()) return WValue{ .none = {} }; |
| | 1479 | |
| 1405 | return switch (ty.zigTypeTag()) { | 1480 | return switch (ty.zigTypeTag()) { |
| 1406 | .Struct, .ErrorUnion, .Optional, .Pointer => operand, // pass as pointer | 1481 | .Struct, .ErrorUnion, .Optional, .Pointer => operand, // pass as pointer |
| 1407 | else => switch (operand) { | 1482 | else => switch (operand) { |
| ... | @@ -1415,7 +1490,10 @@ fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue { | ... | @@ -1415,7 +1490,10 @@ fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue { |
| 1415 | // load local's value from memory by its stack position | 1490 | // load local's value from memory by its stack position |
| 1416 | try self.emitWValue(operand); | 1491 | try self.emitWValue(operand); |
| 1417 | // Build the opcode with the right bitsize | 1492 | // Build the opcode with the right bitsize |
| 1418 | const signedness: std.builtin.Signedness = if (ty.isUnsignedInt()) .unsigned else .signed; | 1493 | const signedness: std.builtin.Signedness = if (ty.isUnsignedInt() or ty.zigTypeTag() == .ErrorSet) |
| | 1494 | .unsigned |
| | 1495 | else |
| | 1496 | .signed; |
| 1419 | // check if we should pass by pointer or value based on ABI size | 1497 | // check if we should pass by pointer or value based on ABI size |
| 1420 | // TODO: Implement a way to get ABI values from a given type, | 1498 | // TODO: Implement a way to get ABI values from a given type, |
| 1421 | // that is portable across the backend, rather than copying logic. | 1499 | // that is portable across the backend, rather than copying logic. |
| ... | @@ -1526,6 +1604,7 @@ fn airWrapBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { | ... | @@ -1526,6 +1604,7 @@ fn airWrapBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 1526 | } | 1604 | } |
| 1527 | | 1605 | |
| 1528 | fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void { | 1606 | fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void { |
| | 1607 | if (val.isUndefDeep()) return self.emitUndefined(ty); |
| 1529 | switch (ty.zigTypeTag()) { | 1608 | switch (ty.zigTypeTag()) { |
| 1530 | .Int => { | 1609 | .Int => { |
| 1531 | const int_info = ty.intInfo(self.target); | 1610 | const int_info = ty.intInfo(self.target); |
| ... | @@ -1553,10 +1632,21 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void { | ... | @@ -1553,10 +1632,21 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void { |
| 1553 | } | 1632 | } |
| 1554 | }, | 1633 | }, |
| 1555 | .Pointer => { | 1634 | .Pointer => { |
| 1556 | if (val.castTag(.decl_ref)) |payload| { | 1635 | if (val.castTag(.slice)) |slice| { |
| | 1636 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| | 1637 | try self.emitConstant(slice.data.ptr, ty.slicePtrFieldType(&buf)); |
| | 1638 | try self.emitConstant(slice.data.len, Type.usize); |
| | 1639 | } else if (val.castTag(.decl_ref)) |payload| { |
| 1557 | const decl = payload.data; | 1640 | const decl = payload.data; |
| 1558 | decl.alive = true; | 1641 | decl.alive = true; |
| 1559 | try self.addLabel(.memory_address, decl.link.wasm.sym_index); | 1642 | // Function pointers use a table index, rather than a memory address |
| | 1643 | if (decl.ty.zigTypeTag() == .Fn) { |
| | 1644 | const target_sym_index = decl.link.wasm.sym_index; |
| | 1645 | try self.bin_file.addTableFunction(target_sym_index); |
| | 1646 | try self.addLabel(.function_index, target_sym_index); |
| | 1647 | } else { |
| | 1648 | try self.addLabel(.memory_address, decl.link.wasm.sym_index); |
| | 1649 | } |
| 1560 | } else return self.fail("Wasm TODO: emitConstant for other const pointer tag {s}", .{val.tag()}); | 1650 | } else return self.fail("Wasm TODO: emitConstant for other const pointer tag {s}", .{val.tag()}); |
| 1561 | }, | 1651 | }, |
| 1562 | .Void => {}, | 1652 | .Void => {}, |
| ... | @@ -1631,6 +1721,22 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void { | ... | @@ -1631,6 +1721,22 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void { |
| 1631 | } | 1721 | } |
| 1632 | } | 1722 | } |
| 1633 | | 1723 | |
| | 1724 | fn emitUndefined(self: *Self, ty: Type) InnerError!void { |
| | 1725 | switch (ty.zigTypeTag()) { |
| | 1726 | .Int => switch (ty.intInfo(self.target).bits) { |
| | 1727 | 0...32 => try self.addImm32(@bitCast(i32, @as(u32, 0xaaaaaaaa))), |
| | 1728 | 33...64 => try self.addImm64(0xaaaaaaaaaaaaaaaa), |
| | 1729 | else => |bits| return self.fail("Wasm TODO: emitUndefined for integer bitsize: {d}", .{bits}), |
| | 1730 | }, |
| | 1731 | .Float => switch (ty.floatBits(self.target)) { |
| | 1732 | 0...32 => try self.addInst(.{ .tag = .f32_const, .data = .{ .float32 = @bitCast(f32, @as(u32, 0xaaaaaaaa)) } }), |
| | 1733 | 33...64 => try self.addFloat64(@bitCast(f64, @as(u64, 0xaaaaaaaaaaaaaaaa))), |
| | 1734 | else => |bits| return self.fail("Wasm TODO: emitUndefined for float bitsize: {d}", .{bits}), |
| | 1735 | }, |
| | 1736 | else => return self.fail("Wasm TODO: emitUndefined for type: {}\n", .{ty}), |
| | 1737 | } |
| | 1738 | } |
| | 1739 | |
| 1634 | /// Returns a `Value` as a signed 32 bit value. | 1740 | /// Returns a `Value` as a signed 32 bit value. |
| 1635 | /// It's illegal to provide a value with a type that cannot be represented | 1741 | /// It's illegal to provide a value with a type that cannot be represented |
| 1636 | /// as an integer value. | 1742 | /// as an integer value. |
| ... | @@ -1781,7 +1887,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) Inner | ... | @@ -1781,7 +1887,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) Inner |
| 1781 | }); | 1887 | }); |
| 1782 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 1888 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 1783 | | 1889 | |
| 1784 | const cmp_tmp = try self.allocLocal(lhs_ty); | 1890 | const cmp_tmp = try self.allocLocal(Type.initTag(.i32)); // bool is always i32 |
| 1785 | try self.addLabel(.local_set, cmp_tmp.local); | 1891 | try self.addLabel(.local_set, cmp_tmp.local); |
| 1786 | return cmp_tmp; | 1892 | return cmp_tmp; |
| 1787 | } | 1893 | } |
| ... | @@ -2081,9 +2187,25 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -2081,9 +2187,25 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2081 | } | 2187 | } |
| 2082 | | 2188 | |
| 2083 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | 2189 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| | 2190 | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2084 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 2191 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2085 | _ = ty_op; | 2192 | const operand = self.resolveInst(ty_op.operand); |
| 2086 | return self.fail("TODO: wasm airWrapErrUnionPayload", .{}); | 2193 | |
| | 2194 | const op_ty = self.air.typeOf(ty_op.operand); |
| | 2195 | if (!op_ty.hasCodeGenBits()) return WValue.none; |
| | 2196 | const err_ty = self.air.getRefType(ty_op.ty); |
| | 2197 | const offset = err_ty.errorUnionSet().abiSize(self.target); |
| | 2198 | |
| | 2199 | return WValue{ .local_with_offset = .{ |
| | 2200 | .local = operand.local, |
| | 2201 | .offset = @intCast(u32, offset), |
| | 2202 | } }; |
| | 2203 | } |
| | 2204 | |
| | 2205 | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| | 2206 | if (self.liveness.isUnused(inst)) return WValue.none; |
| | 2207 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| | 2208 | return self.resolveInst(ty_op.operand); |
| 2087 | } | 2209 | } |
| 2088 | | 2210 | |
| 2089 | fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | 2211 | fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| ... | @@ -2092,20 +2214,26 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -2092,20 +2214,26 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2092 | const operand = self.resolveInst(ty_op.operand); | 2214 | const operand = self.resolveInst(ty_op.operand); |
| 2093 | const ref_ty = self.air.typeOf(ty_op.operand); | 2215 | const ref_ty = self.air.typeOf(ty_op.operand); |
| 2094 | const ref_info = ref_ty.intInfo(self.target); | 2216 | const ref_info = ref_ty.intInfo(self.target); |
| 2095 | const op_bits = ref_info.bits; | 2217 | const wanted_info = ty.intInfo(self.target); |
| 2096 | const wanted_bits = ty.intInfo(self.target).bits; | | |
| 2097 | | 2218 | |
| 2098 | if (op_bits > 32 and wanted_bits <= 32) { | 2219 | const op_bits = toWasmIntBits(ref_info.bits) orelse |
| | 2220 | return self.fail("TODO: Wasm intcast integer types of bitsize: {d}", .{ref_info.bits}); |
| | 2221 | const wanted_bits = toWasmIntBits(wanted_info.bits) orelse |
| | 2222 | return self.fail("TODO: Wasm intcast integer types of bitsize: {d}", .{wanted_info.bits}); |
| | 2223 | |
| | 2224 | // hot path |
| | 2225 | if (op_bits == wanted_bits) return operand; |
| | 2226 | |
| | 2227 | if (op_bits > 32 and wanted_bits == 32) { |
| 2099 | try self.emitWValue(operand); | 2228 | try self.emitWValue(operand); |
| 2100 | try self.addTag(.i32_wrap_i64); | 2229 | try self.addTag(.i32_wrap_i64); |
| 2101 | } else if (op_bits <= 32 and wanted_bits > 32) { | 2230 | } else if (op_bits == 32 and wanted_bits > 32) { |
| 2102 | try self.emitWValue(operand); | 2231 | try self.emitWValue(operand); |
| 2103 | try self.addTag(switch (ref_info.signedness) { | 2232 | try self.addTag(switch (ref_info.signedness) { |
| 2104 | .signed => .i64_extend_i32_s, | 2233 | .signed => .i64_extend_i32_s, |
| 2105 | .unsigned => .i64_extend_i32_u, | 2234 | .unsigned => .i64_extend_i32_u, |
| 2106 | }); | 2235 | }); |
| 2107 | } else return operand; | 2236 | } |
| 2108 | | | |
| 2109 | const result = try self.allocLocal(ty); | 2237 | const result = try self.allocLocal(ty); |
| 2110 | try self.addLabel(.local_set, result.local); | 2238 | try self.addLabel(.local_set, result.local); |
| 2111 | return result; | 2239 | return result; |
| ... | @@ -2180,19 +2308,7 @@ fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -2180,19 +2308,7 @@ fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2180 | const operand = self.resolveInst(ty_op.operand); | 2308 | const operand = self.resolveInst(ty_op.operand); |
| 2181 | const pointer_width = self.target.cpu.arch.ptrBitWidth() / 8; | 2309 | const pointer_width = self.target.cpu.arch.ptrBitWidth() / 8; |
| 2182 | | 2310 | |
| 2183 | // Get pointer to slice | 2311 | return try self.load(operand, Type.usize, pointer_width); |
| 2184 | try self.emitWValue(operand); | | |
| 2185 | // length of slice is stored after the pointer of the slice | | |
| 2186 | const extra_index = try self.addExtra(Mir.MemArg{ | | |
| 2187 | .offset = pointer_width, | | |
| 2188 | .alignment = pointer_width, | | |
| 2189 | }); | | |
| 2190 | try self.addInst(.{ .tag = .i32_load, .data = .{ .payload = extra_index } }); | | |
| 2191 | | | |
| 2192 | const result = try self.allocLocal(Type.initTag(.i32)); // pointer is always i32 | | |
| 2193 | // store slice length in local | | |
| 2194 | try self.addLabel(.local_set, result.local); | | |
| 2195 | return result; | | |
| 2196 | } | 2312 | } |
| 2197 | | 2313 | |
| 2198 | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | 2314 | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| ... | @@ -2206,7 +2322,8 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -2206,7 +2322,8 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2206 | const elem_size = elem_ty.abiSize(self.target); | 2322 | const elem_size = elem_ty.abiSize(self.target); |
| 2207 | | 2323 | |
| 2208 | // load pointer onto stack | 2324 | // load pointer onto stack |
| 2209 | try self.emitWValue(slice); | 2325 | const slice_ptr = try self.load(slice, slice_ty, 0); |
| | 2326 | try self.addLabel(.local_get, slice_ptr.local); |
| 2210 | | 2327 | |
| 2211 | // calculate index into slice | 2328 | // calculate index into slice |
| 2212 | try self.emitWValue(index); | 2329 | try self.emitWValue(index); |
| ... | @@ -2214,25 +2331,79 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { | ... | @@ -2214,25 +2331,79 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2214 | try self.addTag(.i32_mul); | 2331 | try self.addTag(.i32_mul); |
| 2215 | try self.addTag(.i32_add); | 2332 | try self.addTag(.i32_add); |
| 2216 | | 2333 | |
| 2217 | const abi_size = if (elem_size < 8) | 2334 | const result = try self.allocLocal(elem_ty); |
| 2218 | @intCast(u8, elem_size) | 2335 | try self.addLabel(.local_set, result.local); |
| 2219 | else | 2336 | return switch (elem_ty.zigTypeTag()) { |
| 2220 | @as(u8, 4); // elements larger than 8 bytes will be passed by pointer | 2337 | // pass as pointer |
| | 2338 | .Pointer, .Struct, .Optional => result, |
| | 2339 | // pass by value |
| | 2340 | else => try self.load(result, elem_ty, 0), |
| | 2341 | }; |
| | 2342 | } |
| 2221 | | 2343 | |
| 2222 | const extra_index = try self.addExtra(Mir.MemArg{ | 2344 | fn airSlicePtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2223 | .offset = 0, | 2345 | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2224 | .alignment = elem_ty.abiAlignment(self.target), | 2346 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2225 | }); | 2347 | const operand = self.resolveInst(ty_op.operand); |
| 2226 | const signedness: std.builtin.Signedness = if (elem_ty.isUnsignedInt()) .unsigned else .signed; | 2348 | return try self.load(operand, Type.usize, 0); |
| 2227 | const opcode = buildOpcode(.{ | 2349 | } |
| 2228 | .valtype1 = try self.typeToValtype(elem_ty), | 2350 | |
| 2229 | .width = abi_size * 8, | 2351 | fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2230 | .op = .load, | 2352 | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2231 | .signedness = signedness, | 2353 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2232 | }); | 2354 | const operand = self.resolveInst(ty_op.operand); |
| 2233 | try self.addInst(.{ .tag = Mir.Inst.Tag.fromOpcode(opcode), .data = .{ .payload = extra_index } }); | 2355 | const op_ty = self.air.typeOf(ty_op.operand); |
| | 2356 | const int_info = self.air.getRefType(ty_op.ty).intInfo(self.target); |
| | 2357 | const wanted_bits = int_info.bits; |
| | 2358 | const result = try self.allocLocal(self.air.getRefType(ty_op.ty)); |
| | 2359 | const op_bits = op_ty.intInfo(self.target).bits; |
| | 2360 | |
| | 2361 | const wasm_bits = toWasmIntBits(wanted_bits) orelse |
| | 2362 | return self.fail("TODO: Implement wasm integer truncation for integer bitsize: {d}", .{wanted_bits}); |
| | 2363 | |
| | 2364 | // Use wasm's instruction to wrap from 64bit to 32bit integer when possible |
| | 2365 | if (op_bits == 64 and wanted_bits == 32) { |
| | 2366 | try self.emitWValue(operand); |
| | 2367 | try self.addTag(.i32_wrap_i64); |
| | 2368 | try self.addLabel(.local_set, result.local); |
| | 2369 | return result; |
| | 2370 | } |
| | 2371 | |
| | 2372 | // Any other truncation must be done manually |
| | 2373 | if (int_info.signedness == .unsigned) { |
| | 2374 | const mask = (@as(u65, 1) << @intCast(u7, wanted_bits)) - 1; |
| | 2375 | try self.emitWValue(operand); |
| | 2376 | switch (wasm_bits) { |
| | 2377 | 32 => { |
| | 2378 | try self.addImm32(@bitCast(i32, @intCast(u32, mask))); |
| | 2379 | try self.addTag(.i32_and); |
| | 2380 | }, |
| | 2381 | 64 => { |
| | 2382 | try self.addImm64(@intCast(u64, mask)); |
| | 2383 | try self.addTag(.i64_and); |
| | 2384 | }, |
| | 2385 | else => unreachable, |
| | 2386 | } |
| | 2387 | } else { |
| | 2388 | const shift_bits = wasm_bits - wanted_bits; |
| | 2389 | try self.emitWValue(operand); |
| | 2390 | switch (wasm_bits) { |
| | 2391 | 32 => { |
| | 2392 | try self.addImm32(@bitCast(i16, shift_bits)); |
| | 2393 | try self.addTag(.i32_shl); |
| | 2394 | try self.addImm32(@bitCast(i16, shift_bits)); |
| | 2395 | try self.addTag(.i32_shr_s); |
| | 2396 | }, |
| | 2397 | 64 => { |
| | 2398 | try self.addImm64(shift_bits); |
| | 2399 | try self.addTag(.i64_shl); |
| | 2400 | try self.addImm64(shift_bits); |
| | 2401 | try self.addTag(.i64_shr_s); |
| | 2402 | }, |
| | 2403 | else => unreachable, |
| | 2404 | } |
| | 2405 | } |
| 2234 | | 2406 | |
| 2235 | const result = try self.allocLocal(elem_ty); | | |
| 2236 | try self.addLabel(.local_set, result.local); | 2407 | try self.addLabel(.local_set, result.local); |
| 2237 | return result; | 2408 | return result; |
| 2238 | } | 2409 | } |