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" |
11 #include "src/scopes.h" | 11 #include "src/scopes.h" |
12 #include "src/token.h" | 12 #include "src/token.h" |
13 | 13 |
14 namespace v8 { | 14 namespace v8 { |
15 namespace internal { | 15 namespace internal { |
16 namespace interpreter { | 16 namespace interpreter { |
17 | 17 |
18 BytecodeGenerator::BytecodeGenerator(Isolate* isolate, Zone* zone) | 18 BytecodeGenerator::BytecodeGenerator(Isolate* isolate, Zone* zone) |
19 : builder_(isolate, zone) { | 19 : builder_(isolate, zone) { |
20 InitializeAstVisitor(isolate, zone); | 20 InitializeAstVisitor(isolate, zone); |
21 } | 21 } |
22 | 22 |
23 | 23 |
24 BytecodeGenerator::~BytecodeGenerator() {} | 24 BytecodeGenerator::~BytecodeGenerator() {} |
25 | 25 |
26 | 26 |
27 Handle<BytecodeArray> BytecodeGenerator::MakeBytecode(CompilationInfo* info) { | 27 Handle<BytecodeArray> BytecodeGenerator::MakeBytecode(CompilationInfo* info) { |
| 28 set_info(info); |
28 set_scope(info->scope()); | 29 set_scope(info->scope()); |
29 | 30 |
30 // This a temporary guard (oth). | 31 // This a temporary guard (oth). |
31 DCHECK(scope()->is_function_scope()); | 32 DCHECK(scope()->is_function_scope()); |
32 | 33 |
33 builder().set_parameter_count(info->num_parameters_including_this()); | 34 builder().set_parameter_count(info->num_parameters_including_this()); |
34 builder().set_locals_count(scope()->num_stack_slots()); | 35 builder().set_locals_count(scope()->num_stack_slots()); |
35 | 36 |
36 // Visit implicit declaration of the function name. | 37 // Visit implicit declaration of the function name. |
37 if (scope()->is_function_scope() && scope()->function() != NULL) { | 38 if (scope()->is_function_scope() && scope()->function() != NULL) { |
38 VisitVariableDeclaration(scope()->function()); | 39 VisitVariableDeclaration(scope()->function()); |
39 } | 40 } |
40 | 41 |
41 // Visit declarations within the function scope. | 42 // Visit declarations within the function scope. |
42 VisitDeclarations(scope()->declarations()); | 43 VisitDeclarations(scope()->declarations()); |
43 | 44 |
44 // Visit statements in the function body. | 45 // Visit statements in the function body. |
45 VisitStatements(info->literal()->body()); | 46 VisitStatements(info->literal()->body()); |
46 | 47 |
47 set_scope(nullptr); | 48 set_scope(nullptr); |
| 49 set_info(nullptr); |
48 return builder_.ToBytecodeArray(); | 50 return builder_.ToBytecodeArray(); |
49 } | 51 } |
50 | 52 |
51 | 53 |
52 void BytecodeGenerator::VisitBlock(Block* node) { | 54 void BytecodeGenerator::VisitBlock(Block* node) { |
53 if (node->scope() == NULL) { | 55 if (node->scope() == NULL) { |
54 // Visit statements in the same scope, no declarations. | 56 // Visit statements in the same scope, no declarations. |
55 VisitStatements(node->statements()); | 57 VisitStatements(node->statements()); |
56 } else { | 58 } else { |
57 // Visit declarations and statements in a block scope. | 59 // Visit declarations and statements in a block scope. |
(...skipping 19 matching lines...) Expand all Loading... |
77 // Details stored in scope, i.e. variable index. | 79 // Details stored in scope, i.e. variable index. |
78 break; | 80 break; |
79 case VariableLocation::CONTEXT: | 81 case VariableLocation::CONTEXT: |
80 case VariableLocation::LOOKUP: | 82 case VariableLocation::LOOKUP: |
81 UNIMPLEMENTED(); | 83 UNIMPLEMENTED(); |
82 break; | 84 break; |
83 } | 85 } |
84 } | 86 } |
85 | 87 |
86 | 88 |
87 void BytecodeGenerator::VisitFunctionDeclaration(FunctionDeclaration* node) { | 89 void BytecodeGenerator::VisitFunctionDeclaration(FunctionDeclaration* decl) { |
88 UNIMPLEMENTED(); | 90 UNIMPLEMENTED(); |
89 } | 91 } |
90 | 92 |
91 | 93 |
92 void BytecodeGenerator::VisitImportDeclaration(ImportDeclaration* node) { | 94 void BytecodeGenerator::VisitImportDeclaration(ImportDeclaration* decl) { |
93 UNIMPLEMENTED(); | 95 UNIMPLEMENTED(); |
94 } | 96 } |
95 | 97 |
96 | 98 |
97 void BytecodeGenerator::VisitExportDeclaration(ExportDeclaration* node) { | 99 void BytecodeGenerator::VisitExportDeclaration(ExportDeclaration* decl) { |
98 UNIMPLEMENTED(); | 100 UNIMPLEMENTED(); |
99 } | 101 } |
100 | 102 |
101 | 103 |
102 void BytecodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { | 104 void BytecodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
103 Visit(stmt->expression()); | 105 Visit(stmt->expression()); |
104 } | 106 } |
105 | 107 |
106 | 108 |
107 void BytecodeGenerator::VisitEmptyStatement(EmptyStatement* node) { | 109 void BytecodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) { |
108 UNIMPLEMENTED(); | 110 UNIMPLEMENTED(); |
109 } | 111 } |
110 | 112 |
111 | 113 |
112 void BytecodeGenerator::VisitIfStatement(IfStatement* node) { UNIMPLEMENTED(); } | 114 void BytecodeGenerator::VisitIfStatement(IfStatement* stmt) { UNIMPLEMENTED(); } |
113 | 115 |
114 | 116 |
115 void BytecodeGenerator::VisitContinueStatement(ContinueStatement* node) { | 117 void BytecodeGenerator::VisitContinueStatement(ContinueStatement* stmt) { |
116 UNIMPLEMENTED(); | 118 UNIMPLEMENTED(); |
117 } | 119 } |
118 | 120 |
119 | 121 |
120 void BytecodeGenerator::VisitBreakStatement(BreakStatement* node) { | 122 void BytecodeGenerator::VisitBreakStatement(BreakStatement* stmt) { |
121 UNIMPLEMENTED(); | 123 UNIMPLEMENTED(); |
122 } | 124 } |
123 | 125 |
124 | 126 |
125 void BytecodeGenerator::VisitReturnStatement(ReturnStatement* node) { | 127 void BytecodeGenerator::VisitReturnStatement(ReturnStatement* stmt) { |
126 Visit(node->expression()); | 128 Visit(stmt->expression()); |
127 builder().Return(); | 129 builder().Return(); |
128 } | 130 } |
129 | 131 |
130 | 132 |
131 void BytecodeGenerator::VisitWithStatement(WithStatement* node) { | 133 void BytecodeGenerator::VisitWithStatement(WithStatement* stmt) { |
132 UNIMPLEMENTED(); | 134 UNIMPLEMENTED(); |
133 } | 135 } |
134 | 136 |
135 | 137 |
136 void BytecodeGenerator::VisitSwitchStatement(SwitchStatement* node) { | 138 void BytecodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) { |
137 UNIMPLEMENTED(); | 139 UNIMPLEMENTED(); |
138 } | 140 } |
139 | 141 |
140 | 142 |
141 void BytecodeGenerator::VisitCaseClause(CaseClause* clause) { UNIMPLEMENTED(); } | 143 void BytecodeGenerator::VisitCaseClause(CaseClause* clause) { UNIMPLEMENTED(); } |
142 | 144 |
143 | 145 |
144 void BytecodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { | 146 void BytecodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) { |
145 UNIMPLEMENTED(); | 147 UNIMPLEMENTED(); |
146 } | 148 } |
147 | 149 |
148 | 150 |
149 void BytecodeGenerator::VisitWhileStatement(WhileStatement* node) { | 151 void BytecodeGenerator::VisitWhileStatement(WhileStatement* stmt) { |
150 UNIMPLEMENTED(); | 152 UNIMPLEMENTED(); |
151 } | 153 } |
152 | 154 |
153 | 155 |
154 void BytecodeGenerator::VisitForStatement(ForStatement* node) { | 156 void BytecodeGenerator::VisitForStatement(ForStatement* stmt) { |
155 UNIMPLEMENTED(); | 157 UNIMPLEMENTED(); |
156 } | 158 } |
157 | 159 |
158 | 160 |
159 void BytecodeGenerator::VisitForInStatement(ForInStatement* node) { | 161 void BytecodeGenerator::VisitForInStatement(ForInStatement* stmt) { |
160 UNIMPLEMENTED(); | 162 UNIMPLEMENTED(); |
161 } | 163 } |
162 | 164 |
163 | 165 |
164 void BytecodeGenerator::VisitForOfStatement(ForOfStatement* node) { | 166 void BytecodeGenerator::VisitForOfStatement(ForOfStatement* stmt) { |
165 UNIMPLEMENTED(); | 167 UNIMPLEMENTED(); |
166 } | 168 } |
167 | 169 |
168 | 170 |
169 void BytecodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { | 171 void BytecodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) { |
170 UNIMPLEMENTED(); | 172 UNIMPLEMENTED(); |
171 } | 173 } |
172 | 174 |
173 | 175 |
174 void BytecodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { | 176 void BytecodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
175 UNIMPLEMENTED(); | 177 UNIMPLEMENTED(); |
176 } | 178 } |
177 | 179 |
178 | 180 |
179 void BytecodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { | 181 void BytecodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) { |
180 UNIMPLEMENTED(); | 182 UNIMPLEMENTED(); |
181 } | 183 } |
182 | 184 |
183 | 185 |
184 void BytecodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { | 186 void BytecodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) { |
185 UNIMPLEMENTED(); | 187 UNIMPLEMENTED(); |
186 } | 188 } |
187 | 189 |
188 | 190 |
189 void BytecodeGenerator::VisitClassLiteral(ClassLiteral* node) { | 191 void BytecodeGenerator::VisitClassLiteral(ClassLiteral* expr) { |
190 UNIMPLEMENTED(); | 192 UNIMPLEMENTED(); |
191 } | 193 } |
192 | 194 |
193 | 195 |
194 void BytecodeGenerator::VisitNativeFunctionLiteral( | 196 void BytecodeGenerator::VisitNativeFunctionLiteral( |
195 NativeFunctionLiteral* node) { | 197 NativeFunctionLiteral* expr) { |
196 UNIMPLEMENTED(); | 198 UNIMPLEMENTED(); |
197 } | 199 } |
198 | 200 |
199 | 201 |
200 void BytecodeGenerator::VisitConditional(Conditional* node) { UNIMPLEMENTED(); } | 202 void BytecodeGenerator::VisitConditional(Conditional* expr) { UNIMPLEMENTED(); } |
201 | 203 |
202 | 204 |
203 void BytecodeGenerator::VisitLiteral(Literal* expr) { | 205 void BytecodeGenerator::VisitLiteral(Literal* expr) { |
204 Handle<Object> value = expr->value(); | 206 Handle<Object> value = expr->value(); |
205 if (value->IsSmi()) { | 207 if (value->IsSmi()) { |
206 builder().LoadLiteral(Smi::cast(*value)); | 208 builder().LoadLiteral(Smi::cast(*value)); |
207 } else if (value->IsUndefined()) { | 209 } else if (value->IsUndefined()) { |
208 builder().LoadUndefined(); | 210 builder().LoadUndefined(); |
209 } else if (value->IsTrue()) { | 211 } else if (value->IsTrue()) { |
210 builder().LoadTrue(); | 212 builder().LoadTrue(); |
211 } else if (value->IsFalse()) { | 213 } else if (value->IsFalse()) { |
212 builder().LoadFalse(); | 214 builder().LoadFalse(); |
213 } else if (value->IsNull()) { | 215 } else if (value->IsNull()) { |
214 builder().LoadNull(); | 216 builder().LoadNull(); |
215 } else if (value->IsTheHole()) { | 217 } else if (value->IsTheHole()) { |
216 builder().LoadTheHole(); | 218 builder().LoadTheHole(); |
217 } else { | 219 } else { |
218 builder().LoadLiteral(value); | 220 builder().LoadLiteral(value); |
219 } | 221 } |
220 } | 222 } |
221 | 223 |
222 | 224 |
223 void BytecodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { | 225 void BytecodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) { |
224 UNIMPLEMENTED(); | 226 UNIMPLEMENTED(); |
225 } | 227 } |
226 | 228 |
227 | 229 |
228 void BytecodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { | 230 void BytecodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) { |
229 UNIMPLEMENTED(); | 231 UNIMPLEMENTED(); |
230 } | 232 } |
231 | 233 |
232 | 234 |
233 void BytecodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { | 235 void BytecodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) { |
234 UNIMPLEMENTED(); | 236 UNIMPLEMENTED(); |
235 } | 237 } |
236 | 238 |
237 | 239 |
238 void BytecodeGenerator::VisitVariableProxy(VariableProxy* proxy) { | 240 void BytecodeGenerator::VisitVariableProxy(VariableProxy* proxy) { |
239 Variable* variable = proxy->var(); | 241 Variable* variable = proxy->var(); |
240 switch (variable->location()) { | 242 switch (variable->location()) { |
241 case VariableLocation::LOCAL: { | 243 case VariableLocation::LOCAL: { |
242 Register source(variable->index()); | 244 Register source(variable->index()); |
243 builder().LoadAccumulatorWithRegister(source); | 245 builder().LoadAccumulatorWithRegister(source); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 } | 281 } |
280 case NAMED_PROPERTY: | 282 case NAMED_PROPERTY: |
281 case KEYED_PROPERTY: | 283 case KEYED_PROPERTY: |
282 case NAMED_SUPER_PROPERTY: | 284 case NAMED_SUPER_PROPERTY: |
283 case KEYED_SUPER_PROPERTY: | 285 case KEYED_SUPER_PROPERTY: |
284 UNIMPLEMENTED(); | 286 UNIMPLEMENTED(); |
285 } | 287 } |
286 } | 288 } |
287 | 289 |
288 | 290 |
289 void BytecodeGenerator::VisitYield(Yield* node) { UNIMPLEMENTED(); } | 291 void BytecodeGenerator::VisitYield(Yield* expr) { UNIMPLEMENTED(); } |
290 | 292 |
291 | 293 |
292 void BytecodeGenerator::VisitThrow(Throw* node) { UNIMPLEMENTED(); } | 294 void BytecodeGenerator::VisitThrow(Throw* expr) { UNIMPLEMENTED(); } |
293 | 295 |
294 | 296 |
295 void BytecodeGenerator::VisitProperty(Property* node) { UNIMPLEMENTED(); } | 297 void BytecodeGenerator::VisitProperty(Property* expr) { |
| 298 LhsKind property_kind = Property::GetAssignType(expr); |
| 299 FeedbackVectorICSlot slot = expr->PropertyFeedbackSlot(); |
| 300 switch (property_kind) { |
| 301 case VARIABLE: |
| 302 UNREACHABLE(); |
| 303 break; |
| 304 case NAMED_PROPERTY: { |
| 305 TemporaryRegisterScope temporary_register_scope(&builder_); |
| 306 Register name = temporary_register_scope.NewRegister(); |
| 307 builder().LoadLiteral(expr->key()->AsLiteral()->AsPropertyName()); |
| 308 builder().StoreAccumulatorInRegister(name); |
| 309 Visit(expr->obj()); |
| 310 builder().LoadNamedProperty(name, feedback_index(slot), language_mode()); |
| 311 break; |
| 312 } |
| 313 case KEYED_PROPERTY: { |
| 314 TemporaryRegisterScope temporary_register_scope(&builder_); |
| 315 Register key = temporary_register_scope.NewRegister(); |
| 316 Visit(expr->key()); |
| 317 builder().StoreAccumulatorInRegister(key); |
| 318 Visit(expr->obj()); |
| 319 builder().LoadKeyedProperty(key, feedback_index(slot), language_mode()); |
| 320 break; |
| 321 } |
| 322 case NAMED_SUPER_PROPERTY: |
| 323 case KEYED_SUPER_PROPERTY: |
| 324 UNIMPLEMENTED(); |
| 325 } |
| 326 } |
296 | 327 |
297 | 328 |
298 void BytecodeGenerator::VisitCall(Call* node) { UNIMPLEMENTED(); } | 329 void BytecodeGenerator::VisitCall(Call* expr) { UNIMPLEMENTED(); } |
299 | 330 |
300 | 331 |
301 void BytecodeGenerator::VisitCallNew(CallNew* node) { UNIMPLEMENTED(); } | 332 void BytecodeGenerator::VisitCallNew(CallNew* expr) { UNIMPLEMENTED(); } |
302 | 333 |
303 | 334 |
304 void BytecodeGenerator::VisitCallRuntime(CallRuntime* node) { UNIMPLEMENTED(); } | 335 void BytecodeGenerator::VisitCallRuntime(CallRuntime* expr) { UNIMPLEMENTED(); } |
305 | 336 |
306 | 337 |
307 void BytecodeGenerator::VisitUnaryOperation(UnaryOperation* node) { | 338 void BytecodeGenerator::VisitUnaryOperation(UnaryOperation* expr) { |
308 UNIMPLEMENTED(); | 339 UNIMPLEMENTED(); |
309 } | 340 } |
310 | 341 |
311 | 342 |
312 void BytecodeGenerator::VisitCountOperation(CountOperation* node) { | 343 void BytecodeGenerator::VisitCountOperation(CountOperation* expr) { |
313 UNIMPLEMENTED(); | 344 UNIMPLEMENTED(); |
314 } | 345 } |
315 | 346 |
316 | 347 |
317 void BytecodeGenerator::VisitBinaryOperation(BinaryOperation* binop) { | 348 void BytecodeGenerator::VisitBinaryOperation(BinaryOperation* binop) { |
318 switch (binop->op()) { | 349 switch (binop->op()) { |
319 case Token::COMMA: | 350 case Token::COMMA: |
320 case Token::OR: | 351 case Token::OR: |
321 case Token::AND: | 352 case Token::AND: |
322 UNIMPLEMENTED(); | 353 UNIMPLEMENTED(); |
323 break; | 354 break; |
324 default: | 355 default: |
325 VisitArithmeticExpression(binop); | 356 VisitArithmeticExpression(binop); |
326 break; | 357 break; |
327 } | 358 } |
328 } | 359 } |
329 | 360 |
330 | 361 |
331 void BytecodeGenerator::VisitCompareOperation(CompareOperation* node) { | 362 void BytecodeGenerator::VisitCompareOperation(CompareOperation* expr) { |
332 UNIMPLEMENTED(); | 363 UNIMPLEMENTED(); |
333 } | 364 } |
334 | 365 |
335 | 366 |
336 void BytecodeGenerator::VisitSpread(Spread* node) { UNREACHABLE(); } | 367 void BytecodeGenerator::VisitSpread(Spread* expr) { UNREACHABLE(); } |
337 | 368 |
338 | 369 |
339 void BytecodeGenerator::VisitEmptyParentheses(EmptyParentheses* node) { | 370 void BytecodeGenerator::VisitEmptyParentheses(EmptyParentheses* expr) { |
340 UNREACHABLE(); | 371 UNREACHABLE(); |
341 } | 372 } |
342 | 373 |
343 | 374 |
344 void BytecodeGenerator::VisitThisFunction(ThisFunction* node) { | 375 void BytecodeGenerator::VisitThisFunction(ThisFunction* expr) { |
345 UNIMPLEMENTED(); | 376 UNIMPLEMENTED(); |
346 } | 377 } |
347 | 378 |
348 | 379 |
349 void BytecodeGenerator::VisitSuperCallReference(SuperCallReference* node) { | 380 void BytecodeGenerator::VisitSuperCallReference(SuperCallReference* expr) { |
350 UNIMPLEMENTED(); | 381 UNIMPLEMENTED(); |
351 } | 382 } |
352 | 383 |
353 | 384 |
354 void BytecodeGenerator::VisitSuperPropertyReference( | 385 void BytecodeGenerator::VisitSuperPropertyReference( |
355 SuperPropertyReference* node) { | 386 SuperPropertyReference* expr) { |
356 UNIMPLEMENTED(); | 387 UNIMPLEMENTED(); |
357 } | 388 } |
358 | 389 |
359 | 390 |
360 void BytecodeGenerator::VisitArithmeticExpression(BinaryOperation* binop) { | 391 void BytecodeGenerator::VisitArithmeticExpression(BinaryOperation* binop) { |
361 Token::Value op = binop->op(); | 392 Token::Value op = binop->op(); |
362 Expression* left = binop->left(); | 393 Expression* left = binop->left(); |
363 Expression* right = binop->right(); | 394 Expression* right = binop->right(); |
364 | 395 |
365 TemporaryRegisterScope temporary_register_scope(&builder_); | 396 TemporaryRegisterScope temporary_register_scope(&builder_); |
366 Register temporary = temporary_register_scope.NewRegister(); | 397 Register temporary = temporary_register_scope.NewRegister(); |
367 | 398 |
368 Visit(left); | 399 Visit(left); |
369 builder().StoreAccumulatorInRegister(temporary); | 400 builder().StoreAccumulatorInRegister(temporary); |
370 Visit(right); | 401 Visit(right); |
371 builder().BinaryOperation(op, temporary); | 402 builder().BinaryOperation(op, temporary); |
372 } | 403 } |
373 | 404 |
| 405 |
| 406 LanguageMode BytecodeGenerator::language_mode() const { |
| 407 return info()->language_mode(); |
| 408 } |
| 409 |
| 410 |
| 411 int BytecodeGenerator::feedback_index(FeedbackVectorICSlot slot) const { |
| 412 return info()->feedback_vector()->GetIndex(slot); |
| 413 } |
| 414 |
374 } // namespace interpreter | 415 } // namespace interpreter |
375 } // namespace internal | 416 } // namespace internal |
376 } // namespace v8 | 417 } // namespace v8 |
OLD | NEW |