| ... | ... | @@ -27,8 +27,6 @@ const WValue = union(enum) { |
| 27 | 27 | none: void, |
| 28 | 28 | /// Index of the local variable |
| 29 | 29 | local: u32, |
| 30 | | /// Represents the index of a local that contains a pointer to its stack position |
| 31 | | stack: u32, |
| 32 | 30 | /// An immediate 32bit value |
| 33 | 31 | imm32: u32, |
| 34 | 32 | /// An immediate 64bit value |
| ... | ... | @@ -38,21 +36,13 @@ const WValue = union(enum) { |
| 38 | 36 | /// A constant 64bit float value |
| 39 | 37 | float64: f64, |
| 40 | 38 | /// A value that represents a pointer to the data section |
| 41 | | memory: u64, |
| 39 | /// Note: The value contains the symbol index, rather than the actual address |
| 40 | /// as we use this to perform the relocation. |
| 41 | memory: u32, |
| 42 | 42 | /// Represents a function pointer |
| 43 | 43 | /// In wasm function pointers are indexes into a function table, |
| 44 | 44 | /// rather than an address in the data section. |
| 45 | 45 | function_index: u32, |
| 46 | | /// Used for types that contains of multiple areas within |
| 47 | | /// a memory region in the stack. |
| 48 | | /// The local represents the position in the stack, |
| 49 | | /// whereas the offset represents the offset from that position. |
| 50 | | local_with_offset: struct { |
| 51 | | /// Index of the local variable |
| 52 | | local: u32, |
| 53 | | /// The offset from the local's stack position |
| 54 | | offset: u32, |
| 55 | | }, |
| 56 | 46 | }; |
| 57 | 47 | |
| 58 | 48 | /// Wasm ops, but without input/output/signedness information |
| ... | ... | @@ -514,7 +504,7 @@ pub const Result = union(enum) { |
| 514 | 504 | }; |
| 515 | 505 | |
| 516 | 506 | /// Hashmap to store generated `WValue` for each `Air.Inst.Ref` |
| 517 | | pub const ValueTable = std.AutoHashMapUnmanaged(Air.Inst.Index, WValue); |
| 507 | pub const ValueTable = std.AutoHashMapUnmanaged(Air.Inst.Ref, WValue); |
| 518 | 508 | |
| 519 | 509 | const Self = @This(); |
| 520 | 510 | |
| ... | ... | @@ -601,7 +591,7 @@ fn fail(self: *Self, comptime fmt: []const u8, args: anytype) InnerError { |
| 601 | 591 | |
| 602 | 592 | /// Resolves the `WValue` for the given instruction `inst` |
| 603 | 593 | /// When the given instruction has a `Value`, it returns a constant instead |
| 604 | | fn resolveInst(self: Self, ref: Air.Inst.Ref) WValue { |
| 594 | fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue { |
| 605 | 595 | const gop = try self.values.getOrPut(self.gpa, ref); |
| 606 | 596 | if (gop.found_existing) return gop.value_ptr.*; |
| 607 | 597 | |
| ... | ... | @@ -609,7 +599,10 @@ fn resolveInst(self: Self, ref: Air.Inst.Ref) WValue { |
| 609 | 599 | // means we must generate it from a constant. |
| 610 | 600 | const val = self.air.value(ref).?; |
| 611 | 601 | const ty = self.air.typeOf(ref); |
| 612 | | return self.lowerConstant(val, ty); |
| 602 | if (!ty.hasCodeGenBits() and !ty.isInt()) return WValue{ .none = {} }; |
| 603 | const result = try self.lowerConstant(val, ty); |
| 604 | gop.value_ptr.* = result; |
| 605 | return result; |
| 613 | 606 | } |
| 614 | 607 | |
| 615 | 608 | /// Appends a MIR instruction and returns its index within the list of instructions |
| ... | ... | @@ -729,10 +722,9 @@ fn genBlockType(self: *Self, ty: Type) InnerError!u8 { |
| 729 | 722 | } |
| 730 | 723 | |
| 731 | 724 | /// Writes the bytecode depending on the given `WValue` in `val` |
| 732 | | fn emitWValue(self: *Self, val: WValue) InnerError!void { |
| 733 | | switch (val) { |
| 725 | fn emitWValue(self: *Self, value: WValue) InnerError!void { |
| 726 | switch (value) { |
| 734 | 727 | .none => {}, // no-op |
| 735 | | .local_with_offset => |with_off| try self.addLabel(.local_get, with_off.local), |
| 736 | 728 | .local => |idx| try self.addLabel(.local_get, idx), |
| 737 | 729 | .imm32 => |val| try self.addImm32(@bitCast(i32, val)), |
| 738 | 730 | .imm64 => |val| try self.addImm64(val), |
| ... | ... | @@ -1195,7 +1187,6 @@ fn toWasmIntBits(bits: u16) ?u16 { |
| 1195 | 1187 | |
| 1196 | 1188 | /// Performs a copy of bytes for a given type. Copying all bytes |
| 1197 | 1189 | /// from rhs to lhs. |
| 1198 | | /// Asserts `lhs` and `rhs` have their active tag set to `local` |
| 1199 | 1190 | /// |
| 1200 | 1191 | /// TODO: Perform feature detection and when bulk_memory is available, |
| 1201 | 1192 | /// use wasm's mem.copy instruction. |
| ... | ... | @@ -1204,9 +1195,9 @@ fn memCopy(self: *Self, ty: Type, lhs: WValue, rhs: WValue) !void { |
| 1204 | 1195 | var offset: u32 = 0; |
| 1205 | 1196 | while (offset < abi_size) : (offset += 1) { |
| 1206 | 1197 | // get lhs' address to store the result |
| 1207 | | try self.addLabel(.local_get, lhs.local); |
| 1198 | try self.emitWValue(lhs); |
| 1208 | 1199 | // load byte from rhs' adress |
| 1209 | | try self.addLabel(.local_get, rhs.local); |
| 1200 | try self.emitWValue(rhs); |
| 1210 | 1201 | try self.addMemArg(.i32_load8_u, .{ .offset = offset, .alignment = 1 }); |
| 1211 | 1202 | // store the result in lhs (we already have its address on the stack) |
| 1212 | 1203 | try self.addMemArg(.i32_store8, .{ .offset = offset, .alignment = 1 }); |
| ... | ... | @@ -1456,13 +1447,13 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { |
| 1456 | 1447 | fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1457 | 1448 | for (body) |inst| { |
| 1458 | 1449 | const result = try self.genInst(inst); |
| 1459 | | try self.values.putNoClobber(self.gpa, inst, result); |
| 1450 | try self.values.putNoClobber(self.gpa, Air.indexToRef(inst), result); |
| 1460 | 1451 | } |
| 1461 | 1452 | } |
| 1462 | 1453 | |
| 1463 | 1454 | fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1464 | 1455 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 1465 | | const operand = self.resolveInst(un_op); |
| 1456 | const operand = try self.resolveInst(un_op); |
| 1466 | 1457 | // result must be stored in the stack and we return a pointer |
| 1467 | 1458 | // to the stack instead |
| 1468 | 1459 | if (self.return_value != .none) { |
| ... | ... | @@ -1492,7 +1483,7 @@ fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1492 | 1483 | |
| 1493 | 1484 | fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1494 | 1485 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 1495 | | const operand = self.resolveInst(un_op); |
| 1486 | const operand = try self.resolveInst(un_op); |
| 1496 | 1487 | const ret_ty = self.air.typeOf(un_op).childType(); |
| 1497 | 1488 | if (!ret_ty.hasCodeGenBits()) return WValue.none; |
| 1498 | 1489 | |
| ... | ... | @@ -1539,24 +1530,11 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1539 | 1530 | |
| 1540 | 1531 | for (args) |arg| { |
| 1541 | 1532 | const arg_ref = @intToEnum(Air.Inst.Ref, arg); |
| 1542 | | const arg_val = self.resolveInst(arg_ref); |
| 1533 | const arg_val = try self.resolveInst(arg_ref); |
| 1543 | 1534 | |
| 1544 | 1535 | const arg_ty = self.air.typeOf(arg_ref); |
| 1545 | 1536 | if (!arg_ty.hasCodeGenBits()) continue; |
| 1546 | | |
| 1547 | | // If we need to pass by reference, but the argument is a constant, |
| 1548 | | // we must first lower it before passing it. |
| 1549 | | if (self.isByRef(arg_ty) and arg_val == .constant) { |
| 1550 | | const arg_local = try self.allocStack(arg_ty); |
| 1551 | | try self.store(arg_local, arg_val, arg_ty, 0); |
| 1552 | | try self.emitWValue(arg_local); |
| 1553 | | } else if (arg_val == .none) { |
| 1554 | | // TODO: Remove this branch when zero-sized pointers do not generate |
| 1555 | | // an argument. |
| 1556 | | try self.addImm32(0); |
| 1557 | | } else { |
| 1558 | | try self.emitWValue(arg_val); |
| 1559 | | } |
| 1537 | try self.emitWValue(arg_val); |
| 1560 | 1538 | } |
| 1561 | 1539 | |
| 1562 | 1540 | if (target) |direct| { |
| ... | ... | @@ -1565,7 +1543,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1565 | 1543 | // in this case we call a function pointer |
| 1566 | 1544 | // so load its value onto the stack |
| 1567 | 1545 | std.debug.assert(ty.zigTypeTag() == .Pointer); |
| 1568 | | const operand = self.resolveInst(pl_op.operand); |
| 1546 | const operand = try self.resolveInst(pl_op.operand); |
| 1569 | 1547 | try self.emitWValue(operand); |
| 1570 | 1548 | |
| 1571 | 1549 | var fn_type = try self.genFunctype(fn_ty); |
| ... | ... | @@ -1609,142 +1587,44 @@ fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1609 | 1587 | fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1610 | 1588 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 1611 | 1589 | |
| 1612 | | const lhs = self.resolveInst(bin_op.lhs); |
| 1613 | | const rhs = self.resolveInst(bin_op.rhs); |
| 1590 | const lhs = try self.resolveInst(bin_op.lhs); |
| 1591 | const rhs = try self.resolveInst(bin_op.rhs); |
| 1614 | 1592 | const ty = self.air.typeOf(bin_op.lhs).childType(); |
| 1615 | 1593 | |
| 1616 | | const offset: u32 = switch (lhs) { |
| 1617 | | .local_with_offset => |with_off| with_off.offset, |
| 1618 | | else => 0, |
| 1619 | | }; |
| 1620 | | |
| 1621 | | try self.store(lhs, rhs, ty, offset); |
| 1594 | try self.store(lhs, rhs, ty, 0); |
| 1622 | 1595 | return .none; |
| 1623 | 1596 | } |
| 1624 | 1597 | |
| 1625 | 1598 | fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerError!void { |
| 1626 | 1599 | switch (ty.zigTypeTag()) { |
| 1627 | | .ErrorUnion, .Optional => { |
| 1628 | | var buf: Type.Payload.ElemType = undefined; |
| 1629 | | const payload_ty = if (ty.zigTypeTag() == .ErrorUnion) ty.errorUnionPayload() else ty.optionalChild(&buf); |
| 1630 | | const tag_ty = if (ty.zigTypeTag() == .ErrorUnion) ty.errorUnionSet() else Type.initTag(.u8); |
| 1631 | | const payload_offset = if (ty.zigTypeTag() == .ErrorUnion) |
| 1632 | | @intCast(u32, tag_ty.abiSize(self.target)) |
| 1633 | | else if (ty.isPtrLikeOptional()) |
| 1634 | | @as(u32, 0) |
| 1635 | | else |
| 1636 | | @intCast(u32, ty.abiSize(self.target) - payload_ty.abiSize(self.target)); |
| 1637 | | |
| 1638 | | switch (rhs) { |
| 1639 | | .constant => { |
| 1640 | | if (rhs.constant.val.castTag(.decl_ref)) |_| { |
| 1641 | | // retrieve values from memory instead |
| 1642 | | const mem_local = try self.allocLocal(Type.usize); |
| 1643 | | try self.emitWValue(rhs); |
| 1644 | | try self.addLabel(.local_set, mem_local.local); |
| 1645 | | try self.store(lhs, mem_local, ty, 0); |
| 1646 | | return; |
| 1647 | | } else if (ty.isPtrLikeOptional()) { |
| 1648 | | // set the address of rhs to lhs |
| 1649 | | try self.store(lhs, rhs, Type.usize, 0); |
| 1650 | | return; |
| 1651 | | } |
| 1652 | | // constant will contain both tag and payload, |
| 1653 | | // so save those in 2 temporary locals before storing them |
| 1654 | | // in memory |
| 1655 | | try self.emitWValue(rhs); |
| 1656 | | const tag_local = try self.allocLocal(tag_ty); |
| 1657 | | |
| 1658 | | if (payload_ty.hasCodeGenBits()) { |
| 1659 | | const payload_local = try self.allocLocal(payload_ty); |
| 1660 | | try self.addLabel(.local_set, payload_local.local); |
| 1661 | | if (self.isByRef(payload_ty)) { |
| 1662 | | const ptr = try self.buildPointerOffset(lhs, payload_offset, .new); |
| 1663 | | try self.store(ptr, payload_local, payload_ty, 0); |
| 1664 | | } else { |
| 1665 | | try self.store(lhs, payload_local, payload_ty, payload_offset); |
| 1666 | | } |
| 1667 | | } |
| 1668 | | try self.addLabel(.local_set, tag_local.local); |
| 1600 | .ErrorUnion => { |
| 1601 | const err_ty = ty.errorUnionSet(); |
| 1602 | const pl_ty = ty.errorUnionPayload(); |
| 1603 | if (!pl_ty.hasCodeGenBits()) { |
| 1604 | const err_val = try self.load(rhs, err_ty, 0); |
| 1605 | return self.store(lhs, err_val, err_ty, 0); |
| 1606 | } |
| 1669 | 1607 | |
| 1670 | | try self.store(lhs, tag_local, tag_ty, 0); |
| 1671 | | return; |
| 1672 | | }, |
| 1673 | | .local => { |
| 1674 | | // When the optional is pointer-like, we simply store the pointer |
| 1675 | | // instead. |
| 1676 | | if (ty.isPtrLikeOptional()) { |
| 1677 | | try self.store(lhs, rhs, Type.usize, 0); |
| 1678 | | return; |
| 1679 | | } |
| 1680 | | // Load values from `rhs` stack position and store in `lhs` instead |
| 1681 | | const tag_local = try self.load(rhs, tag_ty, 0); |
| 1682 | | if (payload_ty.hasCodeGenBits()) { |
| 1683 | | if (self.isByRef(payload_ty)) { |
| 1684 | | const payload_ptr = try self.buildPointerOffset(rhs, payload_offset, .new); |
| 1685 | | const lhs_payload_ptr = try self.buildPointerOffset(lhs, payload_offset, .new); |
| 1686 | | try self.store(lhs_payload_ptr, payload_ptr, payload_ty, 0); |
| 1687 | | } else { |
| 1688 | | const payload_local = try self.load(rhs, payload_ty, payload_offset); |
| 1689 | | try self.store(lhs, payload_local, payload_ty, payload_offset); |
| 1690 | | } |
| 1691 | | } |
| 1692 | | return try self.store(lhs, tag_local, tag_ty, 0); |
| 1693 | | }, |
| 1694 | | .local_with_offset => |with_offset| { |
| 1695 | | // check if we're storing the payload, or the error |
| 1696 | | if (with_offset.offset == 0) { |
| 1697 | | try self.store(lhs, .{ .local = with_offset.local }, tag_ty, 0); |
| 1698 | | return; |
| 1699 | | } |
| 1700 | | const tag_local = try self.allocLocal(tag_ty); |
| 1701 | | try self.addImm32(0); |
| 1702 | | try self.addLabel(.local_set, tag_local.local); |
| 1703 | | try self.store(lhs, tag_local, tag_ty, 0); |
| 1704 | | |
| 1705 | | return try self.store( |
| 1706 | | lhs, |
| 1707 | | .{ .local = with_offset.local }, |
| 1708 | | payload_ty, |
| 1709 | | with_offset.offset, |
| 1710 | | ); |
| 1711 | | }, |
| 1712 | | else => unreachable, |
| 1608 | return try self.memCopy(ty, lhs, rhs); |
| 1609 | }, |
| 1610 | .Optional => { |
| 1611 | if (ty.isPtrLikeOptional()) { |
| 1612 | return self.store(lhs, rhs, Type.usize, 0); |
| 1613 | } |
| 1614 | var buf: Type.Payload.ElemType = undefined; |
| 1615 | const pl_ty = ty.optionalChild(&buf); |
| 1616 | if (!pl_ty.hasCodeGenBits()) { |
| 1617 | // const null_val = try self.load(rhs, Type.initTag(.u8), 0); |
| 1618 | return self.store(lhs, rhs, Type.initTag(.u8), 0); |
| 1713 | 1619 | } |
| 1620 | |
| 1621 | return self.memCopy(ty, lhs, rhs); |
| 1714 | 1622 | }, |
| 1715 | 1623 | .Struct, .Array => { |
| 1716 | 1624 | return try self.memCopy(ty, lhs, rhs); |
| 1717 | 1625 | }, |
| 1718 | 1626 | .Pointer => { |
| 1719 | | if (ty.isSlice() and rhs == .constant) { |
| 1720 | | try self.emitWValue(rhs); |
| 1721 | | |
| 1722 | | const val = rhs.constant.val; |
| 1723 | | const len_local = try self.allocLocal(Type.usize); |
| 1724 | | const ptr_local = try self.allocLocal(Type.usize); |
| 1725 | | const len_offset = self.ptrSize(); |
| 1726 | | if (val.castTag(.decl_ref)) |decl| { |
| 1727 | | const decl_ty: Type = decl.data.ty; |
| 1728 | | if (decl_ty.isSlice()) { |
| 1729 | | // for decl references we also need to retrieve the length and the original decl's pointer |
| 1730 | | try self.addMemArg(.i32_load, .{ .offset = 0, .alignment = self.ptrSize() }); |
| 1731 | | try self.addLabel(.memory_address, decl.data.link.wasm.sym_index); |
| 1732 | | try self.addMemArg(.i32_load, .{ .offset = len_offset, .alignment = self.ptrSize() }); |
| 1733 | | } else if (decl_ty.zigTypeTag() == .Array) { |
| 1734 | | const len = decl_ty.arrayLen(); |
| 1735 | | switch (self.ptrSize()) { |
| 1736 | | 4 => try self.addImm32(@bitCast(i32, @intCast(u32, len))), |
| 1737 | | 8 => try self.addImm64(len), |
| 1738 | | else => unreachable, |
| 1739 | | } |
| 1740 | | } else return self.fail("Wasm todo: Implement storing slices for decl_ref with type: {}", .{decl_ty}); |
| 1741 | | } |
| 1742 | | try self.addLabel(.local_set, len_local.local); |
| 1743 | | try self.addLabel(.local_set, ptr_local.local); |
| 1744 | | try self.store(lhs, ptr_local, Type.usize, 0); |
| 1745 | | try self.store(lhs, len_local, Type.usize, len_offset); |
| 1746 | | return; |
| 1747 | | } else if (ty.isSlice()) { |
| 1627 | if (ty.isSlice()) { |
| 1748 | 1628 | // store pointer first |
| 1749 | 1629 | const ptr_local = try self.load(rhs, Type.usize, 0); |
| 1750 | 1630 | try self.store(lhs, ptr_local, Type.usize, 0); |
| ... | ... | @@ -1790,7 +1670,7 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro |
| 1790 | 1670 | |
| 1791 | 1671 | fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1792 | 1672 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1793 | | const operand = self.resolveInst(ty_op.operand); |
| 1673 | const operand = try self.resolveInst(ty_op.operand); |
| 1794 | 1674 | const ty = self.air.getRefType(ty_op.ty); |
| 1795 | 1675 | |
| 1796 | 1676 | if (!ty.hasCodeGenBits()) return WValue{ .none = {} }; |
| ... | ... | @@ -1801,10 +1681,7 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1801 | 1681 | return new_local; |
| 1802 | 1682 | } |
| 1803 | 1683 | |
| 1804 | | return switch (operand) { |
| 1805 | | .local_with_offset => |with_offset| try self.load(operand, ty, with_offset.offset), |
| 1806 | | else => try self.load(operand, ty, 0), |
| 1807 | | }; |
| 1684 | return self.load(operand, ty, 0); |
| 1808 | 1685 | } |
| 1809 | 1686 | |
| 1810 | 1687 | fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue { |
| ... | ... | @@ -1862,8 +1739,8 @@ fn airBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 1862 | 1739 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 1863 | 1740 | |
| 1864 | 1741 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 1865 | | const lhs = self.resolveInst(bin_op.lhs); |
| 1866 | | const rhs = self.resolveInst(bin_op.rhs); |
| 1742 | const lhs = try self.resolveInst(bin_op.lhs); |
| 1743 | const rhs = try self.resolveInst(bin_op.rhs); |
| 1867 | 1744 | const operand_ty = self.air.typeOfIndex(inst); |
| 1868 | 1745 | |
| 1869 | 1746 | if (self.isByRef(operand_ty)) { |
| ... | ... | @@ -1889,8 +1766,8 @@ fn airBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 1889 | 1766 | |
| 1890 | 1767 | fn airWrapBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 1891 | 1768 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 1892 | | const lhs = self.resolveInst(bin_op.lhs); |
| 1893 | | const rhs = self.resolveInst(bin_op.rhs); |
| 1769 | const lhs = try self.resolveInst(bin_op.lhs); |
| 1770 | const rhs = try self.resolveInst(bin_op.rhs); |
| 1894 | 1771 | |
| 1895 | 1772 | try self.emitWValue(lhs); |
| 1896 | 1773 | try self.emitWValue(rhs); |
| ... | ... | @@ -1960,8 +1837,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 1960 | 1837 | var space: Value.BigIntSpace = undefined; |
| 1961 | 1838 | const bigint = val.toBigInt(&space); |
| 1962 | 1839 | if (bigint.limbs.len == 1 and bigint.limbs[0] == 0) { |
| 1963 | | try self.addLabel(.local_get, result.local); |
| 1964 | | return; |
| 1840 | return result; |
| 1965 | 1841 | } |
| 1966 | 1842 | if (@sizeOf(usize) != @sizeOf(u64)) { |
| 1967 | 1843 | return self.fail("Wasm todo: Implement big integers for 32bit compiler", .{}); |
| ... | ... | @@ -1979,9 +1855,9 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 1979 | 1855 | .Float => switch (ty.floatBits(self.target)) { |
| 1980 | 1856 | 0...32 => return WValue{ .float32 = val.toFloat(f32) }, |
| 1981 | 1857 | 33...64 => return WValue{ .float64 = val.toFloat(f64) }, |
| 1982 | | else => |bits| return self.fail("Wasm TODO: emitConstant for floats with {d} bits", .{bits}), |
| 1858 | else => |bits| return self.fail("Wasm TODO: lowerConstant for floats with {d} bits", .{bits}), |
| 1983 | 1859 | }, |
| 1984 | | .Pointer => switch (val) { |
| 1860 | .Pointer => switch (val.tag()) { |
| 1985 | 1861 | .slice => { |
| 1986 | 1862 | const result = try self.allocStack(ty); |
| 1987 | 1863 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| ... | ... | @@ -1998,6 +1874,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 1998 | 1874 | try self.addImm64(len); |
| 1999 | 1875 | try self.addMemArg(.i64_store, .{ .offset = self.ptrSize(), .alignment = self.ptrSize() }); |
| 2000 | 1876 | }, |
| 1877 | else => unreachable, |
| 2001 | 1878 | } |
| 2002 | 1879 | return result; |
| 2003 | 1880 | }, |
| ... | ... | @@ -2005,14 +1882,24 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2005 | 1882 | const decl = val.castTag(.decl_ref).?.data; |
| 2006 | 1883 | decl.markAlive(); |
| 2007 | 1884 | const target_sym_index = decl.link.wasm.sym_index; |
| 2008 | | if (decl.ty.zigTypeTag() == .Fn) { |
| 1885 | if (ty.isSlice()) { |
| 1886 | var slice_len: Value.Payload.U64 = .{ |
| 1887 | .base = .{ .tag = .int_u64 }, |
| 1888 | .data = val.sliceLen(), |
| 1889 | }; |
| 1890 | var slice_val: Value.Payload.Slice = .{ |
| 1891 | .base = .{ .tag = .slice }, |
| 1892 | .data = .{ .ptr = val.slicePtr(), .len = Value.initPayload(&slice_len.base) }, |
| 1893 | }; |
| 1894 | return self.lowerConstant(Value.initPayload(&slice_val.base), ty); |
| 1895 | } else if (decl.ty.zigTypeTag() == .Fn) { |
| 2009 | 1896 | try self.bin_file.addTableFunction(target_sym_index); |
| 2010 | 1897 | return WValue{ .function_index = target_sym_index }; |
| 2011 | 1898 | } else return WValue{ .memory = target_sym_index }; |
| 2012 | 1899 | }, |
| 2013 | 1900 | .int_u64, .one => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt()) }, |
| 2014 | 1901 | .zero, .null_value => return WValue{ .imm32 = 0 }, |
| 2015 | | else => return self.fail("Wasm TODO: emitConstant for other const pointer tag {s}", .{val.tag()}), |
| 1902 | else => return self.fail("Wasm TODO: lowerConstant for other const pointer tag {s}", .{val.tag()}), |
| 2016 | 1903 | }, |
| 2017 | 1904 | .Enum => { |
| 2018 | 1905 | if (val.castTag(.enum_field_index)) |field_index| { |
| ... | ... | @@ -2035,9 +1922,12 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2035 | 1922 | return self.lowerConstant(val, int_tag_ty); |
| 2036 | 1923 | } |
| 2037 | 1924 | }, |
| 2038 | | .ErrorSet => { |
| 2039 | | const error_index = self.global_error_set.get(val.getError().?).?; |
| 2040 | | return WValue{ .imm32 = error_index }; |
| 1925 | .ErrorSet => switch (val.tag()) { |
| 1926 | .@"error" => { |
| 1927 | const error_index = self.global_error_set.get(val.getError().?).?; |
| 1928 | return WValue{ .imm32 = error_index }; |
| 1929 | }, |
| 1930 | else => return WValue{ .imm32 = 0 }, |
| 2041 | 1931 | }, |
| 2042 | 1932 | .ErrorUnion => { |
| 2043 | 1933 | const error_type = ty.errorUnionSet(); |
| ... | ... | @@ -2051,25 +1941,25 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2051 | 1941 | |
| 2052 | 1942 | const result = try self.allocStack(ty); |
| 2053 | 1943 | try self.store(result, error_value, error_type, 0); |
| 2054 | | const payload = try lowerConstant( |
| 1944 | const payload = try self.lowerConstant( |
| 2055 | 1945 | if (val.castTag(.eu_payload)) |pl| pl.data else Value.initTag(.undef), |
| 2056 | 1946 | payload_type, |
| 2057 | 1947 | ); |
| 2058 | | const pl_ptr = if (self.isByRef(payload_type)) try self.buildPointerOffset( |
| 2059 | | result, |
| 2060 | | error_type.abiSize(self.target), |
| 2061 | | .new, |
| 2062 | | ) else result; |
| 2063 | | try self.store(pl_ptr, payload, payload_type, 0); |
| 1948 | const pl_ptr = if (self.isByRef(payload_type)) |
| 1949 | try self.buildPointerOffset(result, error_type.abiSize(self.target), .new) |
| 1950 | else |
| 1951 | result; |
| 1952 | try self.store(pl_ptr, payload, payload_type, @intCast(u32, error_type.abiSize(self.target))); |
| 2064 | 1953 | return result; |
| 2065 | 1954 | }, |
| 2066 | 1955 | .Optional => if (ty.isPtrLikeOptional()) { |
| 2067 | | return self.lowerConstant(val, payload_type); |
| 1956 | var buf: Type.Payload.ElemType = undefined; |
| 1957 | return self.lowerConstant(val, ty.optionalChild(&buf)); |
| 2068 | 1958 | } else { |
| 2069 | 1959 | var buf: Type.Payload.ElemType = undefined; |
| 2070 | 1960 | const payload_type = ty.optionalChild(&buf); |
| 2071 | 1961 | const is_pl = val.tag() == .opt_payload; |
| 2072 | | const null_value = WValue{ .imm32 = if (is_pl) @as(u32, 0) else 1 }; |
| 1962 | const null_value = WValue{ .imm32 = if (is_pl) @as(u32, 1) else 0 }; |
| 2073 | 1963 | if (!payload_type.hasCodeGenBits()) { |
| 2074 | 1964 | return null_value; |
| 2075 | 1965 | } |
| ... | ... | @@ -2081,11 +1971,11 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2081 | 1971 | payload_type, |
| 2082 | 1972 | ); |
| 2083 | 1973 | |
| 1974 | const offset = @intCast(u32, ty.abiSize(self.target) - payload_type.abiSize(self.target)); |
| 2084 | 1975 | const pl_ptr = if (self.isByRef(payload_type)) blk: { |
| 2085 | | const offset = ty.abiSize(self.target) - payload_type.abiSize(); |
| 2086 | 1976 | break :blk try self.buildPointerOffset(result, offset, .new); |
| 2087 | 1977 | } else result; |
| 2088 | | try self.store(pl_ptr, payload, payload_type, 0); |
| 1978 | try self.store(pl_ptr, payload, payload_type, offset); |
| 2089 | 1979 | return result; |
| 2090 | 1980 | }, |
| 2091 | 1981 | .Struct => { |
| ... | ... | @@ -2096,7 +1986,6 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2096 | 1986 | const fields = ty.structFields(); |
| 2097 | 1987 | const offset = try self.copyLocal(result, ty); |
| 2098 | 1988 | for (fields.values()) |field, index| { |
| 2099 | | const tmp = try self.allocLocal(field.ty); |
| 2100 | 1989 | const field_value = try self.lowerConstant(struct_data.data[index], field.ty); |
| 2101 | 1990 | try self.store(offset, field_value, field.ty, 0); |
| 2102 | 1991 | |
| ... | ... | @@ -2118,12 +2007,10 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2118 | 2007 | } else if (val.castTag(.array)) |array| { |
| 2119 | 2008 | const elem_ty = ty.childType(); |
| 2120 | 2009 | const elem_size = elem_ty.abiSize(self.target); |
| 2121 | | const tmp = try self.allocLocal(elem_ty); |
| 2122 | 2010 | const offset = try self.copyLocal(result, ty); |
| 2123 | 2011 | for (array.data) |value, index| { |
| 2124 | | try self.lowerConstant(value, elem_ty); |
| 2125 | | try self.addLabel(.local_set, tmp.local); |
| 2126 | | try self.store(offset, tmp, elem_ty, 0); |
| 2012 | const elem_val = try self.lowerConstant(value, elem_ty); |
| 2013 | try self.store(offset, elem_val, elem_ty, 0); |
| 2127 | 2014 | |
| 2128 | 2015 | if (index != (array.data.len - 1)) { |
| 2129 | 2016 | _ = try self.buildPointerOffset(offset, elem_size, .modify); |
| ... | ... | @@ -2136,18 +2023,16 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2136 | 2023 | const sentinel = ty.sentinel(); |
| 2137 | 2024 | const len = ty.arrayLen(); |
| 2138 | 2025 | const len_with_sent = len + @boolToInt(sentinel != null); |
| 2139 | | const tmp = try self.allocLocal(elem_ty); |
| 2140 | 2026 | const offset = try self.copyLocal(result, ty); |
| 2141 | 2027 | |
| 2142 | 2028 | var index: u32 = 0; |
| 2143 | 2029 | while (index < len_with_sent) : (index += 1) { |
| 2144 | | if (sentinel != null and index == len) { |
| 2145 | | try self.lowerConstant(sentinel.?, elem_ty); |
| 2146 | | } else { |
| 2030 | const elem_val = if (sentinel != null and index == len) |
| 2031 | try self.lowerConstant(sentinel.?, elem_ty) |
| 2032 | else |
| 2147 | 2033 | try self.lowerConstant(value, elem_ty); |
| 2148 | | } |
| 2149 | | try self.addLabel(.local_set, tmp.local); |
| 2150 | | try self.store(offset, tmp, elem_ty, 0); |
| 2034 | |
| 2035 | try self.store(offset, elem_val, elem_ty, 0); |
| 2151 | 2036 | |
| 2152 | 2037 | if (index != (len_with_sent - 1)) { |
| 2153 | 2038 | _ = try self.buildPointerOffset(offset, elem_size, .modify); |
| ... | ... | @@ -2156,19 +2041,18 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2156 | 2041 | } else if (val.tag() == .empty_array_sentinel) { |
| 2157 | 2042 | const elem_ty = ty.childType(); |
| 2158 | 2043 | const sent_val = ty.sentinel().?; |
| 2159 | | const tmp = try self.allocLocal(elem_ty); |
| 2160 | | try self.lowerConstant(sent_val, elem_ty); |
| 2161 | | try self.addLabel(.local_set, tmp.local); |
| 2162 | | try self.store(result, tmp, elem_ty, 0); |
| 2044 | const sentinel = try self.lowerConstant(sent_val, elem_ty); |
| 2045 | try self.store(result, sentinel, elem_ty, 0); |
| 2163 | 2046 | } else unreachable; |
| 2164 | 2047 | return result; |
| 2165 | 2048 | }, |
| 2166 | | else => |zig_type| return self.fail("Wasm TODO: emitConstant for zigTypeTag {s}", .{zig_type}), |
| 2049 | else => |zig_type| return self.fail("Wasm TODO: LowerConstant for zigTypeTag {s}", .{zig_type}), |
| 2167 | 2050 | } |
| 2168 | 2051 | } |
| 2169 | 2052 | |
| 2170 | 2053 | fn emitUndefined(self: *Self, ty: Type) InnerError!WValue { |
| 2171 | 2054 | switch (ty.zigTypeTag()) { |
| 2055 | .Bool, .ErrorSet => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 2172 | 2056 | .Int => switch (ty.intInfo(self.target).bits) { |
| 2173 | 2057 | 0...32 => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 2174 | 2058 | 33...64 => return WValue{ .imm64 = 0xaaaaaaaaaaaaaaaa }, |
| ... | ... | @@ -2185,7 +2069,7 @@ fn emitUndefined(self: *Self, ty: Type) InnerError!WValue { |
| 2185 | 2069 | var offset: u32 = 0; |
| 2186 | 2070 | while (offset < abi_size) : (offset += 1) { |
| 2187 | 2071 | try self.emitWValue(result); |
| 2188 | | try self.addImm32(0xaaaaaaaa); |
| 2072 | try self.addImm32(@bitCast(i32, @as(u32, 0xaaaaaaaa))); |
| 2189 | 2073 | switch (self.arch()) { |
| 2190 | 2074 | .wasm32 => try self.addMemArg(.i32_store8, .{ .offset = offset, .alignment = 1 }), |
| 2191 | 2075 | .wasm64 => try self.addMemArg(.i64_store8, .{ .offset = offset, .alignment = 1 }), |
| ... | ... | @@ -2199,7 +2083,50 @@ fn emitUndefined(self: *Self, ty: Type) InnerError!WValue { |
| 2199 | 2083 | .wasm64 => return WValue{ .imm64 = 0xaaaaaaaaaaaaaaaa }, |
| 2200 | 2084 | else => unreachable, |
| 2201 | 2085 | }, |
| 2202 | | else => return self.fail("Wasm TODO: emitUndefined for type: {}\n", .{ty}), |
| 2086 | .Optional => { |
| 2087 | var buf: Type.Payload.ElemType = undefined; |
| 2088 | const pl_ty = ty.optionalChild(&buf); |
| 2089 | if (ty.isPtrLikeOptional()) { |
| 2090 | return self.emitUndefined(pl_ty); |
| 2091 | } |
| 2092 | if (!pl_ty.hasCodeGenBits()) { |
| 2093 | return self.emitUndefined(Type.initTag(.u8)); |
| 2094 | } |
| 2095 | const result = try self.allocStack(ty); |
| 2096 | const abi_size = ty.abiSize(self.target); |
| 2097 | var offset: u32 = 0; |
| 2098 | while (offset < abi_size) : (offset += 1) { |
| 2099 | try self.emitWValue(result); |
| 2100 | try self.addImm32(@bitCast(i32, @as(u32, 0xaaaaaaaa))); |
| 2101 | switch (self.arch()) { |
| 2102 | .wasm32 => try self.addMemArg(.i32_store8, .{ .offset = offset, .alignment = 1 }), |
| 2103 | .wasm64 => try self.addMemArg(.i64_store8, .{ .offset = offset, .alignment = 1 }), |
| 2104 | else => unreachable, |
| 2105 | } |
| 2106 | } |
| 2107 | return result; |
| 2108 | }, |
| 2109 | .ErrorUnion => { |
| 2110 | // const error_set = ty.errorUnionSet(); |
| 2111 | const pl_ty = ty.errorUnionPayload(); |
| 2112 | if (!pl_ty.hasCodeGenBits()) { |
| 2113 | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 2114 | } |
| 2115 | const result = try self.allocStack(ty); |
| 2116 | const abi_size = ty.abiSize(self.target); |
| 2117 | var offset: u32 = 0; |
| 2118 | while (offset < abi_size) : (offset += 1) { |
| 2119 | try self.emitWValue(result); |
| 2120 | try self.addImm32(@bitCast(i32, @as(u32, 0xaaaaaaaa))); |
| 2121 | switch (self.arch()) { |
| 2122 | .wasm32 => try self.addMemArg(.i32_store8, .{ .offset = offset, .alignment = 1 }), |
| 2123 | .wasm64 => try self.addMemArg(.i64_store8, .{ .offset = offset, .alignment = 1 }), |
| 2124 | else => unreachable, |
| 2125 | } |
| 2126 | } |
| 2127 | return result; |
| 2128 | }, |
| 2129 | else => return self.fail("Wasm TODO: emitUndefined for type: {}\n", .{ty.zigTypeTag()}), |
| 2203 | 2130 | } |
| 2204 | 2131 | } |
| 2205 | 2132 | |
| ... | ... | @@ -2298,7 +2225,7 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2298 | 2225 | |
| 2299 | 2226 | fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2300 | 2227 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 2301 | | const condition = self.resolveInst(pl_op.operand); |
| 2228 | const condition = try self.resolveInst(pl_op.operand); |
| 2302 | 2229 | const extra = self.air.extraData(Air.CondBr, pl_op.payload); |
| 2303 | 2230 | const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len]; |
| 2304 | 2231 | const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| ... | ... | @@ -2325,8 +2252,8 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2325 | 2252 | |
| 2326 | 2253 | fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!WValue { |
| 2327 | 2254 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 2328 | | const lhs = self.resolveInst(bin_op.lhs); |
| 2329 | | const rhs = self.resolveInst(bin_op.rhs); |
| 2255 | const lhs = try self.resolveInst(bin_op.lhs); |
| 2256 | const rhs = try self.resolveInst(bin_op.rhs); |
| 2330 | 2257 | const operand_ty = self.air.typeOf(bin_op.lhs); |
| 2331 | 2258 | |
| 2332 | 2259 | if (operand_ty.zigTypeTag() == .Optional and !operand_ty.isPtrLikeOptional()) { |
| ... | ... | @@ -2377,7 +2304,7 @@ fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2377 | 2304 | |
| 2378 | 2305 | // if operand has codegen bits we should break with a value |
| 2379 | 2306 | if (self.air.typeOf(br.operand).hasCodeGenBits()) { |
| 2380 | | try self.emitWValue(self.resolveInst(br.operand)); |
| 2307 | try self.emitWValue(try self.resolveInst(br.operand)); |
| 2381 | 2308 | |
| 2382 | 2309 | if (block.value != .none) { |
| 2383 | 2310 | try self.addLabel(.local_set, block.value.local); |
| ... | ... | @@ -2395,7 +2322,7 @@ fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2395 | 2322 | fn airNot(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2396 | 2323 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2397 | 2324 | |
| 2398 | | const operand = self.resolveInst(ty_op.operand); |
| 2325 | const operand = try self.resolveInst(ty_op.operand); |
| 2399 | 2326 | try self.emitWValue(operand); |
| 2400 | 2327 | |
| 2401 | 2328 | // wasm does not have booleans nor the `not` instruction, therefore compare with 0 |
| ... | ... | @@ -2425,20 +2352,21 @@ fn airUnreachable(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2425 | 2352 | |
| 2426 | 2353 | fn airBitcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2427 | 2354 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2428 | | const operand = self.resolveInst(ty_op.operand); |
| 2429 | | if (operand == .constant) { |
| 2430 | | const result = try self.allocLocal(self.air.typeOfIndex(inst)); |
| 2431 | | try self.emitWValue(operand); |
| 2432 | | try self.addLabel(.local_set, result.local); |
| 2433 | | return result; |
| 2434 | | } |
| 2355 | const operand = try self.resolveInst(ty_op.operand); |
| 2356 | // if (operand == .constant) { |
| 2357 | // std.debug.print("Let's take a look at this!!!!!!\n--------------------\n", .{}); |
| 2358 | // const result = try self.allocLocal(self.air.typeOfIndex(inst)); |
| 2359 | // try self.emitWValue(operand); |
| 2360 | // try self.addLabel(.local_set, result.local); |
| 2361 | // return result; |
| 2362 | // } |
| 2435 | 2363 | return operand; |
| 2436 | 2364 | } |
| 2437 | 2365 | |
| 2438 | 2366 | fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2439 | 2367 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 2440 | 2368 | const extra = self.air.extraData(Air.StructField, ty_pl.payload); |
| 2441 | | const struct_ptr = self.resolveInst(extra.data.struct_operand); |
| 2369 | const struct_ptr = try self.resolveInst(extra.data.struct_operand); |
| 2442 | 2370 | const struct_ty = self.air.typeOf(extra.data.struct_operand).childType(); |
| 2443 | 2371 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(extra.data.field_index, self.target)) catch { |
| 2444 | 2372 | return self.fail("Field type '{}' too big to fit into stack frame", .{ |
| ... | ... | @@ -2450,7 +2378,7 @@ fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2450 | 2378 | |
| 2451 | 2379 | fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerError!WValue { |
| 2452 | 2380 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2453 | | const struct_ptr = self.resolveInst(ty_op.operand); |
| 2381 | const struct_ptr = try self.resolveInst(ty_op.operand); |
| 2454 | 2382 | const struct_ty = self.air.typeOf(ty_op.operand).childType(); |
| 2455 | 2383 | const field_ty = struct_ty.structFieldType(index); |
| 2456 | 2384 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(index, self.target)) catch { |
| ... | ... | @@ -2462,47 +2390,35 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerEr |
| 2462 | 2390 | } |
| 2463 | 2391 | |
| 2464 | 2392 | fn structFieldPtr(self: *Self, struct_ptr: WValue, offset: u32) InnerError!WValue { |
| 2465 | | var final_offset = offset; |
| 2466 | | const local = switch (struct_ptr) { |
| 2467 | | .local => |local| local, |
| 2468 | | .local_with_offset => |with_offset| blk: { |
| 2469 | | final_offset += with_offset.offset; |
| 2470 | | break :blk with_offset.local; |
| 2471 | | }, |
| 2472 | | else => unreachable, |
| 2473 | | }; |
| 2474 | | return self.buildPointerOffset(.{ .local = local }, final_offset, .new); |
| 2393 | return self.buildPointerOffset(struct_ptr, offset, .new); |
| 2475 | 2394 | } |
| 2476 | 2395 | |
| 2477 | 2396 | fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2478 | | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2397 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 2479 | 2398 | |
| 2480 | 2399 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 2481 | 2400 | const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 2482 | 2401 | const struct_ty = self.air.typeOf(struct_field.struct_operand); |
| 2483 | | const operand = self.resolveInst(struct_field.struct_operand); |
| 2402 | const operand = try self.resolveInst(struct_field.struct_operand); |
| 2484 | 2403 | const field_index = struct_field.field_index; |
| 2485 | 2404 | const field_ty = struct_ty.structFieldType(field_index); |
| 2486 | | if (!field_ty.hasCodeGenBits()) return WValue.none; |
| 2405 | if (!field_ty.hasCodeGenBits()) return WValue{ .none = {} }; |
| 2487 | 2406 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(field_index, self.target)) catch { |
| 2488 | 2407 | return self.fail("Field type '{}' too big to fit into stack frame", .{field_ty}); |
| 2489 | 2408 | }; |
| 2490 | 2409 | |
| 2491 | 2410 | if (self.isByRef(field_ty)) { |
| 2492 | | return WValue{ .local_with_offset = .{ .local = operand.local, .offset = offset } }; |
| 2411 | return self.buildPointerOffset(operand, offset, .new); |
| 2493 | 2412 | } |
| 2494 | 2413 | |
| 2495 | | switch (operand) { |
| 2496 | | .local_with_offset => |with_offset| return try self.load(operand, field_ty, offset + with_offset.offset), |
| 2497 | | else => return try self.load(operand, field_ty, offset), |
| 2498 | | } |
| 2414 | return self.load(operand, field_ty, offset); |
| 2499 | 2415 | } |
| 2500 | 2416 | |
| 2501 | 2417 | fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2502 | 2418 | // result type is always 'noreturn' |
| 2503 | 2419 | const blocktype = wasm.block_empty; |
| 2504 | 2420 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 2505 | | const target = self.resolveInst(pl_op.operand); |
| 2421 | const target = try self.resolveInst(pl_op.operand); |
| 2506 | 2422 | const target_ty = self.air.typeOf(pl_op.operand); |
| 2507 | 2423 | const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload); |
| 2508 | 2424 | var extra_index: usize = switch_br.end; |
| ... | ... | @@ -2650,7 +2566,7 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2650 | 2566 | |
| 2651 | 2567 | fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!WValue { |
| 2652 | 2568 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 2653 | | const operand = self.resolveInst(un_op); |
| 2569 | const operand = try self.resolveInst(un_op); |
| 2654 | 2570 | const err_ty = self.air.typeOf(un_op); |
| 2655 | 2571 | const pl_ty = err_ty.errorUnionPayload(); |
| 2656 | 2572 | |
| ... | ... | @@ -2675,7 +2591,7 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!W |
| 2675 | 2591 | fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2676 | 2592 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 2677 | 2593 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2678 | | const operand = self.resolveInst(ty_op.operand); |
| 2594 | const operand = try self.resolveInst(ty_op.operand); |
| 2679 | 2595 | const err_ty = self.air.typeOf(ty_op.operand); |
| 2680 | 2596 | const payload_ty = err_ty.errorUnionPayload(); |
| 2681 | 2597 | if (!payload_ty.hasCodeGenBits()) return WValue{ .none = {} }; |
| ... | ... | @@ -2690,7 +2606,7 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2690 | 2606 | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2691 | 2607 | |
| 2692 | 2608 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2693 | | const operand = self.resolveInst(ty_op.operand); |
| 2609 | const operand = try self.resolveInst(ty_op.operand); |
| 2694 | 2610 | const err_ty = self.air.typeOf(ty_op.operand); |
| 2695 | 2611 | const payload_ty = err_ty.errorUnionPayload(); |
| 2696 | 2612 | if (!payload_ty.hasCodeGenBits()) { |
| ... | ... | @@ -2703,7 +2619,7 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2703 | 2619 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2704 | 2620 | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2705 | 2621 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2706 | | const operand = self.resolveInst(ty_op.operand); |
| 2622 | const operand = try self.resolveInst(ty_op.operand); |
| 2707 | 2623 | |
| 2708 | 2624 | const op_ty = self.air.typeOf(ty_op.operand); |
| 2709 | 2625 | if (!op_ty.hasCodeGenBits()) return operand; |
| ... | ... | @@ -2725,7 +2641,7 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2725 | 2641 | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2726 | 2642 | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2727 | 2643 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2728 | | const operand = self.resolveInst(ty_op.operand); |
| 2644 | const operand = try self.resolveInst(ty_op.operand); |
| 2729 | 2645 | const err_ty = self.air.getRefType(ty_op.ty); |
| 2730 | 2646 | |
| 2731 | 2647 | const err_union = try self.allocStack(err_ty); |
| ... | ... | @@ -2739,7 +2655,7 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2739 | 2655 | |
| 2740 | 2656 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2741 | 2657 | const ty = self.air.getRefType(ty_op.ty); |
| 2742 | | const operand = self.resolveInst(ty_op.operand); |
| 2658 | const operand = try self.resolveInst(ty_op.operand); |
| 2743 | 2659 | const ref_ty = self.air.typeOf(ty_op.operand); |
| 2744 | 2660 | const ref_info = ref_ty.intInfo(self.target); |
| 2745 | 2661 | const wanted_info = ty.intInfo(self.target); |
| ... | ... | @@ -2770,7 +2686,7 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2770 | 2686 | |
| 2771 | 2687 | fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!WValue { |
| 2772 | 2688 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 2773 | | const operand = self.resolveInst(un_op); |
| 2689 | const operand = try self.resolveInst(un_op); |
| 2774 | 2690 | |
| 2775 | 2691 | const op_ty = self.air.typeOf(un_op); |
| 2776 | 2692 | const optional_ty = if (op_kind == .ptr) op_ty.childType() else op_ty; |
| ... | ... | @@ -2801,7 +2717,7 @@ fn isNull(self: *Self, operand: WValue, optional_ty: Type, opcode: wasm.Opcode) |
| 2801 | 2717 | fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2802 | 2718 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 2803 | 2719 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2804 | | const operand = self.resolveInst(ty_op.operand); |
| 2720 | const operand = try self.resolveInst(ty_op.operand); |
| 2805 | 2721 | const opt_ty = self.air.typeOf(ty_op.operand); |
| 2806 | 2722 | const payload_ty = self.air.typeOfIndex(inst); |
| 2807 | 2723 | if (!payload_ty.hasCodeGenBits()) return WValue{ .none = {} }; |
| ... | ... | @@ -2820,7 +2736,7 @@ fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2820 | 2736 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 2821 | 2737 | |
| 2822 | 2738 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2823 | | const operand = self.resolveInst(ty_op.operand); |
| 2739 | const operand = try self.resolveInst(ty_op.operand); |
| 2824 | 2740 | const opt_ty = self.air.typeOf(ty_op.operand).childType(); |
| 2825 | 2741 | |
| 2826 | 2742 | var buf: Type.Payload.ElemType = undefined; |
| ... | ... | @@ -2835,7 +2751,7 @@ fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2835 | 2751 | |
| 2836 | 2752 | fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2837 | 2753 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2838 | | const operand = self.resolveInst(ty_op.operand); |
| 2754 | const operand = try self.resolveInst(ty_op.operand); |
| 2839 | 2755 | const opt_ty = self.air.typeOf(ty_op.operand).childType(); |
| 2840 | 2756 | var buf: Type.Payload.ElemType = undefined; |
| 2841 | 2757 | const payload_ty = opt_ty.optionalChild(&buf); |
| ... | ... | @@ -2871,7 +2787,7 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2871 | 2787 | return non_null_bit; |
| 2872 | 2788 | } |
| 2873 | 2789 | |
| 2874 | | const operand = self.resolveInst(ty_op.operand); |
| 2790 | const operand = try self.resolveInst(ty_op.operand); |
| 2875 | 2791 | const op_ty = self.air.typeOfIndex(inst); |
| 2876 | 2792 | if (op_ty.isPtrLikeOptional()) { |
| 2877 | 2793 | return operand; |
| ... | ... | @@ -2897,8 +2813,8 @@ fn airSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2897 | 2813 | |
| 2898 | 2814 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 2899 | 2815 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2900 | | const lhs = self.resolveInst(bin_op.lhs); |
| 2901 | | const rhs = self.resolveInst(bin_op.rhs); |
| 2816 | const lhs = try self.resolveInst(bin_op.lhs); |
| 2817 | const rhs = try self.resolveInst(bin_op.rhs); |
| 2902 | 2818 | const slice_ty = self.air.typeOfIndex(inst); |
| 2903 | 2819 | |
| 2904 | 2820 | const slice = try self.allocStack(slice_ty); |
| ... | ... | @@ -2912,7 +2828,7 @@ fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2912 | 2828 | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2913 | 2829 | |
| 2914 | 2830 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2915 | | const operand = self.resolveInst(ty_op.operand); |
| 2831 | const operand = try self.resolveInst(ty_op.operand); |
| 2916 | 2832 | |
| 2917 | 2833 | return try self.load(operand, Type.usize, self.ptrSize()); |
| 2918 | 2834 | } |
| ... | ... | @@ -2922,8 +2838,8 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2922 | 2838 | |
| 2923 | 2839 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 2924 | 2840 | const slice_ty = self.air.typeOf(bin_op.lhs); |
| 2925 | | const slice = self.resolveInst(bin_op.lhs); |
| 2926 | | const index = self.resolveInst(bin_op.rhs); |
| 2841 | const slice = try self.resolveInst(bin_op.lhs); |
| 2842 | const index = try self.resolveInst(bin_op.rhs); |
| 2927 | 2843 | const elem_ty = slice_ty.childType(); |
| 2928 | 2844 | const elem_size = elem_ty.abiSize(self.target); |
| 2929 | 2845 | |
| ... | ... | @@ -2954,8 +2870,8 @@ fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2954 | 2870 | const elem_ty = self.air.getRefType(ty_pl.ty).childType(); |
| 2955 | 2871 | const elem_size = elem_ty.abiSize(self.target); |
| 2956 | 2872 | |
| 2957 | | const slice = self.resolveInst(bin_op.lhs); |
| 2958 | | const index = self.resolveInst(bin_op.rhs); |
| 2873 | const slice = try self.resolveInst(bin_op.lhs); |
| 2874 | const index = try self.resolveInst(bin_op.rhs); |
| 2959 | 2875 | |
| 2960 | 2876 | const slice_ptr = try self.load(slice, slice_ty, 0); |
| 2961 | 2877 | try self.addLabel(.local_get, slice_ptr.local); |
| ... | ... | @@ -2974,14 +2890,14 @@ fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2974 | 2890 | fn airSlicePtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2975 | 2891 | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2976 | 2892 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2977 | | const operand = self.resolveInst(ty_op.operand); |
| 2893 | const operand = try self.resolveInst(ty_op.operand); |
| 2978 | 2894 | return try self.load(operand, Type.usize, 0); |
| 2979 | 2895 | } |
| 2980 | 2896 | |
| 2981 | 2897 | fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2982 | 2898 | if (self.liveness.isUnused(inst)) return WValue.none; |
| 2983 | 2899 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2984 | | const operand = self.resolveInst(ty_op.operand); |
| 2900 | const operand = try self.resolveInst(ty_op.operand); |
| 2985 | 2901 | const op_ty = self.air.typeOf(ty_op.operand); |
| 2986 | 2902 | const int_info = self.air.getRefType(ty_op.ty).intInfo(self.target); |
| 2987 | 2903 | const wanted_bits = int_info.bits; |
| ... | ... | @@ -3040,12 +2956,12 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3040 | 2956 | |
| 3041 | 2957 | fn airBoolToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3042 | 2958 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 3043 | | return self.resolveInst(un_op); |
| 2959 | return try self.resolveInst(un_op); |
| 3044 | 2960 | } |
| 3045 | 2961 | |
| 3046 | 2962 | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3047 | 2963 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 3048 | | const operand = self.resolveInst(ty_op.operand); |
| 2964 | const operand = try self.resolveInst(ty_op.operand); |
| 3049 | 2965 | const array_ty = self.air.typeOf(ty_op.operand).childType(); |
| 3050 | 2966 | const ty = Type.@"usize"; |
| 3051 | 2967 | const ptr_width = @intCast(u32, ty.abiSize(self.target)); |
| ... | ... | @@ -3072,7 +2988,7 @@ fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3072 | 2988 | fn airPtrToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3073 | 2989 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3074 | 2990 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 3075 | | return self.resolveInst(un_op); |
| 2991 | return try self.resolveInst(un_op); |
| 3076 | 2992 | } |
| 3077 | 2993 | |
| 3078 | 2994 | fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| ... | ... | @@ -3080,8 +2996,8 @@ fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3080 | 2996 | |
| 3081 | 2997 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 3082 | 2998 | const ptr_ty = self.air.typeOf(bin_op.lhs); |
| 3083 | | const pointer = self.resolveInst(bin_op.lhs); |
| 3084 | | const index = self.resolveInst(bin_op.rhs); |
| 2999 | const pointer = try self.resolveInst(bin_op.lhs); |
| 3000 | const index = try self.resolveInst(bin_op.rhs); |
| 3085 | 3001 | const elem_ty = ptr_ty.childType(); |
| 3086 | 3002 | const elem_size = elem_ty.abiSize(self.target); |
| 3087 | 3003 | |
| ... | ... | @@ -3115,8 +3031,8 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3115 | 3031 | const elem_ty = self.air.getRefType(ty_pl.ty).childType(); |
| 3116 | 3032 | const elem_size = elem_ty.abiSize(self.target); |
| 3117 | 3033 | |
| 3118 | | const ptr = self.resolveInst(bin_op.lhs); |
| 3119 | | const index = self.resolveInst(bin_op.rhs); |
| 3034 | const ptr = try self.resolveInst(bin_op.lhs); |
| 3035 | const index = try self.resolveInst(bin_op.rhs); |
| 3120 | 3036 | |
| 3121 | 3037 | // load pointer onto the stack |
| 3122 | 3038 | if (ptr_ty.isSlice()) { |
| ... | ... | @@ -3140,8 +3056,8 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3140 | 3056 | fn airPtrBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 3141 | 3057 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3142 | 3058 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 3143 | | const ptr = self.resolveInst(bin_op.lhs); |
| 3144 | | const offset = self.resolveInst(bin_op.rhs); |
| 3059 | const ptr = try self.resolveInst(bin_op.lhs); |
| 3060 | const offset = try self.resolveInst(bin_op.rhs); |
| 3145 | 3061 | const ptr_ty = self.air.typeOf(bin_op.lhs); |
| 3146 | 3062 | const pointee_ty = switch (ptr_ty.ptrSize()) { |
| 3147 | 3063 | .One => ptr_ty.childType().childType(), // ptr to array, so get array element type |
| ... | ... | @@ -3167,9 +3083,9 @@ fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3167 | 3083 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 3168 | 3084 | const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 3169 | 3085 | |
| 3170 | | const ptr = self.resolveInst(pl_op.operand); |
| 3171 | | const value = self.resolveInst(bin_op.lhs); |
| 3172 | | const len = self.resolveInst(bin_op.rhs); |
| 3086 | const ptr = try self.resolveInst(pl_op.operand); |
| 3087 | const value = try self.resolveInst(bin_op.lhs); |
| 3088 | const len = try self.resolveInst(bin_op.rhs); |
| 3173 | 3089 | try self.memSet(ptr, len, value); |
| 3174 | 3090 | |
| 3175 | 3091 | return WValue.none; |
| ... | ... | @@ -3236,8 +3152,8 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3236 | 3152 | |
| 3237 | 3153 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 3238 | 3154 | const array_ty = self.air.typeOf(bin_op.lhs); |
| 3239 | | const array = self.resolveInst(bin_op.lhs); |
| 3240 | | const index = self.resolveInst(bin_op.rhs); |
| 3155 | const array = try self.resolveInst(bin_op.lhs); |
| 3156 | const index = try self.resolveInst(bin_op.rhs); |
| 3241 | 3157 | const elem_ty = array_ty.childType(); |
| 3242 | 3158 | const elem_size = elem_ty.abiSize(self.target); |
| 3243 | 3159 | |
| ... | ... | @@ -3261,7 +3177,7 @@ fn airFloatToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3261 | 3177 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3262 | 3178 | |
| 3263 | 3179 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 3264 | | const operand = self.resolveInst(ty_op.operand); |
| 3180 | const operand = try self.resolveInst(ty_op.operand); |
| 3265 | 3181 | const dest_ty = self.air.typeOfIndex(inst); |
| 3266 | 3182 | const op_ty = self.air.typeOf(ty_op.operand); |
| 3267 | 3183 | |
| ... | ... | @@ -3283,7 +3199,7 @@ fn airSplat(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3283 | 3199 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3284 | 3200 | |
| 3285 | 3201 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 3286 | | const operand = self.resolveInst(ty_op.operand); |
| 3202 | const operand = try self.resolveInst(ty_op.operand); |
| 3287 | 3203 | |
| 3288 | 3204 | _ = ty_op; |
| 3289 | 3205 | _ = operand; |