| 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 "src/ast/scopes.h" | 7 #include "src/ast/scopes.h" |
| 8 #include "src/code-stubs.h" | 8 #include "src/code-stubs.h" |
| 9 #include "src/compiler.h" | 9 #include "src/compiler.h" |
| 10 #include "src/interpreter/bytecode-register-allocator.h" | 10 #include "src/interpreter/bytecode-register-allocator.h" |
| (...skipping 1276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1287 Compiler::GetSharedFunctionInfo(expr, info()->script(), info()); | 1287 Compiler::GetSharedFunctionInfo(expr, info()->script(), info()); |
| 1288 if (shared_info.is_null()) { | 1288 if (shared_info.is_null()) { |
| 1289 return SetStackOverflow(); | 1289 return SetStackOverflow(); |
| 1290 } | 1290 } |
| 1291 uint8_t flags = CreateClosureFlags::Encode(expr->pretenure(), | 1291 uint8_t flags = CreateClosureFlags::Encode(expr->pretenure(), |
| 1292 scope()->is_function_scope()); | 1292 scope()->is_function_scope()); |
| 1293 builder()->CreateClosure(shared_info, flags); | 1293 builder()->CreateClosure(shared_info, flags); |
| 1294 execution_result()->SetResultInAccumulator(); | 1294 execution_result()->SetResultInAccumulator(); |
| 1295 } | 1295 } |
| 1296 | 1296 |
| 1297 void BytecodeGenerator::VisitClassLiteral(ClassLiteral* expr) { | |
| 1298 if (expr->scope()->ContextLocalCount() > 0) { | |
| 1299 VisitNewLocalBlockContext(expr->scope()); | |
| 1300 ContextScope scope(this, expr->scope()); | |
| 1301 VisitDeclarations(expr->scope()->declarations()); | |
| 1302 VisitClassLiteralContents(expr); | |
| 1303 } else { | |
| 1304 VisitDeclarations(expr->scope()->declarations()); | |
| 1305 VisitClassLiteralContents(expr); | |
| 1306 } | |
| 1307 } | |
| 1308 | |
| 1309 void BytecodeGenerator::VisitClassLiteralContents(ClassLiteral* expr) { | |
| 1310 VisitClassLiteralForRuntimeDefinition(expr); | |
| 1311 | |
| 1312 // Load the "prototype" from the constructor. | |
| 1313 register_allocator()->PrepareForConsecutiveAllocations(2); | |
| 1314 Register literal = register_allocator()->NextConsecutiveRegister(); | |
| 1315 Register prototype = register_allocator()->NextConsecutiveRegister(); | |
| 1316 Handle<String> name = isolate()->factory()->prototype_string(); | |
| 1317 FeedbackVectorSlot slot = expr->PrototypeSlot(); | |
| 1318 builder() | |
| 1319 ->StoreAccumulatorInRegister(literal) | |
| 1320 .LoadNamedProperty(literal, name, feedback_index(slot)) | |
| 1321 .StoreAccumulatorInRegister(prototype); | |
| 1322 | |
| 1323 VisitClassLiteralProperties(expr, literal, prototype); | |
| 1324 builder()->CallRuntime(Runtime::kToFastProperties, literal, 1); | |
| 1325 // Assign to class variable. | |
| 1326 if (expr->class_variable_proxy() != nullptr) { | |
| 1327 Variable* var = expr->class_variable_proxy()->var(); | |
| 1328 FeedbackVectorSlot slot = expr->NeedsProxySlot() | |
| 1329 ? expr->ProxySlot() | |
| 1330 : FeedbackVectorSlot::Invalid(); | |
| 1331 VisitVariableAssignment(var, Token::INIT, slot); | |
| 1332 } | |
| 1333 execution_result()->SetResultInAccumulator(); | |
| 1334 } | |
| 1335 | |
| 1336 void BytecodeGenerator::VisitClassLiteralForRuntimeDefinition( | |
| 1337 ClassLiteral* expr) { | |
| 1338 AccumulatorResultScope result_scope(this); | |
| 1339 register_allocator()->PrepareForConsecutiveAllocations(4); | |
| 1340 Register extends = register_allocator()->NextConsecutiveRegister(); | |
| 1341 Register constructor = register_allocator()->NextConsecutiveRegister(); | |
| 1342 Register start_position = register_allocator()->NextConsecutiveRegister(); | |
| 1343 Register end_position = register_allocator()->NextConsecutiveRegister(); | |
| 1344 | |
| 1345 VisitForAccumulatorValueOrTheHole(expr->extends()); | |
| 1346 builder()->StoreAccumulatorInRegister(extends); | |
| 1347 | |
| 1348 VisitForAccumulatorValue(expr->constructor()); | |
| 1349 builder() | |
| 1350 ->StoreAccumulatorInRegister(constructor) | |
| 1351 .LoadLiteral(Smi::FromInt(expr->start_position())) | |
| 1352 .StoreAccumulatorInRegister(start_position) | |
| 1353 .LoadLiteral(Smi::FromInt(expr->end_position())) | |
| 1354 .StoreAccumulatorInRegister(end_position) | |
| 1355 .CallRuntime(Runtime::kDefineClass, extends, 4); | |
| 1356 result_scope.SetResultInAccumulator(); | |
| 1357 } | |
| 1358 | |
| 1359 void BytecodeGenerator::VisitClassLiteralProperties(ClassLiteral* expr, | |
| 1360 Register literal, | |
| 1361 Register prototype) { | |
| 1362 RegisterAllocationScope register_scope(this); | |
| 1363 register_allocator()->PrepareForConsecutiveAllocations(5); | |
| 1364 Register receiver = register_allocator()->NextConsecutiveRegister(); | |
| 1365 Register key = register_allocator()->NextConsecutiveRegister(); | |
| 1366 Register value = register_allocator()->NextConsecutiveRegister(); | |
| 1367 Register attr = register_allocator()->NextConsecutiveRegister(); | |
| 1368 Register set_function_name = register_allocator()->NextConsecutiveRegister(); | |
| 1369 | |
| 1370 bool attr_assigned = false; | |
| 1371 Register old_receiver = Register::invalid_value(); | |
| 1372 | |
| 1373 // Create nodes to store method values into the literal. | |
| 1374 for (int i = 0; i < expr->properties()->length(); i++) { | |
| 1375 ObjectLiteral::Property* property = expr->properties()->at(i); | |
| 1376 | |
| 1377 // Set-up receiver. | |
| 1378 Register new_receiver = property->is_static() ? literal : prototype; | |
| 1379 if (new_receiver != old_receiver) { | |
| 1380 builder()->MoveRegister(new_receiver, receiver); | |
| 1381 old_receiver = new_receiver; | |
| 1382 } | |
| 1383 | |
| 1384 VisitForAccumulatorValue(property->key()); | |
| 1385 builder()->CastAccumulatorToName().StoreAccumulatorInRegister(key); | |
| 1386 // The static prototype property is read only. We handle the non computed | |
| 1387 // property name case in the parser. Since this is the only case where we | |
| 1388 // need to check for an own read only property we special case this so we do | |
| 1389 // not need to do this for every property. | |
| 1390 if (property->is_static() && property->is_computed_name()) { | |
| 1391 VisitClassLiteralStaticPrototypeWithComputedName(key); | |
| 1392 } | |
| 1393 VisitForAccumulatorValue(property->value()); | |
| 1394 builder()->StoreAccumulatorInRegister(value); | |
| 1395 | |
| 1396 VisitSetHomeObject(value, receiver, property); | |
| 1397 | |
| 1398 if (!attr_assigned) { | |
| 1399 builder() | |
| 1400 ->LoadLiteral(Smi::FromInt(DONT_ENUM)) | |
| 1401 .StoreAccumulatorInRegister(attr); | |
| 1402 attr_assigned = true; | |
| 1403 } | |
| 1404 | |
| 1405 switch (property->kind()) { | |
| 1406 case ObjectLiteral::Property::CONSTANT: | |
| 1407 case ObjectLiteral::Property::MATERIALIZED_LITERAL: | |
| 1408 case ObjectLiteral::Property::PROTOTYPE: | |
| 1409 // Invalid properties for ES6 classes. | |
| 1410 UNREACHABLE(); | |
| 1411 break; | |
| 1412 case ObjectLiteral::Property::COMPUTED: { | |
| 1413 builder() | |
| 1414 ->LoadLiteral(Smi::FromInt(property->NeedsSetFunctionName())) | |
| 1415 .StoreAccumulatorInRegister(set_function_name); | |
| 1416 builder()->CallRuntime(Runtime::kDefineDataPropertyInLiteral, receiver, | |
| 1417 5); | |
| 1418 break; | |
| 1419 } | |
| 1420 case ObjectLiteral::Property::GETTER: { | |
| 1421 builder()->CallRuntime(Runtime::kDefineGetterPropertyUnchecked, | |
| 1422 receiver, 4); | |
| 1423 break; | |
| 1424 } | |
| 1425 case ObjectLiteral::Property::SETTER: { | |
| 1426 builder()->CallRuntime(Runtime::kDefineSetterPropertyUnchecked, | |
| 1427 receiver, 4); | |
| 1428 break; | |
| 1429 } | |
| 1430 } | |
| 1431 } | |
| 1432 } | |
| 1433 | |
| 1434 void BytecodeGenerator::VisitClassLiteralStaticPrototypeWithComputedName( | |
| 1435 Register key) { | |
| 1436 BytecodeLabel done; | |
| 1437 builder() | |
| 1438 ->LoadLiteral(isolate()->factory()->prototype_string()) | |
| 1439 .CompareOperation(Token::Value::EQ_STRICT, key) | |
| 1440 .JumpIfFalse(&done) | |
| 1441 .CallRuntime(Runtime::kThrowStaticPrototypeError, Register(0), 0) | |
| 1442 .Bind(&done); | |
| 1443 } | |
| 1444 | |
| 1445 void BytecodeGenerator::VisitNativeFunctionLiteral( | 1297 void BytecodeGenerator::VisitNativeFunctionLiteral( |
| 1446 NativeFunctionLiteral* expr) { | 1298 NativeFunctionLiteral* expr) { |
| 1447 // Find or build a shared function info for the native function template. | 1299 // Find or build a shared function info for the native function template. |
| 1448 Handle<SharedFunctionInfo> shared_info = | 1300 Handle<SharedFunctionInfo> shared_info = |
| 1449 Compiler::GetSharedFunctionInfoForNative(expr->extension(), expr->name()); | 1301 Compiler::GetSharedFunctionInfoForNative(expr->extension(), expr->name()); |
| 1450 builder()->CreateClosure(shared_info, NOT_TENURED); | 1302 builder()->CreateClosure(shared_info, NOT_TENURED); |
| 1451 execution_result()->SetResultInAccumulator(); | 1303 execution_result()->SetResultInAccumulator(); |
| 1452 } | 1304 } |
| 1453 | 1305 |
| 1454 void BytecodeGenerator::VisitDoExpression(DoExpression* expr) { | 1306 void BytecodeGenerator::VisitDoExpression(DoExpression* expr) { |
| (...skipping 1705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3160 return execution_context()->scope()->language_mode(); | 3012 return execution_context()->scope()->language_mode(); |
| 3161 } | 3013 } |
| 3162 | 3014 |
| 3163 int BytecodeGenerator::feedback_index(FeedbackVectorSlot slot) const { | 3015 int BytecodeGenerator::feedback_index(FeedbackVectorSlot slot) const { |
| 3164 return TypeFeedbackVector::GetIndex(slot); | 3016 return TypeFeedbackVector::GetIndex(slot); |
| 3165 } | 3017 } |
| 3166 | 3018 |
| 3167 } // namespace interpreter | 3019 } // namespace interpreter |
| 3168 } // namespace internal | 3020 } // namespace internal |
| 3169 } // namespace v8 | 3021 } // namespace v8 |
| OLD | NEW |