Chromium Code Reviews| Index: src/interpreter/bytecode-array-builder.cc |
| diff --git a/src/interpreter/bytecode-array-builder.cc b/src/interpreter/bytecode-array-builder.cc |
| index f3a1b1ea0803959990370da458ae10da87d8ec54..db21dbceeb025aa420c256a1f0686c1e5b3b20ec 100644 |
| --- a/src/interpreter/bytecode-array-builder.cc |
| +++ b/src/interpreter/bytecode-array-builder.cc |
| @@ -76,9 +76,56 @@ Handle<BytecodeArray> BytecodeArrayBuilder::ToBytecodeArray() { |
| } |
| -BytecodeArrayBuilder& BytecodeArrayBuilder::BinaryOperation(Token::Value binop, |
| +template <size_t N> |
| +void BytecodeArrayBuilder::Output(uint8_t(&bytes)[N]) { |
| + DCHECK_EQ(Bytecodes::NumberOfOperands(Bytecodes::FromByte(bytes[0])), N - 1); |
| + for (int i = 1; i < static_cast<int>(N); i++) { |
| + DCHECK(OperandIsValid(Bytecodes::FromByte(bytes[0]), i - 1, bytes[i])); |
| + } |
| + bytecodes()->insert(bytecodes()->end(), bytes, bytes + N); |
| +} |
| + |
| + |
| +void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0, |
| + uint8_t operand1, uint8_t operand2) { |
| + uint8_t bytes[] = {Bytecodes::ToByte(bytecode), operand0, operand1, operand2}; |
| + Output(bytes); |
| +} |
| + |
| + |
| +void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0, |
| + uint8_t operand1) { |
| + uint8_t bytes[] = {Bytecodes::ToByte(bytecode), operand0, operand1}; |
| + Output(bytes); |
| +} |
| + |
| + |
| +void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0) { |
| + uint8_t bytes[] = {Bytecodes::ToByte(bytecode), operand0}; |
| + Output(bytes); |
| +} |
| + |
| + |
| +void BytecodeArrayBuilder::Output(Bytecode bytecode) { |
| + uint8_t bytes[] = {Bytecodes::ToByte(bytecode)}; |
| + Output(bytes); |
| +} |
| + |
| + |
| +BytecodeArrayBuilder& BytecodeArrayBuilder::BinaryOperation(Token::Value op, |
| Register reg) { |
| - Output(BytecodeForBinaryOperation(binop), reg.ToOperand()); |
| + Output(BytecodeForBinaryOperation(op), reg.ToOperand()); |
| + return *this; |
| +} |
| + |
| + |
| +BytecodeArrayBuilder& BytecodeArrayBuilder::CompareOperation( |
| + Token::Value op, Register reg, LanguageMode language_mode) { |
| + if (!is_sloppy(language_mode)) { |
| + UNIMPLEMENTED(); |
| + } |
| + |
| + Output(BytecodeForCompareOperation(op), reg.ToOperand()); |
| return *this; |
| } |
| @@ -99,7 +146,7 @@ BytecodeArrayBuilder& BytecodeArrayBuilder::LoadLiteral( |
| BytecodeArrayBuilder& BytecodeArrayBuilder::LoadLiteral(Handle<Object> object) { |
| size_t entry = GetConstantPoolEntry(object); |
| - if (FitsInByteOperand(entry)) { |
| + if (FitsInIdxOperand(entry)) { |
| Output(Bytecode::kLdaConstant, static_cast<uint8_t>(entry)); |
| } else { |
| UNIMPLEMENTED(); |
| @@ -158,7 +205,7 @@ BytecodeArrayBuilder& BytecodeArrayBuilder::LoadNamedProperty( |
| UNIMPLEMENTED(); |
| } |
| - if (FitsInByteOperand(feedback_slot)) { |
| + if (FitsInIdxOperand(feedback_slot)) { |
| Output(Bytecode::kLoadIC, object.ToOperand(), |
| static_cast<uint8_t>(feedback_slot)); |
| } else { |
| @@ -174,7 +221,7 @@ BytecodeArrayBuilder& BytecodeArrayBuilder::LoadKeyedProperty( |
| UNIMPLEMENTED(); |
| } |
| - if (FitsInByteOperand(feedback_slot)) { |
| + if (FitsInIdxOperand(feedback_slot)) { |
| Output(Bytecode::kKeyedLoadIC, object.ToOperand(), |
| static_cast<uint8_t>(feedback_slot)); |
| } else { |
| @@ -191,7 +238,7 @@ BytecodeArrayBuilder& BytecodeArrayBuilder::StoreNamedProperty( |
| UNIMPLEMENTED(); |
| } |
| - if (FitsInByteOperand(feedback_slot)) { |
| + if (FitsInIdxOperand(feedback_slot)) { |
| Output(Bytecode::kStoreIC, object.ToOperand(), name.ToOperand(), |
| static_cast<uint8_t>(feedback_slot)); |
| } else { |
| @@ -208,7 +255,7 @@ BytecodeArrayBuilder& BytecodeArrayBuilder::StoreKeyedProperty( |
| UNIMPLEMENTED(); |
| } |
| - if (FitsInByteOperand(feedback_slot)) { |
| + if (FitsInIdxOperand(feedback_slot)) { |
| Output(Bytecode::kKeyedStoreIC, object.ToOperand(), key.ToOperand(), |
| static_cast<uint8_t>(feedback_slot)); |
| } else { |
| @@ -218,6 +265,131 @@ BytecodeArrayBuilder& BytecodeArrayBuilder::StoreKeyedProperty( |
| } |
| +BytecodeArrayBuilder& BytecodeArrayBuilder::CastAccumulatorToBoolean() { |
| + // TODO(oth): Consider peeking at previous bytecode to see if it |
| + // outputs a boolean value to the accumulator, if so this |
| + // instruction can be elided. Similarly if it's a Load instruction |
| + // change the instruction to basic true/false cast. Need to be |
| + // careful with backwards branches though might only have |
| + // troublesome cases in handwritten bytecode, but can check for |
| + // unbound labels bound before this point to spot back branch |
| + // targets. |
| + Output(Bytecode::kCastToBoolean); |
| + return *this; |
| +} |
| + |
| + |
| +BytecodeArrayBuilder& BytecodeArrayBuilder::Bind(BytecodeLabel* label) { |
| + if (label->is_forward_target()) { |
| + // An earlier jump instruction refers to this label. Update it's location. |
| + PatchJump(bytecodes()->end(), bytecodes()->begin() + label->offset()); |
| + // Now treat as if the label will only be back referred to. |
| + } |
| + label->bind_to(bytecodes()->size()); |
| + return *this; |
| +} |
| + |
| + |
| +// static |
| +bool BytecodeArrayBuilder::IsJumpWithSmi8Operand(Bytecode jump_bytecode) { |
| + return jump_bytecode == Bytecode::kJump || |
| + jump_bytecode == Bytecode::kJumpIfTrue || |
| + jump_bytecode == Bytecode::kJumpIfFalse; |
| +} |
| + |
| + |
| +// static |
| +Bytecode BytecodeArrayBuilder::GetJumpWithConstantOperand( |
| + Bytecode jump_bytecode) { |
| + switch (jump_bytecode) { |
| + case Bytecode::kJump: |
| + return Bytecode::kJumpConstant; |
| + case Bytecode::kJumpIfTrue: |
| + return Bytecode::kJumpIfTrueConstant; |
| + case Bytecode::kJumpIfFalse: |
| + return Bytecode::kJumpIfFalseConstant; |
| + default: |
| + UNREACHABLE(); |
| + return Bytecode::kJumpConstant; |
| + } |
| +} |
| + |
| + |
| +void BytecodeArrayBuilder::PatchJump( |
| + const ZoneVector<uint8_t>::iterator& jump_target, |
| + ZoneVector<uint8_t>::iterator jump_location) { |
| + Bytecode jump_bytecode = Bytecodes::FromByte(*jump_location); |
| + DCHECK(IsJumpWithSmi8Operand(jump_bytecode)); |
| + DCHECK_EQ(Bytecodes::Size(jump_bytecode), 2); |
| + int delta = static_cast<int>(jump_target - jump_location); |
| + DCHECK_GE(delta, 0); |
| + if (FitsInImm8Operand(delta)) { |
| + // Just update the operand |
| + jump_location++; |
| + *jump_location = static_cast<uint8_t>(delta); |
| + } else { |
| + // Update the jump type and operand |
| + size_t entry = GetConstantPoolEntry(handle(Smi::FromInt(delta), isolate())); |
| + if (FitsInIdxOperand(entry)) { |
| + *jump_location++ = |
| + Bytecodes::ToByte(GetJumpWithConstantOperand(jump_bytecode)); |
| + *jump_location = static_cast<uint8_t>(entry); |
| + } else { |
| + UNIMPLEMENTED(); |
| + } |
| + } |
| +} |
| + |
| + |
| +void BytecodeArrayBuilder::OutputJump( |
| + Bytecode jump_bytecode, const ZoneVector<uint8_t>::iterator& jump_target) { |
| + DCHECK(IsJumpWithSmi8Operand(jump_bytecode)); |
| + |
| + int delta = static_cast<int>(jump_target - bytecodes()->end()); |
| + if (FitsInImm8Operand(delta)) { |
| + Output(jump_bytecode, static_cast<uint8_t>(delta)); |
| + } else { |
| + size_t entry = GetConstantPoolEntry(handle(Smi::FromInt(delta), isolate())); |
| + if (FitsInIdxOperand(entry)) { |
| + Output(GetJumpWithConstantOperand(jump_bytecode), |
| + static_cast<uint8_t>(entry)); |
| + } else { |
| + UNIMPLEMENTED(); |
| + } |
| + } |
| +} |
| + |
| + |
| +BytecodeArrayBuilder& BytecodeArrayBuilder::Jump(Bytecode jump_bytecode, |
| + BytecodeLabel* label) { |
| + if (label->is_bound()) { |
| + // Label has been bound already so this is a backwards jump. |
| + OutputJump(jump_bytecode, bytecodes()->begin() + label->offset()); |
| + } else { |
| + // Label has not yet been bound so this is a forward reference |
| + // that will be patched when the label is bound. |
| + label->set_referrer(bytecodes()->size()); |
| + OutputJump(jump_bytecode, bytecodes()->end()); |
| + } |
| + return *this; |
| +} |
| + |
| + |
| +BytecodeArrayBuilder& BytecodeArrayBuilder::Jump(BytecodeLabel* label) { |
| + return Jump(Bytecode::kJump, label); |
| +} |
| + |
| + |
| +BytecodeArrayBuilder& BytecodeArrayBuilder::JumpIfTrue(BytecodeLabel* label) { |
| + return Jump(Bytecode::kJumpIfTrue, label); |
| +} |
| + |
| + |
| +BytecodeArrayBuilder& BytecodeArrayBuilder::JumpIfFalse(BytecodeLabel* label) { |
| + return Jump(Bytecode::kJumpIfFalse, label); |
| +} |
| + |
| + |
| BytecodeArrayBuilder& BytecodeArrayBuilder::Return() { |
| Output(Bytecode::kReturn); |
| return *this; |
| @@ -227,7 +399,7 @@ BytecodeArrayBuilder& BytecodeArrayBuilder::Return() { |
| BytecodeArrayBuilder& BytecodeArrayBuilder::Call(Register callable, |
| Register receiver, |
| size_t arg_count) { |
| - if (FitsInByteOperand(arg_count)) { |
| + if (FitsInIdxOperand(arg_count)) { |
| Output(Bytecode::kCall, callable.ToOperand(), receiver.ToOperand(), |
| static_cast<uint8_t>(arg_count)); |
| } else { |
| @@ -299,44 +471,6 @@ bool BytecodeArrayBuilder::OperandIsValid(Bytecode bytecode, int operand_index, |
| } |
| -void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0, |
| - uint8_t operand1, uint8_t operand2) { |
| - DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 3); |
| - DCHECK(OperandIsValid(bytecode, 0, operand0) && |
| - OperandIsValid(bytecode, 1, operand1) && |
| - OperandIsValid(bytecode, 2, operand2)); |
| - bytecodes_.push_back(Bytecodes::ToByte(bytecode)); |
| - bytecodes_.push_back(operand0); |
| - bytecodes_.push_back(operand1); |
| - bytecodes_.push_back(operand2); |
| -} |
| - |
| - |
| -void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0, |
| - uint8_t operand1) { |
| - DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 2); |
| - DCHECK(OperandIsValid(bytecode, 0, operand0) && |
| - OperandIsValid(bytecode, 1, operand1)); |
| - bytecodes_.push_back(Bytecodes::ToByte(bytecode)); |
| - bytecodes_.push_back(operand0); |
| - bytecodes_.push_back(operand1); |
| -} |
| - |
| - |
| -void BytecodeArrayBuilder::Output(Bytecode bytecode, uint8_t operand0) { |
| - DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 1); |
| - DCHECK(OperandIsValid(bytecode, 0, operand0)); |
| - bytecodes_.push_back(Bytecodes::ToByte(bytecode)); |
| - bytecodes_.push_back(operand0); |
| -} |
| - |
| - |
| -void BytecodeArrayBuilder::Output(Bytecode bytecode) { |
| - DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 0); |
| - bytecodes_.push_back(Bytecodes::ToByte(bytecode)); |
| -} |
| - |
| - |
| // static |
| Bytecode BytecodeArrayBuilder::BytecodeForBinaryOperation(Token::Value op) { |
| switch (op) { |
| @@ -358,17 +492,53 @@ Bytecode BytecodeArrayBuilder::BytecodeForBinaryOperation(Token::Value op) { |
| // static |
| -bool BytecodeArrayBuilder::FitsInByteOperand(int value) { |
| +Bytecode BytecodeArrayBuilder::BytecodeForCompareOperation(Token::Value op) { |
| + switch (op) { |
| + case Token::Value::EQ: |
| + return Bytecode::kTestEqual; |
| + case Token::Value::NE: |
| + return Bytecode::kTestNotEqual; |
| + case Token::Value::EQ_STRICT: |
| + return Bytecode::kTestEqualStrict; |
| + case Token::Value::NE_STRICT: |
| + return Bytecode::kTestNotEqualStrict; |
| + case Token::Value::LT: |
| + return Bytecode::kTestLessThan; |
| + case Token::Value::GT: |
| + return Bytecode::kTestGreaterThan; |
| + case Token::Value::LTE: |
| + return Bytecode::kTestLessThanEqual; |
| + case Token::Value::GTE: |
| + return Bytecode::kTestGreaterThanEqual; |
| + case Token::Value::INSTANCEOF: |
| + return Bytecode::kTestInstanceOf; |
| + case Token::Value::IN: |
| + return Bytecode::kTestIn; |
| + default: |
| + UNIMPLEMENTED(); |
|
Michael Starzinger
2015/09/23 08:46:24
nit: This should actually be UNREACHABLE(), right?
oth
2015/09/23 10:46:56
Done.
|
| + return static_cast<Bytecode>(-1); |
| + } |
| +} |
| + |
| + |
| +// static |
| +bool BytecodeArrayBuilder::FitsInIdxOperand(int value) { |
| return 0 <= value && value <= 255; |
| } |
| // static |
| -bool BytecodeArrayBuilder::FitsInByteOperand(size_t value) { |
| +bool BytecodeArrayBuilder::FitsInIdxOperand(size_t value) { |
| return value <= 255; |
| } |
| +// static |
| +bool BytecodeArrayBuilder::FitsInImm8Operand(int value) { |
| + return -128 <= value && value < 128; |
| +} |
| + |
| + |
| TemporaryRegisterScope::TemporaryRegisterScope(BytecodeArrayBuilder* builder) |
| : builder_(builder), count_(0), last_register_index_(-1) {} |