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 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/wasm/asm-wasm-builder.h" | 7 #include "src/wasm/asm-wasm-builder.h" |
8 #include "src/wasm/wasm-macro-gen.h" | 8 #include "src/wasm/wasm-macro-gen.h" |
9 #include "src/wasm/wasm-opcodes.h" | 9 #include "src/wasm/wasm-opcodes.h" |
10 | 10 |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 current_function_builder_->Emit(kExprReturn); | 178 current_function_builder_->Emit(kExprReturn); |
179 } else { | 179 } else { |
180 marking_exported = true; | 180 marking_exported = true; |
181 } | 181 } |
182 RECURSE(Visit(stmt->expression())); | 182 RECURSE(Visit(stmt->expression())); |
183 if (!in_function_) { | 183 if (!in_function_) { |
184 marking_exported = false; | 184 marking_exported = false; |
185 } | 185 } |
186 } | 186 } |
187 | 187 |
188 void VisitWithStatement(WithStatement* stmt) { | 188 void VisitWithStatement(WithStatement* stmt) { UNREACHABLE(); } |
189 RECURSE(stmt->expression()); | |
190 RECURSE(stmt->statement()); | |
191 } | |
192 | 189 |
193 void VisitSwitchStatement(SwitchStatement* stmt) { | 190 void VisitSwitchStatement(SwitchStatement* stmt) { |
194 RECURSE(Visit(stmt->tag())); | 191 RECURSE(Visit(stmt->tag())); |
195 | 192 |
196 ZoneList<CaseClause*>* clauses = stmt->cases(); | 193 ZoneList<CaseClause*>* clauses = stmt->cases(); |
197 | 194 |
198 for (int i = 0; i < clauses->length(); ++i) { | 195 for (int i = 0; i < clauses->length(); ++i) { |
199 CaseClause* clause = clauses->at(i); | 196 CaseClause* clause = clauses->at(i); |
200 if (!clause->is_default()) { | 197 if (!clause->is_default()) { |
201 Expression* label = clause->label(); | 198 Expression* label = clause->label(); |
202 RECURSE(Visit(label)); | 199 RECURSE(Visit(label)); |
203 } | 200 } |
204 ZoneList<Statement*>* stmts = clause->statements(); | 201 ZoneList<Statement*>* stmts = clause->statements(); |
205 RECURSE(VisitStatements(stmts)); | 202 RECURSE(VisitStatements(stmts)); |
206 } | 203 } |
207 } | 204 } |
208 | 205 |
209 void VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } | 206 void VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } |
210 | 207 |
211 void VisitDoWhileStatement(DoWhileStatement* stmt) { | 208 void VisitDoWhileStatement(DoWhileStatement* stmt) { |
209 DCHECK(in_function_); | |
210 current_function_builder_->Emit(kExprLoop); | |
211 uint32_t index = current_function_builder_->EmitEditableImmediate(0); | |
212 int prev_block_size = block_size_; | |
213 block_size_ = 0; | |
214 breakable_blocks_.push_back( | |
215 std::make_pair(stmt->AsBreakableStatement(), true)); | |
216 block_size_++; | |
bradn
2015/12/16 15:54:17
Would it be clearer if we incremented block_size_
aseemgarg
2015/12/19 00:28:39
Took care in the next commit. Can't really move to
| |
212 RECURSE(Visit(stmt->body())); | 217 RECURSE(Visit(stmt->body())); |
218 block_size_++; | |
bradn
2015/12/16 15:54:17
Could we add {} around the whole statement plus th
aseemgarg
2015/12/19 00:28:39
Took care in the next commit.
| |
219 current_function_builder_->Emit(kExprIf); | |
213 RECURSE(Visit(stmt->cond())); | 220 RECURSE(Visit(stmt->cond())); |
221 current_function_builder_->EmitWithU8(kExprBr, 0); | |
222 current_function_builder_->Emit(kExprNop); | |
223 current_function_builder_->EditImmediate(index, block_size_); | |
224 block_size_ = prev_block_size; | |
225 breakable_blocks_.pop_back(); | |
214 } | 226 } |
215 | 227 |
216 void VisitWhileStatement(WhileStatement* stmt) { | 228 void VisitWhileStatement(WhileStatement* stmt) { |
217 DCHECK(in_function_); | 229 DCHECK(in_function_); |
218 current_function_builder_->EmitWithU8(kExprLoop, 1); | 230 current_function_builder_->EmitWithU8(kExprLoop, 1); |
219 breakable_blocks_.push_back( | 231 breakable_blocks_.push_back( |
220 std::make_pair(stmt->AsBreakableStatement(), true)); | 232 std::make_pair(stmt->AsBreakableStatement(), true)); |
221 current_function_builder_->Emit(kExprIf); | 233 current_function_builder_->Emit(kExprIf); |
222 RECURSE(Visit(stmt->cond())); | 234 RECURSE(Visit(stmt->cond())); |
223 current_function_builder_->EmitWithU8(kExprBr, 0); | 235 current_function_builder_->EmitWithU8(kExprBr, 0); |
224 RECURSE(Visit(stmt->body())); | 236 RECURSE(Visit(stmt->body())); |
225 breakable_blocks_.pop_back(); | 237 breakable_blocks_.pop_back(); |
226 } | 238 } |
227 | 239 |
228 void VisitForStatement(ForStatement* stmt) { | 240 void VisitForStatement(ForStatement* stmt) { |
241 DCHECK(in_function_); | |
229 if (stmt->init() != NULL) { | 242 if (stmt->init() != NULL) { |
243 block_size_++; | |
230 RECURSE(Visit(stmt->init())); | 244 RECURSE(Visit(stmt->init())); |
231 } | 245 } |
246 current_function_builder_->Emit(kExprLoop); | |
247 uint32_t index = current_function_builder_->EmitEditableImmediate(0); | |
248 int prev_block_size = block_size_; | |
249 block_size_ = 0; | |
250 breakable_blocks_.push_back( | |
251 std::make_pair(stmt->AsBreakableStatement(), true)); | |
232 if (stmt->cond() != NULL) { | 252 if (stmt->cond() != NULL) { |
253 block_size_++; | |
254 current_function_builder_->Emit(kExprIf); | |
bradn
2015/12/16 15:54:17
Same here?
aseemgarg
2015/12/19 00:28:39
Took care in the next commit.
| |
255 current_function_builder_->Emit(kExprBoolNot); | |
233 RECURSE(Visit(stmt->cond())); | 256 RECURSE(Visit(stmt->cond())); |
257 current_function_builder_->EmitWithU8(kExprBr, 1); | |
258 current_function_builder_->Emit(kExprNop); | |
259 } | |
260 if (stmt->body() != NULL) { | |
261 block_size_++; | |
262 RECURSE(Visit(stmt->body())); | |
234 } | 263 } |
235 if (stmt->next() != NULL) { | 264 if (stmt->next() != NULL) { |
265 block_size_++; | |
236 RECURSE(Visit(stmt->next())); | 266 RECURSE(Visit(stmt->next())); |
237 } | 267 } |
238 RECURSE(Visit(stmt->body())); | 268 block_size_++; |
bradn
2015/12/16 15:54:17
Same here?
aseemgarg
2015/12/19 00:28:39
Took care in the next commit.
| |
269 current_function_builder_->EmitWithU8(kExprBr, 0); | |
270 current_function_builder_->Emit(kExprNop); | |
271 current_function_builder_->EditImmediate(index, block_size_); | |
272 block_size_ = prev_block_size; | |
273 breakable_blocks_.pop_back(); | |
239 } | 274 } |
240 | 275 |
241 void VisitForInStatement(ForInStatement* stmt) { | 276 void VisitForInStatement(ForInStatement* stmt) { UNREACHABLE(); } |
242 RECURSE(Visit(stmt->enumerable())); | |
243 RECURSE(Visit(stmt->body())); | |
244 } | |
245 | 277 |
246 void VisitForOfStatement(ForOfStatement* stmt) { | 278 void VisitForOfStatement(ForOfStatement* stmt) { UNREACHABLE(); } |
247 RECURSE(Visit(stmt->iterable())); | |
248 RECURSE(Visit(stmt->body())); | |
249 } | |
250 | 279 |
251 void VisitTryCatchStatement(TryCatchStatement* stmt) { | 280 void VisitTryCatchStatement(TryCatchStatement* stmt) { UNREACHABLE(); } |
252 RECURSE(Visit(stmt->try_block())); | |
253 RECURSE(Visit(stmt->catch_block())); | |
254 } | |
255 | 281 |
256 void VisitTryFinallyStatement(TryFinallyStatement* stmt) { | 282 void VisitTryFinallyStatement(TryFinallyStatement* stmt) { UNREACHABLE(); } |
257 RECURSE(Visit(stmt->try_block())); | |
258 RECURSE(Visit(stmt->finally_block())); | |
259 } | |
260 | 283 |
261 void VisitDebuggerStatement(DebuggerStatement* stmt) {} | 284 void VisitDebuggerStatement(DebuggerStatement* stmt) { UNREACHABLE(); } |
262 | 285 |
263 void VisitFunctionLiteral(FunctionLiteral* expr) { | 286 void VisitFunctionLiteral(FunctionLiteral* expr) { |
264 Scope* scope = expr->scope(); | 287 Scope* scope = expr->scope(); |
265 if (in_function_) { | 288 if (in_function_) { |
266 if (expr->bounds().lower->IsFunction()) { | 289 if (expr->bounds().lower->IsFunction()) { |
267 Type::FunctionType* func_type = expr->bounds().lower->AsFunction(); | 290 Type::FunctionType* func_type = expr->bounds().lower->AsFunction(); |
268 LocalType return_type = TypeFrom(func_type->Result()); | 291 LocalType return_type = TypeFrom(func_type->Result()); |
269 current_function_builder_->ReturnType(return_type); | 292 current_function_builder_->ReturnType(return_type); |
270 for (int i = 0; i < expr->parameter_count(); i++) { | 293 for (int i = 0; i < expr->parameter_count(); i++) { |
271 LocalType type = TypeFrom(func_type->Parameter(i)); | 294 LocalType type = TypeFrom(func_type->Parameter(i)); |
272 DCHECK(type != kAstStmt); | 295 DCHECK(type != kAstStmt); |
273 LookupOrInsertLocal(scope->parameter(i), type); | 296 LookupOrInsertLocal(scope->parameter(i), type); |
274 } | 297 } |
275 } else { | 298 } else { |
276 UNREACHABLE(); | 299 UNREACHABLE(); |
277 } | 300 } |
278 } | 301 } |
279 RECURSE(VisitDeclarations(scope->declarations())); | 302 RECURSE(VisitDeclarations(scope->declarations())); |
280 RECURSE(VisitStatements(expr->body())); | 303 RECURSE(VisitStatements(expr->body())); |
281 } | 304 } |
282 | 305 |
283 void VisitNativeFunctionLiteral(NativeFunctionLiteral* expr) {} | 306 void VisitNativeFunctionLiteral(NativeFunctionLiteral* expr) { |
307 UNREACHABLE(); | |
308 } | |
284 | 309 |
285 void VisitConditional(Conditional* expr) { | 310 void VisitConditional(Conditional* expr) { |
311 DCHECK(in_function_); | |
312 current_function_builder_->Emit(kExprIfElse); | |
286 RECURSE(Visit(expr->condition())); | 313 RECURSE(Visit(expr->condition())); |
287 RECURSE(Visit(expr->then_expression())); | 314 RECURSE(Visit(expr->then_expression())); |
288 RECURSE(Visit(expr->else_expression())); | 315 RECURSE(Visit(expr->else_expression())); |
289 } | 316 } |
290 | 317 |
291 void VisitVariableProxy(VariableProxy* expr) { | 318 void VisitVariableProxy(VariableProxy* expr) { |
292 if (in_function_) { | 319 if (in_function_) { |
293 Variable* var = expr->var(); | 320 Variable* var = expr->var(); |
294 if (var->is_function()) { | 321 if (var->is_function()) { |
295 DCHECK(!is_set_op_); | 322 DCHECK(!is_set_op_); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 current_function_builder_->EmitCode(code, sizeof(code)); | 379 current_function_builder_->EmitCode(code, sizeof(code)); |
353 break; | 380 break; |
354 } | 381 } |
355 default: | 382 default: |
356 UNREACHABLE(); | 383 UNREACHABLE(); |
357 } | 384 } |
358 } | 385 } |
359 } | 386 } |
360 } | 387 } |
361 | 388 |
362 void VisitRegExpLiteral(RegExpLiteral* expr) {} | 389 void VisitRegExpLiteral(RegExpLiteral* expr) { UNREACHABLE(); } |
363 | 390 |
364 void VisitObjectLiteral(ObjectLiteral* expr) { | 391 void VisitObjectLiteral(ObjectLiteral* expr) { |
365 ZoneList<ObjectLiteralProperty*>* props = expr->properties(); | 392 ZoneList<ObjectLiteralProperty*>* props = expr->properties(); |
366 for (int i = 0; i < props->length(); ++i) { | 393 for (int i = 0; i < props->length(); ++i) { |
367 ObjectLiteralProperty* prop = props->at(i); | 394 ObjectLiteralProperty* prop = props->at(i); |
368 RECURSE(Visit(prop->value())); | 395 RECURSE(Visit(prop->value())); |
369 } | 396 } |
370 } | 397 } |
371 | 398 |
372 void VisitArrayLiteral(ArrayLiteral* expr) { | 399 void VisitArrayLiteral(ArrayLiteral* expr) { UNREACHABLE(); } |
373 ZoneList<Expression*>* values = expr->values(); | |
374 for (int i = 0; i < values->length(); ++i) { | |
375 Expression* value = values->at(i); | |
376 RECURSE(Visit(value)); | |
377 } | |
378 } | |
379 | 400 |
380 void LoadInitFunction() { | 401 void LoadInitFunction() { |
381 if (!init_function_initialized) { | 402 if (!init_function_initialized) { |
382 init_function_initialized = true; | 403 init_function_initialized = true; |
383 unsigned char init[] = "__init__"; | 404 unsigned char init[] = "__init__"; |
384 init_function_index = builder_->AddFunction(init, 8); | 405 init_function_index = builder_->AddFunction(init, 8); |
385 current_function_builder_ = builder_->FunctionAt(init_function_index); | 406 current_function_builder_ = builder_->FunctionAt(init_function_index); |
386 current_function_builder_->ReturnType(kAstStmt); | 407 current_function_builder_->ReturnType(kAstStmt); |
387 current_function_builder_->Exported(1); | 408 current_function_builder_->Exported(1); |
388 in_function_ = true; | 409 in_function_ = true; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 } | 441 } |
421 is_set_op_ = true; | 442 is_set_op_ = true; |
422 RECURSE(Visit(expr->target())); | 443 RECURSE(Visit(expr->target())); |
423 DCHECK(!is_set_op_); | 444 DCHECK(!is_set_op_); |
424 RECURSE(Visit(expr->value())); | 445 RECURSE(Visit(expr->value())); |
425 if (in_init) { | 446 if (in_init) { |
426 UnLoadInitFunction(); | 447 UnLoadInitFunction(); |
427 } | 448 } |
428 } | 449 } |
429 | 450 |
430 void VisitYield(Yield* expr) { | 451 void VisitYield(Yield* expr) { UNREACHABLE(); } |
431 RECURSE(Visit(expr->generator_object())); | |
432 RECURSE(Visit(expr->expression())); | |
433 } | |
434 | 452 |
435 void VisitThrow(Throw* expr) { RECURSE(Visit(expr->exception())); } | 453 void VisitThrow(Throw* expr) { UNREACHABLE(); } |
436 | 454 |
437 void VisitProperty(Property* expr) { | 455 void VisitProperty(Property* expr) { |
438 Expression* obj = expr->obj(); | 456 Expression* obj = expr->obj(); |
439 DCHECK(obj->bounds().lower == obj->bounds().upper); | 457 DCHECK(obj->bounds().lower == obj->bounds().upper); |
440 TypeImpl<ZoneTypeConfig>* type = obj->bounds().lower; | 458 TypeImpl<ZoneTypeConfig>* type = obj->bounds().lower; |
441 MachineType mtype; | 459 MachineType mtype; |
442 int size; | 460 int size; |
443 if (type->Is(cache_.kUint8Array)) { | 461 if (type->Is(cache_.kUint8Array)) { |
444 mtype = MachineType::Uint8(); | 462 mtype = MachineType::Uint8(); |
445 size = 1; | 463 size = 1; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
530 DCHECK(TypeOf(expr->expression()) == kAstI32); | 548 DCHECK(TypeOf(expr->expression()) == kAstI32); |
531 current_function_builder_->Emit(kExprBoolNot); | 549 current_function_builder_->Emit(kExprBoolNot); |
532 break; | 550 break; |
533 } | 551 } |
534 default: | 552 default: |
535 UNREACHABLE(); | 553 UNREACHABLE(); |
536 } | 554 } |
537 RECURSE(Visit(expr->expression())); | 555 RECURSE(Visit(expr->expression())); |
538 } | 556 } |
539 | 557 |
540 void VisitCountOperation(CountOperation* expr) { | 558 void VisitCountOperation(CountOperation* expr) { UNREACHABLE(); } |
541 RECURSE(Visit(expr->expression())); | |
542 } | |
543 | 559 |
544 bool MatchIntBinaryOperation(BinaryOperation* expr, Token::Value op, | 560 bool MatchIntBinaryOperation(BinaryOperation* expr, Token::Value op, |
545 int32_t val) { | 561 int32_t val) { |
546 DCHECK(expr->right() != NULL); | 562 DCHECK(expr->right() != NULL); |
547 if (expr->op() == op && expr->right()->IsLiteral() && | 563 if (expr->op() == op && expr->right()->IsLiteral() && |
548 TypeOf(expr) == kAstI32) { | 564 TypeOf(expr) == kAstI32) { |
549 Literal* right = expr->right()->AsLiteral(); | 565 Literal* right = expr->right()->AsLiteral(); |
550 DCHECK(right->raw_value()->IsNumber()); | 566 DCHECK(right->raw_value()->IsNumber()); |
551 if (static_cast<int32_t>(right->raw_value()->AsNumber()) == val) { | 567 if (static_cast<int32_t>(right->raw_value()->AsNumber()) == val) { |
552 return true; | 568 return true; |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
834 } else { | 850 } else { |
835 UNREACHABLE(); | 851 UNREACHABLE(); |
836 } | 852 } |
837 } | 853 } |
838 | 854 |
839 #undef CASE | 855 #undef CASE |
840 #undef NON_SIGNED_INT | 856 #undef NON_SIGNED_INT |
841 #undef SIGNED | 857 #undef SIGNED |
842 #undef NON_SIGNED | 858 #undef NON_SIGNED |
843 | 859 |
844 void VisitThisFunction(ThisFunction* expr) {} | 860 void VisitThisFunction(ThisFunction* expr) { UNREACHABLE(); } |
845 | 861 |
846 void VisitDeclarations(ZoneList<Declaration*>* decls) { | 862 void VisitDeclarations(ZoneList<Declaration*>* decls) { |
847 for (int i = 0; i < decls->length(); ++i) { | 863 for (int i = 0; i < decls->length(); ++i) { |
848 Declaration* decl = decls->at(i); | 864 Declaration* decl = decls->at(i); |
849 RECURSE(Visit(decl)); | 865 RECURSE(Visit(decl)); |
850 } | 866 } |
851 } | 867 } |
852 | 868 |
853 void VisitClassLiteral(ClassLiteral* expr) {} | 869 void VisitClassLiteral(ClassLiteral* expr) { UNREACHABLE(); } |
854 | 870 |
855 void VisitSpread(Spread* expr) {} | 871 void VisitSpread(Spread* expr) { UNREACHABLE(); } |
856 | 872 |
857 void VisitSuperPropertyReference(SuperPropertyReference* expr) {} | 873 void VisitSuperPropertyReference(SuperPropertyReference* expr) { |
874 UNREACHABLE(); | |
875 } | |
858 | 876 |
859 void VisitSuperCallReference(SuperCallReference* expr) {} | 877 void VisitSuperCallReference(SuperCallReference* expr) { UNREACHABLE(); } |
860 | 878 |
861 void VisitSloppyBlockFunctionStatement(SloppyBlockFunctionStatement* expr) {} | 879 void VisitSloppyBlockFunctionStatement(SloppyBlockFunctionStatement* expr) { |
880 UNREACHABLE(); | |
881 } | |
862 | 882 |
863 void VisitDoExpression(DoExpression* expr) {} | 883 void VisitDoExpression(DoExpression* expr) { UNREACHABLE(); } |
864 | 884 |
865 void VisitRewritableAssignmentExpression( | 885 void VisitRewritableAssignmentExpression( |
866 RewritableAssignmentExpression* expr) {} | 886 RewritableAssignmentExpression* expr) { |
887 UNREACHABLE(); | |
888 } | |
867 | 889 |
868 struct IndexContainer : public ZoneObject { | 890 struct IndexContainer : public ZoneObject { |
869 uint16_t index; | 891 uint16_t index; |
870 }; | 892 }; |
871 | 893 |
872 uint16_t LookupOrInsertLocal(Variable* v, LocalType type) { | 894 uint16_t LookupOrInsertLocal(Variable* v, LocalType type) { |
873 DCHECK(current_function_builder_ != NULL); | 895 DCHECK(current_function_builder_ != NULL); |
874 ZoneHashMap::Entry* entry = | 896 ZoneHashMap::Entry* entry = |
875 local_variables_.Lookup(v, ComputePointerHash(v)); | 897 local_variables_.Lookup(v, ComputePointerHash(v)); |
876 if (entry == NULL) { | 898 if (entry == NULL) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
969 // that zone in constructor may be thrown away once wasm module is written. | 991 // that zone in constructor may be thrown away once wasm module is written. |
970 WasmModuleIndex* AsmWasmBuilder::Run() { | 992 WasmModuleIndex* AsmWasmBuilder::Run() { |
971 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); | 993 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); |
972 impl.Compile(); | 994 impl.Compile(); |
973 WasmModuleWriter* writer = impl.builder_->Build(zone_); | 995 WasmModuleWriter* writer = impl.builder_->Build(zone_); |
974 return writer->WriteTo(zone_); | 996 return writer->WriteTo(zone_); |
975 } | 997 } |
976 } // namespace wasm | 998 } // namespace wasm |
977 } // namespace internal | 999 } // namespace internal |
978 } // namespace v8 | 1000 } // namespace v8 |
OLD | NEW |