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/interpreter/bytecode-generator.h" | 5 #include "src/interpreter/bytecode-generator.h" |
6 | 6 |
7 #include <stack> | 7 #include <stack> |
8 | 8 |
9 #include "src/compiler.h" | 9 #include "src/compiler.h" |
10 #include "src/objects.h" | 10 #include "src/objects.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 if (scope()->is_function_scope() && scope()->function() != NULL) { | 38 if (scope()->is_function_scope() && scope()->function() != NULL) { |
39 VisitVariableDeclaration(scope()->function()); | 39 VisitVariableDeclaration(scope()->function()); |
40 } | 40 } |
41 | 41 |
42 // Visit declarations within the function scope. | 42 // Visit declarations within the function scope. |
43 VisitDeclarations(scope()->declarations()); | 43 VisitDeclarations(scope()->declarations()); |
44 | 44 |
45 // Visit statements in the function body. | 45 // Visit statements in the function body. |
46 VisitStatements(info->literal()->body()); | 46 VisitStatements(info->literal()->body()); |
47 | 47 |
48 // If the last bytecode wasn't a return, then return 'undefined' to avoid | |
49 // falling off the end. | |
50 if (!builder_.HasExplicitReturn()) { | |
51 builder_.LoadUndefined(); | |
52 builder_.Return(); | |
53 } | |
54 | |
55 set_scope(nullptr); | 48 set_scope(nullptr); |
56 set_info(nullptr); | 49 set_info(nullptr); |
57 return builder_.ToBytecodeArray(); | 50 return builder_.ToBytecodeArray(); |
58 } | 51 } |
59 | 52 |
60 | 53 |
61 void BytecodeGenerator::VisitBlock(Block* node) { | 54 void BytecodeGenerator::VisitBlock(Block* node) { |
| 55 builder().EnterBlock(); |
62 if (node->scope() == NULL) { | 56 if (node->scope() == NULL) { |
63 // Visit statements in the same scope, no declarations. | 57 // Visit statements in the same scope, no declarations. |
64 VisitStatements(node->statements()); | 58 VisitStatements(node->statements()); |
65 } else { | 59 } else { |
66 // Visit declarations and statements in a block scope. | 60 // Visit declarations and statements in a block scope. |
67 if (node->scope()->ContextLocalCount() > 0) { | 61 if (node->scope()->ContextLocalCount() > 0) { |
68 UNIMPLEMENTED(); | 62 UNIMPLEMENTED(); |
69 } else { | 63 } else { |
70 VisitDeclarations(node->scope()->declarations()); | 64 VisitDeclarations(node->scope()->declarations()); |
71 VisitStatements(node->statements()); | 65 VisitStatements(node->statements()); |
72 } | 66 } |
73 } | 67 } |
| 68 builder().LeaveBlock(); |
74 } | 69 } |
75 | 70 |
76 | 71 |
77 void BytecodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) { | 72 void BytecodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) { |
78 Variable* variable = decl->proxy()->var(); | 73 Variable* variable = decl->proxy()->var(); |
79 switch (variable->location()) { | 74 switch (variable->location()) { |
80 case VariableLocation::GLOBAL: | 75 case VariableLocation::GLOBAL: |
81 case VariableLocation::UNALLOCATED: | 76 case VariableLocation::UNALLOCATED: |
82 UNIMPLEMENTED(); | 77 UNIMPLEMENTED(); |
83 break; | 78 break; |
(...skipping 23 matching lines...) Expand all Loading... |
107 UNIMPLEMENTED(); | 102 UNIMPLEMENTED(); |
108 } | 103 } |
109 | 104 |
110 | 105 |
111 void BytecodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { | 106 void BytecodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
112 Visit(stmt->expression()); | 107 Visit(stmt->expression()); |
113 } | 108 } |
114 | 109 |
115 | 110 |
116 void BytecodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) { | 111 void BytecodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) { |
117 UNIMPLEMENTED(); | 112 // TODO(oth): For control-flow it could be useful to signal empty paths here. |
118 } | 113 } |
119 | 114 |
120 | 115 |
121 void BytecodeGenerator::VisitIfStatement(IfStatement* stmt) { UNIMPLEMENTED(); } | 116 void BytecodeGenerator::VisitIfStatement(IfStatement* stmt) { |
| 117 BytecodeLabel else_start, else_end; |
| 118 // TODO(oth): Spot easy cases where there code would not need to |
| 119 // emit the then block or the else block, e.g. condition is |
| 120 // obviously true/1/false/0. |
| 121 Visit(stmt->condition()); |
| 122 builder().CastAccumulatorToBoolean(); |
| 123 builder().JumpIfFalse(&else_start); |
| 124 |
| 125 Visit(stmt->then_statement()); |
| 126 builder().Jump(&else_end); |
| 127 builder().Bind(&else_start); |
| 128 |
| 129 Visit(stmt->else_statement()); |
| 130 builder().Bind(&else_end); |
| 131 } |
122 | 132 |
123 | 133 |
124 void BytecodeGenerator::VisitSloppyBlockFunctionStatement( | 134 void BytecodeGenerator::VisitSloppyBlockFunctionStatement( |
125 SloppyBlockFunctionStatement* stmt) { | 135 SloppyBlockFunctionStatement* stmt) { |
126 Visit(stmt->statement()); | 136 Visit(stmt->statement()); |
127 } | 137 } |
128 | 138 |
129 | 139 |
130 void BytecodeGenerator::VisitContinueStatement(ContinueStatement* stmt) { | 140 void BytecodeGenerator::VisitContinueStatement(ContinueStatement* stmt) { |
131 UNIMPLEMENTED(); | 141 UNIMPLEMENTED(); |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 UNIMPLEMENTED(); | 481 UNIMPLEMENTED(); |
472 break; | 482 break; |
473 default: | 483 default: |
474 VisitArithmeticExpression(binop); | 484 VisitArithmeticExpression(binop); |
475 break; | 485 break; |
476 } | 486 } |
477 } | 487 } |
478 | 488 |
479 | 489 |
480 void BytecodeGenerator::VisitCompareOperation(CompareOperation* expr) { | 490 void BytecodeGenerator::VisitCompareOperation(CompareOperation* expr) { |
481 UNIMPLEMENTED(); | 491 Token::Value op = expr->op(); |
| 492 Expression* left = expr->left(); |
| 493 Expression* right = expr->right(); |
| 494 |
| 495 TemporaryRegisterScope temporary_register_scope(&builder_); |
| 496 Register temporary = temporary_register_scope.NewRegister(); |
| 497 |
| 498 Visit(left); |
| 499 builder().StoreAccumulatorInRegister(temporary); |
| 500 Visit(right); |
| 501 builder().CompareOperation(op, temporary, language_mode()); |
482 } | 502 } |
483 | 503 |
484 | 504 |
485 void BytecodeGenerator::VisitSpread(Spread* expr) { UNREACHABLE(); } | 505 void BytecodeGenerator::VisitSpread(Spread* expr) { UNREACHABLE(); } |
486 | 506 |
487 | 507 |
488 void BytecodeGenerator::VisitEmptyParentheses(EmptyParentheses* expr) { | 508 void BytecodeGenerator::VisitEmptyParentheses(EmptyParentheses* expr) { |
489 UNREACHABLE(); | 509 UNREACHABLE(); |
490 } | 510 } |
491 | 511 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 } | 546 } |
527 | 547 |
528 | 548 |
529 int BytecodeGenerator::feedback_index(FeedbackVectorICSlot slot) const { | 549 int BytecodeGenerator::feedback_index(FeedbackVectorICSlot slot) const { |
530 return info()->feedback_vector()->GetIndex(slot); | 550 return info()->feedback_vector()->GetIndex(slot); |
531 } | 551 } |
532 | 552 |
533 } // namespace interpreter | 553 } // namespace interpreter |
534 } // namespace internal | 554 } // namespace internal |
535 } // namespace v8 | 555 } // namespace v8 |
OLD | NEW |