Index: src/code-stub-assembler.cc |
diff --git a/src/code-stub-assembler.cc b/src/code-stub-assembler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9a0394d4161867464f244632c4a529780392ef52 |
--- /dev/null |
+++ b/src/code-stub-assembler.cc |
@@ -0,0 +1,405 @@ |
+// Copyright 2016 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "src/code-stub-assembler.h" |
+#include "src/code-factory.h" |
+ |
+namespace v8 { |
+namespace internal { |
+ |
+using compiler::Node; |
+ |
+CodeStubAssembler::CodeStubAssembler(Isolate* isolate, Zone* zone, |
+ const CallInterfaceDescriptor& descriptor, |
+ Code::Flags flags, const char* name, |
+ size_t result_size) |
+ : compiler::CodeAssembler(isolate, zone, descriptor, flags, name, |
+ result_size) {} |
+ |
+CodeStubAssembler::CodeStubAssembler(Isolate* isolate, Zone* zone, |
+ int parameter_count, Code::Flags flags, |
+ const char* name) |
+ : compiler::CodeAssembler(isolate, zone, parameter_count, flags, name) {} |
+ |
+Node* CodeStubAssembler::SmiToWord32(Node* value) { |
+ Node* result = WordSar(value, SmiShiftBitsConstant()); |
+ if (Is64()) { |
+ result = TruncateInt64ToInt32(result); |
+ } |
+ return result; |
+} |
+ |
+Node* CodeStubAssembler::SmiToFloat64(Node* value) { |
+ return ChangeInt32ToFloat64(SmiUntag(value)); |
+} |
+ |
+Node* CodeStubAssembler::SmiAdd(Node* a, Node* b) { return IntPtrAdd(a, b); } |
+ |
+Node* CodeStubAssembler::SmiAddWithOverflow(Node* a, Node* b) { |
+ return IntPtrAddWithOverflow(a, b); |
+} |
+ |
+Node* CodeStubAssembler::SmiSub(Node* a, Node* b) { return IntPtrSub(a, b); } |
+ |
+Node* CodeStubAssembler::SmiSubWithOverflow(Node* a, Node* b) { |
+ return IntPtrSubWithOverflow(a, b); |
+} |
+ |
+Node* CodeStubAssembler::SmiMin(Node* a, Node* b) { |
+ // TODO(bmeurer): Consider using Select once available. |
+ Variable min(this, MachineRepresentation::kTagged); |
+ Label if_a(this), if_b(this), join(this); |
+ BranchIfSmiLessThan(a, b, &if_a, &if_b); |
+ Bind(&if_a); |
+ min.Bind(a); |
+ Goto(&join); |
+ Bind(&if_b); |
+ min.Bind(b); |
+ Goto(&join); |
+ Bind(&join); |
+ return min.value(); |
+} |
+ |
+Node* CodeStubAssembler::SmiEqual(Node* a, Node* b) { return WordEqual(a, b); } |
+ |
+Node* CodeStubAssembler::SmiLessThan(Node* a, Node* b) { |
+ return IntPtrLessThan(a, b); |
+} |
+ |
+Node* CodeStubAssembler::SmiLessThanOrEqual(Node* a, Node* b) { |
+ return IntPtrLessThanOrEqual(a, b); |
+} |
+ |
+Node* CodeStubAssembler::WordIsSmi(Node* a) { |
+ return WordEqual(WordAnd(a, IntPtrConstant(kSmiTagMask)), IntPtrConstant(0)); |
+} |
+ |
+Node* CodeStubAssembler::WordIsPositiveSmi(Node* a) { |
+ return WordEqual(WordAnd(a, IntPtrConstant(kSmiTagMask | kSmiSignMask)), |
+ IntPtrConstant(0)); |
+} |
+ |
+Node* CodeStubAssembler::LoadBufferObject(Node* buffer, int offset, |
+ MachineType rep) { |
+ return Load(rep, buffer, IntPtrConstant(offset)); |
+} |
+ |
+Node* CodeStubAssembler::LoadObjectField(Node* object, int offset, |
+ MachineType rep) { |
+ return Load(rep, object, IntPtrConstant(offset - kHeapObjectTag)); |
+} |
+ |
+Node* CodeStubAssembler::LoadHeapNumberValue(Node* object) { |
+ return Load(MachineType::Float64(), object, |
+ IntPtrConstant(HeapNumber::kValueOffset - kHeapObjectTag)); |
+} |
+ |
+Node* CodeStubAssembler::LoadMap(Node* object) { |
+ return LoadObjectField(object, HeapObject::kMapOffset); |
+} |
+ |
+Node* CodeStubAssembler::LoadInstanceType(Node* object) { |
+ return LoadMapInstanceType(LoadMap(object)); |
+} |
+ |
+Node* CodeStubAssembler::LoadElements(Node* object) { |
+ return LoadObjectField(object, JSObject::kElementsOffset); |
+} |
+ |
+Node* CodeStubAssembler::LoadFixedArrayBaseLength(Node* array) { |
+ return LoadObjectField(array, FixedArrayBase::kLengthOffset); |
+} |
+ |
+Node* CodeStubAssembler::LoadMapBitField(Node* map) { |
+ return Load(MachineType::Uint8(), map, |
+ IntPtrConstant(Map::kBitFieldOffset - kHeapObjectTag)); |
+} |
+ |
+Node* CodeStubAssembler::LoadMapBitField2(Node* map) { |
+ return Load(MachineType::Uint8(), map, |
+ IntPtrConstant(Map::kBitField2Offset - kHeapObjectTag)); |
+} |
+ |
+Node* CodeStubAssembler::LoadMapBitField3(Node* map) { |
+ return Load(MachineType::Uint32(), map, |
+ IntPtrConstant(Map::kBitField3Offset - kHeapObjectTag)); |
+} |
+ |
+Node* CodeStubAssembler::LoadMapInstanceType(Node* map) { |
+ return Load(MachineType::Uint8(), map, |
+ IntPtrConstant(Map::kInstanceTypeOffset - kHeapObjectTag)); |
+} |
+ |
+Node* CodeStubAssembler::LoadMapDescriptors(Node* map) { |
+ return LoadObjectField(map, Map::kDescriptorsOffset); |
+} |
+ |
+Node* CodeStubAssembler::LoadNameHash(Node* name) { |
+ return Load(MachineType::Uint32(), name, |
+ IntPtrConstant(Name::kHashFieldOffset - kHeapObjectTag)); |
+} |
+ |
+Node* CodeStubAssembler::LoadFixedArrayElementInt32Index( |
+ Node* object, Node* int32_index, int additional_offset) { |
+ Node* header_size = IntPtrConstant(additional_offset + |
+ FixedArray::kHeaderSize - kHeapObjectTag); |
+ Node* scaled_index = WordShl(int32_index, IntPtrConstant(kPointerSizeLog2)); |
+ Node* offset = IntPtrAdd(scaled_index, header_size); |
+ return Load(MachineType::AnyTagged(), object, offset); |
+} |
+ |
+Node* CodeStubAssembler::LoadFixedArrayElementSmiIndex(Node* object, |
+ Node* smi_index, |
+ int additional_offset) { |
+ int const kSmiShiftBits = kSmiShiftSize + kSmiTagSize; |
+ Node* header_size = IntPtrConstant(additional_offset + |
+ FixedArray::kHeaderSize - kHeapObjectTag); |
+ Node* scaled_index = |
+ (kSmiShiftBits > kPointerSizeLog2) |
+ ? WordSar(smi_index, IntPtrConstant(kSmiShiftBits - kPointerSizeLog2)) |
+ : WordShl(smi_index, |
+ IntPtrConstant(kPointerSizeLog2 - kSmiShiftBits)); |
+ Node* offset = IntPtrAdd(scaled_index, header_size); |
+ return Load(MachineType::AnyTagged(), object, offset); |
+} |
+ |
+Node* CodeStubAssembler::LoadFixedArrayElementConstantIndex(Node* object, |
+ int index) { |
+ Node* offset = IntPtrConstant(FixedArray::kHeaderSize - kHeapObjectTag + |
+ index * kPointerSize); |
+ return Load(MachineType::AnyTagged(), object, offset); |
+} |
+ |
+Node* CodeStubAssembler::StoreMapNoWriteBarrier(Node* object, Node* map) { |
+ return StoreNoWriteBarrier( |
+ MachineRepresentation::kTagged, object, |
+ IntPtrConstant(HeapNumber::kMapOffset - kHeapObjectTag), map); |
+} |
+ |
+Node* CodeStubAssembler::StoreFixedArrayElementNoWriteBarrier(Node* object, |
+ Node* index, |
+ Node* value) { |
+ Node* offset = |
+ IntPtrAdd(WordShl(index, IntPtrConstant(kPointerSizeLog2)), |
+ IntPtrConstant(FixedArray::kHeaderSize - kHeapObjectTag)); |
+ return StoreNoWriteBarrier(MachineRepresentation::kTagged, object, offset, |
+ value); |
+} |
+ |
+Node* CodeStubAssembler::StoreHeapNumberValue(Node* object, Node* value) { |
+ return StoreNoWriteBarrier( |
+ MachineRepresentation::kFloat64, object, |
+ IntPtrConstant(HeapNumber::kValueOffset - kHeapObjectTag), value); |
+} |
+ |
+Node* CodeStubAssembler::AllocateHeapNumber() { |
+ Node* result = Allocate(HeapNumber::kSize, kNone); |
+ StoreMapNoWriteBarrier(result, HeapNumberMapConstant()); |
+ return result; |
+} |
+ |
+Node* CodeStubAssembler::AllocateHeapNumberWithValue(Node* value) { |
+ Node* result = AllocateHeapNumber(); |
+ StoreHeapNumberValue(result, value); |
+ return result; |
+} |
+ |
+Node* CodeStubAssembler::BitFieldDecode(Node* word32, uint32_t shift, |
+ uint32_t mask) { |
+ return Word32Shr(Word32And(word32, Int32Constant(mask)), |
+ Int32Constant(shift)); |
+} |
+ |
+Node* CodeStubAssembler::ChangeFloat64ToTagged(Node* value) { |
+ Node* value32 = TruncateFloat64ToInt32RoundToZero(value); |
+ Node* value64 = ChangeInt32ToFloat64(value32); |
+ |
+ Label if_valueisint32(this), if_valueisheapnumber(this), if_join(this); |
+ |
+ Label if_valueisequal(this), if_valueisnotequal(this); |
+ Branch(Float64Equal(value, value64), &if_valueisequal, &if_valueisnotequal); |
+ Bind(&if_valueisequal); |
+ { |
+ Label if_valueiszero(this), if_valueisnotzero(this); |
+ Branch(Float64Equal(value, Float64Constant(0.0)), &if_valueiszero, |
+ &if_valueisnotzero); |
+ |
+ Bind(&if_valueiszero); |
+ BranchIfInt32LessThan(Float64ExtractHighWord32(value), Int32Constant(0), |
+ &if_valueisheapnumber, &if_valueisint32); |
+ |
+ Bind(&if_valueisnotzero); |
+ Goto(&if_valueisint32); |
+ } |
+ Bind(&if_valueisnotequal); |
+ Goto(&if_valueisheapnumber); |
+ |
+ Variable var_result(this, MachineRepresentation::kTagged); |
+ Bind(&if_valueisint32); |
+ { |
+ if (Is64()) { |
+ Node* result = SmiTag(ChangeInt32ToInt64(value32)); |
+ var_result.Bind(result); |
+ Goto(&if_join); |
+ } else { |
+ Node* pair = Int32AddWithOverflow(value32, value32); |
+ Node* overflow = Projection(1, pair); |
+ Label if_overflow(this, Label::kDeferred), if_notoverflow(this); |
+ Branch(overflow, &if_overflow, &if_notoverflow); |
+ Bind(&if_overflow); |
+ Goto(&if_valueisheapnumber); |
+ Bind(&if_notoverflow); |
+ { |
+ Node* result = Projection(0, pair); |
+ var_result.Bind(result); |
+ Goto(&if_join); |
+ } |
+ } |
+ } |
+ Bind(&if_valueisheapnumber); |
+ { |
+ Node* result = AllocateHeapNumberWithValue(value); |
+ var_result.Bind(result); |
+ Goto(&if_join); |
+ } |
+ Bind(&if_join); |
+ return var_result.value(); |
+} |
+ |
+Node* CodeStubAssembler::ChangeInt32ToTagged(Node* value) { |
+ if (Is64()) { |
+ return SmiTag(ChangeInt32ToInt64(value)); |
+ } |
+ Variable var_result(this, MachineRepresentation::kTagged); |
+ Node* pair = Int32AddWithOverflow(value, value); |
+ Node* overflow = Projection(1, pair); |
+ Label if_overflow(this, Label::kDeferred), if_notoverflow(this), |
+ if_join(this); |
+ Branch(overflow, &if_overflow, &if_notoverflow); |
+ Bind(&if_overflow); |
+ { |
+ Node* value64 = ChangeInt32ToFloat64(value); |
+ Node* result = AllocateHeapNumberWithValue(value64); |
+ var_result.Bind(result); |
+ } |
+ Goto(&if_join); |
+ Bind(&if_notoverflow); |
+ { |
+ Node* result = Projection(0, pair); |
+ var_result.Bind(result); |
+ } |
+ Goto(&if_join); |
+ Bind(&if_join); |
+ return var_result.value(); |
+} |
+ |
+Node* CodeStubAssembler::TruncateTaggedToFloat64(Node* context, Node* value) { |
+ // We might need to loop once due to ToNumber conversion. |
+ Variable var_value(this, MachineRepresentation::kTagged), |
+ var_result(this, MachineRepresentation::kFloat64); |
+ Label loop(this, &var_value), done_loop(this, &var_result); |
+ var_value.Bind(value); |
+ Goto(&loop); |
+ Bind(&loop); |
+ { |
+ // Load the current {value}. |
+ value = var_value.value(); |
+ |
+ // Check if the {value} is a Smi or a HeapObject. |
+ Label if_valueissmi(this), if_valueisnotsmi(this); |
+ Branch(WordIsSmi(value), &if_valueissmi, &if_valueisnotsmi); |
+ |
+ Bind(&if_valueissmi); |
+ { |
+ // Convert the Smi {value}. |
+ var_result.Bind(SmiToFloat64(value)); |
+ Goto(&done_loop); |
+ } |
+ |
+ Bind(&if_valueisnotsmi); |
+ { |
+ // Check if {value} is a HeapNumber. |
+ Label if_valueisheapnumber(this), |
+ if_valueisnotheapnumber(this, Label::kDeferred); |
+ Branch(WordEqual(LoadMap(value), HeapNumberMapConstant()), |
+ &if_valueisheapnumber, &if_valueisnotheapnumber); |
+ |
+ Bind(&if_valueisheapnumber); |
+ { |
+ // Load the floating point value. |
+ var_result.Bind(LoadHeapNumberValue(value)); |
+ Goto(&done_loop); |
+ } |
+ |
+ Bind(&if_valueisnotheapnumber); |
+ { |
+ // Convert the {value} to a Number first. |
+ Callable callable = CodeFactory::NonNumberToNumber(isolate()); |
+ var_value.Bind(CallStub(callable, context, value)); |
+ Goto(&loop); |
+ } |
+ } |
+ } |
+ Bind(&done_loop); |
+ return var_result.value(); |
+} |
+ |
+Node* CodeStubAssembler::TruncateTaggedToWord32(Node* context, Node* value) { |
+ // We might need to loop once due to ToNumber conversion. |
+ Variable var_value(this, MachineRepresentation::kTagged), |
+ var_result(this, MachineRepresentation::kWord32); |
+ Label loop(this, &var_value), done_loop(this, &var_result); |
+ var_value.Bind(value); |
+ Goto(&loop); |
+ Bind(&loop); |
+ { |
+ // Load the current {value}. |
+ value = var_value.value(); |
+ |
+ // Check if the {value} is a Smi or a HeapObject. |
+ Label if_valueissmi(this), if_valueisnotsmi(this); |
+ Branch(WordIsSmi(value), &if_valueissmi, &if_valueisnotsmi); |
+ |
+ Bind(&if_valueissmi); |
+ { |
+ // Convert the Smi {value}. |
+ var_result.Bind(SmiToWord32(value)); |
+ Goto(&done_loop); |
+ } |
+ |
+ Bind(&if_valueisnotsmi); |
+ { |
+ // Check if {value} is a HeapNumber. |
+ Label if_valueisheapnumber(this), |
+ if_valueisnotheapnumber(this, Label::kDeferred); |
+ Branch(WordEqual(LoadMap(value), HeapNumberMapConstant()), |
+ &if_valueisheapnumber, &if_valueisnotheapnumber); |
+ |
+ Bind(&if_valueisheapnumber); |
+ { |
+ // Truncate the floating point value. |
+ var_result.Bind(TruncateHeapNumberValueToWord32(value)); |
+ Goto(&done_loop); |
+ } |
+ |
+ Bind(&if_valueisnotheapnumber); |
+ { |
+ // Convert the {value} to a Number first. |
+ Callable callable = CodeFactory::NonNumberToNumber(isolate()); |
+ var_value.Bind(CallStub(callable, context, value)); |
+ Goto(&loop); |
+ } |
+ } |
+ } |
+ Bind(&done_loop); |
+ return var_result.value(); |
+} |
+ |
+Node* CodeStubAssembler::TruncateHeapNumberValueToWord32(Node* object) { |
+ Node* value = LoadHeapNumberValue(object); |
+ return TruncateFloat64ToInt32JavaScript(value); |
+} |
+ |
+} // namespace internal |
+} // namespace v8 |