OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_ASMJS_ASM_PARSER_H_ |
| 6 #define V8_ASMJS_ASM_PARSER_H_ |
| 7 |
| 8 #include <list> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "src/asmjs/asm-scanner.h" |
| 13 #include "src/asmjs/asm-typer.h" |
| 14 #include "src/asmjs/asm-types.h" |
| 15 #include "src/wasm/signature-map.h" |
| 16 #include "src/wasm/wasm-module-builder.h" |
| 17 |
| 18 namespace v8 { |
| 19 namespace internal { |
| 20 namespace wasm { |
| 21 |
| 22 class AsmJsParser { |
| 23 public: |
| 24 explicit AsmJsParser(Isolate* isolate, Zone* zone, Handle<Script> script, |
| 25 int start, int end); |
| 26 bool Run(); |
| 27 const char* failure_message() const { return failure_message_.c_str(); } |
| 28 int failure_location() const { return failure_location_; } |
| 29 WasmModuleBuilder* builder() { return builder_; } |
| 30 const AsmTyper::StdlibSet* stdlib_uses() const { return &stdlib_uses_; } |
| 31 |
| 32 private: |
| 33 Zone* zone_; |
| 34 Zone* zone() { return zone_; } |
| 35 |
| 36 AsmJsScanner scanner_; |
| 37 WasmModuleBuilder* builder_; |
| 38 WasmFunctionBuilder* current_function_builder_; |
| 39 AsmType* return_type_; |
| 40 std::uintptr_t stack_limit_; |
| 41 AsmTyper::StdlibSet stdlib_uses_; |
| 42 |
| 43 // Error Handling. |
| 44 bool failed_; |
| 45 std::string failure_message_; |
| 46 int failure_location_; |
| 47 |
| 48 // clang-format off |
| 49 enum class VarKind { |
| 50 kUnused, |
| 51 kLocal, |
| 52 kGlobal, |
| 53 kSpecial, |
| 54 kFunction, |
| 55 kTable, |
| 56 kImportedFunction, |
| 57 #define V(_unused0, Name, _unused1, _unused2) kMath##Name, |
| 58 STDLIB_MATH_FUNCTION_LIST(V) |
| 59 #undef V |
| 60 #define V(Name) kMath##Name, |
| 61 STDLIB_MATH_VALUE_LIST(V) |
| 62 #undef V |
| 63 }; |
| 64 // clang-format on |
| 65 |
| 66 struct FunctionImportInfo { |
| 67 std::string name; |
| 68 SignatureMap cache; |
| 69 std::vector<uint32_t> cache_index; |
| 70 }; |
| 71 std::list<FunctionImportInfo> function_import_info_; |
| 72 |
| 73 struct VarInfo { |
| 74 AsmType* type; |
| 75 uint32_t index; |
| 76 uint64_t mask; |
| 77 VarKind kind; |
| 78 WasmFunctionBuilder* function_builder; |
| 79 bool mut; |
| 80 FunctionImportInfo* import; |
| 81 |
| 82 VarInfo(); |
| 83 void DeclareGlobalImport(AsmType* type, uint32_t index); |
| 84 void DeclareStdlibFunc(VarKind kind, AsmType* type); |
| 85 }; |
| 86 std::vector<VarInfo> global_var_info_; |
| 87 std::vector<VarInfo> local_var_info_; |
| 88 VarInfo* GetVarInfo(AsmJsScanner::token_t token); |
| 89 uint32_t VarIndex(VarInfo* info); |
| 90 void DeclareGlobal(VarInfo* info, bool mut, AsmType* type, ValueType vtype, |
| 91 const WasmInitExpr& init = WasmInitExpr()); |
| 92 |
| 93 // Manage temporaries. |
| 94 int function_temp_locals_offset_; |
| 95 int function_temp_locals_used_; |
| 96 int32_t TempVariable(int i); |
| 97 |
| 98 // Global imports. |
| 99 // NOTE: Holds the strings referenced in wasm-module-builder for imports. |
| 100 struct GlobalImport { |
| 101 std::string import_name; |
| 102 uint32_t import_index; |
| 103 uint32_t global_index; |
| 104 bool needs_init; |
| 105 }; |
| 106 std::list<GlobalImport> global_imports_; |
| 107 void AddGlobalImport(std::string name, AsmType* type, ValueType vtype, |
| 108 bool mut, VarInfo* info); |
| 109 |
| 110 // Module Related. |
| 111 AsmJsScanner::token_t stdlib_name_; |
| 112 AsmJsScanner::token_t foreign_name_; |
| 113 AsmJsScanner::token_t heap_name_; |
| 114 |
| 115 bool Peek(AsmJsScanner::token_t token) { return scanner_.Token() == token; } |
| 116 AsmJsScanner::token_t Consume() { |
| 117 AsmJsScanner::token_t ret = scanner_.Token(); |
| 118 scanner_.Next(); |
| 119 return ret; |
| 120 } |
| 121 void SkipSemicolon(); |
| 122 |
| 123 // Block stack. |
| 124 enum class BlockKind { kRegular, kLoop, kOther }; |
| 125 struct BlockInfo { |
| 126 BlockKind kind; |
| 127 AsmJsScanner::token_t label; |
| 128 }; |
| 129 std::vector<BlockInfo> block_stack_; |
| 130 void Begin(AsmJsScanner::token_t label = 0); |
| 131 void Loop(AsmJsScanner::token_t label = 0); |
| 132 void End(); |
| 133 void BareBegin(BlockKind kind = BlockKind::kOther, |
| 134 AsmJsScanner::token_t label = 0); |
| 135 void BareEnd(); |
| 136 int FindLabelDepth(AsmJsScanner::token_t label, bool loop); |
| 137 |
| 138 // Track if parsing a heap assignment. |
| 139 bool inside_heap_assignment_; |
| 140 int heap_assignment_location_; |
| 141 AsmType* heap_access_type_; |
| 142 |
| 143 AsmType* call_coercion_; |
| 144 |
| 145 AsmJsScanner::token_t pending_label_; |
| 146 |
| 147 // Types used for stdlib function and their set up. |
| 148 AsmType* stdlib_dq2d_; |
| 149 AsmType* stdlib_dqdq2d_; |
| 150 AsmType* stdlib_fq2f_; |
| 151 AsmType* stdlib_i2s_; |
| 152 AsmType* stdlib_ii2s_; |
| 153 AsmType* stdlib_minmax_; |
| 154 AsmType* stdlib_abs_; |
| 155 AsmType* stdlib_ceil_like_; |
| 156 AsmType* stdlib_fround_; |
| 157 void InitializeStdlibTypes(); |
| 158 |
| 159 FunctionSig* ConvertSignature(AsmType* return_type, |
| 160 const std::vector<AsmType*>& params); |
| 161 |
| 162 // 6.1 ValidateModule |
| 163 void ValidateModule(); |
| 164 void ValidateModuleParameters(); |
| 165 void ValidateModuleVars(); |
| 166 void ValidateModuleVar(bool mut); |
| 167 void ValidateModuleVarStdlib(VarInfo* info); |
| 168 |
| 169 void ValidateExport(); // 6.2 ValidateExport |
| 170 void ValidateFunctionTable(); // 6.3 ValidateFunctionTable |
| 171 void ValidateFunction(); // 6.4 ValidateFunction |
| 172 void ValidateStatement(); // ValidateStatement |
| 173 void Block(); // 6.5.1 Block |
| 174 void ExpressionStatement(); // 6.5.2 ExpressionStatement |
| 175 void EmptyStatement(); // 6.5.3 EmptyStatement |
| 176 void IfStatement(); // 6.5.4 IfStatement |
| 177 void ReturnStatement(); // 6.5.5 ReturnStatement |
| 178 bool IterationStatement(); // 6.5.6 IterationStatement |
| 179 void WhileStatement(); // 6.5.6 IterationStatement - while |
| 180 void DoStatement(); // 6.5.6 IterationStatement - do |
| 181 void ForStatement(); // 6.5.6 IterationStatement - for |
| 182 void BreakStatement(); // 6.5.7 BreakStatement |
| 183 void ContinueStatement(); // 6.5.8 ContinueStatement |
| 184 void LabelledStatement(); // 6.5.9 LabelledStatement |
| 185 void SwitchStatement(); // 6.5.10 SwitchStatement |
| 186 void ValidateCase(); // 6.6. ValidateCase |
| 187 void ValidateDefault(); // 6.7 ValidateDefault |
| 188 AsmType* ValidateExpression(); // 6.8 ValidateExpression |
| 189 AsmType* Expression(AsmType* expect); // 6.8.1 Expression |
| 190 AsmType* NumericLiteral(); // 6.8.2 NumericLiteral |
| 191 AsmType* Identifier(); // 6.8.3 Identifier |
| 192 AsmType* CallExpression(); // 6.8.4 CallExpression |
| 193 AsmType* MemberExpression(); // 6.8.5 MemberExpression |
| 194 AsmType* AssignmentExpression(); // 6.8.6 AssignmentExpression |
| 195 AsmType* UnaryExpression(); // 6.8.7 UnaryExpression |
| 196 AsmType* MultiplicativeExpression(); // 6.8.8 MultaplicativeExpression |
| 197 AsmType* AdditiveExpression(); // 6.8.9 AdditiveExpression |
| 198 AsmType* ShiftExpression(); // 6.8.10 ShiftExpression |
| 199 AsmType* RelationalExpression(); // 6.8.11 RelationalExpression |
| 200 AsmType* EqualityExpression(); // 6.8.12 EqualityExpression |
| 201 AsmType* BitwiseANDExpression(); // 6.8.13 BitwiseANDExpression |
| 202 AsmType* BitwiseXORExpression(); // 6.8.14 BitwiseXORExpression |
| 203 AsmType* BitwiseORExpression(); // 6.8.15 BitwiseORExpression |
| 204 AsmType* ConditionalExpression(); // 6.8.16 ConditionalExpression |
| 205 AsmType* ParenthesizedExpression(); // 6.8.17 ParenthesiedExpression |
| 206 AsmType* ValidateCall(); // 6.9 ValidateCall |
| 207 bool PeekCall(); // 6.9 ValidateCall - helper |
| 208 void ValidateHeapAccess(); // 6.10 ValidateHeapAccess |
| 209 void ValidateFloatCoercion(); // 6.11 ValidateFloatCoercion |
| 210 |
| 211 void GatherCases(std::vector<int32_t>* cases); |
| 212 }; |
| 213 |
| 214 } // namespace wasm |
| 215 } // namespace internal |
| 216 } // namespace v8 |
| 217 #endif |
OLD | NEW |