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

Side by Side Diff: src/ia32/codegen-ia32.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, 4 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/ia32/codegen-ia32.h ('k') | src/ia32/full-codegen-ia32.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_IA32) 30 #if defined(V8_TARGET_ARCH_IA32)
31 31
32 #include "bootstrapper.h" 32 #include "bootstrapper.h"
33 #include "code-stubs-ia32.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 884 matching lines...) Expand 10 before | Expand all | Expand 10 after
927 ToBooleanStub stub; 928 ToBooleanStub stub;
928 Result temp = frame_->CallStub(&stub, 1); 929 Result temp = frame_->CallStub(&stub, 1);
929 // Convert the result to a condition code. 930 // Convert the result to a condition code.
930 __ test(temp.reg(), Operand(temp.reg())); 931 __ test(temp.reg(), Operand(temp.reg()));
931 temp.Unuse(); 932 temp.Unuse();
932 dest->Split(not_equal); 933 dest->Split(not_equal);
933 } 934 }
934 } 935 }
935 936
936 937
937 class FloatingPointHelper : public AllStatic {
938 public:
939
940 enum ArgLocation {
941 ARGS_ON_STACK,
942 ARGS_IN_REGISTERS
943 };
944
945 // Code pattern for loading a floating point value. Input value must
946 // be either a smi or a heap number object (fp value). Requirements:
947 // operand in register number. Returns operand as floating point number
948 // on FPU stack.
949 static void LoadFloatOperand(MacroAssembler* masm, Register number);
950
951 // Code pattern for loading floating point values. Input values must
952 // be either smi or heap number objects (fp values). Requirements:
953 // operand_1 on TOS+1 or in edx, operand_2 on TOS+2 or in eax.
954 // Returns operands as floating point numbers on FPU stack.
955 static void LoadFloatOperands(MacroAssembler* masm,
956 Register scratch,
957 ArgLocation arg_location = ARGS_ON_STACK);
958
959 // Similar to LoadFloatOperand but assumes that both operands are smis.
960 // Expects operands in edx, eax.
961 static void LoadFloatSmis(MacroAssembler* masm, Register scratch);
962
963 // Test if operands are smi or number objects (fp). Requirements:
964 // operand_1 in eax, operand_2 in edx; falls through on float
965 // operands, jumps to the non_float label otherwise.
966 static void CheckFloatOperands(MacroAssembler* masm,
967 Label* non_float,
968 Register scratch);
969
970 // Takes the operands in edx and eax and loads them as integers in eax
971 // and ecx.
972 static void LoadAsIntegers(MacroAssembler* masm,
973 TypeInfo type_info,
974 bool use_sse3,
975 Label* operand_conversion_failure);
976 static void LoadNumbersAsIntegers(MacroAssembler* masm,
977 TypeInfo type_info,
978 bool use_sse3,
979 Label* operand_conversion_failure);
980 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
981 bool use_sse3,
982 Label* operand_conversion_failure);
983
984 // Test if operands are smis or heap numbers and load them
985 // into xmm0 and xmm1 if they are. Operands are in edx and eax.
986 // Leaves operands unchanged.
987 static void LoadSSE2Operands(MacroAssembler* masm);
988
989 // Test if operands are numbers (smi or HeapNumber objects), and load
990 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
991 // either operand is not a number. Operands are in edx and eax.
992 // Leaves operands unchanged.
993 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
994
995 // Similar to LoadSSE2Operands but assumes that both operands are smis.
996 // Expects operands in edx, eax.
997 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
998 };
999
1000
1001 const char* GenericBinaryOpStub::GetName() {
1002 if (name_ != NULL) return name_;
1003 const int kMaxNameLength = 100;
1004 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
1005 if (name_ == NULL) return "OOM";
1006 const char* op_name = Token::Name(op_);
1007 const char* overwrite_name;
1008 switch (mode_) {
1009 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
1010 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
1011 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
1012 default: overwrite_name = "UnknownOverwrite"; break;
1013 }
1014
1015 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
1016 "GenericBinaryOpStub_%s_%s%s_%s%s_%s_%s",
1017 op_name,
1018 overwrite_name,
1019 (flags_ & NO_SMI_CODE_IN_STUB) ? "_NoSmiInStub" : "",
1020 args_in_registers_ ? "RegArgs" : "StackArgs",
1021 args_reversed_ ? "_R" : "",
1022 static_operands_type_.ToString(),
1023 BinaryOpIC::GetName(runtime_operands_type_));
1024 return name_;
1025 }
1026
1027
1028 // Perform or call the specialized stub for a binary operation. Requires the 938 // Perform or call the specialized stub for a binary operation. Requires the
1029 // three registers left, right and dst to be distinct and spilled. This 939 // three registers left, right and dst to be distinct and spilled. This
1030 // deferred operation has up to three entry points: The main one calls the 940 // deferred operation has up to three entry points: The main one calls the
1031 // runtime system. The second is for when the result is a non-Smi. The 941 // runtime system. The second is for when the result is a non-Smi. The
1032 // third is for when at least one of the inputs is non-Smi and we have SSE2. 942 // third is for when at least one of the inputs is non-Smi and we have SSE2.
1033 class DeferredInlineBinaryOperation: public DeferredCode { 943 class DeferredInlineBinaryOperation: public DeferredCode {
1034 public: 944 public:
1035 DeferredInlineBinaryOperation(Token::Value op, 945 DeferredInlineBinaryOperation(Token::Value op,
1036 Register dst, 946 Register dst,
1037 Register left, 947 Register left,
(...skipping 8835 matching lines...) Expand 10 before | Expand all | Expand 10 after
9873 break; 9783 break;
9874 } 9784 }
9875 9785
9876 case UNLOADED: 9786 case UNLOADED:
9877 case ILLEGAL: 9787 case ILLEGAL:
9878 UNREACHABLE(); 9788 UNREACHABLE();
9879 } 9789 }
9880 } 9790 }
9881 9791
9882 9792
9883 void FastNewClosureStub::Generate(MacroAssembler* masm) {
9884 // Create a new closure from the given function info in new
9885 // space. Set the context to the current context in esi.
9886 Label gc;
9887 __ AllocateInNewSpace(JSFunction::kSize, eax, ebx, ecx, &gc, TAG_OBJECT);
9888
9889 // Get the function info from the stack.
9890 __ mov(edx, Operand(esp, 1 * kPointerSize));
9891
9892 // Compute the function map in the current global context and set that
9893 // as the map of the allocated object.
9894 __ mov(ecx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
9895 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalContextOffset));
9896 __ mov(ecx, Operand(ecx, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
9897 __ mov(FieldOperand(eax, JSObject::kMapOffset), ecx);
9898
9899 // Initialize the rest of the function. We don't have to update the
9900 // write barrier because the allocated object is in new space.
9901 __ mov(ebx, Immediate(Factory::empty_fixed_array()));
9902 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ebx);
9903 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
9904 __ mov(FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset),
9905 Immediate(Factory::the_hole_value()));
9906 __ mov(FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset), edx);
9907 __ mov(FieldOperand(eax, JSFunction::kContextOffset), esi);
9908 __ mov(FieldOperand(eax, JSFunction::kLiteralsOffset), ebx);
9909
9910 // Initialize the code pointer in the function to be the one
9911 // found in the shared function info object.
9912 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
9913 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
9914 __ mov(FieldOperand(eax, JSFunction::kCodeEntryOffset), edx);
9915
9916 // Return and remove the on-stack parameter.
9917 __ ret(1 * kPointerSize);
9918
9919 // Create a new closure through the slower runtime call.
9920 __ bind(&gc);
9921 __ pop(ecx); // Temporarily remove return address.
9922 __ pop(edx);
9923 __ push(esi);
9924 __ push(edx);
9925 __ push(ecx); // Restore return address.
9926 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
9927 }
9928
9929
9930 void FastNewContextStub::Generate(MacroAssembler* masm) {
9931 // Try to allocate the context in new space.
9932 Label gc;
9933 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
9934 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
9935 eax, ebx, ecx, &gc, TAG_OBJECT);
9936
9937 // Get the function from the stack.
9938 __ mov(ecx, Operand(esp, 1 * kPointerSize));
9939
9940 // Setup the object header.
9941 __ mov(FieldOperand(eax, HeapObject::kMapOffset), Factory::context_map());
9942 __ mov(FieldOperand(eax, Context::kLengthOffset),
9943 Immediate(Smi::FromInt(length)));
9944
9945 // Setup the fixed slots.
9946 __ xor_(ebx, Operand(ebx)); // Set to NULL.
9947 __ mov(Operand(eax, Context::SlotOffset(Context::CLOSURE_INDEX)), ecx);
9948 __ mov(Operand(eax, Context::SlotOffset(Context::FCONTEXT_INDEX)), eax);
9949 __ mov(Operand(eax, Context::SlotOffset(Context::PREVIOUS_INDEX)), ebx);
9950 __ mov(Operand(eax, Context::SlotOffset(Context::EXTENSION_INDEX)), ebx);
9951
9952 // Copy the global object from the surrounding context. We go through the
9953 // context in the function (ecx) to match the allocation behavior we have
9954 // in the runtime system (see Heap::AllocateFunctionContext).
9955 __ mov(ebx, FieldOperand(ecx, JSFunction::kContextOffset));
9956 __ mov(ebx, Operand(ebx, Context::SlotOffset(Context::GLOBAL_INDEX)));
9957 __ mov(Operand(eax, Context::SlotOffset(Context::GLOBAL_INDEX)), ebx);
9958
9959 // Initialize the rest of the slots to undefined.
9960 __ mov(ebx, Factory::undefined_value());
9961 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
9962 __ mov(Operand(eax, Context::SlotOffset(i)), ebx);
9963 }
9964
9965 // Return and remove the on-stack parameter.
9966 __ mov(esi, Operand(eax));
9967 __ ret(1 * kPointerSize);
9968
9969 // Need to collect. Call into runtime system.
9970 __ bind(&gc);
9971 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
9972 }
9973
9974
9975 void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
9976 // Stack layout on entry:
9977 //
9978 // [esp + kPointerSize]: constant elements.
9979 // [esp + (2 * kPointerSize)]: literal index.
9980 // [esp + (3 * kPointerSize)]: literals array.
9981
9982 // All sizes here are multiples of kPointerSize.
9983 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
9984 int size = JSArray::kSize + elements_size;
9985
9986 // Load boilerplate object into ecx and check if we need to create a
9987 // boilerplate.
9988 Label slow_case;
9989 __ mov(ecx, Operand(esp, 3 * kPointerSize));
9990 __ mov(eax, Operand(esp, 2 * kPointerSize));
9991 STATIC_ASSERT(kPointerSize == 4);
9992 STATIC_ASSERT(kSmiTagSize == 1);
9993 STATIC_ASSERT(kSmiTag == 0);
9994 __ mov(ecx, CodeGenerator::FixedArrayElementOperand(ecx, eax));
9995 __ cmp(ecx, Factory::undefined_value());
9996 __ j(equal, &slow_case);
9997
9998 if (FLAG_debug_code) {
9999 const char* message;
10000 Handle<Map> expected_map;
10001 if (mode_ == CLONE_ELEMENTS) {
10002 message = "Expected (writable) fixed array";
10003 expected_map = Factory::fixed_array_map();
10004 } else {
10005 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
10006 message = "Expected copy-on-write fixed array";
10007 expected_map = Factory::fixed_cow_array_map();
10008 }
10009 __ push(ecx);
10010 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
10011 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset), expected_map);
10012 __ Assert(equal, message);
10013 __ pop(ecx);
10014 }
10015
10016 // Allocate both the JS array and the elements array in one big
10017 // allocation. This avoids multiple limit checks.
10018 __ AllocateInNewSpace(size, eax, ebx, edx, &slow_case, TAG_OBJECT);
10019
10020 // Copy the JS array part.
10021 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
10022 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
10023 __ mov(ebx, FieldOperand(ecx, i));
10024 __ mov(FieldOperand(eax, i), ebx);
10025 }
10026 }
10027
10028 if (length_ > 0) {
10029 // Get hold of the elements array of the boilerplate and setup the
10030 // elements pointer in the resulting object.
10031 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
10032 __ lea(edx, Operand(eax, JSArray::kSize));
10033 __ mov(FieldOperand(eax, JSArray::kElementsOffset), edx);
10034
10035 // Copy the elements array.
10036 for (int i = 0; i < elements_size; i += kPointerSize) {
10037 __ mov(ebx, FieldOperand(ecx, i));
10038 __ mov(FieldOperand(edx, i), ebx);
10039 }
10040 }
10041
10042 // Return and remove the on-stack parameters.
10043 __ ret(3 * kPointerSize);
10044
10045 __ bind(&slow_case);
10046 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
10047 }
10048
10049
10050 // NOTE: The stub does not handle the inlined cases (Smis, Booleans, undefined).
10051 void ToBooleanStub::Generate(MacroAssembler* masm) {
10052 Label false_result, true_result, not_string;
10053 __ mov(eax, Operand(esp, 1 * kPointerSize));
10054
10055 // 'null' => false.
10056 __ cmp(eax, Factory::null_value());
10057 __ j(equal, &false_result);
10058
10059 // Get the map and type of the heap object.
10060 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
10061 __ movzx_b(ecx, FieldOperand(edx, Map::kInstanceTypeOffset));
10062
10063 // Undetectable => false.
10064 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
10065 1 << Map::kIsUndetectable);
10066 __ j(not_zero, &false_result);
10067
10068 // JavaScript object => true.
10069 __ CmpInstanceType(edx, FIRST_JS_OBJECT_TYPE);
10070 __ j(above_equal, &true_result);
10071
10072 // String value => false iff empty.
10073 __ CmpInstanceType(edx, FIRST_NONSTRING_TYPE);
10074 __ j(above_equal, &not_string);
10075 STATIC_ASSERT(kSmiTag == 0);
10076 __ cmp(FieldOperand(eax, String::kLengthOffset), Immediate(0));
10077 __ j(zero, &false_result);
10078 __ jmp(&true_result);
10079
10080 __ bind(&not_string);
10081 // HeapNumber => false iff +0, -0, or NaN.
10082 __ cmp(edx, Factory::heap_number_map());
10083 __ j(not_equal, &true_result);
10084 __ fldz();
10085 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
10086 __ FCmp();
10087 __ j(zero, &false_result);
10088 // Fall through to |true_result|.
10089
10090 // Return 1/0 for true/false in eax.
10091 __ bind(&true_result);
10092 __ mov(eax, 1);
10093 __ ret(1 * kPointerSize);
10094 __ bind(&false_result);
10095 __ mov(eax, 0);
10096 __ ret(1 * kPointerSize);
10097 }
10098
10099
10100 void GenericBinaryOpStub::GenerateCall(
10101 MacroAssembler* masm,
10102 Register left,
10103 Register right) {
10104 if (!ArgsInRegistersSupported()) {
10105 // Pass arguments on the stack.
10106 __ push(left);
10107 __ push(right);
10108 } else {
10109 // The calling convention with registers is left in edx and right in eax.
10110 Register left_arg = edx;
10111 Register right_arg = eax;
10112 if (!(left.is(left_arg) && right.is(right_arg))) {
10113 if (left.is(right_arg) && right.is(left_arg)) {
10114 if (IsOperationCommutative()) {
10115 SetArgsReversed();
10116 } else {
10117 __ xchg(left, right);
10118 }
10119 } else if (left.is(left_arg)) {
10120 __ mov(right_arg, right);
10121 } else if (right.is(right_arg)) {
10122 __ mov(left_arg, left);
10123 } else if (left.is(right_arg)) {
10124 if (IsOperationCommutative()) {
10125 __ mov(left_arg, right);
10126 SetArgsReversed();
10127 } else {
10128 // Order of moves important to avoid destroying left argument.
10129 __ mov(left_arg, left);
10130 __ mov(right_arg, right);
10131 }
10132 } else if (right.is(left_arg)) {
10133 if (IsOperationCommutative()) {
10134 __ mov(right_arg, left);
10135 SetArgsReversed();
10136 } else {
10137 // Order of moves important to avoid destroying right argument.
10138 __ mov(right_arg, right);
10139 __ mov(left_arg, left);
10140 }
10141 } else {
10142 // Order of moves is not important.
10143 __ mov(left_arg, left);
10144 __ mov(right_arg, right);
10145 }
10146 }
10147
10148 // Update flags to indicate that arguments are in registers.
10149 SetArgsInRegisters();
10150 __ IncrementCounter(&Counters::generic_binary_stub_calls_regs, 1);
10151 }
10152
10153 // Call the stub.
10154 __ CallStub(this);
10155 }
10156
10157
10158 void GenericBinaryOpStub::GenerateCall(
10159 MacroAssembler* masm,
10160 Register left,
10161 Smi* right) {
10162 if (!ArgsInRegistersSupported()) {
10163 // Pass arguments on the stack.
10164 __ push(left);
10165 __ push(Immediate(right));
10166 } else {
10167 // The calling convention with registers is left in edx and right in eax.
10168 Register left_arg = edx;
10169 Register right_arg = eax;
10170 if (left.is(left_arg)) {
10171 __ mov(right_arg, Immediate(right));
10172 } else if (left.is(right_arg) && IsOperationCommutative()) {
10173 __ mov(left_arg, Immediate(right));
10174 SetArgsReversed();
10175 } else {
10176 // For non-commutative operations, left and right_arg might be
10177 // the same register. Therefore, the order of the moves is
10178 // important here in order to not overwrite left before moving
10179 // it to left_arg.
10180 __ mov(left_arg, left);
10181 __ mov(right_arg, Immediate(right));
10182 }
10183
10184 // Update flags to indicate that arguments are in registers.
10185 SetArgsInRegisters();
10186 __ IncrementCounter(&Counters::generic_binary_stub_calls_regs, 1);
10187 }
10188
10189 // Call the stub.
10190 __ CallStub(this);
10191 }
10192
10193
10194 void GenericBinaryOpStub::GenerateCall(
10195 MacroAssembler* masm,
10196 Smi* left,
10197 Register right) {
10198 if (!ArgsInRegistersSupported()) {
10199 // Pass arguments on the stack.
10200 __ push(Immediate(left));
10201 __ push(right);
10202 } else {
10203 // The calling convention with registers is left in edx and right in eax.
10204 Register left_arg = edx;
10205 Register right_arg = eax;
10206 if (right.is(right_arg)) {
10207 __ mov(left_arg, Immediate(left));
10208 } else if (right.is(left_arg) && IsOperationCommutative()) {
10209 __ mov(right_arg, Immediate(left));
10210 SetArgsReversed();
10211 } else {
10212 // For non-commutative operations, right and left_arg might be
10213 // the same register. Therefore, the order of the moves is
10214 // important here in order to not overwrite right before moving
10215 // it to right_arg.
10216 __ mov(right_arg, right);
10217 __ mov(left_arg, Immediate(left));
10218 }
10219 // Update flags to indicate that arguments are in registers.
10220 SetArgsInRegisters();
10221 __ IncrementCounter(&Counters::generic_binary_stub_calls_regs, 1);
10222 }
10223
10224 // Call the stub.
10225 __ CallStub(this);
10226 }
10227
10228
10229 Result GenericBinaryOpStub::GenerateCall(MacroAssembler* masm, 9793 Result GenericBinaryOpStub::GenerateCall(MacroAssembler* masm,
10230 VirtualFrame* frame, 9794 VirtualFrame* frame,
10231 Result* left, 9795 Result* left,
10232 Result* right) { 9796 Result* right) {
10233 if (ArgsInRegistersSupported()) { 9797 if (ArgsInRegistersSupported()) {
10234 SetArgsInRegisters(); 9798 SetArgsInRegisters();
10235 return frame->CallStub(this, left, right); 9799 return frame->CallStub(this, left, right);
10236 } else { 9800 } else {
10237 frame->Push(left); 9801 frame->Push(left);
10238 frame->Push(right); 9802 frame->Push(right);
10239 return frame->CallStub(this, 2); 9803 return frame->CallStub(this, 2);
10240 } 9804 }
10241 } 9805 }
10242 9806
10243 9807
10244 void GenericBinaryOpStub::GenerateSmiCode(MacroAssembler* masm, Label* slow) {
10245 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
10246 // dividend in eax and edx free for the division. Use eax, ebx for those.
10247 Comment load_comment(masm, "-- Load arguments");
10248 Register left = edx;
10249 Register right = eax;
10250 if (op_ == Token::DIV || op_ == Token::MOD) {
10251 left = eax;
10252 right = ebx;
10253 if (HasArgsInRegisters()) {
10254 __ mov(ebx, eax);
10255 __ mov(eax, edx);
10256 }
10257 }
10258 if (!HasArgsInRegisters()) {
10259 __ mov(right, Operand(esp, 1 * kPointerSize));
10260 __ mov(left, Operand(esp, 2 * kPointerSize));
10261 }
10262
10263 if (static_operands_type_.IsSmi()) {
10264 if (FLAG_debug_code) {
10265 __ AbortIfNotSmi(left);
10266 __ AbortIfNotSmi(right);
10267 }
10268 if (op_ == Token::BIT_OR) {
10269 __ or_(right, Operand(left));
10270 GenerateReturn(masm);
10271 return;
10272 } else if (op_ == Token::BIT_AND) {
10273 __ and_(right, Operand(left));
10274 GenerateReturn(masm);
10275 return;
10276 } else if (op_ == Token::BIT_XOR) {
10277 __ xor_(right, Operand(left));
10278 GenerateReturn(masm);
10279 return;
10280 }
10281 }
10282
10283 // 2. Prepare the smi check of both operands by oring them together.
10284 Comment smi_check_comment(masm, "-- Smi check arguments");
10285 Label not_smis;
10286 Register combined = ecx;
10287 ASSERT(!left.is(combined) && !right.is(combined));
10288 switch (op_) {
10289 case Token::BIT_OR:
10290 // Perform the operation into eax and smi check the result. Preserve
10291 // eax in case the result is not a smi.
10292 ASSERT(!left.is(ecx) && !right.is(ecx));
10293 __ mov(ecx, right);
10294 __ or_(right, Operand(left)); // Bitwise or is commutative.
10295 combined = right;
10296 break;
10297
10298 case Token::BIT_XOR:
10299 case Token::BIT_AND:
10300 case Token::ADD:
10301 case Token::SUB:
10302 case Token::MUL:
10303 case Token::DIV:
10304 case Token::MOD:
10305 __ mov(combined, right);
10306 __ or_(combined, Operand(left));
10307 break;
10308
10309 case Token::SHL:
10310 case Token::SAR:
10311 case Token::SHR:
10312 // Move the right operand into ecx for the shift operation, use eax
10313 // for the smi check register.
10314 ASSERT(!left.is(ecx) && !right.is(ecx));
10315 __ mov(ecx, right);
10316 __ or_(right, Operand(left));
10317 combined = right;
10318 break;
10319
10320 default:
10321 break;
10322 }
10323
10324 // 3. Perform the smi check of the operands.
10325 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
10326 __ test(combined, Immediate(kSmiTagMask));
10327 __ j(not_zero, &not_smis, not_taken);
10328
10329 // 4. Operands are both smis, perform the operation leaving the result in
10330 // eax and check the result if necessary.
10331 Comment perform_smi(masm, "-- Perform smi operation");
10332 Label use_fp_on_smis;
10333 switch (op_) {
10334 case Token::BIT_OR:
10335 // Nothing to do.
10336 break;
10337
10338 case Token::BIT_XOR:
10339 ASSERT(right.is(eax));
10340 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
10341 break;
10342
10343 case Token::BIT_AND:
10344 ASSERT(right.is(eax));
10345 __ and_(right, Operand(left)); // Bitwise and is commutative.
10346 break;
10347
10348 case Token::SHL:
10349 // Remove tags from operands (but keep sign).
10350 __ SmiUntag(left);
10351 __ SmiUntag(ecx);
10352 // Perform the operation.
10353 __ shl_cl(left);
10354 // Check that the *signed* result fits in a smi.
10355 __ cmp(left, 0xc0000000);
10356 __ j(sign, &use_fp_on_smis, not_taken);
10357 // Tag the result and store it in register eax.
10358 __ SmiTag(left);
10359 __ mov(eax, left);
10360 break;
10361
10362 case Token::SAR:
10363 // Remove tags from operands (but keep sign).
10364 __ SmiUntag(left);
10365 __ SmiUntag(ecx);
10366 // Perform the operation.
10367 __ sar_cl(left);
10368 // Tag the result and store it in register eax.
10369 __ SmiTag(left);
10370 __ mov(eax, left);
10371 break;
10372
10373 case Token::SHR:
10374 // Remove tags from operands (but keep sign).
10375 __ SmiUntag(left);
10376 __ SmiUntag(ecx);
10377 // Perform the operation.
10378 __ shr_cl(left);
10379 // Check that the *unsigned* result fits in a smi.
10380 // Neither of the two high-order bits can be set:
10381 // - 0x80000000: high bit would be lost when smi tagging.
10382 // - 0x40000000: this number would convert to negative when
10383 // Smi tagging these two cases can only happen with shifts
10384 // by 0 or 1 when handed a valid smi.
10385 __ test(left, Immediate(0xc0000000));
10386 __ j(not_zero, slow, not_taken);
10387 // Tag the result and store it in register eax.
10388 __ SmiTag(left);
10389 __ mov(eax, left);
10390 break;
10391
10392 case Token::ADD:
10393 ASSERT(right.is(eax));
10394 __ add(right, Operand(left)); // Addition is commutative.
10395 __ j(overflow, &use_fp_on_smis, not_taken);
10396 break;
10397
10398 case Token::SUB:
10399 __ sub(left, Operand(right));
10400 __ j(overflow, &use_fp_on_smis, not_taken);
10401 __ mov(eax, left);
10402 break;
10403
10404 case Token::MUL:
10405 // If the smi tag is 0 we can just leave the tag on one operand.
10406 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
10407 // We can't revert the multiplication if the result is not a smi
10408 // so save the right operand.
10409 __ mov(ebx, right);
10410 // Remove tag from one of the operands (but keep sign).
10411 __ SmiUntag(right);
10412 // Do multiplication.
10413 __ imul(right, Operand(left)); // Multiplication is commutative.
10414 __ j(overflow, &use_fp_on_smis, not_taken);
10415 // Check for negative zero result. Use combined = left | right.
10416 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
10417 break;
10418
10419 case Token::DIV:
10420 // We can't revert the division if the result is not a smi so
10421 // save the left operand.
10422 __ mov(edi, left);
10423 // Check for 0 divisor.
10424 __ test(right, Operand(right));
10425 __ j(zero, &use_fp_on_smis, not_taken);
10426 // Sign extend left into edx:eax.
10427 ASSERT(left.is(eax));
10428 __ cdq();
10429 // Divide edx:eax by right.
10430 __ idiv(right);
10431 // Check for the corner case of dividing the most negative smi by
10432 // -1. We cannot use the overflow flag, since it is not set by idiv
10433 // instruction.
10434 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
10435 __ cmp(eax, 0x40000000);
10436 __ j(equal, &use_fp_on_smis);
10437 // Check for negative zero result. Use combined = left | right.
10438 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
10439 // Check that the remainder is zero.
10440 __ test(edx, Operand(edx));
10441 __ j(not_zero, &use_fp_on_smis);
10442 // Tag the result and store it in register eax.
10443 __ SmiTag(eax);
10444 break;
10445
10446 case Token::MOD:
10447 // Check for 0 divisor.
10448 __ test(right, Operand(right));
10449 __ j(zero, &not_smis, not_taken);
10450
10451 // Sign extend left into edx:eax.
10452 ASSERT(left.is(eax));
10453 __ cdq();
10454 // Divide edx:eax by right.
10455 __ idiv(right);
10456 // Check for negative zero result. Use combined = left | right.
10457 __ NegativeZeroTest(edx, combined, slow);
10458 // Move remainder to register eax.
10459 __ mov(eax, edx);
10460 break;
10461
10462 default:
10463 UNREACHABLE();
10464 }
10465
10466 // 5. Emit return of result in eax.
10467 GenerateReturn(masm);
10468
10469 // 6. For some operations emit inline code to perform floating point
10470 // operations on known smis (e.g., if the result of the operation
10471 // overflowed the smi range).
10472 switch (op_) {
10473 case Token::SHL: {
10474 Comment perform_float(masm, "-- Perform float operation on smis");
10475 __ bind(&use_fp_on_smis);
10476 // Result we want is in left == edx, so we can put the allocated heap
10477 // number in eax.
10478 __ AllocateHeapNumber(eax, ecx, ebx, slow);
10479 // Store the result in the HeapNumber and return.
10480 if (CpuFeatures::IsSupported(SSE2)) {
10481 CpuFeatures::Scope use_sse2(SSE2);
10482 __ cvtsi2sd(xmm0, Operand(left));
10483 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
10484 } else {
10485 // It's OK to overwrite the right argument on the stack because we
10486 // are about to return.
10487 __ mov(Operand(esp, 1 * kPointerSize), left);
10488 __ fild_s(Operand(esp, 1 * kPointerSize));
10489 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
10490 }
10491 GenerateReturn(masm);
10492 break;
10493 }
10494
10495 case Token::ADD:
10496 case Token::SUB:
10497 case Token::MUL:
10498 case Token::DIV: {
10499 Comment perform_float(masm, "-- Perform float operation on smis");
10500 __ bind(&use_fp_on_smis);
10501 // Restore arguments to edx, eax.
10502 switch (op_) {
10503 case Token::ADD:
10504 // Revert right = right + left.
10505 __ sub(right, Operand(left));
10506 break;
10507 case Token::SUB:
10508 // Revert left = left - right.
10509 __ add(left, Operand(right));
10510 break;
10511 case Token::MUL:
10512 // Right was clobbered but a copy is in ebx.
10513 __ mov(right, ebx);
10514 break;
10515 case Token::DIV:
10516 // Left was clobbered but a copy is in edi. Right is in ebx for
10517 // division.
10518 __ mov(edx, edi);
10519 __ mov(eax, right);
10520 break;
10521 default: UNREACHABLE();
10522 break;
10523 }
10524 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
10525 if (CpuFeatures::IsSupported(SSE2)) {
10526 CpuFeatures::Scope use_sse2(SSE2);
10527 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
10528 switch (op_) {
10529 case Token::ADD: __ addsd(xmm0, xmm1); break;
10530 case Token::SUB: __ subsd(xmm0, xmm1); break;
10531 case Token::MUL: __ mulsd(xmm0, xmm1); break;
10532 case Token::DIV: __ divsd(xmm0, xmm1); break;
10533 default: UNREACHABLE();
10534 }
10535 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
10536 } else { // SSE2 not available, use FPU.
10537 FloatingPointHelper::LoadFloatSmis(masm, ebx);
10538 switch (op_) {
10539 case Token::ADD: __ faddp(1); break;
10540 case Token::SUB: __ fsubp(1); break;
10541 case Token::MUL: __ fmulp(1); break;
10542 case Token::DIV: __ fdivp(1); break;
10543 default: UNREACHABLE();
10544 }
10545 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
10546 }
10547 __ mov(eax, ecx);
10548 GenerateReturn(masm);
10549 break;
10550 }
10551
10552 default:
10553 break;
10554 }
10555
10556 // 7. Non-smi operands, fall out to the non-smi code with the operands in
10557 // edx and eax.
10558 Comment done_comment(masm, "-- Enter non-smi code");
10559 __ bind(&not_smis);
10560 switch (op_) {
10561 case Token::BIT_OR:
10562 case Token::SHL:
10563 case Token::SAR:
10564 case Token::SHR:
10565 // Right operand is saved in ecx and eax was destroyed by the smi
10566 // check.
10567 __ mov(eax, ecx);
10568 break;
10569
10570 case Token::DIV:
10571 case Token::MOD:
10572 // Operands are in eax, ebx at this point.
10573 __ mov(edx, eax);
10574 __ mov(eax, ebx);
10575 break;
10576
10577 default:
10578 break;
10579 }
10580 }
10581
10582
10583 void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
10584 Label call_runtime;
10585
10586 __ IncrementCounter(&Counters::generic_binary_stub_calls, 1);
10587
10588 // Generate fast case smi code if requested. This flag is set when the fast
10589 // case smi code is not generated by the caller. Generating it here will speed
10590 // up common operations.
10591 if (ShouldGenerateSmiCode()) {
10592 GenerateSmiCode(masm, &call_runtime);
10593 } else if (op_ != Token::MOD) { // MOD goes straight to runtime.
10594 if (!HasArgsInRegisters()) {
10595 GenerateLoadArguments(masm);
10596 }
10597 }
10598
10599 // Floating point case.
10600 if (ShouldGenerateFPCode()) {
10601 switch (op_) {
10602 case Token::ADD:
10603 case Token::SUB:
10604 case Token::MUL:
10605 case Token::DIV: {
10606 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
10607 HasSmiCodeInStub()) {
10608 // Execution reaches this point when the first non-smi argument occurs
10609 // (and only if smi code is generated). This is the right moment to
10610 // patch to HEAP_NUMBERS state. The transition is attempted only for
10611 // the four basic operations. The stub stays in the DEFAULT state
10612 // forever for all other operations (also if smi code is skipped).
10613 GenerateTypeTransition(masm);
10614 break;
10615 }
10616
10617 Label not_floats;
10618 if (CpuFeatures::IsSupported(SSE2)) {
10619 CpuFeatures::Scope use_sse2(SSE2);
10620 if (static_operands_type_.IsNumber()) {
10621 if (FLAG_debug_code) {
10622 // Assert at runtime that inputs are only numbers.
10623 __ AbortIfNotNumber(edx);
10624 __ AbortIfNotNumber(eax);
10625 }
10626 if (static_operands_type_.IsSmi()) {
10627 if (FLAG_debug_code) {
10628 __ AbortIfNotSmi(edx);
10629 __ AbortIfNotSmi(eax);
10630 }
10631 FloatingPointHelper::LoadSSE2Smis(masm, ecx);
10632 } else {
10633 FloatingPointHelper::LoadSSE2Operands(masm);
10634 }
10635 } else {
10636 FloatingPointHelper::LoadSSE2Operands(masm, &call_runtime);
10637 }
10638
10639 switch (op_) {
10640 case Token::ADD: __ addsd(xmm0, xmm1); break;
10641 case Token::SUB: __ subsd(xmm0, xmm1); break;
10642 case Token::MUL: __ mulsd(xmm0, xmm1); break;
10643 case Token::DIV: __ divsd(xmm0, xmm1); break;
10644 default: UNREACHABLE();
10645 }
10646 GenerateHeapResultAllocation(masm, &call_runtime);
10647 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
10648 GenerateReturn(masm);
10649 } else { // SSE2 not available, use FPU.
10650 if (static_operands_type_.IsNumber()) {
10651 if (FLAG_debug_code) {
10652 // Assert at runtime that inputs are only numbers.
10653 __ AbortIfNotNumber(edx);
10654 __ AbortIfNotNumber(eax);
10655 }
10656 } else {
10657 FloatingPointHelper::CheckFloatOperands(masm, &call_runtime, ebx);
10658 }
10659 FloatingPointHelper::LoadFloatOperands(
10660 masm,
10661 ecx,
10662 FloatingPointHelper::ARGS_IN_REGISTERS);
10663 switch (op_) {
10664 case Token::ADD: __ faddp(1); break;
10665 case Token::SUB: __ fsubp(1); break;
10666 case Token::MUL: __ fmulp(1); break;
10667 case Token::DIV: __ fdivp(1); break;
10668 default: UNREACHABLE();
10669 }
10670 Label after_alloc_failure;
10671 GenerateHeapResultAllocation(masm, &after_alloc_failure);
10672 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
10673 GenerateReturn(masm);
10674 __ bind(&after_alloc_failure);
10675 __ ffree();
10676 __ jmp(&call_runtime);
10677 }
10678 __ bind(&not_floats);
10679 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
10680 !HasSmiCodeInStub()) {
10681 // Execution reaches this point when the first non-number argument
10682 // occurs (and only if smi code is skipped from the stub, otherwise
10683 // the patching has already been done earlier in this case branch).
10684 // Try patching to STRINGS for ADD operation.
10685 if (op_ == Token::ADD) {
10686 GenerateTypeTransition(masm);
10687 }
10688 }
10689 break;
10690 }
10691 case Token::MOD: {
10692 // For MOD we go directly to runtime in the non-smi case.
10693 break;
10694 }
10695 case Token::BIT_OR:
10696 case Token::BIT_AND:
10697 case Token::BIT_XOR:
10698 case Token::SAR:
10699 case Token::SHL:
10700 case Token::SHR: {
10701 Label non_smi_result;
10702 FloatingPointHelper::LoadAsIntegers(masm,
10703 static_operands_type_,
10704 use_sse3_,
10705 &call_runtime);
10706 switch (op_) {
10707 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
10708 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
10709 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
10710 case Token::SAR: __ sar_cl(eax); break;
10711 case Token::SHL: __ shl_cl(eax); break;
10712 case Token::SHR: __ shr_cl(eax); break;
10713 default: UNREACHABLE();
10714 }
10715 if (op_ == Token::SHR) {
10716 // Check if result is non-negative and fits in a smi.
10717 __ test(eax, Immediate(0xc0000000));
10718 __ j(not_zero, &call_runtime);
10719 } else {
10720 // Check if result fits in a smi.
10721 __ cmp(eax, 0xc0000000);
10722 __ j(negative, &non_smi_result);
10723 }
10724 // Tag smi result and return.
10725 __ SmiTag(eax);
10726 GenerateReturn(masm);
10727
10728 // All ops except SHR return a signed int32 that we load in
10729 // a HeapNumber.
10730 if (op_ != Token::SHR) {
10731 __ bind(&non_smi_result);
10732 // Allocate a heap number if needed.
10733 __ mov(ebx, Operand(eax)); // ebx: result
10734 Label skip_allocation;
10735 switch (mode_) {
10736 case OVERWRITE_LEFT:
10737 case OVERWRITE_RIGHT:
10738 // If the operand was an object, we skip the
10739 // allocation of a heap number.
10740 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
10741 1 * kPointerSize : 2 * kPointerSize));
10742 __ test(eax, Immediate(kSmiTagMask));
10743 __ j(not_zero, &skip_allocation, not_taken);
10744 // Fall through!
10745 case NO_OVERWRITE:
10746 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
10747 __ bind(&skip_allocation);
10748 break;
10749 default: UNREACHABLE();
10750 }
10751 // Store the result in the HeapNumber and return.
10752 if (CpuFeatures::IsSupported(SSE2)) {
10753 CpuFeatures::Scope use_sse2(SSE2);
10754 __ cvtsi2sd(xmm0, Operand(ebx));
10755 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
10756 } else {
10757 __ mov(Operand(esp, 1 * kPointerSize), ebx);
10758 __ fild_s(Operand(esp, 1 * kPointerSize));
10759 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
10760 }
10761 GenerateReturn(masm);
10762 }
10763 break;
10764 }
10765 default: UNREACHABLE(); break;
10766 }
10767 }
10768
10769 // If all else fails, use the runtime system to get the correct
10770 // result. If arguments was passed in registers now place them on the
10771 // stack in the correct order below the return address.
10772 __ bind(&call_runtime);
10773 if (HasArgsInRegisters()) {
10774 GenerateRegisterArgsPush(masm);
10775 }
10776
10777 switch (op_) {
10778 case Token::ADD: {
10779 // Test for string arguments before calling runtime.
10780 Label not_strings, not_string1, string1, string1_smi2;
10781
10782 // If this stub has already generated FP-specific code then the arguments
10783 // are already in edx, eax
10784 if (!ShouldGenerateFPCode() && !HasArgsInRegisters()) {
10785 GenerateLoadArguments(masm);
10786 }
10787
10788 // Registers containing left and right operands respectively.
10789 Register lhs, rhs;
10790 if (HasArgsReversed()) {
10791 lhs = eax;
10792 rhs = edx;
10793 } else {
10794 lhs = edx;
10795 rhs = eax;
10796 }
10797
10798 // Test if first argument is a string.
10799 __ test(lhs, Immediate(kSmiTagMask));
10800 __ j(zero, &not_string1);
10801 __ CmpObjectType(lhs, FIRST_NONSTRING_TYPE, ecx);
10802 __ j(above_equal, &not_string1);
10803
10804 // First argument is a string, test second.
10805 __ test(rhs, Immediate(kSmiTagMask));
10806 __ j(zero, &string1_smi2);
10807 __ CmpObjectType(rhs, FIRST_NONSTRING_TYPE, ecx);
10808 __ j(above_equal, &string1);
10809
10810 // First and second argument are strings. Jump to the string add stub.
10811 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
10812 __ TailCallStub(&string_add_stub);
10813
10814 __ bind(&string1_smi2);
10815 // First argument is a string, second is a smi. Try to lookup the number
10816 // string for the smi in the number string cache.
10817 NumberToStringStub::GenerateLookupNumberStringCache(
10818 masm, rhs, edi, ebx, ecx, true, &string1);
10819
10820 // Replace second argument on stack and tailcall string add stub to make
10821 // the result.
10822 __ mov(Operand(esp, 1 * kPointerSize), edi);
10823 __ TailCallStub(&string_add_stub);
10824
10825 // Only first argument is a string.
10826 __ bind(&string1);
10827 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_FUNCTION);
10828
10829 // First argument was not a string, test second.
10830 __ bind(&not_string1);
10831 __ test(rhs, Immediate(kSmiTagMask));
10832 __ j(zero, &not_strings);
10833 __ CmpObjectType(rhs, FIRST_NONSTRING_TYPE, ecx);
10834 __ j(above_equal, &not_strings);
10835
10836 // Only second argument is a string.
10837 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_FUNCTION);
10838
10839 __ bind(&not_strings);
10840 // Neither argument is a string.
10841 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
10842 break;
10843 }
10844 case Token::SUB:
10845 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
10846 break;
10847 case Token::MUL:
10848 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
10849 break;
10850 case Token::DIV:
10851 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
10852 break;
10853 case Token::MOD:
10854 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
10855 break;
10856 case Token::BIT_OR:
10857 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
10858 break;
10859 case Token::BIT_AND:
10860 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
10861 break;
10862 case Token::BIT_XOR:
10863 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
10864 break;
10865 case Token::SAR:
10866 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
10867 break;
10868 case Token::SHL:
10869 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
10870 break;
10871 case Token::SHR:
10872 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
10873 break;
10874 default:
10875 UNREACHABLE();
10876 }
10877 }
10878
10879
10880 void GenericBinaryOpStub::GenerateHeapResultAllocation(MacroAssembler* masm,
10881 Label* alloc_failure) {
10882 Label skip_allocation;
10883 OverwriteMode mode = mode_;
10884 if (HasArgsReversed()) {
10885 if (mode == OVERWRITE_RIGHT) {
10886 mode = OVERWRITE_LEFT;
10887 } else if (mode == OVERWRITE_LEFT) {
10888 mode = OVERWRITE_RIGHT;
10889 }
10890 }
10891 switch (mode) {
10892 case OVERWRITE_LEFT: {
10893 // If the argument in edx is already an object, we skip the
10894 // allocation of a heap number.
10895 __ test(edx, Immediate(kSmiTagMask));
10896 __ j(not_zero, &skip_allocation, not_taken);
10897 // Allocate a heap number for the result. Keep eax and edx intact
10898 // for the possible runtime call.
10899 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
10900 // Now edx can be overwritten losing one of the arguments as we are
10901 // now done and will not need it any more.
10902 __ mov(edx, Operand(ebx));
10903 __ bind(&skip_allocation);
10904 // Use object in edx as a result holder
10905 __ mov(eax, Operand(edx));
10906 break;
10907 }
10908 case OVERWRITE_RIGHT:
10909 // If the argument in eax is already an object, we skip the
10910 // allocation of a heap number.
10911 __ test(eax, Immediate(kSmiTagMask));
10912 __ j(not_zero, &skip_allocation, not_taken);
10913 // Fall through!
10914 case NO_OVERWRITE:
10915 // Allocate a heap number for the result. Keep eax and edx intact
10916 // for the possible runtime call.
10917 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
10918 // Now eax can be overwritten losing one of the arguments as we are
10919 // now done and will not need it any more.
10920 __ mov(eax, ebx);
10921 __ bind(&skip_allocation);
10922 break;
10923 default: UNREACHABLE();
10924 }
10925 }
10926
10927
10928 void GenericBinaryOpStub::GenerateLoadArguments(MacroAssembler* masm) {
10929 // If arguments are not passed in registers read them from the stack.
10930 ASSERT(!HasArgsInRegisters());
10931 __ mov(eax, Operand(esp, 1 * kPointerSize));
10932 __ mov(edx, Operand(esp, 2 * kPointerSize));
10933 }
10934
10935
10936 void GenericBinaryOpStub::GenerateReturn(MacroAssembler* masm) {
10937 // If arguments are not passed in registers remove them from the stack before
10938 // returning.
10939 if (!HasArgsInRegisters()) {
10940 __ ret(2 * kPointerSize); // Remove both operands
10941 } else {
10942 __ ret(0);
10943 }
10944 }
10945
10946
10947 void GenericBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
10948 ASSERT(HasArgsInRegisters());
10949 __ pop(ecx);
10950 if (HasArgsReversed()) {
10951 __ push(eax);
10952 __ push(edx);
10953 } else {
10954 __ push(edx);
10955 __ push(eax);
10956 }
10957 __ push(ecx);
10958 }
10959
10960
10961 void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
10962 // Ensure the operands are on the stack.
10963 if (HasArgsInRegisters()) {
10964 GenerateRegisterArgsPush(masm);
10965 }
10966
10967 __ pop(ecx); // Save return address.
10968
10969 // Left and right arguments are now on top.
10970 // Push this stub's key. Although the operation and the type info are
10971 // encoded into the key, the encoding is opaque, so push them too.
10972 __ push(Immediate(Smi::FromInt(MinorKey())));
10973 __ push(Immediate(Smi::FromInt(op_)));
10974 __ push(Immediate(Smi::FromInt(runtime_operands_type_)));
10975
10976 __ push(ecx); // Push return address.
10977
10978 // Patch the caller to an appropriate specialized stub and return the
10979 // operation result to the caller of the stub.
10980 __ TailCallExternalReference(
10981 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
10982 5,
10983 1);
10984 }
10985
10986
10987 Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
10988 GenericBinaryOpStub stub(key, type_info);
10989 return stub.GetCode();
10990 }
10991
10992
10993 void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
10994 // Input on stack:
10995 // esp[4]: argument (should be number).
10996 // esp[0]: return address.
10997 // Test that eax is a number.
10998 Label runtime_call;
10999 Label runtime_call_clear_stack;
11000 Label input_not_smi;
11001 Label loaded;
11002 __ mov(eax, Operand(esp, kPointerSize));
11003 __ test(eax, Immediate(kSmiTagMask));
11004 __ j(not_zero, &input_not_smi);
11005 // Input is a smi. Untag and load it onto the FPU stack.
11006 // Then load the low and high words of the double into ebx, edx.
11007 STATIC_ASSERT(kSmiTagSize == 1);
11008 __ sar(eax, 1);
11009 __ sub(Operand(esp), Immediate(2 * kPointerSize));
11010 __ mov(Operand(esp, 0), eax);
11011 __ fild_s(Operand(esp, 0));
11012 __ fst_d(Operand(esp, 0));
11013 __ pop(edx);
11014 __ pop(ebx);
11015 __ jmp(&loaded);
11016 __ bind(&input_not_smi);
11017 // Check if input is a HeapNumber.
11018 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
11019 __ cmp(Operand(ebx), Immediate(Factory::heap_number_map()));
11020 __ j(not_equal, &runtime_call);
11021 // Input is a HeapNumber. Push it on the FPU stack and load its
11022 // low and high words into ebx, edx.
11023 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
11024 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
11025 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
11026
11027 __ bind(&loaded);
11028 // ST[0] == double value
11029 // ebx = low 32 bits of double value
11030 // edx = high 32 bits of double value
11031 // Compute hash (the shifts are arithmetic):
11032 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
11033 __ mov(ecx, ebx);
11034 __ xor_(ecx, Operand(edx));
11035 __ mov(eax, ecx);
11036 __ sar(eax, 16);
11037 __ xor_(ecx, Operand(eax));
11038 __ mov(eax, ecx);
11039 __ sar(eax, 8);
11040 __ xor_(ecx, Operand(eax));
11041 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
11042 __ and_(Operand(ecx), Immediate(TranscendentalCache::kCacheSize - 1));
11043
11044 // ST[0] == double value.
11045 // ebx = low 32 bits of double value.
11046 // edx = high 32 bits of double value.
11047 // ecx = TranscendentalCache::hash(double value).
11048 __ mov(eax,
11049 Immediate(ExternalReference::transcendental_cache_array_address()));
11050 // Eax points to cache array.
11051 __ mov(eax, Operand(eax, type_ * sizeof(TranscendentalCache::caches_[0])));
11052 // Eax points to the cache for the type type_.
11053 // If NULL, the cache hasn't been initialized yet, so go through runtime.
11054 __ test(eax, Operand(eax));
11055 __ j(zero, &runtime_call_clear_stack);
11056 #ifdef DEBUG
11057 // Check that the layout of cache elements match expectations.
11058 { TranscendentalCache::Element test_elem[2];
11059 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
11060 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
11061 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
11062 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
11063 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
11064 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
11065 CHECK_EQ(0, elem_in0 - elem_start);
11066 CHECK_EQ(kIntSize, elem_in1 - elem_start);
11067 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
11068 }
11069 #endif
11070 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
11071 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
11072 __ lea(ecx, Operand(eax, ecx, times_4, 0));
11073 // Check if cache matches: Double value is stored in uint32_t[2] array.
11074 Label cache_miss;
11075 __ cmp(ebx, Operand(ecx, 0));
11076 __ j(not_equal, &cache_miss);
11077 __ cmp(edx, Operand(ecx, kIntSize));
11078 __ j(not_equal, &cache_miss);
11079 // Cache hit!
11080 __ mov(eax, Operand(ecx, 2 * kIntSize));
11081 __ fstp(0);
11082 __ ret(kPointerSize);
11083
11084 __ bind(&cache_miss);
11085 // Update cache with new value.
11086 // We are short on registers, so use no_reg as scratch.
11087 // This gives slightly larger code.
11088 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
11089 GenerateOperation(masm);
11090 __ mov(Operand(ecx, 0), ebx);
11091 __ mov(Operand(ecx, kIntSize), edx);
11092 __ mov(Operand(ecx, 2 * kIntSize), eax);
11093 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
11094 __ ret(kPointerSize);
11095
11096 __ bind(&runtime_call_clear_stack);
11097 __ fstp(0);
11098 __ bind(&runtime_call);
11099 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
11100 }
11101
11102
11103 Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
11104 switch (type_) {
11105 // Add more cases when necessary.
11106 case TranscendentalCache::SIN: return Runtime::kMath_sin;
11107 case TranscendentalCache::COS: return Runtime::kMath_cos;
11108 default:
11109 UNIMPLEMENTED();
11110 return Runtime::kAbort;
11111 }
11112 }
11113
11114
11115 void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
11116 // Only free register is edi.
11117 Label done;
11118 ASSERT(type_ == TranscendentalCache::SIN ||
11119 type_ == TranscendentalCache::COS);
11120 // More transcendental types can be added later.
11121
11122 // Both fsin and fcos require arguments in the range +/-2^63 and
11123 // return NaN for infinities and NaN. They can share all code except
11124 // the actual fsin/fcos operation.
11125 Label in_range;
11126 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
11127 // work. We must reduce it to the appropriate range.
11128 __ mov(edi, edx);
11129 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
11130 int supported_exponent_limit =
11131 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
11132 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
11133 __ j(below, &in_range, taken);
11134 // Check for infinity and NaN. Both return NaN for sin.
11135 __ cmp(Operand(edi), Immediate(0x7ff00000));
11136 Label non_nan_result;
11137 __ j(not_equal, &non_nan_result, taken);
11138 // Input is +/-Infinity or NaN. Result is NaN.
11139 __ fstp(0);
11140 // NaN is represented by 0x7ff8000000000000.
11141 __ push(Immediate(0x7ff80000));
11142 __ push(Immediate(0));
11143 __ fld_d(Operand(esp, 0));
11144 __ add(Operand(esp), Immediate(2 * kPointerSize));
11145 __ jmp(&done);
11146
11147 __ bind(&non_nan_result);
11148
11149 // Use fpmod to restrict argument to the range +/-2*PI.
11150 __ mov(edi, eax); // Save eax before using fnstsw_ax.
11151 __ fldpi();
11152 __ fadd(0);
11153 __ fld(1);
11154 // FPU Stack: input, 2*pi, input.
11155 {
11156 Label no_exceptions;
11157 __ fwait();
11158 __ fnstsw_ax();
11159 // Clear if Illegal Operand or Zero Division exceptions are set.
11160 __ test(Operand(eax), Immediate(5));
11161 __ j(zero, &no_exceptions);
11162 __ fnclex();
11163 __ bind(&no_exceptions);
11164 }
11165
11166 // Compute st(0) % st(1)
11167 {
11168 Label partial_remainder_loop;
11169 __ bind(&partial_remainder_loop);
11170 __ fprem1();
11171 __ fwait();
11172 __ fnstsw_ax();
11173 __ test(Operand(eax), Immediate(0x400 /* C2 */));
11174 // If C2 is set, computation only has partial result. Loop to
11175 // continue computation.
11176 __ j(not_zero, &partial_remainder_loop);
11177 }
11178 // FPU Stack: input, 2*pi, input % 2*pi
11179 __ fstp(2);
11180 __ fstp(0);
11181 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
11182
11183 // FPU Stack: input % 2*pi
11184 __ bind(&in_range);
11185 switch (type_) {
11186 case TranscendentalCache::SIN:
11187 __ fsin();
11188 break;
11189 case TranscendentalCache::COS:
11190 __ fcos();
11191 break;
11192 default:
11193 UNREACHABLE();
11194 }
11195 __ bind(&done);
11196 }
11197
11198
11199 // Get the integer part of a heap number. Surprisingly, all this bit twiddling
11200 // is faster than using the built-in instructions on floating point registers.
11201 // Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
11202 // trashed registers.
11203 void IntegerConvert(MacroAssembler* masm,
11204 Register source,
11205 TypeInfo type_info,
11206 bool use_sse3,
11207 Label* conversion_failure) {
11208 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
11209 Label done, right_exponent, normal_exponent;
11210 Register scratch = ebx;
11211 Register scratch2 = edi;
11212 if (type_info.IsInteger32() && CpuFeatures::IsEnabled(SSE2)) {
11213 CpuFeatures::Scope scope(SSE2);
11214 __ cvttsd2si(ecx, FieldOperand(source, HeapNumber::kValueOffset));
11215 return;
11216 }
11217 if (!type_info.IsInteger32() || !use_sse3) {
11218 // Get exponent word.
11219 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
11220 // Get exponent alone in scratch2.
11221 __ mov(scratch2, scratch);
11222 __ and_(scratch2, HeapNumber::kExponentMask);
11223 }
11224 if (use_sse3) {
11225 CpuFeatures::Scope scope(SSE3);
11226 if (!type_info.IsInteger32()) {
11227 // Check whether the exponent is too big for a 64 bit signed integer.
11228 static const uint32_t kTooBigExponent =
11229 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
11230 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
11231 __ j(greater_equal, conversion_failure);
11232 }
11233 // Load x87 register with heap number.
11234 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
11235 // Reserve space for 64 bit answer.
11236 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
11237 // Do conversion, which cannot fail because we checked the exponent.
11238 __ fisttp_d(Operand(esp, 0));
11239 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
11240 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
11241 } else {
11242 // Load ecx with zero. We use this either for the final shift or
11243 // for the answer.
11244 __ xor_(ecx, Operand(ecx));
11245 // Check whether the exponent matches a 32 bit signed int that cannot be
11246 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
11247 // exponent is 30 (biased). This is the exponent that we are fastest at and
11248 // also the highest exponent we can handle here.
11249 const uint32_t non_smi_exponent =
11250 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
11251 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
11252 // If we have a match of the int32-but-not-Smi exponent then skip some
11253 // logic.
11254 __ j(equal, &right_exponent);
11255 // If the exponent is higher than that then go to slow case. This catches
11256 // numbers that don't fit in a signed int32, infinities and NaNs.
11257 __ j(less, &normal_exponent);
11258
11259 {
11260 // Handle a big exponent. The only reason we have this code is that the
11261 // >>> operator has a tendency to generate numbers with an exponent of 31.
11262 const uint32_t big_non_smi_exponent =
11263 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
11264 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
11265 __ j(not_equal, conversion_failure);
11266 // We have the big exponent, typically from >>>. This means the number is
11267 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
11268 __ mov(scratch2, scratch);
11269 __ and_(scratch2, HeapNumber::kMantissaMask);
11270 // Put back the implicit 1.
11271 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
11272 // Shift up the mantissa bits to take up the space the exponent used to
11273 // take. We just orred in the implicit bit so that took care of one and
11274 // we want to use the full unsigned range so we subtract 1 bit from the
11275 // shift distance.
11276 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
11277 __ shl(scratch2, big_shift_distance);
11278 // Get the second half of the double.
11279 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
11280 // Shift down 21 bits to get the most significant 11 bits or the low
11281 // mantissa word.
11282 __ shr(ecx, 32 - big_shift_distance);
11283 __ or_(ecx, Operand(scratch2));
11284 // We have the answer in ecx, but we may need to negate it.
11285 __ test(scratch, Operand(scratch));
11286 __ j(positive, &done);
11287 __ neg(ecx);
11288 __ jmp(&done);
11289 }
11290
11291 __ bind(&normal_exponent);
11292 // Exponent word in scratch, exponent part of exponent word in scratch2.
11293 // Zero in ecx.
11294 // We know the exponent is smaller than 30 (biased). If it is less than
11295 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
11296 // it rounds to zero.
11297 const uint32_t zero_exponent =
11298 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
11299 __ sub(Operand(scratch2), Immediate(zero_exponent));
11300 // ecx already has a Smi zero.
11301 __ j(less, &done);
11302
11303 // We have a shifted exponent between 0 and 30 in scratch2.
11304 __ shr(scratch2, HeapNumber::kExponentShift);
11305 __ mov(ecx, Immediate(30));
11306 __ sub(ecx, Operand(scratch2));
11307
11308 __ bind(&right_exponent);
11309 // Here ecx is the shift, scratch is the exponent word.
11310 // Get the top bits of the mantissa.
11311 __ and_(scratch, HeapNumber::kMantissaMask);
11312 // Put back the implicit 1.
11313 __ or_(scratch, 1 << HeapNumber::kExponentShift);
11314 // Shift up the mantissa bits to take up the space the exponent used to
11315 // take. We have kExponentShift + 1 significant bits int he low end of the
11316 // word. Shift them to the top bits.
11317 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
11318 __ shl(scratch, shift_distance);
11319 // Get the second half of the double. For some exponents we don't
11320 // actually need this because the bits get shifted out again, but
11321 // it's probably slower to test than just to do it.
11322 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
11323 // Shift down 22 bits to get the most significant 10 bits or the low
11324 // mantissa word.
11325 __ shr(scratch2, 32 - shift_distance);
11326 __ or_(scratch2, Operand(scratch));
11327 // Move down according to the exponent.
11328 __ shr_cl(scratch2);
11329 // Now the unsigned answer is in scratch2. We need to move it to ecx and
11330 // we may need to fix the sign.
11331 Label negative;
11332 __ xor_(ecx, Operand(ecx));
11333 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
11334 __ j(greater, &negative);
11335 __ mov(ecx, scratch2);
11336 __ jmp(&done);
11337 __ bind(&negative);
11338 __ sub(ecx, Operand(scratch2));
11339 __ bind(&done);
11340 }
11341 }
11342
11343
11344 // Input: edx, eax are the left and right objects of a bit op.
11345 // Output: eax, ecx are left and right integers for a bit op.
11346 void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm,
11347 TypeInfo type_info,
11348 bool use_sse3,
11349 Label* conversion_failure) {
11350 // Check float operands.
11351 Label arg1_is_object, check_undefined_arg1;
11352 Label arg2_is_object, check_undefined_arg2;
11353 Label load_arg2, done;
11354
11355 if (!type_info.IsDouble()) {
11356 if (!type_info.IsSmi()) {
11357 __ test(edx, Immediate(kSmiTagMask));
11358 __ j(not_zero, &arg1_is_object);
11359 } else {
11360 if (FLAG_debug_code) __ AbortIfNotSmi(edx);
11361 }
11362 __ SmiUntag(edx);
11363 __ jmp(&load_arg2);
11364 }
11365
11366 __ bind(&arg1_is_object);
11367
11368 // Get the untagged integer version of the edx heap number in ecx.
11369 IntegerConvert(masm, edx, type_info, use_sse3, conversion_failure);
11370 __ mov(edx, ecx);
11371
11372 // Here edx has the untagged integer, eax has a Smi or a heap number.
11373 __ bind(&load_arg2);
11374 if (!type_info.IsDouble()) {
11375 // Test if arg2 is a Smi.
11376 if (!type_info.IsSmi()) {
11377 __ test(eax, Immediate(kSmiTagMask));
11378 __ j(not_zero, &arg2_is_object);
11379 } else {
11380 if (FLAG_debug_code) __ AbortIfNotSmi(eax);
11381 }
11382 __ SmiUntag(eax);
11383 __ mov(ecx, eax);
11384 __ jmp(&done);
11385 }
11386
11387 __ bind(&arg2_is_object);
11388
11389 // Get the untagged integer version of the eax heap number in ecx.
11390 IntegerConvert(masm, eax, type_info, use_sse3, conversion_failure);
11391 __ bind(&done);
11392 __ mov(eax, edx);
11393 }
11394
11395
11396 // Input: edx, eax are the left and right objects of a bit op.
11397 // Output: eax, ecx are left and right integers for a bit op.
11398 void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
11399 bool use_sse3,
11400 Label* conversion_failure) {
11401 // Check float operands.
11402 Label arg1_is_object, check_undefined_arg1;
11403 Label arg2_is_object, check_undefined_arg2;
11404 Label load_arg2, done;
11405
11406 // Test if arg1 is a Smi.
11407 __ test(edx, Immediate(kSmiTagMask));
11408 __ j(not_zero, &arg1_is_object);
11409
11410 __ SmiUntag(edx);
11411 __ jmp(&load_arg2);
11412
11413 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
11414 __ bind(&check_undefined_arg1);
11415 __ cmp(edx, Factory::undefined_value());
11416 __ j(not_equal, conversion_failure);
11417 __ mov(edx, Immediate(0));
11418 __ jmp(&load_arg2);
11419
11420 __ bind(&arg1_is_object);
11421 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
11422 __ cmp(ebx, Factory::heap_number_map());
11423 __ j(not_equal, &check_undefined_arg1);
11424
11425 // Get the untagged integer version of the edx heap number in ecx.
11426 IntegerConvert(masm,
11427 edx,
11428 TypeInfo::Unknown(),
11429 use_sse3,
11430 conversion_failure);
11431 __ mov(edx, ecx);
11432
11433 // Here edx has the untagged integer, eax has a Smi or a heap number.
11434 __ bind(&load_arg2);
11435
11436 // Test if arg2 is a Smi.
11437 __ test(eax, Immediate(kSmiTagMask));
11438 __ j(not_zero, &arg2_is_object);
11439
11440 __ SmiUntag(eax);
11441 __ mov(ecx, eax);
11442 __ jmp(&done);
11443
11444 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
11445 __ bind(&check_undefined_arg2);
11446 __ cmp(eax, Factory::undefined_value());
11447 __ j(not_equal, conversion_failure);
11448 __ mov(ecx, Immediate(0));
11449 __ jmp(&done);
11450
11451 __ bind(&arg2_is_object);
11452 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
11453 __ cmp(ebx, Factory::heap_number_map());
11454 __ j(not_equal, &check_undefined_arg2);
11455
11456 // Get the untagged integer version of the eax heap number in ecx.
11457 IntegerConvert(masm,
11458 eax,
11459 TypeInfo::Unknown(),
11460 use_sse3,
11461 conversion_failure);
11462 __ bind(&done);
11463 __ mov(eax, edx);
11464 }
11465
11466
11467 void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
11468 TypeInfo type_info,
11469 bool use_sse3,
11470 Label* conversion_failure) {
11471 if (type_info.IsNumber()) {
11472 LoadNumbersAsIntegers(masm, type_info, use_sse3, conversion_failure);
11473 } else {
11474 LoadUnknownsAsIntegers(masm, use_sse3, conversion_failure);
11475 }
11476 }
11477
11478
11479 void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
11480 Register number) {
11481 Label load_smi, done;
11482
11483 __ test(number, Immediate(kSmiTagMask));
11484 __ j(zero, &load_smi, not_taken);
11485 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
11486 __ jmp(&done);
11487
11488 __ bind(&load_smi);
11489 __ SmiUntag(number);
11490 __ push(number);
11491 __ fild_s(Operand(esp, 0));
11492 __ pop(number);
11493
11494 __ bind(&done);
11495 }
11496
11497
11498 void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
11499 Label load_smi_edx, load_eax, load_smi_eax, done;
11500 // Load operand in edx into xmm0.
11501 __ test(edx, Immediate(kSmiTagMask));
11502 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
11503 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
11504
11505 __ bind(&load_eax);
11506 // Load operand in eax into xmm1.
11507 __ test(eax, Immediate(kSmiTagMask));
11508 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
11509 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
11510 __ jmp(&done);
11511
11512 __ bind(&load_smi_edx);
11513 __ SmiUntag(edx); // Untag smi before converting to float.
11514 __ cvtsi2sd(xmm0, Operand(edx));
11515 __ SmiTag(edx); // Retag smi for heap number overwriting test.
11516 __ jmp(&load_eax);
11517
11518 __ bind(&load_smi_eax);
11519 __ SmiUntag(eax); // Untag smi before converting to float.
11520 __ cvtsi2sd(xmm1, Operand(eax));
11521 __ SmiTag(eax); // Retag smi for heap number overwriting test.
11522
11523 __ bind(&done);
11524 }
11525
11526
11527 void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
11528 Label* not_numbers) {
11529 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
11530 // Load operand in edx into xmm0, or branch to not_numbers.
11531 __ test(edx, Immediate(kSmiTagMask));
11532 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
11533 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), Factory::heap_number_map());
11534 __ j(not_equal, not_numbers); // Argument in edx is not a number.
11535 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
11536 __ bind(&load_eax);
11537 // Load operand in eax into xmm1, or branch to not_numbers.
11538 __ test(eax, Immediate(kSmiTagMask));
11539 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
11540 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), Factory::heap_number_map());
11541 __ j(equal, &load_float_eax);
11542 __ jmp(not_numbers); // Argument in eax is not a number.
11543 __ bind(&load_smi_edx);
11544 __ SmiUntag(edx); // Untag smi before converting to float.
11545 __ cvtsi2sd(xmm0, Operand(edx));
11546 __ SmiTag(edx); // Retag smi for heap number overwriting test.
11547 __ jmp(&load_eax);
11548 __ bind(&load_smi_eax);
11549 __ SmiUntag(eax); // Untag smi before converting to float.
11550 __ cvtsi2sd(xmm1, Operand(eax));
11551 __ SmiTag(eax); // Retag smi for heap number overwriting test.
11552 __ jmp(&done);
11553 __ bind(&load_float_eax);
11554 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
11555 __ bind(&done);
11556 }
11557
11558
11559 void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
11560 Register scratch) {
11561 const Register left = edx;
11562 const Register right = eax;
11563 __ mov(scratch, left);
11564 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
11565 __ SmiUntag(scratch);
11566 __ cvtsi2sd(xmm0, Operand(scratch));
11567
11568 __ mov(scratch, right);
11569 __ SmiUntag(scratch);
11570 __ cvtsi2sd(xmm1, Operand(scratch));
11571 }
11572
11573
11574 void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
11575 Register scratch,
11576 ArgLocation arg_location) {
11577 Label load_smi_1, load_smi_2, done_load_1, done;
11578 if (arg_location == ARGS_IN_REGISTERS) {
11579 __ mov(scratch, edx);
11580 } else {
11581 __ mov(scratch, Operand(esp, 2 * kPointerSize));
11582 }
11583 __ test(scratch, Immediate(kSmiTagMask));
11584 __ j(zero, &load_smi_1, not_taken);
11585 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
11586 __ bind(&done_load_1);
11587
11588 if (arg_location == ARGS_IN_REGISTERS) {
11589 __ mov(scratch, eax);
11590 } else {
11591 __ mov(scratch, Operand(esp, 1 * kPointerSize));
11592 }
11593 __ test(scratch, Immediate(kSmiTagMask));
11594 __ j(zero, &load_smi_2, not_taken);
11595 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
11596 __ jmp(&done);
11597
11598 __ bind(&load_smi_1);
11599 __ SmiUntag(scratch);
11600 __ push(scratch);
11601 __ fild_s(Operand(esp, 0));
11602 __ pop(scratch);
11603 __ jmp(&done_load_1);
11604
11605 __ bind(&load_smi_2);
11606 __ SmiUntag(scratch);
11607 __ push(scratch);
11608 __ fild_s(Operand(esp, 0));
11609 __ pop(scratch);
11610
11611 __ bind(&done);
11612 }
11613
11614
11615 void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
11616 Register scratch) {
11617 const Register left = edx;
11618 const Register right = eax;
11619 __ mov(scratch, left);
11620 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
11621 __ SmiUntag(scratch);
11622 __ push(scratch);
11623 __ fild_s(Operand(esp, 0));
11624
11625 __ mov(scratch, right);
11626 __ SmiUntag(scratch);
11627 __ mov(Operand(esp, 0), scratch);
11628 __ fild_s(Operand(esp, 0));
11629 __ pop(scratch);
11630 }
11631
11632
11633 void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
11634 Label* non_float,
11635 Register scratch) {
11636 Label test_other, done;
11637 // Test if both operands are floats or smi -> scratch=k_is_float;
11638 // Otherwise scratch = k_not_float.
11639 __ test(edx, Immediate(kSmiTagMask));
11640 __ j(zero, &test_other, not_taken); // argument in edx is OK
11641 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
11642 __ cmp(scratch, Factory::heap_number_map());
11643 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
11644
11645 __ bind(&test_other);
11646 __ test(eax, Immediate(kSmiTagMask));
11647 __ j(zero, &done); // argument in eax is OK
11648 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
11649 __ cmp(scratch, Factory::heap_number_map());
11650 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
11651
11652 // Fall-through: Both operands are numbers.
11653 __ bind(&done);
11654 }
11655
11656
11657 void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
11658 Label slow, done;
11659
11660 if (op_ == Token::SUB) {
11661 // Check whether the value is a smi.
11662 Label try_float;
11663 __ test(eax, Immediate(kSmiTagMask));
11664 __ j(not_zero, &try_float, not_taken);
11665
11666 if (negative_zero_ == kStrictNegativeZero) {
11667 // Go slow case if the value of the expression is zero
11668 // to make sure that we switch between 0 and -0.
11669 __ test(eax, Operand(eax));
11670 __ j(zero, &slow, not_taken);
11671 }
11672
11673 // The value of the expression is a smi that is not zero. Try
11674 // optimistic subtraction '0 - value'.
11675 Label undo;
11676 __ mov(edx, Operand(eax));
11677 __ Set(eax, Immediate(0));
11678 __ sub(eax, Operand(edx));
11679 __ j(no_overflow, &done, taken);
11680
11681 // Restore eax and go slow case.
11682 __ bind(&undo);
11683 __ mov(eax, Operand(edx));
11684 __ jmp(&slow);
11685
11686 // Try floating point case.
11687 __ bind(&try_float);
11688 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
11689 __ cmp(edx, Factory::heap_number_map());
11690 __ j(not_equal, &slow);
11691 if (overwrite_ == UNARY_OVERWRITE) {
11692 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
11693 __ xor_(edx, HeapNumber::kSignMask); // Flip sign.
11694 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), edx);
11695 } else {
11696 __ mov(edx, Operand(eax));
11697 // edx: operand
11698 __ AllocateHeapNumber(eax, ebx, ecx, &undo);
11699 // eax: allocated 'empty' number
11700 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
11701 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
11702 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
11703 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
11704 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
11705 }
11706 } else if (op_ == Token::BIT_NOT) {
11707 // Check if the operand is a heap number.
11708 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
11709 __ cmp(edx, Factory::heap_number_map());
11710 __ j(not_equal, &slow, not_taken);
11711
11712 // Convert the heap number in eax to an untagged integer in ecx.
11713 IntegerConvert(masm,
11714 eax,
11715 TypeInfo::Unknown(),
11716 CpuFeatures::IsSupported(SSE3),
11717 &slow);
11718
11719 // Do the bitwise operation and check if the result fits in a smi.
11720 Label try_float;
11721 __ not_(ecx);
11722 __ cmp(ecx, 0xc0000000);
11723 __ j(sign, &try_float, not_taken);
11724
11725 // Tag the result as a smi and we're done.
11726 STATIC_ASSERT(kSmiTagSize == 1);
11727 __ lea(eax, Operand(ecx, times_2, kSmiTag));
11728 __ jmp(&done);
11729
11730 // Try to store the result in a heap number.
11731 __ bind(&try_float);
11732 if (overwrite_ == UNARY_NO_OVERWRITE) {
11733 // Allocate a fresh heap number, but don't overwrite eax until
11734 // we're sure we can do it without going through the slow case
11735 // that needs the value in eax.
11736 __ AllocateHeapNumber(ebx, edx, edi, &slow);
11737 __ mov(eax, Operand(ebx));
11738 }
11739 if (CpuFeatures::IsSupported(SSE2)) {
11740 CpuFeatures::Scope use_sse2(SSE2);
11741 __ cvtsi2sd(xmm0, Operand(ecx));
11742 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
11743 } else {
11744 __ push(ecx);
11745 __ fild_s(Operand(esp, 0));
11746 __ pop(ecx);
11747 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
11748 }
11749 } else {
11750 UNIMPLEMENTED();
11751 }
11752
11753 // Return from the stub.
11754 __ bind(&done);
11755 __ StubReturn(1);
11756
11757 // Handle the slow case by jumping to the JavaScript builtin.
11758 __ bind(&slow);
11759 __ pop(ecx); // pop return address.
11760 __ push(eax);
11761 __ push(ecx); // push return address
11762 switch (op_) {
11763 case Token::SUB:
11764 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
11765 break;
11766 case Token::BIT_NOT:
11767 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
11768 break;
11769 default:
11770 UNREACHABLE();
11771 }
11772 }
11773
11774
11775 void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
11776 // The key is in edx and the parameter count is in eax.
11777
11778 // The displacement is used for skipping the frame pointer on the
11779 // stack. It is the offset of the last parameter (if any) relative
11780 // to the frame pointer.
11781 static const int kDisplacement = 1 * kPointerSize;
11782
11783 // Check that the key is a smi.
11784 Label slow;
11785 __ test(edx, Immediate(kSmiTagMask));
11786 __ j(not_zero, &slow, not_taken);
11787
11788 // Check if the calling frame is an arguments adaptor frame.
11789 Label adaptor;
11790 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
11791 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
11792 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
11793 __ j(equal, &adaptor);
11794
11795 // Check index against formal parameters count limit passed in
11796 // through register eax. Use unsigned comparison to get negative
11797 // check for free.
11798 __ cmp(edx, Operand(eax));
11799 __ j(above_equal, &slow, not_taken);
11800
11801 // Read the argument from the stack and return it.
11802 STATIC_ASSERT(kSmiTagSize == 1);
11803 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
11804 __ lea(ebx, Operand(ebp, eax, times_2, 0));
11805 __ neg(edx);
11806 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
11807 __ ret(0);
11808
11809 // Arguments adaptor case: Check index against actual arguments
11810 // limit found in the arguments adaptor frame. Use unsigned
11811 // comparison to get negative check for free.
11812 __ bind(&adaptor);
11813 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
11814 __ cmp(edx, Operand(ecx));
11815 __ j(above_equal, &slow, not_taken);
11816
11817 // Read the argument from the stack and return it.
11818 STATIC_ASSERT(kSmiTagSize == 1);
11819 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
11820 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
11821 __ neg(edx);
11822 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
11823 __ ret(0);
11824
11825 // Slow-case: Handle non-smi or out-of-bounds access to arguments
11826 // by calling the runtime system.
11827 __ bind(&slow);
11828 __ pop(ebx); // Return address.
11829 __ push(edx);
11830 __ push(ebx);
11831 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
11832 }
11833
11834
11835 void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
11836 // esp[0] : return address
11837 // esp[4] : number of parameters
11838 // esp[8] : receiver displacement
11839 // esp[16] : function
11840
11841 // The displacement is used for skipping the return address and the
11842 // frame pointer on the stack. It is the offset of the last
11843 // parameter (if any) relative to the frame pointer.
11844 static const int kDisplacement = 2 * kPointerSize;
11845
11846 // Check if the calling frame is an arguments adaptor frame.
11847 Label adaptor_frame, try_allocate, runtime;
11848 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
11849 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
11850 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
11851 __ j(equal, &adaptor_frame);
11852
11853 // Get the length from the frame.
11854 __ mov(ecx, Operand(esp, 1 * kPointerSize));
11855 __ jmp(&try_allocate);
11856
11857 // Patch the arguments.length and the parameters pointer.
11858 __ bind(&adaptor_frame);
11859 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
11860 __ mov(Operand(esp, 1 * kPointerSize), ecx);
11861 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
11862 __ mov(Operand(esp, 2 * kPointerSize), edx);
11863
11864 // Try the new space allocation. Start out with computing the size of
11865 // the arguments object and the elements array.
11866 Label add_arguments_object;
11867 __ bind(&try_allocate);
11868 __ test(ecx, Operand(ecx));
11869 __ j(zero, &add_arguments_object);
11870 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
11871 __ bind(&add_arguments_object);
11872 __ add(Operand(ecx), Immediate(Heap::kArgumentsObjectSize));
11873
11874 // Do the allocation of both objects in one go.
11875 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
11876
11877 // Get the arguments boilerplate from the current (global) context.
11878 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
11879 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
11880 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
11881 __ mov(edi, Operand(edi, offset));
11882
11883 // Copy the JS object part.
11884 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
11885 __ mov(ebx, FieldOperand(edi, i));
11886 __ mov(FieldOperand(eax, i), ebx);
11887 }
11888
11889 // Setup the callee in-object property.
11890 STATIC_ASSERT(Heap::arguments_callee_index == 0);
11891 __ mov(ebx, Operand(esp, 3 * kPointerSize));
11892 __ mov(FieldOperand(eax, JSObject::kHeaderSize), ebx);
11893
11894 // Get the length (smi tagged) and set that as an in-object property too.
11895 STATIC_ASSERT(Heap::arguments_length_index == 1);
11896 __ mov(ecx, Operand(esp, 1 * kPointerSize));
11897 __ mov(FieldOperand(eax, JSObject::kHeaderSize + kPointerSize), ecx);
11898
11899 // If there are no actual arguments, we're done.
11900 Label done;
11901 __ test(ecx, Operand(ecx));
11902 __ j(zero, &done);
11903
11904 // Get the parameters pointer from the stack.
11905 __ mov(edx, Operand(esp, 2 * kPointerSize));
11906
11907 // Setup the elements pointer in the allocated arguments object and
11908 // initialize the header in the elements fixed array.
11909 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSize));
11910 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
11911 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
11912 Immediate(Factory::fixed_array_map()));
11913 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
11914 // Untag the length for the loop below.
11915 __ SmiUntag(ecx);
11916
11917 // Copy the fixed array slots.
11918 Label loop;
11919 __ bind(&loop);
11920 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
11921 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
11922 __ add(Operand(edi), Immediate(kPointerSize));
11923 __ sub(Operand(edx), Immediate(kPointerSize));
11924 __ dec(ecx);
11925 __ j(not_zero, &loop);
11926
11927 // Return and remove the on-stack parameters.
11928 __ bind(&done);
11929 __ ret(3 * kPointerSize);
11930
11931 // Do the runtime call to allocate the arguments object.
11932 __ bind(&runtime);
11933 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
11934 }
11935
11936
11937 void RegExpExecStub::Generate(MacroAssembler* masm) {
11938 // Just jump directly to runtime if native RegExp is not selected at compile
11939 // time or if regexp entry in generated code is turned off runtime switch or
11940 // at compilation.
11941 #ifdef V8_INTERPRETED_REGEXP
11942 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
11943 #else // V8_INTERPRETED_REGEXP
11944 if (!FLAG_regexp_entry_native) {
11945 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
11946 return;
11947 }
11948
11949 // Stack frame on entry.
11950 // esp[0]: return address
11951 // esp[4]: last_match_info (expected JSArray)
11952 // esp[8]: previous index
11953 // esp[12]: subject string
11954 // esp[16]: JSRegExp object
11955
11956 static const int kLastMatchInfoOffset = 1 * kPointerSize;
11957 static const int kPreviousIndexOffset = 2 * kPointerSize;
11958 static const int kSubjectOffset = 3 * kPointerSize;
11959 static const int kJSRegExpOffset = 4 * kPointerSize;
11960
11961 Label runtime, invoke_regexp;
11962
11963 // Ensure that a RegExp stack is allocated.
11964 ExternalReference address_of_regexp_stack_memory_address =
11965 ExternalReference::address_of_regexp_stack_memory_address();
11966 ExternalReference address_of_regexp_stack_memory_size =
11967 ExternalReference::address_of_regexp_stack_memory_size();
11968 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
11969 __ test(ebx, Operand(ebx));
11970 __ j(zero, &runtime, not_taken);
11971
11972 // Check that the first argument is a JSRegExp object.
11973 __ mov(eax, Operand(esp, kJSRegExpOffset));
11974 STATIC_ASSERT(kSmiTag == 0);
11975 __ test(eax, Immediate(kSmiTagMask));
11976 __ j(zero, &runtime);
11977 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
11978 __ j(not_equal, &runtime);
11979 // Check that the RegExp has been compiled (data contains a fixed array).
11980 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
11981 if (FLAG_debug_code) {
11982 __ test(ecx, Immediate(kSmiTagMask));
11983 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
11984 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
11985 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
11986 }
11987
11988 // ecx: RegExp data (FixedArray)
11989 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
11990 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
11991 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
11992 __ j(not_equal, &runtime);
11993
11994 // ecx: RegExp data (FixedArray)
11995 // Check that the number of captures fit in the static offsets vector buffer.
11996 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
11997 // Calculate number of capture registers (number_of_captures + 1) * 2. This
11998 // uses the asumption that smis are 2 * their untagged value.
11999 STATIC_ASSERT(kSmiTag == 0);
12000 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
12001 __ add(Operand(edx), Immediate(2)); // edx was a smi.
12002 // Check that the static offsets vector buffer is large enough.
12003 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
12004 __ j(above, &runtime);
12005
12006 // ecx: RegExp data (FixedArray)
12007 // edx: Number of capture registers
12008 // Check that the second argument is a string.
12009 __ mov(eax, Operand(esp, kSubjectOffset));
12010 __ test(eax, Immediate(kSmiTagMask));
12011 __ j(zero, &runtime);
12012 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
12013 __ j(NegateCondition(is_string), &runtime);
12014 // Get the length of the string to ebx.
12015 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
12016
12017 // ebx: Length of subject string as a smi
12018 // ecx: RegExp data (FixedArray)
12019 // edx: Number of capture registers
12020 // Check that the third argument is a positive smi less than the subject
12021 // string length. A negative value will be greater (unsigned comparison).
12022 __ mov(eax, Operand(esp, kPreviousIndexOffset));
12023 __ test(eax, Immediate(kSmiTagMask));
12024 __ j(not_zero, &runtime);
12025 __ cmp(eax, Operand(ebx));
12026 __ j(above_equal, &runtime);
12027
12028 // ecx: RegExp data (FixedArray)
12029 // edx: Number of capture registers
12030 // Check that the fourth object is a JSArray object.
12031 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
12032 __ test(eax, Immediate(kSmiTagMask));
12033 __ j(zero, &runtime);
12034 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
12035 __ j(not_equal, &runtime);
12036 // Check that the JSArray is in fast case.
12037 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
12038 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
12039 __ cmp(eax, Factory::fixed_array_map());
12040 __ j(not_equal, &runtime);
12041 // Check that the last match info has space for the capture registers and the
12042 // additional information.
12043 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
12044 __ SmiUntag(eax);
12045 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
12046 __ cmp(edx, Operand(eax));
12047 __ j(greater, &runtime);
12048
12049 // ecx: RegExp data (FixedArray)
12050 // Check the representation and encoding of the subject string.
12051 Label seq_ascii_string, seq_two_byte_string, check_code;
12052 __ mov(eax, Operand(esp, kSubjectOffset));
12053 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
12054 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
12055 // First check for flat two byte string.
12056 __ and_(ebx,
12057 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
12058 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
12059 __ j(zero, &seq_two_byte_string);
12060 // Any other flat string must be a flat ascii string.
12061 __ test(Operand(ebx),
12062 Immediate(kIsNotStringMask | kStringRepresentationMask));
12063 __ j(zero, &seq_ascii_string);
12064
12065 // Check for flat cons string.
12066 // A flat cons string is a cons string where the second part is the empty
12067 // string. In that case the subject string is just the first part of the cons
12068 // string. Also in this case the first part of the cons string is known to be
12069 // a sequential string or an external string.
12070 STATIC_ASSERT(kExternalStringTag != 0);
12071 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
12072 __ test(Operand(ebx),
12073 Immediate(kIsNotStringMask | kExternalStringTag));
12074 __ j(not_zero, &runtime);
12075 // String is a cons string.
12076 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
12077 __ cmp(Operand(edx), Factory::empty_string());
12078 __ j(not_equal, &runtime);
12079 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
12080 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
12081 // String is a cons string with empty second part.
12082 // eax: first part of cons string.
12083 // ebx: map of first part of cons string.
12084 // Is first part a flat two byte string?
12085 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
12086 kStringRepresentationMask | kStringEncodingMask);
12087 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
12088 __ j(zero, &seq_two_byte_string);
12089 // Any other flat string must be ascii.
12090 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
12091 kStringRepresentationMask);
12092 __ j(not_zero, &runtime);
12093
12094 __ bind(&seq_ascii_string);
12095 // eax: subject string (flat ascii)
12096 // ecx: RegExp data (FixedArray)
12097 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
12098 __ Set(edi, Immediate(1)); // Type is ascii.
12099 __ jmp(&check_code);
12100
12101 __ bind(&seq_two_byte_string);
12102 // eax: subject string (flat two byte)
12103 // ecx: RegExp data (FixedArray)
12104 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
12105 __ Set(edi, Immediate(0)); // Type is two byte.
12106
12107 __ bind(&check_code);
12108 // Check that the irregexp code has been generated for the actual string
12109 // encoding. If it has, the field contains a code object otherwise it contains
12110 // the hole.
12111 __ CmpObjectType(edx, CODE_TYPE, ebx);
12112 __ j(not_equal, &runtime);
12113
12114 // eax: subject string
12115 // edx: code
12116 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
12117 // Load used arguments before starting to push arguments for call to native
12118 // RegExp code to avoid handling changing stack height.
12119 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
12120 __ SmiUntag(ebx); // Previous index from smi.
12121
12122 // eax: subject string
12123 // ebx: previous index
12124 // edx: code
12125 // edi: encoding of subject string (1 if ascii 0 if two_byte);
12126 // All checks done. Now push arguments for native regexp code.
12127 __ IncrementCounter(&Counters::regexp_entry_native, 1);
12128
12129 static const int kRegExpExecuteArguments = 7;
12130 __ PrepareCallCFunction(kRegExpExecuteArguments, ecx);
12131
12132 // Argument 7: Indicate that this is a direct call from JavaScript.
12133 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
12134
12135 // Argument 6: Start (high end) of backtracking stack memory area.
12136 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
12137 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
12138 __ mov(Operand(esp, 5 * kPointerSize), ecx);
12139
12140 // Argument 5: static offsets vector buffer.
12141 __ mov(Operand(esp, 4 * kPointerSize),
12142 Immediate(ExternalReference::address_of_static_offsets_vector()));
12143
12144 // Argument 4: End of string data
12145 // Argument 3: Start of string data
12146 Label setup_two_byte, setup_rest;
12147 __ test(edi, Operand(edi));
12148 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
12149 __ j(zero, &setup_two_byte);
12150 __ SmiUntag(edi);
12151 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
12152 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
12153 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
12154 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
12155 __ jmp(&setup_rest);
12156
12157 __ bind(&setup_two_byte);
12158 STATIC_ASSERT(kSmiTag == 0);
12159 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
12160 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
12161 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
12162 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
12163 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
12164
12165 __ bind(&setup_rest);
12166
12167 // Argument 2: Previous index.
12168 __ mov(Operand(esp, 1 * kPointerSize), ebx);
12169
12170 // Argument 1: Subject string.
12171 __ mov(Operand(esp, 0 * kPointerSize), eax);
12172
12173 // Locate the code entry and call it.
12174 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
12175 __ CallCFunction(edx, kRegExpExecuteArguments);
12176
12177 // Check the result.
12178 Label success;
12179 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
12180 __ j(equal, &success, taken);
12181 Label failure;
12182 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
12183 __ j(equal, &failure, taken);
12184 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
12185 // If not exception it can only be retry. Handle that in the runtime system.
12186 __ j(not_equal, &runtime);
12187 // Result must now be exception. If there is no pending exception already a
12188 // stack overflow (on the backtrack stack) was detected in RegExp code but
12189 // haven't created the exception yet. Handle that in the runtime system.
12190 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
12191 ExternalReference pending_exception(Top::k_pending_exception_address);
12192 __ mov(eax,
12193 Operand::StaticVariable(ExternalReference::the_hole_value_location()));
12194 __ cmp(eax, Operand::StaticVariable(pending_exception));
12195 __ j(equal, &runtime);
12196 __ bind(&failure);
12197 // For failure and exception return null.
12198 __ mov(Operand(eax), Factory::null_value());
12199 __ ret(4 * kPointerSize);
12200
12201 // Load RegExp data.
12202 __ bind(&success);
12203 __ mov(eax, Operand(esp, kJSRegExpOffset));
12204 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
12205 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
12206 // Calculate number of capture registers (number_of_captures + 1) * 2.
12207 STATIC_ASSERT(kSmiTag == 0);
12208 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
12209 __ add(Operand(edx), Immediate(2)); // edx was a smi.
12210
12211 // edx: Number of capture registers
12212 // Load last_match_info which is still known to be a fast case JSArray.
12213 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
12214 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
12215
12216 // ebx: last_match_info backing store (FixedArray)
12217 // edx: number of capture registers
12218 // Store the capture count.
12219 __ SmiTag(edx); // Number of capture registers to smi.
12220 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
12221 __ SmiUntag(edx); // Number of capture registers back from smi.
12222 // Store last subject and last input.
12223 __ mov(eax, Operand(esp, kSubjectOffset));
12224 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
12225 __ mov(ecx, ebx);
12226 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
12227 __ mov(eax, Operand(esp, kSubjectOffset));
12228 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
12229 __ mov(ecx, ebx);
12230 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
12231
12232 // Get the static offsets vector filled by the native regexp code.
12233 ExternalReference address_of_static_offsets_vector =
12234 ExternalReference::address_of_static_offsets_vector();
12235 __ mov(ecx, Immediate(address_of_static_offsets_vector));
12236
12237 // ebx: last_match_info backing store (FixedArray)
12238 // ecx: offsets vector
12239 // edx: number of capture registers
12240 Label next_capture, done;
12241 // Capture register counter starts from number of capture registers and
12242 // counts down until wraping after zero.
12243 __ bind(&next_capture);
12244 __ sub(Operand(edx), Immediate(1));
12245 __ j(negative, &done);
12246 // Read the value from the static offsets vector buffer.
12247 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
12248 __ SmiTag(edi);
12249 // Store the smi value in the last match info.
12250 __ mov(FieldOperand(ebx,
12251 edx,
12252 times_pointer_size,
12253 RegExpImpl::kFirstCaptureOffset),
12254 edi);
12255 __ jmp(&next_capture);
12256 __ bind(&done);
12257
12258 // Return last match info.
12259 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
12260 __ ret(4 * kPointerSize);
12261
12262 // Do the runtime call to execute the regexp.
12263 __ bind(&runtime);
12264 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
12265 #endif // V8_INTERPRETED_REGEXP
12266 }
12267
12268
12269 void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
12270 Register object,
12271 Register result,
12272 Register scratch1,
12273 Register scratch2,
12274 bool object_is_smi,
12275 Label* not_found) {
12276 // Use of registers. Register result is used as a temporary.
12277 Register number_string_cache = result;
12278 Register mask = scratch1;
12279 Register scratch = scratch2;
12280
12281 // Load the number string cache.
12282 ExternalReference roots_address = ExternalReference::roots_address();
12283 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
12284 __ mov(number_string_cache,
12285 Operand::StaticArray(scratch, times_pointer_size, roots_address));
12286 // Make the hash mask from the length of the number string cache. It
12287 // contains two elements (number and string) for each cache entry.
12288 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
12289 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
12290 __ sub(Operand(mask), Immediate(1)); // Make mask.
12291
12292 // Calculate the entry in the number string cache. The hash value in the
12293 // number string cache for smis is just the smi value, and the hash for
12294 // doubles is the xor of the upper and lower words. See
12295 // Heap::GetNumberStringCache.
12296 Label smi_hash_calculated;
12297 Label load_result_from_cache;
12298 if (object_is_smi) {
12299 __ mov(scratch, object);
12300 __ SmiUntag(scratch);
12301 } else {
12302 Label not_smi, hash_calculated;
12303 STATIC_ASSERT(kSmiTag == 0);
12304 __ test(object, Immediate(kSmiTagMask));
12305 __ j(not_zero, &not_smi);
12306 __ mov(scratch, object);
12307 __ SmiUntag(scratch);
12308 __ jmp(&smi_hash_calculated);
12309 __ bind(&not_smi);
12310 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
12311 Factory::heap_number_map());
12312 __ j(not_equal, not_found);
12313 STATIC_ASSERT(8 == kDoubleSize);
12314 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
12315 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
12316 // Object is heap number and hash is now in scratch. Calculate cache index.
12317 __ and_(scratch, Operand(mask));
12318 Register index = scratch;
12319 Register probe = mask;
12320 __ mov(probe,
12321 FieldOperand(number_string_cache,
12322 index,
12323 times_twice_pointer_size,
12324 FixedArray::kHeaderSize));
12325 __ test(probe, Immediate(kSmiTagMask));
12326 __ j(zero, not_found);
12327 if (CpuFeatures::IsSupported(SSE2)) {
12328 CpuFeatures::Scope fscope(SSE2);
12329 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
12330 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
12331 __ ucomisd(xmm0, xmm1);
12332 } else {
12333 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
12334 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
12335 __ FCmp();
12336 }
12337 __ j(parity_even, not_found); // Bail out if NaN is involved.
12338 __ j(not_equal, not_found); // The cache did not contain this value.
12339 __ jmp(&load_result_from_cache);
12340 }
12341
12342 __ bind(&smi_hash_calculated);
12343 // Object is smi and hash is now in scratch. Calculate cache index.
12344 __ and_(scratch, Operand(mask));
12345 Register index = scratch;
12346 // Check if the entry is the smi we are looking for.
12347 __ cmp(object,
12348 FieldOperand(number_string_cache,
12349 index,
12350 times_twice_pointer_size,
12351 FixedArray::kHeaderSize));
12352 __ j(not_equal, not_found);
12353
12354 // Get the result from the cache.
12355 __ bind(&load_result_from_cache);
12356 __ mov(result,
12357 FieldOperand(number_string_cache,
12358 index,
12359 times_twice_pointer_size,
12360 FixedArray::kHeaderSize + kPointerSize));
12361 __ IncrementCounter(&Counters::number_to_string_native, 1);
12362 }
12363
12364
12365 void NumberToStringStub::Generate(MacroAssembler* masm) {
12366 Label runtime;
12367
12368 __ mov(ebx, Operand(esp, kPointerSize));
12369
12370 // Generate code to lookup number in the number string cache.
12371 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
12372 __ ret(1 * kPointerSize);
12373
12374 __ bind(&runtime);
12375 // Handle number to string in the runtime system if not found in the cache.
12376 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
12377 }
12378
12379
12380 static int NegativeComparisonResult(Condition cc) {
12381 ASSERT(cc != equal);
12382 ASSERT((cc == less) || (cc == less_equal)
12383 || (cc == greater) || (cc == greater_equal));
12384 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
12385 }
12386
12387
12388 void CompareStub::Generate(MacroAssembler* masm) {
12389 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
12390
12391 Label check_unequal_objects, done;
12392
12393 // NOTICE! This code is only reached after a smi-fast-case check, so
12394 // it is certain that at least one operand isn't a smi.
12395
12396 // Identical objects can be compared fast, but there are some tricky cases
12397 // for NaN and undefined.
12398 {
12399 Label not_identical;
12400 __ cmp(eax, Operand(edx));
12401 __ j(not_equal, &not_identical);
12402
12403 if (cc_ != equal) {
12404 // Check for undefined. undefined OP undefined is false even though
12405 // undefined == undefined.
12406 Label check_for_nan;
12407 __ cmp(edx, Factory::undefined_value());
12408 __ j(not_equal, &check_for_nan);
12409 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
12410 __ ret(0);
12411 __ bind(&check_for_nan);
12412 }
12413
12414 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
12415 // so we do the second best thing - test it ourselves.
12416 // Note: if cc_ != equal, never_nan_nan_ is not used.
12417 if (never_nan_nan_ && (cc_ == equal)) {
12418 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
12419 __ ret(0);
12420 } else {
12421 Label heap_number;
12422 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
12423 Immediate(Factory::heap_number_map()));
12424 __ j(equal, &heap_number);
12425 if (cc_ != equal) {
12426 // Call runtime on identical JSObjects. Otherwise return equal.
12427 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
12428 __ j(above_equal, &not_identical);
12429 }
12430 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
12431 __ ret(0);
12432
12433 __ bind(&heap_number);
12434 // It is a heap number, so return non-equal if it's NaN and equal if
12435 // it's not NaN.
12436 // The representation of NaN values has all exponent bits (52..62) set,
12437 // and not all mantissa bits (0..51) clear.
12438 // We only accept QNaNs, which have bit 51 set.
12439 // Read top bits of double representation (second word of value).
12440
12441 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
12442 // all bits in the mask are set. We only need to check the word
12443 // that contains the exponent and high bit of the mantissa.
12444 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
12445 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
12446 __ xor_(eax, Operand(eax));
12447 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
12448 // bits.
12449 __ add(edx, Operand(edx));
12450 __ cmp(edx, kQuietNaNHighBitsMask << 1);
12451 if (cc_ == equal) {
12452 STATIC_ASSERT(EQUAL != 1);
12453 __ setcc(above_equal, eax);
12454 __ ret(0);
12455 } else {
12456 Label nan;
12457 __ j(above_equal, &nan);
12458 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
12459 __ ret(0);
12460 __ bind(&nan);
12461 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
12462 __ ret(0);
12463 }
12464 }
12465
12466 __ bind(&not_identical);
12467 }
12468
12469 // Strict equality can quickly decide whether objects are equal.
12470 // Non-strict object equality is slower, so it is handled later in the stub.
12471 if (cc_ == equal && strict_) {
12472 Label slow; // Fallthrough label.
12473 Label not_smis;
12474 // If we're doing a strict equality comparison, we don't have to do
12475 // type conversion, so we generate code to do fast comparison for objects
12476 // and oddballs. Non-smi numbers and strings still go through the usual
12477 // slow-case code.
12478 // If either is a Smi (we know that not both are), then they can only
12479 // be equal if the other is a HeapNumber. If so, use the slow case.
12480 STATIC_ASSERT(kSmiTag == 0);
12481 ASSERT_EQ(0, Smi::FromInt(0));
12482 __ mov(ecx, Immediate(kSmiTagMask));
12483 __ and_(ecx, Operand(eax));
12484 __ test(ecx, Operand(edx));
12485 __ j(not_zero, &not_smis);
12486 // One operand is a smi.
12487
12488 // Check whether the non-smi is a heap number.
12489 STATIC_ASSERT(kSmiTagMask == 1);
12490 // ecx still holds eax & kSmiTag, which is either zero or one.
12491 __ sub(Operand(ecx), Immediate(0x01));
12492 __ mov(ebx, edx);
12493 __ xor_(ebx, Operand(eax));
12494 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
12495 __ xor_(ebx, Operand(eax));
12496 // if eax was smi, ebx is now edx, else eax.
12497
12498 // Check if the non-smi operand is a heap number.
12499 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
12500 Immediate(Factory::heap_number_map()));
12501 // If heap number, handle it in the slow case.
12502 __ j(equal, &slow);
12503 // Return non-equal (ebx is not zero)
12504 __ mov(eax, ebx);
12505 __ ret(0);
12506
12507 __ bind(&not_smis);
12508 // If either operand is a JSObject or an oddball value, then they are not
12509 // equal since their pointers are different
12510 // There is no test for undetectability in strict equality.
12511
12512 // Get the type of the first operand.
12513 // If the first object is a JS object, we have done pointer comparison.
12514 Label first_non_object;
12515 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
12516 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
12517 __ j(below, &first_non_object);
12518
12519 // Return non-zero (eax is not zero)
12520 Label return_not_equal;
12521 STATIC_ASSERT(kHeapObjectTag != 0);
12522 __ bind(&return_not_equal);
12523 __ ret(0);
12524
12525 __ bind(&first_non_object);
12526 // Check for oddballs: true, false, null, undefined.
12527 __ CmpInstanceType(ecx, ODDBALL_TYPE);
12528 __ j(equal, &return_not_equal);
12529
12530 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
12531 __ j(above_equal, &return_not_equal);
12532
12533 // Check for oddballs: true, false, null, undefined.
12534 __ CmpInstanceType(ecx, ODDBALL_TYPE);
12535 __ j(equal, &return_not_equal);
12536
12537 // Fall through to the general case.
12538 __ bind(&slow);
12539 }
12540
12541 // Generate the number comparison code.
12542 if (include_number_compare_) {
12543 Label non_number_comparison;
12544 Label unordered;
12545 if (CpuFeatures::IsSupported(SSE2)) {
12546 CpuFeatures::Scope use_sse2(SSE2);
12547 CpuFeatures::Scope use_cmov(CMOV);
12548
12549 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
12550 __ ucomisd(xmm0, xmm1);
12551
12552 // Don't base result on EFLAGS when a NaN is involved.
12553 __ j(parity_even, &unordered, not_taken);
12554 // Return a result of -1, 0, or 1, based on EFLAGS.
12555 __ mov(eax, 0); // equal
12556 __ mov(ecx, Immediate(Smi::FromInt(1)));
12557 __ cmov(above, eax, Operand(ecx));
12558 __ mov(ecx, Immediate(Smi::FromInt(-1)));
12559 __ cmov(below, eax, Operand(ecx));
12560 __ ret(0);
12561 } else {
12562 FloatingPointHelper::CheckFloatOperands(
12563 masm, &non_number_comparison, ebx);
12564 FloatingPointHelper::LoadFloatOperand(masm, eax);
12565 FloatingPointHelper::LoadFloatOperand(masm, edx);
12566 __ FCmp();
12567
12568 // Don't base result on EFLAGS when a NaN is involved.
12569 __ j(parity_even, &unordered, not_taken);
12570
12571 Label below_label, above_label;
12572 // Return a result of -1, 0, or 1, based on EFLAGS.
12573 __ j(below, &below_label, not_taken);
12574 __ j(above, &above_label, not_taken);
12575
12576 __ xor_(eax, Operand(eax));
12577 __ ret(0);
12578
12579 __ bind(&below_label);
12580 __ mov(eax, Immediate(Smi::FromInt(-1)));
12581 __ ret(0);
12582
12583 __ bind(&above_label);
12584 __ mov(eax, Immediate(Smi::FromInt(1)));
12585 __ ret(0);
12586 }
12587
12588 // If one of the numbers was NaN, then the result is always false.
12589 // The cc is never not-equal.
12590 __ bind(&unordered);
12591 ASSERT(cc_ != not_equal);
12592 if (cc_ == less || cc_ == less_equal) {
12593 __ mov(eax, Immediate(Smi::FromInt(1)));
12594 } else {
12595 __ mov(eax, Immediate(Smi::FromInt(-1)));
12596 }
12597 __ ret(0);
12598
12599 // The number comparison code did not provide a valid result.
12600 __ bind(&non_number_comparison);
12601 }
12602
12603 // Fast negative check for symbol-to-symbol equality.
12604 Label check_for_strings;
12605 if (cc_ == equal) {
12606 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
12607 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
12608
12609 // We've already checked for object identity, so if both operands
12610 // are symbols they aren't equal. Register eax already holds a
12611 // non-zero value, which indicates not equal, so just return.
12612 __ ret(0);
12613 }
12614
12615 __ bind(&check_for_strings);
12616
12617 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
12618 &check_unequal_objects);
12619
12620 // Inline comparison of ascii strings.
12621 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
12622 edx,
12623 eax,
12624 ecx,
12625 ebx,
12626 edi);
12627 #ifdef DEBUG
12628 __ Abort("Unexpected fall-through from string comparison");
12629 #endif
12630
12631 __ bind(&check_unequal_objects);
12632 if (cc_ == equal && !strict_) {
12633 // Non-strict equality. Objects are unequal if
12634 // they are both JSObjects and not undetectable,
12635 // and their pointers are different.
12636 Label not_both_objects;
12637 Label return_unequal;
12638 // At most one is a smi, so we can test for smi by adding the two.
12639 // A smi plus a heap object has the low bit set, a heap object plus
12640 // a heap object has the low bit clear.
12641 STATIC_ASSERT(kSmiTag == 0);
12642 STATIC_ASSERT(kSmiTagMask == 1);
12643 __ lea(ecx, Operand(eax, edx, times_1, 0));
12644 __ test(ecx, Immediate(kSmiTagMask));
12645 __ j(not_zero, &not_both_objects);
12646 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
12647 __ j(below, &not_both_objects);
12648 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
12649 __ j(below, &not_both_objects);
12650 // We do not bail out after this point. Both are JSObjects, and
12651 // they are equal if and only if both are undetectable.
12652 // The and of the undetectable flags is 1 if and only if they are equal.
12653 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
12654 1 << Map::kIsUndetectable);
12655 __ j(zero, &return_unequal);
12656 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
12657 1 << Map::kIsUndetectable);
12658 __ j(zero, &return_unequal);
12659 // The objects are both undetectable, so they both compare as the value
12660 // undefined, and are equal.
12661 __ Set(eax, Immediate(EQUAL));
12662 __ bind(&return_unequal);
12663 // Return non-equal by returning the non-zero object pointer in eax,
12664 // or return equal if we fell through to here.
12665 __ ret(0); // rax, rdx were pushed
12666 __ bind(&not_both_objects);
12667 }
12668
12669 // Push arguments below the return address.
12670 __ pop(ecx);
12671 __ push(edx);
12672 __ push(eax);
12673
12674 // Figure out which native to call and setup the arguments.
12675 Builtins::JavaScript builtin;
12676 if (cc_ == equal) {
12677 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
12678 } else {
12679 builtin = Builtins::COMPARE;
12680 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
12681 }
12682
12683 // Restore return address on the stack.
12684 __ push(ecx);
12685
12686 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
12687 // tagged as a small integer.
12688 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
12689 }
12690
12691
12692 void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
12693 Label* label,
12694 Register object,
12695 Register scratch) {
12696 __ test(object, Immediate(kSmiTagMask));
12697 __ j(zero, label);
12698 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
12699 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
12700 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
12701 __ cmp(scratch, kSymbolTag | kStringTag);
12702 __ j(not_equal, label);
12703 }
12704
12705
12706 void StackCheckStub::Generate(MacroAssembler* masm) {
12707 // Because builtins always remove the receiver from the stack, we
12708 // have to fake one to avoid underflowing the stack. The receiver
12709 // must be inserted below the return address on the stack so we
12710 // temporarily store that in a register.
12711 __ pop(eax);
12712 __ push(Immediate(Smi::FromInt(0)));
12713 __ push(eax);
12714
12715 // Do tail-call to runtime routine.
12716 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
12717 }
12718
12719
12720 void CallFunctionStub::Generate(MacroAssembler* masm) {
12721 Label slow;
12722
12723 // If the receiver might be a value (string, number or boolean) check for this
12724 // and box it if it is.
12725 if (ReceiverMightBeValue()) {
12726 // Get the receiver from the stack.
12727 // +1 ~ return address
12728 Label receiver_is_value, receiver_is_js_object;
12729 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
12730
12731 // Check if receiver is a smi (which is a number value).
12732 __ test(eax, Immediate(kSmiTagMask));
12733 __ j(zero, &receiver_is_value, not_taken);
12734
12735 // Check if the receiver is a valid JS object.
12736 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
12737 __ j(above_equal, &receiver_is_js_object);
12738
12739 // Call the runtime to box the value.
12740 __ bind(&receiver_is_value);
12741 __ EnterInternalFrame();
12742 __ push(eax);
12743 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
12744 __ LeaveInternalFrame();
12745 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
12746
12747 __ bind(&receiver_is_js_object);
12748 }
12749
12750 // Get the function to call from the stack.
12751 // +2 ~ receiver, return address
12752 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
12753
12754 // Check that the function really is a JavaScript function.
12755 __ test(edi, Immediate(kSmiTagMask));
12756 __ j(zero, &slow, not_taken);
12757 // Goto slow case if we do not have a function.
12758 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
12759 __ j(not_equal, &slow, not_taken);
12760
12761 // Fast-case: Just invoke the function.
12762 ParameterCount actual(argc_);
12763 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
12764
12765 // Slow-case: Non-function called.
12766 __ bind(&slow);
12767 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
12768 // of the original receiver from the call site).
12769 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
12770 __ Set(eax, Immediate(argc_));
12771 __ Set(ebx, Immediate(0));
12772 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
12773 Handle<Code> adaptor(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
12774 __ jmp(adaptor, RelocInfo::CODE_TARGET);
12775 }
12776
12777
12778 void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
12779 // eax holds the exception.
12780
12781 // Adjust this code if not the case.
12782 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
12783
12784 // Drop the sp to the top of the handler.
12785 ExternalReference handler_address(Top::k_handler_address);
12786 __ mov(esp, Operand::StaticVariable(handler_address));
12787
12788 // Restore next handler and frame pointer, discard handler state.
12789 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
12790 __ pop(Operand::StaticVariable(handler_address));
12791 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
12792 __ pop(ebp);
12793 __ pop(edx); // Remove state.
12794
12795 // Before returning we restore the context from the frame pointer if
12796 // not NULL. The frame pointer is NULL in the exception handler of
12797 // a JS entry frame.
12798 __ xor_(esi, Operand(esi)); // Tentatively set context pointer to NULL.
12799 Label skip;
12800 __ cmp(ebp, 0);
12801 __ j(equal, &skip, not_taken);
12802 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
12803 __ bind(&skip);
12804
12805 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
12806 __ ret(0);
12807 }
12808
12809
12810 // If true, a Handle<T> passed by value is passed and returned by
12811 // using the location_ field directly. If false, it is passed and
12812 // returned as a pointer to a handle.
12813 #ifdef USING_BSD_ABI
12814 static const bool kPassHandlesDirectly = true;
12815 #else
12816 static const bool kPassHandlesDirectly = false;
12817 #endif
12818
12819
12820 void ApiGetterEntryStub::Generate(MacroAssembler* masm) {
12821 Label empty_handle;
12822 Label prologue;
12823 Label promote_scheduled_exception;
12824 __ EnterApiExitFrame(ExitFrame::MODE_NORMAL, kStackSpace, kArgc);
12825 STATIC_ASSERT(kArgc == 4);
12826 if (kPassHandlesDirectly) {
12827 // When handles as passed directly we don't have to allocate extra
12828 // space for and pass an out parameter.
12829 __ mov(Operand(esp, 0 * kPointerSize), ebx); // name.
12830 __ mov(Operand(esp, 1 * kPointerSize), eax); // arguments pointer.
12831 } else {
12832 // The function expects three arguments to be passed but we allocate
12833 // four to get space for the output cell. The argument slots are filled
12834 // as follows:
12835 //
12836 // 3: output cell
12837 // 2: arguments pointer
12838 // 1: name
12839 // 0: pointer to the output cell
12840 //
12841 // Note that this is one more "argument" than the function expects
12842 // so the out cell will have to be popped explicitly after returning
12843 // from the function.
12844 __ mov(Operand(esp, 1 * kPointerSize), ebx); // name.
12845 __ mov(Operand(esp, 2 * kPointerSize), eax); // arguments pointer.
12846 __ mov(ebx, esp);
12847 __ add(Operand(ebx), Immediate(3 * kPointerSize));
12848 __ mov(Operand(esp, 0 * kPointerSize), ebx); // output
12849 __ mov(Operand(esp, 3 * kPointerSize), Immediate(0)); // out cell.
12850 }
12851 // Call the api function!
12852 __ call(fun()->address(), RelocInfo::RUNTIME_ENTRY);
12853 // Check if the function scheduled an exception.
12854 ExternalReference scheduled_exception_address =
12855 ExternalReference::scheduled_exception_address();
12856 __ cmp(Operand::StaticVariable(scheduled_exception_address),
12857 Immediate(Factory::the_hole_value()));
12858 __ j(not_equal, &promote_scheduled_exception, not_taken);
12859 if (!kPassHandlesDirectly) {
12860 // The returned value is a pointer to the handle holding the result.
12861 // Dereference this to get to the location.
12862 __ mov(eax, Operand(eax, 0));
12863 }
12864 // Check if the result handle holds 0.
12865 __ test(eax, Operand(eax));
12866 __ j(zero, &empty_handle, not_taken);
12867 // It was non-zero. Dereference to get the result value.
12868 __ mov(eax, Operand(eax, 0));
12869 __ bind(&prologue);
12870 __ LeaveExitFrame(ExitFrame::MODE_NORMAL);
12871 __ ret(0);
12872 __ bind(&promote_scheduled_exception);
12873 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
12874 __ bind(&empty_handle);
12875 // It was zero; the result is undefined.
12876 __ mov(eax, Factory::undefined_value());
12877 __ jmp(&prologue);
12878 }
12879
12880
12881 void CEntryStub::GenerateCore(MacroAssembler* masm,
12882 Label* throw_normal_exception,
12883 Label* throw_termination_exception,
12884 Label* throw_out_of_memory_exception,
12885 bool do_gc,
12886 bool always_allocate_scope,
12887 int /* alignment_skew */) {
12888 // eax: result parameter for PerformGC, if any
12889 // ebx: pointer to C function (C callee-saved)
12890 // ebp: frame pointer (restored after C call)
12891 // esp: stack pointer (restored after C call)
12892 // edi: number of arguments including receiver (C callee-saved)
12893 // esi: pointer to the first argument (C callee-saved)
12894
12895 // Result returned in eax, or eax+edx if result_size_ is 2.
12896
12897 // Check stack alignment.
12898 if (FLAG_debug_code) {
12899 __ CheckStackAlignment();
12900 }
12901
12902 if (do_gc) {
12903 // Pass failure code returned from last attempt as first argument to
12904 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
12905 // stack alignment is known to be correct. This function takes one argument
12906 // which is passed on the stack, and we know that the stack has been
12907 // prepared to pass at least one argument.
12908 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
12909 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
12910 }
12911
12912 ExternalReference scope_depth =
12913 ExternalReference::heap_always_allocate_scope_depth();
12914 if (always_allocate_scope) {
12915 __ inc(Operand::StaticVariable(scope_depth));
12916 }
12917
12918 // Call C function.
12919 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
12920 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
12921 __ call(Operand(ebx));
12922 // Result is in eax or edx:eax - do not destroy these registers!
12923
12924 if (always_allocate_scope) {
12925 __ dec(Operand::StaticVariable(scope_depth));
12926 }
12927
12928 // Make sure we're not trying to return 'the hole' from the runtime
12929 // call as this may lead to crashes in the IC code later.
12930 if (FLAG_debug_code) {
12931 Label okay;
12932 __ cmp(eax, Factory::the_hole_value());
12933 __ j(not_equal, &okay);
12934 __ int3();
12935 __ bind(&okay);
12936 }
12937
12938 // Check for failure result.
12939 Label failure_returned;
12940 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
12941 __ lea(ecx, Operand(eax, 1));
12942 // Lower 2 bits of ecx are 0 iff eax has failure tag.
12943 __ test(ecx, Immediate(kFailureTagMask));
12944 __ j(zero, &failure_returned, not_taken);
12945
12946 // Exit the JavaScript to C++ exit frame.
12947 __ LeaveExitFrame(mode_);
12948 __ ret(0);
12949
12950 // Handling of failure.
12951 __ bind(&failure_returned);
12952
12953 Label retry;
12954 // If the returned exception is RETRY_AFTER_GC continue at retry label
12955 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
12956 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
12957 __ j(zero, &retry, taken);
12958
12959 // Special handling of out of memory exceptions.
12960 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
12961 __ j(equal, throw_out_of_memory_exception);
12962
12963 // Retrieve the pending exception and clear the variable.
12964 ExternalReference pending_exception_address(Top::k_pending_exception_address);
12965 __ mov(eax, Operand::StaticVariable(pending_exception_address));
12966 __ mov(edx,
12967 Operand::StaticVariable(ExternalReference::the_hole_value_location()));
12968 __ mov(Operand::StaticVariable(pending_exception_address), edx);
12969
12970 // Special handling of termination exceptions which are uncatchable
12971 // by javascript code.
12972 __ cmp(eax, Factory::termination_exception());
12973 __ j(equal, throw_termination_exception);
12974
12975 // Handle normal exception.
12976 __ jmp(throw_normal_exception);
12977
12978 // Retry.
12979 __ bind(&retry);
12980 }
12981
12982
12983 void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
12984 UncatchableExceptionType type) {
12985 // Adjust this code if not the case.
12986 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
12987
12988 // Drop sp to the top stack handler.
12989 ExternalReference handler_address(Top::k_handler_address);
12990 __ mov(esp, Operand::StaticVariable(handler_address));
12991
12992 // Unwind the handlers until the ENTRY handler is found.
12993 Label loop, done;
12994 __ bind(&loop);
12995 // Load the type of the current stack handler.
12996 const int kStateOffset = StackHandlerConstants::kStateOffset;
12997 __ cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY));
12998 __ j(equal, &done);
12999 // Fetch the next handler in the list.
13000 const int kNextOffset = StackHandlerConstants::kNextOffset;
13001 __ mov(esp, Operand(esp, kNextOffset));
13002 __ jmp(&loop);
13003 __ bind(&done);
13004
13005 // Set the top handler address to next handler past the current ENTRY handler.
13006 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
13007 __ pop(Operand::StaticVariable(handler_address));
13008
13009 if (type == OUT_OF_MEMORY) {
13010 // Set external caught exception to false.
13011 ExternalReference external_caught(Top::k_external_caught_exception_address);
13012 __ mov(eax, false);
13013 __ mov(Operand::StaticVariable(external_caught), eax);
13014
13015 // Set pending exception and eax to out of memory exception.
13016 ExternalReference pending_exception(Top::k_pending_exception_address);
13017 __ mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
13018 __ mov(Operand::StaticVariable(pending_exception), eax);
13019 }
13020
13021 // Clear the context pointer.
13022 __ xor_(esi, Operand(esi));
13023
13024 // Restore fp from handler and discard handler state.
13025 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
13026 __ pop(ebp);
13027 __ pop(edx); // State.
13028
13029 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
13030 __ ret(0);
13031 }
13032
13033
13034 void CEntryStub::Generate(MacroAssembler* masm) {
13035 // eax: number of arguments including receiver
13036 // ebx: pointer to C function (C callee-saved)
13037 // ebp: frame pointer (restored after C call)
13038 // esp: stack pointer (restored after C call)
13039 // esi: current context (C callee-saved)
13040 // edi: JS function of the caller (C callee-saved)
13041
13042 // NOTE: Invocations of builtins may return failure objects instead
13043 // of a proper result. The builtin entry handles this by performing
13044 // a garbage collection and retrying the builtin (twice).
13045
13046 // Enter the exit frame that transitions from JavaScript to C++.
13047 __ EnterExitFrame(mode_);
13048
13049 // eax: result parameter for PerformGC, if any (setup below)
13050 // ebx: pointer to builtin function (C callee-saved)
13051 // ebp: frame pointer (restored after C call)
13052 // esp: stack pointer (restored after C call)
13053 // edi: number of arguments including receiver (C callee-saved)
13054 // esi: argv pointer (C callee-saved)
13055
13056 Label throw_normal_exception;
13057 Label throw_termination_exception;
13058 Label throw_out_of_memory_exception;
13059
13060 // Call into the runtime system.
13061 GenerateCore(masm,
13062 &throw_normal_exception,
13063 &throw_termination_exception,
13064 &throw_out_of_memory_exception,
13065 false,
13066 false);
13067
13068 // Do space-specific GC and retry runtime call.
13069 GenerateCore(masm,
13070 &throw_normal_exception,
13071 &throw_termination_exception,
13072 &throw_out_of_memory_exception,
13073 true,
13074 false);
13075
13076 // Do full GC and retry runtime call one final time.
13077 Failure* failure = Failure::InternalError();
13078 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
13079 GenerateCore(masm,
13080 &throw_normal_exception,
13081 &throw_termination_exception,
13082 &throw_out_of_memory_exception,
13083 true,
13084 true);
13085
13086 __ bind(&throw_out_of_memory_exception);
13087 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
13088
13089 __ bind(&throw_termination_exception);
13090 GenerateThrowUncatchable(masm, TERMINATION);
13091
13092 __ bind(&throw_normal_exception);
13093 GenerateThrowTOS(masm);
13094 }
13095
13096
13097 void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
13098 Label invoke, exit;
13099 #ifdef ENABLE_LOGGING_AND_PROFILING
13100 Label not_outermost_js, not_outermost_js_2;
13101 #endif
13102
13103 // Setup frame.
13104 __ push(ebp);
13105 __ mov(ebp, Operand(esp));
13106
13107 // Push marker in two places.
13108 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
13109 __ push(Immediate(Smi::FromInt(marker))); // context slot
13110 __ push(Immediate(Smi::FromInt(marker))); // function slot
13111 // Save callee-saved registers (C calling conventions).
13112 __ push(edi);
13113 __ push(esi);
13114 __ push(ebx);
13115
13116 // Save copies of the top frame descriptor on the stack.
13117 ExternalReference c_entry_fp(Top::k_c_entry_fp_address);
13118 __ push(Operand::StaticVariable(c_entry_fp));
13119
13120 #ifdef ENABLE_LOGGING_AND_PROFILING
13121 // If this is the outermost JS call, set js_entry_sp value.
13122 ExternalReference js_entry_sp(Top::k_js_entry_sp_address);
13123 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
13124 __ j(not_equal, &not_outermost_js);
13125 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
13126 __ bind(&not_outermost_js);
13127 #endif
13128
13129 // Call a faked try-block that does the invoke.
13130 __ call(&invoke);
13131
13132 // Caught exception: Store result (exception) in the pending
13133 // exception field in the JSEnv and return a failure sentinel.
13134 ExternalReference pending_exception(Top::k_pending_exception_address);
13135 __ mov(Operand::StaticVariable(pending_exception), eax);
13136 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
13137 __ jmp(&exit);
13138
13139 // Invoke: Link this frame into the handler chain.
13140 __ bind(&invoke);
13141 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
13142
13143 // Clear any pending exceptions.
13144 __ mov(edx,
13145 Operand::StaticVariable(ExternalReference::the_hole_value_location()));
13146 __ mov(Operand::StaticVariable(pending_exception), edx);
13147
13148 // Fake a receiver (NULL).
13149 __ push(Immediate(0)); // receiver
13150
13151 // Invoke the function by calling through JS entry trampoline
13152 // builtin and pop the faked function when we return. Notice that we
13153 // cannot store a reference to the trampoline code directly in this
13154 // stub, because the builtin stubs may not have been generated yet.
13155 if (is_construct) {
13156 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
13157 __ mov(edx, Immediate(construct_entry));
13158 } else {
13159 ExternalReference entry(Builtins::JSEntryTrampoline);
13160 __ mov(edx, Immediate(entry));
13161 }
13162 __ mov(edx, Operand(edx, 0)); // deref address
13163 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
13164 __ call(Operand(edx));
13165
13166 // Unlink this frame from the handler chain.
13167 __ pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
13168 // Pop next_sp.
13169 __ add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
13170
13171 #ifdef ENABLE_LOGGING_AND_PROFILING
13172 // If current EBP value is the same as js_entry_sp value, it means that
13173 // the current function is the outermost.
13174 __ cmp(ebp, Operand::StaticVariable(js_entry_sp));
13175 __ j(not_equal, &not_outermost_js_2);
13176 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
13177 __ bind(&not_outermost_js_2);
13178 #endif
13179
13180 // Restore the top frame descriptor from the stack.
13181 __ bind(&exit);
13182 __ pop(Operand::StaticVariable(ExternalReference(Top::k_c_entry_fp_address)));
13183
13184 // Restore callee-saved registers (C calling conventions).
13185 __ pop(ebx);
13186 __ pop(esi);
13187 __ pop(edi);
13188 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
13189
13190 // Restore frame pointer and return.
13191 __ pop(ebp);
13192 __ ret(0);
13193 }
13194
13195
13196 void InstanceofStub::Generate(MacroAssembler* masm) {
13197 // Get the object - go slow case if it's a smi.
13198 Label slow;
13199 __ mov(eax, Operand(esp, 2 * kPointerSize)); // 2 ~ return address, function
13200 __ test(eax, Immediate(kSmiTagMask));
13201 __ j(zero, &slow, not_taken);
13202
13203 // Check that the left hand is a JS object.
13204 __ IsObjectJSObjectType(eax, eax, edx, &slow);
13205
13206 // Get the prototype of the function.
13207 __ mov(edx, Operand(esp, 1 * kPointerSize)); // 1 ~ return address
13208 // edx is function, eax is map.
13209
13210 // Look up the function and the map in the instanceof cache.
13211 Label miss;
13212 ExternalReference roots_address = ExternalReference::roots_address();
13213 __ mov(ecx, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
13214 __ cmp(edx, Operand::StaticArray(ecx, times_pointer_size, roots_address));
13215 __ j(not_equal, &miss);
13216 __ mov(ecx, Immediate(Heap::kInstanceofCacheMapRootIndex));
13217 __ cmp(eax, Operand::StaticArray(ecx, times_pointer_size, roots_address));
13218 __ j(not_equal, &miss);
13219 __ mov(ecx, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
13220 __ mov(eax, Operand::StaticArray(ecx, times_pointer_size, roots_address));
13221 __ ret(2 * kPointerSize);
13222
13223 __ bind(&miss);
13224 __ TryGetFunctionPrototype(edx, ebx, ecx, &slow);
13225
13226 // Check that the function prototype is a JS object.
13227 __ test(ebx, Immediate(kSmiTagMask));
13228 __ j(zero, &slow, not_taken);
13229 __ IsObjectJSObjectType(ebx, ecx, ecx, &slow);
13230
13231 // Register mapping:
13232 // eax is object map.
13233 // edx is function.
13234 // ebx is function prototype.
13235 __ mov(ecx, Immediate(Heap::kInstanceofCacheMapRootIndex));
13236 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), eax);
13237 __ mov(ecx, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
13238 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), edx);
13239
13240 __ mov(ecx, FieldOperand(eax, Map::kPrototypeOffset));
13241
13242 // Loop through the prototype chain looking for the function prototype.
13243 Label loop, is_instance, is_not_instance;
13244 __ bind(&loop);
13245 __ cmp(ecx, Operand(ebx));
13246 __ j(equal, &is_instance);
13247 __ cmp(Operand(ecx), Immediate(Factory::null_value()));
13248 __ j(equal, &is_not_instance);
13249 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
13250 __ mov(ecx, FieldOperand(ecx, Map::kPrototypeOffset));
13251 __ jmp(&loop);
13252
13253 __ bind(&is_instance);
13254 __ Set(eax, Immediate(0));
13255 __ mov(ecx, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
13256 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), eax);
13257 __ ret(2 * kPointerSize);
13258
13259 __ bind(&is_not_instance);
13260 __ Set(eax, Immediate(Smi::FromInt(1)));
13261 __ mov(ecx, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
13262 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), eax);
13263 __ ret(2 * kPointerSize);
13264
13265 // Slow-case: Go through the JavaScript implementation.
13266 __ bind(&slow);
13267 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
13268 }
13269
13270
13271 int CompareStub::MinorKey() {
13272 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
13273 // stubs the never NaN NaN condition is only taken into account if the
13274 // condition is equals.
13275 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
13276 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
13277 return ConditionField::encode(static_cast<unsigned>(cc_))
13278 | RegisterField::encode(false) // lhs_ and rhs_ are not used
13279 | StrictField::encode(strict_)
13280 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
13281 | IncludeNumberCompareField::encode(include_number_compare_);
13282 }
13283
13284
13285 // Unfortunately you have to run without snapshots to see most of these
13286 // names in the profile since most compare stubs end up in the snapshot.
13287 const char* CompareStub::GetName() {
13288 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
13289
13290 if (name_ != NULL) return name_;
13291 const int kMaxNameLength = 100;
13292 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
13293 if (name_ == NULL) return "OOM";
13294
13295 const char* cc_name;
13296 switch (cc_) {
13297 case less: cc_name = "LT"; break;
13298 case greater: cc_name = "GT"; break;
13299 case less_equal: cc_name = "LE"; break;
13300 case greater_equal: cc_name = "GE"; break;
13301 case equal: cc_name = "EQ"; break;
13302 case not_equal: cc_name = "NE"; break;
13303 default: cc_name = "UnknownCondition"; break;
13304 }
13305
13306 const char* strict_name = "";
13307 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
13308 strict_name = "_STRICT";
13309 }
13310
13311 const char* never_nan_nan_name = "";
13312 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
13313 never_nan_nan_name = "_NO_NAN";
13314 }
13315
13316 const char* include_number_compare_name = "";
13317 if (!include_number_compare_) {
13318 include_number_compare_name = "_NO_NUMBER";
13319 }
13320
13321 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
13322 "CompareStub_%s%s%s%s",
13323 cc_name,
13324 strict_name,
13325 never_nan_nan_name,
13326 include_number_compare_name);
13327 return name_;
13328 }
13329
13330
13331 // -------------------------------------------------------------------------
13332 // StringCharCodeAtGenerator
13333
13334 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
13335 Label flat_string;
13336 Label ascii_string;
13337 Label got_char_code;
13338
13339 // If the receiver is a smi trigger the non-string case.
13340 STATIC_ASSERT(kSmiTag == 0);
13341 __ test(object_, Immediate(kSmiTagMask));
13342 __ j(zero, receiver_not_string_);
13343
13344 // Fetch the instance type of the receiver into result register.
13345 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
13346 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
13347 // If the receiver is not a string trigger the non-string case.
13348 __ test(result_, Immediate(kIsNotStringMask));
13349 __ j(not_zero, receiver_not_string_);
13350
13351 // If the index is non-smi trigger the non-smi case.
13352 STATIC_ASSERT(kSmiTag == 0);
13353 __ test(index_, Immediate(kSmiTagMask));
13354 __ j(not_zero, &index_not_smi_);
13355
13356 // Put smi-tagged index into scratch register.
13357 __ mov(scratch_, index_);
13358 __ bind(&got_smi_index_);
13359
13360 // Check for index out of range.
13361 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
13362 __ j(above_equal, index_out_of_range_);
13363
13364 // We need special handling for non-flat strings.
13365 STATIC_ASSERT(kSeqStringTag == 0);
13366 __ test(result_, Immediate(kStringRepresentationMask));
13367 __ j(zero, &flat_string);
13368
13369 // Handle non-flat strings.
13370 __ test(result_, Immediate(kIsConsStringMask));
13371 __ j(zero, &call_runtime_);
13372
13373 // ConsString.
13374 // Check whether the right hand side is the empty string (i.e. if
13375 // this is really a flat string in a cons string). If that is not
13376 // the case we would rather go to the runtime system now to flatten
13377 // the string.
13378 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
13379 Immediate(Factory::empty_string()));
13380 __ j(not_equal, &call_runtime_);
13381 // Get the first of the two strings and load its instance type.
13382 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
13383 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
13384 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
13385 // If the first cons component is also non-flat, then go to runtime.
13386 STATIC_ASSERT(kSeqStringTag == 0);
13387 __ test(result_, Immediate(kStringRepresentationMask));
13388 __ j(not_zero, &call_runtime_);
13389
13390 // Check for 1-byte or 2-byte string.
13391 __ bind(&flat_string);
13392 STATIC_ASSERT(kAsciiStringTag != 0);
13393 __ test(result_, Immediate(kStringEncodingMask));
13394 __ j(not_zero, &ascii_string);
13395
13396 // 2-byte string.
13397 // Load the 2-byte character code into the result register.
13398 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
13399 __ movzx_w(result_, FieldOperand(object_,
13400 scratch_, times_1, // Scratch is smi-tagged.
13401 SeqTwoByteString::kHeaderSize));
13402 __ jmp(&got_char_code);
13403
13404 // ASCII string.
13405 // Load the byte into the result register.
13406 __ bind(&ascii_string);
13407 __ SmiUntag(scratch_);
13408 __ movzx_b(result_, FieldOperand(object_,
13409 scratch_, times_1,
13410 SeqAsciiString::kHeaderSize));
13411 __ bind(&got_char_code);
13412 __ SmiTag(result_);
13413 __ bind(&exit_);
13414 }
13415
13416
13417 void StringCharCodeAtGenerator::GenerateSlow(
13418 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
13419 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
13420
13421 // Index is not a smi.
13422 __ bind(&index_not_smi_);
13423 // If index is a heap number, try converting it to an integer.
13424 __ CheckMap(index_, Factory::heap_number_map(), index_not_number_, true);
13425 call_helper.BeforeCall(masm);
13426 __ push(object_);
13427 __ push(index_);
13428 __ push(index_); // Consumed by runtime conversion function.
13429 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
13430 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
13431 } else {
13432 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
13433 // NumberToSmi discards numbers that are not exact integers.
13434 __ CallRuntime(Runtime::kNumberToSmi, 1);
13435 }
13436 if (!scratch_.is(eax)) {
13437 // Save the conversion result before the pop instructions below
13438 // have a chance to overwrite it.
13439 __ mov(scratch_, eax);
13440 }
13441 __ pop(index_);
13442 __ pop(object_);
13443 // Reload the instance type.
13444 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
13445 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
13446 call_helper.AfterCall(masm);
13447 // If index is still not a smi, it must be out of range.
13448 STATIC_ASSERT(kSmiTag == 0);
13449 __ test(scratch_, Immediate(kSmiTagMask));
13450 __ j(not_zero, index_out_of_range_);
13451 // Otherwise, return to the fast path.
13452 __ jmp(&got_smi_index_);
13453
13454 // Call runtime. We get here when the receiver is a string and the
13455 // index is a number, but the code of getting the actual character
13456 // is too complex (e.g., when the string needs to be flattened).
13457 __ bind(&call_runtime_);
13458 call_helper.BeforeCall(masm);
13459 __ push(object_);
13460 __ push(index_);
13461 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
13462 if (!result_.is(eax)) {
13463 __ mov(result_, eax);
13464 }
13465 call_helper.AfterCall(masm);
13466 __ jmp(&exit_);
13467
13468 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
13469 }
13470
13471
13472 // -------------------------------------------------------------------------
13473 // StringCharFromCodeGenerator
13474
13475 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
13476 // Fast case of Heap::LookupSingleCharacterStringFromCode.
13477 STATIC_ASSERT(kSmiTag == 0);
13478 STATIC_ASSERT(kSmiShiftSize == 0);
13479 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
13480 __ test(code_,
13481 Immediate(kSmiTagMask |
13482 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
13483 __ j(not_zero, &slow_case_, not_taken);
13484
13485 __ Set(result_, Immediate(Factory::single_character_string_cache()));
13486 STATIC_ASSERT(kSmiTag == 0);
13487 STATIC_ASSERT(kSmiTagSize == 1);
13488 STATIC_ASSERT(kSmiShiftSize == 0);
13489 // At this point code register contains smi tagged ascii char code.
13490 __ mov(result_, FieldOperand(result_,
13491 code_, times_half_pointer_size,
13492 FixedArray::kHeaderSize));
13493 __ cmp(result_, Factory::undefined_value());
13494 __ j(equal, &slow_case_, not_taken);
13495 __ bind(&exit_);
13496 }
13497
13498
13499 void StringCharFromCodeGenerator::GenerateSlow(
13500 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
13501 __ Abort("Unexpected fallthrough to CharFromCode slow case");
13502
13503 __ bind(&slow_case_);
13504 call_helper.BeforeCall(masm);
13505 __ push(code_);
13506 __ CallRuntime(Runtime::kCharFromCode, 1);
13507 if (!result_.is(eax)) {
13508 __ mov(result_, eax);
13509 }
13510 call_helper.AfterCall(masm);
13511 __ jmp(&exit_);
13512
13513 __ Abort("Unexpected fallthrough from CharFromCode slow case");
13514 }
13515
13516
13517 // -------------------------------------------------------------------------
13518 // StringCharAtGenerator
13519
13520 void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
13521 char_code_at_generator_.GenerateFast(masm);
13522 char_from_code_generator_.GenerateFast(masm);
13523 }
13524
13525
13526 void StringCharAtGenerator::GenerateSlow(
13527 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
13528 char_code_at_generator_.GenerateSlow(masm, call_helper);
13529 char_from_code_generator_.GenerateSlow(masm, call_helper);
13530 }
13531
13532
13533 void StringAddStub::Generate(MacroAssembler* masm) {
13534 Label string_add_runtime;
13535
13536 // Load the two arguments.
13537 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
13538 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
13539
13540 // Make sure that both arguments are strings if not known in advance.
13541 if (string_check_) {
13542 __ test(eax, Immediate(kSmiTagMask));
13543 __ j(zero, &string_add_runtime);
13544 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
13545 __ j(above_equal, &string_add_runtime);
13546
13547 // First argument is a a string, test second.
13548 __ test(edx, Immediate(kSmiTagMask));
13549 __ j(zero, &string_add_runtime);
13550 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
13551 __ j(above_equal, &string_add_runtime);
13552 }
13553
13554 // Both arguments are strings.
13555 // eax: first string
13556 // edx: second string
13557 // Check if either of the strings are empty. In that case return the other.
13558 Label second_not_zero_length, both_not_zero_length;
13559 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
13560 STATIC_ASSERT(kSmiTag == 0);
13561 __ test(ecx, Operand(ecx));
13562 __ j(not_zero, &second_not_zero_length);
13563 // Second string is empty, result is first string which is already in eax.
13564 __ IncrementCounter(&Counters::string_add_native, 1);
13565 __ ret(2 * kPointerSize);
13566 __ bind(&second_not_zero_length);
13567 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
13568 STATIC_ASSERT(kSmiTag == 0);
13569 __ test(ebx, Operand(ebx));
13570 __ j(not_zero, &both_not_zero_length);
13571 // First string is empty, result is second string which is in edx.
13572 __ mov(eax, edx);
13573 __ IncrementCounter(&Counters::string_add_native, 1);
13574 __ ret(2 * kPointerSize);
13575
13576 // Both strings are non-empty.
13577 // eax: first string
13578 // ebx: length of first string as a smi
13579 // ecx: length of second string as a smi
13580 // edx: second string
13581 // Look at the length of the result of adding the two strings.
13582 Label string_add_flat_result, longer_than_two;
13583 __ bind(&both_not_zero_length);
13584 __ add(ebx, Operand(ecx));
13585 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
13586 // Handle exceptionally long strings in the runtime system.
13587 __ j(overflow, &string_add_runtime);
13588 // Use the runtime system when adding two one character strings, as it
13589 // contains optimizations for this specific case using the symbol table.
13590 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
13591 __ j(not_equal, &longer_than_two);
13592
13593 // Check that both strings are non-external ascii strings.
13594 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
13595 &string_add_runtime);
13596
13597 // Get the two characters forming the sub string.
13598 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
13599 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
13600
13601 // Try to lookup two character string in symbol table. If it is not found
13602 // just allocate a new one.
13603 Label make_two_character_string, make_flat_ascii_string;
13604 StringHelper::GenerateTwoCharacterSymbolTableProbe(
13605 masm, ebx, ecx, eax, edx, edi, &make_two_character_string);
13606 __ IncrementCounter(&Counters::string_add_native, 1);
13607 __ ret(2 * kPointerSize);
13608
13609 __ bind(&make_two_character_string);
13610 __ Set(ebx, Immediate(Smi::FromInt(2)));
13611 __ jmp(&make_flat_ascii_string);
13612
13613 __ bind(&longer_than_two);
13614 // Check if resulting string will be flat.
13615 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
13616 __ j(below, &string_add_flat_result);
13617
13618 // If result is not supposed to be flat allocate a cons string object. If both
13619 // strings are ascii the result is an ascii cons string.
13620 Label non_ascii, allocated, ascii_data;
13621 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
13622 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
13623 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
13624 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
13625 __ and_(ecx, Operand(edi));
13626 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
13627 __ test(ecx, Immediate(kAsciiStringTag));
13628 __ j(zero, &non_ascii);
13629 __ bind(&ascii_data);
13630 // Allocate an acsii cons string.
13631 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
13632 __ bind(&allocated);
13633 // Fill the fields of the cons string.
13634 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
13635 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
13636 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
13637 Immediate(String::kEmptyHashField));
13638 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
13639 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
13640 __ mov(eax, ecx);
13641 __ IncrementCounter(&Counters::string_add_native, 1);
13642 __ ret(2 * kPointerSize);
13643 __ bind(&non_ascii);
13644 // At least one of the strings is two-byte. Check whether it happens
13645 // to contain only ascii characters.
13646 // ecx: first instance type AND second instance type.
13647 // edi: second instance type.
13648 __ test(ecx, Immediate(kAsciiDataHintMask));
13649 __ j(not_zero, &ascii_data);
13650 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
13651 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
13652 __ xor_(edi, Operand(ecx));
13653 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
13654 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
13655 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
13656 __ j(equal, &ascii_data);
13657 // Allocate a two byte cons string.
13658 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
13659 __ jmp(&allocated);
13660
13661 // Handle creating a flat result. First check that both strings are not
13662 // external strings.
13663 // eax: first string
13664 // ebx: length of resulting flat string as a smi
13665 // edx: second string
13666 __ bind(&string_add_flat_result);
13667 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
13668 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
13669 __ and_(ecx, kStringRepresentationMask);
13670 __ cmp(ecx, kExternalStringTag);
13671 __ j(equal, &string_add_runtime);
13672 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
13673 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
13674 __ and_(ecx, kStringRepresentationMask);
13675 __ cmp(ecx, kExternalStringTag);
13676 __ j(equal, &string_add_runtime);
13677 // Now check if both strings are ascii strings.
13678 // eax: first string
13679 // ebx: length of resulting flat string as a smi
13680 // edx: second string
13681 Label non_ascii_string_add_flat_result;
13682 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
13683 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
13684 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
13685 __ j(zero, &non_ascii_string_add_flat_result);
13686 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
13687 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
13688 __ j(zero, &string_add_runtime);
13689
13690 __ bind(&make_flat_ascii_string);
13691 // Both strings are ascii strings. As they are short they are both flat.
13692 // ebx: length of resulting flat string as a smi
13693 __ SmiUntag(ebx);
13694 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
13695 // eax: result string
13696 __ mov(ecx, eax);
13697 // Locate first character of result.
13698 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
13699 // Load first argument and locate first character.
13700 __ mov(edx, Operand(esp, 2 * kPointerSize));
13701 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
13702 __ SmiUntag(edi);
13703 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
13704 // eax: result string
13705 // ecx: first character of result
13706 // edx: first char of first argument
13707 // edi: length of first argument
13708 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
13709 // Load second argument and locate first character.
13710 __ mov(edx, Operand(esp, 1 * kPointerSize));
13711 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
13712 __ SmiUntag(edi);
13713 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
13714 // eax: result string
13715 // ecx: next character of result
13716 // edx: first char of second argument
13717 // edi: length of second argument
13718 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
13719 __ IncrementCounter(&Counters::string_add_native, 1);
13720 __ ret(2 * kPointerSize);
13721
13722 // Handle creating a flat two byte result.
13723 // eax: first string - known to be two byte
13724 // ebx: length of resulting flat string as a smi
13725 // edx: second string
13726 __ bind(&non_ascii_string_add_flat_result);
13727 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
13728 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
13729 __ j(not_zero, &string_add_runtime);
13730 // Both strings are two byte strings. As they are short they are both
13731 // flat.
13732 __ SmiUntag(ebx);
13733 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
13734 // eax: result string
13735 __ mov(ecx, eax);
13736 // Locate first character of result.
13737 __ add(Operand(ecx),
13738 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
13739 // Load first argument and locate first character.
13740 __ mov(edx, Operand(esp, 2 * kPointerSize));
13741 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
13742 __ SmiUntag(edi);
13743 __ add(Operand(edx),
13744 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
13745 // eax: result string
13746 // ecx: first character of result
13747 // edx: first char of first argument
13748 // edi: length of first argument
13749 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
13750 // Load second argument and locate first character.
13751 __ mov(edx, Operand(esp, 1 * kPointerSize));
13752 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
13753 __ SmiUntag(edi);
13754 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
13755 // eax: result string
13756 // ecx: next character of result
13757 // edx: first char of second argument
13758 // edi: length of second argument
13759 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
13760 __ IncrementCounter(&Counters::string_add_native, 1);
13761 __ ret(2 * kPointerSize);
13762
13763 // Just jump to runtime to add the two strings.
13764 __ bind(&string_add_runtime);
13765 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
13766 }
13767
13768
13769 void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
13770 Register dest,
13771 Register src,
13772 Register count,
13773 Register scratch,
13774 bool ascii) {
13775 Label loop;
13776 __ bind(&loop);
13777 // This loop just copies one character at a time, as it is only used for very
13778 // short strings.
13779 if (ascii) {
13780 __ mov_b(scratch, Operand(src, 0));
13781 __ mov_b(Operand(dest, 0), scratch);
13782 __ add(Operand(src), Immediate(1));
13783 __ add(Operand(dest), Immediate(1));
13784 } else {
13785 __ mov_w(scratch, Operand(src, 0));
13786 __ mov_w(Operand(dest, 0), scratch);
13787 __ add(Operand(src), Immediate(2));
13788 __ add(Operand(dest), Immediate(2));
13789 }
13790 __ sub(Operand(count), Immediate(1));
13791 __ j(not_zero, &loop);
13792 }
13793
13794
13795 void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
13796 Register dest,
13797 Register src,
13798 Register count,
13799 Register scratch,
13800 bool ascii) {
13801 // Copy characters using rep movs of doublewords.
13802 // The destination is aligned on a 4 byte boundary because we are
13803 // copying to the beginning of a newly allocated string.
13804 ASSERT(dest.is(edi)); // rep movs destination
13805 ASSERT(src.is(esi)); // rep movs source
13806 ASSERT(count.is(ecx)); // rep movs count
13807 ASSERT(!scratch.is(dest));
13808 ASSERT(!scratch.is(src));
13809 ASSERT(!scratch.is(count));
13810
13811 // Nothing to do for zero characters.
13812 Label done;
13813 __ test(count, Operand(count));
13814 __ j(zero, &done);
13815
13816 // Make count the number of bytes to copy.
13817 if (!ascii) {
13818 __ shl(count, 1);
13819 }
13820
13821 // Don't enter the rep movs if there are less than 4 bytes to copy.
13822 Label last_bytes;
13823 __ test(count, Immediate(~3));
13824 __ j(zero, &last_bytes);
13825
13826 // Copy from edi to esi using rep movs instruction.
13827 __ mov(scratch, count);
13828 __ sar(count, 2); // Number of doublewords to copy.
13829 __ cld();
13830 __ rep_movs();
13831
13832 // Find number of bytes left.
13833 __ mov(count, scratch);
13834 __ and_(count, 3);
13835
13836 // Check if there are more bytes to copy.
13837 __ bind(&last_bytes);
13838 __ test(count, Operand(count));
13839 __ j(zero, &done);
13840
13841 // Copy remaining characters.
13842 Label loop;
13843 __ bind(&loop);
13844 __ mov_b(scratch, Operand(src, 0));
13845 __ mov_b(Operand(dest, 0), scratch);
13846 __ add(Operand(src), Immediate(1));
13847 __ add(Operand(dest), Immediate(1));
13848 __ sub(Operand(count), Immediate(1));
13849 __ j(not_zero, &loop);
13850
13851 __ bind(&done);
13852 }
13853
13854
13855 void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
13856 Register c1,
13857 Register c2,
13858 Register scratch1,
13859 Register scratch2,
13860 Register scratch3,
13861 Label* not_found) {
13862 // Register scratch3 is the general scratch register in this function.
13863 Register scratch = scratch3;
13864
13865 // Make sure that both characters are not digits as such strings has a
13866 // different hash algorithm. Don't try to look for these in the symbol table.
13867 Label not_array_index;
13868 __ mov(scratch, c1);
13869 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
13870 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
13871 __ j(above, &not_array_index);
13872 __ mov(scratch, c2);
13873 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
13874 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
13875 __ j(below_equal, not_found);
13876
13877 __ bind(&not_array_index);
13878 // Calculate the two character string hash.
13879 Register hash = scratch1;
13880 GenerateHashInit(masm, hash, c1, scratch);
13881 GenerateHashAddCharacter(masm, hash, c2, scratch);
13882 GenerateHashGetHash(masm, hash, scratch);
13883
13884 // Collect the two characters in a register.
13885 Register chars = c1;
13886 __ shl(c2, kBitsPerByte);
13887 __ or_(chars, Operand(c2));
13888
13889 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
13890 // hash: hash of two character string.
13891
13892 // Load the symbol table.
13893 Register symbol_table = c2;
13894 ExternalReference roots_address = ExternalReference::roots_address();
13895 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
13896 __ mov(symbol_table,
13897 Operand::StaticArray(scratch, times_pointer_size, roots_address));
13898
13899 // Calculate capacity mask from the symbol table capacity.
13900 Register mask = scratch2;
13901 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
13902 __ SmiUntag(mask);
13903 __ sub(Operand(mask), Immediate(1));
13904
13905 // Registers
13906 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
13907 // hash: hash of two character string
13908 // symbol_table: symbol table
13909 // mask: capacity mask
13910 // scratch: -
13911
13912 // Perform a number of probes in the symbol table.
13913 static const int kProbes = 4;
13914 Label found_in_symbol_table;
13915 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
13916 for (int i = 0; i < kProbes; i++) {
13917 // Calculate entry in symbol table.
13918 __ mov(scratch, hash);
13919 if (i > 0) {
13920 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
13921 }
13922 __ and_(scratch, Operand(mask));
13923
13924 // Load the entry from the symbol table.
13925 Register candidate = scratch; // Scratch register contains candidate.
13926 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
13927 __ mov(candidate,
13928 FieldOperand(symbol_table,
13929 scratch,
13930 times_pointer_size,
13931 SymbolTable::kElementsStartOffset));
13932
13933 // If entry is undefined no string with this hash can be found.
13934 __ cmp(candidate, Factory::undefined_value());
13935 __ j(equal, not_found);
13936
13937 // If length is not 2 the string is not a candidate.
13938 __ cmp(FieldOperand(candidate, String::kLengthOffset),
13939 Immediate(Smi::FromInt(2)));
13940 __ j(not_equal, &next_probe[i]);
13941
13942 // As we are out of registers save the mask on the stack and use that
13943 // register as a temporary.
13944 __ push(mask);
13945 Register temp = mask;
13946
13947 // Check that the candidate is a non-external ascii string.
13948 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
13949 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
13950 __ JumpIfInstanceTypeIsNotSequentialAscii(
13951 temp, temp, &next_probe_pop_mask[i]);
13952
13953 // Check if the two characters match.
13954 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
13955 __ and_(temp, 0x0000ffff);
13956 __ cmp(chars, Operand(temp));
13957 __ j(equal, &found_in_symbol_table);
13958 __ bind(&next_probe_pop_mask[i]);
13959 __ pop(mask);
13960 __ bind(&next_probe[i]);
13961 }
13962
13963 // No matching 2 character string found by probing.
13964 __ jmp(not_found);
13965
13966 // Scratch register contains result when we fall through to here.
13967 Register result = scratch;
13968 __ bind(&found_in_symbol_table);
13969 __ pop(mask); // Pop saved mask from the stack.
13970 if (!result.is(eax)) {
13971 __ mov(eax, result);
13972 }
13973 }
13974
13975
13976 void StringHelper::GenerateHashInit(MacroAssembler* masm,
13977 Register hash,
13978 Register character,
13979 Register scratch) {
13980 // hash = character + (character << 10);
13981 __ mov(hash, character);
13982 __ shl(hash, 10);
13983 __ add(hash, Operand(character));
13984 // hash ^= hash >> 6;
13985 __ mov(scratch, hash);
13986 __ sar(scratch, 6);
13987 __ xor_(hash, Operand(scratch));
13988 }
13989
13990
13991 void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
13992 Register hash,
13993 Register character,
13994 Register scratch) {
13995 // hash += character;
13996 __ add(hash, Operand(character));
13997 // hash += hash << 10;
13998 __ mov(scratch, hash);
13999 __ shl(scratch, 10);
14000 __ add(hash, Operand(scratch));
14001 // hash ^= hash >> 6;
14002 __ mov(scratch, hash);
14003 __ sar(scratch, 6);
14004 __ xor_(hash, Operand(scratch));
14005 }
14006
14007
14008 void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
14009 Register hash,
14010 Register scratch) {
14011 // hash += hash << 3;
14012 __ mov(scratch, hash);
14013 __ shl(scratch, 3);
14014 __ add(hash, Operand(scratch));
14015 // hash ^= hash >> 11;
14016 __ mov(scratch, hash);
14017 __ sar(scratch, 11);
14018 __ xor_(hash, Operand(scratch));
14019 // hash += hash << 15;
14020 __ mov(scratch, hash);
14021 __ shl(scratch, 15);
14022 __ add(hash, Operand(scratch));
14023
14024 // if (hash == 0) hash = 27;
14025 Label hash_not_zero;
14026 __ test(hash, Operand(hash));
14027 __ j(not_zero, &hash_not_zero);
14028 __ mov(hash, Immediate(27));
14029 __ bind(&hash_not_zero);
14030 }
14031
14032
14033 void SubStringStub::Generate(MacroAssembler* masm) {
14034 Label runtime;
14035
14036 // Stack frame on entry.
14037 // esp[0]: return address
14038 // esp[4]: to
14039 // esp[8]: from
14040 // esp[12]: string
14041
14042 // Make sure first argument is a string.
14043 __ mov(eax, Operand(esp, 3 * kPointerSize));
14044 STATIC_ASSERT(kSmiTag == 0);
14045 __ test(eax, Immediate(kSmiTagMask));
14046 __ j(zero, &runtime);
14047 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
14048 __ j(NegateCondition(is_string), &runtime);
14049
14050 // eax: string
14051 // ebx: instance type
14052
14053 // Calculate length of sub string using the smi values.
14054 Label result_longer_than_two;
14055 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
14056 __ test(ecx, Immediate(kSmiTagMask));
14057 __ j(not_zero, &runtime);
14058 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
14059 __ test(edx, Immediate(kSmiTagMask));
14060 __ j(not_zero, &runtime);
14061 __ sub(ecx, Operand(edx));
14062 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
14063 Label return_eax;
14064 __ j(equal, &return_eax);
14065 // Special handling of sub-strings of length 1 and 2. One character strings
14066 // are handled in the runtime system (looked up in the single character
14067 // cache). Two character strings are looked for in the symbol cache.
14068 __ SmiUntag(ecx); // Result length is no longer smi.
14069 __ cmp(ecx, 2);
14070 __ j(greater, &result_longer_than_two);
14071 __ j(less, &runtime);
14072
14073 // Sub string of length 2 requested.
14074 // eax: string
14075 // ebx: instance type
14076 // ecx: sub string length (value is 2)
14077 // edx: from index (smi)
14078 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
14079
14080 // Get the two characters forming the sub string.
14081 __ SmiUntag(edx); // From index is no longer smi.
14082 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
14083 __ movzx_b(ecx,
14084 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
14085
14086 // Try to lookup two character string in symbol table.
14087 Label make_two_character_string;
14088 StringHelper::GenerateTwoCharacterSymbolTableProbe(
14089 masm, ebx, ecx, eax, edx, edi, &make_two_character_string);
14090 __ ret(3 * kPointerSize);
14091
14092 __ bind(&make_two_character_string);
14093 // Setup registers for allocating the two character string.
14094 __ mov(eax, Operand(esp, 3 * kPointerSize));
14095 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
14096 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
14097 __ Set(ecx, Immediate(2));
14098
14099 __ bind(&result_longer_than_two);
14100 // eax: string
14101 // ebx: instance type
14102 // ecx: result string length
14103 // Check for flat ascii string
14104 Label non_ascii_flat;
14105 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
14106
14107 // Allocate the result.
14108 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
14109
14110 // eax: result string
14111 // ecx: result string length
14112 __ mov(edx, esi); // esi used by following code.
14113 // Locate first character of result.
14114 __ mov(edi, eax);
14115 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
14116 // Load string argument and locate character of sub string start.
14117 __ mov(esi, Operand(esp, 3 * kPointerSize));
14118 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
14119 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
14120 __ SmiUntag(ebx);
14121 __ add(esi, Operand(ebx));
14122
14123 // eax: result string
14124 // ecx: result length
14125 // edx: original value of esi
14126 // edi: first character of result
14127 // esi: character of sub string start
14128 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
14129 __ mov(esi, edx); // Restore esi.
14130 __ IncrementCounter(&Counters::sub_string_native, 1);
14131 __ ret(3 * kPointerSize);
14132
14133 __ bind(&non_ascii_flat);
14134 // eax: string
14135 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
14136 // ecx: result string length
14137 // Check for flat two byte string
14138 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
14139 __ j(not_equal, &runtime);
14140
14141 // Allocate the result.
14142 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
14143
14144 // eax: result string
14145 // ecx: result string length
14146 __ mov(edx, esi); // esi used by following code.
14147 // Locate first character of result.
14148 __ mov(edi, eax);
14149 __ add(Operand(edi),
14150 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
14151 // Load string argument and locate character of sub string start.
14152 __ mov(esi, Operand(esp, 3 * kPointerSize));
14153 __ add(Operand(esi),
14154 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
14155 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
14156 // As from is a smi it is 2 times the value which matches the size of a two
14157 // byte character.
14158 STATIC_ASSERT(kSmiTag == 0);
14159 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
14160 __ add(esi, Operand(ebx));
14161
14162 // eax: result string
14163 // ecx: result length
14164 // edx: original value of esi
14165 // edi: first character of result
14166 // esi: character of sub string start
14167 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
14168 __ mov(esi, edx); // Restore esi.
14169
14170 __ bind(&return_eax);
14171 __ IncrementCounter(&Counters::sub_string_native, 1);
14172 __ ret(3 * kPointerSize);
14173
14174 // Just jump to runtime to create the sub string.
14175 __ bind(&runtime);
14176 __ TailCallRuntime(Runtime::kSubString, 3, 1);
14177 }
14178
14179
14180 void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
14181 Register left,
14182 Register right,
14183 Register scratch1,
14184 Register scratch2,
14185 Register scratch3) {
14186 Label result_not_equal;
14187 Label result_greater;
14188 Label compare_lengths;
14189
14190 __ IncrementCounter(&Counters::string_compare_native, 1);
14191
14192 // Find minimum length.
14193 Label left_shorter;
14194 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
14195 __ mov(scratch3, scratch1);
14196 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
14197
14198 Register length_delta = scratch3;
14199
14200 __ j(less_equal, &left_shorter);
14201 // Right string is shorter. Change scratch1 to be length of right string.
14202 __ sub(scratch1, Operand(length_delta));
14203 __ bind(&left_shorter);
14204
14205 Register min_length = scratch1;
14206
14207 // If either length is zero, just compare lengths.
14208 __ test(min_length, Operand(min_length));
14209 __ j(zero, &compare_lengths);
14210
14211 // Change index to run from -min_length to -1 by adding min_length
14212 // to string start. This means that loop ends when index reaches zero,
14213 // which doesn't need an additional compare.
14214 __ SmiUntag(min_length);
14215 __ lea(left,
14216 FieldOperand(left,
14217 min_length, times_1,
14218 SeqAsciiString::kHeaderSize));
14219 __ lea(right,
14220 FieldOperand(right,
14221 min_length, times_1,
14222 SeqAsciiString::kHeaderSize));
14223 __ neg(min_length);
14224
14225 Register index = min_length; // index = -min_length;
14226
14227 {
14228 // Compare loop.
14229 Label loop;
14230 __ bind(&loop);
14231 // Compare characters.
14232 __ mov_b(scratch2, Operand(left, index, times_1, 0));
14233 __ cmpb(scratch2, Operand(right, index, times_1, 0));
14234 __ j(not_equal, &result_not_equal);
14235 __ add(Operand(index), Immediate(1));
14236 __ j(not_zero, &loop);
14237 }
14238
14239 // Compare lengths - strings up to min-length are equal.
14240 __ bind(&compare_lengths);
14241 __ test(length_delta, Operand(length_delta));
14242 __ j(not_zero, &result_not_equal);
14243
14244 // Result is EQUAL.
14245 STATIC_ASSERT(EQUAL == 0);
14246 STATIC_ASSERT(kSmiTag == 0);
14247 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
14248 __ ret(0);
14249
14250 __ bind(&result_not_equal);
14251 __ j(greater, &result_greater);
14252
14253 // Result is LESS.
14254 __ Set(eax, Immediate(Smi::FromInt(LESS)));
14255 __ ret(0);
14256
14257 // Result is GREATER.
14258 __ bind(&result_greater);
14259 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
14260 __ ret(0);
14261 }
14262
14263
14264 void StringCompareStub::Generate(MacroAssembler* masm) {
14265 Label runtime;
14266
14267 // Stack frame on entry.
14268 // esp[0]: return address
14269 // esp[4]: right string
14270 // esp[8]: left string
14271
14272 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
14273 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
14274
14275 Label not_same;
14276 __ cmp(edx, Operand(eax));
14277 __ j(not_equal, &not_same);
14278 STATIC_ASSERT(EQUAL == 0);
14279 STATIC_ASSERT(kSmiTag == 0);
14280 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
14281 __ IncrementCounter(&Counters::string_compare_native, 1);
14282 __ ret(2 * kPointerSize);
14283
14284 __ bind(&not_same);
14285
14286 // Check that both objects are sequential ascii strings.
14287 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
14288
14289 // Compare flat ascii strings.
14290 // Drop arguments from the stack.
14291 __ pop(ecx);
14292 __ add(Operand(esp), Immediate(2 * kPointerSize));
14293 __ push(ecx);
14294 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
14295
14296 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
14297 // tagged as a small integer.
14298 __ bind(&runtime);
14299 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
14300 }
14301
14302 #undef __ 9808 #undef __
14303 9809
14304 #define __ masm. 9810 #define __ masm.
14305 9811
14306 MemCopyFunction CreateMemCopyFunction() { 9812 MemCopyFunction CreateMemCopyFunction() {
14307 size_t actual_size; 9813 size_t actual_size;
14308 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, 9814 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
14309 &actual_size, 9815 &actual_size,
14310 true)); 9816 true));
14311 CHECK(buffer); 9817 CHECK(buffer);
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
14502 masm.GetCode(&desc); 10008 masm.GetCode(&desc);
14503 // Call the function from C++. 10009 // Call the function from C++.
14504 return FUNCTION_CAST<MemCopyFunction>(buffer); 10010 return FUNCTION_CAST<MemCopyFunction>(buffer);
14505 } 10011 }
14506 10012
14507 #undef __ 10013 #undef __
14508 10014
14509 } } // namespace v8::internal 10015 } } // namespace v8::internal
14510 10016
14511 #endif // V8_TARGET_ARCH_IA32 10017 #endif // V8_TARGET_ARCH_IA32
OLDNEW
« no previous file with comments | « src/ia32/codegen-ia32.h ('k') | src/ia32/full-codegen-ia32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698