OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 SRC_WASM_ASM_TYPER_H_ | |
6 #define SRC_WASM_ASM_TYPER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "src/allocation.h" | |
11 #include "src/ast/ast-type-bounds.h" | |
12 #include "src/ast/ast.h" | |
13 #include "src/effects.h" | |
14 #include "src/type-info.h" | |
15 #include "src/types.h" | |
16 #include "src/wasm/asm-types.h" | |
17 #include "src/zone.h" | |
18 | |
19 namespace v8 { | |
20 namespace internal { | |
21 namespace wasm { | |
22 | |
23 class AsmTyperHarnessBuilder; | |
24 | |
25 class AsmTyper final { | |
26 public: | |
27 enum StandardMember { | |
28 kHeap = -4, | |
29 kFFI = -3, | |
30 kStdlib = -2, | |
31 kModule = -1, | |
32 kNone = 0, | |
33 kInfinity, | |
34 kNaN, | |
35 kMathAcos, | |
36 kMathAsin, | |
37 kMathAtan, | |
38 kMathCos, | |
39 kMathSin, | |
40 kMathTan, | |
41 kMathExp, | |
42 kMathLog, | |
43 kMathCeil, | |
44 kMathFloor, | |
45 kMathSqrt, | |
46 kMathAbs, | |
47 kMathMin, | |
48 kMathMax, | |
49 kMathAtan2, | |
50 kMathPow, | |
51 kMathImul, | |
52 kMathFround, | |
53 kMathE, | |
54 kMathLN10, | |
55 kMathLN2, | |
56 kMathLOG2E, | |
57 kMathLOG10E, | |
58 kMathPI, | |
59 kMathSQRT1_2, | |
60 kMathSQRT2, | |
61 }; | |
62 | |
63 ~AsmTyper() = default; | |
64 AsmTyper(Isolate* isolate, Zone* zone, Script* script, FunctionLiteral* root); | |
65 | |
66 bool Validate(); | |
67 | |
68 const char* error_message() const { return error_message_; } | |
69 | |
70 private: | |
71 friend class v8::internal::wasm::AsmTyperHarnessBuilder; | |
72 | |
73 class VariableInfo : public ZoneObject { | |
74 public: | |
75 enum Mutability { | |
76 kInvalidMutability, | |
77 kLocal, | |
78 kMutableGlobal, | |
79 kImmutableGlobal, | |
80 }; | |
81 | |
82 VariableInfo() = default; | |
83 explicit VariableInfo(AsmType* t) : type_(t) {} | |
84 | |
85 VariableInfo* Clone(Zone* zone) const; | |
86 | |
87 bool IsMutable() const { | |
88 return mutability_ == kLocal || mutability_ == kMutableGlobal; | |
89 } | |
90 | |
91 bool IsGlobal() const { | |
92 return mutability_ == kImmutableGlobal || mutability_ == kMutableGlobal; | |
93 } | |
94 | |
95 bool IsStdlib() const { return standard_member_ == kStdlib; } | |
96 bool IsFFI() const { return standard_member_ == kFFI; } | |
97 bool IsHeap() const { return standard_member_ == kHeap; } | |
98 | |
99 void MarkDefined() { missing_definition_ = false; } | |
100 void FirstForwardUseIs(VariableProxy* var); | |
101 | |
102 StandardMember standard_member() const { return standard_member_; } | |
103 void set_standard_member(StandardMember standard_member) { | |
104 standard_member_ = standard_member; | |
105 } | |
106 | |
107 AsmType* type() const { return type_; } | |
108 void set_type(AsmType* type) { type_ = type; } | |
109 | |
110 Mutability mutability() const { return mutability_; } | |
111 void set_mutability(Mutability mutability) { mutability_ = mutability; } | |
112 | |
113 bool missing_definition() const { return missing_definition_; } | |
114 | |
115 VariableProxy* first_forward_use() const { return first_forward_use_; } | |
116 | |
117 private: | |
118 AsmType* type_ = AsmType::None(); | |
119 StandardMember standard_member_ = kNone; | |
120 Mutability mutability_ = kInvalidMutability; | |
121 // missing_definition_ is set to true for forward definition - i.e., use | |
122 // before definition. | |
123 bool missing_definition_ = false; | |
124 // first_forward_use_ holds the AST node that first referenced this | |
125 // VariableInfo. Used for error messages. | |
126 VariableProxy* first_forward_use_ = nullptr; | |
127 }; | |
128 | |
129 // RAII-style manager for the in_function_ member variable. | |
130 struct FunctionScope { | |
131 explicit FunctionScope(AsmTyper* typer) : typer_(typer) { | |
132 DCHECK(!typer_->in_function_); | |
133 typer_->in_function_ = true; | |
134 typer_->local_scope_.Clear(); | |
135 typer_->return_type_ = nullptr; | |
136 } | |
137 | |
138 ~FunctionScope() { | |
139 DCHECK(typer_->in_function_); | |
140 typer_->in_function_ = false; | |
141 } | |
142 | |
143 AsmTyper* typer_; | |
144 }; | |
145 | |
146 // FlattenedStatements is an iterator class for ZoneList<Statement*> that | |
147 // flattens the Block construct in the AST. This is here because we need it in | |
148 // the tests. | |
149 class FlattenedStatements { | |
bradnelson
2016/06/24 00:19:03
Any reason this can't live inside asm-types.cc ?
John
2016/06/24 17:47:58
It may end up there -- that's where it was before
| |
150 public: | |
151 explicit FlattenedStatements(Zone* zone, ZoneList<Statement*>* s); | |
152 Statement* Next(); | |
153 | |
154 private: | |
155 struct Context { | |
156 explicit Context(ZoneList<Statement*>* s) : statements_(s) {} | |
157 ZoneList<Statement*>* statements_; | |
158 int next_index_ = 0; | |
159 }; | |
160 | |
161 ZoneVector<Context> context_stack_; | |
162 | |
163 DISALLOW_IMPLICIT_CONSTRUCTORS(FlattenedStatements); | |
164 }; | |
165 | |
166 using ObjectTypeMap = ZoneMap<std::string, VariableInfo*>; | |
167 void InitializeStdlib(); | |
168 | |
169 void AddForwardReference(VariableProxy* proxy, VariableInfo* info); | |
170 bool AddGlobal(Variable* global, VariableInfo* info); | |
171 bool AddLocal(Variable* global, VariableInfo* info); | |
172 // Used for 5.5 GlobalVariableTypeAnnotations | |
173 VariableInfo* ImportLookup(Property* expr); | |
174 // 3.3 Environment Lookup | |
175 // *VIOLATION* In the spec, the lookup function's prototype is | |
bradnelson
2016/06/24 00:19:03
Unclear how this is a violation of the spec? maybe
John
2016/06/24 17:47:58
Done.
| |
176 // | |
177 // Lookup(Delta, Gamma, x) | |
178 // | |
179 // Delta is the global_scope_ member, and Gamma, local_scope_. | |
180 VariableInfo* Lookup(Variable* variable); | |
181 | |
182 // 6.1 ValidateModule | |
183 AsmType* ValidateModule(FunctionLiteral* fun); | |
184 // 6.? ValidateGlobalDeclaration | |
bradnelson
2016/06/24 00:19:03
Isn't this really described in 6.1 (but not by tha
John
2016/06/24 17:47:58
Done.
| |
185 AsmType* ValidateGlobalDeclaration(Assignment* assign); | |
186 // 6.3 ValidateFunctionTable | |
bradnelson
2016/06/24 00:19:03
Any reason this isn't in spec order?
John
2016/06/24 17:47:58
Not really. Done.
| |
187 AsmType* ValidateFunctionTable(Assignment* assign); | |
188 // 6.2 ValidateExport | |
189 AsmType* ValidateExport(ReturnStatement* exports); | |
190 // 6.4 ValidateFunction | |
191 AsmType* ValidateFunction(FunctionDeclaration* fun_decl); | |
192 // 6.5 ValidateStatement | |
193 AsmType* ValidateStatement(Statement* statement); | |
194 // 6.5.1 BlockStatement | |
195 AsmType* ValidateBlockStatement(Block* block); | |
196 // 6.5.2 ExpressionStatement | |
197 AsmType* ValidateExpressionStatement(ExpressionStatement* expr); | |
198 // 6.5.3 EmptyStatement | |
199 AsmType* ValidateEmptyStatement(EmptyStatement* empty); | |
200 // 6.5.4 IfStatement | |
201 AsmType* ValidateIfStatement(IfStatement* if_stmt); | |
202 // 6.5.5 ReturnStatement | |
203 AsmType* ValidateReturnStatement(ReturnStatement* ret_stmt); | |
204 // 6.5.6 IterationStatement | |
205 // 6.5.6.a WhileStatement | |
206 AsmType* ValidateWhileStatement(WhileStatement* while_stmt); | |
207 // 6.5.6.b DoWhileStatement | |
208 AsmType* ValidateDoWhileStatement(DoWhileStatement* do_while); | |
209 // 6.5.6.c ForStatement | |
210 AsmType* ValidateForStatement(ForStatement* for_stmt); | |
211 // 6.5.7 BreakStatement | |
212 AsmType* ValidateBreakStatement(BreakStatement* brk_stmt); | |
213 // 6.5.8 ContinueStatement | |
214 AsmType* ValidateContinueStatement(ContinueStatement* cont_stmt); | |
215 // 6.5.9 LabelledStatement | |
216 // JPP, what to do?! AsmType* ValidateLabelledStatement(brk_stmt); | |
217 // 6.5.10 SwitchStatement | |
218 AsmType* ValidateSwitchStatement(SwitchStatement* stmt); | |
219 // 6.6 ValidateCase | |
220 AsmType* ValidateCase(CaseClause* label); | |
221 // 6.7 ValidateDefault | |
222 AsmType* ValidateDefault(CaseClause* label); | |
223 // 6.8 ValidateExpression | |
224 AsmType* ValidateExpression(Expression* expr); | |
225 AsmType* ValidateCompareOperation(CompareOperation* cmp); | |
226 AsmType* ValidateBinaryOperation(BinaryOperation* binop); | |
227 // 6.8.1 Expression | |
228 AsmType* ValidateCommaExpression(BinaryOperation* comma); | |
229 // 6.8.2 NumericLiteral | |
230 AsmType* ValidateNumericLiteral(Literal* literal); | |
231 // 6.8.3 Identifier | |
232 AsmType* ValidateIdentifier(VariableProxy* proxy); | |
233 // 6.8.4 CallExpression | |
234 AsmType* ValidateCallExpression(Call* call); | |
235 // 6.8.5 MemberExpression | |
236 AsmType* ValidateMemberExpression(Property* prop); | |
237 // 6.8.6 AssignmentExpression | |
238 AsmType* ValidateAssignmentExpression(Assignment* assignment); | |
239 // 6.8.7 UnaryExpression | |
240 AsmType* ValidateUnaryExpression(UnaryOperation* unop); | |
241 // 6.8.8 MultiplicativeExpression | |
242 AsmType* ValidateMultiplicativeExpression(BinaryOperation* binop); | |
243 // 6.8.9 AdditiveExpression | |
244 AsmType* ValidateAdditiveExpression(BinaryOperation* binop, | |
245 uint32_t intish_count); | |
246 // 6.8.10 ShiftExpression | |
247 AsmType* ValidateShiftExpression(BinaryOperation* binop); | |
248 // 6.8.11 RelationalExpression | |
249 AsmType* ValidateRelationalExpression(CompareOperation* cmpop); | |
250 // 6.8.12 EqualityExpression | |
251 AsmType* ValidateEqualityExpression(CompareOperation* cmpop); | |
252 // 6.8.13 BitwiseANDExpression | |
253 AsmType* ValidateBitwiseANDExpression(BinaryOperation* binop); | |
254 // 6.8.14 BitwiseXORExpression | |
255 AsmType* ValidateBitwiseXORExpression(BinaryOperation* binop); | |
256 // 6.8.15 BitwiseORExpression | |
257 AsmType* ValidateBitwiseORExpression(BinaryOperation* binop); | |
258 // 6.8.16 ConditionalExpression | |
259 AsmType* ValidateConditionalExpression(Conditional* cond); | |
260 // 6.9 ValidateCall | |
261 AsmType* ValidateCall(AsmType* return_type, Call* call); | |
262 // 6.10 ValidateHeapAccess | |
263 enum HeapAccessType { LoadFromHeap, StoreToHeap }; | |
264 AsmType* ValidateHeapAccess(Property* heap, HeapAccessType access_type); | |
265 // 6.11 ValidateFloatCoercion | |
266 bool IsCallToFround(Call* call); | |
267 AsmType* ValidateFloatCoercion(Call* call); | |
268 | |
269 // 5.1 ParameterTypeAnnotations | |
270 AsmType* ParameterTypeAnnotations(Variable* parameter, | |
271 Expression* annotation); | |
272 // 5.2 ReturnTypeAnnotations | |
273 AsmType* ReturnTypeAnnotations(ReturnStatement* statement); | |
274 // 5.4 VariableTypeAnnotations | |
275 AsmType* VariableTypeAnnotations(Expression* initializer); | |
276 // 5.5 GlobalVariableTypeAnnotations | |
277 AsmType* ImportExpression(Property* import); | |
278 AsmType* NewHeapView(CallNew* new_heap_view); | |
279 | |
280 Isolate* isolate_; | |
281 Zone* zone_; | |
282 Script* script_; | |
283 FunctionLiteral* root_; | |
284 bool in_function_ = false; | |
285 | |
286 AsmType* return_type_ = nullptr; | |
287 | |
288 ZoneVector<VariableInfo*> forward_definitions_; | |
289 ObjectTypeMap stdlib_types_; | |
290 ObjectTypeMap stdlib_math_types_; | |
291 | |
292 // The ASM module name. This member is used to prevent globals from redefining | |
293 // the module name. | |
294 VariableInfo module_info_; | |
bradnelson
2016/06/24 00:19:03
Ah good, had not handled this before!
John
2016/06/24 17:47:58
Acknowledged.
| |
295 Handle<String> module_name_; | |
296 | |
297 // 3 Environments | |
298 ZoneHashMap global_scope_; // 3.1 Global environment | |
299 ZoneHashMap local_scope_; // 3.2 Variable environment | |
300 | |
301 static const int kErrorMessageLimit = 100; | |
302 char error_message_[kErrorMessageLimit]; | |
303 | |
304 DISALLOW_IMPLICIT_CONSTRUCTORS(AsmTyper); | |
305 }; | |
306 | |
307 } // namespace wasm | |
308 } // namespace internal | |
309 } // namespace v8 | |
310 | |
311 #endif // SRC_WASM_ASM_TYPER_H_ | |
OLD | NEW |