Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: src/asmjs/asm-typer.h

Issue 2398023002: [wasm] asm.js - Parse and convert asm.js to wasm a function at a time. (Closed)
Patch Set: fix Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/asmjs/asm-js.cc ('k') | src/asmjs/asm-typer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 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 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 SRC_ASMJS_ASM_TYPER_H_ 5 #ifndef SRC_ASMJS_ASM_TYPER_H_
6 #define SRC_ASMJS_ASM_TYPER_H_ 6 #define SRC_ASMJS_ASM_TYPER_H_
7 7
8 #include <cstdint> 8 #include <cstdint>
9 #include <string> 9 #include <string>
10 #include <unordered_set> 10 #include <unordered_set>
11 11
12 #include "src/allocation.h" 12 #include "src/allocation.h"
13 #include "src/asmjs/asm-types.h" 13 #include "src/asmjs/asm-types.h"
14 #include "src/ast/ast-type-bounds.h" 14 #include "src/ast/ast-type-bounds.h"
15 #include "src/ast/ast-types.h" 15 #include "src/ast/ast-types.h"
16 #include "src/ast/ast.h" 16 #include "src/ast/ast.h"
17 #include "src/effects.h" 17 #include "src/effects.h"
18 #include "src/type-info.h" 18 #include "src/type-info.h"
19 #include "src/zone/zone-containers.h" 19 #include "src/zone/zone-containers.h"
20 #include "src/zone/zone.h" 20 #include "src/zone/zone.h"
21 21
22 namespace v8 { 22 namespace v8 {
23 namespace internal { 23 namespace internal {
24 namespace wasm { 24 namespace wasm {
25 25
26 class AsmType; 26 class AsmType;
27 class AsmTyperHarnessBuilder; 27 class AsmTyperHarnessBuilder;
28 class SourceLayoutTracker;
28 29
29 class AsmTyper final { 30 class AsmTyper final {
30 public: 31 public:
31 enum StandardMember { 32 enum StandardMember {
32 kHeap = -4, 33 kHeap = -4,
33 kFFI = -3, 34 kFFI = -3,
34 kStdlib = -2, 35 kStdlib = -2,
35 kModule = -1, 36 kModule = -1,
36 kNone = 0, 37 kNone = 0,
37 kInfinity, 38 kInfinity,
(...skipping 24 matching lines...) Expand all
62 kMathLOG10E, 63 kMathLOG10E,
63 kMathPI, 64 kMathPI,
64 kMathSQRT1_2, 65 kMathSQRT1_2,
65 kMathSQRT2, 66 kMathSQRT2,
66 }; 67 };
67 68
68 ~AsmTyper() = default; 69 ~AsmTyper() = default;
69 AsmTyper(Isolate* isolate, Zone* zone, Script* script, FunctionLiteral* root); 70 AsmTyper(Isolate* isolate, Zone* zone, Script* script, FunctionLiteral* root);
70 71
71 bool Validate(); 72 bool Validate();
73 // Do asm.js validation in phases (to interleave with conversion to wasm).
74 bool ValidateBeforeFunctionsPhase();
75 bool ValidateInnerFunction(FunctionDeclaration* decl);
76 bool ValidateAfterFunctionsPhase();
77 void ClearFunctionNodeTypes();
72 78
73 const char* error_message() const { return error_message_; } 79 const char* error_message() const { return error_message_; }
74 80
75 AsmType* TypeOf(AstNode* node) const; 81 AsmType* TypeOf(AstNode* node) const;
76 AsmType* TypeOf(Variable* v) const; 82 AsmType* TypeOf(Variable* v) const;
77 StandardMember VariableAsStandardMember(Variable* var); 83 StandardMember VariableAsStandardMember(Variable* var);
78 84
79 typedef std::unordered_set<StandardMember, std::hash<int> > StdlibSet; 85 typedef std::unordered_set<StandardMember, std::hash<int> > StdlibSet;
80 86
81 StdlibSet StdlibUses() const { return stdlib_uses_; } 87 StdlibSet StdlibUses() const { return stdlib_uses_; }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 bool IsGlobal() const { 129 bool IsGlobal() const {
124 return mutability_ == kImmutableGlobal || mutability_ == kConstGlobal || 130 return mutability_ == kImmutableGlobal || mutability_ == kConstGlobal ||
125 mutability_ == kMutableGlobal; 131 mutability_ == kMutableGlobal;
126 } 132 }
127 133
128 bool IsStdlib() const { return standard_member_ == kStdlib; } 134 bool IsStdlib() const { return standard_member_ == kStdlib; }
129 bool IsFFI() const { return standard_member_ == kFFI; } 135 bool IsFFI() const { return standard_member_ == kFFI; }
130 bool IsHeap() const { return standard_member_ == kHeap; } 136 bool IsHeap() const { return standard_member_ == kHeap; }
131 137
132 void MarkDefined() { missing_definition_ = false; } 138 void MarkDefined() { missing_definition_ = false; }
133 void FirstForwardUseIs(VariableProxy* var); 139 void SetFirstForwardUse(int source_location);
134 140
135 StandardMember standard_member() const { return standard_member_; } 141 StandardMember standard_member() const { return standard_member_; }
136 void set_standard_member(StandardMember standard_member) { 142 void set_standard_member(StandardMember standard_member) {
137 standard_member_ = standard_member; 143 standard_member_ = standard_member;
138 } 144 }
139 145
140 AsmType* type() const { return type_; } 146 AsmType* type() const { return type_; }
141 void set_type(AsmType* type) { type_ = type; } 147 void set_type(AsmType* type) { type_ = type; }
142 148
143 Mutability mutability() const { return mutability_; } 149 Mutability mutability() const { return mutability_; }
144 void set_mutability(Mutability mutability) { mutability_ = mutability; } 150 void set_mutability(Mutability mutability) { mutability_ = mutability; }
145 151
146 bool missing_definition() const { return missing_definition_; } 152 bool missing_definition() const { return missing_definition_; }
147 153
148 VariableProxy* first_forward_use() const { return first_forward_use_; } 154 int source_location() const { return source_location_; }
149 155
150 static VariableInfo* ForSpecialSymbol(Zone* zone, 156 static VariableInfo* ForSpecialSymbol(Zone* zone,
151 StandardMember standard_member); 157 StandardMember standard_member);
152 158
153 private: 159 private:
154 AsmType* type_; 160 AsmType* type_;
155 StandardMember standard_member_ = kNone; 161 StandardMember standard_member_ = kNone;
156 Mutability mutability_ = kInvalidMutability; 162 Mutability mutability_ = kInvalidMutability;
157 // missing_definition_ is set to true for forward definition - i.e., use 163 // missing_definition_ is set to true for forward definition - i.e., use
158 // before definition. 164 // before definition.
159 bool missing_definition_ = false; 165 bool missing_definition_ = false;
160 // first_forward_use_ holds the AST node that first referenced this 166 // source_location_ holds the line number that first referenced this
161 // VariableInfo. Used for error messages. 167 // VariableInfo. Used for error messages.
162 VariableProxy* first_forward_use_ = nullptr; 168 // TODO(bradnelson): When merged with console change, this should
169 // become a source location.
170 int source_location_ = -1;
163 }; 171 };
164 172
165 // RAII-style manager for the in_function_ member variable. 173 // RAII-style manager for the in_function_ member variable.
166 struct FunctionScope { 174 struct FunctionScope {
167 explicit FunctionScope(AsmTyper* typer) : typer_(typer) { 175 explicit FunctionScope(AsmTyper* typer) : typer_(typer) {
168 DCHECK(!typer_->in_function_); 176 DCHECK(!typer_->in_function_);
169 typer_->in_function_ = true; 177 typer_->in_function_ = true;
170 typer_->local_scope_.Clear(); 178 typer_->local_scope_.Clear();
171 typer_->return_type_ = AsmType::None(); 179 typer_->return_type_ = AsmType::None();
172 } 180 }
(...skipping 19 matching lines...) Expand all
192 explicit Context(ZoneList<Statement*>* s) : statements_(s) {} 200 explicit Context(ZoneList<Statement*>* s) : statements_(s) {}
193 ZoneList<Statement*>* statements_; 201 ZoneList<Statement*>* statements_;
194 int next_index_ = 0; 202 int next_index_ = 0;
195 }; 203 };
196 204
197 ZoneVector<Context> context_stack_; 205 ZoneVector<Context> context_stack_;
198 206
199 DISALLOW_IMPLICIT_CONSTRUCTORS(FlattenedStatements); 207 DISALLOW_IMPLICIT_CONSTRUCTORS(FlattenedStatements);
200 }; 208 };
201 209
210 class SourceLayoutTracker {
211 public:
212 SourceLayoutTracker() = default;
213 bool IsValid() const;
214 void AddUseAsm(const AstNode& node) { use_asm_.AddNewElement(node); }
215 void AddGlobal(const AstNode& node) { globals_.AddNewElement(node); }
216 void AddFunction(const AstNode& node) { functions_.AddNewElement(node); }
217 void AddTable(const AstNode& node) { tables_.AddNewElement(node); }
218 void AddExport(const AstNode& node) { exports_.AddNewElement(node); }
219
220 private:
221 class Section {
222 public:
223 Section() = default;
224 Section(const Section&) = default;
225 Section& operator=(const Section&) = default;
226
227 void AddNewElement(const AstNode& node);
228 bool IsPrecededBy(const Section& other) const;
229
230 private:
231 int start_ = kNoSourcePosition;
232 int end_ = kNoSourcePosition;
233 };
234
235 Section use_asm_;
236 Section globals_;
237 Section functions_;
238 Section tables_;
239 Section exports_;
240
241 DISALLOW_COPY_AND_ASSIGN(SourceLayoutTracker);
242 };
243
202 using ObjectTypeMap = ZoneMap<std::string, VariableInfo*>; 244 using ObjectTypeMap = ZoneMap<std::string, VariableInfo*>;
203 void InitializeStdlib(); 245 void InitializeStdlib();
204 void SetTypeOf(AstNode* node, AsmType* type); 246 void SetTypeOf(AstNode* node, AsmType* type);
205 247
206 void AddForwardReference(VariableProxy* proxy, VariableInfo* info); 248 void AddForwardReference(VariableProxy* proxy, VariableInfo* info);
207 bool AddGlobal(Variable* global, VariableInfo* info); 249 bool AddGlobal(Variable* global, VariableInfo* info);
208 bool AddLocal(Variable* global, VariableInfo* info); 250 bool AddLocal(Variable* global, VariableInfo* info);
209 // Used for 5.5 GlobalVariableTypeAnnotations 251 // Used for 5.5 GlobalVariableTypeAnnotations
210 VariableInfo* ImportLookup(Property* expr); 252 VariableInfo* ImportLookup(Property* expr);
211 // 3.3 Environment Lookup 253 // 3.3 Environment Lookup
212 // NOTE: In the spec, the lookup function's prototype is 254 // NOTE: In the spec, the lookup function's prototype is
213 // 255 //
214 // Lookup(Delta, Gamma, x) 256 // Lookup(Delta, Gamma, x)
215 // 257 //
216 // Delta is the global_scope_ member, and Gamma, local_scope_. 258 // Delta is the global_scope_ member, and Gamma, local_scope_.
217 VariableInfo* Lookup(Variable* variable) const; 259 VariableInfo* Lookup(Variable* variable) const;
218 260
219 // All of the ValidateXXX methods below return AsmType::None() in case of 261 // All of the ValidateXXX methods below return AsmType::None() in case of
220 // validation failure. 262 // validation failure.
221 263
222 // 6.1 ValidateModule 264 // 6.1 ValidateModule
223 AsmType* ValidateModule(FunctionLiteral* fun); 265 AsmType* ValidateModuleBeforeFunctionsPhase(FunctionLiteral* fun);
266 AsmType* ValidateModuleFunction(FunctionDeclaration* fun_decl);
267 AsmType* ValidateModuleFunctions(FunctionLiteral* fun);
268 AsmType* ValidateModuleAfterFunctionsPhase(FunctionLiteral* fun);
224 AsmType* ValidateGlobalDeclaration(Assignment* assign); 269 AsmType* ValidateGlobalDeclaration(Assignment* assign);
225 // 6.2 ValidateExport 270 // 6.2 ValidateExport
226 AsmType* ExportType(VariableProxy* fun_export); 271 AsmType* ExportType(VariableProxy* fun_export);
227 AsmType* ValidateExport(ReturnStatement* exports); 272 AsmType* ValidateExport(ReturnStatement* exports);
228 // 6.3 ValidateFunctionTable 273 // 6.3 ValidateFunctionTable
229 AsmType* ValidateFunctionTable(Assignment* assign); 274 AsmType* ValidateFunctionTable(Assignment* assign);
230 // 6.4 ValidateFunction 275 // 6.4 ValidateFunction
231 AsmType* ValidateFunction(FunctionDeclaration* fun_decl); 276 AsmType* ValidateFunction(FunctionDeclaration* fun_decl);
232 // 6.5 ValidateStatement 277 // 6.5 ValidateStatement
233 AsmType* ValidateStatement(Statement* statement); 278 AsmType* ValidateStatement(Statement* statement);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 // the module name. 383 // the module name.
339 VariableInfo* module_info_; 384 VariableInfo* module_info_;
340 Handle<String> module_name_; 385 Handle<String> module_name_;
341 386
342 // 3 Environments 387 // 3 Environments
343 ZoneHashMap global_scope_; // 3.1 Global environment 388 ZoneHashMap global_scope_; // 3.1 Global environment
344 ZoneHashMap local_scope_; // 3.2 Variable environment 389 ZoneHashMap local_scope_; // 3.2 Variable environment
345 390
346 std::uintptr_t stack_limit_; 391 std::uintptr_t stack_limit_;
347 bool stack_overflow_ = false; 392 bool stack_overflow_ = false;
348 ZoneMap<AstNode*, AsmType*> node_types_; 393 ZoneMap<AstNode*, AsmType*> module_node_types_;
394 ZoneMap<AstNode*, AsmType*> function_node_types_;
349 static const int kErrorMessageLimit = 128; 395 static const int kErrorMessageLimit = 128;
350 AsmType* fround_type_; 396 AsmType* fround_type_;
351 AsmType* ffi_type_; 397 AsmType* ffi_type_;
352 char error_message_[kErrorMessageLimit]; 398 char error_message_[kErrorMessageLimit];
353 StdlibSet stdlib_uses_; 399 StdlibSet stdlib_uses_;
354 400
401 SourceLayoutTracker source_layout_;
402 ReturnStatement* module_return_;
403 ZoneVector<Assignment*> function_pointer_tables_;
404
355 DISALLOW_IMPLICIT_CONSTRUCTORS(AsmTyper); 405 DISALLOW_IMPLICIT_CONSTRUCTORS(AsmTyper);
356 }; 406 };
357 407
358 } // namespace wasm 408 } // namespace wasm
359 } // namespace internal 409 } // namespace internal
360 } // namespace v8 410 } // namespace v8
361 411
362 #endif // SRC_ASMJS_ASM_TYPER_H_ 412 #endif // SRC_ASMJS_ASM_TYPER_H_
OLDNEW
« no previous file with comments | « src/asmjs/asm-js.cc ('k') | src/asmjs/asm-typer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698