| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_WASM_AST_DECODER_H_ | 5 #ifndef V8_WASM_AST_DECODER_H_ |
| 6 #define V8_WASM_AST_DECODER_H_ | 6 #define V8_WASM_AST_DECODER_H_ |
| 7 | 7 |
| 8 #include "src/signature.h" | 8 #include "src/signature.h" |
| 9 #include "src/wasm/decoder.h" | 9 #include "src/wasm/decoder.h" |
| 10 #include "src/wasm/wasm-opcodes.h" | 10 #include "src/wasm/wasm-opcodes.h" |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 } else { | 177 } else { |
| 178 offset = 0; | 178 offset = 0; |
| 179 length = 1; | 179 length = 1; |
| 180 } | 180 } |
| 181 } | 181 } |
| 182 }; | 182 }; |
| 183 | 183 |
| 184 typedef compiler::WasmGraphBuilder TFBuilder; | 184 typedef compiler::WasmGraphBuilder TFBuilder; |
| 185 struct ModuleEnv; // forward declaration of module interface. | 185 struct ModuleEnv; // forward declaration of module interface. |
| 186 | 186 |
| 187 // Interface the function environment during decoding, include the signature | 187 // All of the various data structures necessary to decode a function body. |
| 188 // and number of locals. | 188 struct FunctionBody { |
| 189 struct FunctionEnv { | 189 ModuleEnv* module; // module environment |
| 190 ModuleEnv* module; // module environment | 190 FunctionSig* sig; // function signature |
| 191 FunctionSig* sig; // signature of this function | 191 const byte* base; // base of the module bytes, for error reporting |
| 192 uint32_t local_i32_count; // number of int32 locals | 192 const byte* start; // start of the function body |
| 193 uint32_t local_i64_count; // number of int64 locals | 193 const byte* end; // end of the function body |
| 194 uint32_t local_f32_count; // number of float32 locals | |
| 195 uint32_t local_f64_count; // number of float64 locals | |
| 196 uint32_t total_locals; // sum of parameters and all locals | |
| 197 | |
| 198 uint32_t GetLocalCount() { return total_locals; } | |
| 199 LocalType GetLocalType(uint32_t index) { | |
| 200 if (index < static_cast<uint32_t>(sig->parameter_count())) { | |
| 201 return sig->GetParam(index); | |
| 202 } | |
| 203 index -= static_cast<uint32_t>(sig->parameter_count()); | |
| 204 if (index < local_i32_count) return kAstI32; | |
| 205 index -= local_i32_count; | |
| 206 if (index < local_i64_count) return kAstI64; | |
| 207 index -= local_i64_count; | |
| 208 if (index < local_f32_count) return kAstF32; | |
| 209 index -= local_f32_count; | |
| 210 if (index < local_f64_count) return kAstF64; | |
| 211 return kAstStmt; | |
| 212 } | |
| 213 | |
| 214 void AddLocals(LocalType type, uint32_t count) { | |
| 215 switch (type) { | |
| 216 case kAstI32: | |
| 217 local_i32_count += count; | |
| 218 break; | |
| 219 case kAstI64: | |
| 220 local_i64_count += count; | |
| 221 break; | |
| 222 case kAstF32: | |
| 223 local_f32_count += count; | |
| 224 break; | |
| 225 case kAstF64: | |
| 226 local_f64_count += count; | |
| 227 break; | |
| 228 default: | |
| 229 UNREACHABLE(); | |
| 230 } | |
| 231 total_locals += count; | |
| 232 DCHECK_EQ(total_locals, | |
| 233 (sig->parameter_count() + local_i32_count + local_i64_count + | |
| 234 local_f32_count + local_f64_count)); | |
| 235 } | |
| 236 | |
| 237 void SumLocals() { | |
| 238 total_locals = static_cast<uint32_t>(sig->parameter_count()) + | |
| 239 local_i32_count + local_i64_count + local_f32_count + | |
| 240 local_f64_count; | |
| 241 } | |
| 242 }; | 194 }; |
| 243 | 195 |
| 244 struct Tree; | 196 struct Tree; |
| 245 typedef Result<Tree*> TreeResult; | 197 typedef Result<Tree*> TreeResult; |
| 246 | 198 |
| 247 std::ostream& operator<<(std::ostream& os, const Tree& tree); | 199 std::ostream& operator<<(std::ostream& os, const Tree& tree); |
| 248 | 200 |
| 249 TreeResult VerifyWasmCode(FunctionEnv* env, const byte* base, const byte* start, | 201 TreeResult VerifyWasmCode(FunctionBody& body); |
| 250 const byte* end); | 202 TreeResult BuildTFGraph(TFBuilder* builder, FunctionBody& body); |
| 251 TreeResult BuildTFGraph(TFBuilder* builder, FunctionEnv* env, const byte* base, | 203 void PrintAst(FunctionBody& body); |
| 252 const byte* start, const byte* end); | |
| 253 | 204 |
| 254 void PrintAst(FunctionEnv* env, const byte* start, const byte* end); | 205 inline TreeResult VerifyWasmCode(ModuleEnv* module, FunctionSig* sig, |
| 255 | 206 const byte* start, const byte* end) { |
| 256 inline TreeResult VerifyWasmCode(FunctionEnv* env, const byte* start, | 207 FunctionBody body = {module, sig, nullptr, start, end}; |
| 257 const byte* end) { | 208 return VerifyWasmCode(body); |
| 258 return VerifyWasmCode(env, nullptr, start, end); | |
| 259 } | 209 } |
| 260 | 210 |
| 261 inline TreeResult BuildTFGraph(TFBuilder* builder, FunctionEnv* env, | 211 inline TreeResult BuildTFGraph(TFBuilder* builder, ModuleEnv* module, |
| 262 const byte* start, const byte* end) { | 212 FunctionSig* sig, const byte* start, |
| 263 return BuildTFGraph(builder, env, nullptr, start, end); | 213 const byte* end) { |
| 214 FunctionBody body = {module, sig, nullptr, start, end}; |
| 215 return BuildTFGraph(builder, body); |
| 264 } | 216 } |
| 265 | 217 |
| 266 enum ReadUnsignedLEB128ErrorCode { kNoError, kInvalidLEB128, kMissingLEB128 }; | 218 enum ReadUnsignedLEB128ErrorCode { kNoError, kInvalidLEB128, kMissingLEB128 }; |
| 267 | 219 |
| 268 ReadUnsignedLEB128ErrorCode ReadUnsignedLEB128Operand(const byte*, const byte*, | 220 ReadUnsignedLEB128ErrorCode ReadUnsignedLEB128Operand(const byte*, const byte*, |
| 269 int*, uint32_t*); | 221 int*, uint32_t*); |
| 270 | 222 |
| 271 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, FunctionEnv* env, | 223 std::vector<LocalType>* DecodeLocalDeclsForTesting(const byte* start, |
| 224 const byte* end); |
| 225 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, |
| 272 const byte* start, const byte* end); | 226 const byte* start, const byte* end); |
| 273 | 227 |
| 274 // Computes the length of the opcode at the given address. | 228 // Computes the length of the opcode at the given address. |
| 275 int OpcodeLength(const byte* pc, const byte* end); | 229 int OpcodeLength(const byte* pc, const byte* end); |
| 276 | 230 |
| 277 // Computes the arity (number of sub-nodes) of the opcode at the given address. | 231 // Computes the arity (number of sub-nodes) of the opcode at the given address. |
| 278 int OpcodeArity(FunctionEnv* env, const byte* pc, const byte* end); | 232 int OpcodeArity(ModuleEnv* module, FunctionSig* sig, const byte* pc, |
| 233 const byte* end); |
| 279 } // namespace wasm | 234 } // namespace wasm |
| 280 } // namespace internal | 235 } // namespace internal |
| 281 } // namespace v8 | 236 } // namespace v8 |
| 282 | 237 |
| 283 #endif // V8_WASM_AST_DECODER_H_ | 238 #endif // V8_WASM_AST_DECODER_H_ |
| OLD | NEW |