| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_WASM_AST_DECODER_H_ |
| 6 | #define V8_WASM_AST_DECODER_H_ |
| 7 | |
| 8 | #include "src/signature.h" |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9 | #include "src/wasm/decoder.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10 | #include "src/wasm/wasm-opcodes.h" |
| 11 | #include "src/wasm/wasm-result.h" |
| 12 | |
| 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16 | class BitVector; // forward declaration |
| 17 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18 | namespace compiler { // external declarations from compiler. |
| 19 | class WasmGraphBuilder; |
| 20 | } |
| 21 | |
| 22 | namespace wasm { |
| 23 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 24 | const uint32_t kMaxNumWasmLocals = 8000000; |
| 25 | struct WasmGlobal; |
| 26 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 27 | // Helpers for decoding different kinds of operands which follow bytecodes. |
| 28 | struct LocalIndexOperand { |
| 29 | uint32_t index; |
| 30 | LocalType type; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 31 | unsigned length; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 32 | |
| 33 | inline LocalIndexOperand(Decoder* decoder, const byte* pc) { |
| 34 | index = decoder->checked_read_u32v(pc, 1, &length, "local index"); |
| 35 | type = kAstStmt; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | struct ImmI8Operand { |
| 40 | int8_t value; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 41 | unsigned length; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 42 | inline ImmI8Operand(Decoder* decoder, const byte* pc) { |
| 43 | value = bit_cast<int8_t>(decoder->checked_read_u8(pc, 1, "immi8")); |
| 44 | length = 1; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | struct ImmI32Operand { |
| 49 | int32_t value; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 50 | unsigned length; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 51 | inline ImmI32Operand(Decoder* decoder, const byte* pc) { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 52 | value = decoder->checked_read_i32v(pc, 1, &length, "immi32"); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 53 | } |
| 54 | }; |
| 55 | |
| 56 | struct ImmI64Operand { |
| 57 | int64_t value; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 58 | unsigned length; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 59 | inline ImmI64Operand(Decoder* decoder, const byte* pc) { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 60 | value = decoder->checked_read_i64v(pc, 1, &length, "immi64"); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | |
| 64 | struct ImmF32Operand { |
| 65 | float value; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 66 | unsigned length; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 67 | inline ImmF32Operand(Decoder* decoder, const byte* pc) { |
| 68 | value = bit_cast<float>(decoder->checked_read_u32(pc, 1, "immf32")); |
| 69 | length = 4; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | struct ImmF64Operand { |
| 74 | double value; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 75 | unsigned length; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 76 | inline ImmF64Operand(Decoder* decoder, const byte* pc) { |
| 77 | value = bit_cast<double>(decoder->checked_read_u64(pc, 1, "immf64")); |
| 78 | length = 8; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | struct GlobalIndexOperand { |
| 83 | uint32_t index; |
| 84 | LocalType type; |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 85 | const WasmGlobal* global; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 86 | unsigned length; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 87 | |
| 88 | inline GlobalIndexOperand(Decoder* decoder, const byte* pc) { |
| 89 | index = decoder->checked_read_u32v(pc, 1, &length, "global index"); |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 90 | global = nullptr; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 91 | type = kAstStmt; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 92 | } |
| 93 | }; |
| 94 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 95 | struct BlockTypeOperand { |
| 96 | uint32_t arity; |
| 97 | const byte* types; // pointer to encoded types for the block. |
| 98 | unsigned length; |
| 99 | |
| 100 | inline BlockTypeOperand(Decoder* decoder, const byte* pc) { |
| 101 | uint8_t val = decoder->checked_read_u8(pc, 1, "block type"); |
| 102 | LocalType type = kAstStmt; |
| 103 | length = 1; |
| 104 | arity = 0; |
| 105 | types = nullptr; |
| 106 | if (decode_local_type(val, &type)) { |
| 107 | arity = type == kAstStmt ? 0 : 1; |
| 108 | types = pc + 1; |
| 109 | } else { |
| 110 | // Handle multi-value blocks. |
| 111 | if (!FLAG_wasm_mv_prototype) { |
| 112 | decoder->error(pc, pc + 1, "invalid block arity > 1"); |
| 113 | return; |
| 114 | } |
| 115 | if (val != kMultivalBlock) { |
| 116 | decoder->error(pc, pc + 1, "invalid block type"); |
| 117 | return; |
| 118 | } |
| 119 | // Decode and check the types vector of the block. |
| 120 | unsigned len = 0; |
| 121 | uint32_t count = decoder->checked_read_u32v(pc, 2, &len, "block arity"); |
| 122 | // {count} is encoded as {arity-2}, so that a {0} count here corresponds |
| 123 | // to a block with 2 values. This makes invalid/redundant encodings |
| 124 | // impossible. |
| 125 | arity = count + 2; |
| 126 | length = 1 + len + arity; |
| 127 | types = pc + 1 + 1 + len; |
| 128 | |
| 129 | for (uint32_t i = 0; i < arity; i++) { |
| 130 | uint32_t offset = 1 + 1 + len + i; |
| 131 | val = decoder->checked_read_u8(pc, offset, "block type"); |
| 132 | decode_local_type(val, &type); |
| 133 | if (type == kAstStmt) { |
| 134 | decoder->error(pc, pc + offset, "invalid block type"); |
| 135 | return; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | // Decode a byte representing a local type. Return {false} if the encoded |
| 141 | // byte was invalid or {kMultivalBlock}. |
| 142 | bool decode_local_type(uint8_t val, LocalType* result) { |
| 143 | switch (static_cast<LocalTypeCode>(val)) { |
| 144 | case kLocalVoid: |
| 145 | *result = kAstStmt; |
| 146 | return true; |
| 147 | case kLocalI32: |
| 148 | *result = kAstI32; |
| 149 | return true; |
| 150 | case kLocalI64: |
| 151 | *result = kAstI64; |
| 152 | return true; |
| 153 | case kLocalF32: |
| 154 | *result = kAstF32; |
| 155 | return true; |
| 156 | case kLocalF64: |
| 157 | *result = kAstF64; |
| 158 | return true; |
| 159 | default: |
| 160 | *result = kAstStmt; |
| 161 | return false; |
| 162 | } |
| 163 | } |
| 164 | LocalType read_entry(unsigned index) { |
| 165 | DCHECK_LT(index, arity); |
| 166 | LocalType result; |
| 167 | CHECK(decode_local_type(types[index], &result)); |
| 168 | return result; |
| 169 | } |
| 170 | }; |
| 171 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 172 | struct Control; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 173 | struct BreakDepthOperand { |
| 174 | uint32_t depth; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 175 | Control* target; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 176 | unsigned length; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 177 | inline BreakDepthOperand(Decoder* decoder, const byte* pc) { |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 178 | depth = decoder->checked_read_u32v(pc, 1, &length, "break depth"); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 179 | target = nullptr; |
| 180 | } |
| 181 | }; |
| 182 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 183 | struct CallIndirectOperand { |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 184 | uint32_t index; |
| 185 | FunctionSig* sig; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 186 | unsigned length; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 187 | inline CallIndirectOperand(Decoder* decoder, const byte* pc) { |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 188 | unsigned len1 = 0; |
| 189 | unsigned len2 = 0; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 190 | index = decoder->checked_read_u32v(pc, 1 + len1, &len2, "signature index"); |
| 191 | length = len1 + len2; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 192 | sig = nullptr; |
| 193 | } |
| 194 | }; |
| 195 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 196 | struct CallFunctionOperand { |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 197 | uint32_t index; |
| 198 | FunctionSig* sig; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 199 | unsigned length; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 200 | inline CallFunctionOperand(Decoder* decoder, const byte* pc) { |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 201 | unsigned len1 = 0; |
| 202 | unsigned len2 = 0; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 203 | index = decoder->checked_read_u32v(pc, 1 + len1, &len2, "function index"); |
| 204 | length = len1 + len2; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 205 | sig = nullptr; |
| 206 | } |
| 207 | }; |
| 208 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 209 | struct BranchTableOperand { |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 210 | uint32_t table_count; |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 211 | const byte* start; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 212 | const byte* table; |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 213 | inline BranchTableOperand(Decoder* decoder, const byte* pc) { |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 214 | DCHECK_EQ(kExprBrTable, decoder->checked_read_u8(pc, 0, "opcode")); |
| 215 | start = pc + 1; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 216 | unsigned len1 = 0; |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 217 | table_count = decoder->checked_read_u32v(pc, 1, &len1, "table count"); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 218 | if (table_count > (UINT_MAX / sizeof(uint32_t)) - 1 || |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 219 | len1 > UINT_MAX - (table_count + 1) * sizeof(uint32_t)) { |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 220 | decoder->error(pc, "branch table size overflow"); |
| 221 | } |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 222 | table = pc + 1 + len1; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 223 | } |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 224 | inline uint32_t read_entry(Decoder* decoder, unsigned i) { |
| 225 | DCHECK(i <= table_count); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 226 | return table ? decoder->read_u32(table + i * sizeof(uint32_t)) : 0; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 230 | // A helper to iterate over a branch table. |
| 231 | class BranchTableIterator { |
| 232 | public: |
| 233 | unsigned cur_index() { return index_; } |
| 234 | bool has_next() { return index_ <= table_count_; } |
| 235 | uint32_t next() { |
| 236 | DCHECK(has_next()); |
| 237 | index_++; |
| 238 | unsigned length = 0; |
| 239 | uint32_t result = |
| 240 | decoder_->checked_read_u32v(pc_, 0, &length, "branch table entry"); |
| 241 | pc_ += length; |
| 242 | return result; |
| 243 | } |
| 244 | // length, including the length of the {BranchTableOperand}, but not the |
| 245 | // opcode. |
| 246 | unsigned length() { |
| 247 | while (has_next()) next(); |
| 248 | return static_cast<unsigned>(pc_ - start_); |
| 249 | } |
| 250 | const byte* pc() { return pc_; } |
| 251 | |
| 252 | BranchTableIterator(Decoder* decoder, BranchTableOperand& operand) |
| 253 | : decoder_(decoder), |
| 254 | start_(operand.start), |
| 255 | pc_(operand.table), |
| 256 | index_(0), |
| 257 | table_count_(operand.table_count) {} |
| 258 | |
| 259 | private: |
| 260 | Decoder* decoder_; |
| 261 | const byte* start_; |
| 262 | const byte* pc_; |
| 263 | uint32_t index_; // the current index. |
| 264 | uint32_t table_count_; // the count of entries, not including default. |
| 265 | }; |
| 266 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 267 | struct MemoryAccessOperand { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 268 | uint32_t alignment; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 269 | uint32_t offset; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 270 | unsigned length; |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 271 | inline MemoryAccessOperand(Decoder* decoder, const byte* pc, |
| 272 | uint32_t max_alignment) { |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 273 | unsigned alignment_length; |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 274 | alignment = |
| 275 | decoder->checked_read_u32v(pc, 1, &alignment_length, "alignment"); |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 276 | if (max_alignment < alignment) { |
| 277 | decoder->error(pc, pc + 1, |
| 278 | "invalid alignment; expected maximum alignment is %u, " |
| 279 | "actual alignment is %u", |
| 280 | max_alignment, alignment); |
| 281 | } |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 282 | unsigned offset_length; |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 283 | offset = decoder->checked_read_u32v(pc, 1 + alignment_length, |
| 284 | &offset_length, "offset"); |
| 285 | length = alignment_length + offset_length; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 286 | } |
| 287 | }; |
| 288 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 289 | typedef compiler::WasmGraphBuilder TFBuilder; |
| 290 | struct ModuleEnv; // forward declaration of module interface. |
| 291 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 292 | // All of the various data structures necessary to decode a function body. |
| 293 | struct FunctionBody { |
| 294 | ModuleEnv* module; // module environment |
| 295 | FunctionSig* sig; // function signature |
| 296 | const byte* base; // base of the module bytes, for error reporting |
| 297 | const byte* start; // start of the function body |
| 298 | const byte* end; // end of the function body |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 301 | static inline FunctionBody FunctionBodyForTesting(const byte* start, |
| 302 | const byte* end) { |
| 303 | return {nullptr, nullptr, start, start, end}; |
| 304 | } |
| 305 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 306 | struct DecodeStruct { |
| 307 | int unused; |
| 308 | }; |
| 309 | typedef Result<DecodeStruct*> DecodeResult; |
| 310 | inline std::ostream& operator<<(std::ostream& os, const DecodeStruct& tree) { |
| 311 | return os; |
| 312 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 313 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 314 | V8_EXPORT_PRIVATE DecodeResult VerifyWasmCode(AccountingAllocator* allocator, |
| 315 | FunctionBody& body); |
| 316 | DecodeResult BuildTFGraph(AccountingAllocator* allocator, TFBuilder* builder, |
| 317 | FunctionBody& body); |
| 318 | bool PrintAst(AccountingAllocator* allocator, const FunctionBody& body, |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 319 | std::ostream& os, |
| 320 | std::vector<std::tuple<uint32_t, int, int>>* offset_table); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 321 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 322 | // A simplified form of AST printing, e.g. from a debugger. |
| 323 | void PrintAstForDebugging(const byte* start, const byte* end); |
| 324 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 325 | inline DecodeResult VerifyWasmCode(AccountingAllocator* allocator, |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 326 | ModuleEnv* module, FunctionSig* sig, |
| 327 | const byte* start, const byte* end) { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 328 | FunctionBody body = {module, sig, nullptr, start, end}; |
| 329 | return VerifyWasmCode(allocator, body); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 332 | inline DecodeResult BuildTFGraph(AccountingAllocator* allocator, |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 333 | TFBuilder* builder, ModuleEnv* module, |
| 334 | FunctionSig* sig, const byte* start, |
| 335 | const byte* end) { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 336 | FunctionBody body = {module, sig, nullptr, start, end}; |
| 337 | return BuildTFGraph(allocator, builder, body); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 340 | struct AstLocalDecls { |
| 341 | // The size of the encoded declarations. |
| 342 | uint32_t decls_encoded_size; // size of encoded declarations |
| 343 | |
| 344 | // Total number of locals. |
| 345 | uint32_t total_local_count; |
| 346 | |
| 347 | // List of {local type, count} pairs. |
| 348 | ZoneVector<std::pair<LocalType, uint32_t>> local_types; |
| 349 | |
| 350 | // Constructor initializes the vector. |
| 351 | explicit AstLocalDecls(Zone* zone) |
| 352 | : decls_encoded_size(0), total_local_count(0), local_types(zone) {} |
| 353 | }; |
| 354 | |
| 355 | bool DecodeLocalDecls(AstLocalDecls& decls, const byte* start, const byte* end); |
| 356 | BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 357 | const byte* start, const byte* end); |
| 358 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 359 | // Computes the length of the opcode at the given address. |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 360 | unsigned OpcodeLength(const byte* pc, const byte* end); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 361 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 362 | // A simple forward iterator for bytecodes. |
| 363 | class BytecodeIterator : public Decoder { |
| 364 | public: |
| 365 | // If one wants to iterate over the bytecode without looking at {pc_offset()}. |
| 366 | class iterator { |
| 367 | public: |
| 368 | inline iterator& operator++() { |
| 369 | DCHECK_LT(ptr_, end_); |
| 370 | ptr_ += OpcodeLength(ptr_, end_); |
| 371 | return *this; |
| 372 | } |
| 373 | inline WasmOpcode operator*() { |
| 374 | DCHECK_LT(ptr_, end_); |
| 375 | return static_cast<WasmOpcode>(*ptr_); |
| 376 | } |
| 377 | inline bool operator==(const iterator& that) { |
| 378 | return this->ptr_ == that.ptr_; |
| 379 | } |
| 380 | inline bool operator!=(const iterator& that) { |
| 381 | return this->ptr_ != that.ptr_; |
| 382 | } |
| 383 | |
| 384 | private: |
| 385 | friend class BytecodeIterator; |
| 386 | const byte* ptr_; |
| 387 | const byte* end_; |
| 388 | iterator(const byte* ptr, const byte* end) : ptr_(ptr), end_(end) {} |
| 389 | }; |
| 390 | |
| 391 | // Create a new {BytecodeIterator}. If the {decls} pointer is non-null, |
| 392 | // assume the bytecode starts with local declarations and decode them. |
| 393 | // Otherwise, do not decode local decls. |
| 394 | BytecodeIterator(const byte* start, const byte* end, |
| 395 | AstLocalDecls* decls = nullptr); |
| 396 | |
| 397 | inline iterator begin() const { return iterator(pc_, end_); } |
| 398 | inline iterator end() const { return iterator(end_, end_); } |
| 399 | |
| 400 | WasmOpcode current() { |
| 401 | return static_cast<WasmOpcode>( |
| 402 | checked_read_u8(pc_, 0, "expected bytecode")); |
| 403 | } |
| 404 | |
| 405 | void next() { |
| 406 | if (pc_ < end_) { |
| 407 | pc_ += OpcodeLength(pc_, end_); |
| 408 | if (pc_ >= end_) pc_ = end_; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | bool has_next() { return pc_ < end_; } |
| 413 | }; |
| 414 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 415 | } // namespace wasm |
| 416 | } // namespace internal |
| 417 | } // namespace v8 |
| 418 | |
| 419 | #endif // V8_WASM_AST_DECODER_H_ |