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,10 +1544,6 @@ class WasmBinaryReader {
1543
1544
Signature getSignatureByTypeIndex (Index index);
1544
1545
Signature getSignatureByFunctionIndex (Index index);
1545
1546
1546
- size_t nextLabel;
1547
-
1548
- Name getNextLabel ();
1549
-
1550
1547
// We read functions, globals, etc. before we know their final names, so we
1551
1548
// need to backpatch the names later. Map the original names to their indices
1552
1549
// so we can find the final names based on index.
@@ -1559,81 +1556,27 @@ class WasmBinaryReader {
1559
1556
std::unordered_map<Name, Index> elemIndices;
1560
1557
1561
1558
Function* currFunction = nullptr ;
1562
- // before we see a function (like global init expressions), there is no end of
1563
- // function to check
1564
- Index endOfFunction = -1 ;
1565
1559
1566
1560
std::map<Index, Name> elemTables;
1567
1561
1568
- // Throws a parsing error if we are not in a function context
1569
- void requireFunctionContext (const char * error);
1570
-
1571
1562
void readFunctions ();
1572
1563
void readVars ();
1573
1564
1565
+ [[nodiscard]] Result<> readInst ();
1566
+
1574
1567
std::map<Export*, Index> exportIndices;
1575
1568
std::vector<std::unique_ptr<Export>> exportOrder;
1576
1569
void readExports ();
1577
1570
1578
1571
// The strings in the strings section (which are referred to by StringConst).
1579
1572
std::vector<Name> strings;
1580
1573
void readStrings ();
1574
+ Name getIndexedString ();
1581
1575
1582
1576
Expression* readExpression ();
1583
1577
void readGlobals ();
1584
1578
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);
1579
+ IRBuilder builder;
1637
1580
1638
1581
void validateBinary (); // validations that cannot be performed on the Module
1639
1582
void processNames ();
@@ -1661,127 +1604,12 @@ class WasmBinaryReader {
1661
1604
void readNextDebugLocation ();
1662
1605
void readSourceMapHeader ();
1663
1606
1664
- // AST reading
1665
- int depth = 0 ; // only for debugging
1666
-
1667
- BinaryConsts::ASTNodes readExpression (Expression*& curr);
1668
- void pushBlockElements (Block* curr, Type type, size_t start);
1669
- void visitBlock (Block* curr);
1670
-
1671
- // Gets a block of expressions. If it's just one, return that singleton.
1672
- Expression* getBlockOrSingleton (Type type);
1673
-
1674
- BreakTarget getBreakTarget (int32_t offset);
1675
- Name getExceptionTargetName (int32_t offset);
1676
-
1677
1607
Index readMemoryAccess (Address& alignment, Address& offset);
1608
+ std::tuple<Name, Address, Address> getMemarg ();
1678
1609
1679
- void visitIf (If* curr);
1680
- void visitLoop (Loop* curr);
1681
- void visitBreak (Break* curr, uint8_t code);
1682
- void visitSwitch (Switch* curr);
1683
- void visitCall (Call* curr);
1684
- void visitCallIndirect (CallIndirect* curr);
1685
- void visitLocalGet (LocalGet* curr);
1686
- void visitLocalSet (LocalSet* curr, uint8_t code);
1687
- void visitGlobalGet (GlobalGet* curr);
1688
- void visitGlobalSet (GlobalSet* curr);
1689
- bool maybeVisitLoad (Expression*& out,
1690
- uint8_t code,
1691
- std::optional<BinaryConsts::ASTNodes> prefix);
1692
- bool maybeVisitStore (Expression*& out,
1693
- uint8_t code,
1694
- std::optional<BinaryConsts::ASTNodes> prefix);
1695
- bool maybeVisitNontrappingTrunc (Expression*& out, uint32_t code);
1696
- bool maybeVisitAtomicRMW (Expression*& out, uint8_t code);
1697
- bool maybeVisitAtomicCmpxchg (Expression*& out, uint8_t code);
1698
- bool maybeVisitAtomicWait (Expression*& out, uint8_t code);
1699
- bool maybeVisitAtomicNotify (Expression*& out, uint8_t code);
1700
- bool maybeVisitAtomicFence (Expression*& out, uint8_t code);
1701
- bool maybeVisitConst (Expression*& out, uint8_t code);
1702
- bool maybeVisitUnary (Expression*& out, uint8_t code);
1703
- bool maybeVisitBinary (Expression*& out, uint8_t code);
1704
- bool maybeVisitTruncSat (Expression*& out, uint32_t code);
1705
- bool maybeVisitSIMDBinary (Expression*& out, uint32_t code);
1706
- bool maybeVisitSIMDUnary (Expression*& out, uint32_t code);
1707
- bool maybeVisitSIMDConst (Expression*& out, uint32_t code);
1708
- bool maybeVisitSIMDStore (Expression*& out, uint32_t code);
1709
- bool maybeVisitSIMDExtract (Expression*& out, uint32_t code);
1710
- bool maybeVisitSIMDReplace (Expression*& out, uint32_t code);
1711
- bool maybeVisitSIMDShuffle (Expression*& out, uint32_t code);
1712
- bool maybeVisitSIMDTernary (Expression*& out, uint32_t code);
1713
- bool maybeVisitSIMDShift (Expression*& out, uint32_t code);
1714
- bool maybeVisitSIMDLoad (Expression*& out, uint32_t code);
1715
- bool maybeVisitSIMDLoadStoreLane (Expression*& out, uint32_t code);
1716
- bool maybeVisitMemoryInit (Expression*& out, uint32_t code);
1717
- bool maybeVisitDataDrop (Expression*& out, uint32_t code);
1718
- bool maybeVisitMemoryCopy (Expression*& out, uint32_t code);
1719
- bool maybeVisitMemoryFill (Expression*& out, uint32_t code);
1720
- bool maybeVisitTableSize (Expression*& out, uint32_t code);
1721
- bool maybeVisitTableGrow (Expression*& out, uint32_t code);
1722
- bool maybeVisitTableFill (Expression*& out, uint32_t code);
1723
- bool maybeVisitTableCopy (Expression*& out, uint32_t code);
1724
- bool maybeVisitTableInit (Expression*& out, uint32_t code);
1725
- bool maybeVisitRefI31 (Expression*& out, uint32_t code);
1726
- bool maybeVisitI31Get (Expression*& out, uint32_t code);
1727
- bool maybeVisitRefTest (Expression*& out, uint32_t code);
1728
- bool maybeVisitRefCast (Expression*& out, uint32_t code);
1729
- bool maybeVisitBrOn (Expression*& out, uint32_t code);
1730
- bool maybeVisitStructNew (Expression*& out, uint32_t code);
1731
- bool maybeVisitStructGet (Expression*& out, uint32_t code);
1732
- bool maybeVisitStructSet (Expression*& out, uint32_t code);
1733
- bool maybeVisitArrayNewData (Expression*& out, uint32_t code);
1734
- bool maybeVisitArrayNewElem (Expression*& out, uint32_t code);
1735
- bool maybeVisitArrayNewFixed (Expression*& out, uint32_t code);
1736
- bool maybeVisitArrayGet (Expression*& out, uint32_t code);
1737
- bool maybeVisitArraySet (Expression*& out, uint32_t code);
1738
- bool maybeVisitArrayLen (Expression*& out, uint32_t code);
1739
- bool maybeVisitArrayCopy (Expression*& out, uint32_t code);
1740
- bool maybeVisitArrayFill (Expression*& out, uint32_t code);
1741
- bool maybeVisitArrayInit (Expression*& out, uint32_t code);
1742
- bool maybeVisitStringNew (Expression*& out, uint32_t code);
1743
- bool maybeVisitStringAsWTF16 (Expression*& out, uint32_t code);
1744
- bool maybeVisitStringConst (Expression*& out, uint32_t code);
1745
- bool maybeVisitStringMeasure (Expression*& out, uint32_t code);
1746
- bool maybeVisitStringEncode (Expression*& out, uint32_t code);
1747
- bool maybeVisitStringConcat (Expression*& out, uint32_t code);
1748
- bool maybeVisitStringEq (Expression*& out, uint32_t code);
1749
- bool maybeVisitStringWTF16Get (Expression*& out, uint32_t code);
1750
- bool maybeVisitStringSliceWTF (Expression*& out, uint32_t code);
1751
- void visitSelect (Select* curr, uint8_t code);
1752
- void visitReturn (Return* curr);
1753
- void visitMemorySize (MemorySize* curr);
1754
- void visitMemoryGrow (MemoryGrow* curr);
1755
- void visitNop (Nop* curr);
1756
- void visitUnreachable (Unreachable* curr);
1757
- void visitDrop (Drop* curr);
1758
- void visitRefNull (RefNull* curr);
1759
- void visitRefIsNull (RefIsNull* curr);
1760
- void visitRefFunc (RefFunc* curr);
1761
- void visitRefEq (RefEq* curr);
1762
- void visitTableGet (TableGet* curr);
1763
- void visitTableSet (TableSet* curr);
1764
- void visitTryOrTryInBlock (Expression*& out);
1765
- void visitTryTable (TryTable* curr);
1766
- void visitThrow (Throw* curr);
1767
- void visitRethrow (Rethrow* curr);
1768
- void visitThrowRef (ThrowRef* curr);
1769
- void visitCallRef (CallRef* curr);
1770
- void visitRefAsCast (RefCast* curr, uint32_t code);
1771
- void visitRefAs (RefAs* curr, uint8_t code);
1772
- void visitContNew (ContNew* curr);
1773
- void visitContBind (ContBind* curr);
1774
- void visitResume (Resume* curr);
1775
- void visitSuspend (Suspend* curr);
1776
-
1777
- [[noreturn]] void throwError (std::string text);
1778
-
1779
- // Struct/Array instructions have an unnecessary heap type that is just for
1780
- // validation (except for the case of unreachability, but that's not a problem
1781
- // anyhow, we can ignore it there). That is, we also have a reference typed
1782
- // child from which we can infer the type anyhow, and we just need to check
1783
- // that type is the same.
1784
- void validateHeapTypeUsingChild (Expression* child, HeapType heapType);
1610
+ [[noreturn]] void throwError (std::string text) {
1611
+ throw ParseException (text, 0 , pos);
1612
+ }
1785
1613
1786
1614
private:
1787
1615
bool hasDWARFSections ();
0 commit comments