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

Side by Side Diff: src/x64/codegen-x64.cc

Issue 3195022: Move code stubs from codegen*.* files to code-stub*.* files. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 3 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/x64/codegen-x64.h ('k') | src/x64/full-codegen-x64.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 12 matching lines...) Expand all
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "v8.h" 28 #include "v8.h"
29 29
30 #if defined(V8_TARGET_ARCH_X64) 30 #if defined(V8_TARGET_ARCH_X64)
31 31
32 #include "bootstrapper.h" 32 #include "bootstrapper.h"
33 #include "code-stubs-x64.h"
33 #include "codegen-inl.h" 34 #include "codegen-inl.h"
34 #include "compiler.h" 35 #include "compiler.h"
35 #include "debug.h" 36 #include "debug.h"
36 #include "ic-inl.h" 37 #include "ic-inl.h"
37 #include "parser.h" 38 #include "parser.h"
38 #include "regexp-macro-assembler.h" 39 #include "regexp-macro-assembler.h"
39 #include "register-allocator-inl.h" 40 #include "register-allocator-inl.h"
40 #include "scopes.h" 41 #include "scopes.h"
41 #include "virtual-frame-inl.h" 42 #include "virtual-frame-inl.h"
42 43
(...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 ToBooleanStub stub; 801 ToBooleanStub stub;
801 Result temp = frame_->CallStub(&stub, 1); 802 Result temp = frame_->CallStub(&stub, 1);
802 // Convert the result to a condition code. 803 // Convert the result to a condition code.
803 __ testq(temp.reg(), temp.reg()); 804 __ testq(temp.reg(), temp.reg());
804 temp.Unuse(); 805 temp.Unuse();
805 dest->Split(not_equal); 806 dest->Split(not_equal);
806 } 807 }
807 } 808 }
808 809
809 810
810 class FloatingPointHelper : public AllStatic {
811 public:
812 // Load the operands from rdx and rax into xmm0 and xmm1, as doubles.
813 // If the operands are not both numbers, jump to not_numbers.
814 // Leaves rdx and rax unchanged. SmiOperands assumes both are smis.
815 // NumberOperands assumes both are smis or heap numbers.
816 static void LoadSSE2SmiOperands(MacroAssembler* masm);
817 static void LoadSSE2NumberOperands(MacroAssembler* masm);
818 static void LoadSSE2UnknownOperands(MacroAssembler* masm,
819 Label* not_numbers);
820
821 // Takes the operands in rdx and rax and loads them as integers in rax
822 // and rcx.
823 static void LoadAsIntegers(MacroAssembler* masm,
824 Label* operand_conversion_failure,
825 Register heap_number_map);
826 // As above, but we know the operands to be numbers. In that case,
827 // conversion can't fail.
828 static void LoadNumbersAsIntegers(MacroAssembler* masm);
829 };
830
831
832 const char* GenericBinaryOpStub::GetName() {
833 if (name_ != NULL) return name_;
834 const int kMaxNameLength = 100;
835 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
836 if (name_ == NULL) return "OOM";
837 const char* op_name = Token::Name(op_);
838 const char* overwrite_name;
839 switch (mode_) {
840 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
841 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
842 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
843 default: overwrite_name = "UnknownOverwrite"; break;
844 }
845
846 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
847 "GenericBinaryOpStub_%s_%s%s_%s%s_%s_%s",
848 op_name,
849 overwrite_name,
850 (flags_ & NO_SMI_CODE_IN_STUB) ? "_NoSmiInStub" : "",
851 args_in_registers_ ? "RegArgs" : "StackArgs",
852 args_reversed_ ? "_R" : "",
853 static_operands_type_.ToString(),
854 BinaryOpIC::GetName(runtime_operands_type_));
855 return name_;
856 }
857
858
859 // Call the specialized stub for a binary operation. 811 // Call the specialized stub for a binary operation.
860 class DeferredInlineBinaryOperation: public DeferredCode { 812 class DeferredInlineBinaryOperation: public DeferredCode {
861 public: 813 public:
862 DeferredInlineBinaryOperation(Token::Value op, 814 DeferredInlineBinaryOperation(Token::Value op,
863 Register dst, 815 Register dst,
864 Register left, 816 Register left,
865 Register right, 817 Register right,
866 OverwriteMode mode) 818 OverwriteMode mode)
867 : op_(op), dst_(dst), left_(left), right_(right), mode_(mode) { 819 : op_(op), dst_(dst), left_(left), right_(right), mode_(mode) {
868 set_comment("[ DeferredInlineBinaryOperation"); 820 set_comment("[ DeferredInlineBinaryOperation");
(...skipping 7943 matching lines...) Expand 10 before | Expand all | Expand 10 after
8812 break; 8764 break;
8813 } 8765 }
8814 8766
8815 case UNLOADED: 8767 case UNLOADED:
8816 case ILLEGAL: 8768 case ILLEGAL:
8817 UNREACHABLE(); 8769 UNREACHABLE();
8818 } 8770 }
8819 } 8771 }
8820 8772
8821 8773
8822 void FastNewClosureStub::Generate(MacroAssembler* masm) {
8823 // Create a new closure from the given function info in new
8824 // space. Set the context to the current context in rsi.
8825 Label gc;
8826 __ AllocateInNewSpace(JSFunction::kSize, rax, rbx, rcx, &gc, TAG_OBJECT);
8827
8828 // Get the function info from the stack.
8829 __ movq(rdx, Operand(rsp, 1 * kPointerSize));
8830
8831 // Compute the function map in the current global context and set that
8832 // as the map of the allocated object.
8833 __ movq(rcx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
8834 __ movq(rcx, FieldOperand(rcx, GlobalObject::kGlobalContextOffset));
8835 __ movq(rcx, Operand(rcx, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
8836 __ movq(FieldOperand(rax, JSObject::kMapOffset), rcx);
8837
8838 // Initialize the rest of the function. We don't have to update the
8839 // write barrier because the allocated object is in new space.
8840 __ LoadRoot(rbx, Heap::kEmptyFixedArrayRootIndex);
8841 __ LoadRoot(rcx, Heap::kTheHoleValueRootIndex);
8842 __ movq(FieldOperand(rax, JSObject::kPropertiesOffset), rbx);
8843 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rbx);
8844 __ movq(FieldOperand(rax, JSFunction::kPrototypeOrInitialMapOffset), rcx);
8845 __ movq(FieldOperand(rax, JSFunction::kSharedFunctionInfoOffset), rdx);
8846 __ movq(FieldOperand(rax, JSFunction::kContextOffset), rsi);
8847 __ movq(FieldOperand(rax, JSFunction::kLiteralsOffset), rbx);
8848
8849 // Initialize the code pointer in the function to be the one
8850 // found in the shared function info object.
8851 __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
8852 __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
8853 __ movq(FieldOperand(rax, JSFunction::kCodeEntryOffset), rdx);
8854
8855
8856 // Return and remove the on-stack parameter.
8857 __ ret(1 * kPointerSize);
8858
8859 // Create a new closure through the slower runtime call.
8860 __ bind(&gc);
8861 __ pop(rcx); // Temporarily remove return address.
8862 __ pop(rdx);
8863 __ push(rsi);
8864 __ push(rdx);
8865 __ push(rcx); // Restore return address.
8866 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
8867 }
8868
8869
8870 void FastNewContextStub::Generate(MacroAssembler* masm) {
8871 // Try to allocate the context in new space.
8872 Label gc;
8873 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
8874 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
8875 rax, rbx, rcx, &gc, TAG_OBJECT);
8876
8877 // Get the function from the stack.
8878 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
8879
8880 // Setup the object header.
8881 __ LoadRoot(kScratchRegister, Heap::kContextMapRootIndex);
8882 __ movq(FieldOperand(rax, HeapObject::kMapOffset), kScratchRegister);
8883 __ Move(FieldOperand(rax, FixedArray::kLengthOffset), Smi::FromInt(length));
8884
8885 // Setup the fixed slots.
8886 __ xor_(rbx, rbx); // Set to NULL.
8887 __ movq(Operand(rax, Context::SlotOffset(Context::CLOSURE_INDEX)), rcx);
8888 __ movq(Operand(rax, Context::SlotOffset(Context::FCONTEXT_INDEX)), rax);
8889 __ movq(Operand(rax, Context::SlotOffset(Context::PREVIOUS_INDEX)), rbx);
8890 __ movq(Operand(rax, Context::SlotOffset(Context::EXTENSION_INDEX)), rbx);
8891
8892 // Copy the global object from the surrounding context.
8893 __ movq(rbx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
8894 __ movq(Operand(rax, Context::SlotOffset(Context::GLOBAL_INDEX)), rbx);
8895
8896 // Initialize the rest of the slots to undefined.
8897 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
8898 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
8899 __ movq(Operand(rax, Context::SlotOffset(i)), rbx);
8900 }
8901
8902 // Return and remove the on-stack parameter.
8903 __ movq(rsi, rax);
8904 __ ret(1 * kPointerSize);
8905
8906 // Need to collect. Call into runtime system.
8907 __ bind(&gc);
8908 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
8909 }
8910
8911
8912 void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
8913 // Stack layout on entry:
8914 //
8915 // [rsp + kPointerSize]: constant elements.
8916 // [rsp + (2 * kPointerSize)]: literal index.
8917 // [rsp + (3 * kPointerSize)]: literals array.
8918
8919 // All sizes here are multiples of kPointerSize.
8920 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
8921 int size = JSArray::kSize + elements_size;
8922
8923 // Load boilerplate object into rcx and check if we need to create a
8924 // boilerplate.
8925 Label slow_case;
8926 __ movq(rcx, Operand(rsp, 3 * kPointerSize));
8927 __ movq(rax, Operand(rsp, 2 * kPointerSize));
8928 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
8929 __ movq(rcx,
8930 FieldOperand(rcx, index.reg, index.scale, FixedArray::kHeaderSize));
8931 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
8932 __ j(equal, &slow_case);
8933
8934 if (FLAG_debug_code) {
8935 const char* message;
8936 Heap::RootListIndex expected_map_index;
8937 if (mode_ == CLONE_ELEMENTS) {
8938 message = "Expected (writable) fixed array";
8939 expected_map_index = Heap::kFixedArrayMapRootIndex;
8940 } else {
8941 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
8942 message = "Expected copy-on-write fixed array";
8943 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
8944 }
8945 __ push(rcx);
8946 __ movq(rcx, FieldOperand(rcx, JSArray::kElementsOffset));
8947 __ CompareRoot(FieldOperand(rcx, HeapObject::kMapOffset),
8948 expected_map_index);
8949 __ Assert(equal, message);
8950 __ pop(rcx);
8951 }
8952
8953 // Allocate both the JS array and the elements array in one big
8954 // allocation. This avoids multiple limit checks.
8955 __ AllocateInNewSpace(size, rax, rbx, rdx, &slow_case, TAG_OBJECT);
8956
8957 // Copy the JS array part.
8958 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
8959 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
8960 __ movq(rbx, FieldOperand(rcx, i));
8961 __ movq(FieldOperand(rax, i), rbx);
8962 }
8963 }
8964
8965 if (length_ > 0) {
8966 // Get hold of the elements array of the boilerplate and setup the
8967 // elements pointer in the resulting object.
8968 __ movq(rcx, FieldOperand(rcx, JSArray::kElementsOffset));
8969 __ lea(rdx, Operand(rax, JSArray::kSize));
8970 __ movq(FieldOperand(rax, JSArray::kElementsOffset), rdx);
8971
8972 // Copy the elements array.
8973 for (int i = 0; i < elements_size; i += kPointerSize) {
8974 __ movq(rbx, FieldOperand(rcx, i));
8975 __ movq(FieldOperand(rdx, i), rbx);
8976 }
8977 }
8978
8979 // Return and remove the on-stack parameters.
8980 __ ret(3 * kPointerSize);
8981
8982 __ bind(&slow_case);
8983 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
8984 }
8985
8986
8987 void ToBooleanStub::Generate(MacroAssembler* masm) {
8988 Label false_result, true_result, not_string;
8989 __ movq(rax, Operand(rsp, 1 * kPointerSize));
8990
8991 // 'null' => false.
8992 __ CompareRoot(rax, Heap::kNullValueRootIndex);
8993 __ j(equal, &false_result);
8994
8995 // Get the map and type of the heap object.
8996 // We don't use CmpObjectType because we manipulate the type field.
8997 __ movq(rdx, FieldOperand(rax, HeapObject::kMapOffset));
8998 __ movzxbq(rcx, FieldOperand(rdx, Map::kInstanceTypeOffset));
8999
9000 // Undetectable => false.
9001 __ movzxbq(rbx, FieldOperand(rdx, Map::kBitFieldOffset));
9002 __ and_(rbx, Immediate(1 << Map::kIsUndetectable));
9003 __ j(not_zero, &false_result);
9004
9005 // JavaScript object => true.
9006 __ cmpq(rcx, Immediate(FIRST_JS_OBJECT_TYPE));
9007 __ j(above_equal, &true_result);
9008
9009 // String value => false iff empty.
9010 __ cmpq(rcx, Immediate(FIRST_NONSTRING_TYPE));
9011 __ j(above_equal, &not_string);
9012 __ movq(rdx, FieldOperand(rax, String::kLengthOffset));
9013 __ SmiTest(rdx);
9014 __ j(zero, &false_result);
9015 __ jmp(&true_result);
9016
9017 __ bind(&not_string);
9018 __ CompareRoot(rdx, Heap::kHeapNumberMapRootIndex);
9019 __ j(not_equal, &true_result);
9020 // HeapNumber => false iff +0, -0, or NaN.
9021 // These three cases set the zero flag when compared to zero using ucomisd.
9022 __ xorpd(xmm0, xmm0);
9023 __ ucomisd(xmm0, FieldOperand(rax, HeapNumber::kValueOffset));
9024 __ j(zero, &false_result);
9025 // Fall through to |true_result|.
9026
9027 // Return 1/0 for true/false in rax.
9028 __ bind(&true_result);
9029 __ movq(rax, Immediate(1));
9030 __ ret(1 * kPointerSize);
9031 __ bind(&false_result);
9032 __ xor_(rax, rax);
9033 __ ret(1 * kPointerSize);
9034 }
9035
9036
9037 void GenericBinaryOpStub::GenerateCall(
9038 MacroAssembler* masm,
9039 Register left,
9040 Register right) {
9041 if (!ArgsInRegistersSupported()) {
9042 // Pass arguments on the stack.
9043 __ push(left);
9044 __ push(right);
9045 } else {
9046 // The calling convention with registers is left in rdx and right in rax.
9047 Register left_arg = rdx;
9048 Register right_arg = rax;
9049 if (!(left.is(left_arg) && right.is(right_arg))) {
9050 if (left.is(right_arg) && right.is(left_arg)) {
9051 if (IsOperationCommutative()) {
9052 SetArgsReversed();
9053 } else {
9054 __ xchg(left, right);
9055 }
9056 } else if (left.is(left_arg)) {
9057 __ movq(right_arg, right);
9058 } else if (right.is(right_arg)) {
9059 __ movq(left_arg, left);
9060 } else if (left.is(right_arg)) {
9061 if (IsOperationCommutative()) {
9062 __ movq(left_arg, right);
9063 SetArgsReversed();
9064 } else {
9065 // Order of moves important to avoid destroying left argument.
9066 __ movq(left_arg, left);
9067 __ movq(right_arg, right);
9068 }
9069 } else if (right.is(left_arg)) {
9070 if (IsOperationCommutative()) {
9071 __ movq(right_arg, left);
9072 SetArgsReversed();
9073 } else {
9074 // Order of moves important to avoid destroying right argument.
9075 __ movq(right_arg, right);
9076 __ movq(left_arg, left);
9077 }
9078 } else {
9079 // Order of moves is not important.
9080 __ movq(left_arg, left);
9081 __ movq(right_arg, right);
9082 }
9083 }
9084
9085 // Update flags to indicate that arguments are in registers.
9086 SetArgsInRegisters();
9087 __ IncrementCounter(&Counters::generic_binary_stub_calls_regs, 1);
9088 }
9089
9090 // Call the stub.
9091 __ CallStub(this);
9092 }
9093
9094
9095 void GenericBinaryOpStub::GenerateCall(
9096 MacroAssembler* masm,
9097 Register left,
9098 Smi* right) {
9099 if (!ArgsInRegistersSupported()) {
9100 // Pass arguments on the stack.
9101 __ push(left);
9102 __ Push(right);
9103 } else {
9104 // The calling convention with registers is left in rdx and right in rax.
9105 Register left_arg = rdx;
9106 Register right_arg = rax;
9107 if (left.is(left_arg)) {
9108 __ Move(right_arg, right);
9109 } else if (left.is(right_arg) && IsOperationCommutative()) {
9110 __ Move(left_arg, right);
9111 SetArgsReversed();
9112 } else {
9113 // For non-commutative operations, left and right_arg might be
9114 // the same register. Therefore, the order of the moves is
9115 // important here in order to not overwrite left before moving
9116 // it to left_arg.
9117 __ movq(left_arg, left);
9118 __ Move(right_arg, right);
9119 }
9120
9121 // Update flags to indicate that arguments are in registers.
9122 SetArgsInRegisters();
9123 __ IncrementCounter(&Counters::generic_binary_stub_calls_regs, 1);
9124 }
9125
9126 // Call the stub.
9127 __ CallStub(this);
9128 }
9129
9130
9131 void GenericBinaryOpStub::GenerateCall(
9132 MacroAssembler* masm,
9133 Smi* left,
9134 Register right) {
9135 if (!ArgsInRegistersSupported()) {
9136 // Pass arguments on the stack.
9137 __ Push(left);
9138 __ push(right);
9139 } else {
9140 // The calling convention with registers is left in rdx and right in rax.
9141 Register left_arg = rdx;
9142 Register right_arg = rax;
9143 if (right.is(right_arg)) {
9144 __ Move(left_arg, left);
9145 } else if (right.is(left_arg) && IsOperationCommutative()) {
9146 __ Move(right_arg, left);
9147 SetArgsReversed();
9148 } else {
9149 // For non-commutative operations, right and left_arg might be
9150 // the same register. Therefore, the order of the moves is
9151 // important here in order to not overwrite right before moving
9152 // it to right_arg.
9153 __ movq(right_arg, right);
9154 __ Move(left_arg, left);
9155 }
9156 // Update flags to indicate that arguments are in registers.
9157 SetArgsInRegisters();
9158 __ IncrementCounter(&Counters::generic_binary_stub_calls_regs, 1);
9159 }
9160
9161 // Call the stub.
9162 __ CallStub(this);
9163 }
9164
9165
9166 Result GenericBinaryOpStub::GenerateCall(MacroAssembler* masm, 8774 Result GenericBinaryOpStub::GenerateCall(MacroAssembler* masm,
9167 VirtualFrame* frame, 8775 VirtualFrame* frame,
9168 Result* left, 8776 Result* left,
9169 Result* right) { 8777 Result* right) {
9170 if (ArgsInRegistersSupported()) { 8778 if (ArgsInRegistersSupported()) {
9171 SetArgsInRegisters(); 8779 SetArgsInRegisters();
9172 return frame->CallStub(this, left, right); 8780 return frame->CallStub(this, left, right);
9173 } else { 8781 } else {
9174 frame->Push(left); 8782 frame->Push(left);
9175 frame->Push(right); 8783 frame->Push(right);
9176 return frame->CallStub(this, 2); 8784 return frame->CallStub(this, 2);
9177 } 8785 }
9178 } 8786 }
9179 8787
9180
9181 void GenericBinaryOpStub::GenerateSmiCode(MacroAssembler* masm, Label* slow) {
9182 // 1. Move arguments into rdx, rax except for DIV and MOD, which need the
9183 // dividend in rax and rdx free for the division. Use rax, rbx for those.
9184 Comment load_comment(masm, "-- Load arguments");
9185 Register left = rdx;
9186 Register right = rax;
9187 if (op_ == Token::DIV || op_ == Token::MOD) {
9188 left = rax;
9189 right = rbx;
9190 if (HasArgsInRegisters()) {
9191 __ movq(rbx, rax);
9192 __ movq(rax, rdx);
9193 }
9194 }
9195 if (!HasArgsInRegisters()) {
9196 __ movq(right, Operand(rsp, 1 * kPointerSize));
9197 __ movq(left, Operand(rsp, 2 * kPointerSize));
9198 }
9199
9200 Label not_smis;
9201 // 2. Smi check both operands.
9202 if (static_operands_type_.IsSmi()) {
9203 // Skip smi check if we know that both arguments are smis.
9204 if (FLAG_debug_code) {
9205 __ AbortIfNotSmi(left);
9206 __ AbortIfNotSmi(right);
9207 }
9208 if (op_ == Token::BIT_OR) {
9209 // Handle OR here, since we do extra smi-checking in the or code below.
9210 __ SmiOr(right, right, left);
9211 GenerateReturn(masm);
9212 return;
9213 }
9214 } else {
9215 if (op_ != Token::BIT_OR) {
9216 // Skip the check for OR as it is better combined with the
9217 // actual operation.
9218 Comment smi_check_comment(masm, "-- Smi check arguments");
9219 __ JumpIfNotBothSmi(left, right, &not_smis);
9220 }
9221 }
9222
9223 // 3. Operands are both smis (except for OR), perform the operation leaving
9224 // the result in rax and check the result if necessary.
9225 Comment perform_smi(masm, "-- Perform smi operation");
9226 Label use_fp_on_smis;
9227 switch (op_) {
9228 case Token::ADD: {
9229 ASSERT(right.is(rax));
9230 __ SmiAdd(right, right, left, &use_fp_on_smis); // ADD is commutative.
9231 break;
9232 }
9233
9234 case Token::SUB: {
9235 __ SmiSub(left, left, right, &use_fp_on_smis);
9236 __ movq(rax, left);
9237 break;
9238 }
9239
9240 case Token::MUL:
9241 ASSERT(right.is(rax));
9242 __ SmiMul(right, right, left, &use_fp_on_smis); // MUL is commutative.
9243 break;
9244
9245 case Token::DIV:
9246 ASSERT(left.is(rax));
9247 __ SmiDiv(left, left, right, &use_fp_on_smis);
9248 break;
9249
9250 case Token::MOD:
9251 ASSERT(left.is(rax));
9252 __ SmiMod(left, left, right, slow);
9253 break;
9254
9255 case Token::BIT_OR:
9256 ASSERT(right.is(rax));
9257 __ movq(rcx, right); // Save the right operand.
9258 __ SmiOr(right, right, left); // BIT_OR is commutative.
9259 __ testb(right, Immediate(kSmiTagMask));
9260 __ j(not_zero, &not_smis);
9261 break;
9262
9263 case Token::BIT_AND:
9264 ASSERT(right.is(rax));
9265 __ SmiAnd(right, right, left); // BIT_AND is commutative.
9266 break;
9267
9268 case Token::BIT_XOR:
9269 ASSERT(right.is(rax));
9270 __ SmiXor(right, right, left); // BIT_XOR is commutative.
9271 break;
9272
9273 case Token::SHL:
9274 case Token::SHR:
9275 case Token::SAR:
9276 switch (op_) {
9277 case Token::SAR:
9278 __ SmiShiftArithmeticRight(left, left, right);
9279 break;
9280 case Token::SHR:
9281 __ SmiShiftLogicalRight(left, left, right, slow);
9282 break;
9283 case Token::SHL:
9284 __ SmiShiftLeft(left, left, right);
9285 break;
9286 default:
9287 UNREACHABLE();
9288 }
9289 __ movq(rax, left);
9290 break;
9291
9292 default:
9293 UNREACHABLE();
9294 break;
9295 }
9296
9297 // 4. Emit return of result in rax.
9298 GenerateReturn(masm);
9299
9300 // 5. For some operations emit inline code to perform floating point
9301 // operations on known smis (e.g., if the result of the operation
9302 // overflowed the smi range).
9303 switch (op_) {
9304 case Token::ADD:
9305 case Token::SUB:
9306 case Token::MUL:
9307 case Token::DIV: {
9308 ASSERT(use_fp_on_smis.is_linked());
9309 __ bind(&use_fp_on_smis);
9310 if (op_ == Token::DIV) {
9311 __ movq(rdx, rax);
9312 __ movq(rax, rbx);
9313 }
9314 // left is rdx, right is rax.
9315 __ AllocateHeapNumber(rbx, rcx, slow);
9316 FloatingPointHelper::LoadSSE2SmiOperands(masm);
9317 switch (op_) {
9318 case Token::ADD: __ addsd(xmm0, xmm1); break;
9319 case Token::SUB: __ subsd(xmm0, xmm1); break;
9320 case Token::MUL: __ mulsd(xmm0, xmm1); break;
9321 case Token::DIV: __ divsd(xmm0, xmm1); break;
9322 default: UNREACHABLE();
9323 }
9324 __ movsd(FieldOperand(rbx, HeapNumber::kValueOffset), xmm0);
9325 __ movq(rax, rbx);
9326 GenerateReturn(masm);
9327 }
9328 default:
9329 break;
9330 }
9331
9332 // 6. Non-smi operands, fall out to the non-smi code with the operands in
9333 // rdx and rax.
9334 Comment done_comment(masm, "-- Enter non-smi code");
9335 __ bind(&not_smis);
9336
9337 switch (op_) {
9338 case Token::DIV:
9339 case Token::MOD:
9340 // Operands are in rax, rbx at this point.
9341 __ movq(rdx, rax);
9342 __ movq(rax, rbx);
9343 break;
9344
9345 case Token::BIT_OR:
9346 // Right operand is saved in rcx and rax was destroyed by the smi
9347 // operation.
9348 __ movq(rax, rcx);
9349 break;
9350
9351 default:
9352 break;
9353 }
9354 }
9355
9356
9357 void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
9358 Label call_runtime;
9359
9360 if (ShouldGenerateSmiCode()) {
9361 GenerateSmiCode(masm, &call_runtime);
9362 } else if (op_ != Token::MOD) {
9363 if (!HasArgsInRegisters()) {
9364 GenerateLoadArguments(masm);
9365 }
9366 }
9367 // Floating point case.
9368 if (ShouldGenerateFPCode()) {
9369 switch (op_) {
9370 case Token::ADD:
9371 case Token::SUB:
9372 case Token::MUL:
9373 case Token::DIV: {
9374 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
9375 HasSmiCodeInStub()) {
9376 // Execution reaches this point when the first non-smi argument occurs
9377 // (and only if smi code is generated). This is the right moment to
9378 // patch to HEAP_NUMBERS state. The transition is attempted only for
9379 // the four basic operations. The stub stays in the DEFAULT state
9380 // forever for all other operations (also if smi code is skipped).
9381 GenerateTypeTransition(masm);
9382 break;
9383 }
9384
9385 Label not_floats;
9386 // rax: y
9387 // rdx: x
9388 if (static_operands_type_.IsNumber()) {
9389 if (FLAG_debug_code) {
9390 // Assert at runtime that inputs are only numbers.
9391 __ AbortIfNotNumber(rdx);
9392 __ AbortIfNotNumber(rax);
9393 }
9394 FloatingPointHelper::LoadSSE2NumberOperands(masm);
9395 } else {
9396 FloatingPointHelper::LoadSSE2UnknownOperands(masm, &call_runtime);
9397 }
9398
9399 switch (op_) {
9400 case Token::ADD: __ addsd(xmm0, xmm1); break;
9401 case Token::SUB: __ subsd(xmm0, xmm1); break;
9402 case Token::MUL: __ mulsd(xmm0, xmm1); break;
9403 case Token::DIV: __ divsd(xmm0, xmm1); break;
9404 default: UNREACHABLE();
9405 }
9406 // Allocate a heap number, if needed.
9407 Label skip_allocation;
9408 OverwriteMode mode = mode_;
9409 if (HasArgsReversed()) {
9410 if (mode == OVERWRITE_RIGHT) {
9411 mode = OVERWRITE_LEFT;
9412 } else if (mode == OVERWRITE_LEFT) {
9413 mode = OVERWRITE_RIGHT;
9414 }
9415 }
9416 switch (mode) {
9417 case OVERWRITE_LEFT:
9418 __ JumpIfNotSmi(rdx, &skip_allocation);
9419 __ AllocateHeapNumber(rbx, rcx, &call_runtime);
9420 __ movq(rdx, rbx);
9421 __ bind(&skip_allocation);
9422 __ movq(rax, rdx);
9423 break;
9424 case OVERWRITE_RIGHT:
9425 // If the argument in rax is already an object, we skip the
9426 // allocation of a heap number.
9427 __ JumpIfNotSmi(rax, &skip_allocation);
9428 // Fall through!
9429 case NO_OVERWRITE:
9430 // Allocate a heap number for the result. Keep rax and rdx intact
9431 // for the possible runtime call.
9432 __ AllocateHeapNumber(rbx, rcx, &call_runtime);
9433 __ movq(rax, rbx);
9434 __ bind(&skip_allocation);
9435 break;
9436 default: UNREACHABLE();
9437 }
9438 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm0);
9439 GenerateReturn(masm);
9440 __ bind(&not_floats);
9441 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
9442 !HasSmiCodeInStub()) {
9443 // Execution reaches this point when the first non-number argument
9444 // occurs (and only if smi code is skipped from the stub, otherwise
9445 // the patching has already been done earlier in this case branch).
9446 // A perfect moment to try patching to STRINGS for ADD operation.
9447 if (op_ == Token::ADD) {
9448 GenerateTypeTransition(masm);
9449 }
9450 }
9451 break;
9452 }
9453 case Token::MOD: {
9454 // For MOD we go directly to runtime in the non-smi case.
9455 break;
9456 }
9457 case Token::BIT_OR:
9458 case Token::BIT_AND:
9459 case Token::BIT_XOR:
9460 case Token::SAR:
9461 case Token::SHL:
9462 case Token::SHR: {
9463 Label skip_allocation, non_smi_shr_result;
9464 Register heap_number_map = r9;
9465 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9466 if (static_operands_type_.IsNumber()) {
9467 if (FLAG_debug_code) {
9468 // Assert at runtime that inputs are only numbers.
9469 __ AbortIfNotNumber(rdx);
9470 __ AbortIfNotNumber(rax);
9471 }
9472 FloatingPointHelper::LoadNumbersAsIntegers(masm);
9473 } else {
9474 FloatingPointHelper::LoadAsIntegers(masm,
9475 &call_runtime,
9476 heap_number_map);
9477 }
9478 switch (op_) {
9479 case Token::BIT_OR: __ orl(rax, rcx); break;
9480 case Token::BIT_AND: __ andl(rax, rcx); break;
9481 case Token::BIT_XOR: __ xorl(rax, rcx); break;
9482 case Token::SAR: __ sarl_cl(rax); break;
9483 case Token::SHL: __ shll_cl(rax); break;
9484 case Token::SHR: {
9485 __ shrl_cl(rax);
9486 // Check if result is negative. This can only happen for a shift
9487 // by zero.
9488 __ testl(rax, rax);
9489 __ j(negative, &non_smi_shr_result);
9490 break;
9491 }
9492 default: UNREACHABLE();
9493 }
9494
9495 STATIC_ASSERT(kSmiValueSize == 32);
9496 // Tag smi result and return.
9497 __ Integer32ToSmi(rax, rax);
9498 GenerateReturn(masm);
9499
9500 // All bit-ops except SHR return a signed int32 that can be
9501 // returned immediately as a smi.
9502 // We might need to allocate a HeapNumber if we shift a negative
9503 // number right by zero (i.e., convert to UInt32).
9504 if (op_ == Token::SHR) {
9505 ASSERT(non_smi_shr_result.is_linked());
9506 __ bind(&non_smi_shr_result);
9507 // Allocate a heap number if needed.
9508 __ movl(rbx, rax); // rbx holds result value (uint32 value as int64).
9509 switch (mode_) {
9510 case OVERWRITE_LEFT:
9511 case OVERWRITE_RIGHT:
9512 // If the operand was an object, we skip the
9513 // allocation of a heap number.
9514 __ movq(rax, Operand(rsp, mode_ == OVERWRITE_RIGHT ?
9515 1 * kPointerSize : 2 * kPointerSize));
9516 __ JumpIfNotSmi(rax, &skip_allocation);
9517 // Fall through!
9518 case NO_OVERWRITE:
9519 // Allocate heap number in new space.
9520 // Not using AllocateHeapNumber macro in order to reuse
9521 // already loaded heap_number_map.
9522 __ AllocateInNewSpace(HeapNumber::kSize,
9523 rax,
9524 rcx,
9525 no_reg,
9526 &call_runtime,
9527 TAG_OBJECT);
9528 // Set the map.
9529 if (FLAG_debug_code) {
9530 __ AbortIfNotRootValue(heap_number_map,
9531 Heap::kHeapNumberMapRootIndex,
9532 "HeapNumberMap register clobbered.");
9533 }
9534 __ movq(FieldOperand(rax, HeapObject::kMapOffset),
9535 heap_number_map);
9536 __ bind(&skip_allocation);
9537 break;
9538 default: UNREACHABLE();
9539 }
9540 // Store the result in the HeapNumber and return.
9541 __ cvtqsi2sd(xmm0, rbx);
9542 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm0);
9543 GenerateReturn(masm);
9544 }
9545
9546 break;
9547 }
9548 default: UNREACHABLE(); break;
9549 }
9550 }
9551
9552 // If all else fails, use the runtime system to get the correct
9553 // result. If arguments was passed in registers now place them on the
9554 // stack in the correct order below the return address.
9555 __ bind(&call_runtime);
9556
9557 if (HasArgsInRegisters()) {
9558 GenerateRegisterArgsPush(masm);
9559 }
9560
9561 switch (op_) {
9562 case Token::ADD: {
9563 // Registers containing left and right operands respectively.
9564 Register lhs, rhs;
9565
9566 if (HasArgsReversed()) {
9567 lhs = rax;
9568 rhs = rdx;
9569 } else {
9570 lhs = rdx;
9571 rhs = rax;
9572 }
9573
9574 // Test for string arguments before calling runtime.
9575 Label not_strings, both_strings, not_string1, string1, string1_smi2;
9576
9577 // If this stub has already generated FP-specific code then the arguments
9578 // are already in rdx and rax.
9579 if (!ShouldGenerateFPCode() && !HasArgsInRegisters()) {
9580 GenerateLoadArguments(masm);
9581 }
9582
9583 Condition is_smi;
9584 is_smi = masm->CheckSmi(lhs);
9585 __ j(is_smi, &not_string1);
9586 __ CmpObjectType(lhs, FIRST_NONSTRING_TYPE, r8);
9587 __ j(above_equal, &not_string1);
9588
9589 // First argument is a a string, test second.
9590 is_smi = masm->CheckSmi(rhs);
9591 __ j(is_smi, &string1_smi2);
9592 __ CmpObjectType(rhs, FIRST_NONSTRING_TYPE, r9);
9593 __ j(above_equal, &string1);
9594
9595 // First and second argument are strings.
9596 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
9597 __ TailCallStub(&string_add_stub);
9598
9599 __ bind(&string1_smi2);
9600 // First argument is a string, second is a smi. Try to lookup the number
9601 // string for the smi in the number string cache.
9602 NumberToStringStub::GenerateLookupNumberStringCache(
9603 masm, rhs, rbx, rcx, r8, true, &string1);
9604
9605 // Replace second argument on stack and tailcall string add stub to make
9606 // the result.
9607 __ movq(Operand(rsp, 1 * kPointerSize), rbx);
9608 __ TailCallStub(&string_add_stub);
9609
9610 // Only first argument is a string.
9611 __ bind(&string1);
9612 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_FUNCTION);
9613
9614 // First argument was not a string, test second.
9615 __ bind(&not_string1);
9616 is_smi = masm->CheckSmi(rhs);
9617 __ j(is_smi, &not_strings);
9618 __ CmpObjectType(rhs, FIRST_NONSTRING_TYPE, rhs);
9619 __ j(above_equal, &not_strings);
9620
9621 // Only second argument is a string.
9622 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_FUNCTION);
9623
9624 __ bind(&not_strings);
9625 // Neither argument is a string.
9626 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
9627 break;
9628 }
9629 case Token::SUB:
9630 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
9631 break;
9632 case Token::MUL:
9633 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
9634 break;
9635 case Token::DIV:
9636 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
9637 break;
9638 case Token::MOD:
9639 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
9640 break;
9641 case Token::BIT_OR:
9642 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
9643 break;
9644 case Token::BIT_AND:
9645 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
9646 break;
9647 case Token::BIT_XOR:
9648 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
9649 break;
9650 case Token::SAR:
9651 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
9652 break;
9653 case Token::SHL:
9654 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
9655 break;
9656 case Token::SHR:
9657 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
9658 break;
9659 default:
9660 UNREACHABLE();
9661 }
9662 }
9663
9664
9665 void GenericBinaryOpStub::GenerateLoadArguments(MacroAssembler* masm) {
9666 ASSERT(!HasArgsInRegisters());
9667 __ movq(rax, Operand(rsp, 1 * kPointerSize));
9668 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
9669 }
9670
9671
9672 void GenericBinaryOpStub::GenerateReturn(MacroAssembler* masm) {
9673 // If arguments are not passed in registers remove them from the stack before
9674 // returning.
9675 if (!HasArgsInRegisters()) {
9676 __ ret(2 * kPointerSize); // Remove both operands
9677 } else {
9678 __ ret(0);
9679 }
9680 }
9681
9682
9683 void GenericBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
9684 ASSERT(HasArgsInRegisters());
9685 __ pop(rcx);
9686 if (HasArgsReversed()) {
9687 __ push(rax);
9688 __ push(rdx);
9689 } else {
9690 __ push(rdx);
9691 __ push(rax);
9692 }
9693 __ push(rcx);
9694 }
9695
9696
9697 void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
9698 Label get_result;
9699
9700 // Ensure the operands are on the stack.
9701 if (HasArgsInRegisters()) {
9702 GenerateRegisterArgsPush(masm);
9703 }
9704
9705 // Left and right arguments are already on stack.
9706 __ pop(rcx); // Save the return address.
9707
9708 // Push this stub's key.
9709 __ Push(Smi::FromInt(MinorKey()));
9710
9711 // Although the operation and the type info are encoded into the key,
9712 // the encoding is opaque, so push them too.
9713 __ Push(Smi::FromInt(op_));
9714
9715 __ Push(Smi::FromInt(runtime_operands_type_));
9716
9717 __ push(rcx); // The return address.
9718
9719 // Perform patching to an appropriate fast case and return the result.
9720 __ TailCallExternalReference(
9721 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
9722 5,
9723 1);
9724 }
9725
9726
9727 Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
9728 GenericBinaryOpStub stub(key, type_info);
9729 return stub.GetCode();
9730 }
9731
9732
9733 void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
9734 // Input on stack:
9735 // rsp[8]: argument (should be number).
9736 // rsp[0]: return address.
9737 Label runtime_call;
9738 Label runtime_call_clear_stack;
9739 Label input_not_smi;
9740 Label loaded;
9741 // Test that rax is a number.
9742 __ movq(rax, Operand(rsp, kPointerSize));
9743 __ JumpIfNotSmi(rax, &input_not_smi);
9744 // Input is a smi. Untag and load it onto the FPU stack.
9745 // Then load the bits of the double into rbx.
9746 __ SmiToInteger32(rax, rax);
9747 __ subq(rsp, Immediate(kPointerSize));
9748 __ cvtlsi2sd(xmm1, rax);
9749 __ movsd(Operand(rsp, 0), xmm1);
9750 __ movq(rbx, xmm1);
9751 __ movq(rdx, xmm1);
9752 __ fld_d(Operand(rsp, 0));
9753 __ addq(rsp, Immediate(kPointerSize));
9754 __ jmp(&loaded);
9755
9756 __ bind(&input_not_smi);
9757 // Check if input is a HeapNumber.
9758 __ Move(rbx, Factory::heap_number_map());
9759 __ cmpq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
9760 __ j(not_equal, &runtime_call);
9761 // Input is a HeapNumber. Push it on the FPU stack and load its
9762 // bits into rbx.
9763 __ fld_d(FieldOperand(rax, HeapNumber::kValueOffset));
9764 __ movq(rbx, FieldOperand(rax, HeapNumber::kValueOffset));
9765 __ movq(rdx, rbx);
9766 __ bind(&loaded);
9767 // ST[0] == double value
9768 // rbx = bits of double value.
9769 // rdx = also bits of double value.
9770 // Compute hash (h is 32 bits, bits are 64 and the shifts are arithmetic):
9771 // h = h0 = bits ^ (bits >> 32);
9772 // h ^= h >> 16;
9773 // h ^= h >> 8;
9774 // h = h & (cacheSize - 1);
9775 // or h = (h0 ^ (h0 >> 8) ^ (h0 >> 16) ^ (h0 >> 24)) & (cacheSize - 1)
9776 __ sar(rdx, Immediate(32));
9777 __ xorl(rdx, rbx);
9778 __ movl(rcx, rdx);
9779 __ movl(rax, rdx);
9780 __ movl(rdi, rdx);
9781 __ sarl(rdx, Immediate(8));
9782 __ sarl(rcx, Immediate(16));
9783 __ sarl(rax, Immediate(24));
9784 __ xorl(rcx, rdx);
9785 __ xorl(rax, rdi);
9786 __ xorl(rcx, rax);
9787 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
9788 __ andl(rcx, Immediate(TranscendentalCache::kCacheSize - 1));
9789
9790 // ST[0] == double value.
9791 // rbx = bits of double value.
9792 // rcx = TranscendentalCache::hash(double value).
9793 __ movq(rax, ExternalReference::transcendental_cache_array_address());
9794 // rax points to cache array.
9795 __ movq(rax, Operand(rax, type_ * sizeof(TranscendentalCache::caches_[0])));
9796 // rax points to the cache for the type type_.
9797 // If NULL, the cache hasn't been initialized yet, so go through runtime.
9798 __ testq(rax, rax);
9799 __ j(zero, &runtime_call_clear_stack);
9800 #ifdef DEBUG
9801 // Check that the layout of cache elements match expectations.
9802 { // NOLINT - doesn't like a single brace on a line.
9803 TranscendentalCache::Element test_elem[2];
9804 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
9805 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
9806 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
9807 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
9808 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
9809 // Two uint_32's and a pointer per element.
9810 CHECK_EQ(16, static_cast<int>(elem2_start - elem_start));
9811 CHECK_EQ(0, static_cast<int>(elem_in0 - elem_start));
9812 CHECK_EQ(kIntSize, static_cast<int>(elem_in1 - elem_start));
9813 CHECK_EQ(2 * kIntSize, static_cast<int>(elem_out - elem_start));
9814 }
9815 #endif
9816 // Find the address of the rcx'th entry in the cache, i.e., &rax[rcx*16].
9817 __ addl(rcx, rcx);
9818 __ lea(rcx, Operand(rax, rcx, times_8, 0));
9819 // Check if cache matches: Double value is stored in uint32_t[2] array.
9820 Label cache_miss;
9821 __ cmpq(rbx, Operand(rcx, 0));
9822 __ j(not_equal, &cache_miss);
9823 // Cache hit!
9824 __ movq(rax, Operand(rcx, 2 * kIntSize));
9825 __ fstp(0); // Clear FPU stack.
9826 __ ret(kPointerSize);
9827
9828 __ bind(&cache_miss);
9829 // Update cache with new value.
9830 Label nan_result;
9831 GenerateOperation(masm, &nan_result);
9832 __ AllocateHeapNumber(rax, rdi, &runtime_call_clear_stack);
9833 __ movq(Operand(rcx, 0), rbx);
9834 __ movq(Operand(rcx, 2 * kIntSize), rax);
9835 __ fstp_d(FieldOperand(rax, HeapNumber::kValueOffset));
9836 __ ret(kPointerSize);
9837
9838 __ bind(&runtime_call_clear_stack);
9839 __ fstp(0);
9840 __ bind(&runtime_call);
9841 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
9842
9843 __ bind(&nan_result);
9844 __ fstp(0); // Remove argument from FPU stack.
9845 __ LoadRoot(rax, Heap::kNanValueRootIndex);
9846 __ movq(Operand(rcx, 0), rbx);
9847 __ movq(Operand(rcx, 2 * kIntSize), rax);
9848 __ ret(kPointerSize);
9849 }
9850
9851
9852 Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
9853 switch (type_) {
9854 // Add more cases when necessary.
9855 case TranscendentalCache::SIN: return Runtime::kMath_sin;
9856 case TranscendentalCache::COS: return Runtime::kMath_cos;
9857 default:
9858 UNIMPLEMENTED();
9859 return Runtime::kAbort;
9860 }
9861 }
9862
9863
9864 void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm,
9865 Label* on_nan_result) {
9866 // Registers:
9867 // rbx: Bits of input double. Must be preserved.
9868 // rcx: Pointer to cache entry. Must be preserved.
9869 // st(0): Input double
9870 Label done;
9871 ASSERT(type_ == TranscendentalCache::SIN ||
9872 type_ == TranscendentalCache::COS);
9873 // More transcendental types can be added later.
9874
9875 // Both fsin and fcos require arguments in the range +/-2^63 and
9876 // return NaN for infinities and NaN. They can share all code except
9877 // the actual fsin/fcos operation.
9878 Label in_range;
9879 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
9880 // work. We must reduce it to the appropriate range.
9881 __ movq(rdi, rbx);
9882 // Move exponent and sign bits to low bits.
9883 __ shr(rdi, Immediate(HeapNumber::kMantissaBits));
9884 // Remove sign bit.
9885 __ andl(rdi, Immediate((1 << HeapNumber::kExponentBits) - 1));
9886 int supported_exponent_limit = (63 + HeapNumber::kExponentBias);
9887 __ cmpl(rdi, Immediate(supported_exponent_limit));
9888 __ j(below, &in_range);
9889 // Check for infinity and NaN. Both return NaN for sin.
9890 __ cmpl(rdi, Immediate(0x7ff));
9891 __ j(equal, on_nan_result);
9892
9893 // Use fpmod to restrict argument to the range +/-2*PI.
9894 __ fldpi();
9895 __ fadd(0);
9896 __ fld(1);
9897 // FPU Stack: input, 2*pi, input.
9898 {
9899 Label no_exceptions;
9900 __ fwait();
9901 __ fnstsw_ax();
9902 // Clear if Illegal Operand or Zero Division exceptions are set.
9903 __ testl(rax, Immediate(5)); // #IO and #ZD flags of FPU status word.
9904 __ j(zero, &no_exceptions);
9905 __ fnclex();
9906 __ bind(&no_exceptions);
9907 }
9908
9909 // Compute st(0) % st(1)
9910 {
9911 Label partial_remainder_loop;
9912 __ bind(&partial_remainder_loop);
9913 __ fprem1();
9914 __ fwait();
9915 __ fnstsw_ax();
9916 __ testl(rax, Immediate(0x400)); // Check C2 bit of FPU status word.
9917 // If C2 is set, computation only has partial result. Loop to
9918 // continue computation.
9919 __ j(not_zero, &partial_remainder_loop);
9920 }
9921 // FPU Stack: input, 2*pi, input % 2*pi
9922 __ fstp(2);
9923 // FPU Stack: input % 2*pi, 2*pi,
9924 __ fstp(0);
9925 // FPU Stack: input % 2*pi
9926 __ bind(&in_range);
9927 switch (type_) {
9928 case TranscendentalCache::SIN:
9929 __ fsin();
9930 break;
9931 case TranscendentalCache::COS:
9932 __ fcos();
9933 break;
9934 default:
9935 UNREACHABLE();
9936 }
9937 __ bind(&done);
9938 }
9939
9940
9941 // Get the integer part of a heap number.
9942 // Overwrites the contents of rdi, rbx and rcx. Result cannot be rdi or rbx.
9943 void IntegerConvert(MacroAssembler* masm,
9944 Register result,
9945 Register source) {
9946 // Result may be rcx. If result and source are the same register, source will
9947 // be overwritten.
9948 ASSERT(!result.is(rdi) && !result.is(rbx));
9949 // TODO(lrn): When type info reaches here, if value is a 32-bit integer, use
9950 // cvttsd2si (32-bit version) directly.
9951 Register double_exponent = rbx;
9952 Register double_value = rdi;
9953 Label done, exponent_63_plus;
9954 // Get double and extract exponent.
9955 __ movq(double_value, FieldOperand(source, HeapNumber::kValueOffset));
9956 // Clear result preemptively, in case we need to return zero.
9957 __ xorl(result, result);
9958 __ movq(xmm0, double_value); // Save copy in xmm0 in case we need it there.
9959 // Double to remove sign bit, shift exponent down to least significant bits.
9960 // and subtract bias to get the unshifted, unbiased exponent.
9961 __ lea(double_exponent, Operand(double_value, double_value, times_1, 0));
9962 __ shr(double_exponent, Immediate(64 - HeapNumber::kExponentBits));
9963 __ subl(double_exponent, Immediate(HeapNumber::kExponentBias));
9964 // Check whether the exponent is too big for a 63 bit unsigned integer.
9965 __ cmpl(double_exponent, Immediate(63));
9966 __ j(above_equal, &exponent_63_plus);
9967 // Handle exponent range 0..62.
9968 __ cvttsd2siq(result, xmm0);
9969 __ jmp(&done);
9970
9971 __ bind(&exponent_63_plus);
9972 // Exponent negative or 63+.
9973 __ cmpl(double_exponent, Immediate(83));
9974 // If exponent negative or above 83, number contains no significant bits in
9975 // the range 0..2^31, so result is zero, and rcx already holds zero.
9976 __ j(above, &done);
9977
9978 // Exponent in rage 63..83.
9979 // Mantissa * 2^exponent contains bits in the range 2^0..2^31, namely
9980 // the least significant exponent-52 bits.
9981
9982 // Negate low bits of mantissa if value is negative.
9983 __ addq(double_value, double_value); // Move sign bit to carry.
9984 __ sbbl(result, result); // And convert carry to -1 in result register.
9985 // if scratch2 is negative, do (scratch2-1)^-1, otherwise (scratch2-0)^0.
9986 __ addl(double_value, result);
9987 // Do xor in opposite directions depending on where we want the result
9988 // (depending on whether result is rcx or not).
9989
9990 if (result.is(rcx)) {
9991 __ xorl(double_value, result);
9992 // Left shift mantissa by (exponent - mantissabits - 1) to save the
9993 // bits that have positional values below 2^32 (the extra -1 comes from the
9994 // doubling done above to move the sign bit into the carry flag).
9995 __ leal(rcx, Operand(double_exponent, -HeapNumber::kMantissaBits - 1));
9996 __ shll_cl(double_value);
9997 __ movl(result, double_value);
9998 } else {
9999 // As the then-branch, but move double-value to result before shifting.
10000 __ xorl(result, double_value);
10001 __ leal(rcx, Operand(double_exponent, -HeapNumber::kMantissaBits - 1));
10002 __ shll_cl(result);
10003 }
10004
10005 __ bind(&done);
10006 }
10007
10008
10009 // Input: rdx, rax are the left and right objects of a bit op.
10010 // Output: rax, rcx are left and right integers for a bit op.
10011 void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm) {
10012 // Check float operands.
10013 Label done;
10014 Label rax_is_smi;
10015 Label rax_is_object;
10016 Label rdx_is_object;
10017
10018 __ JumpIfNotSmi(rdx, &rdx_is_object);
10019 __ SmiToInteger32(rdx, rdx);
10020 __ JumpIfSmi(rax, &rax_is_smi);
10021
10022 __ bind(&rax_is_object);
10023 IntegerConvert(masm, rcx, rax); // Uses rdi, rcx and rbx.
10024 __ jmp(&done);
10025
10026 __ bind(&rdx_is_object);
10027 IntegerConvert(masm, rdx, rdx); // Uses rdi, rcx and rbx.
10028 __ JumpIfNotSmi(rax, &rax_is_object);
10029 __ bind(&rax_is_smi);
10030 __ SmiToInteger32(rcx, rax);
10031
10032 __ bind(&done);
10033 __ movl(rax, rdx);
10034 }
10035
10036
10037 // Input: rdx, rax are the left and right objects of a bit op.
10038 // Output: rax, rcx are left and right integers for a bit op.
10039 void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
10040 Label* conversion_failure,
10041 Register heap_number_map) {
10042 // Check float operands.
10043 Label arg1_is_object, check_undefined_arg1;
10044 Label arg2_is_object, check_undefined_arg2;
10045 Label load_arg2, done;
10046
10047 __ JumpIfNotSmi(rdx, &arg1_is_object);
10048 __ SmiToInteger32(rdx, rdx);
10049 __ jmp(&load_arg2);
10050
10051 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
10052 __ bind(&check_undefined_arg1);
10053 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
10054 __ j(not_equal, conversion_failure);
10055 __ movl(rdx, Immediate(0));
10056 __ jmp(&load_arg2);
10057
10058 __ bind(&arg1_is_object);
10059 __ cmpq(FieldOperand(rdx, HeapObject::kMapOffset), heap_number_map);
10060 __ j(not_equal, &check_undefined_arg1);
10061 // Get the untagged integer version of the edx heap number in rcx.
10062 IntegerConvert(masm, rdx, rdx);
10063
10064 // Here rdx has the untagged integer, rax has a Smi or a heap number.
10065 __ bind(&load_arg2);
10066 // Test if arg2 is a Smi.
10067 __ JumpIfNotSmi(rax, &arg2_is_object);
10068 __ SmiToInteger32(rax, rax);
10069 __ movl(rcx, rax);
10070 __ jmp(&done);
10071
10072 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
10073 __ bind(&check_undefined_arg2);
10074 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
10075 __ j(not_equal, conversion_failure);
10076 __ movl(rcx, Immediate(0));
10077 __ jmp(&done);
10078
10079 __ bind(&arg2_is_object);
10080 __ cmpq(FieldOperand(rax, HeapObject::kMapOffset), heap_number_map);
10081 __ j(not_equal, &check_undefined_arg2);
10082 // Get the untagged integer version of the rax heap number in rcx.
10083 IntegerConvert(masm, rcx, rax);
10084 __ bind(&done);
10085 __ movl(rax, rdx);
10086 }
10087
10088
10089 void FloatingPointHelper::LoadSSE2SmiOperands(MacroAssembler* masm) {
10090 __ SmiToInteger32(kScratchRegister, rdx);
10091 __ cvtlsi2sd(xmm0, kScratchRegister);
10092 __ SmiToInteger32(kScratchRegister, rax);
10093 __ cvtlsi2sd(xmm1, kScratchRegister);
10094 }
10095
10096
10097 void FloatingPointHelper::LoadSSE2NumberOperands(MacroAssembler* masm) {
10098 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, done;
10099 // Load operand in rdx into xmm0.
10100 __ JumpIfSmi(rdx, &load_smi_rdx);
10101 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
10102 // Load operand in rax into xmm1.
10103 __ JumpIfSmi(rax, &load_smi_rax);
10104 __ bind(&load_nonsmi_rax);
10105 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
10106 __ jmp(&done);
10107
10108 __ bind(&load_smi_rdx);
10109 __ SmiToInteger32(kScratchRegister, rdx);
10110 __ cvtlsi2sd(xmm0, kScratchRegister);
10111 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
10112
10113 __ bind(&load_smi_rax);
10114 __ SmiToInteger32(kScratchRegister, rax);
10115 __ cvtlsi2sd(xmm1, kScratchRegister);
10116
10117 __ bind(&done);
10118 }
10119
10120
10121 void FloatingPointHelper::LoadSSE2UnknownOperands(MacroAssembler* masm,
10122 Label* not_numbers) {
10123 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, load_float_rax, done;
10124 // Load operand in rdx into xmm0, or branch to not_numbers.
10125 __ LoadRoot(rcx, Heap::kHeapNumberMapRootIndex);
10126 __ JumpIfSmi(rdx, &load_smi_rdx);
10127 __ cmpq(FieldOperand(rdx, HeapObject::kMapOffset), rcx);
10128 __ j(not_equal, not_numbers); // Argument in rdx is not a number.
10129 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
10130 // Load operand in rax into xmm1, or branch to not_numbers.
10131 __ JumpIfSmi(rax, &load_smi_rax);
10132
10133 __ bind(&load_nonsmi_rax);
10134 __ cmpq(FieldOperand(rax, HeapObject::kMapOffset), rcx);
10135 __ j(not_equal, not_numbers);
10136 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
10137 __ jmp(&done);
10138
10139 __ bind(&load_smi_rdx);
10140 __ SmiToInteger32(kScratchRegister, rdx);
10141 __ cvtlsi2sd(xmm0, kScratchRegister);
10142 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
10143
10144 __ bind(&load_smi_rax);
10145 __ SmiToInteger32(kScratchRegister, rax);
10146 __ cvtlsi2sd(xmm1, kScratchRegister);
10147 __ bind(&done);
10148 }
10149
10150
10151 void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
10152 Label slow, done;
10153
10154 if (op_ == Token::SUB) {
10155 // Check whether the value is a smi.
10156 Label try_float;
10157 __ JumpIfNotSmi(rax, &try_float);
10158
10159 if (negative_zero_ == kIgnoreNegativeZero) {
10160 __ SmiCompare(rax, Smi::FromInt(0));
10161 __ j(equal, &done);
10162 }
10163
10164 // Enter runtime system if the value of the smi is zero
10165 // to make sure that we switch between 0 and -0.
10166 // Also enter it if the value of the smi is Smi::kMinValue.
10167 __ SmiNeg(rax, rax, &done);
10168
10169 // Either zero or Smi::kMinValue, neither of which become a smi when
10170 // negated.
10171 if (negative_zero_ == kStrictNegativeZero) {
10172 __ SmiCompare(rax, Smi::FromInt(0));
10173 __ j(not_equal, &slow);
10174 __ Move(rax, Factory::minus_zero_value());
10175 __ jmp(&done);
10176 } else {
10177 __ jmp(&slow);
10178 }
10179
10180 // Try floating point case.
10181 __ bind(&try_float);
10182 __ movq(rdx, FieldOperand(rax, HeapObject::kMapOffset));
10183 __ CompareRoot(rdx, Heap::kHeapNumberMapRootIndex);
10184 __ j(not_equal, &slow);
10185 // Operand is a float, negate its value by flipping sign bit.
10186 __ movq(rdx, FieldOperand(rax, HeapNumber::kValueOffset));
10187 __ movq(kScratchRegister, Immediate(0x01));
10188 __ shl(kScratchRegister, Immediate(63));
10189 __ xor_(rdx, kScratchRegister); // Flip sign.
10190 // rdx is value to store.
10191 if (overwrite_ == UNARY_OVERWRITE) {
10192 __ movq(FieldOperand(rax, HeapNumber::kValueOffset), rdx);
10193 } else {
10194 __ AllocateHeapNumber(rcx, rbx, &slow);
10195 // rcx: allocated 'empty' number
10196 __ movq(FieldOperand(rcx, HeapNumber::kValueOffset), rdx);
10197 __ movq(rax, rcx);
10198 }
10199 } else if (op_ == Token::BIT_NOT) {
10200 // Check if the operand is a heap number.
10201 __ movq(rdx, FieldOperand(rax, HeapObject::kMapOffset));
10202 __ CompareRoot(rdx, Heap::kHeapNumberMapRootIndex);
10203 __ j(not_equal, &slow);
10204
10205 // Convert the heap number in rax to an untagged integer in rcx.
10206 IntegerConvert(masm, rax, rax);
10207
10208 // Do the bitwise operation and smi tag the result.
10209 __ notl(rax);
10210 __ Integer32ToSmi(rax, rax);
10211 }
10212
10213 // Return from the stub.
10214 __ bind(&done);
10215 __ StubReturn(1);
10216
10217 // Handle the slow case by jumping to the JavaScript builtin.
10218 __ bind(&slow);
10219 __ pop(rcx); // pop return address
10220 __ push(rax);
10221 __ push(rcx); // push return address
10222 switch (op_) {
10223 case Token::SUB:
10224 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
10225 break;
10226 case Token::BIT_NOT:
10227 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
10228 break;
10229 default:
10230 UNREACHABLE();
10231 }
10232 }
10233
10234
10235 void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
10236 // The key is in rdx and the parameter count is in rax.
10237
10238 // The displacement is used for skipping the frame pointer on the
10239 // stack. It is the offset of the last parameter (if any) relative
10240 // to the frame pointer.
10241 static const int kDisplacement = 1 * kPointerSize;
10242
10243 // Check that the key is a smi.
10244 Label slow;
10245 __ JumpIfNotSmi(rdx, &slow);
10246
10247 // Check if the calling frame is an arguments adaptor frame.
10248 Label adaptor;
10249 __ movq(rbx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
10250 __ SmiCompare(Operand(rbx, StandardFrameConstants::kContextOffset),
10251 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
10252 __ j(equal, &adaptor);
10253
10254 // Check index against formal parameters count limit passed in
10255 // through register rax. Use unsigned comparison to get negative
10256 // check for free.
10257 __ cmpq(rdx, rax);
10258 __ j(above_equal, &slow);
10259
10260 // Read the argument from the stack and return it.
10261 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
10262 __ lea(rbx, Operand(rbp, index.reg, index.scale, 0));
10263 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
10264 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
10265 __ Ret();
10266
10267 // Arguments adaptor case: Check index against actual arguments
10268 // limit found in the arguments adaptor frame. Use unsigned
10269 // comparison to get negative check for free.
10270 __ bind(&adaptor);
10271 __ movq(rcx, Operand(rbx, ArgumentsAdaptorFrameConstants::kLengthOffset));
10272 __ cmpq(rdx, rcx);
10273 __ j(above_equal, &slow);
10274
10275 // Read the argument from the stack and return it.
10276 index = masm->SmiToIndex(rax, rcx, kPointerSizeLog2);
10277 __ lea(rbx, Operand(rbx, index.reg, index.scale, 0));
10278 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
10279 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
10280 __ Ret();
10281
10282 // Slow-case: Handle non-smi or out-of-bounds access to arguments
10283 // by calling the runtime system.
10284 __ bind(&slow);
10285 __ pop(rbx); // Return address.
10286 __ push(rdx);
10287 __ push(rbx);
10288 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
10289 }
10290
10291
10292 void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
10293 // rsp[0] : return address
10294 // rsp[8] : number of parameters
10295 // rsp[16] : receiver displacement
10296 // rsp[24] : function
10297
10298 // The displacement is used for skipping the return address and the
10299 // frame pointer on the stack. It is the offset of the last
10300 // parameter (if any) relative to the frame pointer.
10301 static const int kDisplacement = 2 * kPointerSize;
10302
10303 // Check if the calling frame is an arguments adaptor frame.
10304 Label adaptor_frame, try_allocate, runtime;
10305 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
10306 __ SmiCompare(Operand(rdx, StandardFrameConstants::kContextOffset),
10307 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
10308 __ j(equal, &adaptor_frame);
10309
10310 // Get the length from the frame.
10311 __ SmiToInteger32(rcx, Operand(rsp, 1 * kPointerSize));
10312 __ jmp(&try_allocate);
10313
10314 // Patch the arguments.length and the parameters pointer.
10315 __ bind(&adaptor_frame);
10316 __ SmiToInteger32(rcx,
10317 Operand(rdx,
10318 ArgumentsAdaptorFrameConstants::kLengthOffset));
10319 // Space on stack must already hold a smi.
10320 __ Integer32ToSmiField(Operand(rsp, 1 * kPointerSize), rcx);
10321 // Do not clobber the length index for the indexing operation since
10322 // it is used compute the size for allocation later.
10323 __ lea(rdx, Operand(rdx, rcx, times_pointer_size, kDisplacement));
10324 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
10325
10326 // Try the new space allocation. Start out with computing the size of
10327 // the arguments object and the elements array.
10328 Label add_arguments_object;
10329 __ bind(&try_allocate);
10330 __ testl(rcx, rcx);
10331 __ j(zero, &add_arguments_object);
10332 __ leal(rcx, Operand(rcx, times_pointer_size, FixedArray::kHeaderSize));
10333 __ bind(&add_arguments_object);
10334 __ addl(rcx, Immediate(Heap::kArgumentsObjectSize));
10335
10336 // Do the allocation of both objects in one go.
10337 __ AllocateInNewSpace(rcx, rax, rdx, rbx, &runtime, TAG_OBJECT);
10338
10339 // Get the arguments boilerplate from the current (global) context.
10340 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
10341 __ movq(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
10342 __ movq(rdi, FieldOperand(rdi, GlobalObject::kGlobalContextOffset));
10343 __ movq(rdi, Operand(rdi, offset));
10344
10345 // Copy the JS object part.
10346 STATIC_ASSERT(JSObject::kHeaderSize == 3 * kPointerSize);
10347 __ movq(kScratchRegister, FieldOperand(rdi, 0 * kPointerSize));
10348 __ movq(rdx, FieldOperand(rdi, 1 * kPointerSize));
10349 __ movq(rbx, FieldOperand(rdi, 2 * kPointerSize));
10350 __ movq(FieldOperand(rax, 0 * kPointerSize), kScratchRegister);
10351 __ movq(FieldOperand(rax, 1 * kPointerSize), rdx);
10352 __ movq(FieldOperand(rax, 2 * kPointerSize), rbx);
10353
10354 // Setup the callee in-object property.
10355 ASSERT(Heap::arguments_callee_index == 0);
10356 __ movq(kScratchRegister, Operand(rsp, 3 * kPointerSize));
10357 __ movq(FieldOperand(rax, JSObject::kHeaderSize), kScratchRegister);
10358
10359 // Get the length (smi tagged) and set that as an in-object property too.
10360 ASSERT(Heap::arguments_length_index == 1);
10361 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
10362 __ movq(FieldOperand(rax, JSObject::kHeaderSize + kPointerSize), rcx);
10363
10364 // If there are no actual arguments, we're done.
10365 Label done;
10366 __ SmiTest(rcx);
10367 __ j(zero, &done);
10368
10369 // Get the parameters pointer from the stack and untag the length.
10370 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
10371
10372 // Setup the elements pointer in the allocated arguments object and
10373 // initialize the header in the elements fixed array.
10374 __ lea(rdi, Operand(rax, Heap::kArgumentsObjectSize));
10375 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rdi);
10376 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
10377 __ movq(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
10378 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
10379 __ SmiToInteger32(rcx, rcx); // Untag length for the loop below.
10380
10381 // Copy the fixed array slots.
10382 Label loop;
10383 __ bind(&loop);
10384 __ movq(kScratchRegister, Operand(rdx, -1 * kPointerSize)); // Skip receiver.
10385 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize), kScratchRegister);
10386 __ addq(rdi, Immediate(kPointerSize));
10387 __ subq(rdx, Immediate(kPointerSize));
10388 __ decl(rcx);
10389 __ j(not_zero, &loop);
10390
10391 // Return and remove the on-stack parameters.
10392 __ bind(&done);
10393 __ ret(3 * kPointerSize);
10394
10395 // Do the runtime call to allocate the arguments object.
10396 __ bind(&runtime);
10397 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
10398 }
10399
10400
10401 void RegExpExecStub::Generate(MacroAssembler* masm) {
10402 // Just jump directly to runtime if native RegExp is not selected at compile
10403 // time or if regexp entry in generated code is turned off runtime switch or
10404 // at compilation.
10405 #ifdef V8_INTERPRETED_REGEXP
10406 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
10407 #else // V8_INTERPRETED_REGEXP
10408 if (!FLAG_regexp_entry_native) {
10409 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
10410 return;
10411 }
10412
10413 // Stack frame on entry.
10414 // esp[0]: return address
10415 // esp[8]: last_match_info (expected JSArray)
10416 // esp[16]: previous index
10417 // esp[24]: subject string
10418 // esp[32]: JSRegExp object
10419
10420 static const int kLastMatchInfoOffset = 1 * kPointerSize;
10421 static const int kPreviousIndexOffset = 2 * kPointerSize;
10422 static const int kSubjectOffset = 3 * kPointerSize;
10423 static const int kJSRegExpOffset = 4 * kPointerSize;
10424
10425 Label runtime;
10426
10427 // Ensure that a RegExp stack is allocated.
10428 ExternalReference address_of_regexp_stack_memory_address =
10429 ExternalReference::address_of_regexp_stack_memory_address();
10430 ExternalReference address_of_regexp_stack_memory_size =
10431 ExternalReference::address_of_regexp_stack_memory_size();
10432 __ movq(kScratchRegister, address_of_regexp_stack_memory_size);
10433 __ movq(kScratchRegister, Operand(kScratchRegister, 0));
10434 __ testq(kScratchRegister, kScratchRegister);
10435 __ j(zero, &runtime);
10436
10437
10438 // Check that the first argument is a JSRegExp object.
10439 __ movq(rax, Operand(rsp, kJSRegExpOffset));
10440 __ JumpIfSmi(rax, &runtime);
10441 __ CmpObjectType(rax, JS_REGEXP_TYPE, kScratchRegister);
10442 __ j(not_equal, &runtime);
10443 // Check that the RegExp has been compiled (data contains a fixed array).
10444 __ movq(rcx, FieldOperand(rax, JSRegExp::kDataOffset));
10445 if (FLAG_debug_code) {
10446 Condition is_smi = masm->CheckSmi(rcx);
10447 __ Check(NegateCondition(is_smi),
10448 "Unexpected type for RegExp data, FixedArray expected");
10449 __ CmpObjectType(rcx, FIXED_ARRAY_TYPE, kScratchRegister);
10450 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
10451 }
10452
10453 // rcx: RegExp data (FixedArray)
10454 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
10455 __ SmiToInteger32(rbx, FieldOperand(rcx, JSRegExp::kDataTagOffset));
10456 __ cmpl(rbx, Immediate(JSRegExp::IRREGEXP));
10457 __ j(not_equal, &runtime);
10458
10459 // rcx: RegExp data (FixedArray)
10460 // Check that the number of captures fit in the static offsets vector buffer.
10461 __ SmiToInteger32(rdx,
10462 FieldOperand(rcx, JSRegExp::kIrregexpCaptureCountOffset));
10463 // Calculate number of capture registers (number_of_captures + 1) * 2.
10464 __ leal(rdx, Operand(rdx, rdx, times_1, 2));
10465 // Check that the static offsets vector buffer is large enough.
10466 __ cmpl(rdx, Immediate(OffsetsVector::kStaticOffsetsVectorSize));
10467 __ j(above, &runtime);
10468
10469 // rcx: RegExp data (FixedArray)
10470 // rdx: Number of capture registers
10471 // Check that the second argument is a string.
10472 __ movq(rax, Operand(rsp, kSubjectOffset));
10473 __ JumpIfSmi(rax, &runtime);
10474 Condition is_string = masm->IsObjectStringType(rax, rbx, rbx);
10475 __ j(NegateCondition(is_string), &runtime);
10476
10477 // rax: Subject string.
10478 // rcx: RegExp data (FixedArray).
10479 // rdx: Number of capture registers.
10480 // Check that the third argument is a positive smi less than the string
10481 // length. A negative value will be greater (unsigned comparison).
10482 __ movq(rbx, Operand(rsp, kPreviousIndexOffset));
10483 __ JumpIfNotSmi(rbx, &runtime);
10484 __ SmiCompare(rbx, FieldOperand(rax, String::kLengthOffset));
10485 __ j(above_equal, &runtime);
10486
10487 // rcx: RegExp data (FixedArray)
10488 // rdx: Number of capture registers
10489 // Check that the fourth object is a JSArray object.
10490 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
10491 __ JumpIfSmi(rax, &runtime);
10492 __ CmpObjectType(rax, JS_ARRAY_TYPE, kScratchRegister);
10493 __ j(not_equal, &runtime);
10494 // Check that the JSArray is in fast case.
10495 __ movq(rbx, FieldOperand(rax, JSArray::kElementsOffset));
10496 __ movq(rax, FieldOperand(rbx, HeapObject::kMapOffset));
10497 __ Cmp(rax, Factory::fixed_array_map());
10498 __ j(not_equal, &runtime);
10499 // Check that the last match info has space for the capture registers and the
10500 // additional information. Ensure no overflow in add.
10501 STATIC_ASSERT(FixedArray::kMaxLength < kMaxInt - FixedArray::kLengthOffset);
10502 __ SmiToInteger32(rax, FieldOperand(rbx, FixedArray::kLengthOffset));
10503 __ addl(rdx, Immediate(RegExpImpl::kLastMatchOverhead));
10504 __ cmpl(rdx, rax);
10505 __ j(greater, &runtime);
10506
10507 // rcx: RegExp data (FixedArray)
10508 // Check the representation and encoding of the subject string.
10509 Label seq_ascii_string, seq_two_byte_string, check_code;
10510 __ movq(rax, Operand(rsp, kSubjectOffset));
10511 __ movq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
10512 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
10513 // First check for flat two byte string.
10514 __ andb(rbx, Immediate(
10515 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask));
10516 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
10517 __ j(zero, &seq_two_byte_string);
10518 // Any other flat string must be a flat ascii string.
10519 __ testb(rbx, Immediate(kIsNotStringMask | kStringRepresentationMask));
10520 __ j(zero, &seq_ascii_string);
10521
10522 // Check for flat cons string.
10523 // A flat cons string is a cons string where the second part is the empty
10524 // string. In that case the subject string is just the first part of the cons
10525 // string. Also in this case the first part of the cons string is known to be
10526 // a sequential string or an external string.
10527 STATIC_ASSERT(kExternalStringTag !=0);
10528 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
10529 __ testb(rbx, Immediate(kIsNotStringMask | kExternalStringTag));
10530 __ j(not_zero, &runtime);
10531 // String is a cons string.
10532 __ movq(rdx, FieldOperand(rax, ConsString::kSecondOffset));
10533 __ Cmp(rdx, Factory::empty_string());
10534 __ j(not_equal, &runtime);
10535 __ movq(rax, FieldOperand(rax, ConsString::kFirstOffset));
10536 __ movq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
10537 // String is a cons string with empty second part.
10538 // rax: first part of cons string.
10539 // rbx: map of first part of cons string.
10540 // Is first part a flat two byte string?
10541 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
10542 Immediate(kStringRepresentationMask | kStringEncodingMask));
10543 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
10544 __ j(zero, &seq_two_byte_string);
10545 // Any other flat string must be ascii.
10546 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
10547 Immediate(kStringRepresentationMask));
10548 __ j(not_zero, &runtime);
10549
10550 __ bind(&seq_ascii_string);
10551 // rax: subject string (sequential ascii)
10552 // rcx: RegExp data (FixedArray)
10553 __ movq(r11, FieldOperand(rcx, JSRegExp::kDataAsciiCodeOffset));
10554 __ Set(rdi, 1); // Type is ascii.
10555 __ jmp(&check_code);
10556
10557 __ bind(&seq_two_byte_string);
10558 // rax: subject string (flat two-byte)
10559 // rcx: RegExp data (FixedArray)
10560 __ movq(r11, FieldOperand(rcx, JSRegExp::kDataUC16CodeOffset));
10561 __ Set(rdi, 0); // Type is two byte.
10562
10563 __ bind(&check_code);
10564 // Check that the irregexp code has been generated for the actual string
10565 // encoding. If it has, the field contains a code object otherwise it contains
10566 // the hole.
10567 __ CmpObjectType(r11, CODE_TYPE, kScratchRegister);
10568 __ j(not_equal, &runtime);
10569
10570 // rax: subject string
10571 // rdi: encoding of subject string (1 if ascii, 0 if two_byte);
10572 // r11: code
10573 // Load used arguments before starting to push arguments for call to native
10574 // RegExp code to avoid handling changing stack height.
10575 __ SmiToInteger64(rbx, Operand(rsp, kPreviousIndexOffset));
10576
10577 // rax: subject string
10578 // rbx: previous index
10579 // rdi: encoding of subject string (1 if ascii 0 if two_byte);
10580 // r11: code
10581 // All checks done. Now push arguments for native regexp code.
10582 __ IncrementCounter(&Counters::regexp_entry_native, 1);
10583
10584 // rsi is caller save on Windows and used to pass parameter on Linux.
10585 __ push(rsi);
10586
10587 static const int kRegExpExecuteArguments = 7;
10588 __ PrepareCallCFunction(kRegExpExecuteArguments);
10589 int argument_slots_on_stack =
10590 masm->ArgumentStackSlotsForCFunctionCall(kRegExpExecuteArguments);
10591
10592 // Argument 7: Indicate that this is a direct call from JavaScript.
10593 __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kPointerSize),
10594 Immediate(1));
10595
10596 // Argument 6: Start (high end) of backtracking stack memory area.
10597 __ movq(kScratchRegister, address_of_regexp_stack_memory_address);
10598 __ movq(r9, Operand(kScratchRegister, 0));
10599 __ movq(kScratchRegister, address_of_regexp_stack_memory_size);
10600 __ addq(r9, Operand(kScratchRegister, 0));
10601 // Argument 6 passed in r9 on Linux and on the stack on Windows.
10602 #ifdef _WIN64
10603 __ movq(Operand(rsp, (argument_slots_on_stack - 2) * kPointerSize), r9);
10604 #endif
10605
10606 // Argument 5: static offsets vector buffer.
10607 __ movq(r8, ExternalReference::address_of_static_offsets_vector());
10608 // Argument 5 passed in r8 on Linux and on the stack on Windows.
10609 #ifdef _WIN64
10610 __ movq(Operand(rsp, (argument_slots_on_stack - 3) * kPointerSize), r8);
10611 #endif
10612
10613 // First four arguments are passed in registers on both Linux and Windows.
10614 #ifdef _WIN64
10615 Register arg4 = r9;
10616 Register arg3 = r8;
10617 Register arg2 = rdx;
10618 Register arg1 = rcx;
10619 #else
10620 Register arg4 = rcx;
10621 Register arg3 = rdx;
10622 Register arg2 = rsi;
10623 Register arg1 = rdi;
10624 #endif
10625
10626 // Keep track on aliasing between argX defined above and the registers used.
10627 // rax: subject string
10628 // rbx: previous index
10629 // rdi: encoding of subject string (1 if ascii 0 if two_byte);
10630 // r11: code
10631
10632 // Argument 4: End of string data
10633 // Argument 3: Start of string data
10634 Label setup_two_byte, setup_rest;
10635 __ testb(rdi, rdi);
10636 __ j(zero, &setup_two_byte);
10637 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
10638 __ lea(arg4, FieldOperand(rax, rdi, times_1, SeqAsciiString::kHeaderSize));
10639 __ lea(arg3, FieldOperand(rax, rbx, times_1, SeqAsciiString::kHeaderSize));
10640 __ jmp(&setup_rest);
10641 __ bind(&setup_two_byte);
10642 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
10643 __ lea(arg4, FieldOperand(rax, rdi, times_2, SeqTwoByteString::kHeaderSize));
10644 __ lea(arg3, FieldOperand(rax, rbx, times_2, SeqTwoByteString::kHeaderSize));
10645
10646 __ bind(&setup_rest);
10647 // Argument 2: Previous index.
10648 __ movq(arg2, rbx);
10649
10650 // Argument 1: Subject string.
10651 __ movq(arg1, rax);
10652
10653 // Locate the code entry and call it.
10654 __ addq(r11, Immediate(Code::kHeaderSize - kHeapObjectTag));
10655 __ CallCFunction(r11, kRegExpExecuteArguments);
10656
10657 // rsi is caller save, as it is used to pass parameter.
10658 __ pop(rsi);
10659
10660 // Check the result.
10661 Label success;
10662 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::SUCCESS));
10663 __ j(equal, &success);
10664 Label failure;
10665 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::FAILURE));
10666 __ j(equal, &failure);
10667 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::EXCEPTION));
10668 // If not exception it can only be retry. Handle that in the runtime system.
10669 __ j(not_equal, &runtime);
10670 // Result must now be exception. If there is no pending exception already a
10671 // stack overflow (on the backtrack stack) was detected in RegExp code but
10672 // haven't created the exception yet. Handle that in the runtime system.
10673 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
10674 ExternalReference pending_exception_address(Top::k_pending_exception_address);
10675 __ movq(kScratchRegister, pending_exception_address);
10676 __ Cmp(kScratchRegister, Factory::the_hole_value());
10677 __ j(equal, &runtime);
10678 __ bind(&failure);
10679 // For failure and exception return null.
10680 __ Move(rax, Factory::null_value());
10681 __ ret(4 * kPointerSize);
10682
10683 // Load RegExp data.
10684 __ bind(&success);
10685 __ movq(rax, Operand(rsp, kJSRegExpOffset));
10686 __ movq(rcx, FieldOperand(rax, JSRegExp::kDataOffset));
10687 __ SmiToInteger32(rax,
10688 FieldOperand(rcx, JSRegExp::kIrregexpCaptureCountOffset));
10689 // Calculate number of capture registers (number_of_captures + 1) * 2.
10690 __ leal(rdx, Operand(rax, rax, times_1, 2));
10691
10692 // rdx: Number of capture registers
10693 // Load last_match_info which is still known to be a fast case JSArray.
10694 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
10695 __ movq(rbx, FieldOperand(rax, JSArray::kElementsOffset));
10696
10697 // rbx: last_match_info backing store (FixedArray)
10698 // rdx: number of capture registers
10699 // Store the capture count.
10700 __ Integer32ToSmi(kScratchRegister, rdx);
10701 __ movq(FieldOperand(rbx, RegExpImpl::kLastCaptureCountOffset),
10702 kScratchRegister);
10703 // Store last subject and last input.
10704 __ movq(rax, Operand(rsp, kSubjectOffset));
10705 __ movq(FieldOperand(rbx, RegExpImpl::kLastSubjectOffset), rax);
10706 __ movq(rcx, rbx);
10707 __ RecordWrite(rcx, RegExpImpl::kLastSubjectOffset, rax, rdi);
10708 __ movq(rax, Operand(rsp, kSubjectOffset));
10709 __ movq(FieldOperand(rbx, RegExpImpl::kLastInputOffset), rax);
10710 __ movq(rcx, rbx);
10711 __ RecordWrite(rcx, RegExpImpl::kLastInputOffset, rax, rdi);
10712
10713 // Get the static offsets vector filled by the native regexp code.
10714 __ movq(rcx, ExternalReference::address_of_static_offsets_vector());
10715
10716 // rbx: last_match_info backing store (FixedArray)
10717 // rcx: offsets vector
10718 // rdx: number of capture registers
10719 Label next_capture, done;
10720 // Capture register counter starts from number of capture registers and
10721 // counts down until wraping after zero.
10722 __ bind(&next_capture);
10723 __ subq(rdx, Immediate(1));
10724 __ j(negative, &done);
10725 // Read the value from the static offsets vector buffer and make it a smi.
10726 __ movl(rdi, Operand(rcx, rdx, times_int_size, 0));
10727 __ Integer32ToSmi(rdi, rdi, &runtime);
10728 // Store the smi value in the last match info.
10729 __ movq(FieldOperand(rbx,
10730 rdx,
10731 times_pointer_size,
10732 RegExpImpl::kFirstCaptureOffset),
10733 rdi);
10734 __ jmp(&next_capture);
10735 __ bind(&done);
10736
10737 // Return last match info.
10738 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
10739 __ ret(4 * kPointerSize);
10740
10741 // Do the runtime call to execute the regexp.
10742 __ bind(&runtime);
10743 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
10744 #endif // V8_INTERPRETED_REGEXP
10745 }
10746
10747
10748 void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
10749 Register object,
10750 Register result,
10751 Register scratch1,
10752 Register scratch2,
10753 bool object_is_smi,
10754 Label* not_found) {
10755 // Use of registers. Register result is used as a temporary.
10756 Register number_string_cache = result;
10757 Register mask = scratch1;
10758 Register scratch = scratch2;
10759
10760 // Load the number string cache.
10761 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
10762
10763 // Make the hash mask from the length of the number string cache. It
10764 // contains two elements (number and string) for each cache entry.
10765 __ SmiToInteger32(
10766 mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
10767 __ shrl(mask, Immediate(1));
10768 __ subq(mask, Immediate(1)); // Make mask.
10769
10770 // Calculate the entry in the number string cache. The hash value in the
10771 // number string cache for smis is just the smi value, and the hash for
10772 // doubles is the xor of the upper and lower words. See
10773 // Heap::GetNumberStringCache.
10774 Label is_smi;
10775 Label load_result_from_cache;
10776 if (!object_is_smi) {
10777 __ JumpIfSmi(object, &is_smi);
10778 __ CheckMap(object, Factory::heap_number_map(), not_found, true);
10779
10780 STATIC_ASSERT(8 == kDoubleSize);
10781 __ movl(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
10782 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset));
10783 GenerateConvertHashCodeToIndex(masm, scratch, mask);
10784
10785 Register index = scratch;
10786 Register probe = mask;
10787 __ movq(probe,
10788 FieldOperand(number_string_cache,
10789 index,
10790 times_1,
10791 FixedArray::kHeaderSize));
10792 __ JumpIfSmi(probe, not_found);
10793 ASSERT(CpuFeatures::IsSupported(SSE2));
10794 CpuFeatures::Scope fscope(SSE2);
10795 __ movsd(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
10796 __ movsd(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
10797 __ ucomisd(xmm0, xmm1);
10798 __ j(parity_even, not_found); // Bail out if NaN is involved.
10799 __ j(not_equal, not_found); // The cache did not contain this value.
10800 __ jmp(&load_result_from_cache);
10801 }
10802
10803 __ bind(&is_smi);
10804 __ SmiToInteger32(scratch, object);
10805 GenerateConvertHashCodeToIndex(masm, scratch, mask);
10806
10807 Register index = scratch;
10808 // Check if the entry is the smi we are looking for.
10809 __ cmpq(object,
10810 FieldOperand(number_string_cache,
10811 index,
10812 times_1,
10813 FixedArray::kHeaderSize));
10814 __ j(not_equal, not_found);
10815
10816 // Get the result from the cache.
10817 __ bind(&load_result_from_cache);
10818 __ movq(result,
10819 FieldOperand(number_string_cache,
10820 index,
10821 times_1,
10822 FixedArray::kHeaderSize + kPointerSize));
10823 __ IncrementCounter(&Counters::number_to_string_native, 1);
10824 }
10825
10826
10827 void NumberToStringStub::GenerateConvertHashCodeToIndex(MacroAssembler* masm,
10828 Register hash,
10829 Register mask) {
10830 __ and_(hash, mask);
10831 // Each entry in string cache consists of two pointer sized fields,
10832 // but times_twice_pointer_size (multiplication by 16) scale factor
10833 // is not supported by addrmode on x64 platform.
10834 // So we have to premultiply entry index before lookup.
10835 __ shl(hash, Immediate(kPointerSizeLog2 + 1));
10836 }
10837
10838
10839 void NumberToStringStub::Generate(MacroAssembler* masm) {
10840 Label runtime;
10841
10842 __ movq(rbx, Operand(rsp, kPointerSize));
10843
10844 // Generate code to lookup number in the number string cache.
10845 GenerateLookupNumberStringCache(masm, rbx, rax, r8, r9, false, &runtime);
10846 __ ret(1 * kPointerSize);
10847
10848 __ bind(&runtime);
10849 // Handle number to string in the runtime system if not found in the cache.
10850 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
10851 }
10852
10853
10854 static int NegativeComparisonResult(Condition cc) {
10855 ASSERT(cc != equal);
10856 ASSERT((cc == less) || (cc == less_equal)
10857 || (cc == greater) || (cc == greater_equal));
10858 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
10859 }
10860
10861
10862 void CompareStub::Generate(MacroAssembler* masm) {
10863 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
10864
10865 Label check_unequal_objects, done;
10866 // The compare stub returns a positive, negative, or zero 64-bit integer
10867 // value in rax, corresponding to result of comparing the two inputs.
10868 // NOTICE! This code is only reached after a smi-fast-case check, so
10869 // it is certain that at least one operand isn't a smi.
10870
10871 // Two identical objects are equal unless they are both NaN or undefined.
10872 {
10873 Label not_identical;
10874 __ cmpq(rax, rdx);
10875 __ j(not_equal, &not_identical);
10876
10877 if (cc_ != equal) {
10878 // Check for undefined. undefined OP undefined is false even though
10879 // undefined == undefined.
10880 Label check_for_nan;
10881 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
10882 __ j(not_equal, &check_for_nan);
10883 __ Set(rax, NegativeComparisonResult(cc_));
10884 __ ret(0);
10885 __ bind(&check_for_nan);
10886 }
10887
10888 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
10889 // so we do the second best thing - test it ourselves.
10890 // Note: if cc_ != equal, never_nan_nan_ is not used.
10891 // We cannot set rax to EQUAL until just before return because
10892 // rax must be unchanged on jump to not_identical.
10893
10894 if (never_nan_nan_ && (cc_ == equal)) {
10895 __ Set(rax, EQUAL);
10896 __ ret(0);
10897 } else {
10898 Label heap_number;
10899 // If it's not a heap number, then return equal for (in)equality operator.
10900 __ Cmp(FieldOperand(rdx, HeapObject::kMapOffset),
10901 Factory::heap_number_map());
10902 __ j(equal, &heap_number);
10903 if (cc_ != equal) {
10904 // Call runtime on identical JSObjects. Otherwise return equal.
10905 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rcx);
10906 __ j(above_equal, &not_identical);
10907 }
10908 __ Set(rax, EQUAL);
10909 __ ret(0);
10910
10911 __ bind(&heap_number);
10912 // It is a heap number, so return equal if it's not NaN.
10913 // For NaN, return 1 for every condition except greater and
10914 // greater-equal. Return -1 for them, so the comparison yields
10915 // false for all conditions except not-equal.
10916 __ Set(rax, EQUAL);
10917 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
10918 __ ucomisd(xmm0, xmm0);
10919 __ setcc(parity_even, rax);
10920 // rax is 0 for equal non-NaN heapnumbers, 1 for NaNs.
10921 if (cc_ == greater_equal || cc_ == greater) {
10922 __ neg(rax);
10923 }
10924 __ ret(0);
10925 }
10926
10927 __ bind(&not_identical);
10928 }
10929
10930 if (cc_ == equal) { // Both strict and non-strict.
10931 Label slow; // Fallthrough label.
10932
10933 // If we're doing a strict equality comparison, we don't have to do
10934 // type conversion, so we generate code to do fast comparison for objects
10935 // and oddballs. Non-smi numbers and strings still go through the usual
10936 // slow-case code.
10937 if (strict_) {
10938 // If either is a Smi (we know that not both are), then they can only
10939 // be equal if the other is a HeapNumber. If so, use the slow case.
10940 {
10941 Label not_smis;
10942 __ SelectNonSmi(rbx, rax, rdx, &not_smis);
10943
10944 // Check if the non-smi operand is a heap number.
10945 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
10946 Factory::heap_number_map());
10947 // If heap number, handle it in the slow case.
10948 __ j(equal, &slow);
10949 // Return non-equal. ebx (the lower half of rbx) is not zero.
10950 __ movq(rax, rbx);
10951 __ ret(0);
10952
10953 __ bind(&not_smis);
10954 }
10955
10956 // If either operand is a JSObject or an oddball value, then they are not
10957 // equal since their pointers are different
10958 // There is no test for undetectability in strict equality.
10959
10960 // If the first object is a JS object, we have done pointer comparison.
10961 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
10962 Label first_non_object;
10963 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rcx);
10964 __ j(below, &first_non_object);
10965 // Return non-zero (eax (not rax) is not zero)
10966 Label return_not_equal;
10967 STATIC_ASSERT(kHeapObjectTag != 0);
10968 __ bind(&return_not_equal);
10969 __ ret(0);
10970
10971 __ bind(&first_non_object);
10972 // Check for oddballs: true, false, null, undefined.
10973 __ CmpInstanceType(rcx, ODDBALL_TYPE);
10974 __ j(equal, &return_not_equal);
10975
10976 __ CmpObjectType(rdx, FIRST_JS_OBJECT_TYPE, rcx);
10977 __ j(above_equal, &return_not_equal);
10978
10979 // Check for oddballs: true, false, null, undefined.
10980 __ CmpInstanceType(rcx, ODDBALL_TYPE);
10981 __ j(equal, &return_not_equal);
10982
10983 // Fall through to the general case.
10984 }
10985 __ bind(&slow);
10986 }
10987
10988 // Generate the number comparison code.
10989 if (include_number_compare_) {
10990 Label non_number_comparison;
10991 Label unordered;
10992 FloatingPointHelper::LoadSSE2UnknownOperands(masm, &non_number_comparison);
10993 __ xorl(rax, rax);
10994 __ xorl(rcx, rcx);
10995 __ ucomisd(xmm0, xmm1);
10996
10997 // Don't base result on EFLAGS when a NaN is involved.
10998 __ j(parity_even, &unordered);
10999 // Return a result of -1, 0, or 1, based on EFLAGS.
11000 __ setcc(above, rax);
11001 __ setcc(below, rcx);
11002 __ subq(rax, rcx);
11003 __ ret(0);
11004
11005 // If one of the numbers was NaN, then the result is always false.
11006 // The cc is never not-equal.
11007 __ bind(&unordered);
11008 ASSERT(cc_ != not_equal);
11009 if (cc_ == less || cc_ == less_equal) {
11010 __ Set(rax, 1);
11011 } else {
11012 __ Set(rax, -1);
11013 }
11014 __ ret(0);
11015
11016 // The number comparison code did not provide a valid result.
11017 __ bind(&non_number_comparison);
11018 }
11019
11020 // Fast negative check for symbol-to-symbol equality.
11021 Label check_for_strings;
11022 if (cc_ == equal) {
11023 BranchIfNonSymbol(masm, &check_for_strings, rax, kScratchRegister);
11024 BranchIfNonSymbol(masm, &check_for_strings, rdx, kScratchRegister);
11025
11026 // We've already checked for object identity, so if both operands
11027 // are symbols they aren't equal. Register eax (not rax) already holds a
11028 // non-zero value, which indicates not equal, so just return.
11029 __ ret(0);
11030 }
11031
11032 __ bind(&check_for_strings);
11033
11034 __ JumpIfNotBothSequentialAsciiStrings(
11035 rdx, rax, rcx, rbx, &check_unequal_objects);
11036
11037 // Inline comparison of ascii strings.
11038 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
11039 rdx,
11040 rax,
11041 rcx,
11042 rbx,
11043 rdi,
11044 r8);
11045
11046 #ifdef DEBUG
11047 __ Abort("Unexpected fall-through from string comparison");
11048 #endif
11049
11050 __ bind(&check_unequal_objects);
11051 if (cc_ == equal && !strict_) {
11052 // Not strict equality. Objects are unequal if
11053 // they are both JSObjects and not undetectable,
11054 // and their pointers are different.
11055 Label not_both_objects, return_unequal;
11056 // At most one is a smi, so we can test for smi by adding the two.
11057 // A smi plus a heap object has the low bit set, a heap object plus
11058 // a heap object has the low bit clear.
11059 STATIC_ASSERT(kSmiTag == 0);
11060 STATIC_ASSERT(kSmiTagMask == 1);
11061 __ lea(rcx, Operand(rax, rdx, times_1, 0));
11062 __ testb(rcx, Immediate(kSmiTagMask));
11063 __ j(not_zero, &not_both_objects);
11064 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rbx);
11065 __ j(below, &not_both_objects);
11066 __ CmpObjectType(rdx, FIRST_JS_OBJECT_TYPE, rcx);
11067 __ j(below, &not_both_objects);
11068 __ testb(FieldOperand(rbx, Map::kBitFieldOffset),
11069 Immediate(1 << Map::kIsUndetectable));
11070 __ j(zero, &return_unequal);
11071 __ testb(FieldOperand(rcx, Map::kBitFieldOffset),
11072 Immediate(1 << Map::kIsUndetectable));
11073 __ j(zero, &return_unequal);
11074 // The objects are both undetectable, so they both compare as the value
11075 // undefined, and are equal.
11076 __ Set(rax, EQUAL);
11077 __ bind(&return_unequal);
11078 // Return non-equal by returning the non-zero object pointer in eax,
11079 // or return equal if we fell through to here.
11080 __ ret(0);
11081 __ bind(&not_both_objects);
11082 }
11083
11084 // Push arguments below the return address to prepare jump to builtin.
11085 __ pop(rcx);
11086 __ push(rdx);
11087 __ push(rax);
11088
11089 // Figure out which native to call and setup the arguments.
11090 Builtins::JavaScript builtin;
11091 if (cc_ == equal) {
11092 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
11093 } else {
11094 builtin = Builtins::COMPARE;
11095 __ Push(Smi::FromInt(NegativeComparisonResult(cc_)));
11096 }
11097
11098 // Restore return address on the stack.
11099 __ push(rcx);
11100
11101 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
11102 // tagged as a small integer.
11103 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
11104 }
11105
11106
11107 void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
11108 Label* label,
11109 Register object,
11110 Register scratch) {
11111 __ JumpIfSmi(object, label);
11112 __ movq(scratch, FieldOperand(object, HeapObject::kMapOffset));
11113 __ movzxbq(scratch,
11114 FieldOperand(scratch, Map::kInstanceTypeOffset));
11115 // Ensure that no non-strings have the symbol bit set.
11116 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
11117 STATIC_ASSERT(kSymbolTag != 0);
11118 __ testb(scratch, Immediate(kIsSymbolMask));
11119 __ j(zero, label);
11120 }
11121
11122
11123 void StackCheckStub::Generate(MacroAssembler* masm) {
11124 // Because builtins always remove the receiver from the stack, we
11125 // have to fake one to avoid underflowing the stack. The receiver
11126 // must be inserted below the return address on the stack so we
11127 // temporarily store that in a register.
11128 __ pop(rax);
11129 __ Push(Smi::FromInt(0));
11130 __ push(rax);
11131
11132 // Do tail-call to runtime routine.
11133 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
11134 }
11135
11136
11137 void CallFunctionStub::Generate(MacroAssembler* masm) {
11138 Label slow;
11139
11140 // If the receiver might be a value (string, number or boolean) check for this
11141 // and box it if it is.
11142 if (ReceiverMightBeValue()) {
11143 // Get the receiver from the stack.
11144 // +1 ~ return address
11145 Label receiver_is_value, receiver_is_js_object;
11146 __ movq(rax, Operand(rsp, (argc_ + 1) * kPointerSize));
11147
11148 // Check if receiver is a smi (which is a number value).
11149 __ JumpIfSmi(rax, &receiver_is_value);
11150
11151 // Check if the receiver is a valid JS object.
11152 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rdi);
11153 __ j(above_equal, &receiver_is_js_object);
11154
11155 // Call the runtime to box the value.
11156 __ bind(&receiver_is_value);
11157 __ EnterInternalFrame();
11158 __ push(rax);
11159 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
11160 __ LeaveInternalFrame();
11161 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rax);
11162
11163 __ bind(&receiver_is_js_object);
11164 }
11165
11166 // Get the function to call from the stack.
11167 // +2 ~ receiver, return address
11168 __ movq(rdi, Operand(rsp, (argc_ + 2) * kPointerSize));
11169
11170 // Check that the function really is a JavaScript function.
11171 __ JumpIfSmi(rdi, &slow);
11172 // Goto slow case if we do not have a function.
11173 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
11174 __ j(not_equal, &slow);
11175
11176 // Fast-case: Just invoke the function.
11177 ParameterCount actual(argc_);
11178 __ InvokeFunction(rdi, actual, JUMP_FUNCTION);
11179
11180 // Slow-case: Non-function called.
11181 __ bind(&slow);
11182 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
11183 // of the original receiver from the call site).
11184 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rdi);
11185 __ Set(rax, argc_);
11186 __ Set(rbx, 0);
11187 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
11188 Handle<Code> adaptor(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
11189 __ Jump(adaptor, RelocInfo::CODE_TARGET);
11190 }
11191
11192
11193 void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
11194 // Check that stack should contain next handler, frame pointer, state and
11195 // return address in that order.
11196 STATIC_ASSERT(StackHandlerConstants::kFPOffset + kPointerSize ==
11197 StackHandlerConstants::kStateOffset);
11198 STATIC_ASSERT(StackHandlerConstants::kStateOffset + kPointerSize ==
11199 StackHandlerConstants::kPCOffset);
11200
11201 ExternalReference handler_address(Top::k_handler_address);
11202 __ movq(kScratchRegister, handler_address);
11203 __ movq(rsp, Operand(kScratchRegister, 0));
11204 // get next in chain
11205 __ pop(rcx);
11206 __ movq(Operand(kScratchRegister, 0), rcx);
11207 __ pop(rbp); // pop frame pointer
11208 __ pop(rdx); // remove state
11209
11210 // Before returning we restore the context from the frame pointer if not NULL.
11211 // The frame pointer is NULL in the exception handler of a JS entry frame.
11212 __ xor_(rsi, rsi); // tentatively set context pointer to NULL
11213 Label skip;
11214 __ cmpq(rbp, Immediate(0));
11215 __ j(equal, &skip);
11216 __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
11217 __ bind(&skip);
11218 __ ret(0);
11219 }
11220
11221
11222 void ApiGetterEntryStub::Generate(MacroAssembler* masm) {
11223 Label empty_result;
11224 Label prologue;
11225 Label promote_scheduled_exception;
11226 __ EnterApiExitFrame(ExitFrame::MODE_NORMAL, kStackSpace, 0);
11227 ASSERT_EQ(kArgc, 4);
11228 #ifdef _WIN64
11229 // All the parameters should be set up by a caller.
11230 #else
11231 // Set 1st parameter register with property name.
11232 __ movq(rsi, rdx);
11233 // Second parameter register rdi should be set with pointer to AccessorInfo
11234 // by a caller.
11235 #endif
11236 // Call the api function!
11237 __ movq(rax,
11238 reinterpret_cast<int64_t>(fun()->address()),
11239 RelocInfo::RUNTIME_ENTRY);
11240 __ call(rax);
11241 // Check if the function scheduled an exception.
11242 ExternalReference scheduled_exception_address =
11243 ExternalReference::scheduled_exception_address();
11244 __ movq(rsi, scheduled_exception_address);
11245 __ Cmp(Operand(rsi, 0), Factory::the_hole_value());
11246 __ j(not_equal, &promote_scheduled_exception);
11247 #ifdef _WIN64
11248 // rax keeps a pointer to v8::Handle, unpack it.
11249 __ movq(rax, Operand(rax, 0));
11250 #endif
11251 // Check if the result handle holds 0.
11252 __ testq(rax, rax);
11253 __ j(zero, &empty_result);
11254 // It was non-zero. Dereference to get the result value.
11255 __ movq(rax, Operand(rax, 0));
11256 __ bind(&prologue);
11257 __ LeaveExitFrame(ExitFrame::MODE_NORMAL);
11258 __ ret(0);
11259 __ bind(&promote_scheduled_exception);
11260 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
11261 __ bind(&empty_result);
11262 // It was zero; the result is undefined.
11263 __ Move(rax, Factory::undefined_value());
11264 __ jmp(&prologue);
11265 }
11266
11267
11268 void CEntryStub::GenerateCore(MacroAssembler* masm,
11269 Label* throw_normal_exception,
11270 Label* throw_termination_exception,
11271 Label* throw_out_of_memory_exception,
11272 bool do_gc,
11273 bool always_allocate_scope,
11274 int /* alignment_skew */) {
11275 // rax: result parameter for PerformGC, if any.
11276 // rbx: pointer to C function (C callee-saved).
11277 // rbp: frame pointer (restored after C call).
11278 // rsp: stack pointer (restored after C call).
11279 // r14: number of arguments including receiver (C callee-saved).
11280 // r12: pointer to the first argument (C callee-saved).
11281 // This pointer is reused in LeaveExitFrame(), so it is stored in a
11282 // callee-saved register.
11283
11284 // Simple results returned in rax (both AMD64 and Win64 calling conventions).
11285 // Complex results must be written to address passed as first argument.
11286 // AMD64 calling convention: a struct of two pointers in rax+rdx
11287
11288 // Check stack alignment.
11289 if (FLAG_debug_code) {
11290 __ CheckStackAlignment();
11291 }
11292
11293 if (do_gc) {
11294 // Pass failure code returned from last attempt as first argument to
11295 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
11296 // stack is known to be aligned. This function takes one argument which is
11297 // passed in register.
11298 #ifdef _WIN64
11299 __ movq(rcx, rax);
11300 #else // _WIN64
11301 __ movq(rdi, rax);
11302 #endif
11303 __ movq(kScratchRegister,
11304 FUNCTION_ADDR(Runtime::PerformGC),
11305 RelocInfo::RUNTIME_ENTRY);
11306 __ call(kScratchRegister);
11307 }
11308
11309 ExternalReference scope_depth =
11310 ExternalReference::heap_always_allocate_scope_depth();
11311 if (always_allocate_scope) {
11312 __ movq(kScratchRegister, scope_depth);
11313 __ incl(Operand(kScratchRegister, 0));
11314 }
11315
11316 // Call C function.
11317 #ifdef _WIN64
11318 // Windows 64-bit ABI passes arguments in rcx, rdx, r8, r9
11319 // Store Arguments object on stack, below the 4 WIN64 ABI parameter slots.
11320 __ movq(Operand(rsp, 4 * kPointerSize), r14); // argc.
11321 __ movq(Operand(rsp, 5 * kPointerSize), r12); // argv.
11322 if (result_size_ < 2) {
11323 // Pass a pointer to the Arguments object as the first argument.
11324 // Return result in single register (rax).
11325 __ lea(rcx, Operand(rsp, 4 * kPointerSize));
11326 } else {
11327 ASSERT_EQ(2, result_size_);
11328 // Pass a pointer to the result location as the first argument.
11329 __ lea(rcx, Operand(rsp, 6 * kPointerSize));
11330 // Pass a pointer to the Arguments object as the second argument.
11331 __ lea(rdx, Operand(rsp, 4 * kPointerSize));
11332 }
11333
11334 #else // _WIN64
11335 // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9.
11336 __ movq(rdi, r14); // argc.
11337 __ movq(rsi, r12); // argv.
11338 #endif
11339 __ call(rbx);
11340 // Result is in rax - do not destroy this register!
11341
11342 if (always_allocate_scope) {
11343 __ movq(kScratchRegister, scope_depth);
11344 __ decl(Operand(kScratchRegister, 0));
11345 }
11346
11347 // Check for failure result.
11348 Label failure_returned;
11349 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
11350 #ifdef _WIN64
11351 // If return value is on the stack, pop it to registers.
11352 if (result_size_ > 1) {
11353 ASSERT_EQ(2, result_size_);
11354 // Read result values stored on stack. Result is stored
11355 // above the four argument mirror slots and the two
11356 // Arguments object slots.
11357 __ movq(rax, Operand(rsp, 6 * kPointerSize));
11358 __ movq(rdx, Operand(rsp, 7 * kPointerSize));
11359 }
11360 #endif
11361 __ lea(rcx, Operand(rax, 1));
11362 // Lower 2 bits of rcx are 0 iff rax has failure tag.
11363 __ testl(rcx, Immediate(kFailureTagMask));
11364 __ j(zero, &failure_returned);
11365
11366 // Exit the JavaScript to C++ exit frame.
11367 __ LeaveExitFrame(mode_, result_size_);
11368 __ ret(0);
11369
11370 // Handling of failure.
11371 __ bind(&failure_returned);
11372
11373 Label retry;
11374 // If the returned exception is RETRY_AFTER_GC continue at retry label
11375 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
11376 __ testl(rax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
11377 __ j(zero, &retry);
11378
11379 // Special handling of out of memory exceptions.
11380 __ movq(kScratchRegister, Failure::OutOfMemoryException(), RelocInfo::NONE);
11381 __ cmpq(rax, kScratchRegister);
11382 __ j(equal, throw_out_of_memory_exception);
11383
11384 // Retrieve the pending exception and clear the variable.
11385 ExternalReference pending_exception_address(Top::k_pending_exception_address);
11386 __ movq(kScratchRegister, pending_exception_address);
11387 __ movq(rax, Operand(kScratchRegister, 0));
11388 __ movq(rdx, ExternalReference::the_hole_value_location());
11389 __ movq(rdx, Operand(rdx, 0));
11390 __ movq(Operand(kScratchRegister, 0), rdx);
11391
11392 // Special handling of termination exceptions which are uncatchable
11393 // by javascript code.
11394 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
11395 __ j(equal, throw_termination_exception);
11396
11397 // Handle normal exception.
11398 __ jmp(throw_normal_exception);
11399
11400 // Retry.
11401 __ bind(&retry);
11402 }
11403
11404
11405 void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
11406 UncatchableExceptionType type) {
11407 // Fetch top stack handler.
11408 ExternalReference handler_address(Top::k_handler_address);
11409 __ movq(kScratchRegister, handler_address);
11410 __ movq(rsp, Operand(kScratchRegister, 0));
11411
11412 // Unwind the handlers until the ENTRY handler is found.
11413 Label loop, done;
11414 __ bind(&loop);
11415 // Load the type of the current stack handler.
11416 const int kStateOffset = StackHandlerConstants::kStateOffset;
11417 __ cmpq(Operand(rsp, kStateOffset), Immediate(StackHandler::ENTRY));
11418 __ j(equal, &done);
11419 // Fetch the next handler in the list.
11420 const int kNextOffset = StackHandlerConstants::kNextOffset;
11421 __ movq(rsp, Operand(rsp, kNextOffset));
11422 __ jmp(&loop);
11423 __ bind(&done);
11424
11425 // Set the top handler address to next handler past the current ENTRY handler.
11426 __ movq(kScratchRegister, handler_address);
11427 __ pop(Operand(kScratchRegister, 0));
11428
11429 if (type == OUT_OF_MEMORY) {
11430 // Set external caught exception to false.
11431 ExternalReference external_caught(Top::k_external_caught_exception_address);
11432 __ movq(rax, Immediate(false));
11433 __ store_rax(external_caught);
11434
11435 // Set pending exception and rax to out of memory exception.
11436 ExternalReference pending_exception(Top::k_pending_exception_address);
11437 __ movq(rax, Failure::OutOfMemoryException(), RelocInfo::NONE);
11438 __ store_rax(pending_exception);
11439 }
11440
11441 // Clear the context pointer.
11442 __ xor_(rsi, rsi);
11443
11444 // Restore registers from handler.
11445 STATIC_ASSERT(StackHandlerConstants::kNextOffset + kPointerSize ==
11446 StackHandlerConstants::kFPOffset);
11447 __ pop(rbp); // FP
11448 STATIC_ASSERT(StackHandlerConstants::kFPOffset + kPointerSize ==
11449 StackHandlerConstants::kStateOffset);
11450 __ pop(rdx); // State
11451
11452 STATIC_ASSERT(StackHandlerConstants::kStateOffset + kPointerSize ==
11453 StackHandlerConstants::kPCOffset);
11454 __ ret(0);
11455 }
11456
11457
11458 void CEntryStub::Generate(MacroAssembler* masm) {
11459 // rax: number of arguments including receiver
11460 // rbx: pointer to C function (C callee-saved)
11461 // rbp: frame pointer of calling JS frame (restored after C call)
11462 // rsp: stack pointer (restored after C call)
11463 // rsi: current context (restored)
11464
11465 // NOTE: Invocations of builtins may return failure objects
11466 // instead of a proper result. The builtin entry handles
11467 // this by performing a garbage collection and retrying the
11468 // builtin once.
11469
11470 // Enter the exit frame that transitions from JavaScript to C++.
11471 __ EnterExitFrame(mode_, result_size_);
11472
11473 // rax: Holds the context at this point, but should not be used.
11474 // On entry to code generated by GenerateCore, it must hold
11475 // a failure result if the collect_garbage argument to GenerateCore
11476 // is true. This failure result can be the result of code
11477 // generated by a previous call to GenerateCore. The value
11478 // of rax is then passed to Runtime::PerformGC.
11479 // rbx: pointer to builtin function (C callee-saved).
11480 // rbp: frame pointer of exit frame (restored after C call).
11481 // rsp: stack pointer (restored after C call).
11482 // r14: number of arguments including receiver (C callee-saved).
11483 // r12: argv pointer (C callee-saved).
11484
11485 Label throw_normal_exception;
11486 Label throw_termination_exception;
11487 Label throw_out_of_memory_exception;
11488
11489 // Call into the runtime system.
11490 GenerateCore(masm,
11491 &throw_normal_exception,
11492 &throw_termination_exception,
11493 &throw_out_of_memory_exception,
11494 false,
11495 false);
11496
11497 // Do space-specific GC and retry runtime call.
11498 GenerateCore(masm,
11499 &throw_normal_exception,
11500 &throw_termination_exception,
11501 &throw_out_of_memory_exception,
11502 true,
11503 false);
11504
11505 // Do full GC and retry runtime call one final time.
11506 Failure* failure = Failure::InternalError();
11507 __ movq(rax, failure, RelocInfo::NONE);
11508 GenerateCore(masm,
11509 &throw_normal_exception,
11510 &throw_termination_exception,
11511 &throw_out_of_memory_exception,
11512 true,
11513 true);
11514
11515 __ bind(&throw_out_of_memory_exception);
11516 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
11517
11518 __ bind(&throw_termination_exception);
11519 GenerateThrowUncatchable(masm, TERMINATION);
11520
11521 __ bind(&throw_normal_exception);
11522 GenerateThrowTOS(masm);
11523 }
11524
11525
11526 void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
11527 Label invoke, exit;
11528 #ifdef ENABLE_LOGGING_AND_PROFILING
11529 Label not_outermost_js, not_outermost_js_2;
11530 #endif
11531
11532 // Setup frame.
11533 __ push(rbp);
11534 __ movq(rbp, rsp);
11535
11536 // Push the stack frame type marker twice.
11537 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
11538 // Scratch register is neither callee-save, nor an argument register on any
11539 // platform. It's free to use at this point.
11540 // Cannot use smi-register for loading yet.
11541 __ movq(kScratchRegister,
11542 reinterpret_cast<uint64_t>(Smi::FromInt(marker)),
11543 RelocInfo::NONE);
11544 __ push(kScratchRegister); // context slot
11545 __ push(kScratchRegister); // function slot
11546 // Save callee-saved registers (X64/Win64 calling conventions).
11547 __ push(r12);
11548 __ push(r13);
11549 __ push(r14);
11550 __ push(r15);
11551 #ifdef _WIN64
11552 __ push(rdi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
11553 __ push(rsi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
11554 #endif
11555 __ push(rbx);
11556 // TODO(X64): On Win64, if we ever use XMM6-XMM15, the low low 64 bits are
11557 // callee save as well.
11558
11559 // Save copies of the top frame descriptor on the stack.
11560 ExternalReference c_entry_fp(Top::k_c_entry_fp_address);
11561 __ load_rax(c_entry_fp);
11562 __ push(rax);
11563
11564 // Set up the roots and smi constant registers.
11565 // Needs to be done before any further smi loads.
11566 ExternalReference roots_address = ExternalReference::roots_address();
11567 __ movq(kRootRegister, roots_address);
11568 __ InitializeSmiConstantRegister();
11569
11570 #ifdef ENABLE_LOGGING_AND_PROFILING
11571 // If this is the outermost JS call, set js_entry_sp value.
11572 ExternalReference js_entry_sp(Top::k_js_entry_sp_address);
11573 __ load_rax(js_entry_sp);
11574 __ testq(rax, rax);
11575 __ j(not_zero, &not_outermost_js);
11576 __ movq(rax, rbp);
11577 __ store_rax(js_entry_sp);
11578 __ bind(&not_outermost_js);
11579 #endif
11580
11581 // Call a faked try-block that does the invoke.
11582 __ call(&invoke);
11583
11584 // Caught exception: Store result (exception) in the pending
11585 // exception field in the JSEnv and return a failure sentinel.
11586 ExternalReference pending_exception(Top::k_pending_exception_address);
11587 __ store_rax(pending_exception);
11588 __ movq(rax, Failure::Exception(), RelocInfo::NONE);
11589 __ jmp(&exit);
11590
11591 // Invoke: Link this frame into the handler chain.
11592 __ bind(&invoke);
11593 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
11594
11595 // Clear any pending exceptions.
11596 __ load_rax(ExternalReference::the_hole_value_location());
11597 __ store_rax(pending_exception);
11598
11599 // Fake a receiver (NULL).
11600 __ push(Immediate(0)); // receiver
11601
11602 // Invoke the function by calling through JS entry trampoline
11603 // builtin and pop the faked function when we return. We load the address
11604 // from an external reference instead of inlining the call target address
11605 // directly in the code, because the builtin stubs may not have been
11606 // generated yet at the time this code is generated.
11607 if (is_construct) {
11608 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
11609 __ load_rax(construct_entry);
11610 } else {
11611 ExternalReference entry(Builtins::JSEntryTrampoline);
11612 __ load_rax(entry);
11613 }
11614 __ lea(kScratchRegister, FieldOperand(rax, Code::kHeaderSize));
11615 __ call(kScratchRegister);
11616
11617 // Unlink this frame from the handler chain.
11618 __ movq(kScratchRegister, ExternalReference(Top::k_handler_address));
11619 __ pop(Operand(kScratchRegister, 0));
11620 // Pop next_sp.
11621 __ addq(rsp, Immediate(StackHandlerConstants::kSize - kPointerSize));
11622
11623 #ifdef ENABLE_LOGGING_AND_PROFILING
11624 // If current EBP value is the same as js_entry_sp value, it means that
11625 // the current function is the outermost.
11626 __ movq(kScratchRegister, js_entry_sp);
11627 __ cmpq(rbp, Operand(kScratchRegister, 0));
11628 __ j(not_equal, &not_outermost_js_2);
11629 __ movq(Operand(kScratchRegister, 0), Immediate(0));
11630 __ bind(&not_outermost_js_2);
11631 #endif
11632
11633 // Restore the top frame descriptor from the stack.
11634 __ bind(&exit);
11635 __ movq(kScratchRegister, ExternalReference(Top::k_c_entry_fp_address));
11636 __ pop(Operand(kScratchRegister, 0));
11637
11638 // Restore callee-saved registers (X64 conventions).
11639 __ pop(rbx);
11640 #ifdef _WIN64
11641 // Callee save on in Win64 ABI, arguments/volatile in AMD64 ABI.
11642 __ pop(rsi);
11643 __ pop(rdi);
11644 #endif
11645 __ pop(r15);
11646 __ pop(r14);
11647 __ pop(r13);
11648 __ pop(r12);
11649 __ addq(rsp, Immediate(2 * kPointerSize)); // remove markers
11650
11651 // Restore frame pointer and return.
11652 __ pop(rbp);
11653 __ ret(0);
11654 }
11655
11656
11657 void InstanceofStub::Generate(MacroAssembler* masm) {
11658 // Implements "value instanceof function" operator.
11659 // Expected input state:
11660 // rsp[0] : return address
11661 // rsp[1] : function pointer
11662 // rsp[2] : value
11663 // Returns a bitwise zero to indicate that the value
11664 // is and instance of the function and anything else to
11665 // indicate that the value is not an instance.
11666
11667 // Get the object - go slow case if it's a smi.
11668 Label slow;
11669 __ movq(rax, Operand(rsp, 2 * kPointerSize));
11670 __ JumpIfSmi(rax, &slow);
11671
11672 // Check that the left hand is a JS object. Leave its map in rax.
11673 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rax);
11674 __ j(below, &slow);
11675 __ CmpInstanceType(rax, LAST_JS_OBJECT_TYPE);
11676 __ j(above, &slow);
11677
11678 // Get the prototype of the function.
11679 __ movq(rdx, Operand(rsp, 1 * kPointerSize));
11680 // rdx is function, rax is map.
11681
11682 // Look up the function and the map in the instanceof cache.
11683 Label miss;
11684 __ CompareRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
11685 __ j(not_equal, &miss);
11686 __ CompareRoot(rax, Heap::kInstanceofCacheMapRootIndex);
11687 __ j(not_equal, &miss);
11688 __ LoadRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
11689 __ ret(2 * kPointerSize);
11690
11691 __ bind(&miss);
11692 __ TryGetFunctionPrototype(rdx, rbx, &slow);
11693
11694 // Check that the function prototype is a JS object.
11695 __ JumpIfSmi(rbx, &slow);
11696 __ CmpObjectType(rbx, FIRST_JS_OBJECT_TYPE, kScratchRegister);
11697 __ j(below, &slow);
11698 __ CmpInstanceType(kScratchRegister, LAST_JS_OBJECT_TYPE);
11699 __ j(above, &slow);
11700
11701 // Register mapping:
11702 // rax is object map.
11703 // rdx is function.
11704 // rbx is function prototype.
11705 __ StoreRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
11706 __ StoreRoot(rax, Heap::kInstanceofCacheMapRootIndex);
11707
11708 __ movq(rcx, FieldOperand(rax, Map::kPrototypeOffset));
11709
11710 // Loop through the prototype chain looking for the function prototype.
11711 Label loop, is_instance, is_not_instance;
11712 __ LoadRoot(kScratchRegister, Heap::kNullValueRootIndex);
11713 __ bind(&loop);
11714 __ cmpq(rcx, rbx);
11715 __ j(equal, &is_instance);
11716 __ cmpq(rcx, kScratchRegister);
11717 // The code at is_not_instance assumes that kScratchRegister contains a
11718 // non-zero GCable value (the null object in this case).
11719 __ j(equal, &is_not_instance);
11720 __ movq(rcx, FieldOperand(rcx, HeapObject::kMapOffset));
11721 __ movq(rcx, FieldOperand(rcx, Map::kPrototypeOffset));
11722 __ jmp(&loop);
11723
11724 __ bind(&is_instance);
11725 __ xorl(rax, rax);
11726 // Store bitwise zero in the cache. This is a Smi in GC terms.
11727 STATIC_ASSERT(kSmiTag == 0);
11728 __ StoreRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
11729 __ ret(2 * kPointerSize);
11730
11731 __ bind(&is_not_instance);
11732 // We have to store a non-zero value in the cache.
11733 __ StoreRoot(kScratchRegister, Heap::kInstanceofCacheAnswerRootIndex);
11734 __ ret(2 * kPointerSize);
11735
11736 // Slow-case: Go through the JavaScript implementation.
11737 __ bind(&slow);
11738 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
11739 }
11740
11741
11742 int CompareStub::MinorKey() {
11743 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
11744 // stubs the never NaN NaN condition is only taken into account if the
11745 // condition is equals.
11746 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
11747 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
11748 return ConditionField::encode(static_cast<unsigned>(cc_))
11749 | RegisterField::encode(false) // lhs_ and rhs_ are not used
11750 | StrictField::encode(strict_)
11751 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
11752 | IncludeNumberCompareField::encode(include_number_compare_);
11753 }
11754
11755
11756 // Unfortunately you have to run without snapshots to see most of these
11757 // names in the profile since most compare stubs end up in the snapshot.
11758 const char* CompareStub::GetName() {
11759 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
11760
11761 if (name_ != NULL) return name_;
11762 const int kMaxNameLength = 100;
11763 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
11764 if (name_ == NULL) return "OOM";
11765
11766 const char* cc_name;
11767 switch (cc_) {
11768 case less: cc_name = "LT"; break;
11769 case greater: cc_name = "GT"; break;
11770 case less_equal: cc_name = "LE"; break;
11771 case greater_equal: cc_name = "GE"; break;
11772 case equal: cc_name = "EQ"; break;
11773 case not_equal: cc_name = "NE"; break;
11774 default: cc_name = "UnknownCondition"; break;
11775 }
11776
11777 const char* strict_name = "";
11778 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
11779 strict_name = "_STRICT";
11780 }
11781
11782 const char* never_nan_nan_name = "";
11783 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
11784 never_nan_nan_name = "_NO_NAN";
11785 }
11786
11787 const char* include_number_compare_name = "";
11788 if (!include_number_compare_) {
11789 include_number_compare_name = "_NO_NUMBER";
11790 }
11791
11792 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
11793 "CompareStub_%s%s%s%s",
11794 cc_name,
11795 strict_name,
11796 never_nan_nan_name,
11797 include_number_compare_name);
11798 return name_;
11799 }
11800
11801
11802 // -------------------------------------------------------------------------
11803 // StringCharCodeAtGenerator
11804
11805 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
11806 Label flat_string;
11807 Label ascii_string;
11808 Label got_char_code;
11809
11810 // If the receiver is a smi trigger the non-string case.
11811 __ JumpIfSmi(object_, receiver_not_string_);
11812
11813 // Fetch the instance type of the receiver into result register.
11814 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
11815 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
11816 // If the receiver is not a string trigger the non-string case.
11817 __ testb(result_, Immediate(kIsNotStringMask));
11818 __ j(not_zero, receiver_not_string_);
11819
11820 // If the index is non-smi trigger the non-smi case.
11821 __ JumpIfNotSmi(index_, &index_not_smi_);
11822
11823 // Put smi-tagged index into scratch register.
11824 __ movq(scratch_, index_);
11825 __ bind(&got_smi_index_);
11826
11827 // Check for index out of range.
11828 __ SmiCompare(scratch_, FieldOperand(object_, String::kLengthOffset));
11829 __ j(above_equal, index_out_of_range_);
11830
11831 // We need special handling for non-flat strings.
11832 STATIC_ASSERT(kSeqStringTag == 0);
11833 __ testb(result_, Immediate(kStringRepresentationMask));
11834 __ j(zero, &flat_string);
11835
11836 // Handle non-flat strings.
11837 __ testb(result_, Immediate(kIsConsStringMask));
11838 __ j(zero, &call_runtime_);
11839
11840 // ConsString.
11841 // Check whether the right hand side is the empty string (i.e. if
11842 // this is really a flat string in a cons string). If that is not
11843 // the case we would rather go to the runtime system now to flatten
11844 // the string.
11845 __ CompareRoot(FieldOperand(object_, ConsString::kSecondOffset),
11846 Heap::kEmptyStringRootIndex);
11847 __ j(not_equal, &call_runtime_);
11848 // Get the first of the two strings and load its instance type.
11849 __ movq(object_, FieldOperand(object_, ConsString::kFirstOffset));
11850 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
11851 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
11852 // If the first cons component is also non-flat, then go to runtime.
11853 STATIC_ASSERT(kSeqStringTag == 0);
11854 __ testb(result_, Immediate(kStringRepresentationMask));
11855 __ j(not_zero, &call_runtime_);
11856
11857 // Check for 1-byte or 2-byte string.
11858 __ bind(&flat_string);
11859 STATIC_ASSERT(kAsciiStringTag != 0);
11860 __ testb(result_, Immediate(kStringEncodingMask));
11861 __ j(not_zero, &ascii_string);
11862
11863 // 2-byte string.
11864 // Load the 2-byte character code into the result register.
11865 __ SmiToInteger32(scratch_, scratch_);
11866 __ movzxwl(result_, FieldOperand(object_,
11867 scratch_, times_2,
11868 SeqTwoByteString::kHeaderSize));
11869 __ jmp(&got_char_code);
11870
11871 // ASCII string.
11872 // Load the byte into the result register.
11873 __ bind(&ascii_string);
11874 __ SmiToInteger32(scratch_, scratch_);
11875 __ movzxbl(result_, FieldOperand(object_,
11876 scratch_, times_1,
11877 SeqAsciiString::kHeaderSize));
11878 __ bind(&got_char_code);
11879 __ Integer32ToSmi(result_, result_);
11880 __ bind(&exit_);
11881 }
11882
11883
11884 void StringCharCodeAtGenerator::GenerateSlow(
11885 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
11886 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
11887
11888 // Index is not a smi.
11889 __ bind(&index_not_smi_);
11890 // If index is a heap number, try converting it to an integer.
11891 __ CheckMap(index_, Factory::heap_number_map(), index_not_number_, true);
11892 call_helper.BeforeCall(masm);
11893 __ push(object_);
11894 __ push(index_);
11895 __ push(index_); // Consumed by runtime conversion function.
11896 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
11897 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
11898 } else {
11899 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
11900 // NumberToSmi discards numbers that are not exact integers.
11901 __ CallRuntime(Runtime::kNumberToSmi, 1);
11902 }
11903 if (!scratch_.is(rax)) {
11904 // Save the conversion result before the pop instructions below
11905 // have a chance to overwrite it.
11906 __ movq(scratch_, rax);
11907 }
11908 __ pop(index_);
11909 __ pop(object_);
11910 // Reload the instance type.
11911 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
11912 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
11913 call_helper.AfterCall(masm);
11914 // If index is still not a smi, it must be out of range.
11915 __ JumpIfNotSmi(scratch_, index_out_of_range_);
11916 // Otherwise, return to the fast path.
11917 __ jmp(&got_smi_index_);
11918
11919 // Call runtime. We get here when the receiver is a string and the
11920 // index is a number, but the code of getting the actual character
11921 // is too complex (e.g., when the string needs to be flattened).
11922 __ bind(&call_runtime_);
11923 call_helper.BeforeCall(masm);
11924 __ push(object_);
11925 __ push(index_);
11926 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
11927 if (!result_.is(rax)) {
11928 __ movq(result_, rax);
11929 }
11930 call_helper.AfterCall(masm);
11931 __ jmp(&exit_);
11932
11933 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
11934 }
11935
11936
11937 // -------------------------------------------------------------------------
11938 // StringCharFromCodeGenerator
11939
11940 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
11941 // Fast case of Heap::LookupSingleCharacterStringFromCode.
11942 __ JumpIfNotSmi(code_, &slow_case_);
11943 __ SmiCompare(code_, Smi::FromInt(String::kMaxAsciiCharCode));
11944 __ j(above, &slow_case_);
11945
11946 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
11947 SmiIndex index = masm->SmiToIndex(kScratchRegister, code_, kPointerSizeLog2);
11948 __ movq(result_, FieldOperand(result_, index.reg, index.scale,
11949 FixedArray::kHeaderSize));
11950 __ CompareRoot(result_, Heap::kUndefinedValueRootIndex);
11951 __ j(equal, &slow_case_);
11952 __ bind(&exit_);
11953 }
11954
11955
11956 void StringCharFromCodeGenerator::GenerateSlow(
11957 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
11958 __ Abort("Unexpected fallthrough to CharFromCode slow case");
11959
11960 __ bind(&slow_case_);
11961 call_helper.BeforeCall(masm);
11962 __ push(code_);
11963 __ CallRuntime(Runtime::kCharFromCode, 1);
11964 if (!result_.is(rax)) {
11965 __ movq(result_, rax);
11966 }
11967 call_helper.AfterCall(masm);
11968 __ jmp(&exit_);
11969
11970 __ Abort("Unexpected fallthrough from CharFromCode slow case");
11971 }
11972
11973
11974 // -------------------------------------------------------------------------
11975 // StringCharAtGenerator
11976
11977 void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
11978 char_code_at_generator_.GenerateFast(masm);
11979 char_from_code_generator_.GenerateFast(masm);
11980 }
11981
11982
11983 void StringCharAtGenerator::GenerateSlow(
11984 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
11985 char_code_at_generator_.GenerateSlow(masm, call_helper);
11986 char_from_code_generator_.GenerateSlow(masm, call_helper);
11987 }
11988
11989
11990 void StringAddStub::Generate(MacroAssembler* masm) {
11991 Label string_add_runtime;
11992
11993 // Load the two arguments.
11994 __ movq(rax, Operand(rsp, 2 * kPointerSize)); // First argument.
11995 __ movq(rdx, Operand(rsp, 1 * kPointerSize)); // Second argument.
11996
11997 // Make sure that both arguments are strings if not known in advance.
11998 if (string_check_) {
11999 Condition is_smi;
12000 is_smi = masm->CheckSmi(rax);
12001 __ j(is_smi, &string_add_runtime);
12002 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, r8);
12003 __ j(above_equal, &string_add_runtime);
12004
12005 // First argument is a a string, test second.
12006 is_smi = masm->CheckSmi(rdx);
12007 __ j(is_smi, &string_add_runtime);
12008 __ CmpObjectType(rdx, FIRST_NONSTRING_TYPE, r9);
12009 __ j(above_equal, &string_add_runtime);
12010 }
12011
12012 // Both arguments are strings.
12013 // rax: first string
12014 // rdx: second string
12015 // Check if either of the strings are empty. In that case return the other.
12016 Label second_not_zero_length, both_not_zero_length;
12017 __ movq(rcx, FieldOperand(rdx, String::kLengthOffset));
12018 __ SmiTest(rcx);
12019 __ j(not_zero, &second_not_zero_length);
12020 // Second string is empty, result is first string which is already in rax.
12021 __ IncrementCounter(&Counters::string_add_native, 1);
12022 __ ret(2 * kPointerSize);
12023 __ bind(&second_not_zero_length);
12024 __ movq(rbx, FieldOperand(rax, String::kLengthOffset));
12025 __ SmiTest(rbx);
12026 __ j(not_zero, &both_not_zero_length);
12027 // First string is empty, result is second string which is in rdx.
12028 __ movq(rax, rdx);
12029 __ IncrementCounter(&Counters::string_add_native, 1);
12030 __ ret(2 * kPointerSize);
12031
12032 // Both strings are non-empty.
12033 // rax: first string
12034 // rbx: length of first string
12035 // rcx: length of second string
12036 // rdx: second string
12037 // r8: map of first string if string check was performed above
12038 // r9: map of second string if string check was performed above
12039 Label string_add_flat_result, longer_than_two;
12040 __ bind(&both_not_zero_length);
12041
12042 // If arguments where known to be strings, maps are not loaded to r8 and r9
12043 // by the code above.
12044 if (!string_check_) {
12045 __ movq(r8, FieldOperand(rax, HeapObject::kMapOffset));
12046 __ movq(r9, FieldOperand(rdx, HeapObject::kMapOffset));
12047 }
12048 // Get the instance types of the two strings as they will be needed soon.
12049 __ movzxbl(r8, FieldOperand(r8, Map::kInstanceTypeOffset));
12050 __ movzxbl(r9, FieldOperand(r9, Map::kInstanceTypeOffset));
12051
12052 // Look at the length of the result of adding the two strings.
12053 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue / 2);
12054 __ SmiAdd(rbx, rbx, rcx, NULL);
12055 // Use the runtime system when adding two one character strings, as it
12056 // contains optimizations for this specific case using the symbol table.
12057 __ SmiCompare(rbx, Smi::FromInt(2));
12058 __ j(not_equal, &longer_than_two);
12059
12060 // Check that both strings are non-external ascii strings.
12061 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r8, r9, rbx, rcx,
12062 &string_add_runtime);
12063
12064 // Get the two characters forming the sub string.
12065 __ movzxbq(rbx, FieldOperand(rax, SeqAsciiString::kHeaderSize));
12066 __ movzxbq(rcx, FieldOperand(rdx, SeqAsciiString::kHeaderSize));
12067
12068 // Try to lookup two character string in symbol table. If it is not found
12069 // just allocate a new one.
12070 Label make_two_character_string, make_flat_ascii_string;
12071 StringHelper::GenerateTwoCharacterSymbolTableProbe(
12072 masm, rbx, rcx, r14, r11, rdi, r12, &make_two_character_string);
12073 __ IncrementCounter(&Counters::string_add_native, 1);
12074 __ ret(2 * kPointerSize);
12075
12076 __ bind(&make_two_character_string);
12077 __ Set(rbx, 2);
12078 __ jmp(&make_flat_ascii_string);
12079
12080 __ bind(&longer_than_two);
12081 // Check if resulting string will be flat.
12082 __ SmiCompare(rbx, Smi::FromInt(String::kMinNonFlatLength));
12083 __ j(below, &string_add_flat_result);
12084 // Handle exceptionally long strings in the runtime system.
12085 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
12086 __ SmiCompare(rbx, Smi::FromInt(String::kMaxLength));
12087 __ j(above, &string_add_runtime);
12088
12089 // If result is not supposed to be flat, allocate a cons string object. If
12090 // both strings are ascii the result is an ascii cons string.
12091 // rax: first string
12092 // rbx: length of resulting flat string
12093 // rdx: second string
12094 // r8: instance type of first string
12095 // r9: instance type of second string
12096 Label non_ascii, allocated, ascii_data;
12097 __ movl(rcx, r8);
12098 __ and_(rcx, r9);
12099 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
12100 __ testl(rcx, Immediate(kAsciiStringTag));
12101 __ j(zero, &non_ascii);
12102 __ bind(&ascii_data);
12103 // Allocate an acsii cons string.
12104 __ AllocateAsciiConsString(rcx, rdi, no_reg, &string_add_runtime);
12105 __ bind(&allocated);
12106 // Fill the fields of the cons string.
12107 __ movq(FieldOperand(rcx, ConsString::kLengthOffset), rbx);
12108 __ movq(FieldOperand(rcx, ConsString::kHashFieldOffset),
12109 Immediate(String::kEmptyHashField));
12110 __ movq(FieldOperand(rcx, ConsString::kFirstOffset), rax);
12111 __ movq(FieldOperand(rcx, ConsString::kSecondOffset), rdx);
12112 __ movq(rax, rcx);
12113 __ IncrementCounter(&Counters::string_add_native, 1);
12114 __ ret(2 * kPointerSize);
12115 __ bind(&non_ascii);
12116 // At least one of the strings is two-byte. Check whether it happens
12117 // to contain only ascii characters.
12118 // rcx: first instance type AND second instance type.
12119 // r8: first instance type.
12120 // r9: second instance type.
12121 __ testb(rcx, Immediate(kAsciiDataHintMask));
12122 __ j(not_zero, &ascii_data);
12123 __ xor_(r8, r9);
12124 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
12125 __ andb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
12126 __ cmpb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
12127 __ j(equal, &ascii_data);
12128 // Allocate a two byte cons string.
12129 __ AllocateConsString(rcx, rdi, no_reg, &string_add_runtime);
12130 __ jmp(&allocated);
12131
12132 // Handle creating a flat result. First check that both strings are not
12133 // external strings.
12134 // rax: first string
12135 // rbx: length of resulting flat string as smi
12136 // rdx: second string
12137 // r8: instance type of first string
12138 // r9: instance type of first string
12139 __ bind(&string_add_flat_result);
12140 __ SmiToInteger32(rbx, rbx);
12141 __ movl(rcx, r8);
12142 __ and_(rcx, Immediate(kStringRepresentationMask));
12143 __ cmpl(rcx, Immediate(kExternalStringTag));
12144 __ j(equal, &string_add_runtime);
12145 __ movl(rcx, r9);
12146 __ and_(rcx, Immediate(kStringRepresentationMask));
12147 __ cmpl(rcx, Immediate(kExternalStringTag));
12148 __ j(equal, &string_add_runtime);
12149 // Now check if both strings are ascii strings.
12150 // rax: first string
12151 // rbx: length of resulting flat string
12152 // rdx: second string
12153 // r8: instance type of first string
12154 // r9: instance type of second string
12155 Label non_ascii_string_add_flat_result;
12156 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
12157 __ testl(r8, Immediate(kAsciiStringTag));
12158 __ j(zero, &non_ascii_string_add_flat_result);
12159 __ testl(r9, Immediate(kAsciiStringTag));
12160 __ j(zero, &string_add_runtime);
12161
12162 __ bind(&make_flat_ascii_string);
12163 // Both strings are ascii strings. As they are short they are both flat.
12164 __ AllocateAsciiString(rcx, rbx, rdi, r14, r11, &string_add_runtime);
12165 // rcx: result string
12166 __ movq(rbx, rcx);
12167 // Locate first character of result.
12168 __ addq(rcx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
12169 // Locate first character of first argument
12170 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
12171 __ addq(rax, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
12172 // rax: first char of first argument
12173 // rbx: result string
12174 // rcx: first character of result
12175 // rdx: second string
12176 // rdi: length of first argument
12177 StringHelper::GenerateCopyCharacters(masm, rcx, rax, rdi, true);
12178 // Locate first character of second argument.
12179 __ SmiToInteger32(rdi, FieldOperand(rdx, String::kLengthOffset));
12180 __ addq(rdx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
12181 // rbx: result string
12182 // rcx: next character of result
12183 // rdx: first char of second argument
12184 // rdi: length of second argument
12185 StringHelper::GenerateCopyCharacters(masm, rcx, rdx, rdi, true);
12186 __ movq(rax, rbx);
12187 __ IncrementCounter(&Counters::string_add_native, 1);
12188 __ ret(2 * kPointerSize);
12189
12190 // Handle creating a flat two byte result.
12191 // rax: first string - known to be two byte
12192 // rbx: length of resulting flat string
12193 // rdx: second string
12194 // r8: instance type of first string
12195 // r9: instance type of first string
12196 __ bind(&non_ascii_string_add_flat_result);
12197 __ and_(r9, Immediate(kAsciiStringTag));
12198 __ j(not_zero, &string_add_runtime);
12199 // Both strings are two byte strings. As they are short they are both
12200 // flat.
12201 __ AllocateTwoByteString(rcx, rbx, rdi, r14, r11, &string_add_runtime);
12202 // rcx: result string
12203 __ movq(rbx, rcx);
12204 // Locate first character of result.
12205 __ addq(rcx, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
12206 // Locate first character of first argument.
12207 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
12208 __ addq(rax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
12209 // rax: first char of first argument
12210 // rbx: result string
12211 // rcx: first character of result
12212 // rdx: second argument
12213 // rdi: length of first argument
12214 StringHelper::GenerateCopyCharacters(masm, rcx, rax, rdi, false);
12215 // Locate first character of second argument.
12216 __ SmiToInteger32(rdi, FieldOperand(rdx, String::kLengthOffset));
12217 __ addq(rdx, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
12218 // rbx: result string
12219 // rcx: next character of result
12220 // rdx: first char of second argument
12221 // rdi: length of second argument
12222 StringHelper::GenerateCopyCharacters(masm, rcx, rdx, rdi, false);
12223 __ movq(rax, rbx);
12224 __ IncrementCounter(&Counters::string_add_native, 1);
12225 __ ret(2 * kPointerSize);
12226
12227 // Just jump to runtime to add the two strings.
12228 __ bind(&string_add_runtime);
12229 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
12230 }
12231
12232
12233 void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
12234 Register dest,
12235 Register src,
12236 Register count,
12237 bool ascii) {
12238 Label loop;
12239 __ bind(&loop);
12240 // This loop just copies one character at a time, as it is only used for very
12241 // short strings.
12242 if (ascii) {
12243 __ movb(kScratchRegister, Operand(src, 0));
12244 __ movb(Operand(dest, 0), kScratchRegister);
12245 __ incq(src);
12246 __ incq(dest);
12247 } else {
12248 __ movzxwl(kScratchRegister, Operand(src, 0));
12249 __ movw(Operand(dest, 0), kScratchRegister);
12250 __ addq(src, Immediate(2));
12251 __ addq(dest, Immediate(2));
12252 }
12253 __ decl(count);
12254 __ j(not_zero, &loop);
12255 }
12256
12257
12258 void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
12259 Register dest,
12260 Register src,
12261 Register count,
12262 bool ascii) {
12263 // Copy characters using rep movs of doublewords. Align destination on 4 byte
12264 // boundary before starting rep movs. Copy remaining characters after running
12265 // rep movs.
12266 // Count is positive int32, dest and src are character pointers.
12267 ASSERT(dest.is(rdi)); // rep movs destination
12268 ASSERT(src.is(rsi)); // rep movs source
12269 ASSERT(count.is(rcx)); // rep movs count
12270
12271 // Nothing to do for zero characters.
12272 Label done;
12273 __ testl(count, count);
12274 __ j(zero, &done);
12275
12276 // Make count the number of bytes to copy.
12277 if (!ascii) {
12278 STATIC_ASSERT(2 == sizeof(uc16));
12279 __ addl(count, count);
12280 }
12281
12282 // Don't enter the rep movs if there are less than 4 bytes to copy.
12283 Label last_bytes;
12284 __ testl(count, Immediate(~7));
12285 __ j(zero, &last_bytes);
12286
12287 // Copy from edi to esi using rep movs instruction.
12288 __ movl(kScratchRegister, count);
12289 __ shr(count, Immediate(3)); // Number of doublewords to copy.
12290 __ repmovsq();
12291
12292 // Find number of bytes left.
12293 __ movl(count, kScratchRegister);
12294 __ and_(count, Immediate(7));
12295
12296 // Check if there are more bytes to copy.
12297 __ bind(&last_bytes);
12298 __ testl(count, count);
12299 __ j(zero, &done);
12300
12301 // Copy remaining characters.
12302 Label loop;
12303 __ bind(&loop);
12304 __ movb(kScratchRegister, Operand(src, 0));
12305 __ movb(Operand(dest, 0), kScratchRegister);
12306 __ incq(src);
12307 __ incq(dest);
12308 __ decl(count);
12309 __ j(not_zero, &loop);
12310
12311 __ bind(&done);
12312 }
12313
12314 void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
12315 Register c1,
12316 Register c2,
12317 Register scratch1,
12318 Register scratch2,
12319 Register scratch3,
12320 Register scratch4,
12321 Label* not_found) {
12322 // Register scratch3 is the general scratch register in this function.
12323 Register scratch = scratch3;
12324
12325 // Make sure that both characters are not digits as such strings has a
12326 // different hash algorithm. Don't try to look for these in the symbol table.
12327 Label not_array_index;
12328 __ leal(scratch, Operand(c1, -'0'));
12329 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
12330 __ j(above, &not_array_index);
12331 __ leal(scratch, Operand(c2, -'0'));
12332 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
12333 __ j(below_equal, not_found);
12334
12335 __ bind(&not_array_index);
12336 // Calculate the two character string hash.
12337 Register hash = scratch1;
12338 GenerateHashInit(masm, hash, c1, scratch);
12339 GenerateHashAddCharacter(masm, hash, c2, scratch);
12340 GenerateHashGetHash(masm, hash, scratch);
12341
12342 // Collect the two characters in a register.
12343 Register chars = c1;
12344 __ shl(c2, Immediate(kBitsPerByte));
12345 __ orl(chars, c2);
12346
12347 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
12348 // hash: hash of two character string.
12349
12350 // Load the symbol table.
12351 Register symbol_table = c2;
12352 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
12353
12354 // Calculate capacity mask from the symbol table capacity.
12355 Register mask = scratch2;
12356 __ SmiToInteger32(mask,
12357 FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
12358 __ decl(mask);
12359
12360 Register undefined = scratch4;
12361 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
12362
12363 // Registers
12364 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
12365 // hash: hash of two character string (32-bit int)
12366 // symbol_table: symbol table
12367 // mask: capacity mask (32-bit int)
12368 // undefined: undefined value
12369 // scratch: -
12370
12371 // Perform a number of probes in the symbol table.
12372 static const int kProbes = 4;
12373 Label found_in_symbol_table;
12374 Label next_probe[kProbes];
12375 for (int i = 0; i < kProbes; i++) {
12376 // Calculate entry in symbol table.
12377 __ movl(scratch, hash);
12378 if (i > 0) {
12379 __ addl(scratch, Immediate(SymbolTable::GetProbeOffset(i)));
12380 }
12381 __ andl(scratch, mask);
12382
12383 // Load the entry from the symble table.
12384 Register candidate = scratch; // Scratch register contains candidate.
12385 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
12386 __ movq(candidate,
12387 FieldOperand(symbol_table,
12388 scratch,
12389 times_pointer_size,
12390 SymbolTable::kElementsStartOffset));
12391
12392 // If entry is undefined no string with this hash can be found.
12393 __ cmpq(candidate, undefined);
12394 __ j(equal, not_found);
12395
12396 // If length is not 2 the string is not a candidate.
12397 __ SmiCompare(FieldOperand(candidate, String::kLengthOffset),
12398 Smi::FromInt(2));
12399 __ j(not_equal, &next_probe[i]);
12400
12401 // We use kScratchRegister as a temporary register in assumption that
12402 // JumpIfInstanceTypeIsNotSequentialAscii does not use it implicitly
12403 Register temp = kScratchRegister;
12404
12405 // Check that the candidate is a non-external ascii string.
12406 __ movq(temp, FieldOperand(candidate, HeapObject::kMapOffset));
12407 __ movzxbl(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
12408 __ JumpIfInstanceTypeIsNotSequentialAscii(
12409 temp, temp, &next_probe[i]);
12410
12411 // Check if the two characters match.
12412 __ movl(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
12413 __ andl(temp, Immediate(0x0000ffff));
12414 __ cmpl(chars, temp);
12415 __ j(equal, &found_in_symbol_table);
12416 __ bind(&next_probe[i]);
12417 }
12418
12419 // No matching 2 character string found by probing.
12420 __ jmp(not_found);
12421
12422 // Scratch register contains result when we fall through to here.
12423 Register result = scratch;
12424 __ bind(&found_in_symbol_table);
12425 if (!result.is(rax)) {
12426 __ movq(rax, result);
12427 }
12428 }
12429
12430
12431 void StringHelper::GenerateHashInit(MacroAssembler* masm,
12432 Register hash,
12433 Register character,
12434 Register scratch) {
12435 // hash = character + (character << 10);
12436 __ movl(hash, character);
12437 __ shll(hash, Immediate(10));
12438 __ addl(hash, character);
12439 // hash ^= hash >> 6;
12440 __ movl(scratch, hash);
12441 __ sarl(scratch, Immediate(6));
12442 __ xorl(hash, scratch);
12443 }
12444
12445
12446 void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
12447 Register hash,
12448 Register character,
12449 Register scratch) {
12450 // hash += character;
12451 __ addl(hash, character);
12452 // hash += hash << 10;
12453 __ movl(scratch, hash);
12454 __ shll(scratch, Immediate(10));
12455 __ addl(hash, scratch);
12456 // hash ^= hash >> 6;
12457 __ movl(scratch, hash);
12458 __ sarl(scratch, Immediate(6));
12459 __ xorl(hash, scratch);
12460 }
12461
12462
12463 void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
12464 Register hash,
12465 Register scratch) {
12466 // hash += hash << 3;
12467 __ leal(hash, Operand(hash, hash, times_8, 0));
12468 // hash ^= hash >> 11;
12469 __ movl(scratch, hash);
12470 __ sarl(scratch, Immediate(11));
12471 __ xorl(hash, scratch);
12472 // hash += hash << 15;
12473 __ movl(scratch, hash);
12474 __ shll(scratch, Immediate(15));
12475 __ addl(hash, scratch);
12476
12477 // if (hash == 0) hash = 27;
12478 Label hash_not_zero;
12479 __ j(not_zero, &hash_not_zero);
12480 __ movl(hash, Immediate(27));
12481 __ bind(&hash_not_zero);
12482 }
12483
12484 void SubStringStub::Generate(MacroAssembler* masm) {
12485 Label runtime;
12486
12487 // Stack frame on entry.
12488 // rsp[0]: return address
12489 // rsp[8]: to
12490 // rsp[16]: from
12491 // rsp[24]: string
12492
12493 const int kToOffset = 1 * kPointerSize;
12494 const int kFromOffset = kToOffset + kPointerSize;
12495 const int kStringOffset = kFromOffset + kPointerSize;
12496 const int kArgumentsSize = (kStringOffset + kPointerSize) - kToOffset;
12497
12498 // Make sure first argument is a string.
12499 __ movq(rax, Operand(rsp, kStringOffset));
12500 STATIC_ASSERT(kSmiTag == 0);
12501 __ testl(rax, Immediate(kSmiTagMask));
12502 __ j(zero, &runtime);
12503 Condition is_string = masm->IsObjectStringType(rax, rbx, rbx);
12504 __ j(NegateCondition(is_string), &runtime);
12505
12506 // rax: string
12507 // rbx: instance type
12508 // Calculate length of sub string using the smi values.
12509 Label result_longer_than_two;
12510 __ movq(rcx, Operand(rsp, kToOffset));
12511 __ movq(rdx, Operand(rsp, kFromOffset));
12512 __ JumpIfNotBothPositiveSmi(rcx, rdx, &runtime);
12513
12514 __ SmiSub(rcx, rcx, rdx, NULL); // Overflow doesn't happen.
12515 __ cmpq(FieldOperand(rax, String::kLengthOffset), rcx);
12516 Label return_rax;
12517 __ j(equal, &return_rax);
12518 // Special handling of sub-strings of length 1 and 2. One character strings
12519 // are handled in the runtime system (looked up in the single character
12520 // cache). Two character strings are looked for in the symbol cache.
12521 __ SmiToInteger32(rcx, rcx);
12522 __ cmpl(rcx, Immediate(2));
12523 __ j(greater, &result_longer_than_two);
12524 __ j(less, &runtime);
12525
12526 // Sub string of length 2 requested.
12527 // rax: string
12528 // rbx: instance type
12529 // rcx: sub string length (value is 2)
12530 // rdx: from index (smi)
12531 __ JumpIfInstanceTypeIsNotSequentialAscii(rbx, rbx, &runtime);
12532
12533 // Get the two characters forming the sub string.
12534 __ SmiToInteger32(rdx, rdx); // From index is no longer smi.
12535 __ movzxbq(rbx, FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize));
12536 __ movzxbq(rcx,
12537 FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize + 1));
12538
12539 // Try to lookup two character string in symbol table.
12540 Label make_two_character_string;
12541 StringHelper::GenerateTwoCharacterSymbolTableProbe(
12542 masm, rbx, rcx, rax, rdx, rdi, r14, &make_two_character_string);
12543 __ ret(3 * kPointerSize);
12544
12545 __ bind(&make_two_character_string);
12546 // Setup registers for allocating the two character string.
12547 __ movq(rax, Operand(rsp, kStringOffset));
12548 __ movq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
12549 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
12550 __ Set(rcx, 2);
12551
12552 __ bind(&result_longer_than_two);
12553
12554 // rax: string
12555 // rbx: instance type
12556 // rcx: result string length
12557 // Check for flat ascii string
12558 Label non_ascii_flat;
12559 __ JumpIfInstanceTypeIsNotSequentialAscii(rbx, rbx, &non_ascii_flat);
12560
12561 // Allocate the result.
12562 __ AllocateAsciiString(rax, rcx, rbx, rdx, rdi, &runtime);
12563
12564 // rax: result string
12565 // rcx: result string length
12566 __ movq(rdx, rsi); // esi used by following code.
12567 // Locate first character of result.
12568 __ lea(rdi, FieldOperand(rax, SeqAsciiString::kHeaderSize));
12569 // Load string argument and locate character of sub string start.
12570 __ movq(rsi, Operand(rsp, kStringOffset));
12571 __ movq(rbx, Operand(rsp, kFromOffset));
12572 {
12573 SmiIndex smi_as_index = masm->SmiToIndex(rbx, rbx, times_1);
12574 __ lea(rsi, Operand(rsi, smi_as_index.reg, smi_as_index.scale,
12575 SeqAsciiString::kHeaderSize - kHeapObjectTag));
12576 }
12577
12578 // rax: result string
12579 // rcx: result length
12580 // rdx: original value of rsi
12581 // rdi: first character of result
12582 // rsi: character of sub string start
12583 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, true);
12584 __ movq(rsi, rdx); // Restore rsi.
12585 __ IncrementCounter(&Counters::sub_string_native, 1);
12586 __ ret(kArgumentsSize);
12587
12588 __ bind(&non_ascii_flat);
12589 // rax: string
12590 // rbx: instance type & kStringRepresentationMask | kStringEncodingMask
12591 // rcx: result string length
12592 // Check for sequential two byte string
12593 __ cmpb(rbx, Immediate(kSeqStringTag | kTwoByteStringTag));
12594 __ j(not_equal, &runtime);
12595
12596 // Allocate the result.
12597 __ AllocateTwoByteString(rax, rcx, rbx, rdx, rdi, &runtime);
12598
12599 // rax: result string
12600 // rcx: result string length
12601 __ movq(rdx, rsi); // esi used by following code.
12602 // Locate first character of result.
12603 __ lea(rdi, FieldOperand(rax, SeqTwoByteString::kHeaderSize));
12604 // Load string argument and locate character of sub string start.
12605 __ movq(rsi, Operand(rsp, kStringOffset));
12606 __ movq(rbx, Operand(rsp, kFromOffset));
12607 {
12608 SmiIndex smi_as_index = masm->SmiToIndex(rbx, rbx, times_2);
12609 __ lea(rsi, Operand(rsi, smi_as_index.reg, smi_as_index.scale,
12610 SeqAsciiString::kHeaderSize - kHeapObjectTag));
12611 }
12612
12613 // rax: result string
12614 // rcx: result length
12615 // rdx: original value of rsi
12616 // rdi: first character of result
12617 // rsi: character of sub string start
12618 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, false);
12619 __ movq(rsi, rdx); // Restore esi.
12620
12621 __ bind(&return_rax);
12622 __ IncrementCounter(&Counters::sub_string_native, 1);
12623 __ ret(kArgumentsSize);
12624
12625 // Just jump to runtime to create the sub string.
12626 __ bind(&runtime);
12627 __ TailCallRuntime(Runtime::kSubString, 3, 1);
12628 }
12629
12630
12631 void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
12632 Register left,
12633 Register right,
12634 Register scratch1,
12635 Register scratch2,
12636 Register scratch3,
12637 Register scratch4) {
12638 // Ensure that you can always subtract a string length from a non-negative
12639 // number (e.g. another length).
12640 STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
12641
12642 // Find minimum length and length difference.
12643 __ movq(scratch1, FieldOperand(left, String::kLengthOffset));
12644 __ movq(scratch4, scratch1);
12645 __ SmiSub(scratch4,
12646 scratch4,
12647 FieldOperand(right, String::kLengthOffset),
12648 NULL);
12649 // Register scratch4 now holds left.length - right.length.
12650 const Register length_difference = scratch4;
12651 Label left_shorter;
12652 __ j(less, &left_shorter);
12653 // The right string isn't longer that the left one.
12654 // Get the right string's length by subtracting the (non-negative) difference
12655 // from the left string's length.
12656 __ SmiSub(scratch1, scratch1, length_difference, NULL);
12657 __ bind(&left_shorter);
12658 // Register scratch1 now holds Min(left.length, right.length).
12659 const Register min_length = scratch1;
12660
12661 Label compare_lengths;
12662 // If min-length is zero, go directly to comparing lengths.
12663 __ SmiTest(min_length);
12664 __ j(zero, &compare_lengths);
12665
12666 __ SmiToInteger32(min_length, min_length);
12667
12668 // Registers scratch2 and scratch3 are free.
12669 Label result_not_equal;
12670 Label loop;
12671 {
12672 // Check characters 0 .. min_length - 1 in a loop.
12673 // Use scratch3 as loop index, min_length as limit and scratch2
12674 // for computation.
12675 const Register index = scratch3;
12676 __ movl(index, Immediate(0)); // Index into strings.
12677 __ bind(&loop);
12678 // Compare characters.
12679 // TODO(lrn): Could we load more than one character at a time?
12680 __ movb(scratch2, FieldOperand(left,
12681 index,
12682 times_1,
12683 SeqAsciiString::kHeaderSize));
12684 // Increment index and use -1 modifier on next load to give
12685 // the previous load extra time to complete.
12686 __ addl(index, Immediate(1));
12687 __ cmpb(scratch2, FieldOperand(right,
12688 index,
12689 times_1,
12690 SeqAsciiString::kHeaderSize - 1));
12691 __ j(not_equal, &result_not_equal);
12692 __ cmpl(index, min_length);
12693 __ j(not_equal, &loop);
12694 }
12695 // Completed loop without finding different characters.
12696 // Compare lengths (precomputed).
12697 __ bind(&compare_lengths);
12698 __ SmiTest(length_difference);
12699 __ j(not_zero, &result_not_equal);
12700
12701 // Result is EQUAL.
12702 __ Move(rax, Smi::FromInt(EQUAL));
12703 __ ret(0);
12704
12705 Label result_greater;
12706 __ bind(&result_not_equal);
12707 // Unequal comparison of left to right, either character or length.
12708 __ j(greater, &result_greater);
12709
12710 // Result is LESS.
12711 __ Move(rax, Smi::FromInt(LESS));
12712 __ ret(0);
12713
12714 // Result is GREATER.
12715 __ bind(&result_greater);
12716 __ Move(rax, Smi::FromInt(GREATER));
12717 __ ret(0);
12718 }
12719
12720
12721 void StringCompareStub::Generate(MacroAssembler* masm) {
12722 Label runtime;
12723
12724 // Stack frame on entry.
12725 // rsp[0]: return address
12726 // rsp[8]: right string
12727 // rsp[16]: left string
12728
12729 __ movq(rdx, Operand(rsp, 2 * kPointerSize)); // left
12730 __ movq(rax, Operand(rsp, 1 * kPointerSize)); // right
12731
12732 // Check for identity.
12733 Label not_same;
12734 __ cmpq(rdx, rax);
12735 __ j(not_equal, &not_same);
12736 __ Move(rax, Smi::FromInt(EQUAL));
12737 __ IncrementCounter(&Counters::string_compare_native, 1);
12738 __ ret(2 * kPointerSize);
12739
12740 __ bind(&not_same);
12741
12742 // Check that both are sequential ASCII strings.
12743 __ JumpIfNotBothSequentialAsciiStrings(rdx, rax, rcx, rbx, &runtime);
12744
12745 // Inline comparison of ascii strings.
12746 __ IncrementCounter(&Counters::string_compare_native, 1);
12747 // Drop arguments from the stack
12748 __ pop(rcx);
12749 __ addq(rsp, Immediate(2 * kPointerSize));
12750 __ push(rcx);
12751 GenerateCompareFlatAsciiStrings(masm, rdx, rax, rcx, rbx, rdi, r8);
12752
12753 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
12754 // tagged as a small integer.
12755 __ bind(&runtime);
12756 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
12757 }
12758
12759 #undef __ 8788 #undef __
12760 8789
12761 #define __ masm. 8790 #define __ masm.
12762 8791
12763 #ifdef _WIN64 8792 #ifdef _WIN64
12764 typedef double (*ModuloFunction)(double, double); 8793 typedef double (*ModuloFunction)(double, double);
12765 // Define custom fmod implementation. 8794 // Define custom fmod implementation.
12766 ModuloFunction CreateModuloFunction() { 8795 ModuloFunction CreateModuloFunction() {
12767 size_t actual_size; 8796 size_t actual_size;
12768 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, 8797 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
12853 #undef __ 8882 #undef __
12854 8883
12855 void RecordWriteStub::Generate(MacroAssembler* masm) { 8884 void RecordWriteStub::Generate(MacroAssembler* masm) {
12856 masm->RecordWriteHelper(object_, addr_, scratch_); 8885 masm->RecordWriteHelper(object_, addr_, scratch_);
12857 masm->ret(0); 8886 masm->ret(0);
12858 } 8887 }
12859 8888
12860 } } // namespace v8::internal 8889 } } // namespace v8::internal
12861 8890
12862 #endif // V8_TARGET_ARCH_X64 8891 #endif // V8_TARGET_ARCH_X64
OLDNEW
« no previous file with comments | « src/x64/codegen-x64.h ('k') | src/x64/full-codegen-x64.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698