|
29 | 29 | #include "ir/module-utils.h" |
30 | 30 | #include "parsing.h" |
31 | 31 | #include "wasm-builder.h" |
| 32 | +#include "wasm-ir-builder.h" |
32 | 33 | #include "wasm-traversal.h" |
33 | 34 | #include "wasm-validator.h" |
34 | 35 | #include "wasm.h" |
@@ -1543,8 +1544,6 @@ class WasmBinaryReader { |
1543 | 1544 | Signature getSignatureByTypeIndex(Index index); |
1544 | 1545 | Signature getSignatureByFunctionIndex(Index index); |
1545 | 1546 |
|
1546 | | - size_t nextLabel; |
1547 | | - |
1548 | 1547 | Name getNextLabel(); |
1549 | 1548 |
|
1550 | 1549 | // We read the names section first so we know in advance what names various |
@@ -1573,67 +1572,19 @@ class WasmBinaryReader { |
1573 | 1572 | void readVars(); |
1574 | 1573 | void setLocalNames(Function& func, Index i); |
1575 | 1574 |
|
| 1575 | + Result<> readInst(); |
| 1576 | + |
1576 | 1577 | void readExports(); |
1577 | 1578 |
|
1578 | 1579 | // The strings in the strings section (which are referred to by StringConst). |
1579 | 1580 | std::vector<Name> strings; |
1580 | 1581 | void readStrings(); |
| 1582 | + Name getIndexedString(); |
1581 | 1583 |
|
1582 | 1584 | Expression* readExpression(); |
1583 | 1585 | void readGlobals(); |
1584 | 1586 |
|
1585 | | - struct BreakTarget { |
1586 | | - Name name; |
1587 | | - Type type; |
1588 | | - BreakTarget(Name name, Type type) : name(name), type(type) {} |
1589 | | - }; |
1590 | | - std::vector<BreakTarget> breakStack; |
1591 | | - // the names that breaks target. this lets us know if a block has breaks to it |
1592 | | - // or not. |
1593 | | - std::unordered_set<Name> breakTargetNames; |
1594 | | - // the names that delegates target. |
1595 | | - std::unordered_set<Name> exceptionTargetNames; |
1596 | | - |
1597 | | - std::vector<Expression*> expressionStack; |
1598 | | - |
1599 | | - // Control flow structure parsing: these have not just the normal binary |
1600 | | - // data for an instruction, but also some bytes later on like "end" or "else". |
1601 | | - // We must be aware of the connection between those things, for debug info. |
1602 | | - std::vector<Expression*> controlFlowStack; |
1603 | | - |
1604 | | - // Called when we parse the beginning of a control flow structure. |
1605 | | - void startControlFlow(Expression* curr); |
1606 | | - |
1607 | | - // set when we know code is unreachable in the sense of the wasm spec: we are |
1608 | | - // in a block and after an unreachable element. this helps parse stacky wasm |
1609 | | - // code, which can be unsuitable for our IR when unreachable. |
1610 | | - bool unreachableInTheWasmSense; |
1611 | | - |
1612 | | - // set when the current code being processed will not be emitted in the |
1613 | | - // output, which is the case when it is literally unreachable, for example, |
1614 | | - // (block $a |
1615 | | - // (unreachable) |
1616 | | - // (block $b |
1617 | | - // ;; code here is reachable in the wasm sense, even though $b as a whole |
1618 | | - // ;; is not |
1619 | | - // (unreachable) |
1620 | | - // ;; code here is unreachable in the wasm sense |
1621 | | - // ) |
1622 | | - // ) |
1623 | | - bool willBeIgnored; |
1624 | | - |
1625 | | - BinaryConsts::ASTNodes lastSeparator = BinaryConsts::End; |
1626 | | - |
1627 | | - // process a block-type scope, until an end or else marker, or the end of the |
1628 | | - // function |
1629 | | - void processExpressions(); |
1630 | | - void skipUnreachableCode(); |
1631 | | - |
1632 | | - void pushExpression(Expression* curr); |
1633 | | - Expression* popExpression(); |
1634 | | - Expression* popNonVoidExpression(); |
1635 | | - Expression* popTuple(size_t numElems); |
1636 | | - Expression* popTypedExpression(Type type); |
| 1587 | + IRBuilder builder; |
1637 | 1588 |
|
1638 | 1589 | // validations that cannot be performed on the Module |
1639 | 1590 | void validateBinary(); |
@@ -1663,127 +1614,12 @@ class WasmBinaryReader { |
1663 | 1614 | void readNextDebugLocation(); |
1664 | 1615 | void readSourceMapHeader(); |
1665 | 1616 |
|
1666 | | - // AST reading |
1667 | | - int depth = 0; // only for debugging |
1668 | | - |
1669 | | - BinaryConsts::ASTNodes readExpression(Expression*& curr); |
1670 | | - void pushBlockElements(Block* curr, Type type, size_t start); |
1671 | | - void visitBlock(Block* curr); |
1672 | | - |
1673 | | - // Gets a block of expressions. If it's just one, return that singleton. |
1674 | | - Expression* getBlockOrSingleton(Type type); |
1675 | | - |
1676 | | - BreakTarget getBreakTarget(int32_t offset); |
1677 | | - Name getExceptionTargetName(int32_t offset); |
1678 | | - |
1679 | 1617 | Index readMemoryAccess(Address& alignment, Address& offset); |
| 1618 | + std::tuple<Name, Address, Address> getMemarg(); |
1680 | 1619 |
|
1681 | | - void visitIf(If* curr); |
1682 | | - void visitLoop(Loop* curr); |
1683 | | - void visitBreak(Break* curr, uint8_t code); |
1684 | | - void visitSwitch(Switch* curr); |
1685 | | - void visitCall(Call* curr); |
1686 | | - void visitCallIndirect(CallIndirect* curr); |
1687 | | - void visitLocalGet(LocalGet* curr); |
1688 | | - void visitLocalSet(LocalSet* curr, uint8_t code); |
1689 | | - void visitGlobalGet(GlobalGet* curr); |
1690 | | - void visitGlobalSet(GlobalSet* curr); |
1691 | | - bool maybeVisitLoad(Expression*& out, |
1692 | | - uint8_t code, |
1693 | | - std::optional<BinaryConsts::ASTNodes> prefix); |
1694 | | - bool maybeVisitStore(Expression*& out, |
1695 | | - uint8_t code, |
1696 | | - std::optional<BinaryConsts::ASTNodes> prefix); |
1697 | | - bool maybeVisitNontrappingTrunc(Expression*& out, uint32_t code); |
1698 | | - bool maybeVisitAtomicRMW(Expression*& out, uint8_t code); |
1699 | | - bool maybeVisitAtomicCmpxchg(Expression*& out, uint8_t code); |
1700 | | - bool maybeVisitAtomicWait(Expression*& out, uint8_t code); |
1701 | | - bool maybeVisitAtomicNotify(Expression*& out, uint8_t code); |
1702 | | - bool maybeVisitAtomicFence(Expression*& out, uint8_t code); |
1703 | | - bool maybeVisitConst(Expression*& out, uint8_t code); |
1704 | | - bool maybeVisitUnary(Expression*& out, uint8_t code); |
1705 | | - bool maybeVisitBinary(Expression*& out, uint8_t code); |
1706 | | - bool maybeVisitTruncSat(Expression*& out, uint32_t code); |
1707 | | - bool maybeVisitSIMDBinary(Expression*& out, uint32_t code); |
1708 | | - bool maybeVisitSIMDUnary(Expression*& out, uint32_t code); |
1709 | | - bool maybeVisitSIMDConst(Expression*& out, uint32_t code); |
1710 | | - bool maybeVisitSIMDStore(Expression*& out, uint32_t code); |
1711 | | - bool maybeVisitSIMDExtract(Expression*& out, uint32_t code); |
1712 | | - bool maybeVisitSIMDReplace(Expression*& out, uint32_t code); |
1713 | | - bool maybeVisitSIMDShuffle(Expression*& out, uint32_t code); |
1714 | | - bool maybeVisitSIMDTernary(Expression*& out, uint32_t code); |
1715 | | - bool maybeVisitSIMDShift(Expression*& out, uint32_t code); |
1716 | | - bool maybeVisitSIMDLoad(Expression*& out, uint32_t code); |
1717 | | - bool maybeVisitSIMDLoadStoreLane(Expression*& out, uint32_t code); |
1718 | | - bool maybeVisitMemoryInit(Expression*& out, uint32_t code); |
1719 | | - bool maybeVisitDataDrop(Expression*& out, uint32_t code); |
1720 | | - bool maybeVisitMemoryCopy(Expression*& out, uint32_t code); |
1721 | | - bool maybeVisitMemoryFill(Expression*& out, uint32_t code); |
1722 | | - bool maybeVisitTableSize(Expression*& out, uint32_t code); |
1723 | | - bool maybeVisitTableGrow(Expression*& out, uint32_t code); |
1724 | | - bool maybeVisitTableFill(Expression*& out, uint32_t code); |
1725 | | - bool maybeVisitTableCopy(Expression*& out, uint32_t code); |
1726 | | - bool maybeVisitTableInit(Expression*& out, uint32_t code); |
1727 | | - bool maybeVisitRefI31(Expression*& out, uint32_t code); |
1728 | | - bool maybeVisitI31Get(Expression*& out, uint32_t code); |
1729 | | - bool maybeVisitRefTest(Expression*& out, uint32_t code); |
1730 | | - bool maybeVisitRefCast(Expression*& out, uint32_t code); |
1731 | | - bool maybeVisitBrOn(Expression*& out, uint32_t code); |
1732 | | - bool maybeVisitStructNew(Expression*& out, uint32_t code); |
1733 | | - bool maybeVisitStructGet(Expression*& out, uint32_t code); |
1734 | | - bool maybeVisitStructSet(Expression*& out, uint32_t code); |
1735 | | - bool maybeVisitArrayNewData(Expression*& out, uint32_t code); |
1736 | | - bool maybeVisitArrayNewElem(Expression*& out, uint32_t code); |
1737 | | - bool maybeVisitArrayNewFixed(Expression*& out, uint32_t code); |
1738 | | - bool maybeVisitArrayGet(Expression*& out, uint32_t code); |
1739 | | - bool maybeVisitArraySet(Expression*& out, uint32_t code); |
1740 | | - bool maybeVisitArrayLen(Expression*& out, uint32_t code); |
1741 | | - bool maybeVisitArrayCopy(Expression*& out, uint32_t code); |
1742 | | - bool maybeVisitArrayFill(Expression*& out, uint32_t code); |
1743 | | - bool maybeVisitArrayInit(Expression*& out, uint32_t code); |
1744 | | - bool maybeVisitStringNew(Expression*& out, uint32_t code); |
1745 | | - bool maybeVisitStringAsWTF16(Expression*& out, uint32_t code); |
1746 | | - bool maybeVisitStringConst(Expression*& out, uint32_t code); |
1747 | | - bool maybeVisitStringMeasure(Expression*& out, uint32_t code); |
1748 | | - bool maybeVisitStringEncode(Expression*& out, uint32_t code); |
1749 | | - bool maybeVisitStringConcat(Expression*& out, uint32_t code); |
1750 | | - bool maybeVisitStringEq(Expression*& out, uint32_t code); |
1751 | | - bool maybeVisitStringWTF16Get(Expression*& out, uint32_t code); |
1752 | | - bool maybeVisitStringSliceWTF(Expression*& out, uint32_t code); |
1753 | | - void visitSelect(Select* curr, uint8_t code); |
1754 | | - void visitReturn(Return* curr); |
1755 | | - void visitMemorySize(MemorySize* curr); |
1756 | | - void visitMemoryGrow(MemoryGrow* curr); |
1757 | | - void visitNop(Nop* curr); |
1758 | | - void visitUnreachable(Unreachable* curr); |
1759 | | - void visitDrop(Drop* curr); |
1760 | | - void visitRefNull(RefNull* curr); |
1761 | | - void visitRefIsNull(RefIsNull* curr); |
1762 | | - void visitRefFunc(RefFunc* curr); |
1763 | | - void visitRefEq(RefEq* curr); |
1764 | | - void visitTableGet(TableGet* curr); |
1765 | | - void visitTableSet(TableSet* curr); |
1766 | | - void visitTryOrTryInBlock(Expression*& out); |
1767 | | - void visitTryTable(TryTable* curr); |
1768 | | - void visitThrow(Throw* curr); |
1769 | | - void visitRethrow(Rethrow* curr); |
1770 | | - void visitThrowRef(ThrowRef* curr); |
1771 | | - void visitCallRef(CallRef* curr); |
1772 | | - void visitRefAsCast(RefCast* curr, uint32_t code); |
1773 | | - void visitRefAs(RefAs* curr, uint8_t code); |
1774 | | - void visitContNew(ContNew* curr); |
1775 | | - void visitContBind(ContBind* curr); |
1776 | | - void visitResume(Resume* curr); |
1777 | | - void visitSuspend(Suspend* curr); |
1778 | | - |
1779 | | - [[noreturn]] void throwError(std::string text); |
1780 | | - |
1781 | | - // Struct/Array instructions have an unnecessary heap type that is just for |
1782 | | - // validation (except for the case of unreachability, but that's not a problem |
1783 | | - // anyhow, we can ignore it there). That is, we also have a reference typed |
1784 | | - // child from which we can infer the type anyhow, and we just need to check |
1785 | | - // that type is the same. |
1786 | | - void validateHeapTypeUsingChild(Expression* child, HeapType heapType); |
| 1620 | + [[noreturn]] void throwError(std::string text) { |
| 1621 | + throw ParseException(text, 0, pos); |
| 1622 | + } |
1787 | 1623 |
|
1788 | 1624 | private: |
1789 | 1625 | bool hasDWARFSections(); |
|
0 commit comments