Load instruction sequence. See "include/loader/loader.h".
| 124 | |
| 125 | // Load instruction sequence. See "include/loader/loader.h". |
| 126 | Expect<AST::InstrVec> Loader::loadInstrSeq(std::optional<uint64_t> SizeBound) { |
| 127 | AST::InstrVec Instrs; |
| 128 | std::vector<std::pair<OpCode, uint32_t>> BlockStack; |
| 129 | uint32_t Cnt = 0; |
| 130 | bool IsReachEnd = false; |
| 131 | // Read opcode until the End code of the top block. |
| 132 | do { |
| 133 | // Read the opcode and check for errors. |
| 134 | uint64_t Offset = FMgr.getOffset(); |
| 135 | EXPECTED_TRY(OpCode Code, loadOpCode().map_error([this](auto E) { |
| 136 | return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Instruction); |
| 137 | })); |
| 138 | |
| 139 | // Check with proposals. |
| 140 | if (auto Res = Conf.isInstrNeedProposal(Code); unlikely(!!Res)) { |
| 141 | return logNeedProposal(ErrCode::Value::IllegalOpCode, Res.value(), Offset, |
| 142 | ASTNodeAttr::Instruction); |
| 143 | } |
| 144 | |
| 145 | auto logIllegalOpCode = [this, &Offset, |
| 146 | &SizeBound]() -> Unexpected<ErrCode> { |
| 147 | if (SizeBound.has_value() && FMgr.getOffset() > SizeBound.value()) { |
| 148 | return logLoadError(ErrCode::Value::ENDCodeExpected, Offset, |
| 149 | ASTNodeAttr::Instruction); |
| 150 | } else { |
| 151 | return logLoadError(ErrCode::Value::IllegalOpCode, Offset, |
| 152 | ASTNodeAttr::Instruction); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | // Process the instruction that contains a block. |
| 157 | switch (Code) { |
| 158 | case OpCode::Block: |
| 159 | case OpCode::Loop: |
| 160 | case OpCode::If: |
| 161 | case OpCode::Try_table: |
| 162 | BlockStack.emplace_back(Code, Cnt); |
| 163 | break; |
| 164 | case OpCode::Else: { |
| 165 | if (BlockStack.size() == 0 || BlockStack.back().first != OpCode::If) { |
| 166 | // An Else instruction appeared outside the If-block. |
| 167 | return logIllegalOpCode(); |
| 168 | } |
| 169 | uint32_t Pos = BlockStack.back().second; |
| 170 | if (Instrs[Pos].getJumpElse() > 0) { |
| 171 | // An Else instruction appeared before in this If-block. |
| 172 | return logIllegalOpCode(); |
| 173 | } |
| 174 | Instrs[Pos].setJumpElse(Cnt - Pos); |
| 175 | break; |
| 176 | } |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | // Create the instruction node and load contents. |
| 182 | Instrs.emplace_back(Code, static_cast<uint32_t>(Offset)); |
| 183 | EXPECTED_TRY(loadInstruction(Instrs.back())); |
nothing calls this directly
no test coverage detected