Extend ToBooleanStub to be able to return oddballs.

R=titzer@chromium.org

Review URL: https://codereview.chromium.org/450273002

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22997 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 7a86e30c
...@@ -1065,14 +1065,31 @@ Handle<Code> StringAddStub::GenerateCode() { ...@@ -1065,14 +1065,31 @@ Handle<Code> StringAddStub::GenerateCode() {
template <> template <>
HValue* CodeStubGraphBuilder<ToBooleanStub>::BuildCodeInitializedStub() { HValue* CodeStubGraphBuilder<ToBooleanStub>::BuildCodeInitializedStub() {
ToBooleanStub* stub = casted_stub(); ToBooleanStub* stub = casted_stub();
HValue* true_value = NULL;
HValue* false_value = NULL;
switch (stub->GetMode()) {
case ToBooleanStub::RESULT_AS_SMI:
true_value = graph()->GetConstant1();
false_value = graph()->GetConstant0();
break;
case ToBooleanStub::RESULT_AS_ODDBALL:
true_value = graph()->GetConstantTrue();
false_value = graph()->GetConstantFalse();
break;
case ToBooleanStub::RESULT_AS_INVERSE_ODDBALL:
true_value = graph()->GetConstantFalse();
false_value = graph()->GetConstantTrue();
break;
}
IfBuilder if_true(this); IfBuilder if_true(this);
if_true.If<HBranch>(GetParameter(0), stub->GetTypes()); if_true.If<HBranch>(GetParameter(0), stub->GetTypes());
if_true.Then(); if_true.Then();
if_true.Return(graph()->GetConstant1()); if_true.Return(true_value);
if_true.Else(); if_true.Else();
if_true.End(); if_true.End();
return graph()->GetConstant0(); return false_value;
} }
......
...@@ -2315,6 +2315,12 @@ class ToBooleanStub: public HydrogenCodeStub { ...@@ -2315,6 +2315,12 @@ class ToBooleanStub: public HydrogenCodeStub {
NUMBER_OF_TYPES NUMBER_OF_TYPES
}; };
enum ResultMode {
RESULT_AS_SMI, // For Smi(1) on truthy value, Smi(0) otherwise.
RESULT_AS_ODDBALL, // For {true} on truthy value, {false} otherwise.
RESULT_AS_INVERSE_ODDBALL // For {false} on truthy value, {true} otherwise.
};
// At most 8 different types can be distinguished, because the Code object // At most 8 different types can be distinguished, because the Code object
// only has room for a single byte to hold a set of these types. :-P // only has room for a single byte to hold a set of these types. :-P
STATIC_ASSERT(NUMBER_OF_TYPES <= 8); STATIC_ASSERT(NUMBER_OF_TYPES <= 8);
...@@ -2333,13 +2339,16 @@ class ToBooleanStub: public HydrogenCodeStub { ...@@ -2333,13 +2339,16 @@ class ToBooleanStub: public HydrogenCodeStub {
static Types Generic() { return Types((1 << NUMBER_OF_TYPES) - 1); } static Types Generic() { return Types((1 << NUMBER_OF_TYPES) - 1); }
}; };
ToBooleanStub(Isolate* isolate, Types types = Types()) ToBooleanStub(Isolate* isolate, ResultMode mode, Types types = Types())
: HydrogenCodeStub(isolate), types_(types) { } : HydrogenCodeStub(isolate), types_(types), mode_(mode) {}
ToBooleanStub(Isolate* isolate, ExtraICState state) ToBooleanStub(Isolate* isolate, ExtraICState state)
: HydrogenCodeStub(isolate), types_(static_cast<byte>(state)) { } : HydrogenCodeStub(isolate),
types_(static_cast<byte>(state)),
mode_(RESULT_AS_SMI) {}
bool UpdateStatus(Handle<Object> object); bool UpdateStatus(Handle<Object> object);
Types GetTypes() { return types_; } Types GetTypes() { return types_; }
ResultMode GetMode() { return mode_; }
virtual Handle<Code> GenerateCode() V8_OVERRIDE; virtual Handle<Code> GenerateCode() V8_OVERRIDE;
virtual void InitializeInterfaceDescriptor( virtual void InitializeInterfaceDescriptor(
...@@ -2351,7 +2360,7 @@ class ToBooleanStub: public HydrogenCodeStub { ...@@ -2351,7 +2360,7 @@ class ToBooleanStub: public HydrogenCodeStub {
virtual bool SometimesSetsUpAFrame() { return false; } virtual bool SometimesSetsUpAFrame() { return false; }
static void InstallDescriptors(Isolate* isolate) { static void InstallDescriptors(Isolate* isolate) {
ToBooleanStub stub(isolate); ToBooleanStub stub(isolate, RESULT_AS_SMI);
stub.InitializeInterfaceDescriptor( stub.InitializeInterfaceDescriptor(
isolate->code_stub_interface_descriptor(CodeStub::ToBoolean)); isolate->code_stub_interface_descriptor(CodeStub::ToBoolean));
} }
...@@ -2371,13 +2380,19 @@ class ToBooleanStub: public HydrogenCodeStub { ...@@ -2371,13 +2380,19 @@ class ToBooleanStub: public HydrogenCodeStub {
} }
private: private:
class TypesBits : public BitField<byte, 0, NUMBER_OF_TYPES> {};
class ResultModeBits : public BitField<ResultMode, NUMBER_OF_TYPES, 2> {};
Major MajorKey() const { return ToBoolean; } Major MajorKey() const { return ToBoolean; }
int NotMissMinorKey() const { return GetExtraICState(); } int NotMissMinorKey() const {
return TypesBits::encode(types_.ToByte()) | ResultModeBits::encode(mode_);
}
ToBooleanStub(Isolate* isolate, InitializationState init_state) : ToBooleanStub(Isolate* isolate, InitializationState init_state)
HydrogenCodeStub(isolate, init_state) {} : HydrogenCodeStub(isolate, init_state), mode_(RESULT_AS_SMI) {}
Types types_; Types types_;
ResultMode mode_;
}; };
......
...@@ -382,35 +382,15 @@ Node* JSGenericLowering::LowerBranch(Node* node) { ...@@ -382,35 +382,15 @@ Node* JSGenericLowering::LowerBranch(Node* node) {
Node* JSGenericLowering::LowerJSUnaryNot(Node* node) { Node* JSGenericLowering::LowerJSUnaryNot(Node* node) {
ToBooleanStub stub(isolate()); ToBooleanStub stub(isolate(), ToBooleanStub::RESULT_AS_INVERSE_ODDBALL);
CodeStubInterfaceDescriptor* d = stub.GetInterfaceDescriptor(); ReplaceWithICStubCall(node, &stub);
CallDescriptor* desc = linkage()->GetStubCallDescriptor(d);
Node* to_bool =
graph()->NewNode(common()->Call(desc), CodeConstant(stub.GetCode()),
NodeProperties::GetValueInput(node, 0),
NodeProperties::GetContextInput(node),
NodeProperties::GetEffectInput(node),
NodeProperties::GetControlInput(node));
node->ReplaceInput(0, to_bool);
PatchInsertInput(node, 1, SmiConstant(Token::EQ));
ReplaceWithRuntimeCall(node, Runtime::kBooleanize);
return node; return node;
} }
Node* JSGenericLowering::LowerJSToBoolean(Node* node) { Node* JSGenericLowering::LowerJSToBoolean(Node* node) {
ToBooleanStub stub(isolate()); ToBooleanStub stub(isolate(), ToBooleanStub::RESULT_AS_ODDBALL);
CodeStubInterfaceDescriptor* d = stub.GetInterfaceDescriptor(); ReplaceWithICStubCall(node, &stub);
CallDescriptor* desc = linkage()->GetStubCallDescriptor(d);
Node* to_bool =
graph()->NewNode(common()->Call(desc), CodeConstant(stub.GetCode()),
NodeProperties::GetValueInput(node, 0),
NodeProperties::GetContextInput(node),
NodeProperties::GetEffectInput(node),
NodeProperties::GetControlInput(node));
node->ReplaceInput(0, to_bool);
PatchInsertInput(node, 1, SmiConstant(Token::NE));
ReplaceWithRuntimeCall(node, Runtime::kBooleanize);
return node; return node;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment