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

Unified Diff: src/interpreter/bytecode-generator.cc

Issue 1689573004: [interpreter] Support for ES6 super keyword. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix super stores following redux. Created 4 years, 10 months 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 side-by-side diff with in-line comments
Download patch
Index: src/interpreter/bytecode-generator.cc
diff --git a/src/interpreter/bytecode-generator.cc b/src/interpreter/bytecode-generator.cc
index da04906d20a6345490978e552ba2ee1ca5bd69da..e963e04da34ddfd63ecbcb12f474cbd7ad2cd163 100644
--- a/src/interpreter/bytecode-generator.cc
+++ b/src/interpreter/bytecode-generator.cc
@@ -1050,9 +1050,27 @@ void BytecodeGenerator::VisitForInAssignment(Expression* expr,
language_mode());
break;
}
- case NAMED_SUPER_PROPERTY:
- case KEYED_SUPER_PROPERTY:
- UNIMPLEMENTED();
+ case NAMED_SUPER_PROPERTY: {
+ RegisterAllocationScope register_scope(this);
+ Register value = register_allocator()->NewRegister();
+ builder()->StoreAccumulatorInRegister(value);
+ Register receiver = PrepareNamedSuperPropertyArguments(
+ property->obj()->AsSuperPropertyReference(),
+ property->key()->AsLiteral()->AsPropertyName());
+ builder()->LoadAccumulatorWithRegister(value);
+ BuildNamedSuperPropertyStore(receiver);
+ break;
+ }
+ case KEYED_SUPER_PROPERTY: {
+ RegisterAllocationScope register_scope(this);
+ Register value = register_allocator()->NewRegister();
+ builder()->StoreAccumulatorInRegister(value);
+ Register receiver = PrepareKeyedSuperPropertyArguments(
+ property->obj()->AsSuperPropertyReference(), property->key());
+ builder()->LoadAccumulatorWithRegister(value);
+ BuildKeyedSuperPropertyStore(receiver);
+ break;
+ }
}
}
@@ -1495,9 +1513,19 @@ void BytecodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
if (literal_key->value()->IsInternalizedString()) {
if (property->emit_store()) {
VisitForAccumulatorValue(property->value());
- builder()->StoreNamedProperty(
- literal, literal_key->AsPropertyName(),
- feedback_index(property->GetSlot(0)), language_mode());
+ if (FunctionLiteral::NeedsHomeObject(property->value())) {
+ RegisterAllocationScope register_scope(this);
+ Register value = register_allocator()->NewRegister();
+ builder()->StoreAccumulatorInRegister(value);
+ builder()->StoreNamedProperty(
+ literal, literal_key->AsPropertyName(),
+ feedback_index(property->GetSlot(0)), language_mode());
+ VisitSetHomeObject(value, literal, property, 1);
+ } else {
+ builder()->StoreNamedProperty(
+ literal, literal_key->AsPropertyName(),
+ feedback_index(property->GetSlot(0)), language_mode());
+ }
} else {
VisitForEffect(property->value());
}
@@ -1785,6 +1813,84 @@ Register BytecodeGenerator::VisitVariableLoadForRegisterValue(
return register_scope.ResultRegister();
}
+template <size_t N>
+void BytecodeGenerator::InitializeWithConsecutiveRegisters(
+ Register (&registers)[N]) {
+ register_allocator()->PrepareForConsecutiveAllocations(N);
+ for (size_t i = 0; i < N; i++) {
+ registers[i] = register_allocator()->NextConsecutiveRegister();
+ }
+}
+
+Register BytecodeGenerator::PrepareNamedSuperPropertyArguments(
+ SuperPropertyReference* super_property, Handle<Name> name) {
+ Register args[4];
+ register_allocator()->PrepareForConsecutiveAllocations(arraysize(args));
rmcilroy 2016/02/12 14:56:42 unecessary, right? (InitializeWithConsecutiveRegis
oth 2016/02/12 16:50:33 Done.
+
+ InitializeWithConsecutiveRegisters(args);
rmcilroy 2016/02/12 14:56:42 nit - move below Register args[4]; and add whitesp
oth 2016/02/12 16:50:33 Done.
+ VisitForAccumulatorValue(super_property->this_var());
+ builder()->StoreAccumulatorInRegister(args[0]);
+ VisitForAccumulatorValue(super_property->home_object());
+ builder()->StoreAccumulatorInRegister(args[1]);
+ builder()
+ ->LoadLiteral(name)
+ .StoreAccumulatorInRegister(args[2])
+ .LoadLiteral(Smi::FromInt(static_cast<int>(language_mode())))
+ .StoreAccumulatorInRegister(args[3]);
rmcilroy 2016/02/12 14:56:42 As discussed, please drop storing language mode he
oth 2016/02/12 16:50:33 Done.
+ return args[0];
+}
+
+Register BytecodeGenerator::PrepareKeyedSuperPropertyArguments(
+ SuperPropertyReference* super_property, Expression* key) {
+ Register args[4];
+ register_allocator()->PrepareForConsecutiveAllocations(arraysize(args));
rmcilroy 2016/02/12 14:56:42 ditto
oth 2016/02/12 16:50:33 Done.
+
+ InitializeWithConsecutiveRegisters(args);
rmcilroy 2016/02/12 14:56:42 ditto
oth 2016/02/12 16:50:33 Done.
+ VisitForAccumulatorValue(super_property->this_var());
+ builder()->StoreAccumulatorInRegister(args[0]);
+ VisitForAccumulatorValue(super_property->home_object());
+ builder()->StoreAccumulatorInRegister(args[1]);
+ VisitForAccumulatorValue(key);
+ builder()->StoreAccumulatorInRegister(args[2]);
+ // For load operations args[3] is the language mode, for stores
+ // it is the value.
+ return args[0];
+}
+
+void BytecodeGenerator::BuildNamedSuperPropertyLoad(Register receiver) {
+ Register mode(receiver.index() + 3);
+ builder()
+ ->LoadLiteral(Smi::FromInt(static_cast<int>(language_mode())))
+ .StoreAccumulatorInRegister(mode);
+ builder()->CallRuntime(Runtime::kLoadFromSuper, receiver, 4);
+}
+
+void BytecodeGenerator::BuildKeyedSuperPropertyLoad(Register receiver) {
+ Register mode(receiver.index() + 3);
+ builder()
+ ->LoadLiteral(Smi::FromInt(static_cast<int>(language_mode())))
+ .StoreAccumulatorInRegister(mode);
+ builder()->CallRuntime(Runtime::kLoadKeyedFromSuper, receiver, 4);
+}
+
+void BytecodeGenerator::BuildNamedSuperPropertyStore(Register receiver) {
+ Register value(receiver.index() + 3);
+ builder()->StoreAccumulatorInRegister(value);
+ Runtime::FunctionId function_id = is_strict(language_mode())
+ ? Runtime::kStoreToSuper_Strict
+ : Runtime::kStoreToSuper_Sloppy;
+ builder()->CallRuntime(function_id, receiver, 4);
+}
+
+void BytecodeGenerator::BuildKeyedSuperPropertyStore(Register receiver) {
+ Register value(receiver.index() + 3);
+ builder()->StoreAccumulatorInRegister(value);
+ Runtime::FunctionId function_id = is_strict(language_mode())
+ ? Runtime::kStoreKeyedToSuper_Strict
+ : Runtime::kStoreKeyedToSuper_Sloppy;
+ builder()->CallRuntime(function_id, receiver, 4);
+}
+
void BytecodeGenerator::BuildThrowIfHole(Handle<String> name) {
Register name_reg = register_allocator()->NewRegister();
BytecodeLabel end_label;
@@ -1993,7 +2099,7 @@ void BytecodeGenerator::VisitVariableAssignment(Variable* variable,
void BytecodeGenerator::VisitAssignment(Assignment* expr) {
- DCHECK(expr->target()->IsValidReferenceExpression());
+ DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
Register object, key;
Handle<String> name;
@@ -2024,9 +2130,17 @@ void BytecodeGenerator::VisitAssignment(Assignment* expr) {
}
break;
}
- case NAMED_SUPER_PROPERTY:
- case KEYED_SUPER_PROPERTY:
- UNIMPLEMENTED();
+ case NAMED_SUPER_PROPERTY: {
+ object = PrepareNamedSuperPropertyArguments(
+ property->obj()->AsSuperPropertyReference(),
+ property->key()->AsLiteral()->AsPropertyName());
+ break;
+ }
+ case KEYED_SUPER_PROPERTY: {
+ object = PrepareKeyedSuperPropertyArguments(
+ property->obj()->AsSuperPropertyReference(), property->key());
+ break;
+ }
}
// Evaluate the value and potentially handle compound assignments by loading
@@ -2059,10 +2173,18 @@ void BytecodeGenerator::VisitAssignment(Assignment* expr) {
.StoreAccumulatorInRegister(old_value);
break;
}
- case NAMED_SUPER_PROPERTY:
- case KEYED_SUPER_PROPERTY:
- UNIMPLEMENTED();
+ case NAMED_SUPER_PROPERTY: {
+ old_value = register_allocator()->NewRegister();
+ BuildNamedSuperPropertyLoad(object);
+ builder()->StoreAccumulatorInRegister(old_value);
+ break;
+ }
+ case KEYED_SUPER_PROPERTY: {
+ old_value = register_allocator()->NewRegister();
+ BuildKeyedSuperPropertyLoad(object);
+ builder()->StoreAccumulatorInRegister(old_value);
break;
+ }
}
VisitForAccumulatorValue(expr->value());
builder()->BinaryOperation(expr->binary_op(), old_value,
@@ -2089,9 +2211,14 @@ void BytecodeGenerator::VisitAssignment(Assignment* expr) {
builder()->StoreKeyedProperty(object, key, feedback_index(slot),
language_mode());
break;
- case NAMED_SUPER_PROPERTY:
- case KEYED_SUPER_PROPERTY:
- UNIMPLEMENTED();
+ case NAMED_SUPER_PROPERTY: {
+ BuildNamedSuperPropertyStore(object);
+ break;
+ }
+ case KEYED_SUPER_PROPERTY: {
+ BuildKeyedSuperPropertyStore(object);
+ break;
+ }
}
execution_result()->SetResultInAccumulator();
}
@@ -2129,25 +2256,54 @@ void BytecodeGenerator::VisitPropertyLoad(Register obj, Property* expr) {
break;
}
case NAMED_SUPER_PROPERTY:
+ VisitNamedSuperPropertyLoad(expr, Register::invalid_value());
+ break;
case KEYED_SUPER_PROPERTY:
- UNIMPLEMENTED();
+ VisitKeyedSuperPropertyLoad(expr, Register::invalid_value());
+ break;
}
execution_result()->SetResultInAccumulator();
}
-
void BytecodeGenerator::VisitPropertyLoadForAccumulator(Register obj,
Property* expr) {
AccumulatorResultScope result_scope(this);
VisitPropertyLoad(obj, expr);
}
+void BytecodeGenerator::VisitNamedSuperPropertyLoad(Property* property,
+ Register receiver_out) {
+ RegisterAllocationScope register_scope(this);
+ Register receiver = PrepareNamedSuperPropertyArguments(
+ property->obj()->AsSuperPropertyReference(),
+ property->key()->AsLiteral()->AsPropertyName());
+ if (receiver_out.is_valid()) {
+ builder()->MoveRegister(receiver, receiver_out);
+ }
+ BuildNamedSuperPropertyLoad(receiver);
+}
-void BytecodeGenerator::VisitProperty(Property* expr) {
- Register obj = VisitForRegisterValue(expr->obj());
- VisitPropertyLoad(obj, expr);
+void BytecodeGenerator::VisitKeyedSuperPropertyLoad(Property* property,
+ Register receiver_out) {
+ RegisterAllocationScope register_scope(this);
+ Register receiver = PrepareKeyedSuperPropertyArguments(
+ property->obj()->AsSuperPropertyReference(), property->key());
+ if (receiver_out.is_valid()) {
+ builder()->MoveRegister(receiver, receiver_out);
+ }
+ BuildKeyedSuperPropertyLoad(receiver);
}
+void BytecodeGenerator::VisitProperty(Property* expr) {
+ LhsKind property_kind = Property::GetAssignType(expr);
+ if (property_kind != NAMED_SUPER_PROPERTY &&
+ property_kind != KEYED_SUPER_PROPERTY) {
+ Register obj = VisitForRegisterValue(expr->obj());
+ VisitPropertyLoad(obj, expr);
+ } else {
+ VisitPropertyLoad(Register::invalid_value(), expr);
+ }
+}
Register BytecodeGenerator::VisitArguments(ZoneList<Expression*>* args) {
if (args->length() == 0) {
@@ -2181,11 +2337,14 @@ Register BytecodeGenerator::VisitArguments(ZoneList<Expression*>* args) {
return first_arg;
}
-
void BytecodeGenerator::VisitCall(Call* expr) {
Expression* callee_expr = expr->expression();
Call::CallType call_type = expr->GetCallType(isolate());
+ if (call_type == Call::SUPER_CALL) {
+ return VisitCallSuper(expr);
+ }
+
// Prepare the callee and the receiver to the function call. This depends on
// the semantics of the underlying call type.
@@ -2243,10 +2402,21 @@ void BytecodeGenerator::VisitCall(Call* expr) {
builder()->StoreAccumulatorInRegister(callee);
break;
}
- case Call::NAMED_SUPER_PROPERTY_CALL:
- case Call::KEYED_SUPER_PROPERTY_CALL:
+ case Call::NAMED_SUPER_PROPERTY_CALL: {
+ Property* property = callee_expr->AsProperty();
+ VisitNamedSuperPropertyLoad(property, receiver);
+ builder()->StoreAccumulatorInRegister(callee);
+ break;
+ }
+ case Call::KEYED_SUPER_PROPERTY_CALL: {
+ Property* property = callee_expr->AsProperty();
+ VisitKeyedSuperPropertyLoad(property, receiver);
+ builder()->StoreAccumulatorInRegister(callee);
+ break;
+ }
case Call::SUPER_CALL:
- UNIMPLEMENTED();
+ UNREACHABLE();
+ break;
}
// Evaluate all arguments to the function call and store in sequential
@@ -2290,6 +2460,32 @@ void BytecodeGenerator::VisitCall(Call* expr) {
execution_result()->SetResultInAccumulator();
}
+void BytecodeGenerator::VisitCallSuper(Call* expr) {
+ RegisterAllocationScope register_scope(this);
+ SuperCallReference* super = expr->expression()->AsSuperCallReference();
+
+ // Prepare the constructor to the super call.
+ Register this_function = register_allocator()->NewRegister();
+ VisitForAccumulatorValue(super->this_function_var());
+ builder()
+ ->StoreAccumulatorInRegister(this_function)
+ .CallRuntime(Runtime::kInlineGetSuperConstructor, this_function, 1);
+
+ Register constructor = this_function; // Re-use dead this_function register.
+ builder()->StoreAccumulatorInRegister(constructor);
+
+ ZoneList<Expression*>* args = expr->arguments();
+ Register first_arg = VisitArguments(args);
+
+ // The new target is loaded into the accumulator from the
+ // {new.target} variable.
+ VisitForAccumulatorValue(super->new_target_var());
+
+ // Call construct.
+ builder()->SetExpressionPosition(expr);
+ builder()->New(constructor, first_arg, args->length());
+ execution_result()->SetResultInAccumulator();
+}
void BytecodeGenerator::VisitCallNew(CallNew* expr) {
Register constructor = register_allocator()->NewRegister();
@@ -2298,8 +2494,13 @@ void BytecodeGenerator::VisitCallNew(CallNew* expr) {
ZoneList<Expression*>* args = expr->arguments();
Register first_arg = VisitArguments(args);
+
builder()->SetExpressionPosition(expr);
- builder()->New(constructor, first_arg, args->length());
+ // The accumulator holds new target which is the same as the
+ // constructor for CallNew.
+ builder()
+ ->LoadAccumulatorWithRegister(constructor)
+ .New(constructor, first_arg, args->length());
execution_result()->SetResultInAccumulator();
}
@@ -2453,7 +2654,7 @@ void BytecodeGenerator::VisitCountOperation(CountOperation* expr) {
bool is_postfix = expr->is_postfix();
// Evaluate LHS expression and get old value.
- Register obj, key, old_value;
+ Register object, key, old_value;
Handle<String> name;
switch (assign_type) {
case VARIABLE: {
@@ -2464,26 +2665,36 @@ void BytecodeGenerator::VisitCountOperation(CountOperation* expr) {
}
case NAMED_PROPERTY: {
FeedbackVectorSlot slot = property->PropertyFeedbackSlot();
- obj = VisitForRegisterValue(property->obj());
+ object = VisitForRegisterValue(property->obj());
name = property->key()->AsLiteral()->AsPropertyName();
- builder()->LoadNamedProperty(obj, name, feedback_index(slot),
+ builder()->LoadNamedProperty(object, name, feedback_index(slot),
language_mode());
break;
}
case KEYED_PROPERTY: {
FeedbackVectorSlot slot = property->PropertyFeedbackSlot();
- obj = VisitForRegisterValue(property->obj());
+ object = VisitForRegisterValue(property->obj());
// Use visit for accumulator here since we need the key in the accumulator
// for the LoadKeyedProperty.
key = register_allocator()->NewRegister();
VisitForAccumulatorValue(property->key());
builder()->StoreAccumulatorInRegister(key).LoadKeyedProperty(
- obj, feedback_index(slot), language_mode());
+ object, feedback_index(slot), language_mode());
+ break;
+ }
+ case NAMED_SUPER_PROPERTY: {
+ object = PrepareNamedSuperPropertyArguments(
+ property->obj()->AsSuperPropertyReference(),
+ property->key()->AsLiteral()->AsPropertyName());
+ BuildNamedSuperPropertyLoad(object);
+ break;
+ }
+ case KEYED_SUPER_PROPERTY: {
+ object = PrepareKeyedSuperPropertyArguments(
+ property->obj()->AsSuperPropertyReference(), property->key());
+ BuildKeyedSuperPropertyLoad(object);
break;
}
- case NAMED_SUPER_PROPERTY:
- case KEYED_SUPER_PROPERTY:
- UNIMPLEMENTED();
}
// Convert old value into a number.
@@ -2509,18 +2720,23 @@ void BytecodeGenerator::VisitCountOperation(CountOperation* expr) {
break;
}
case NAMED_PROPERTY: {
- builder()->StoreNamedProperty(obj, name, feedback_index(feedback_slot),
+ builder()->StoreNamedProperty(object, name, feedback_index(feedback_slot),
language_mode());
break;
}
case KEYED_PROPERTY: {
- builder()->StoreKeyedProperty(obj, key, feedback_index(feedback_slot),
+ builder()->StoreKeyedProperty(object, key, feedback_index(feedback_slot),
language_mode());
break;
}
- case NAMED_SUPER_PROPERTY:
- case KEYED_SUPER_PROPERTY:
- UNIMPLEMENTED();
+ case NAMED_SUPER_PROPERTY: {
+ BuildNamedSuperPropertyStore(object);
+ break;
+ }
+ case KEYED_SUPER_PROPERTY: {
+ BuildKeyedSuperPropertyStore(object);
+ break;
+ }
}
// Restore old value for postfix expressions.
@@ -2580,13 +2796,15 @@ void BytecodeGenerator::VisitThisFunction(ThisFunction* expr) {
void BytecodeGenerator::VisitSuperCallReference(SuperCallReference* expr) {
- UNIMPLEMENTED();
+ // Handled by VisitCall().
+ UNREACHABLE();
}
void BytecodeGenerator::VisitSuperPropertyReference(
SuperPropertyReference* expr) {
- UNIMPLEMENTED();
+ builder()->CallRuntime(Runtime::kThrowUnsupportedSuperError, Register(0), 0);
+ execution_result()->SetResultInAccumulator();
}
@@ -2759,14 +2977,17 @@ void BytecodeGenerator::VisitObjectLiteralAccessor(
}
}
-
void BytecodeGenerator::VisitSetHomeObject(Register value, Register home_object,
ObjectLiteralProperty* property,
int slot_number) {
Expression* expr = property->value();
- if (!FunctionLiteral::NeedsHomeObject(expr)) return;
-
- UNIMPLEMENTED();
+ if (FunctionLiteral::NeedsHomeObject(expr)) {
+ Handle<Name> name = isolate()->factory()->home_object_symbol();
+ FeedbackVectorSlot slot = property->GetSlot(slot_number);
+ builder()
+ ->LoadAccumulatorWithRegister(home_object)
+ .StoreNamedProperty(value, name, feedback_index(slot), language_mode());
+ }
}
@@ -2799,9 +3020,6 @@ void BytecodeGenerator::VisitRestArgumentsArray(Variable* rest) {
void BytecodeGenerator::VisitThisFunctionVariable(Variable* variable) {
if (variable == nullptr) return;
- // TODO(rmcilroy): Remove once we have tests which exercise this code path.
- UNIMPLEMENTED();
-
// Store the closure we were called with in the given variable.
builder()->LoadAccumulatorWithRegister(Register::function_closure());
VisitVariableAssignment(variable, Token::INIT, FeedbackVectorSlot::Invalid());

Powered by Google App Engine
This is Rietveld 408576698