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" 9 #include "src/wasm/wasm-opcodes.h" 10 #include "src/wasm/wasm-result.h" 11 12 namespace v8 { 13 namespace internal { 14 15 namespace compiler { // external declarations from compiler. 16 class WasmGraphBuilder; 17 } 18 19 namespace wasm { 20 21 typedef compiler::WasmGraphBuilder TFBuilder; 22 struct ModuleEnv; // forward declaration of module interface. 23 24 // Interface the function environment during decoding, include the signature 25 // and number of locals. 26 struct FunctionEnv { 27 ModuleEnv* module; // module environment 28 FunctionSig* sig; // signature of this function 29 uint32_t local_int32_count; // number of int32 locals 30 uint32_t local_int64_count; // number of int64 locals 31 uint32_t local_float32_count; // number of float32 locals 32 uint32_t local_float64_count; // number of float64 locals 33 uint32_t total_locals; // sum of parameters and all locals 34 35 bool IsValidLocal(uint32_t index) { return index < total_locals; } 36 uint32_t GetLocalCount() { return total_locals; } 37 LocalType GetLocalType(uint32_t index) { 38 if (index < static_cast<uint32_t>(sig->parameter_count())) { 39 return sig->GetParam(index); 40 } 41 index -= static_cast<uint32_t>(sig->parameter_count()); 42 if (index < local_int32_count) return kAstI32; 43 index -= local_int32_count; 44 if (index < local_int64_count) return kAstI64; 45 index -= local_int64_count; 46 if (index < local_float32_count) return kAstF32; 47 index -= local_float32_count; 48 if (index < local_float64_count) return kAstF64; 49 return kAstStmt; 50 } 51 52 void AddLocals(LocalType type, uint32_t count) { 53 switch (type) { 54 case kAstI32: 55 local_int32_count += count; 56 break; 57 case kAstI64: 58 local_int64_count += count; 59 break; 60 case kAstF32: 61 local_float32_count += count; 62 break; 63 case kAstF64: 64 local_float64_count += count; 65 break; 66 default: 67 UNREACHABLE(); 68 } 69 total_locals += count; 70 DCHECK(total_locals == 71 (sig->parameter_count() + local_int32_count + local_int64_count + 72 local_float32_count + local_float64_count)); 73 } 74 75 void SumLocals() { 76 total_locals = static_cast<uint32_t>(sig->parameter_count()) + 77 local_int32_count + local_int64_count + local_float32_count + 78 local_float64_count; 79 } 80 }; 81 82 struct Tree; 83 typedef Result<Tree*> TreeResult; 84 85 std::ostream& operator<<(std::ostream& os, const Tree& tree); 86 87 TreeResult VerifyWasmCode(FunctionEnv* env, const byte* base, const byte* start, 88 const byte* end); 89 TreeResult BuildTFGraph(TFBuilder* builder, FunctionEnv* env, const byte* base, 90 const byte* start, const byte* end); 91 92 inline TreeResult VerifyWasmCode(FunctionEnv* env, const byte* start, 93 const byte* end) { 94 return VerifyWasmCode(env, nullptr, start, end); 95 } 96 97 inline TreeResult BuildTFGraph(TFBuilder* builder, FunctionEnv* env, 98 const byte* start, const byte* end) { 99 return BuildTFGraph(builder, env, nullptr, start, end); 100 } 101 102 enum ReadUnsignedLEB128ErrorCode { kNoError, kInvalidLEB128, kMissingLEB128 }; 103 104 ReadUnsignedLEB128ErrorCode ReadUnsignedLEB128Operand(const byte*, const byte*, 105 int*, uint32_t*); 106 107 // Computes the length of the opcode at the given address. 108 int OpcodeLength(const byte* pc); 109 110 // Computes the arity (number of sub-nodes) of the opcode at the given address. 111 int OpcodeArity(FunctionEnv* env, const byte* pc); 112 } // namespace wasm 113 } // namespace internal 114 } // namespace v8 115 116 #endif // V8_WASM_AST_DECODER_H_ 117