| ... | ... | @@ -187,7 +187,13 @@ fn buildOpcode(args: OpcodeBuildArguments) wasm.Opcode { |
| 187 | 187 | 32 => switch (args.valtype1.?) { |
| 188 | 188 | .i64 => if (args.signedness.? == .signed) return .i64_load32_s else return .i64_load32_u, |
| 189 | 189 | .i32 => return .i32_load, |
| 190 | | .f32, .f64 => unreachable, |
| 190 | .f32 => return .f32_load, |
| 191 | .f64 => unreachable, |
| 192 | }, |
| 193 | 64 => switch (args.valtype1.?) { |
| 194 | .i64 => return .i64_load, |
| 195 | .f64 => return .f64_load, |
| 196 | else => unreachable, |
| 191 | 197 | }, |
| 192 | 198 | else => unreachable, |
| 193 | 199 | } else switch (args.valtype1.?) { |
| ... | ... | @@ -216,6 +222,7 @@ fn buildOpcode(args: OpcodeBuildArguments) wasm.Opcode { |
| 216 | 222 | }, |
| 217 | 223 | 64 => switch (args.valtype1.?) { |
| 218 | 224 | .i64 => return .i64_store, |
| 225 | .f64 => return .f64_store, |
| 219 | 226 | else => unreachable, |
| 220 | 227 | }, |
| 221 | 228 | else => unreachable, |
| ... | ... | @@ -505,7 +512,10 @@ gpa: *mem.Allocator, |
| 505 | 512 | /// Table to save `WValue`'s generated by an `Air.Inst` |
| 506 | 513 | values: ValueTable, |
| 507 | 514 | /// Mapping from Air.Inst.Index to block ids |
| 508 | | blocks: std.AutoArrayHashMapUnmanaged(Air.Inst.Index, u32) = .{}, |
| 515 | blocks: std.AutoArrayHashMapUnmanaged(Air.Inst.Index, struct { |
| 516 | label: u32, |
| 517 | value: WValue, |
| 518 | }) = .{}, |
| 509 | 519 | /// `bytes` contains the wasm bytecode belonging to the 'code' section. |
| 510 | 520 | code: ArrayList(u8), |
| 511 | 521 | /// Contains the generated function type bytecode for the current function |
| ... | ... | @@ -984,8 +994,8 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { |
| 984 | 994 | .dbg_stmt => WValue.none, |
| 985 | 995 | .intcast => self.airIntcast(inst), |
| 986 | 996 | |
| 987 | | .is_err => self.airIsErr(inst, .i32_eq), |
| 988 | | .is_non_err => self.airIsErr(inst, .i32_ne), |
| 997 | .is_err => self.airIsErr(inst, .i32_ne), |
| 998 | .is_non_err => self.airIsErr(inst, .i32_eq), |
| 989 | 999 | |
| 990 | 1000 | .is_null => self.airIsNull(inst, .i32_ne), |
| 991 | 1001 | .is_non_null => self.airIsNull(inst, .i32_eq), |
| ... | ... | @@ -1065,12 +1075,12 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1065 | 1075 | |
| 1066 | 1076 | const ret_ty = target.ty.fnReturnType(); |
| 1067 | 1077 | switch (ret_ty.zigTypeTag()) { |
| 1068 | | .ErrorUnion, .Optional => { |
| 1078 | .Void, .NoReturn => return WValue.none, |
| 1079 | else => { |
| 1069 | 1080 | const result_local = try self.allocLocal(ret_ty); |
| 1070 | 1081 | try self.addLabel(.local_set, result_local.local); |
| 1071 | 1082 | return result_local; |
| 1072 | 1083 | }, |
| 1073 | | else => return WValue.none, |
| 1074 | 1084 | } |
| 1075 | 1085 | } |
| 1076 | 1086 | |
| ... | ... | @@ -1086,7 +1096,7 @@ fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1086 | 1096 | if (abi_size == 0) return WValue{ .none = {} }; |
| 1087 | 1097 | |
| 1088 | 1098 | // local, containing the offset to the stack position |
| 1089 | | const local = try self.allocLocal(child_type); |
| 1099 | const local = try self.allocLocal(Type.initTag(.i32)); // always pointer therefore i32 |
| 1090 | 1100 | try self.moveStack(@intCast(u32, abi_size), local.local); |
| 1091 | 1101 | |
| 1092 | 1102 | return local; |
| ... | ... | @@ -1114,29 +1124,64 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro |
| 1114 | 1124 | var buf: Type.Payload.ElemType = undefined; |
| 1115 | 1125 | const payload_ty = if (ty.zigTypeTag() == .ErrorUnion) ty.errorUnionPayload() else ty.optionalChild(&buf); |
| 1116 | 1126 | const tag_ty = if (ty.zigTypeTag() == .ErrorUnion) ty.errorUnionSet() else Type.initTag(.u8); |
| 1117 | | const payload_offset = @intCast(u32, tag_ty.abiSize(self.target)); |
| 1118 | | |
| 1119 | | if (rhs == .constant) { |
| 1120 | | // constant will contain both tag and payload, |
| 1121 | | // so save those in 2 temporary locals before storing them |
| 1122 | | // in memory |
| 1123 | | try self.emitWValue(rhs); |
| 1124 | | const tag_local = try self.allocLocal(tag_ty); |
| 1125 | | const payload_local = try self.allocLocal(payload_ty); |
| 1126 | | |
| 1127 | | try self.addLabel(.local_set, payload_local.local); |
| 1128 | | try self.addLabel(.local_set, tag_local.local); |
| 1129 | | |
| 1130 | | try self.store(lhs, tag_local, tag_ty, 0); |
| 1131 | | return try self.store(lhs, payload_local, payload_ty, payload_offset); |
| 1132 | | } else { |
| 1133 | | // Load values from `rhs` stack position and store in `lhs` instead |
| 1134 | | const tag_local = try self.load(rhs, tag_ty, 0); |
| 1135 | | const payload_local = try self.load(rhs, payload_ty, payload_offset); |
| 1127 | const payload_offset = if (ty.zigTypeTag() == .ErrorUnion) |
| 1128 | @intCast(u32, tag_ty.abiSize(self.target)) |
| 1129 | else |
| 1130 | @intCast(u32, ty.abiSize(self.target) - payload_ty.abiSize(self.target)); |
| 1131 | |
| 1132 | switch (rhs) { |
| 1133 | .constant => { |
| 1134 | // constant will contain both tag and payload, |
| 1135 | // so save those in 2 temporary locals before storing them |
| 1136 | // in memory |
| 1137 | try self.emitWValue(rhs); |
| 1138 | const tag_local = try self.allocLocal(tag_ty); |
| 1139 | const payload_local = try self.allocLocal(payload_ty); |
| 1140 | |
| 1141 | try self.addLabel(.local_set, payload_local.local); |
| 1142 | try self.addLabel(.local_set, tag_local.local); |
| 1143 | |
| 1144 | try self.store(lhs, tag_local, tag_ty, 0); |
| 1145 | return try self.store(lhs, payload_local, payload_ty, payload_offset); |
| 1146 | }, |
| 1147 | .local => { |
| 1148 | // Load values from `rhs` stack position and store in `lhs` instead |
| 1149 | const tag_local = try self.load(rhs, tag_ty, 0); |
| 1150 | const payload_local = try self.load(rhs, payload_ty, payload_offset); |
| 1136 | 1151 | |
| 1137 | | try self.store(lhs, tag_local, tag_ty, 0); |
| 1138 | | return try self.store(lhs, payload_local, payload_ty, payload_offset); |
| 1152 | try self.store(lhs, tag_local, tag_ty, 0); |
| 1153 | return try self.store(lhs, payload_local, payload_ty, payload_offset); |
| 1154 | }, |
| 1155 | .local_with_offset => |with_offset| { |
| 1156 | const tag_local = try self.allocLocal(tag_ty); |
| 1157 | try self.addImm32(0); |
| 1158 | try self.store(lhs, tag_local, tag_ty, 0); |
| 1159 | |
| 1160 | return try self.store( |
| 1161 | lhs, |
| 1162 | .{ .local = with_offset.local }, |
| 1163 | payload_ty, |
| 1164 | with_offset.offset, |
| 1165 | ); |
| 1166 | }, |
| 1167 | else => unreachable, |
| 1168 | } |
| 1169 | }, |
| 1170 | .Struct => { |
| 1171 | // we are copying a struct with its fields. |
| 1172 | // Replace this with a wasm memcpy instruction once we support that feature. |
| 1173 | const fields_len = ty.structFieldCount(); |
| 1174 | var index: usize = 0; |
| 1175 | while (index < fields_len) : (index += 1) { |
| 1176 | const field_ty = ty.structFieldType(index); |
| 1177 | if (!field_ty.hasCodeGenBits()) continue; |
| 1178 | const field_offset = std.math.cast(u32, ty.structFieldOffset(index, self.target)) catch { |
| 1179 | return self.fail("Field type '{}' too big to fit into stack frame", .{field_ty}); |
| 1180 | }; |
| 1181 | const field_local = try self.load(rhs, field_ty, field_offset); |
| 1182 | try self.store(lhs, field_local, field_ty, field_offset); |
| 1139 | 1183 | } |
| 1184 | return; |
| 1140 | 1185 | }, |
| 1141 | 1186 | else => {}, |
| 1142 | 1187 | } |
| ... | ... | @@ -1429,21 +1474,29 @@ fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1429 | 1474 | const extra = self.air.extraData(Air.Block, ty_pl.payload); |
| 1430 | 1475 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; |
| 1431 | 1476 | |
| 1432 | | try self.startBlock(.block, block_ty, null); |
| 1477 | // if block_ty is non-empty, we create a register to store the temporary value |
| 1478 | const block_result: WValue = if (block_ty != wasm.block_empty) |
| 1479 | try self.allocLocal(self.air.getRefType(ty_pl.ty)) |
| 1480 | else |
| 1481 | WValue.none; |
| 1482 | |
| 1483 | try self.startBlock(.block, wasm.block_empty); |
| 1433 | 1484 | // Here we set the current block idx, so breaks know the depth to jump |
| 1434 | 1485 | // to when breaking out. |
| 1435 | | try self.blocks.putNoClobber(self.gpa, inst, self.block_depth); |
| 1486 | try self.blocks.putNoClobber(self.gpa, inst, .{ |
| 1487 | .label = self.block_depth, |
| 1488 | .value = block_result, |
| 1489 | }); |
| 1436 | 1490 | try self.genBody(body); |
| 1437 | 1491 | try self.endBlock(); |
| 1438 | 1492 | |
| 1439 | | return .none; |
| 1493 | return block_result; |
| 1440 | 1494 | } |
| 1441 | 1495 | |
| 1442 | 1496 | /// appends a new wasm block to the code section and increases the `block_depth` by 1 |
| 1443 | | fn startBlock(self: *Self, block_tag: wasm.Opcode, valtype: u8, with_offset: ?usize) !void { |
| 1497 | fn startBlock(self: *Self, block_tag: wasm.Opcode, valtype: u8) !void { |
| 1444 | 1498 | self.block_depth += 1; |
| 1445 | | const offset = with_offset orelse self.mir_instructions.len; |
| 1446 | | try self.addInstAt(offset, .{ |
| 1499 | try self.addInst(.{ |
| 1447 | 1500 | .tag = Mir.Inst.Tag.fromOpcode(block_tag), |
| 1448 | 1501 | .data = .{ .block_type = valtype }, |
| 1449 | 1502 | }); |
| ... | ... | @@ -1462,7 +1515,7 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1462 | 1515 | |
| 1463 | 1516 | // result type of loop is always 'noreturn', meaning we can always |
| 1464 | 1517 | // emit the wasm type 'block_empty'. |
| 1465 | | try self.startBlock(.loop, wasm.block_empty, null); |
| 1518 | try self.startBlock(.loop, wasm.block_empty); |
| 1466 | 1519 | try self.genBody(body); |
| 1467 | 1520 | |
| 1468 | 1521 | // breaking to the index of a loop block will continue the loop instead |
| ... | ... | @@ -1480,13 +1533,10 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1480 | 1533 | const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| 1481 | 1534 | // TODO: Handle death instructions for then and else body |
| 1482 | 1535 | |
| 1483 | | // insert blocks at the position of `offset` so |
| 1484 | | // the condition can jump to it |
| 1485 | | const offset = self.mir_instructions.len; |
| 1486 | | try self.emitWValue(condition); |
| 1487 | | |
| 1488 | 1536 | // result type is always noreturn, so use `block_empty` as type. |
| 1489 | | try self.startBlock(.block, wasm.block_empty, offset); |
| 1537 | try self.startBlock(.block, wasm.block_empty); |
| 1538 | // emit the conditional value |
| 1539 | try self.emitWValue(condition); |
| 1490 | 1540 | |
| 1491 | 1541 | // we inserted the block in front of the condition |
| 1492 | 1542 | // so now check if condition matches. If not, break outside this block |
| ... | ... | @@ -1539,15 +1589,20 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) Inner |
| 1539 | 1589 | |
| 1540 | 1590 | fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1541 | 1591 | const br = self.air.instructions.items(.data)[inst].br; |
| 1592 | const block = self.blocks.get(br.block_inst).?; |
| 1542 | 1593 | |
| 1543 | 1594 | // if operand has codegen bits we should break with a value |
| 1544 | 1595 | if (self.air.typeOf(br.operand).hasCodeGenBits()) { |
| 1545 | 1596 | try self.emitWValue(self.resolveInst(br.operand)); |
| 1597 | |
| 1598 | if (block.value != .none) { |
| 1599 | try self.addLabel(.local_set, block.value.local); |
| 1600 | } |
| 1546 | 1601 | } |
| 1547 | 1602 | |
| 1548 | 1603 | // We map every block to its block index. |
| 1549 | 1604 | // We then determine how far we have to jump to it by subtracting it from current block depth |
| 1550 | | const idx: u32 = self.block_depth - self.blocks.get(br.block_inst).?; |
| 1605 | const idx: u32 = self.block_depth - block.label; |
| 1551 | 1606 | try self.addLabel(.br, idx); |
| 1552 | 1607 | |
| 1553 | 1608 | return .none; |
| ... | ... | @@ -1677,7 +1732,7 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1677 | 1732 | } |
| 1678 | 1733 | |
| 1679 | 1734 | case_list.appendAssumeCapacity(.{ .values = values, .body = case_body }); |
| 1680 | | try self.startBlock(.block, blocktype, null); |
| 1735 | try self.startBlock(.block, blocktype); |
| 1681 | 1736 | } |
| 1682 | 1737 | |
| 1683 | 1738 | // When the highest and lowest values are seperated by '50', |
| ... | ... | @@ -1690,7 +1745,7 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1690 | 1745 | const else_body = self.air.extra[extra_index..][0..switch_br.data.else_body_len]; |
| 1691 | 1746 | const has_else_body = else_body.len != 0; |
| 1692 | 1747 | if (has_else_body) { |
| 1693 | | try self.startBlock(.block, blocktype, null); |
| 1748 | try self.startBlock(.block, blocktype); |
| 1694 | 1749 | } |
| 1695 | 1750 | |
| 1696 | 1751 | if (!is_sparse) { |
| ... | ... | @@ -1698,7 +1753,7 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1698 | 1753 | // The value 'target' represents the index into the table. |
| 1699 | 1754 | // Each index in the table represents a label to the branch |
| 1700 | 1755 | // to jump to. |
| 1701 | | try self.startBlock(.block, blocktype, null); |
| 1756 | try self.startBlock(.block, blocktype); |
| 1702 | 1757 | try self.emitWValue(target); |
| 1703 | 1758 | if (lowest < 0) { |
| 1704 | 1759 | // since br_table works using indexes, starting from '0', we must ensure all values |
| ... | ... | @@ -1754,7 +1809,7 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1754 | 1809 | try self.addLabel(.br_if, 0); |
| 1755 | 1810 | } else { |
| 1756 | 1811 | // in multi-value prongs we must check if any prongs match the target value. |
| 1757 | | try self.startBlock(.block, blocktype, null); |
| 1812 | try self.startBlock(.block, blocktype); |
| 1758 | 1813 | for (case.values) |value| { |
| 1759 | 1814 | try self.emitWValue(target); |
| 1760 | 1815 | try self.emitConstant(value.value, target_ty); |
| ... | ... | @@ -1794,7 +1849,7 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!W |
| 1794 | 1849 | .alignment = err_ty.abiAlignment(self.target), |
| 1795 | 1850 | }); |
| 1796 | 1851 | try self.addInst(.{ |
| 1797 | | .tag = .i32_load, |
| 1852 | .tag = .i32_load16_u, |
| 1798 | 1853 | .data = .{ .payload = mem_arg_index }, |
| 1799 | 1854 | }); |
| 1800 | 1855 | |
| ... | ... | @@ -1811,14 +1866,14 @@ fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue |
| 1811 | 1866 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1812 | 1867 | const operand = self.resolveInst(ty_op.operand); |
| 1813 | 1868 | const err_ty = self.air.typeOf(ty_op.operand); |
| 1814 | | const offset = @intCast(u32, err_ty.errorUnionSet().abiSize(self.target) / 8); |
| 1815 | | |
| 1816 | | return self.load(operand, self.air.getRefType(ty_op.ty), offset); |
| 1869 | const offset = @intCast(u32, err_ty.errorUnionSet().abiSize(self.target)); |
| 1870 | return self.load(operand, err_ty.errorUnionPayload(), offset); |
| 1817 | 1871 | } |
| 1818 | 1872 | |
| 1819 | 1873 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1820 | 1874 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1821 | | return self.resolveInst(ty_op.operand); |
| 1875 | _ = ty_op; |
| 1876 | return self.fail("TODO: wasm airWrapErrUnionPayload", .{}); |
| 1822 | 1877 | } |
| 1823 | 1878 | |
| 1824 | 1879 | fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| ... | ... | @@ -1880,8 +1935,9 @@ fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1880 | 1935 | |
| 1881 | 1936 | var buf: Type.Payload.ElemType = undefined; |
| 1882 | 1937 | const child_ty = opt_ty.optionalChild(&buf); |
| 1938 | const offset = opt_ty.abiSize(self.target) - child_ty.abiSize(self.target); |
| 1883 | 1939 | |
| 1884 | | return self.load(operand, child_ty, @as(u32, 1)); // null tag is 1 byte |
| 1940 | return self.load(operand, child_ty, @intCast(u32, offset)); |
| 1885 | 1941 | } |
| 1886 | 1942 | |
| 1887 | 1943 | fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| ... | ... | @@ -1893,5 +1949,14 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue |
| 1893 | 1949 | |
| 1894 | 1950 | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1895 | 1951 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1896 | | return self.resolveInst(ty_op.operand); |
| 1952 | const operand = self.resolveInst(ty_op.operand); |
| 1953 | |
| 1954 | const op_ty = self.air.typeOf(ty_op.operand); |
| 1955 | const optional_ty = self.air.getRefType(ty_op.ty); |
| 1956 | const offset = optional_ty.abiSize(self.target) - op_ty.abiSize(self.target); |
| 1957 | |
| 1958 | return WValue{ .local_with_offset = .{ |
| 1959 | .local = operand.local, |
| 1960 | .offset = @intCast(u32, offset), |
| 1961 | } }; |
| 1897 | 1962 | } |