Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 30 | #if defined(V8_TARGET_ARCH_ARM) |
| 31 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
| 33 | #include "codegen-inl.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 34 | #include "compiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "debug.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 36 | #include "ic-inl.h" |
| 37 | #include "jsregexp.h" |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 38 | #include "jump-target-light-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | #include "parser.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 40 | #include "regexp-macro-assembler.h" |
| 41 | #include "regexp-stack.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | #include "register-allocator-inl.h" |
| 43 | #include "runtime.h" |
| 44 | #include "scopes.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 45 | #include "virtual-frame-inl.h" |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 46 | #include "virtual-frame-arm-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | |
| 48 | namespace v8 { |
| 49 | namespace internal { |
| 50 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 51 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 53 | Label* slow, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 54 | Condition cc, |
| 55 | bool never_nan_nan); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 57 | Register lhs, |
| 58 | Register rhs, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 59 | Label* lhs_not_nan, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | Label* slow, |
| 61 | bool strict); |
| 62 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 63 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm, |
| 64 | Register lhs, |
| 65 | Register rhs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 67 | Register source, |
| 68 | Register destination, |
| 69 | int known_int); |
| 70 | static bool IsEasyToMultiplyBy(int x); |
| 71 | |
| 72 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 73 | #define __ ACCESS_MASM(masm_) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | |
| 75 | // ------------------------------------------------------------------------- |
| 76 | // Platform-specific DeferredCode functions. |
| 77 | |
| 78 | void DeferredCode::SaveRegisters() { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 79 | // On ARM you either have a completely spilled frame or you |
| 80 | // handle it yourself, but at the moment there's no automation |
| 81 | // of registers and deferred code. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | void DeferredCode::RestoreRegisters() { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| 89 | // ------------------------------------------------------------------------- |
| 90 | // Platform-specific RuntimeCallHelper functions. |
| 91 | |
| 92 | void VirtualFrameRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const { |
| 93 | frame_state_->frame()->AssertIsSpilled(); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | void VirtualFrameRuntimeCallHelper::AfterCall(MacroAssembler* masm) const { |
| 98 | } |
| 99 | |
| 100 | |
| 101 | void ICRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const { |
| 102 | masm->EnterInternalFrame(); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void ICRuntimeCallHelper::AfterCall(MacroAssembler* masm) const { |
| 107 | masm->LeaveInternalFrame(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
| 111 | // ------------------------------------------------------------------------- |
| 112 | // CodeGenState implementation. |
| 113 | |
| 114 | CodeGenState::CodeGenState(CodeGenerator* owner) |
| 115 | : owner_(owner), |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 116 | previous_(owner->state()) { |
| 117 | owner->set_state(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 121 | ConditionCodeGenState::ConditionCodeGenState(CodeGenerator* owner, |
| 122 | JumpTarget* true_target, |
| 123 | JumpTarget* false_target) |
| 124 | : CodeGenState(owner), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | true_target_(true_target), |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 126 | false_target_(false_target) { |
| 127 | owner->set_state(this); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | TypeInfoCodeGenState::TypeInfoCodeGenState(CodeGenerator* owner, |
| 132 | Slot* slot, |
| 133 | TypeInfo type_info) |
| 134 | : CodeGenState(owner), |
| 135 | slot_(slot) { |
| 136 | owner->set_state(this); |
| 137 | old_type_info_ = owner->set_type_info(slot, type_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | CodeGenState::~CodeGenState() { |
| 142 | ASSERT(owner_->state() == this); |
| 143 | owner_->set_state(previous_); |
| 144 | } |
| 145 | |
| 146 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 147 | TypeInfoCodeGenState::~TypeInfoCodeGenState() { |
| 148 | owner()->set_type_info(slot_, old_type_info_); |
| 149 | } |
| 150 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 151 | // ------------------------------------------------------------------------- |
| 152 | // CodeGenerator implementation |
| 153 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 154 | int CodeGenerator::inlined_write_barrier_size_ = -1; |
| 155 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 156 | CodeGenerator::CodeGenerator(MacroAssembler* masm) |
| 157 | : deferred_(8), |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 158 | masm_(masm), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 159 | info_(NULL), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | frame_(NULL), |
| 161 | allocator_(NULL), |
| 162 | cc_reg_(al), |
| 163 | state_(NULL), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 164 | loop_nesting_(0), |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 165 | type_info_(NULL), |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 166 | function_return_(JumpTarget::BIDIRECTIONAL), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | function_return_is_shadowed_(false) { |
| 168 | } |
| 169 | |
| 170 | |
| 171 | // Calling conventions: |
| 172 | // fp: caller's frame pointer |
| 173 | // sp: stack pointer |
| 174 | // r1: called JS function |
| 175 | // cp: callee's context |
| 176 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 177 | void CodeGenerator::Generate(CompilationInfo* info) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 178 | // Record the position for debugging purposes. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 179 | CodeForFunctionPosition(info->function()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 180 | Comment cmnt(masm_, "[ function compiled by virtual frame code generator"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | |
| 182 | // Initialize state. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 183 | info_ = info; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 184 | |
| 185 | int slots = scope()->num_parameters() + scope()->num_stack_slots(); |
| 186 | ScopedVector<TypeInfo> type_info_array(slots); |
| 187 | type_info_ = &type_info_array; |
| 188 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 189 | ASSERT(allocator_ == NULL); |
| 190 | RegisterAllocator register_allocator(this); |
| 191 | allocator_ = ®ister_allocator; |
| 192 | ASSERT(frame_ == NULL); |
| 193 | frame_ = new VirtualFrame(); |
| 194 | cc_reg_ = al; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 195 | |
| 196 | // Adjust for function-level loop nesting. |
| 197 | ASSERT_EQ(0, loop_nesting_); |
| 198 | loop_nesting_ = info->loop_nesting(); |
| 199 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | { |
| 201 | CodeGenState state(this); |
| 202 | |
| 203 | // Entry: |
| 204 | // Stack: receiver, arguments |
| 205 | // lr: return address |
| 206 | // fp: caller's frame pointer |
| 207 | // sp: stack pointer |
| 208 | // r1: called JS function |
| 209 | // cp: callee's context |
| 210 | allocator_->Initialize(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 211 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | #ifdef DEBUG |
| 213 | if (strlen(FLAG_stop_at) > 0 && |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 214 | info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | frame_->SpillAll(); |
| 216 | __ stop("stop-at"); |
| 217 | } |
| 218 | #endif |
| 219 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 220 | frame_->Enter(); |
| 221 | // tos: code slot |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 222 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 223 | // Allocate space for locals and initialize them. This also checks |
| 224 | // for stack overflow. |
| 225 | frame_->AllocateStackSlots(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 226 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 227 | frame_->AssertIsSpilled(); |
| 228 | int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS; |
| 229 | if (heap_slots > 0) { |
| 230 | // Allocate local context. |
| 231 | // Get outer context and create a new context based on it. |
| 232 | __ ldr(r0, frame_->Function()); |
| 233 | frame_->EmitPush(r0); |
| 234 | if (heap_slots <= FastNewContextStub::kMaximumSlots) { |
| 235 | FastNewContextStub stub(heap_slots); |
| 236 | frame_->CallStub(&stub, 1); |
| 237 | } else { |
| 238 | frame_->CallRuntime(Runtime::kNewContext, 1); |
| 239 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 240 | |
| 241 | #ifdef DEBUG |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 242 | JumpTarget verified_true; |
| 243 | __ cmp(r0, cp); |
| 244 | verified_true.Branch(eq); |
| 245 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 246 | verified_true.Bind(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 247 | #endif |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 248 | // Update context local. |
| 249 | __ str(cp, frame_->Context()); |
| 250 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 251 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 252 | // TODO(1241774): Improve this code: |
| 253 | // 1) only needed if we have a context |
| 254 | // 2) no need to recompute context ptr every single time |
| 255 | // 3) don't copy parameter operand code from SlotOperand! |
| 256 | { |
| 257 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 258 | // Note that iteration order is relevant here! If we have the same |
| 259 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 260 | // needs to be copied into the context, it must be the last argument |
| 261 | // passed to the parameter that needs to be copied. This is a rare |
| 262 | // case so we don't check for it, instead we rely on the copying |
| 263 | // order: such a parameter is copied repeatedly into the same |
| 264 | // context location and thus the last value is what is seen inside |
| 265 | // the function. |
| 266 | frame_->AssertIsSpilled(); |
| 267 | for (int i = 0; i < scope()->num_parameters(); i++) { |
| 268 | Variable* par = scope()->parameter(i); |
| 269 | Slot* slot = par->slot(); |
| 270 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
| 271 | ASSERT(!scope()->is_global_scope()); // No params in global scope. |
| 272 | __ ldr(r1, frame_->ParameterAt(i)); |
| 273 | // Loads r2 with context; used below in RecordWrite. |
| 274 | __ str(r1, SlotOperand(slot, r2)); |
| 275 | // Load the offset into r3. |
| 276 | int slot_offset = |
| 277 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 278 | __ RecordWrite(r2, Operand(slot_offset), r3, r1); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 281 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 282 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 283 | // Store the arguments object. This must happen after context |
| 284 | // initialization because the arguments object may be stored in |
| 285 | // the context. |
| 286 | if (ArgumentsMode() != NO_ARGUMENTS_ALLOCATION) { |
| 287 | StoreArgumentsObject(true); |
| 288 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 289 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 290 | // Initialize ThisFunction reference if present. |
| 291 | if (scope()->is_function_scope() && scope()->function() != NULL) { |
| 292 | frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex); |
| 293 | StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | // Initialize the function return target after the locals are set |
| 297 | // up, because it needs the expected frame height from the frame. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 298 | function_return_.SetExpectedHeight(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 299 | function_return_is_shadowed_ = false; |
| 300 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 301 | // Generate code to 'execute' declarations and initialize functions |
| 302 | // (source elements). In case of an illegal redeclaration we need to |
| 303 | // handle that instead of processing the declarations. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 304 | if (scope()->HasIllegalRedeclaration()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | Comment cmnt(masm_, "[ illegal redeclarations"); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 306 | scope()->VisitIllegalRedeclaration(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 307 | } else { |
| 308 | Comment cmnt(masm_, "[ declarations"); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 309 | ProcessDeclarations(scope()->declarations()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | // Bail out if a stack-overflow exception occurred when processing |
| 311 | // declarations. |
| 312 | if (HasStackOverflow()) return; |
| 313 | } |
| 314 | |
| 315 | if (FLAG_trace) { |
| 316 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
| 317 | // Ignore the return value. |
| 318 | } |
| 319 | |
| 320 | // Compile the body of the function in a vanilla state. Don't |
| 321 | // bother compiling all the code if the scope has an illegal |
| 322 | // redeclaration. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 323 | if (!scope()->HasIllegalRedeclaration()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 324 | Comment cmnt(masm_, "[ function body"); |
| 325 | #ifdef DEBUG |
| 326 | bool is_builtin = Bootstrapper::IsActive(); |
| 327 | bool should_trace = |
| 328 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
| 329 | if (should_trace) { |
| 330 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
| 331 | // Ignore the return value. |
| 332 | } |
| 333 | #endif |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 334 | VisitStatements(info->function()->body()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 338 | // Handle the return from the function. |
| 339 | if (has_valid_frame()) { |
| 340 | // If there is a valid frame, control flow can fall off the end of |
| 341 | // the body. In that case there is an implicit return statement. |
| 342 | ASSERT(!function_return_is_shadowed_); |
| 343 | frame_->PrepareForReturn(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 345 | if (function_return_.is_bound()) { |
| 346 | function_return_.Jump(); |
| 347 | } else { |
| 348 | function_return_.Bind(); |
| 349 | GenerateReturnSequence(); |
| 350 | } |
| 351 | } else if (function_return_.is_linked()) { |
| 352 | // If the return target has dangling jumps to it, then we have not |
| 353 | // yet generated the return sequence. This can happen when (a) |
| 354 | // control does not flow off the end of the body so we did not |
| 355 | // compile an artificial return statement just above, and (b) there |
| 356 | // are return statements in the body but (c) they are all shadowed. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | function_return_.Bind(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 358 | GenerateReturnSequence(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 361 | // Adjust for function-level loop nesting. |
| 362 | ASSERT(loop_nesting_ == info->loop_nesting()); |
| 363 | loop_nesting_ = 0; |
| 364 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 365 | // Code generation state must be reset. |
| 366 | ASSERT(!has_cc()); |
| 367 | ASSERT(state_ == NULL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 368 | ASSERT(loop_nesting() == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | ASSERT(!function_return_is_shadowed_); |
| 370 | function_return_.Unuse(); |
| 371 | DeleteFrame(); |
| 372 | |
| 373 | // Process any deferred code using the register allocator. |
| 374 | if (!HasStackOverflow()) { |
| 375 | ProcessDeferred(); |
| 376 | } |
| 377 | |
| 378 | allocator_ = NULL; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 379 | type_info_ = NULL; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | int CodeGenerator::NumberOfSlot(Slot* slot) { |
| 384 | if (slot == NULL) return kInvalidSlotNumber; |
| 385 | switch (slot->type()) { |
| 386 | case Slot::PARAMETER: |
| 387 | return slot->index(); |
| 388 | case Slot::LOCAL: |
| 389 | return slot->index() + scope()->num_parameters(); |
| 390 | default: |
| 391 | break; |
| 392 | } |
| 393 | return kInvalidSlotNumber; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | |
| 397 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 398 | // Currently, this assertion will fail if we try to assign to |
| 399 | // a constant variable that is constant because it is read-only |
| 400 | // (such as the variable referring to a named function expression). |
| 401 | // We need to implement assignments to read-only variables. |
| 402 | // Ideally, we should do this during AST generation (by converting |
| 403 | // such assignments into expression statements); however, in general |
| 404 | // we may not be able to make the decision until past AST generation, |
| 405 | // that is when the entire program is known. |
| 406 | ASSERT(slot != NULL); |
| 407 | int index = slot->index(); |
| 408 | switch (slot->type()) { |
| 409 | case Slot::PARAMETER: |
| 410 | return frame_->ParameterAt(index); |
| 411 | |
| 412 | case Slot::LOCAL: |
| 413 | return frame_->LocalAt(index); |
| 414 | |
| 415 | case Slot::CONTEXT: { |
| 416 | // Follow the context chain if necessary. |
| 417 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 418 | Register context = cp; |
| 419 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
| 420 | for (int i = 0; i < chain_length; i++) { |
| 421 | // Load the closure. |
| 422 | // (All contexts, even 'with' contexts, have a closure, |
| 423 | // and it is the same for all contexts inside a function. |
| 424 | // There is no need to go to the function context first.) |
| 425 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 426 | // Load the function context (which is the incoming, outer context). |
| 427 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 428 | context = tmp; |
| 429 | } |
| 430 | // We may have a 'with' context now. Get the function context. |
| 431 | // (In fact this mov may never be the needed, since the scope analysis |
| 432 | // may not permit a direct context access in this case and thus we are |
| 433 | // always at a function context. However it is safe to dereference be- |
| 434 | // cause the function context of a function context is itself. Before |
| 435 | // deleting this mov we should try to create a counter-example first, |
| 436 | // though...) |
| 437 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 438 | return ContextOperand(tmp, index); |
| 439 | } |
| 440 | |
| 441 | default: |
| 442 | UNREACHABLE(); |
| 443 | return MemOperand(r0, 0); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | |
| 448 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 449 | Slot* slot, |
| 450 | Register tmp, |
| 451 | Register tmp2, |
| 452 | JumpTarget* slow) { |
| 453 | ASSERT(slot->type() == Slot::CONTEXT); |
| 454 | Register context = cp; |
| 455 | |
| 456 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 457 | if (s->num_heap_slots() > 0) { |
| 458 | if (s->calls_eval()) { |
| 459 | // Check that extension is NULL. |
| 460 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 461 | __ tst(tmp2, tmp2); |
| 462 | slow->Branch(ne); |
| 463 | } |
| 464 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 465 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 466 | context = tmp; |
| 467 | } |
| 468 | } |
| 469 | // Check that last extension is NULL. |
| 470 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 471 | __ tst(tmp2, tmp2); |
| 472 | slow->Branch(ne); |
| 473 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 474 | return ContextOperand(tmp, slot->index()); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 479 | // (partially) translated into branches, or it may have set the condition |
| 480 | // code register. If force_cc is set, the value is forced to set the |
| 481 | // condition code register and no value is pushed. If the condition code |
| 482 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 483 | // test for 'true'. |
| 484 | void CodeGenerator::LoadCondition(Expression* x, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 485 | JumpTarget* true_target, |
| 486 | JumpTarget* false_target, |
| 487 | bool force_cc) { |
| 488 | ASSERT(!has_cc()); |
| 489 | int original_height = frame_->height(); |
| 490 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 491 | { ConditionCodeGenState new_state(this, true_target, false_target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 492 | Visit(x); |
| 493 | |
| 494 | // If we hit a stack overflow, we may not have actually visited |
| 495 | // the expression. In that case, we ensure that we have a |
| 496 | // valid-looking frame state because we will continue to generate |
| 497 | // code as we unwind the C++ stack. |
| 498 | // |
| 499 | // It's possible to have both a stack overflow and a valid frame |
| 500 | // state (eg, a subexpression overflowed, visiting it returned |
| 501 | // with a dummied frame state, and visiting this expression |
| 502 | // returned with a normal-looking state). |
| 503 | if (HasStackOverflow() && |
| 504 | has_valid_frame() && |
| 505 | !has_cc() && |
| 506 | frame_->height() == original_height) { |
| 507 | true_target->Jump(); |
| 508 | } |
| 509 | } |
| 510 | if (force_cc && frame_ != NULL && !has_cc()) { |
| 511 | // Convert the TOS value to a boolean in the condition code register. |
| 512 | ToBoolean(true_target, false_target); |
| 513 | } |
| 514 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 515 | ASSERT(!has_valid_frame() || |
| 516 | (has_cc() && frame_->height() == original_height) || |
| 517 | (!has_cc() && frame_->height() == original_height + 1)); |
| 518 | } |
| 519 | |
| 520 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 521 | void CodeGenerator::Load(Expression* expr) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 522 | // We generally assume that we are not in a spilled scope for most |
| 523 | // of the code generator. A failure to ensure this caused issue 815 |
| 524 | // and this assert is designed to catch similar issues. |
| 525 | frame_->AssertIsNotSpilled(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 526 | #ifdef DEBUG |
| 527 | int original_height = frame_->height(); |
| 528 | #endif |
| 529 | JumpTarget true_target; |
| 530 | JumpTarget false_target; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 531 | LoadCondition(expr, &true_target, &false_target, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 532 | |
| 533 | if (has_cc()) { |
| 534 | // Convert cc_reg_ into a boolean value. |
| 535 | JumpTarget loaded; |
| 536 | JumpTarget materialize_true; |
| 537 | materialize_true.Branch(cc_reg_); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 538 | frame_->EmitPushRoot(Heap::kFalseValueRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | loaded.Jump(); |
| 540 | materialize_true.Bind(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 541 | frame_->EmitPushRoot(Heap::kTrueValueRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 542 | loaded.Bind(); |
| 543 | cc_reg_ = al; |
| 544 | } |
| 545 | |
| 546 | if (true_target.is_linked() || false_target.is_linked()) { |
| 547 | // We have at least one condition value that has been "translated" |
| 548 | // into a branch, thus it needs to be loaded explicitly. |
| 549 | JumpTarget loaded; |
| 550 | if (frame_ != NULL) { |
| 551 | loaded.Jump(); // Don't lose the current TOS. |
| 552 | } |
| 553 | bool both = true_target.is_linked() && false_target.is_linked(); |
| 554 | // Load "true" if necessary. |
| 555 | if (true_target.is_linked()) { |
| 556 | true_target.Bind(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 557 | frame_->EmitPushRoot(Heap::kTrueValueRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 558 | } |
| 559 | // If both "true" and "false" need to be loaded jump across the code for |
| 560 | // "false". |
| 561 | if (both) { |
| 562 | loaded.Jump(); |
| 563 | } |
| 564 | // Load "false" if necessary. |
| 565 | if (false_target.is_linked()) { |
| 566 | false_target.Bind(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 567 | frame_->EmitPushRoot(Heap::kFalseValueRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 568 | } |
| 569 | // A value is loaded on all paths reaching this point. |
| 570 | loaded.Bind(); |
| 571 | } |
| 572 | ASSERT(has_valid_frame()); |
| 573 | ASSERT(!has_cc()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 574 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
| 578 | void CodeGenerator::LoadGlobal() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 579 | Register reg = frame_->GetTOSRegister(); |
| 580 | __ ldr(reg, GlobalObject()); |
| 581 | frame_->EmitPush(reg); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | |
| 585 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 586 | Register reg = frame_->GetTOSRegister(); |
| 587 | __ ldr(reg, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 588 | __ ldr(reg, |
| 589 | FieldMemOperand(reg, GlobalObject::kGlobalReceiverOffset)); |
| 590 | frame_->EmitPush(reg); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 594 | ArgumentsAllocationMode CodeGenerator::ArgumentsMode() { |
| 595 | if (scope()->arguments() == NULL) return NO_ARGUMENTS_ALLOCATION; |
| 596 | ASSERT(scope()->arguments_shadow() != NULL); |
| 597 | // We don't want to do lazy arguments allocation for functions that |
| 598 | // have heap-allocated contexts, because it interfers with the |
| 599 | // uninitialized const tracking in the context objects. |
| 600 | return (scope()->num_heap_slots() > 0) |
| 601 | ? EAGER_ARGUMENTS_ALLOCATION |
| 602 | : LAZY_ARGUMENTS_ALLOCATION; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | void CodeGenerator::StoreArgumentsObject(bool initial) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 607 | ArgumentsAllocationMode mode = ArgumentsMode(); |
| 608 | ASSERT(mode != NO_ARGUMENTS_ALLOCATION); |
| 609 | |
| 610 | Comment cmnt(masm_, "[ store arguments object"); |
| 611 | if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) { |
| 612 | // When using lazy arguments allocation, we store the hole value |
| 613 | // as a sentinel indicating that the arguments object hasn't been |
| 614 | // allocated yet. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 615 | frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 616 | } else { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 617 | frame_->SpillAll(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 618 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
| 619 | __ ldr(r2, frame_->Function()); |
| 620 | // The receiver is below the arguments, the return address, and the |
| 621 | // frame pointer on the stack. |
| 622 | const int kReceiverDisplacement = 2 + scope()->num_parameters(); |
| 623 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
| 624 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
| 625 | frame_->Adjust(3); |
| 626 | __ Push(r2, r1, r0); |
| 627 | frame_->CallStub(&stub, 3); |
| 628 | frame_->EmitPush(r0); |
| 629 | } |
| 630 | |
| 631 | Variable* arguments = scope()->arguments()->var(); |
| 632 | Variable* shadow = scope()->arguments_shadow()->var(); |
| 633 | ASSERT(arguments != NULL && arguments->slot() != NULL); |
| 634 | ASSERT(shadow != NULL && shadow->slot() != NULL); |
| 635 | JumpTarget done; |
| 636 | if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) { |
| 637 | // We have to skip storing into the arguments slot if it has |
| 638 | // already been written to. This can happen if the a function |
| 639 | // has a local variable named 'arguments'. |
| 640 | LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 641 | Register arguments = frame_->PopToRegister(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 642 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 643 | __ cmp(arguments, ip); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 644 | done.Branch(ne); |
| 645 | } |
| 646 | StoreToSlot(arguments->slot(), NOT_CONST_INIT); |
| 647 | if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind(); |
| 648 | StoreToSlot(shadow->slot(), NOT_CONST_INIT); |
| 649 | } |
| 650 | |
| 651 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 652 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 653 | // Special handling of identifiers as subexpressions of typeof. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 654 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 655 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 656 | // For a global variable we build the property reference |
| 657 | // <global>.<variable> and perform a (regular non-contextual) property |
| 658 | // load to make sure we do not get reference errors. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 659 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 660 | Literal key(variable->name()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 661 | Property property(&global, &key, RelocInfo::kNoPosition); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 662 | Reference ref(this, &property); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 663 | ref.GetValue(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 664 | } else if (variable != NULL && variable->slot() != NULL) { |
| 665 | // For a variable that rewrites to a slot, we signal it is the immediate |
| 666 | // subexpression of a typeof. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 667 | LoadFromSlotCheckForArguments(variable->slot(), INSIDE_TYPEOF); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 668 | } else { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 669 | // Anything else can be handled normally. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 670 | Load(expr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 675 | Reference::Reference(CodeGenerator* cgen, |
| 676 | Expression* expression, |
| 677 | bool persist_after_get) |
| 678 | : cgen_(cgen), |
| 679 | expression_(expression), |
| 680 | type_(ILLEGAL), |
| 681 | persist_after_get_(persist_after_get) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 682 | // We generally assume that we are not in a spilled scope for most |
| 683 | // of the code generator. A failure to ensure this caused issue 815 |
| 684 | // and this assert is designed to catch similar issues. |
| 685 | cgen->frame()->AssertIsNotSpilled(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 686 | cgen->LoadReference(this); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | Reference::~Reference() { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 691 | ASSERT(is_unloaded() || is_illegal()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | |
| 695 | void CodeGenerator::LoadReference(Reference* ref) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 696 | Comment cmnt(masm_, "[ LoadReference"); |
| 697 | Expression* e = ref->expression(); |
| 698 | Property* property = e->AsProperty(); |
| 699 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 700 | |
| 701 | if (property != NULL) { |
| 702 | // The expression is either a property or a variable proxy that rewrites |
| 703 | // to a property. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 704 | Load(property->obj()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 705 | if (property->key()->IsPropertyName()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 706 | ref->set_type(Reference::NAMED); |
| 707 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 708 | Load(property->key()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 709 | ref->set_type(Reference::KEYED); |
| 710 | } |
| 711 | } else if (var != NULL) { |
| 712 | // The expression is a variable proxy that does not rewrite to a |
| 713 | // property. Global variables are treated as named property references. |
| 714 | if (var->is_global()) { |
| 715 | LoadGlobal(); |
| 716 | ref->set_type(Reference::NAMED); |
| 717 | } else { |
| 718 | ASSERT(var->slot() != NULL); |
| 719 | ref->set_type(Reference::SLOT); |
| 720 | } |
| 721 | } else { |
| 722 | // Anything else is a runtime error. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 723 | Load(e); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 724 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | |
| 729 | void CodeGenerator::UnloadReference(Reference* ref) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 730 | int size = ref->size(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 731 | ref->set_unloaded(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 732 | if (size == 0) return; |
| 733 | |
| 734 | // Pop a reference from the stack while preserving TOS. |
| 735 | VirtualFrame::RegisterAllocationScope scope(this); |
| 736 | Comment cmnt(masm_, "[ UnloadReference"); |
| 737 | if (size > 0) { |
| 738 | Register tos = frame_->PopToRegister(); |
| 739 | frame_->Drop(size); |
| 740 | frame_->EmitPush(tos); |
| 741 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
| 745 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 746 | // register to a boolean in the condition code register. The code |
| 747 | // may jump to 'false_target' in case the register converts to 'false'. |
| 748 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 749 | JumpTarget* false_target) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 750 | // Note: The generated code snippet does not change stack variables. |
| 751 | // Only the condition code should be set. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 752 | bool known_smi = frame_->KnownSmiAt(0); |
| 753 | Register tos = frame_->PopToRegister(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 754 | |
| 755 | // Fast case checks |
| 756 | |
| 757 | // Check if the value is 'false'. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 758 | if (!known_smi) { |
| 759 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 760 | __ cmp(tos, ip); |
| 761 | false_target->Branch(eq); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 762 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 763 | // Check if the value is 'true'. |
| 764 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 765 | __ cmp(tos, ip); |
| 766 | true_target->Branch(eq); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 767 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 768 | // Check if the value is 'undefined'. |
| 769 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 770 | __ cmp(tos, ip); |
| 771 | false_target->Branch(eq); |
| 772 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 773 | |
| 774 | // Check if the value is a smi. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 775 | __ cmp(tos, Operand(Smi::FromInt(0))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 776 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 777 | if (!known_smi) { |
| 778 | false_target->Branch(eq); |
| 779 | __ tst(tos, Operand(kSmiTagMask)); |
| 780 | true_target->Branch(eq); |
| 781 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 782 | // Slow case. |
| 783 | if (CpuFeatures::IsSupported(VFP3)) { |
| 784 | CpuFeatures::Scope scope(VFP3); |
| 785 | // Implements the slow case by using ToBooleanStub. |
| 786 | // The ToBooleanStub takes a single argument, and |
| 787 | // returns a non-zero value for true, or zero for false. |
| 788 | // Both the argument value and the return value use the |
| 789 | // register assigned to tos_ |
| 790 | ToBooleanStub stub(tos); |
| 791 | frame_->CallStub(&stub, 0); |
| 792 | // Convert the result in "tos" to a condition code. |
| 793 | __ cmp(tos, Operand(0)); |
| 794 | } else { |
| 795 | // Implements slow case by calling the runtime. |
| 796 | frame_->EmitPush(tos); |
| 797 | frame_->CallRuntime(Runtime::kToBool, 1); |
| 798 | // Convert the result (r0) to a condition code. |
| 799 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 800 | __ cmp(r0, ip); |
| 801 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 802 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 803 | |
| 804 | cc_reg_ = ne; |
| 805 | } |
| 806 | |
| 807 | |
| 808 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
| 809 | OverwriteMode overwrite_mode, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 810 | GenerateInlineSmi inline_smi, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 811 | int constant_rhs) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 812 | // top of virtual frame: y |
| 813 | // 2nd elt. on virtual frame : x |
| 814 | // result : top of virtual frame |
| 815 | |
| 816 | // Stub is entered with a call: 'return address' is in lr. |
| 817 | switch (op) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 818 | case Token::ADD: |
| 819 | case Token::SUB: |
| 820 | if (inline_smi) { |
| 821 | JumpTarget done; |
| 822 | Register rhs = frame_->PopToRegister(); |
| 823 | Register lhs = frame_->PopToRegister(rhs); |
| 824 | Register scratch = VirtualFrame::scratch0(); |
| 825 | __ orr(scratch, rhs, Operand(lhs)); |
| 826 | // Check they are both small and positive. |
| 827 | __ tst(scratch, Operand(kSmiTagMask | 0xc0000000)); |
| 828 | ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 829 | STATIC_ASSERT(kSmiTag == 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 830 | if (op == Token::ADD) { |
| 831 | __ add(r0, lhs, Operand(rhs), LeaveCC, eq); |
| 832 | } else { |
| 833 | __ sub(r0, lhs, Operand(rhs), LeaveCC, eq); |
| 834 | } |
| 835 | done.Branch(eq); |
| 836 | GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs); |
| 837 | frame_->SpillAll(); |
| 838 | frame_->CallStub(&stub, 0); |
| 839 | done.Bind(); |
| 840 | frame_->EmitPush(r0); |
| 841 | break; |
| 842 | } else { |
| 843 | // Fall through! |
| 844 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 845 | case Token::BIT_OR: |
| 846 | case Token::BIT_AND: |
| 847 | case Token::BIT_XOR: |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 848 | if (inline_smi) { |
| 849 | bool rhs_is_smi = frame_->KnownSmiAt(0); |
| 850 | bool lhs_is_smi = frame_->KnownSmiAt(1); |
| 851 | Register rhs = frame_->PopToRegister(); |
| 852 | Register lhs = frame_->PopToRegister(rhs); |
| 853 | Register smi_test_reg; |
| 854 | Condition cond; |
| 855 | if (!rhs_is_smi || !lhs_is_smi) { |
| 856 | if (rhs_is_smi) { |
| 857 | smi_test_reg = lhs; |
| 858 | } else if (lhs_is_smi) { |
| 859 | smi_test_reg = rhs; |
| 860 | } else { |
| 861 | smi_test_reg = VirtualFrame::scratch0(); |
| 862 | __ orr(smi_test_reg, rhs, Operand(lhs)); |
| 863 | } |
| 864 | // Check they are both Smis. |
| 865 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 866 | cond = eq; |
| 867 | } else { |
| 868 | cond = al; |
| 869 | } |
| 870 | ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now. |
| 871 | if (op == Token::BIT_OR) { |
| 872 | __ orr(r0, lhs, Operand(rhs), LeaveCC, cond); |
| 873 | } else if (op == Token::BIT_AND) { |
| 874 | __ and_(r0, lhs, Operand(rhs), LeaveCC, cond); |
| 875 | } else { |
| 876 | ASSERT(op == Token::BIT_XOR); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 877 | STATIC_ASSERT(kSmiTag == 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 878 | __ eor(r0, lhs, Operand(rhs), LeaveCC, cond); |
| 879 | } |
| 880 | if (cond != al) { |
| 881 | JumpTarget done; |
| 882 | done.Branch(cond); |
| 883 | GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs); |
| 884 | frame_->SpillAll(); |
| 885 | frame_->CallStub(&stub, 0); |
| 886 | done.Bind(); |
| 887 | } |
| 888 | frame_->EmitPush(r0); |
| 889 | break; |
| 890 | } else { |
| 891 | // Fall through! |
| 892 | } |
| 893 | case Token::MUL: |
| 894 | case Token::DIV: |
| 895 | case Token::MOD: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 896 | case Token::SHL: |
| 897 | case Token::SHR: |
| 898 | case Token::SAR: { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 899 | Register rhs = frame_->PopToRegister(); |
| 900 | Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 901 | GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs); |
| 902 | frame_->SpillAll(); |
| 903 | frame_->CallStub(&stub, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 904 | frame_->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 905 | break; |
| 906 | } |
| 907 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 908 | case Token::COMMA: { |
| 909 | Register scratch = frame_->PopToRegister(); |
| 910 | // Simply discard left value. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 911 | frame_->Drop(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 912 | frame_->EmitPush(scratch); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 913 | break; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 914 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 915 | |
| 916 | default: |
| 917 | // Other cases should have been handled before this point. |
| 918 | UNREACHABLE(); |
| 919 | break; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | |
| 924 | class DeferredInlineSmiOperation: public DeferredCode { |
| 925 | public: |
| 926 | DeferredInlineSmiOperation(Token::Value op, |
| 927 | int value, |
| 928 | bool reversed, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 929 | OverwriteMode overwrite_mode, |
| 930 | Register tos) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 931 | : op_(op), |
| 932 | value_(value), |
| 933 | reversed_(reversed), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 934 | overwrite_mode_(overwrite_mode), |
| 935 | tos_register_(tos) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 936 | set_comment("[ DeferredInlinedSmiOperation"); |
| 937 | } |
| 938 | |
| 939 | virtual void Generate(); |
| 940 | |
| 941 | private: |
| 942 | Token::Value op_; |
| 943 | int value_; |
| 944 | bool reversed_; |
| 945 | OverwriteMode overwrite_mode_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 946 | Register tos_register_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 947 | }; |
| 948 | |
| 949 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 950 | |
| 951 | // On entry the non-constant side of the binary operation is in tos_register_ |
| 952 | // and the constant smi side is nowhere. The tos_register_ is not used by the |
| 953 | // virtual frame. On exit the answer is in the tos_register_ and the virtual |
| 954 | // frame is unchanged. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 955 | void DeferredInlineSmiOperation::Generate() { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 956 | VirtualFrame copied_frame(*frame_state()->frame()); |
| 957 | copied_frame.SpillAll(); |
| 958 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 959 | Register lhs = r1; |
| 960 | Register rhs = r0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 961 | switch (op_) { |
| 962 | case Token::ADD: { |
| 963 | // Revert optimistic add. |
| 964 | if (reversed_) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 965 | __ sub(r0, tos_register_, Operand(Smi::FromInt(value_))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 966 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 967 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 968 | __ sub(r1, tos_register_, Operand(Smi::FromInt(value_))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 969 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 970 | } |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | case Token::SUB: { |
| 975 | // Revert optimistic sub. |
| 976 | if (reversed_) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 977 | __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 978 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 979 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 980 | __ add(r1, tos_register_, Operand(Smi::FromInt(value_))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 981 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 982 | } |
| 983 | break; |
| 984 | } |
| 985 | |
| 986 | // For these operations there is no optimistic operation that needs to be |
| 987 | // reverted. |
| 988 | case Token::MUL: |
| 989 | case Token::MOD: |
| 990 | case Token::BIT_OR: |
| 991 | case Token::BIT_XOR: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 992 | case Token::BIT_AND: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 993 | case Token::SHL: |
| 994 | case Token::SHR: |
| 995 | case Token::SAR: { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 996 | if (tos_register_.is(r1)) { |
| 997 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 998 | } else { |
| 999 | ASSERT(tos_register_.is(r0)); |
| 1000 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 1001 | } |
| 1002 | if (reversed_ == tos_register_.is(r1)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1003 | lhs = r0; |
| 1004 | rhs = r1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1005 | } |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | default: |
| 1010 | // Other cases should have been handled before this point. |
| 1011 | UNREACHABLE(); |
| 1012 | break; |
| 1013 | } |
| 1014 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1015 | GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1016 | __ CallStub(&stub); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1017 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1018 | // The generic stub returns its value in r0, but that's not |
| 1019 | // necessarily what we want. We want whatever the inlined code |
| 1020 | // expected, which is that the answer is in the same register as |
| 1021 | // the operand was. |
| 1022 | __ Move(tos_register_, r0); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1023 | |
| 1024 | // The tos register was not in use for the virtual frame that we |
| 1025 | // came into this function with, so we can merge back to that frame |
| 1026 | // without trashing it. |
| 1027 | copied_frame.MergeTo(frame_state()->frame()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 1032 | x &= x - 1; |
| 1033 | return (x & (x - 1)) == 0; |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | // Returns the index of the lowest bit set. |
| 1038 | static int BitPosition(unsigned x) { |
| 1039 | int bit_posn = 0; |
| 1040 | while ((x & 0xf) == 0) { |
| 1041 | bit_posn += 4; |
| 1042 | x >>= 4; |
| 1043 | } |
| 1044 | while ((x & 1) == 0) { |
| 1045 | bit_posn++; |
| 1046 | x >>= 1; |
| 1047 | } |
| 1048 | return bit_posn; |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | void CodeGenerator::SmiOperation(Token::Value op, |
| 1053 | Handle<Object> value, |
| 1054 | bool reversed, |
| 1055 | OverwriteMode mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1056 | int int_value = Smi::cast(*value)->value(); |
| 1057 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1058 | bool both_sides_are_smi = frame_->KnownSmiAt(0); |
| 1059 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1060 | bool something_to_inline; |
| 1061 | switch (op) { |
| 1062 | case Token::ADD: |
| 1063 | case Token::SUB: |
| 1064 | case Token::BIT_AND: |
| 1065 | case Token::BIT_OR: |
| 1066 | case Token::BIT_XOR: { |
| 1067 | something_to_inline = true; |
| 1068 | break; |
| 1069 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1070 | case Token::SHL: { |
| 1071 | something_to_inline = (both_sides_are_smi || !reversed); |
| 1072 | break; |
| 1073 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1074 | case Token::SHR: |
| 1075 | case Token::SAR: { |
| 1076 | if (reversed) { |
| 1077 | something_to_inline = false; |
| 1078 | } else { |
| 1079 | something_to_inline = true; |
| 1080 | } |
| 1081 | break; |
| 1082 | } |
| 1083 | case Token::MOD: { |
| 1084 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 1085 | something_to_inline = false; |
| 1086 | } else { |
| 1087 | something_to_inline = true; |
| 1088 | } |
| 1089 | break; |
| 1090 | } |
| 1091 | case Token::MUL: { |
| 1092 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1093 | something_to_inline = false; |
| 1094 | } else { |
| 1095 | something_to_inline = true; |
| 1096 | } |
| 1097 | break; |
| 1098 | } |
| 1099 | default: { |
| 1100 | something_to_inline = false; |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1104 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1105 | if (!something_to_inline) { |
| 1106 | if (!reversed) { |
| 1107 | // Push the rhs onto the virtual frame by putting it in a TOS register. |
| 1108 | Register rhs = frame_->GetTOSRegister(); |
| 1109 | __ mov(rhs, Operand(value)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1110 | frame_->EmitPush(rhs, TypeInfo::Smi()); |
| 1111 | GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, int_value); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1112 | } else { |
| 1113 | // Pop the rhs, then push lhs and rhs in the right order. Only performs |
| 1114 | // at most one pop, the rest takes place in TOS registers. |
| 1115 | Register lhs = frame_->GetTOSRegister(); // Get reg for pushing. |
| 1116 | Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this. |
| 1117 | __ mov(lhs, Operand(value)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1118 | frame_->EmitPush(lhs, TypeInfo::Smi()); |
| 1119 | TypeInfo t = both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Unknown(); |
| 1120 | frame_->EmitPush(rhs, t); |
| 1121 | GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, kUnknownIntValue); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1122 | } |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | // We move the top of stack to a register (normally no move is invoved). |
| 1127 | Register tos = frame_->PopToRegister(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1128 | switch (op) { |
| 1129 | case Token::ADD: { |
| 1130 | DeferredCode* deferred = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1131 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1132 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1133 | __ add(tos, tos, Operand(value), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1134 | deferred->Branch(vs); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1135 | if (!both_sides_are_smi) { |
| 1136 | __ tst(tos, Operand(kSmiTagMask)); |
| 1137 | deferred->Branch(ne); |
| 1138 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1139 | deferred->BindExit(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1140 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | case Token::SUB: { |
| 1145 | DeferredCode* deferred = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1146 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1147 | |
| 1148 | if (reversed) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1149 | __ rsb(tos, tos, Operand(value), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1150 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1151 | __ sub(tos, tos, Operand(value), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1152 | } |
| 1153 | deferred->Branch(vs); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1154 | if (!both_sides_are_smi) { |
| 1155 | __ tst(tos, Operand(kSmiTagMask)); |
| 1156 | deferred->Branch(ne); |
| 1157 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1158 | deferred->BindExit(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1159 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1160 | break; |
| 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | case Token::BIT_OR: |
| 1165 | case Token::BIT_XOR: |
| 1166 | case Token::BIT_AND: { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1167 | if (both_sides_are_smi) { |
| 1168 | switch (op) { |
| 1169 | case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break; |
| 1170 | case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1171 | case Token::BIT_AND: __ And(tos, tos, Operand(value)); break; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1172 | default: UNREACHABLE(); |
| 1173 | } |
| 1174 | frame_->EmitPush(tos, TypeInfo::Smi()); |
| 1175 | } else { |
| 1176 | DeferredCode* deferred = |
| 1177 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1178 | __ tst(tos, Operand(kSmiTagMask)); |
| 1179 | deferred->Branch(ne); |
| 1180 | switch (op) { |
| 1181 | case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break; |
| 1182 | case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1183 | case Token::BIT_AND: __ And(tos, tos, Operand(value)); break; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1184 | default: UNREACHABLE(); |
| 1185 | } |
| 1186 | deferred->BindExit(); |
| 1187 | TypeInfo result_type = |
| 1188 | (op == Token::BIT_AND) ? TypeInfo::Smi() : TypeInfo::Integer32(); |
| 1189 | frame_->EmitPush(tos, result_type); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1190 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1191 | break; |
| 1192 | } |
| 1193 | |
| 1194 | case Token::SHL: |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1195 | if (reversed) { |
| 1196 | ASSERT(both_sides_are_smi); |
| 1197 | int max_shift = 0; |
| 1198 | int max_result = int_value == 0 ? 1 : int_value; |
| 1199 | while (Smi::IsValid(max_result << 1)) { |
| 1200 | max_shift++; |
| 1201 | max_result <<= 1; |
| 1202 | } |
| 1203 | DeferredCode* deferred = |
| 1204 | new DeferredInlineSmiOperation(op, int_value, true, mode, tos); |
| 1205 | // Mask off the last 5 bits of the shift operand (rhs). This is part |
| 1206 | // of the definition of shift in JS and we know we have a Smi so we |
| 1207 | // can safely do this. The masked version gets passed to the |
| 1208 | // deferred code, but that makes no difference. |
| 1209 | __ and_(tos, tos, Operand(Smi::FromInt(0x1f))); |
| 1210 | __ cmp(tos, Operand(Smi::FromInt(max_shift))); |
| 1211 | deferred->Branch(ge); |
| 1212 | Register scratch = VirtualFrame::scratch0(); |
| 1213 | __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Untag. |
| 1214 | __ mov(tos, Operand(Smi::FromInt(int_value))); // Load constant. |
| 1215 | __ mov(tos, Operand(tos, LSL, scratch)); // Shift constant. |
| 1216 | deferred->BindExit(); |
| 1217 | TypeInfo result = TypeInfo::Integer32(); |
| 1218 | frame_->EmitPush(tos, result); |
| 1219 | break; |
| 1220 | } |
| 1221 | // Fall through! |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1222 | case Token::SHR: |
| 1223 | case Token::SAR: { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1224 | ASSERT(!reversed); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 1225 | TypeInfo result = |
| 1226 | (op == Token::SAR) ? TypeInfo::Integer32() : TypeInfo::Number(); |
| 1227 | if (!reversed) { |
| 1228 | if (op == Token::SHR) { |
| 1229 | if (int_value >= 2) { |
| 1230 | result = TypeInfo::Smi(); |
| 1231 | } else if (int_value >= 1) { |
| 1232 | result = TypeInfo::Integer32(); |
| 1233 | } |
| 1234 | } else { |
| 1235 | if (int_value >= 1) { |
| 1236 | result = TypeInfo::Smi(); |
| 1237 | } |
| 1238 | } |
| 1239 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1240 | Register scratch = VirtualFrame::scratch0(); |
| 1241 | Register scratch2 = VirtualFrame::scratch1(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1242 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 1243 | DeferredCode* deferred = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1244 | new DeferredInlineSmiOperation(op, shift_value, false, mode, tos); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1245 | uint32_t problematic_mask = kSmiTagMask; |
| 1246 | // For unsigned shift by zero all negative smis are problematic. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1247 | bool skip_smi_test = both_sides_are_smi; |
| 1248 | if (shift_value == 0 && op == Token::SHR) { |
| 1249 | problematic_mask |= 0x80000000; |
| 1250 | skip_smi_test = false; |
| 1251 | } |
| 1252 | if (!skip_smi_test) { |
| 1253 | __ tst(tos, Operand(problematic_mask)); |
| 1254 | deferred->Branch(ne); // Go slow for problematic input. |
| 1255 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1256 | switch (op) { |
| 1257 | case Token::SHL: { |
| 1258 | if (shift_value != 0) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1259 | int adjusted_shift = shift_value - kSmiTagSize; |
| 1260 | ASSERT(adjusted_shift >= 0); |
| 1261 | if (adjusted_shift != 0) { |
| 1262 | __ mov(scratch, Operand(tos, LSL, adjusted_shift)); |
| 1263 | // Check that the *signed* result fits in a smi. |
| 1264 | __ add(scratch2, scratch, Operand(0x40000000), SetCC); |
| 1265 | deferred->Branch(mi); |
| 1266 | __ mov(tos, Operand(scratch, LSL, kSmiTagSize)); |
| 1267 | } else { |
| 1268 | // Check that the *signed* result fits in a smi. |
| 1269 | __ add(scratch2, tos, Operand(0x40000000), SetCC); |
| 1270 | deferred->Branch(mi); |
| 1271 | __ mov(tos, Operand(tos, LSL, kSmiTagSize)); |
| 1272 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1273 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | } |
| 1276 | case Token::SHR: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1277 | if (shift_value != 0) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1278 | __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Remove tag. |
| 1279 | // LSR by immediate 0 means shifting 32 bits. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1280 | __ mov(scratch, Operand(scratch, LSR, shift_value)); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1281 | if (shift_value == 1) { |
| 1282 | // check that the *unsigned* result fits in a smi |
| 1283 | // neither of the two high-order bits can be set: |
| 1284 | // - 0x80000000: high bit would be lost when smi tagging |
| 1285 | // - 0x40000000: this number would convert to negative when |
| 1286 | // smi tagging these two cases can only happen with shifts |
| 1287 | // by 0 or 1 when handed a valid smi |
| 1288 | __ tst(scratch, Operand(0xc0000000)); |
| 1289 | deferred->Branch(ne); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1290 | } else { |
| 1291 | ASSERT(shift_value >= 2); |
| 1292 | result = TypeInfo::Smi(); // SHR by at least 2 gives a Smi. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1293 | } |
| 1294 | __ mov(tos, Operand(scratch, LSL, kSmiTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1295 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1296 | break; |
| 1297 | } |
| 1298 | case Token::SAR: { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1299 | // In the ARM instructions set, ASR by immediate 0 means shifting 32 |
| 1300 | // bits. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1301 | if (shift_value != 0) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1302 | // Do the shift and the tag removal in one operation. If the shift |
| 1303 | // is 31 bits (the highest possible value) then we emit the |
| 1304 | // instruction as a shift by 0 which means shift arithmetically by |
| 1305 | // 32. |
| 1306 | __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f)); |
| 1307 | // Put tag back. |
| 1308 | __ mov(tos, Operand(tos, LSL, kSmiTagSize)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1309 | // SAR by at least 1 gives a Smi. |
| 1310 | result = TypeInfo::Smi(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1311 | } |
| 1312 | break; |
| 1313 | } |
| 1314 | default: UNREACHABLE(); |
| 1315 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1316 | deferred->BindExit(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1317 | frame_->EmitPush(tos, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | } |
| 1320 | |
| 1321 | case Token::MOD: { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1322 | ASSERT(!reversed); |
| 1323 | ASSERT(int_value >= 2); |
| 1324 | ASSERT(IsPowerOf2(int_value)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1325 | DeferredCode* deferred = |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1326 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1327 | unsigned mask = (0x80000000u | kSmiTagMask); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1328 | __ tst(tos, Operand(mask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1329 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1330 | mask = (int_value << kSmiTagSize) - 1; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1331 | __ and_(tos, tos, Operand(mask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1332 | deferred->BindExit(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1333 | // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer. |
| 1334 | frame_->EmitPush( |
| 1335 | tos, |
| 1336 | both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1337 | break; |
| 1338 | } |
| 1339 | |
| 1340 | case Token::MUL: { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1341 | ASSERT(IsEasyToMultiplyBy(int_value)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1342 | DeferredCode* deferred = |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1343 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1344 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1345 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1346 | unsigned mask = 0x80000000u; |
| 1347 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1348 | mask |= mask >> 1; |
| 1349 | } |
| 1350 | mask |= kSmiTagMask; |
| 1351 | // This does a single mask that checks for a too high value in a |
| 1352 | // conservative way and for a non-Smi. It also filters out negative |
| 1353 | // numbers, unfortunately, but since this code is inline we prefer |
| 1354 | // brevity to comprehensiveness. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1355 | __ tst(tos, Operand(mask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1356 | deferred->Branch(ne); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1357 | MultiplyByKnownInt(masm_, tos, tos, int_value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1358 | deferred->BindExit(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1359 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1360 | break; |
| 1361 | } |
| 1362 | |
| 1363 | default: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1364 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1365 | break; |
| 1366 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | void CodeGenerator::Comparison(Condition cc, |
| 1371 | Expression* left, |
| 1372 | Expression* right, |
| 1373 | bool strict) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1374 | VirtualFrame::RegisterAllocationScope scope(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1375 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1376 | if (left != NULL) Load(left); |
| 1377 | if (right != NULL) Load(right); |
| 1378 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1379 | // sp[0] : y |
| 1380 | // sp[1] : x |
| 1381 | // result : cc register |
| 1382 | |
| 1383 | // Strict only makes sense for equality comparisons. |
| 1384 | ASSERT(!strict || cc == eq); |
| 1385 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1386 | Register lhs; |
| 1387 | Register rhs; |
| 1388 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1389 | bool lhs_is_smi; |
| 1390 | bool rhs_is_smi; |
| 1391 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1392 | // We load the top two stack positions into registers chosen by the virtual |
| 1393 | // frame. This should keep the register shuffling to a minimum. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1394 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1395 | if (cc == gt || cc == le) { |
| 1396 | cc = ReverseCondition(cc); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1397 | lhs_is_smi = frame_->KnownSmiAt(0); |
| 1398 | rhs_is_smi = frame_->KnownSmiAt(1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1399 | lhs = frame_->PopToRegister(); |
| 1400 | rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again! |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1401 | } else { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1402 | rhs_is_smi = frame_->KnownSmiAt(0); |
| 1403 | lhs_is_smi = frame_->KnownSmiAt(1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1404 | rhs = frame_->PopToRegister(); |
| 1405 | lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again! |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1406 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1407 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1408 | bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi); |
| 1409 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1410 | ASSERT(rhs.is(r0) || rhs.is(r1)); |
| 1411 | ASSERT(lhs.is(r0) || lhs.is(r1)); |
| 1412 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1413 | JumpTarget exit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1414 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1415 | if (!both_sides_are_smi) { |
| 1416 | // Now we have the two sides in r0 and r1. We flush any other registers |
| 1417 | // because the stub doesn't know about register allocation. |
| 1418 | frame_->SpillAll(); |
| 1419 | Register scratch = VirtualFrame::scratch0(); |
| 1420 | Register smi_test_reg; |
| 1421 | if (lhs_is_smi) { |
| 1422 | smi_test_reg = rhs; |
| 1423 | } else if (rhs_is_smi) { |
| 1424 | smi_test_reg = lhs; |
| 1425 | } else { |
| 1426 | __ orr(scratch, lhs, Operand(rhs)); |
| 1427 | smi_test_reg = scratch; |
| 1428 | } |
| 1429 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 1430 | JumpTarget smi; |
| 1431 | smi.Branch(eq); |
| 1432 | |
| 1433 | // Perform non-smi comparison by stub. |
| 1434 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1435 | // We call with 0 args because there are 0 on the stack. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1436 | CompareStub stub(cc, strict, kBothCouldBeNaN, true, lhs, rhs); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1437 | frame_->CallStub(&stub, 0); |
| 1438 | __ cmp(r0, Operand(0)); |
| 1439 | exit.Jump(); |
| 1440 | |
| 1441 | smi.Bind(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1442 | } |
| 1443 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1444 | // Do smi comparisons by pointer comparison. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1445 | __ cmp(lhs, Operand(rhs)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1446 | |
| 1447 | exit.Bind(); |
| 1448 | cc_reg_ = cc; |
| 1449 | } |
| 1450 | |
| 1451 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1452 | // Call the function on the stack with the given arguments. |
| 1453 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1454 | CallFunctionFlags flags, |
| 1455 | int position) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1456 | // Push the arguments ("left-to-right") on the stack. |
| 1457 | int arg_count = args->length(); |
| 1458 | for (int i = 0; i < arg_count; i++) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1459 | Load(args->at(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | // Record the position for debugging purposes. |
| 1463 | CodeForSourcePosition(position); |
| 1464 | |
| 1465 | // Use the shared code stub to call the function. |
| 1466 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1467 | CallFunctionStub call_function(arg_count, in_loop, flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1468 | frame_->CallStub(&call_function, arg_count + 1); |
| 1469 | |
| 1470 | // Restore context and pop function from the stack. |
| 1471 | __ ldr(cp, frame_->Context()); |
| 1472 | frame_->Drop(); // discard the TOS |
| 1473 | } |
| 1474 | |
| 1475 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1476 | void CodeGenerator::CallApplyLazy(Expression* applicand, |
| 1477 | Expression* receiver, |
| 1478 | VariableProxy* arguments, |
| 1479 | int position) { |
| 1480 | // An optimized implementation of expressions of the form |
| 1481 | // x.apply(y, arguments). |
| 1482 | // If the arguments object of the scope has not been allocated, |
| 1483 | // and x.apply is Function.prototype.apply, this optimization |
| 1484 | // just copies y and the arguments of the current function on the |
| 1485 | // stack, as receiver and arguments, and calls x. |
| 1486 | // In the implementation comments, we call x the applicand |
| 1487 | // and y the receiver. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1488 | |
| 1489 | ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION); |
| 1490 | ASSERT(arguments->IsArguments()); |
| 1491 | |
| 1492 | // Load applicand.apply onto the stack. This will usually |
| 1493 | // give us a megamorphic load site. Not super, but it works. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1494 | Load(applicand); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1495 | Handle<String> name = Factory::LookupAsciiSymbol("apply"); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1496 | frame_->Dup(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1497 | frame_->CallLoadIC(name, RelocInfo::CODE_TARGET); |
| 1498 | frame_->EmitPush(r0); |
| 1499 | |
| 1500 | // Load the receiver and the existing arguments object onto the |
| 1501 | // expression stack. Avoid allocating the arguments object here. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1502 | Load(receiver); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1503 | LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF); |
| 1504 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1505 | // At this point the top two stack elements are probably in registers |
| 1506 | // since they were just loaded. Ensure they are in regs and get the |
| 1507 | // regs. |
| 1508 | Register receiver_reg = frame_->Peek2(); |
| 1509 | Register arguments_reg = frame_->Peek(); |
| 1510 | |
| 1511 | // From now on the frame is spilled. |
| 1512 | frame_->SpillAll(); |
| 1513 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1514 | // Emit the source position information after having loaded the |
| 1515 | // receiver and the arguments. |
| 1516 | CodeForSourcePosition(position); |
| 1517 | // Contents of the stack at this point: |
| 1518 | // sp[0]: arguments object of the current function or the hole. |
| 1519 | // sp[1]: receiver |
| 1520 | // sp[2]: applicand.apply |
| 1521 | // sp[3]: applicand. |
| 1522 | |
| 1523 | // Check if the arguments object has been lazily allocated |
| 1524 | // already. If so, just use that instead of copying the arguments |
| 1525 | // from the stack. This also deals with cases where a local variable |
| 1526 | // named 'arguments' has been introduced. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1527 | JumpTarget slow; |
| 1528 | Label done; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1529 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1530 | __ cmp(ip, arguments_reg); |
| 1531 | slow.Branch(ne); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1532 | |
| 1533 | Label build_args; |
| 1534 | // Get rid of the arguments object probe. |
| 1535 | frame_->Drop(); |
| 1536 | // Stack now has 3 elements on it. |
| 1537 | // Contents of stack at this point: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1538 | // sp[0]: receiver - in the receiver_reg register. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1539 | // sp[1]: applicand.apply |
| 1540 | // sp[2]: applicand. |
| 1541 | |
| 1542 | // Check that the receiver really is a JavaScript object. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1543 | __ BranchOnSmi(receiver_reg, &build_args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1544 | // We allow all JSObjects including JSFunctions. As long as |
| 1545 | // JS_FUNCTION_TYPE is the last instance type and it is right |
| 1546 | // after LAST_JS_OBJECT_TYPE, we do not have to check the upper |
| 1547 | // bound. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1548 | STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 1549 | STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1550 | __ CompareObjectType(receiver_reg, r2, r3, FIRST_JS_OBJECT_TYPE); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1551 | __ b(lt, &build_args); |
| 1552 | |
| 1553 | // Check that applicand.apply is Function.prototype.apply. |
| 1554 | __ ldr(r0, MemOperand(sp, kPointerSize)); |
| 1555 | __ BranchOnSmi(r0, &build_args); |
| 1556 | __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE); |
| 1557 | __ b(ne, &build_args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1558 | Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 1559 | __ ldr(r1, FieldMemOperand(r0, JSFunction::kCodeOffset)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1560 | __ cmp(r1, Operand(apply_code)); |
| 1561 | __ b(ne, &build_args); |
| 1562 | |
| 1563 | // Check that applicand is a function. |
| 1564 | __ ldr(r1, MemOperand(sp, 2 * kPointerSize)); |
| 1565 | __ BranchOnSmi(r1, &build_args); |
| 1566 | __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE); |
| 1567 | __ b(ne, &build_args); |
| 1568 | |
| 1569 | // Copy the arguments to this function possibly from the |
| 1570 | // adaptor frame below it. |
| 1571 | Label invoke, adapted; |
| 1572 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 1573 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 1574 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 1575 | __ b(eq, &adapted); |
| 1576 | |
| 1577 | // No arguments adaptor frame. Copy fixed number of arguments. |
| 1578 | __ mov(r0, Operand(scope()->num_parameters())); |
| 1579 | for (int i = 0; i < scope()->num_parameters(); i++) { |
| 1580 | __ ldr(r2, frame_->ParameterAt(i)); |
| 1581 | __ push(r2); |
| 1582 | } |
| 1583 | __ jmp(&invoke); |
| 1584 | |
| 1585 | // Arguments adaptor frame present. Copy arguments from there, but |
| 1586 | // avoid copying too many arguments to avoid stack overflows. |
| 1587 | __ bind(&adapted); |
| 1588 | static const uint32_t kArgumentsLimit = 1 * KB; |
| 1589 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 1590 | __ mov(r0, Operand(r0, LSR, kSmiTagSize)); |
| 1591 | __ mov(r3, r0); |
| 1592 | __ cmp(r0, Operand(kArgumentsLimit)); |
| 1593 | __ b(gt, &build_args); |
| 1594 | |
| 1595 | // Loop through the arguments pushing them onto the execution |
| 1596 | // stack. We don't inform the virtual frame of the push, so we don't |
| 1597 | // have to worry about getting rid of the elements from the virtual |
| 1598 | // frame. |
| 1599 | Label loop; |
| 1600 | // r3 is a small non-negative integer, due to the test above. |
| 1601 | __ cmp(r3, Operand(0)); |
| 1602 | __ b(eq, &invoke); |
| 1603 | // Compute the address of the first argument. |
| 1604 | __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2)); |
| 1605 | __ add(r2, r2, Operand(kPointerSize)); |
| 1606 | __ bind(&loop); |
| 1607 | // Post-decrement argument address by kPointerSize on each iteration. |
| 1608 | __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex)); |
| 1609 | __ push(r4); |
| 1610 | __ sub(r3, r3, Operand(1), SetCC); |
| 1611 | __ b(gt, &loop); |
| 1612 | |
| 1613 | // Invoke the function. |
| 1614 | __ bind(&invoke); |
| 1615 | ParameterCount actual(r0); |
| 1616 | __ InvokeFunction(r1, actual, CALL_FUNCTION); |
| 1617 | // Drop applicand.apply and applicand from the stack, and push |
| 1618 | // the result of the function call, but leave the spilled frame |
| 1619 | // unchanged, with 3 elements, so it is correct when we compile the |
| 1620 | // slow-case code. |
| 1621 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 1622 | __ push(r0); |
| 1623 | // Stack now has 1 element: |
| 1624 | // sp[0]: result |
| 1625 | __ jmp(&done); |
| 1626 | |
| 1627 | // Slow-case: Allocate the arguments object since we know it isn't |
| 1628 | // there, and fall-through to the slow-case where we call |
| 1629 | // applicand.apply. |
| 1630 | __ bind(&build_args); |
| 1631 | // Stack now has 3 elements, because we have jumped from where: |
| 1632 | // sp[0]: receiver |
| 1633 | // sp[1]: applicand.apply |
| 1634 | // sp[2]: applicand. |
| 1635 | StoreArgumentsObject(false); |
| 1636 | |
| 1637 | // Stack and frame now have 4 elements. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1638 | slow.Bind(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1639 | |
| 1640 | // Generic computation of x.apply(y, args) with no special optimization. |
| 1641 | // Flip applicand.apply and applicand on the stack, so |
| 1642 | // applicand looks like the receiver of the applicand.apply call. |
| 1643 | // Then process it as a normal function call. |
| 1644 | __ ldr(r0, MemOperand(sp, 3 * kPointerSize)); |
| 1645 | __ ldr(r1, MemOperand(sp, 2 * kPointerSize)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1646 | __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1647 | |
| 1648 | CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS); |
| 1649 | frame_->CallStub(&call_function, 3); |
| 1650 | // The function and its two arguments have been dropped. |
| 1651 | frame_->Drop(); // Drop the receiver as well. |
| 1652 | frame_->EmitPush(r0); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1653 | frame_->SpillAll(); // A spilled frame is also jumping to label done. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1654 | // Stack now has 1 element: |
| 1655 | // sp[0]: result |
| 1656 | __ bind(&done); |
| 1657 | |
| 1658 | // Restore the context register after a call. |
| 1659 | __ ldr(cp, frame_->Context()); |
| 1660 | } |
| 1661 | |
| 1662 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1663 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1664 | ASSERT(has_cc()); |
| 1665 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
| 1666 | target->Branch(cc); |
| 1667 | cc_reg_ = al; |
| 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | void CodeGenerator::CheckStack() { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1672 | frame_->SpillAll(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1673 | Comment cmnt(masm_, "[ check stack"); |
| 1674 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1675 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1676 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1677 | // gives a return address 12 bytes down. |
| 1678 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1679 | masm_->cmp(sp, Operand(ip)); |
| 1680 | StackCheckStub stub; |
| 1681 | // Call the stub if lower. |
| 1682 | masm_->mov(pc, |
| 1683 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1684 | RelocInfo::CODE_TARGET), |
| 1685 | LeaveCC, |
| 1686 | lo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | |
| 1690 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1691 | #ifdef DEBUG |
| 1692 | int original_height = frame_->height(); |
| 1693 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1694 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1695 | Visit(statements->at(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1696 | } |
| 1697 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | void CodeGenerator::VisitBlock(Block* node) { |
| 1702 | #ifdef DEBUG |
| 1703 | int original_height = frame_->height(); |
| 1704 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1705 | Comment cmnt(masm_, "[ Block"); |
| 1706 | CodeForStatementPosition(node); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1707 | node->break_target()->SetExpectedHeight(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1708 | VisitStatements(node->statements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1709 | if (node->break_target()->is_linked()) { |
| 1710 | node->break_target()->Bind(); |
| 1711 | } |
| 1712 | node->break_target()->Unuse(); |
| 1713 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1714 | } |
| 1715 | |
| 1716 | |
| 1717 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1718 | frame_->EmitPush(cp); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1719 | frame_->EmitPush(Operand(pairs)); |
| 1720 | frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
| 1721 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1722 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
| 1723 | // The result is discarded. |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
| 1728 | #ifdef DEBUG |
| 1729 | int original_height = frame_->height(); |
| 1730 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1731 | Comment cmnt(masm_, "[ Declaration"); |
| 1732 | Variable* var = node->proxy()->var(); |
| 1733 | ASSERT(var != NULL); // must have been resolved |
| 1734 | Slot* slot = var->slot(); |
| 1735 | |
| 1736 | // If it was not possible to allocate the variable at compile time, |
| 1737 | // we need to "declare" it at runtime to make sure it actually |
| 1738 | // exists in the local context. |
| 1739 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1740 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1741 | // during variable resolution and must have mode DYNAMIC. |
| 1742 | ASSERT(var->is_dynamic()); |
| 1743 | // For now, just do a runtime call. |
| 1744 | frame_->EmitPush(cp); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1745 | frame_->EmitPush(Operand(var->name())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1746 | // Declaration nodes are always declared in only two modes. |
| 1747 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1748 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1749 | frame_->EmitPush(Operand(Smi::FromInt(attr))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1750 | // Push initial value, if any. |
| 1751 | // Note: For variables we must not push an initial value (such as |
| 1752 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1753 | // must not destroy the current value. |
| 1754 | if (node->mode() == Variable::CONST) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1755 | frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1756 | } else if (node->fun() != NULL) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1757 | Load(node->fun()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1758 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1759 | frame_->EmitPush(Operand(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1760 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1761 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1762 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
| 1763 | // Ignore the return value (declarations are statements). |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1764 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1765 | ASSERT(frame_->height() == original_height); |
| 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | ASSERT(!var->is_global()); |
| 1770 | |
| 1771 | // If we have a function or a constant, we need to initialize the variable. |
| 1772 | Expression* val = NULL; |
| 1773 | if (node->mode() == Variable::CONST) { |
| 1774 | val = new Literal(Factory::the_hole_value()); |
| 1775 | } else { |
| 1776 | val = node->fun(); // NULL if we don't have a function |
| 1777 | } |
| 1778 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1779 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1780 | if (val != NULL) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1781 | WriteBarrierCharacter wb_info = |
| 1782 | val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI; |
| 1783 | if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1784 | // Set initial value. |
| 1785 | Reference target(this, node->proxy()); |
| 1786 | Load(val); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1787 | target.SetValue(NOT_CONST_INIT, wb_info); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1788 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1789 | // Get rid of the assigned value (declarations are statements). |
| 1790 | frame_->Drop(); |
| 1791 | } |
| 1792 | ASSERT(frame_->height() == original_height); |
| 1793 | } |
| 1794 | |
| 1795 | |
| 1796 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
| 1797 | #ifdef DEBUG |
| 1798 | int original_height = frame_->height(); |
| 1799 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1800 | Comment cmnt(masm_, "[ ExpressionStatement"); |
| 1801 | CodeForStatementPosition(node); |
| 1802 | Expression* expression = node->expression(); |
| 1803 | expression->MarkAsStatement(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1804 | Load(expression); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1805 | frame_->Drop(); |
| 1806 | ASSERT(frame_->height() == original_height); |
| 1807 | } |
| 1808 | |
| 1809 | |
| 1810 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
| 1811 | #ifdef DEBUG |
| 1812 | int original_height = frame_->height(); |
| 1813 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1814 | Comment cmnt(masm_, "// EmptyStatement"); |
| 1815 | CodeForStatementPosition(node); |
| 1816 | // nothing to do |
| 1817 | ASSERT(frame_->height() == original_height); |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
| 1822 | #ifdef DEBUG |
| 1823 | int original_height = frame_->height(); |
| 1824 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1825 | Comment cmnt(masm_, "[ IfStatement"); |
| 1826 | // Generate different code depending on which parts of the if statement |
| 1827 | // are present or not. |
| 1828 | bool has_then_stm = node->HasThenStatement(); |
| 1829 | bool has_else_stm = node->HasElseStatement(); |
| 1830 | |
| 1831 | CodeForStatementPosition(node); |
| 1832 | |
| 1833 | JumpTarget exit; |
| 1834 | if (has_then_stm && has_else_stm) { |
| 1835 | Comment cmnt(masm_, "[ IfThenElse"); |
| 1836 | JumpTarget then; |
| 1837 | JumpTarget else_; |
| 1838 | // if (cond) |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1839 | LoadCondition(node->condition(), &then, &else_, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1840 | if (frame_ != NULL) { |
| 1841 | Branch(false, &else_); |
| 1842 | } |
| 1843 | // then |
| 1844 | if (frame_ != NULL || then.is_linked()) { |
| 1845 | then.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1846 | Visit(node->then_statement()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1847 | } |
| 1848 | if (frame_ != NULL) { |
| 1849 | exit.Jump(); |
| 1850 | } |
| 1851 | // else |
| 1852 | if (else_.is_linked()) { |
| 1853 | else_.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1854 | Visit(node->else_statement()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | } else if (has_then_stm) { |
| 1858 | Comment cmnt(masm_, "[ IfThen"); |
| 1859 | ASSERT(!has_else_stm); |
| 1860 | JumpTarget then; |
| 1861 | // if (cond) |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1862 | LoadCondition(node->condition(), &then, &exit, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1863 | if (frame_ != NULL) { |
| 1864 | Branch(false, &exit); |
| 1865 | } |
| 1866 | // then |
| 1867 | if (frame_ != NULL || then.is_linked()) { |
| 1868 | then.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1869 | Visit(node->then_statement()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | } else if (has_else_stm) { |
| 1873 | Comment cmnt(masm_, "[ IfElse"); |
| 1874 | ASSERT(!has_then_stm); |
| 1875 | JumpTarget else_; |
| 1876 | // if (!cond) |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1877 | LoadCondition(node->condition(), &exit, &else_, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1878 | if (frame_ != NULL) { |
| 1879 | Branch(true, &exit); |
| 1880 | } |
| 1881 | // else |
| 1882 | if (frame_ != NULL || else_.is_linked()) { |
| 1883 | else_.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1884 | Visit(node->else_statement()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | } else { |
| 1888 | Comment cmnt(masm_, "[ If"); |
| 1889 | ASSERT(!has_then_stm && !has_else_stm); |
| 1890 | // if (cond) |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1891 | LoadCondition(node->condition(), &exit, &exit, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1892 | if (frame_ != NULL) { |
| 1893 | if (has_cc()) { |
| 1894 | cc_reg_ = al; |
| 1895 | } else { |
| 1896 | frame_->Drop(); |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | // end |
| 1902 | if (exit.is_linked()) { |
| 1903 | exit.Bind(); |
| 1904 | } |
| 1905 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1906 | } |
| 1907 | |
| 1908 | |
| 1909 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1910 | Comment cmnt(masm_, "[ ContinueStatement"); |
| 1911 | CodeForStatementPosition(node); |
| 1912 | node->target()->continue_target()->Jump(); |
| 1913 | } |
| 1914 | |
| 1915 | |
| 1916 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1917 | Comment cmnt(masm_, "[ BreakStatement"); |
| 1918 | CodeForStatementPosition(node); |
| 1919 | node->target()->break_target()->Jump(); |
| 1920 | } |
| 1921 | |
| 1922 | |
| 1923 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1924 | Comment cmnt(masm_, "[ ReturnStatement"); |
| 1925 | |
| 1926 | CodeForStatementPosition(node); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1927 | Load(node->expression()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 1928 | frame_->PopToR0(); |
| 1929 | frame_->PrepareForReturn(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1930 | if (function_return_is_shadowed_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1931 | function_return_.Jump(); |
| 1932 | } else { |
| 1933 | // Pop the result from the frame and prepare the frame for |
| 1934 | // returning thus making it easier to merge. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1935 | if (function_return_.is_bound()) { |
| 1936 | // If the function return label is already bound we reuse the |
| 1937 | // code by jumping to the return site. |
| 1938 | function_return_.Jump(); |
| 1939 | } else { |
| 1940 | function_return_.Bind(); |
| 1941 | GenerateReturnSequence(); |
| 1942 | } |
| 1943 | } |
| 1944 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1945 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1946 | |
| 1947 | void CodeGenerator::GenerateReturnSequence() { |
| 1948 | if (FLAG_trace) { |
| 1949 | // Push the return value on the stack as the parameter. |
| 1950 | // Runtime::TraceExit returns the parameter as it is. |
| 1951 | frame_->EmitPush(r0); |
| 1952 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 1953 | } |
| 1954 | |
| 1955 | #ifdef DEBUG |
| 1956 | // Add a label for checking the size of the code used for returning. |
| 1957 | Label check_exit_codesize; |
| 1958 | masm_->bind(&check_exit_codesize); |
| 1959 | #endif |
| 1960 | // Make sure that the constant pool is not emitted inside of the return |
| 1961 | // sequence. |
| 1962 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 1963 | // Tear down the frame which will restore the caller's frame pointer and |
| 1964 | // the link register. |
| 1965 | frame_->Exit(); |
| 1966 | |
| 1967 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 1968 | // tool from instrumenting as we rely on the code size here. |
| 1969 | int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize; |
| 1970 | masm_->add(sp, sp, Operand(sp_delta)); |
| 1971 | masm_->Jump(lr); |
| 1972 | DeleteFrame(); |
| 1973 | |
| 1974 | #ifdef DEBUG |
| 1975 | // Check that the size of the code used for returning matches what is |
| 1976 | // expected by the debugger. If the sp_delts above cannot be encoded in |
| 1977 | // the add instruction the add will generate two instructions. |
| 1978 | int return_sequence_length = |
| 1979 | masm_->InstructionsGeneratedSince(&check_exit_codesize); |
| 1980 | CHECK(return_sequence_length == |
| 1981 | Assembler::kJSReturnSequenceInstructions || |
| 1982 | return_sequence_length == |
| 1983 | Assembler::kJSReturnSequenceInstructions + 1); |
| 1984 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | |
| 1989 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
| 1990 | #ifdef DEBUG |
| 1991 | int original_height = frame_->height(); |
| 1992 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1993 | Comment cmnt(masm_, "[ WithEnterStatement"); |
| 1994 | CodeForStatementPosition(node); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1995 | Load(node->expression()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1996 | if (node->is_catch_block()) { |
| 1997 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
| 1998 | } else { |
| 1999 | frame_->CallRuntime(Runtime::kPushContext, 1); |
| 2000 | } |
| 2001 | #ifdef DEBUG |
| 2002 | JumpTarget verified_true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2003 | __ cmp(r0, cp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2004 | verified_true.Branch(eq); |
| 2005 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 2006 | verified_true.Bind(); |
| 2007 | #endif |
| 2008 | // Update context local. |
| 2009 | __ str(cp, frame_->Context()); |
| 2010 | ASSERT(frame_->height() == original_height); |
| 2011 | } |
| 2012 | |
| 2013 | |
| 2014 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
| 2015 | #ifdef DEBUG |
| 2016 | int original_height = frame_->height(); |
| 2017 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2018 | Comment cmnt(masm_, "[ WithExitStatement"); |
| 2019 | CodeForStatementPosition(node); |
| 2020 | // Pop context. |
| 2021 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 2022 | // Update context local. |
| 2023 | __ str(cp, frame_->Context()); |
| 2024 | ASSERT(frame_->height() == original_height); |
| 2025 | } |
| 2026 | |
| 2027 | |
| 2028 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
| 2029 | #ifdef DEBUG |
| 2030 | int original_height = frame_->height(); |
| 2031 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2032 | Comment cmnt(masm_, "[ SwitchStatement"); |
| 2033 | CodeForStatementPosition(node); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2034 | node->break_target()->SetExpectedHeight(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2035 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2036 | Load(node->tag()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2037 | |
| 2038 | JumpTarget next_test; |
| 2039 | JumpTarget fall_through; |
| 2040 | JumpTarget default_entry; |
| 2041 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
| 2042 | ZoneList<CaseClause*>* cases = node->cases(); |
| 2043 | int length = cases->length(); |
| 2044 | CaseClause* default_clause = NULL; |
| 2045 | |
| 2046 | for (int i = 0; i < length; i++) { |
| 2047 | CaseClause* clause = cases->at(i); |
| 2048 | if (clause->is_default()) { |
| 2049 | // Remember the default clause and compile it at the end. |
| 2050 | default_clause = clause; |
| 2051 | continue; |
| 2052 | } |
| 2053 | |
| 2054 | Comment cmnt(masm_, "[ Case clause"); |
| 2055 | // Compile the test. |
| 2056 | next_test.Bind(); |
| 2057 | next_test.Unuse(); |
| 2058 | // Duplicate TOS. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2059 | frame_->Dup(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2060 | Comparison(eq, NULL, clause->label(), true); |
| 2061 | Branch(false, &next_test); |
| 2062 | |
| 2063 | // Before entering the body from the test, remove the switch value from |
| 2064 | // the stack. |
| 2065 | frame_->Drop(); |
| 2066 | |
| 2067 | // Label the body so that fall through is enabled. |
| 2068 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 2069 | default_exit.Bind(); |
| 2070 | } else { |
| 2071 | fall_through.Bind(); |
| 2072 | fall_through.Unuse(); |
| 2073 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2074 | VisitStatements(clause->statements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2075 | |
| 2076 | // If control flow can fall through from the body, jump to the next body |
| 2077 | // or the end of the statement. |
| 2078 | if (frame_ != NULL) { |
| 2079 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 2080 | default_entry.Jump(); |
| 2081 | } else { |
| 2082 | fall_through.Jump(); |
| 2083 | } |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | // The final "test" removes the switch value. |
| 2088 | next_test.Bind(); |
| 2089 | frame_->Drop(); |
| 2090 | |
| 2091 | // If there is a default clause, compile it. |
| 2092 | if (default_clause != NULL) { |
| 2093 | Comment cmnt(masm_, "[ Default clause"); |
| 2094 | default_entry.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2095 | VisitStatements(default_clause->statements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2096 | // If control flow can fall out of the default and there is a case after |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2097 | // it, jump to that case's body. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2098 | if (frame_ != NULL && default_exit.is_bound()) { |
| 2099 | default_exit.Jump(); |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | if (fall_through.is_linked()) { |
| 2104 | fall_through.Bind(); |
| 2105 | } |
| 2106 | |
| 2107 | if (node->break_target()->is_linked()) { |
| 2108 | node->break_target()->Bind(); |
| 2109 | } |
| 2110 | node->break_target()->Unuse(); |
| 2111 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2112 | } |
| 2113 | |
| 2114 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2115 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2116 | #ifdef DEBUG |
| 2117 | int original_height = frame_->height(); |
| 2118 | #endif |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2119 | Comment cmnt(masm_, "[ DoWhileStatement"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2120 | CodeForStatementPosition(node); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2121 | node->break_target()->SetExpectedHeight(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2122 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2123 | IncrementLoopNesting(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2124 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2125 | // Label the top of the loop for the backward CFG edge. If the test |
| 2126 | // is always true we can use the continue target, and if the test is |
| 2127 | // always false there is no need. |
| 2128 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 2129 | switch (info) { |
| 2130 | case ALWAYS_TRUE: |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2131 | node->continue_target()->SetExpectedHeight(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2132 | node->continue_target()->Bind(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2133 | break; |
| 2134 | case ALWAYS_FALSE: |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2135 | node->continue_target()->SetExpectedHeight(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2136 | break; |
| 2137 | case DONT_KNOW: |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2138 | node->continue_target()->SetExpectedHeight(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2139 | body.Bind(); |
| 2140 | break; |
| 2141 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2142 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2143 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2144 | Visit(node->body()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2145 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2146 | // Compile the test. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2147 | switch (info) { |
| 2148 | case ALWAYS_TRUE: |
| 2149 | // If control can fall off the end of the body, jump back to the |
| 2150 | // top. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2151 | if (has_valid_frame()) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2152 | node->continue_target()->Jump(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2153 | } |
| 2154 | break; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2155 | case ALWAYS_FALSE: |
| 2156 | // If we have a continue in the body, we only have to bind its |
| 2157 | // jump target. |
| 2158 | if (node->continue_target()->is_linked()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2159 | node->continue_target()->Bind(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2160 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2161 | break; |
| 2162 | case DONT_KNOW: |
| 2163 | // We have to compile the test expression if it can be reached by |
| 2164 | // control flow falling out of the body or via continue. |
| 2165 | if (node->continue_target()->is_linked()) { |
| 2166 | node->continue_target()->Bind(); |
| 2167 | } |
| 2168 | if (has_valid_frame()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2169 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 2170 | CodeForDoWhileConditionPosition(node); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2171 | LoadCondition(node->cond(), &body, node->break_target(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2172 | if (has_valid_frame()) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2173 | // A invalid frame here indicates that control did not |
| 2174 | // fall out of the test expression. |
| 2175 | Branch(true, &body); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | if (node->break_target()->is_linked()) { |
| 2182 | node->break_target()->Bind(); |
| 2183 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2184 | DecrementLoopNesting(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2185 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 2190 | #ifdef DEBUG |
| 2191 | int original_height = frame_->height(); |
| 2192 | #endif |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2193 | Comment cmnt(masm_, "[ WhileStatement"); |
| 2194 | CodeForStatementPosition(node); |
| 2195 | |
| 2196 | // If the test is never true and has no side effects there is no need |
| 2197 | // to compile the test or body. |
| 2198 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 2199 | if (info == ALWAYS_FALSE) return; |
| 2200 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2201 | node->break_target()->SetExpectedHeight(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2202 | IncrementLoopNesting(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2203 | |
| 2204 | // Label the top of the loop with the continue target for the backward |
| 2205 | // CFG edge. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2206 | node->continue_target()->SetExpectedHeight(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2207 | node->continue_target()->Bind(); |
| 2208 | |
| 2209 | if (info == DONT_KNOW) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2210 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2211 | LoadCondition(node->cond(), &body, node->break_target(), true); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2212 | if (has_valid_frame()) { |
| 2213 | // A NULL frame indicates that control did not fall out of the |
| 2214 | // test expression. |
| 2215 | Branch(false, node->break_target()); |
| 2216 | } |
| 2217 | if (has_valid_frame() || body.is_linked()) { |
| 2218 | body.Bind(); |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | if (has_valid_frame()) { |
| 2223 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2224 | Visit(node->body()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2225 | |
| 2226 | // If control flow can fall out of the body, jump back to the top. |
| 2227 | if (has_valid_frame()) { |
| 2228 | node->continue_target()->Jump(); |
| 2229 | } |
| 2230 | } |
| 2231 | if (node->break_target()->is_linked()) { |
| 2232 | node->break_target()->Bind(); |
| 2233 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2234 | DecrementLoopNesting(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2235 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2236 | } |
| 2237 | |
| 2238 | |
| 2239 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 2240 | #ifdef DEBUG |
| 2241 | int original_height = frame_->height(); |
| 2242 | #endif |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2243 | Comment cmnt(masm_, "[ ForStatement"); |
| 2244 | CodeForStatementPosition(node); |
| 2245 | if (node->init() != NULL) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2246 | Visit(node->init()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | // If the test is never true there is no need to compile the test or |
| 2250 | // body. |
| 2251 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 2252 | if (info == ALWAYS_FALSE) return; |
| 2253 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2254 | node->break_target()->SetExpectedHeight(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2255 | IncrementLoopNesting(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2256 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2257 | // We know that the loop index is a smi if it is not modified in the |
| 2258 | // loop body and it is checked against a constant limit in the loop |
| 2259 | // condition. In this case, we reset the static type information of the |
| 2260 | // loop index to smi before compiling the body, the update expression, and |
| 2261 | // the bottom check of the loop condition. |
| 2262 | TypeInfoCodeGenState type_info_scope(this, |
| 2263 | node->is_fast_smi_loop() ? |
| 2264 | node->loop_variable()->slot() : |
| 2265 | NULL, |
| 2266 | TypeInfo::Smi()); |
| 2267 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2268 | // If there is no update statement, label the top of the loop with the |
| 2269 | // continue target, otherwise with the loop target. |
| 2270 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 2271 | if (node->next() == NULL) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2272 | node->continue_target()->SetExpectedHeight(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2273 | node->continue_target()->Bind(); |
| 2274 | } else { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2275 | node->continue_target()->SetExpectedHeight(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2276 | loop.Bind(); |
| 2277 | } |
| 2278 | |
| 2279 | // If the test is always true, there is no need to compile it. |
| 2280 | if (info == DONT_KNOW) { |
| 2281 | JumpTarget body; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2282 | LoadCondition(node->cond(), &body, node->break_target(), true); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2283 | if (has_valid_frame()) { |
| 2284 | Branch(false, node->break_target()); |
| 2285 | } |
| 2286 | if (has_valid_frame() || body.is_linked()) { |
| 2287 | body.Bind(); |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | if (has_valid_frame()) { |
| 2292 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2293 | Visit(node->body()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2294 | |
| 2295 | if (node->next() == NULL) { |
| 2296 | // If there is no update statement and control flow can fall out |
| 2297 | // of the loop, jump directly to the continue label. |
| 2298 | if (has_valid_frame()) { |
| 2299 | node->continue_target()->Jump(); |
| 2300 | } |
| 2301 | } else { |
| 2302 | // If there is an update statement and control flow can reach it |
| 2303 | // via falling out of the body of the loop or continuing, we |
| 2304 | // compile the update statement. |
| 2305 | if (node->continue_target()->is_linked()) { |
| 2306 | node->continue_target()->Bind(); |
| 2307 | } |
| 2308 | if (has_valid_frame()) { |
| 2309 | // Record source position of the statement as this code which is |
| 2310 | // after the code for the body actually belongs to the loop |
| 2311 | // statement and not the body. |
| 2312 | CodeForStatementPosition(node); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2313 | Visit(node->next()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2314 | loop.Jump(); |
| 2315 | } |
| 2316 | } |
| 2317 | } |
| 2318 | if (node->break_target()->is_linked()) { |
| 2319 | node->break_target()->Bind(); |
| 2320 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2321 | DecrementLoopNesting(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2322 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2323 | } |
| 2324 | |
| 2325 | |
| 2326 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
| 2327 | #ifdef DEBUG |
| 2328 | int original_height = frame_->height(); |
| 2329 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2330 | Comment cmnt(masm_, "[ ForInStatement"); |
| 2331 | CodeForStatementPosition(node); |
| 2332 | |
| 2333 | JumpTarget primitive; |
| 2334 | JumpTarget jsobject; |
| 2335 | JumpTarget fixed_array; |
| 2336 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 2337 | JumpTarget end_del_check; |
| 2338 | JumpTarget exit; |
| 2339 | |
| 2340 | // Get the object to enumerate over (converted to JSObject). |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2341 | Load(node->enumerable()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2342 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2343 | VirtualFrame::SpilledScope spilled_scope(frame_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2344 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 2345 | // to the specification. 12.6.4 mandates a call to ToObject. |
| 2346 | frame_->EmitPop(r0); |
| 2347 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2348 | __ cmp(r0, ip); |
| 2349 | exit.Branch(eq); |
| 2350 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2351 | __ cmp(r0, ip); |
| 2352 | exit.Branch(eq); |
| 2353 | |
| 2354 | // Stack layout in body: |
| 2355 | // [iteration counter (Smi)] |
| 2356 | // [length of array] |
| 2357 | // [FixedArray] |
| 2358 | // [Map or 0] |
| 2359 | // [Object] |
| 2360 | |
| 2361 | // Check if enumerable is already a JSObject |
| 2362 | __ tst(r0, Operand(kSmiTagMask)); |
| 2363 | primitive.Branch(eq); |
| 2364 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
| 2365 | jsobject.Branch(hs); |
| 2366 | |
| 2367 | primitive.Bind(); |
| 2368 | frame_->EmitPush(r0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2369 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2370 | |
| 2371 | jsobject.Bind(); |
| 2372 | // Get the set of properties (as a FixedArray or Map). |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2373 | // r0: value to be iterated over |
| 2374 | frame_->EmitPush(r0); // Push the object being iterated over. |
| 2375 | |
| 2376 | // Check cache validity in generated code. This is a fast case for |
| 2377 | // the JSObject::IsSimpleEnum cache validity checks. If we cannot |
| 2378 | // guarantee cache validity, call the runtime system to check cache |
| 2379 | // validity or get the property names in a fixed array. |
| 2380 | JumpTarget call_runtime; |
| 2381 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 2382 | JumpTarget check_prototype; |
| 2383 | JumpTarget use_cache; |
| 2384 | __ mov(r1, Operand(r0)); |
| 2385 | loop.Bind(); |
| 2386 | // Check that there are no elements. |
| 2387 | __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 2388 | __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex); |
| 2389 | __ cmp(r2, r4); |
| 2390 | call_runtime.Branch(ne); |
| 2391 | // Check that instance descriptors are not empty so that we can |
| 2392 | // check for an enum cache. Leave the map in r3 for the subsequent |
| 2393 | // prototype load. |
| 2394 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 2395 | __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset)); |
| 2396 | __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex); |
| 2397 | __ cmp(r2, ip); |
| 2398 | call_runtime.Branch(eq); |
| 2399 | // Check that there in an enum cache in the non-empty instance |
| 2400 | // descriptors. This is the case if the next enumeration index |
| 2401 | // field does not contain a smi. |
| 2402 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset)); |
| 2403 | __ tst(r2, Operand(kSmiTagMask)); |
| 2404 | call_runtime.Branch(eq); |
| 2405 | // For all objects but the receiver, check that the cache is empty. |
| 2406 | // r4: empty fixed array root. |
| 2407 | __ cmp(r1, r0); |
| 2408 | check_prototype.Branch(eq); |
| 2409 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 2410 | __ cmp(r2, r4); |
| 2411 | call_runtime.Branch(ne); |
| 2412 | check_prototype.Bind(); |
| 2413 | // Load the prototype from the map and loop if non-null. |
| 2414 | __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 2415 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2416 | __ cmp(r1, ip); |
| 2417 | loop.Branch(ne); |
| 2418 | // The enum cache is valid. Load the map of the object being |
| 2419 | // iterated over and use the cache for the iteration. |
| 2420 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 2421 | use_cache.Jump(); |
| 2422 | |
| 2423 | call_runtime.Bind(); |
| 2424 | // Call the runtime to get the property names for the object. |
| 2425 | frame_->EmitPush(r0); // push the object (slot 4) for the runtime call |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2426 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
| 2427 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2428 | // If we got a map from the runtime call, we can do a fast |
| 2429 | // modification check. Otherwise, we got a fixed array, and we have |
| 2430 | // to do a slow check. |
| 2431 | // r0: map or fixed array (result from call to |
| 2432 | // Runtime::kGetPropertyNamesFast) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2433 | __ mov(r2, Operand(r0)); |
| 2434 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 2435 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 2436 | __ cmp(r1, ip); |
| 2437 | fixed_array.Branch(ne); |
| 2438 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2439 | use_cache.Bind(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2440 | // Get enum cache |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2441 | // r0: map (either the result from a call to |
| 2442 | // Runtime::kGetPropertyNamesFast or has been fetched directly from |
| 2443 | // the object) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2444 | __ mov(r1, Operand(r0)); |
| 2445 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 2446 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 2447 | __ ldr(r2, |
| 2448 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 2449 | |
| 2450 | frame_->EmitPush(r0); // map |
| 2451 | frame_->EmitPush(r2); // enum cache bridge cache |
| 2452 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2453 | frame_->EmitPush(r0); |
| 2454 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2455 | frame_->EmitPush(r0); |
| 2456 | entry.Jump(); |
| 2457 | |
| 2458 | fixed_array.Bind(); |
| 2459 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 2460 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 2461 | frame_->EmitPush(r0); |
| 2462 | |
| 2463 | // Push the length of the array and the initial index onto the stack. |
| 2464 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2465 | frame_->EmitPush(r0); |
| 2466 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
| 2467 | frame_->EmitPush(r0); |
| 2468 | |
| 2469 | // Condition. |
| 2470 | entry.Bind(); |
| 2471 | // sp[0] : index |
| 2472 | // sp[1] : array/enum cache length |
| 2473 | // sp[2] : array or enum cache |
| 2474 | // sp[3] : 0 or map |
| 2475 | // sp[4] : enumerable |
| 2476 | // Grab the current frame's height for the break and continue |
| 2477 | // targets only after all the state is pushed on the frame. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2478 | node->break_target()->SetExpectedHeight(); |
| 2479 | node->continue_target()->SetExpectedHeight(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2480 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2481 | // Load the current count to r0, load the length to r1. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2482 | __ Ldrd(r0, r1, frame_->ElementAt(0)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2483 | __ cmp(r0, r1); // compare to the array length |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2484 | node->break_target()->Branch(hs); |
| 2485 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2486 | // Get the i'th entry of the array. |
| 2487 | __ ldr(r2, frame_->ElementAt(2)); |
| 2488 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 2489 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 2490 | |
| 2491 | // Get Map or 0. |
| 2492 | __ ldr(r2, frame_->ElementAt(3)); |
| 2493 | // Check if this (still) matches the map of the enumerable. |
| 2494 | // If not, we have to filter the key. |
| 2495 | __ ldr(r1, frame_->ElementAt(4)); |
| 2496 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 2497 | __ cmp(r1, Operand(r2)); |
| 2498 | end_del_check.Branch(eq); |
| 2499 | |
| 2500 | // Convert the entry to a string (or null if it isn't a property anymore). |
| 2501 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 2502 | frame_->EmitPush(r0); |
| 2503 | frame_->EmitPush(r3); // push entry |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2504 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2505 | __ mov(r3, Operand(r0), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2506 | // If the property has been removed while iterating, we just skip it. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2507 | node->continue_target()->Branch(eq); |
| 2508 | |
| 2509 | end_del_check.Bind(); |
| 2510 | // Store the entry in the 'each' expression and take another spin in the |
| 2511 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 2512 | frame_->EmitPush(r3); // push entry |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2513 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2514 | Reference each(this, node->each()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2515 | if (!each.is_illegal()) { |
| 2516 | if (each.size() > 0) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2517 | // Loading a reference may leave the frame in an unspilled state. |
| 2518 | frame_->SpillAll(); // Sync stack to memory. |
| 2519 | // Get the value (under the reference on the stack) from memory. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2520 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 2521 | frame_->EmitPush(r0); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2522 | each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2523 | frame_->Drop(2); // The result of the set and the extra pushed value. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2524 | } else { |
| 2525 | // If the reference was to a slot we rely on the convenient property |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2526 | // that it doesn't matter whether a value (eg, ebx pushed above) is |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2527 | // right on top of or right underneath a zero-sized reference. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2528 | each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2529 | frame_->Drop(1); // Drop the result of the set operation. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2530 | } |
| 2531 | } |
| 2532 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2533 | // Body. |
| 2534 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2535 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2536 | Visit(node->body()); |
| 2537 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2538 | |
| 2539 | // Next. Reestablish a spilled frame in case we are coming here via |
| 2540 | // a continue in the body. |
| 2541 | node->continue_target()->Bind(); |
| 2542 | frame_->SpillAll(); |
| 2543 | frame_->EmitPop(r0); |
| 2544 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 2545 | frame_->EmitPush(r0); |
| 2546 | entry.Jump(); |
| 2547 | |
| 2548 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 2549 | // any frame. |
| 2550 | node->break_target()->Bind(); |
| 2551 | frame_->Drop(5); |
| 2552 | |
| 2553 | // Exit. |
| 2554 | exit.Bind(); |
| 2555 | node->continue_target()->Unuse(); |
| 2556 | node->break_target()->Unuse(); |
| 2557 | ASSERT(frame_->height() == original_height); |
| 2558 | } |
| 2559 | |
| 2560 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2561 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2562 | #ifdef DEBUG |
| 2563 | int original_height = frame_->height(); |
| 2564 | #endif |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2565 | VirtualFrame::SpilledScope spilled_scope(frame_); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2566 | Comment cmnt(masm_, "[ TryCatchStatement"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2567 | CodeForStatementPosition(node); |
| 2568 | |
| 2569 | JumpTarget try_block; |
| 2570 | JumpTarget exit; |
| 2571 | |
| 2572 | try_block.Call(); |
| 2573 | // --- Catch block --- |
| 2574 | frame_->EmitPush(r0); |
| 2575 | |
| 2576 | // Store the caught exception in the catch variable. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2577 | Variable* catch_var = node->catch_var()->var(); |
| 2578 | ASSERT(catch_var != NULL && catch_var->slot() != NULL); |
| 2579 | StoreToSlot(catch_var->slot(), NOT_CONST_INIT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2580 | |
| 2581 | // Remove the exception from the stack. |
| 2582 | frame_->Drop(); |
| 2583 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2584 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2585 | VisitStatements(node->catch_block()->statements()); |
| 2586 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2587 | if (frame_ != NULL) { |
| 2588 | exit.Jump(); |
| 2589 | } |
| 2590 | |
| 2591 | |
| 2592 | // --- Try block --- |
| 2593 | try_block.Bind(); |
| 2594 | |
| 2595 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2596 | int handler_height = frame_->height(); |
| 2597 | |
| 2598 | // Shadow the labels for all escapes from the try block, including |
| 2599 | // returns. During shadowing, the original label is hidden as the |
| 2600 | // LabelShadow and operations on the original actually affect the |
| 2601 | // shadowing label. |
| 2602 | // |
| 2603 | // We should probably try to unify the escaping labels and the return |
| 2604 | // label. |
| 2605 | int nof_escapes = node->escaping_targets()->length(); |
| 2606 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2607 | |
| 2608 | // Add the shadow target for the function return. |
| 2609 | static const int kReturnShadowIndex = 0; |
| 2610 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2611 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2612 | function_return_is_shadowed_ = true; |
| 2613 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2614 | |
| 2615 | // Add the remaining shadow targets. |
| 2616 | for (int i = 0; i < nof_escapes; i++) { |
| 2617 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
| 2618 | } |
| 2619 | |
| 2620 | // Generate code for the statements in the try block. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2621 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2622 | VisitStatements(node->try_block()->statements()); |
| 2623 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2624 | |
| 2625 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2626 | // After shadowing stops, the original labels are unshadowed and the |
| 2627 | // LabelShadows represent the formerly shadowing labels. |
| 2628 | bool has_unlinks = false; |
| 2629 | for (int i = 0; i < shadows.length(); i++) { |
| 2630 | shadows[i]->StopShadowing(); |
| 2631 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2632 | } |
| 2633 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2634 | |
| 2635 | // Get an external reference to the handler address. |
| 2636 | ExternalReference handler_address(Top::k_handler_address); |
| 2637 | |
| 2638 | // If we can fall off the end of the try block, unlink from try chain. |
| 2639 | if (has_valid_frame()) { |
| 2640 | // The next handler address is on top of the frame. Unlink from |
| 2641 | // the handler list and drop the rest of this handler from the |
| 2642 | // frame. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 2643 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2644 | frame_->EmitPop(r1); // r0 can contain the return value. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2645 | __ mov(r3, Operand(handler_address)); |
| 2646 | __ str(r1, MemOperand(r3)); |
| 2647 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
| 2648 | if (has_unlinks) { |
| 2649 | exit.Jump(); |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | // Generate unlink code for the (formerly) shadowing labels that have been |
| 2654 | // jumped to. Deallocate each shadow target. |
| 2655 | for (int i = 0; i < shadows.length(); i++) { |
| 2656 | if (shadows[i]->is_linked()) { |
| 2657 | // Unlink from try chain; |
| 2658 | shadows[i]->Bind(); |
| 2659 | // Because we can be jumping here (to spilled code) from unspilled |
| 2660 | // code, we need to reestablish a spilled frame at this block. |
| 2661 | frame_->SpillAll(); |
| 2662 | |
| 2663 | // Reload sp from the top handler, because some statements that we |
| 2664 | // break from (eg, for...in) may have left stuff on the stack. |
| 2665 | __ mov(r3, Operand(handler_address)); |
| 2666 | __ ldr(sp, MemOperand(r3)); |
| 2667 | frame_->Forget(frame_->height() - handler_height); |
| 2668 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 2669 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2670 | frame_->EmitPop(r1); // r0 can contain the return value. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2671 | __ str(r1, MemOperand(r3)); |
| 2672 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
| 2673 | |
| 2674 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2675 | frame_->PrepareForReturn(); |
| 2676 | } |
| 2677 | shadows[i]->other_target()->Jump(); |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | exit.Bind(); |
| 2682 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2683 | } |
| 2684 | |
| 2685 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2686 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2687 | #ifdef DEBUG |
| 2688 | int original_height = frame_->height(); |
| 2689 | #endif |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2690 | VirtualFrame::SpilledScope spilled_scope(frame_); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2691 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2692 | CodeForStatementPosition(node); |
| 2693 | |
| 2694 | // State: Used to keep track of reason for entering the finally |
| 2695 | // block. Should probably be extended to hold information for |
| 2696 | // break/continue from within the try block. |
| 2697 | enum { FALLING, THROWING, JUMPING }; |
| 2698 | |
| 2699 | JumpTarget try_block; |
| 2700 | JumpTarget finally_block; |
| 2701 | |
| 2702 | try_block.Call(); |
| 2703 | |
| 2704 | frame_->EmitPush(r0); // save exception object on the stack |
| 2705 | // In case of thrown exceptions, this is where we continue. |
| 2706 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
| 2707 | finally_block.Jump(); |
| 2708 | |
| 2709 | // --- Try block --- |
| 2710 | try_block.Bind(); |
| 2711 | |
| 2712 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2713 | int handler_height = frame_->height(); |
| 2714 | |
| 2715 | // Shadow the labels for all escapes from the try block, including |
| 2716 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2717 | // operations on the original actually affect the shadowing label. |
| 2718 | // |
| 2719 | // We should probably try to unify the escaping labels and the return |
| 2720 | // label. |
| 2721 | int nof_escapes = node->escaping_targets()->length(); |
| 2722 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2723 | |
| 2724 | // Add the shadow target for the function return. |
| 2725 | static const int kReturnShadowIndex = 0; |
| 2726 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2727 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2728 | function_return_is_shadowed_ = true; |
| 2729 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2730 | |
| 2731 | // Add the remaining shadow targets. |
| 2732 | for (int i = 0; i < nof_escapes; i++) { |
| 2733 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
| 2734 | } |
| 2735 | |
| 2736 | // Generate code for the statements in the try block. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2737 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2738 | VisitStatements(node->try_block()->statements()); |
| 2739 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2740 | |
| 2741 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2742 | // After shadowing stops, the original labels are unshadowed and the |
| 2743 | // LabelShadows represent the formerly shadowing labels. |
| 2744 | int nof_unlinks = 0; |
| 2745 | for (int i = 0; i < shadows.length(); i++) { |
| 2746 | shadows[i]->StopShadowing(); |
| 2747 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2748 | } |
| 2749 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2750 | |
| 2751 | // Get an external reference to the handler address. |
| 2752 | ExternalReference handler_address(Top::k_handler_address); |
| 2753 | |
| 2754 | // If we can fall off the end of the try block, unlink from the try |
| 2755 | // chain and set the state on the frame to FALLING. |
| 2756 | if (has_valid_frame()) { |
| 2757 | // The next handler address is on top of the frame. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 2758 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2759 | frame_->EmitPop(r1); |
| 2760 | __ mov(r3, Operand(handler_address)); |
| 2761 | __ str(r1, MemOperand(r3)); |
| 2762 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
| 2763 | |
| 2764 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2765 | // state in r2, then jump around the unlink blocks if any. |
| 2766 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
| 2767 | frame_->EmitPush(r0); |
| 2768 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2769 | if (nof_unlinks > 0) { |
| 2770 | finally_block.Jump(); |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | // Generate code to unlink and set the state for the (formerly) |
| 2775 | // shadowing targets that have been jumped to. |
| 2776 | for (int i = 0; i < shadows.length(); i++) { |
| 2777 | if (shadows[i]->is_linked()) { |
| 2778 | // If we have come from the shadowed return, the return value is |
| 2779 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2780 | // until it is pushed. |
| 2781 | // |
| 2782 | // Because we can be jumping here (to spilled code) from |
| 2783 | // unspilled code, we need to reestablish a spilled frame at |
| 2784 | // this block. |
| 2785 | shadows[i]->Bind(); |
| 2786 | frame_->SpillAll(); |
| 2787 | |
| 2788 | // Reload sp from the top handler, because some statements that |
| 2789 | // we break from (eg, for...in) may have left stuff on the |
| 2790 | // stack. |
| 2791 | __ mov(r3, Operand(handler_address)); |
| 2792 | __ ldr(sp, MemOperand(r3)); |
| 2793 | frame_->Forget(frame_->height() - handler_height); |
| 2794 | |
| 2795 | // Unlink this handler and drop it from the frame. The next |
| 2796 | // handler address is currently on top of the frame. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 2797 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2798 | frame_->EmitPop(r1); |
| 2799 | __ str(r1, MemOperand(r3)); |
| 2800 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
| 2801 | |
| 2802 | if (i == kReturnShadowIndex) { |
| 2803 | // If this label shadowed the function return, materialize the |
| 2804 | // return value on the stack. |
| 2805 | frame_->EmitPush(r0); |
| 2806 | } else { |
| 2807 | // Fake TOS for targets that shadowed breaks and continues. |
| 2808 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
| 2809 | frame_->EmitPush(r0); |
| 2810 | } |
| 2811 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2812 | if (--nof_unlinks > 0) { |
| 2813 | // If this is not the last unlink block, jump around the next. |
| 2814 | finally_block.Jump(); |
| 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | |
| 2819 | // --- Finally block --- |
| 2820 | finally_block.Bind(); |
| 2821 | |
| 2822 | // Push the state on the stack. |
| 2823 | frame_->EmitPush(r2); |
| 2824 | |
| 2825 | // We keep two elements on the stack - the (possibly faked) result |
| 2826 | // and the state - while evaluating the finally block. |
| 2827 | // |
| 2828 | // Generate code for the statements in the finally block. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 2829 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2830 | VisitStatements(node->finally_block()->statements()); |
| 2831 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2832 | |
| 2833 | if (has_valid_frame()) { |
| 2834 | // Restore state and return value or faked TOS. |
| 2835 | frame_->EmitPop(r2); |
| 2836 | frame_->EmitPop(r0); |
| 2837 | } |
| 2838 | |
| 2839 | // Generate code to jump to the right destination for all used |
| 2840 | // formerly shadowing targets. Deallocate each shadow target. |
| 2841 | for (int i = 0; i < shadows.length(); i++) { |
| 2842 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2843 | JumpTarget* original = shadows[i]->other_target(); |
| 2844 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2845 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2846 | JumpTarget skip; |
| 2847 | skip.Branch(ne); |
| 2848 | frame_->PrepareForReturn(); |
| 2849 | original->Jump(); |
| 2850 | skip.Bind(); |
| 2851 | } else { |
| 2852 | original->Branch(eq); |
| 2853 | } |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | if (has_valid_frame()) { |
| 2858 | // Check if we need to rethrow the exception. |
| 2859 | JumpTarget exit; |
| 2860 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2861 | exit.Branch(ne); |
| 2862 | |
| 2863 | // Rethrow exception. |
| 2864 | frame_->EmitPush(r0); |
| 2865 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2866 | |
| 2867 | // Done. |
| 2868 | exit.Bind(); |
| 2869 | } |
| 2870 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2871 | } |
| 2872 | |
| 2873 | |
| 2874 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
| 2875 | #ifdef DEBUG |
| 2876 | int original_height = frame_->height(); |
| 2877 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2878 | Comment cmnt(masm_, "[ DebuggerStatament"); |
| 2879 | CodeForStatementPosition(node); |
| 2880 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2881 | frame_->DebugBreak(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2882 | #endif |
| 2883 | // Ignore the return value. |
| 2884 | ASSERT(frame_->height() == original_height); |
| 2885 | } |
| 2886 | |
| 2887 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2888 | void CodeGenerator::InstantiateFunction( |
| 2889 | Handle<SharedFunctionInfo> function_info) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2890 | // Use the fast case closure allocation code that allocates in new |
| 2891 | // space for nested functions that don't need literals cloning. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2892 | if (scope()->is_function_scope() && function_info->num_literals() == 0) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2893 | FastNewClosureStub stub; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2894 | frame_->EmitPush(Operand(function_info)); |
| 2895 | frame_->SpillAll(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2896 | frame_->CallStub(&stub, 1); |
| 2897 | frame_->EmitPush(r0); |
| 2898 | } else { |
| 2899 | // Create a new closure. |
| 2900 | frame_->EmitPush(cp); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2901 | frame_->EmitPush(Operand(function_info)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2902 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2903 | frame_->EmitPush(r0); |
| 2904 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | |
| 2908 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
| 2909 | #ifdef DEBUG |
| 2910 | int original_height = frame_->height(); |
| 2911 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2912 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2913 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2914 | // Build the function info and instantiate it. |
| 2915 | Handle<SharedFunctionInfo> function_info = |
| 2916 | Compiler::BuildFunctionInfo(node, script(), this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2917 | // Check for stack-overflow exception. |
| 2918 | if (HasStackOverflow()) { |
| 2919 | ASSERT(frame_->height() == original_height); |
| 2920 | return; |
| 2921 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2922 | InstantiateFunction(function_info); |
| 2923 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2927 | void CodeGenerator::VisitSharedFunctionInfoLiteral( |
| 2928 | SharedFunctionInfoLiteral* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2929 | #ifdef DEBUG |
| 2930 | int original_height = frame_->height(); |
| 2931 | #endif |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2932 | Comment cmnt(masm_, "[ SharedFunctionInfoLiteral"); |
| 2933 | InstantiateFunction(node->shared_function_info()); |
| 2934 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | |
| 2938 | void CodeGenerator::VisitConditional(Conditional* node) { |
| 2939 | #ifdef DEBUG |
| 2940 | int original_height = frame_->height(); |
| 2941 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2942 | Comment cmnt(masm_, "[ Conditional"); |
| 2943 | JumpTarget then; |
| 2944 | JumpTarget else_; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2945 | LoadCondition(node->condition(), &then, &else_, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2946 | if (has_valid_frame()) { |
| 2947 | Branch(false, &else_); |
| 2948 | } |
| 2949 | if (has_valid_frame() || then.is_linked()) { |
| 2950 | then.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2951 | Load(node->then_expression()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2952 | } |
| 2953 | if (else_.is_linked()) { |
| 2954 | JumpTarget exit; |
| 2955 | if (has_valid_frame()) exit.Jump(); |
| 2956 | else_.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2957 | Load(node->else_expression()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2958 | if (exit.is_linked()) exit.Bind(); |
| 2959 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2960 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | |
| 2964 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2965 | if (slot->type() == Slot::LOOKUP) { |
| 2966 | ASSERT(slot->var()->is_dynamic()); |
| 2967 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2968 | // JumpTargets do not yet support merging frames so the frame must be |
| 2969 | // spilled when jumping to these targets. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2970 | JumpTarget slow; |
| 2971 | JumpTarget done; |
| 2972 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2973 | // Generate fast case for loading from slots that correspond to |
| 2974 | // local/global variables or arguments unless they are shadowed by |
| 2975 | // eval-introduced bindings. |
| 2976 | EmitDynamicLoadFromSlotFastCase(slot, |
| 2977 | typeof_state, |
| 2978 | &slow, |
| 2979 | &done); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2980 | |
| 2981 | slow.Bind(); |
| 2982 | frame_->EmitPush(cp); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2983 | frame_->EmitPush(Operand(slot->var()->name())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2984 | |
| 2985 | if (typeof_state == INSIDE_TYPEOF) { |
| 2986 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
| 2987 | } else { |
| 2988 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
| 2989 | } |
| 2990 | |
| 2991 | done.Bind(); |
| 2992 | frame_->EmitPush(r0); |
| 2993 | |
| 2994 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2995 | Register scratch = VirtualFrame::scratch0(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2996 | TypeInfo info = type_info(slot); |
| 2997 | frame_->EmitPush(SlotOperand(slot, scratch), info); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2998 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2999 | if (slot->var()->mode() == Variable::CONST) { |
| 3000 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 3001 | // initialized yet) which needs to be converted into the 'undefined' |
| 3002 | // value. |
| 3003 | Comment cmnt(masm_, "[ Unhole const"); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3004 | Register tos = frame_->PopToRegister(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3005 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3006 | __ cmp(tos, ip); |
| 3007 | __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq); |
| 3008 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3014 | void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot, |
| 3015 | TypeofState state) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3016 | VirtualFrame::RegisterAllocationScope scope(this); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3017 | LoadFromSlot(slot, state); |
| 3018 | |
| 3019 | // Bail out quickly if we're not using lazy arguments allocation. |
| 3020 | if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return; |
| 3021 | |
| 3022 | // ... or if the slot isn't a non-parameter arguments slot. |
| 3023 | if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return; |
| 3024 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3025 | // Load the loaded value from the stack into a register but leave it on the |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3026 | // stack. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3027 | Register tos = frame_->Peek(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3028 | |
| 3029 | // If the loaded value is the sentinel that indicates that we |
| 3030 | // haven't loaded the arguments object yet, we need to do it now. |
| 3031 | JumpTarget exit; |
| 3032 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3033 | __ cmp(tos, ip); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3034 | exit.Branch(ne); |
| 3035 | frame_->Drop(); |
| 3036 | StoreArgumentsObject(false); |
| 3037 | exit.Bind(); |
| 3038 | } |
| 3039 | |
| 3040 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3041 | void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) { |
| 3042 | ASSERT(slot != NULL); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3043 | VirtualFrame::RegisterAllocationScope scope(this); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3044 | if (slot->type() == Slot::LOOKUP) { |
| 3045 | ASSERT(slot->var()->is_dynamic()); |
| 3046 | |
| 3047 | // For now, just do a runtime call. |
| 3048 | frame_->EmitPush(cp); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3049 | frame_->EmitPush(Operand(slot->var()->name())); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3050 | |
| 3051 | if (init_state == CONST_INIT) { |
| 3052 | // Same as the case for a normal store, but ignores attribute |
| 3053 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 3054 | // const properties (introduced via eval("const foo = (some |
| 3055 | // expr);")). Also, uses the current function context instead of |
| 3056 | // the top context. |
| 3057 | // |
| 3058 | // Note that we must declare the foo upon entry of eval(), via a |
| 3059 | // context slot declaration, but we cannot initialize it at the |
| 3060 | // same time, because the const declaration may be at the end of |
| 3061 | // the eval code (sigh...) and the const variable may have been |
| 3062 | // used before (where its value is 'undefined'). Thus, we can only |
| 3063 | // do the initialization when we actually encounter the expression |
| 3064 | // and when the expression operands are defined and valid, and |
| 3065 | // thus we need the split into 2 operations: declaration of the |
| 3066 | // context slot followed by initialization. |
| 3067 | frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 3068 | } else { |
| 3069 | frame_->CallRuntime(Runtime::kStoreContextSlot, 3); |
| 3070 | } |
| 3071 | // Storing a variable must keep the (new) value on the expression |
| 3072 | // stack. This is necessary for compiling assignment expressions. |
| 3073 | frame_->EmitPush(r0); |
| 3074 | |
| 3075 | } else { |
| 3076 | ASSERT(!slot->var()->is_dynamic()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3077 | Register scratch = VirtualFrame::scratch0(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3078 | Register scratch2 = VirtualFrame::scratch1(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3079 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3080 | // The frame must be spilled when branching to this target. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3081 | JumpTarget exit; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3082 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3083 | if (init_state == CONST_INIT) { |
| 3084 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 3085 | // Only the first const initialization must be executed (the slot |
| 3086 | // still contains 'the hole' value). When the assignment is |
| 3087 | // executed, the code is identical to a normal store (see below). |
| 3088 | Comment cmnt(masm_, "[ Init const"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3089 | __ ldr(scratch, SlotOperand(slot, scratch)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3090 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3091 | __ cmp(scratch, ip); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3092 | exit.Branch(ne); |
| 3093 | } |
| 3094 | |
| 3095 | // We must execute the store. Storing a variable must keep the |
| 3096 | // (new) value on the stack. This is necessary for compiling |
| 3097 | // assignment expressions. |
| 3098 | // |
| 3099 | // Note: We will reach here even with slot->var()->mode() == |
| 3100 | // Variable::CONST because of const declarations which will |
| 3101 | // initialize consts to 'the hole' value and by doing so, end up |
| 3102 | // calling this code. r2 may be loaded with context; used below in |
| 3103 | // RecordWrite. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3104 | Register tos = frame_->Peek(); |
| 3105 | __ str(tos, SlotOperand(slot, scratch)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3106 | if (slot->type() == Slot::CONTEXT) { |
| 3107 | // Skip write barrier if the written value is a smi. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3108 | __ tst(tos, Operand(kSmiTagMask)); |
| 3109 | // We don't use tos any more after here. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3110 | exit.Branch(eq); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3111 | // scratch is loaded with context when calling SlotOperand above. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3112 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3113 | // We need an extra register. Until we have a way to do that in the |
| 3114 | // virtual frame we will cheat and ask for a free TOS register. |
| 3115 | Register scratch3 = frame_->GetTOSRegister(); |
| 3116 | __ RecordWrite(scratch, Operand(offset), scratch2, scratch3); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3117 | } |
| 3118 | // If we definitely did not jump over the assignment, we do not need |
| 3119 | // to bind the exit label. Doing so can defeat peephole |
| 3120 | // optimization. |
| 3121 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
| 3122 | exit.Bind(); |
| 3123 | } |
| 3124 | } |
| 3125 | } |
| 3126 | |
| 3127 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3128 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 3129 | TypeofState typeof_state, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3130 | JumpTarget* slow) { |
| 3131 | // Check that no extension objects have been created by calls to |
| 3132 | // eval from the current scope to the global scope. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3133 | Register tmp = frame_->scratch0(); |
| 3134 | Register tmp2 = frame_->scratch1(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3135 | Register context = cp; |
| 3136 | Scope* s = scope(); |
| 3137 | while (s != NULL) { |
| 3138 | if (s->num_heap_slots() > 0) { |
| 3139 | if (s->calls_eval()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3140 | frame_->SpillAll(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3141 | // Check that extension is NULL. |
| 3142 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 3143 | __ tst(tmp2, tmp2); |
| 3144 | slow->Branch(ne); |
| 3145 | } |
| 3146 | // Load next context in chain. |
| 3147 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 3148 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 3149 | context = tmp; |
| 3150 | } |
| 3151 | // If no outer scope calls eval, we do not need to check more |
| 3152 | // context extensions. |
| 3153 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 3154 | s = s->outer_scope(); |
| 3155 | } |
| 3156 | |
| 3157 | if (s->is_eval_scope()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3158 | frame_->SpillAll(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3159 | Label next, fast; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3160 | __ Move(tmp, context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3161 | __ bind(&next); |
| 3162 | // Terminate at global context. |
| 3163 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 3164 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 3165 | __ cmp(tmp2, ip); |
| 3166 | __ b(eq, &fast); |
| 3167 | // Check that extension is NULL. |
| 3168 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 3169 | __ tst(tmp2, tmp2); |
| 3170 | slow->Branch(ne); |
| 3171 | // Load next context in chain. |
| 3172 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 3173 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 3174 | __ b(&next); |
| 3175 | __ bind(&fast); |
| 3176 | } |
| 3177 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3178 | // Load the global object. |
| 3179 | LoadGlobal(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3180 | // Setup the name register and call load IC. |
| 3181 | frame_->CallLoadIC(slot->var()->name(), |
| 3182 | typeof_state == INSIDE_TYPEOF |
| 3183 | ? RelocInfo::CODE_TARGET |
| 3184 | : RelocInfo::CODE_TARGET_CONTEXT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 3188 | void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot, |
| 3189 | TypeofState typeof_state, |
| 3190 | JumpTarget* slow, |
| 3191 | JumpTarget* done) { |
| 3192 | // Generate fast-case code for variables that might be shadowed by |
| 3193 | // eval-introduced variables. Eval is used a lot without |
| 3194 | // introducing variables. In those cases, we do not want to |
| 3195 | // perform a runtime call for all variables in the scope |
| 3196 | // containing the eval. |
| 3197 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 3198 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow); |
| 3199 | frame_->SpillAll(); |
| 3200 | done->Jump(); |
| 3201 | |
| 3202 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 3203 | frame_->SpillAll(); |
| 3204 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 3205 | Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite(); |
| 3206 | if (potential_slot != NULL) { |
| 3207 | // Generate fast case for locals that rewrite to slots. |
| 3208 | __ ldr(r0, |
| 3209 | ContextSlotOperandCheckExtensions(potential_slot, |
| 3210 | r1, |
| 3211 | r2, |
| 3212 | slow)); |
| 3213 | if (potential_slot->var()->mode() == Variable::CONST) { |
| 3214 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 3215 | __ cmp(r0, ip); |
| 3216 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
| 3217 | } |
| 3218 | done->Jump(); |
| 3219 | } else if (rewrite != NULL) { |
| 3220 | // Generate fast case for argument loads. |
| 3221 | Property* property = rewrite->AsProperty(); |
| 3222 | if (property != NULL) { |
| 3223 | VariableProxy* obj_proxy = property->obj()->AsVariableProxy(); |
| 3224 | Literal* key_literal = property->key()->AsLiteral(); |
| 3225 | if (obj_proxy != NULL && |
| 3226 | key_literal != NULL && |
| 3227 | obj_proxy->IsArguments() && |
| 3228 | key_literal->handle()->IsSmi()) { |
| 3229 | // Load arguments object if there are no eval-introduced |
| 3230 | // variables. Then load the argument from the arguments |
| 3231 | // object using keyed load. |
| 3232 | __ ldr(r0, |
| 3233 | ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(), |
| 3234 | r1, |
| 3235 | r2, |
| 3236 | slow)); |
| 3237 | frame_->EmitPush(r0); |
| 3238 | __ mov(r1, Operand(key_literal->handle())); |
| 3239 | frame_->EmitPush(r1); |
| 3240 | EmitKeyedLoad(); |
| 3241 | done->Jump(); |
| 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3249 | void CodeGenerator::VisitSlot(Slot* node) { |
| 3250 | #ifdef DEBUG |
| 3251 | int original_height = frame_->height(); |
| 3252 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3253 | Comment cmnt(masm_, "[ Slot"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3254 | LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF); |
| 3255 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | |
| 3259 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
| 3260 | #ifdef DEBUG |
| 3261 | int original_height = frame_->height(); |
| 3262 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3263 | Comment cmnt(masm_, "[ VariableProxy"); |
| 3264 | |
| 3265 | Variable* var = node->var(); |
| 3266 | Expression* expr = var->rewrite(); |
| 3267 | if (expr != NULL) { |
| 3268 | Visit(expr); |
| 3269 | } else { |
| 3270 | ASSERT(var->is_global()); |
| 3271 | Reference ref(this, node); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3272 | ref.GetValue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3273 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3274 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | |
| 3278 | void CodeGenerator::VisitLiteral(Literal* node) { |
| 3279 | #ifdef DEBUG |
| 3280 | int original_height = frame_->height(); |
| 3281 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3282 | Comment cmnt(masm_, "[ Literal"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3283 | Register reg = frame_->GetTOSRegister(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3284 | bool is_smi = node->handle()->IsSmi(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3285 | __ mov(reg, Operand(node->handle())); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3286 | frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3287 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | |
| 3291 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
| 3292 | #ifdef DEBUG |
| 3293 | int original_height = frame_->height(); |
| 3294 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3295 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 3296 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3297 | Register tmp = VirtualFrame::scratch0(); |
| 3298 | // Free up a TOS register that can be used to push the literal. |
| 3299 | Register literal = frame_->GetTOSRegister(); |
| 3300 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3301 | // Retrieve the literal array and check the allocated entry. |
| 3302 | |
| 3303 | // Load the function of this activation. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3304 | __ ldr(tmp, frame_->Function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3305 | |
| 3306 | // Load the literals array of the function. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3307 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3308 | |
| 3309 | // Load the literal at the ast saved index. |
| 3310 | int literal_offset = |
| 3311 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3312 | __ ldr(literal, FieldMemOperand(tmp, literal_offset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3313 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3314 | JumpTarget materialized; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3315 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3316 | __ cmp(literal, ip); |
| 3317 | // This branch locks the virtual frame at the done label to match the |
| 3318 | // one we have here, where the literal register is not on the stack and |
| 3319 | // nothing is spilled. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3320 | materialized.Branch(ne); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3321 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3322 | // If the entry is undefined we call the runtime system to compute |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3323 | // the literal. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3324 | // literal array (0) |
| 3325 | frame_->EmitPush(tmp); |
| 3326 | // literal index (1) |
| 3327 | frame_->EmitPush(Operand(Smi::FromInt(node->literal_index()))); |
| 3328 | // RegExp pattern (2) |
| 3329 | frame_->EmitPush(Operand(node->pattern())); |
| 3330 | // RegExp flags (3) |
| 3331 | frame_->EmitPush(Operand(node->flags())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3332 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3333 | __ Move(literal, r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3334 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3335 | materialized.Bind(); |
| 3336 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3337 | frame_->EmitPush(literal); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3338 | int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize; |
| 3339 | frame_->EmitPush(Operand(Smi::FromInt(size))); |
| 3340 | frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1); |
| 3341 | // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime. |
| 3342 | // r0 is newly allocated space. |
| 3343 | |
| 3344 | // Reuse literal variable with (possibly) a new register, still holding |
| 3345 | // the materialized boilerplate. |
| 3346 | literal = frame_->PopToRegister(r0); |
| 3347 | |
| 3348 | __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize); |
| 3349 | |
| 3350 | // Push the clone. |
| 3351 | frame_->EmitPush(r0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3352 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3356 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
| 3357 | #ifdef DEBUG |
| 3358 | int original_height = frame_->height(); |
| 3359 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3360 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 3361 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3362 | Register literal = frame_->GetTOSRegister(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3363 | // Load the function of this activation. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3364 | __ ldr(literal, frame_->Function()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3365 | // Literal array. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3366 | __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset)); |
| 3367 | frame_->EmitPush(literal); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3368 | // Literal index. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3369 | frame_->EmitPush(Operand(Smi::FromInt(node->literal_index()))); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3370 | // Constant properties. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3371 | frame_->EmitPush(Operand(node->constant_properties())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3372 | // Should the object literal have fast elements? |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3373 | frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0))); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3374 | if (node->depth() > 1) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3375 | frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3376 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3377 | frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3378 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3379 | frame_->EmitPush(r0); // save the result |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3380 | for (int i = 0; i < node->properties()->length(); i++) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3381 | // At the start of each iteration, the top of stack contains |
| 3382 | // the newly created object literal. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3383 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 3384 | Literal* key = property->key(); |
| 3385 | Expression* value = property->value(); |
| 3386 | switch (property->kind()) { |
| 3387 | case ObjectLiteral::Property::CONSTANT: |
| 3388 | break; |
| 3389 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 3390 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 3391 | // else fall through |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3392 | case ObjectLiteral::Property::COMPUTED: |
| 3393 | if (key->handle()->IsSymbol()) { |
| 3394 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3395 | Load(value); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3396 | frame_->PopToR0(); |
| 3397 | // Fetch the object literal. |
| 3398 | frame_->SpillAllButCopyTOSToR1(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3399 | __ mov(r2, Operand(key->handle())); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3400 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
| 3401 | break; |
| 3402 | } |
| 3403 | // else fall through |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3404 | case ObjectLiteral::Property::PROTOTYPE: { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3405 | frame_->Dup(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3406 | Load(key); |
| 3407 | Load(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3408 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3409 | break; |
| 3410 | } |
| 3411 | case ObjectLiteral::Property::SETTER: { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3412 | frame_->Dup(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3413 | Load(key); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3414 | frame_->EmitPush(Operand(Smi::FromInt(1))); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3415 | Load(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3416 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3417 | break; |
| 3418 | } |
| 3419 | case ObjectLiteral::Property::GETTER: { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3420 | frame_->Dup(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3421 | Load(key); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3422 | frame_->EmitPush(Operand(Smi::FromInt(0))); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3423 | Load(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3424 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3425 | break; |
| 3426 | } |
| 3427 | } |
| 3428 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3429 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3433 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
| 3434 | #ifdef DEBUG |
| 3435 | int original_height = frame_->height(); |
| 3436 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3437 | Comment cmnt(masm_, "[ ArrayLiteral"); |
| 3438 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3439 | Register tos = frame_->GetTOSRegister(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3440 | // Load the function of this activation. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3441 | __ ldr(tos, frame_->Function()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3442 | // Load the literals array of the function. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3443 | __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset)); |
| 3444 | frame_->EmitPush(tos); |
| 3445 | frame_->EmitPush(Operand(Smi::FromInt(node->literal_index()))); |
| 3446 | frame_->EmitPush(Operand(node->constant_elements())); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3447 | int length = node->values()->length(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 3448 | if (node->constant_elements()->map() == Heap::fixed_cow_array_map()) { |
| 3449 | FastCloneShallowArrayStub stub( |
| 3450 | FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length); |
| 3451 | frame_->CallStub(&stub, 3); |
| 3452 | __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2); |
| 3453 | } else if (node->depth() > 1) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3454 | frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 3455 | } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3456 | frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3457 | } else { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 3458 | FastCloneShallowArrayStub stub( |
| 3459 | FastCloneShallowArrayStub::CLONE_ELEMENTS, length); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3460 | frame_->CallStub(&stub, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3461 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3462 | frame_->EmitPush(r0); // save the result |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3463 | // r0: created object literal |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3464 | |
| 3465 | // Generate code to set the elements in the array that are not |
| 3466 | // literals. |
| 3467 | for (int i = 0; i < node->values()->length(); i++) { |
| 3468 | Expression* value = node->values()->at(i); |
| 3469 | |
| 3470 | // If value is a literal the property value is already set in the |
| 3471 | // boilerplate object. |
| 3472 | if (value->AsLiteral() != NULL) continue; |
| 3473 | // If value is a materialized literal the property value is already set |
| 3474 | // in the boilerplate object if it is simple. |
| 3475 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
| 3476 | |
| 3477 | // The property must be set by generated code. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3478 | Load(value); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3479 | frame_->PopToR0(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3480 | // Fetch the object literal. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3481 | frame_->SpillAllButCopyTOSToR1(); |
| 3482 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3483 | // Get the elements array. |
| 3484 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 3485 | |
| 3486 | // Write to the indexed properties array. |
| 3487 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
| 3488 | __ str(r0, FieldMemOperand(r1, offset)); |
| 3489 | |
| 3490 | // Update the write barrier for the array address. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3491 | __ RecordWrite(r1, Operand(offset), r3, r2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3492 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3493 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | |
| 3497 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
| 3498 | #ifdef DEBUG |
| 3499 | int original_height = frame_->height(); |
| 3500 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3501 | // Call runtime routine to allocate the catch extension object and |
| 3502 | // assign the exception value to the catch variable. |
| 3503 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3504 | Load(node->key()); |
| 3505 | Load(node->value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3506 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 3507 | frame_->EmitPush(r0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3508 | ASSERT_EQ(original_height + 1, frame_->height()); |
| 3509 | } |
| 3510 | |
| 3511 | |
| 3512 | void CodeGenerator::EmitSlotAssignment(Assignment* node) { |
| 3513 | #ifdef DEBUG |
| 3514 | int original_height = frame_->height(); |
| 3515 | #endif |
| 3516 | Comment cmnt(masm(), "[ Variable Assignment"); |
| 3517 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 3518 | ASSERT(var != NULL); |
| 3519 | Slot* slot = var->slot(); |
| 3520 | ASSERT(slot != NULL); |
| 3521 | |
| 3522 | // Evaluate the right-hand side. |
| 3523 | if (node->is_compound()) { |
| 3524 | // For a compound assignment the right-hand side is a binary operation |
| 3525 | // between the current property value and the actual right-hand side. |
| 3526 | LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF); |
| 3527 | |
| 3528 | // Perform the binary operation. |
| 3529 | Literal* literal = node->value()->AsLiteral(); |
| 3530 | bool overwrite_value = |
| 3531 | (node->value()->AsBinaryOperation() != NULL && |
| 3532 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3533 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 3534 | SmiOperation(node->binary_op(), |
| 3535 | literal->handle(), |
| 3536 | false, |
| 3537 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 3538 | } else { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3539 | GenerateInlineSmi inline_smi = |
| 3540 | loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI; |
| 3541 | if (literal != NULL) { |
| 3542 | ASSERT(!literal->handle()->IsSmi()); |
| 3543 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 3544 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3545 | Load(node->value()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3546 | GenericBinaryOperation(node->binary_op(), |
| 3547 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE, |
| 3548 | inline_smi); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3549 | } |
| 3550 | } else { |
| 3551 | Load(node->value()); |
| 3552 | } |
| 3553 | |
| 3554 | // Perform the assignment. |
| 3555 | if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) { |
| 3556 | CodeForSourcePosition(node->position()); |
| 3557 | StoreToSlot(slot, |
| 3558 | node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT); |
| 3559 | } |
| 3560 | ASSERT_EQ(original_height + 1, frame_->height()); |
| 3561 | } |
| 3562 | |
| 3563 | |
| 3564 | void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) { |
| 3565 | #ifdef DEBUG |
| 3566 | int original_height = frame_->height(); |
| 3567 | #endif |
| 3568 | Comment cmnt(masm(), "[ Named Property Assignment"); |
| 3569 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 3570 | Property* prop = node->target()->AsProperty(); |
| 3571 | ASSERT(var == NULL || (prop == NULL && var->is_global())); |
| 3572 | |
| 3573 | // Initialize name and evaluate the receiver sub-expression if necessary. If |
| 3574 | // the receiver is trivial it is not placed on the stack at this point, but |
| 3575 | // loaded whenever actually needed. |
| 3576 | Handle<String> name; |
| 3577 | bool is_trivial_receiver = false; |
| 3578 | if (var != NULL) { |
| 3579 | name = var->name(); |
| 3580 | } else { |
| 3581 | Literal* lit = prop->key()->AsLiteral(); |
| 3582 | ASSERT_NOT_NULL(lit); |
| 3583 | name = Handle<String>::cast(lit->handle()); |
| 3584 | // Do not materialize the receiver on the frame if it is trivial. |
| 3585 | is_trivial_receiver = prop->obj()->IsTrivial(); |
| 3586 | if (!is_trivial_receiver) Load(prop->obj()); |
| 3587 | } |
| 3588 | |
| 3589 | // Change to slow case in the beginning of an initialization block to |
| 3590 | // avoid the quadratic behavior of repeatedly adding fast properties. |
| 3591 | if (node->starts_initialization_block()) { |
| 3592 | // Initialization block consists of assignments of the form expr.x = ..., so |
| 3593 | // this will never be an assignment to a variable, so there must be a |
| 3594 | // receiver object. |
| 3595 | ASSERT_EQ(NULL, var); |
| 3596 | if (is_trivial_receiver) { |
| 3597 | Load(prop->obj()); |
| 3598 | } else { |
| 3599 | frame_->Dup(); |
| 3600 | } |
| 3601 | frame_->CallRuntime(Runtime::kToSlowProperties, 1); |
| 3602 | } |
| 3603 | |
| 3604 | // Change to fast case at the end of an initialization block. To prepare for |
| 3605 | // that add an extra copy of the receiver to the frame, so that it can be |
| 3606 | // converted back to fast case after the assignment. |
| 3607 | if (node->ends_initialization_block() && !is_trivial_receiver) { |
| 3608 | frame_->Dup(); |
| 3609 | } |
| 3610 | |
| 3611 | // Stack layout: |
| 3612 | // [tos] : receiver (only materialized if non-trivial) |
| 3613 | // [tos+1] : receiver if at the end of an initialization block |
| 3614 | |
| 3615 | // Evaluate the right-hand side. |
| 3616 | if (node->is_compound()) { |
| 3617 | // For a compound assignment the right-hand side is a binary operation |
| 3618 | // between the current property value and the actual right-hand side. |
| 3619 | if (is_trivial_receiver) { |
| 3620 | Load(prop->obj()); |
| 3621 | } else if (var != NULL) { |
| 3622 | LoadGlobal(); |
| 3623 | } else { |
| 3624 | frame_->Dup(); |
| 3625 | } |
| 3626 | EmitNamedLoad(name, var != NULL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3627 | |
| 3628 | // Perform the binary operation. |
| 3629 | Literal* literal = node->value()->AsLiteral(); |
| 3630 | bool overwrite_value = |
| 3631 | (node->value()->AsBinaryOperation() != NULL && |
| 3632 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3633 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 3634 | SmiOperation(node->binary_op(), |
| 3635 | literal->handle(), |
| 3636 | false, |
| 3637 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 3638 | } else { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3639 | GenerateInlineSmi inline_smi = |
| 3640 | loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI; |
| 3641 | if (literal != NULL) { |
| 3642 | ASSERT(!literal->handle()->IsSmi()); |
| 3643 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 3644 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3645 | Load(node->value()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3646 | GenericBinaryOperation(node->binary_op(), |
| 3647 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE, |
| 3648 | inline_smi); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3649 | } |
| 3650 | } else { |
| 3651 | // For non-compound assignment just load the right-hand side. |
| 3652 | Load(node->value()); |
| 3653 | } |
| 3654 | |
| 3655 | // Stack layout: |
| 3656 | // [tos] : value |
| 3657 | // [tos+1] : receiver (only materialized if non-trivial) |
| 3658 | // [tos+2] : receiver if at the end of an initialization block |
| 3659 | |
| 3660 | // Perform the assignment. It is safe to ignore constants here. |
| 3661 | ASSERT(var == NULL || var->mode() != Variable::CONST); |
| 3662 | ASSERT_NE(Token::INIT_CONST, node->op()); |
| 3663 | if (is_trivial_receiver) { |
| 3664 | // Load the receiver and swap with the value. |
| 3665 | Load(prop->obj()); |
| 3666 | Register t0 = frame_->PopToRegister(); |
| 3667 | Register t1 = frame_->PopToRegister(t0); |
| 3668 | frame_->EmitPush(t0); |
| 3669 | frame_->EmitPush(t1); |
| 3670 | } |
| 3671 | CodeForSourcePosition(node->position()); |
| 3672 | bool is_contextual = (var != NULL); |
| 3673 | EmitNamedStore(name, is_contextual); |
| 3674 | frame_->EmitPush(r0); |
| 3675 | |
| 3676 | // Change to fast case at the end of an initialization block. |
| 3677 | if (node->ends_initialization_block()) { |
| 3678 | ASSERT_EQ(NULL, var); |
| 3679 | // The argument to the runtime call is the receiver. |
| 3680 | if (is_trivial_receiver) { |
| 3681 | Load(prop->obj()); |
| 3682 | } else { |
| 3683 | // A copy of the receiver is below the value of the assignment. Swap |
| 3684 | // the receiver and the value of the assignment expression. |
| 3685 | Register t0 = frame_->PopToRegister(); |
| 3686 | Register t1 = frame_->PopToRegister(t0); |
| 3687 | frame_->EmitPush(t0); |
| 3688 | frame_->EmitPush(t1); |
| 3689 | } |
| 3690 | frame_->CallRuntime(Runtime::kToFastProperties, 1); |
| 3691 | } |
| 3692 | |
| 3693 | // Stack layout: |
| 3694 | // [tos] : result |
| 3695 | |
| 3696 | ASSERT_EQ(original_height + 1, frame_->height()); |
| 3697 | } |
| 3698 | |
| 3699 | |
| 3700 | void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) { |
| 3701 | #ifdef DEBUG |
| 3702 | int original_height = frame_->height(); |
| 3703 | #endif |
| 3704 | Comment cmnt(masm_, "[ Keyed Property Assignment"); |
| 3705 | Property* prop = node->target()->AsProperty(); |
| 3706 | ASSERT_NOT_NULL(prop); |
| 3707 | |
| 3708 | // Evaluate the receiver subexpression. |
| 3709 | Load(prop->obj()); |
| 3710 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3711 | WriteBarrierCharacter wb_info; |
| 3712 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3713 | // Change to slow case in the beginning of an initialization block to |
| 3714 | // avoid the quadratic behavior of repeatedly adding fast properties. |
| 3715 | if (node->starts_initialization_block()) { |
| 3716 | frame_->Dup(); |
| 3717 | frame_->CallRuntime(Runtime::kToSlowProperties, 1); |
| 3718 | } |
| 3719 | |
| 3720 | // Change to fast case at the end of an initialization block. To prepare for |
| 3721 | // that add an extra copy of the receiver to the frame, so that it can be |
| 3722 | // converted back to fast case after the assignment. |
| 3723 | if (node->ends_initialization_block()) { |
| 3724 | frame_->Dup(); |
| 3725 | } |
| 3726 | |
| 3727 | // Evaluate the key subexpression. |
| 3728 | Load(prop->key()); |
| 3729 | |
| 3730 | // Stack layout: |
| 3731 | // [tos] : key |
| 3732 | // [tos+1] : receiver |
| 3733 | // [tos+2] : receiver if at the end of an initialization block |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3734 | // |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3735 | // Evaluate the right-hand side. |
| 3736 | if (node->is_compound()) { |
| 3737 | // For a compound assignment the right-hand side is a binary operation |
| 3738 | // between the current property value and the actual right-hand side. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 3739 | // Duplicate receiver and key for loading the current property value. |
| 3740 | frame_->Dup2(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3741 | EmitKeyedLoad(); |
| 3742 | frame_->EmitPush(r0); |
| 3743 | |
| 3744 | // Perform the binary operation. |
| 3745 | Literal* literal = node->value()->AsLiteral(); |
| 3746 | bool overwrite_value = |
| 3747 | (node->value()->AsBinaryOperation() != NULL && |
| 3748 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3749 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 3750 | SmiOperation(node->binary_op(), |
| 3751 | literal->handle(), |
| 3752 | false, |
| 3753 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 3754 | } else { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3755 | GenerateInlineSmi inline_smi = |
| 3756 | loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI; |
| 3757 | if (literal != NULL) { |
| 3758 | ASSERT(!literal->handle()->IsSmi()); |
| 3759 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 3760 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3761 | Load(node->value()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3762 | GenericBinaryOperation(node->binary_op(), |
| 3763 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE, |
| 3764 | inline_smi); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3765 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3766 | wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3767 | } else { |
| 3768 | // For non-compound assignment just load the right-hand side. |
| 3769 | Load(node->value()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3770 | wb_info = node->value()->AsLiteral() != NULL ? |
| 3771 | NEVER_NEWSPACE : |
| 3772 | (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | // Stack layout: |
| 3776 | // [tos] : value |
| 3777 | // [tos+1] : key |
| 3778 | // [tos+2] : receiver |
| 3779 | // [tos+3] : receiver if at the end of an initialization block |
| 3780 | |
| 3781 | // Perform the assignment. It is safe to ignore constants here. |
| 3782 | ASSERT(node->op() != Token::INIT_CONST); |
| 3783 | CodeForSourcePosition(node->position()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3784 | EmitKeyedStore(prop->key()->type(), wb_info); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3785 | frame_->EmitPush(r0); |
| 3786 | |
| 3787 | // Stack layout: |
| 3788 | // [tos] : result |
| 3789 | // [tos+1] : receiver if at the end of an initialization block |
| 3790 | |
| 3791 | // Change to fast case at the end of an initialization block. |
| 3792 | if (node->ends_initialization_block()) { |
| 3793 | // The argument to the runtime call is the extra copy of the receiver, |
| 3794 | // which is below the value of the assignment. Swap the receiver and |
| 3795 | // the value of the assignment expression. |
| 3796 | Register t0 = frame_->PopToRegister(); |
| 3797 | Register t1 = frame_->PopToRegister(t0); |
| 3798 | frame_->EmitPush(t1); |
| 3799 | frame_->EmitPush(t0); |
| 3800 | frame_->CallRuntime(Runtime::kToFastProperties, 1); |
| 3801 | } |
| 3802 | |
| 3803 | // Stack layout: |
| 3804 | // [tos] : result |
| 3805 | |
| 3806 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | |
| 3810 | void CodeGenerator::VisitAssignment(Assignment* node) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3811 | VirtualFrame::RegisterAllocationScope scope(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3812 | #ifdef DEBUG |
| 3813 | int original_height = frame_->height(); |
| 3814 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3815 | Comment cmnt(masm_, "[ Assignment"); |
| 3816 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3817 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 3818 | Property* prop = node->target()->AsProperty(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3819 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3820 | if (var != NULL && !var->is_global()) { |
| 3821 | EmitSlotAssignment(node); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3822 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3823 | } else if ((prop != NULL && prop->key()->IsPropertyName()) || |
| 3824 | (var != NULL && var->is_global())) { |
| 3825 | // Properties whose keys are property names and global variables are |
| 3826 | // treated as named property references. We do not need to consider |
| 3827 | // global 'this' because it is not a valid left-hand side. |
| 3828 | EmitNamedPropertyAssignment(node); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3829 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3830 | } else if (prop != NULL) { |
| 3831 | // Other properties (including rewritten parameters for a function that |
| 3832 | // uses arguments) are keyed property assignments. |
| 3833 | EmitKeyedPropertyAssignment(node); |
| 3834 | |
| 3835 | } else { |
| 3836 | // Invalid left-hand side. |
| 3837 | Load(node->target()); |
| 3838 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
| 3839 | // The runtime call doesn't actually return but the code generator will |
| 3840 | // still generate code and expects a certain frame height. |
| 3841 | frame_->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3842 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3843 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | |
| 3847 | void CodeGenerator::VisitThrow(Throw* node) { |
| 3848 | #ifdef DEBUG |
| 3849 | int original_height = frame_->height(); |
| 3850 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3851 | Comment cmnt(masm_, "[ Throw"); |
| 3852 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3853 | Load(node->exception()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3854 | CodeForSourcePosition(node->position()); |
| 3855 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 3856 | frame_->EmitPush(r0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3857 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | |
| 3861 | void CodeGenerator::VisitProperty(Property* node) { |
| 3862 | #ifdef DEBUG |
| 3863 | int original_height = frame_->height(); |
| 3864 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3865 | Comment cmnt(masm_, "[ Property"); |
| 3866 | |
| 3867 | { Reference property(this, node); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3868 | property.GetValue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3869 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3870 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | |
| 3874 | void CodeGenerator::VisitCall(Call* node) { |
| 3875 | #ifdef DEBUG |
| 3876 | int original_height = frame_->height(); |
| 3877 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3878 | Comment cmnt(masm_, "[ Call"); |
| 3879 | |
| 3880 | Expression* function = node->expression(); |
| 3881 | ZoneList<Expression*>* args = node->arguments(); |
| 3882 | |
| 3883 | // Standard function call. |
| 3884 | // Check if the function is a variable or a property. |
| 3885 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 3886 | Property* property = function->AsProperty(); |
| 3887 | |
| 3888 | // ------------------------------------------------------------------------ |
| 3889 | // Fast-case: Use inline caching. |
| 3890 | // --- |
| 3891 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 3892 | // must be resolved after the arguments have been evaluated. The IC code |
| 3893 | // automatically handles this by loading the arguments before the function |
| 3894 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 3895 | // ------------------------------------------------------------------------ |
| 3896 | |
| 3897 | if (var != NULL && var->is_possibly_eval()) { |
| 3898 | // ---------------------------------- |
| 3899 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 3900 | // ---------------------------------- |
| 3901 | |
| 3902 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 3903 | // resolve the function we need to call and the receiver of the |
| 3904 | // call. Then we call the resolved function using the given |
| 3905 | // arguments. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3906 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3907 | // Prepare stack for call to resolved function. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3908 | Load(function); |
| 3909 | |
| 3910 | // Allocate a frame slot for the receiver. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3911 | frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3912 | |
| 3913 | // Load the arguments. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3914 | int arg_count = args->length(); |
| 3915 | for (int i = 0; i < arg_count; i++) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3916 | Load(args->at(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3919 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 3920 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3921 | // If we know that eval can only be shadowed by eval-introduced |
| 3922 | // variables we attempt to load the global eval function directly |
| 3923 | // in generated code. If we succeed, there is no need to perform a |
| 3924 | // context lookup in the runtime system. |
| 3925 | JumpTarget done; |
| 3926 | if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) { |
| 3927 | ASSERT(var->slot()->type() == Slot::LOOKUP); |
| 3928 | JumpTarget slow; |
| 3929 | // Prepare the stack for the call to |
| 3930 | // ResolvePossiblyDirectEvalNoLookup by pushing the loaded |
| 3931 | // function, the first argument to the eval call and the |
| 3932 | // receiver. |
| 3933 | LoadFromGlobalSlotCheckExtensions(var->slot(), |
| 3934 | NOT_INSIDE_TYPEOF, |
| 3935 | &slow); |
| 3936 | frame_->EmitPush(r0); |
| 3937 | if (arg_count > 0) { |
| 3938 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3939 | frame_->EmitPush(r1); |
| 3940 | } else { |
| 3941 | frame_->EmitPush(r2); |
| 3942 | } |
| 3943 | __ ldr(r1, frame_->Receiver()); |
| 3944 | frame_->EmitPush(r1); |
| 3945 | |
| 3946 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3); |
| 3947 | |
| 3948 | done.Jump(); |
| 3949 | slow.Bind(); |
| 3950 | } |
| 3951 | |
| 3952 | // Prepare the stack for the call to ResolvePossiblyDirectEval by |
| 3953 | // pushing the loaded function, the first argument to the eval |
| 3954 | // call and the receiver. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3955 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3956 | frame_->EmitPush(r1); |
| 3957 | if (arg_count > 0) { |
| 3958 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3959 | frame_->EmitPush(r1); |
| 3960 | } else { |
| 3961 | frame_->EmitPush(r2); |
| 3962 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3963 | __ ldr(r1, frame_->Receiver()); |
| 3964 | frame_->EmitPush(r1); |
| 3965 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3966 | // Resolve the call. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3967 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3968 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3969 | // If we generated fast-case code bind the jump-target where fast |
| 3970 | // and slow case merge. |
| 3971 | if (done.is_linked()) done.Bind(); |
| 3972 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3973 | // Touch up stack with the right values for the function and the receiver. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3974 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3975 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3976 | |
| 3977 | // Call the function. |
| 3978 | CodeForSourcePosition(node->position()); |
| 3979 | |
| 3980 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3981 | CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3982 | frame_->CallStub(&call_function, arg_count + 1); |
| 3983 | |
| 3984 | __ ldr(cp, frame_->Context()); |
| 3985 | // Remove the function from the stack. |
| 3986 | frame_->Drop(); |
| 3987 | frame_->EmitPush(r0); |
| 3988 | |
| 3989 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
| 3990 | // ---------------------------------- |
| 3991 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3992 | // ---------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3993 | // Pass the global object as the receiver and let the IC stub |
| 3994 | // patch the stack to use the global proxy as 'this' in the |
| 3995 | // invoked function. |
| 3996 | LoadGlobal(); |
| 3997 | |
| 3998 | // Load the arguments. |
| 3999 | int arg_count = args->length(); |
| 4000 | for (int i = 0; i < arg_count; i++) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4001 | Load(args->at(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4004 | VirtualFrame::SpilledScope spilled_scope(frame_); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4005 | // Setup the name register and call the IC initialization code. |
| 4006 | __ mov(r2, Operand(var->name())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4007 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 4008 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
| 4009 | CodeForSourcePosition(node->position()); |
| 4010 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 4011 | arg_count + 1); |
| 4012 | __ ldr(cp, frame_->Context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4013 | frame_->EmitPush(r0); |
| 4014 | |
| 4015 | } else if (var != NULL && var->slot() != NULL && |
| 4016 | var->slot()->type() == Slot::LOOKUP) { |
| 4017 | // ---------------------------------- |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 4018 | // JavaScript examples: |
| 4019 | // |
| 4020 | // with (obj) foo(1, 2, 3) // foo may be in obj. |
| 4021 | // |
| 4022 | // function f() {}; |
| 4023 | // function g() { |
| 4024 | // eval(...); |
| 4025 | // f(); // f could be in extension object. |
| 4026 | // } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4027 | // ---------------------------------- |
| 4028 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 4029 | JumpTarget slow, done; |
| 4030 | |
| 4031 | // Generate fast case for loading functions from slots that |
| 4032 | // correspond to local/global variables or arguments unless they |
| 4033 | // are shadowed by eval-introduced bindings. |
| 4034 | EmitDynamicLoadFromSlotFastCase(var->slot(), |
| 4035 | NOT_INSIDE_TYPEOF, |
| 4036 | &slow, |
| 4037 | &done); |
| 4038 | |
| 4039 | slow.Bind(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4040 | // Load the function |
| 4041 | frame_->EmitPush(cp); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4042 | frame_->EmitPush(Operand(var->name())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4043 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
| 4044 | // r0: slot value; r1: receiver |
| 4045 | |
| 4046 | // Load the receiver. |
| 4047 | frame_->EmitPush(r0); // function |
| 4048 | frame_->EmitPush(r1); // receiver |
| 4049 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 4050 | // If fast case code has been generated, emit code to push the |
| 4051 | // function and receiver and have the slow path jump around this |
| 4052 | // code. |
| 4053 | if (done.is_linked()) { |
| 4054 | JumpTarget call; |
| 4055 | call.Jump(); |
| 4056 | done.Bind(); |
| 4057 | frame_->EmitPush(r0); // function |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4058 | LoadGlobalReceiver(VirtualFrame::scratch0()); // receiver |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 4059 | call.Bind(); |
| 4060 | } |
| 4061 | |
| 4062 | // Call the function. At this point, everything is spilled but the |
| 4063 | // function and receiver are in r0 and r1. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4064 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4065 | frame_->EmitPush(r0); |
| 4066 | |
| 4067 | } else if (property != NULL) { |
| 4068 | // Check if the key is a literal string. |
| 4069 | Literal* literal = property->key()->AsLiteral(); |
| 4070 | |
| 4071 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 4072 | // ------------------------------------------------------------------ |
| 4073 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 4074 | // ------------------------------------------------------------------ |
| 4075 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4076 | Handle<String> name = Handle<String>::cast(literal->handle()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4077 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4078 | if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION && |
| 4079 | name->IsEqualTo(CStrVector("apply")) && |
| 4080 | args->length() == 2 && |
| 4081 | args->at(1)->AsVariableProxy() != NULL && |
| 4082 | args->at(1)->AsVariableProxy()->IsArguments()) { |
| 4083 | // Use the optimized Function.prototype.apply that avoids |
| 4084 | // allocating lazily allocated arguments objects. |
| 4085 | CallApplyLazy(property->obj(), |
| 4086 | args->at(0), |
| 4087 | args->at(1)->AsVariableProxy(), |
| 4088 | node->position()); |
| 4089 | |
| 4090 | } else { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4091 | Load(property->obj()); // Receiver. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4092 | // Load the arguments. |
| 4093 | int arg_count = args->length(); |
| 4094 | for (int i = 0; i < arg_count; i++) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4095 | Load(args->at(i)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4096 | } |
| 4097 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4098 | VirtualFrame::SpilledScope spilled_scope(frame_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4099 | // Set the name register and call the IC initialization code. |
| 4100 | __ mov(r2, Operand(name)); |
| 4101 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 4102 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
| 4103 | CodeForSourcePosition(node->position()); |
| 4104 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
| 4105 | __ ldr(cp, frame_->Context()); |
| 4106 | frame_->EmitPush(r0); |
| 4107 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4108 | |
| 4109 | } else { |
| 4110 | // ------------------------------------------- |
| 4111 | // JavaScript example: 'array[index](1, 2, 3)' |
| 4112 | // ------------------------------------------- |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4113 | Load(property->obj()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4114 | if (property->is_synthetic()) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4115 | Load(property->key()); |
| 4116 | EmitKeyedLoad(); |
| 4117 | // Put the function below the receiver. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4118 | // Use the global receiver. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 4119 | frame_->EmitPush(r0); // Function. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4120 | LoadGlobalReceiver(VirtualFrame::scratch0()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4121 | // Call the function. |
| 4122 | CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position()); |
| 4123 | frame_->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4124 | } else { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4125 | // Load the arguments. |
| 4126 | int arg_count = args->length(); |
| 4127 | for (int i = 0; i < arg_count; i++) { |
| 4128 | Load(args->at(i)); |
| 4129 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4130 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4131 | // Set the name register and call the IC initialization code. |
| 4132 | Load(property->key()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4133 | frame_->SpillAll(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4134 | frame_->EmitPop(r2); // Function name. |
| 4135 | |
| 4136 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 4137 | Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop); |
| 4138 | CodeForSourcePosition(node->position()); |
| 4139 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
| 4140 | __ ldr(cp, frame_->Context()); |
| 4141 | frame_->EmitPush(r0); |
| 4142 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | } else { |
| 4146 | // ---------------------------------- |
| 4147 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 4148 | // ---------------------------------- |
| 4149 | |
| 4150 | // Load the function. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4151 | Load(function); |
| 4152 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4153 | // Pass the global proxy as the receiver. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4154 | LoadGlobalReceiver(VirtualFrame::scratch0()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4155 | |
| 4156 | // Call the function. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4157 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4158 | frame_->EmitPush(r0); |
| 4159 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4160 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | |
| 4164 | void CodeGenerator::VisitCallNew(CallNew* node) { |
| 4165 | #ifdef DEBUG |
| 4166 | int original_height = frame_->height(); |
| 4167 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4168 | Comment cmnt(masm_, "[ CallNew"); |
| 4169 | |
| 4170 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 4171 | // expression in new calls must be evaluated before the |
| 4172 | // arguments. This is different from ordinary calls, where the |
| 4173 | // actual function to call is resolved after the arguments have been |
| 4174 | // evaluated. |
| 4175 | |
| 4176 | // Compute function to call and use the global object as the |
| 4177 | // receiver. There is no need to use the global proxy here because |
| 4178 | // it will always be replaced with a newly allocated object. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4179 | Load(node->expression()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4180 | LoadGlobal(); |
| 4181 | |
| 4182 | // Push the arguments ("left-to-right") on the stack. |
| 4183 | ZoneList<Expression*>* args = node->arguments(); |
| 4184 | int arg_count = args->length(); |
| 4185 | for (int i = 0; i < arg_count; i++) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4186 | Load(args->at(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4189 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 4190 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4191 | // r0: the number of arguments. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4192 | __ mov(r0, Operand(arg_count)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4193 | // Load the function into r1 as per calling convention. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4194 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
| 4195 | |
| 4196 | // Call the construct call builtin that handles allocation and |
| 4197 | // constructor invocation. |
| 4198 | CodeForSourcePosition(node->position()); |
| 4199 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4200 | frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4201 | |
| 4202 | // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)). |
| 4203 | __ str(r0, frame_->Top()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4204 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | |
| 4208 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4209 | Register scratch = VirtualFrame::scratch0(); |
| 4210 | JumpTarget null, function, leave, non_function_constructor; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4211 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4212 | // Load the object into register. |
| 4213 | ASSERT(args->length() == 1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4214 | Load(args->at(0)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4215 | Register tos = frame_->PopToRegister(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4216 | |
| 4217 | // If the object is a smi, we return null. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4218 | __ tst(tos, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4219 | null.Branch(eq); |
| 4220 | |
| 4221 | // Check that the object is a JS object but take special care of JS |
| 4222 | // functions to make sure they have 'Function' as their class. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4223 | __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4224 | null.Branch(lt); |
| 4225 | |
| 4226 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 4227 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 4228 | // LAST_JS_OBJECT_TYPE. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 4229 | STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 4230 | STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4231 | __ cmp(scratch, Operand(JS_FUNCTION_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4232 | function.Branch(eq); |
| 4233 | |
| 4234 | // Check if the constructor in the map is a function. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4235 | __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset)); |
| 4236 | __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4237 | non_function_constructor.Branch(ne); |
| 4238 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4239 | // The tos register now contains the constructor function. Grab the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4240 | // instance class name from there. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4241 | __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset)); |
| 4242 | __ ldr(tos, |
| 4243 | FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset)); |
| 4244 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4245 | leave.Jump(); |
| 4246 | |
| 4247 | // Functions have class 'Function'. |
| 4248 | function.Bind(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4249 | __ mov(tos, Operand(Factory::function_class_symbol())); |
| 4250 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4251 | leave.Jump(); |
| 4252 | |
| 4253 | // Objects with a non-function constructor have class 'Object'. |
| 4254 | non_function_constructor.Bind(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4255 | __ mov(tos, Operand(Factory::Object_symbol())); |
| 4256 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4257 | leave.Jump(); |
| 4258 | |
| 4259 | // Non-JS objects have class null. |
| 4260 | null.Bind(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4261 | __ LoadRoot(tos, Heap::kNullValueRootIndex); |
| 4262 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4263 | |
| 4264 | // All done. |
| 4265 | leave.Bind(); |
| 4266 | } |
| 4267 | |
| 4268 | |
| 4269 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4270 | Register scratch = VirtualFrame::scratch0(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4271 | JumpTarget leave; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4272 | |
| 4273 | ASSERT(args->length() == 1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4274 | Load(args->at(0)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4275 | Register tos = frame_->PopToRegister(); // tos contains object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4276 | // if (object->IsSmi()) return the object. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4277 | __ tst(tos, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4278 | leave.Branch(eq); |
| 4279 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4280 | __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4281 | leave.Branch(ne); |
| 4282 | // Load the value. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4283 | __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4284 | leave.Bind(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4285 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | |
| 4289 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4290 | Register scratch1 = VirtualFrame::scratch0(); |
| 4291 | Register scratch2 = VirtualFrame::scratch1(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4292 | JumpTarget leave; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4293 | |
| 4294 | ASSERT(args->length() == 2); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4295 | Load(args->at(0)); // Load the object. |
| 4296 | Load(args->at(1)); // Load the value. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4297 | Register value = frame_->PopToRegister(); |
| 4298 | Register object = frame_->PopToRegister(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4299 | // if (object->IsSmi()) return object. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4300 | __ tst(object, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4301 | leave.Branch(eq); |
| 4302 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4303 | __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4304 | leave.Branch(ne); |
| 4305 | // Store the value. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4306 | __ str(value, FieldMemOperand(object, JSValue::kValueOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4307 | // Update the write barrier. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4308 | __ RecordWrite(object, |
| 4309 | Operand(JSValue::kValueOffset - kHeapObjectTag), |
| 4310 | scratch1, |
| 4311 | scratch2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4312 | // Leave. |
| 4313 | leave.Bind(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4314 | frame_->EmitPush(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | |
| 4318 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4319 | ASSERT(args->length() == 1); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4320 | Load(args->at(0)); |
| 4321 | Register reg = frame_->PopToRegister(); |
| 4322 | __ tst(reg, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4323 | cc_reg_ = eq; |
| 4324 | } |
| 4325 | |
| 4326 | |
| 4327 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4328 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 4329 | ASSERT_EQ(args->length(), 3); |
| 4330 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 4331 | if (ShouldGenerateLog(args->at(0))) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4332 | Load(args->at(1)); |
| 4333 | Load(args->at(2)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4334 | frame_->CallRuntime(Runtime::kLog, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4335 | } |
| 4336 | #endif |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4337 | frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | |
| 4341 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4342 | ASSERT(args->length() == 1); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4343 | Load(args->at(0)); |
| 4344 | Register reg = frame_->PopToRegister(); |
| 4345 | __ tst(reg, Operand(kSmiTagMask | 0x80000000u)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4346 | cc_reg_ = eq; |
| 4347 | } |
| 4348 | |
| 4349 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 4350 | // Generates the Math.pow method. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4351 | void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) { |
| 4352 | ASSERT(args->length() == 2); |
| 4353 | Load(args->at(0)); |
| 4354 | Load(args->at(1)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 4355 | |
| 4356 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 4357 | frame_->CallRuntime(Runtime::kMath_pow, 2); |
| 4358 | frame_->EmitPush(r0); |
| 4359 | } else { |
| 4360 | CpuFeatures::Scope scope(VFP3); |
| 4361 | JumpTarget runtime, done; |
| 4362 | Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return; |
| 4363 | |
| 4364 | Register scratch1 = VirtualFrame::scratch0(); |
| 4365 | Register scratch2 = VirtualFrame::scratch1(); |
| 4366 | |
| 4367 | // Get base and exponent to registers. |
| 4368 | Register exponent = frame_->PopToRegister(); |
| 4369 | Register base = frame_->PopToRegister(exponent); |
| 4370 | Register heap_number_map = no_reg; |
| 4371 | |
| 4372 | // Set the frame for the runtime jump target. The code below jumps to the |
| 4373 | // jump target label so the frame needs to be established before that. |
| 4374 | ASSERT(runtime.entry_frame() == NULL); |
| 4375 | runtime.set_entry_frame(frame_); |
| 4376 | |
| 4377 | __ BranchOnNotSmi(exponent, &exponent_nonsmi); |
| 4378 | __ BranchOnNotSmi(base, &base_nonsmi); |
| 4379 | |
| 4380 | heap_number_map = r6; |
| 4381 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 4382 | |
| 4383 | // Exponent is a smi and base is a smi. Get the smi value into vfp register |
| 4384 | // d1. |
| 4385 | __ SmiToDoubleVFPRegister(base, d1, scratch1, s0); |
| 4386 | __ b(&powi); |
| 4387 | |
| 4388 | __ bind(&base_nonsmi); |
| 4389 | // Exponent is smi and base is non smi. Get the double value from the base |
| 4390 | // into vfp register d1. |
| 4391 | __ ObjectToDoubleVFPRegister(base, d1, |
| 4392 | scratch1, scratch2, heap_number_map, s0, |
| 4393 | runtime.entry_label()); |
| 4394 | |
| 4395 | __ bind(&powi); |
| 4396 | |
| 4397 | // Load 1.0 into d0. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 4398 | __ vmov(d0, 1.0); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 4399 | |
| 4400 | // Get the absolute untagged value of the exponent and use that for the |
| 4401 | // calculation. |
| 4402 | __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC); |
| 4403 | __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative. |
| 4404 | __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative. |
| 4405 | |
| 4406 | // Run through all the bits in the exponent. The result is calculated in d0 |
| 4407 | // and d1 holds base^(bit^2). |
| 4408 | Label more_bits; |
| 4409 | __ bind(&more_bits); |
| 4410 | __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC); |
| 4411 | __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set. |
| 4412 | __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done. |
| 4413 | __ b(ne, &more_bits); |
| 4414 | |
| 4415 | // If exponent is positive we are done. |
| 4416 | __ cmp(exponent, Operand(0)); |
| 4417 | __ b(ge, &allocate_return); |
| 4418 | |
| 4419 | // If exponent is negative result is 1/result (d2 already holds 1.0 in that |
| 4420 | // case). However if d0 has reached infinity this will not provide the |
| 4421 | // correct result, so call runtime if that is the case. |
| 4422 | __ mov(scratch2, Operand(0x7FF00000)); |
| 4423 | __ mov(scratch1, Operand(0)); |
| 4424 | __ vmov(d1, scratch1, scratch2); // Load infinity into d1. |
| 4425 | __ vcmp(d0, d1); |
| 4426 | __ vmrs(pc); |
| 4427 | runtime.Branch(eq); // d0 reached infinity. |
| 4428 | __ vdiv(d0, d2, d0); |
| 4429 | __ b(&allocate_return); |
| 4430 | |
| 4431 | __ bind(&exponent_nonsmi); |
| 4432 | // Special handling of raising to the power of -0.5 and 0.5. First check |
| 4433 | // that the value is a heap number and that the lower bits (which for both |
| 4434 | // values are zero). |
| 4435 | heap_number_map = r6; |
| 4436 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 4437 | __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset)); |
| 4438 | __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset)); |
| 4439 | __ cmp(scratch1, heap_number_map); |
| 4440 | runtime.Branch(ne); |
| 4441 | __ tst(scratch2, scratch2); |
| 4442 | runtime.Branch(ne); |
| 4443 | |
| 4444 | // Load the higher bits (which contains the floating point exponent). |
| 4445 | __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset)); |
| 4446 | |
| 4447 | // Compare exponent with -0.5. |
| 4448 | __ cmp(scratch1, Operand(0xbfe00000)); |
| 4449 | __ b(ne, ¬_minus_half); |
| 4450 | |
| 4451 | // Get the double value from the base into vfp register d0. |
| 4452 | __ ObjectToDoubleVFPRegister(base, d0, |
| 4453 | scratch1, scratch2, heap_number_map, s0, |
| 4454 | runtime.entry_label(), |
| 4455 | AVOID_NANS_AND_INFINITIES); |
| 4456 | |
| 4457 | // Load 1.0 into d2. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 4458 | __ vmov(d2, 1.0); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 4459 | |
| 4460 | // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x). |
| 4461 | __ vdiv(d0, d2, d0); |
| 4462 | __ vsqrt(d0, d0); |
| 4463 | |
| 4464 | __ b(&allocate_return); |
| 4465 | |
| 4466 | __ bind(¬_minus_half); |
| 4467 | // Compare exponent with 0.5. |
| 4468 | __ cmp(scratch1, Operand(0x3fe00000)); |
| 4469 | runtime.Branch(ne); |
| 4470 | |
| 4471 | // Get the double value from the base into vfp register d0. |
| 4472 | __ ObjectToDoubleVFPRegister(base, d0, |
| 4473 | scratch1, scratch2, heap_number_map, s0, |
| 4474 | runtime.entry_label(), |
| 4475 | AVOID_NANS_AND_INFINITIES); |
| 4476 | __ vsqrt(d0, d0); |
| 4477 | |
| 4478 | __ bind(&allocate_return); |
| 4479 | Register scratch3 = r5; |
| 4480 | __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2, |
| 4481 | heap_number_map, runtime.entry_label()); |
| 4482 | __ mov(base, scratch3); |
| 4483 | done.Jump(); |
| 4484 | |
| 4485 | runtime.Bind(); |
| 4486 | |
| 4487 | // Push back the arguments again for the runtime call. |
| 4488 | frame_->EmitPush(base); |
| 4489 | frame_->EmitPush(exponent); |
| 4490 | frame_->CallRuntime(Runtime::kMath_pow, 2); |
| 4491 | __ Move(base, r0); |
| 4492 | |
| 4493 | done.Bind(); |
| 4494 | frame_->EmitPush(base); |
| 4495 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 4499 | // Generates the Math.sqrt method. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4500 | void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) { |
| 4501 | ASSERT(args->length() == 1); |
| 4502 | Load(args->at(0)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 4503 | |
| 4504 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 4505 | frame_->CallRuntime(Runtime::kMath_sqrt, 1); |
| 4506 | frame_->EmitPush(r0); |
| 4507 | } else { |
| 4508 | CpuFeatures::Scope scope(VFP3); |
| 4509 | JumpTarget runtime, done; |
| 4510 | |
| 4511 | Register scratch1 = VirtualFrame::scratch0(); |
| 4512 | Register scratch2 = VirtualFrame::scratch1(); |
| 4513 | |
| 4514 | // Get the value from the frame. |
| 4515 | Register tos = frame_->PopToRegister(); |
| 4516 | |
| 4517 | // Set the frame for the runtime jump target. The code below jumps to the |
| 4518 | // jump target label so the frame needs to be established before that. |
| 4519 | ASSERT(runtime.entry_frame() == NULL); |
| 4520 | runtime.set_entry_frame(frame_); |
| 4521 | |
| 4522 | Register heap_number_map = r6; |
| 4523 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 4524 | |
| 4525 | // Get the double value from the heap number into vfp register d0. |
| 4526 | __ ObjectToDoubleVFPRegister(tos, d0, |
| 4527 | scratch1, scratch2, heap_number_map, s0, |
| 4528 | runtime.entry_label()); |
| 4529 | |
| 4530 | // Calculate the square root of d0 and place result in a heap number object. |
| 4531 | __ vsqrt(d0, d0); |
| 4532 | __ AllocateHeapNumberWithValue( |
| 4533 | tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label()); |
| 4534 | done.Jump(); |
| 4535 | |
| 4536 | runtime.Bind(); |
| 4537 | // Push back the argument again for the runtime call. |
| 4538 | frame_->EmitPush(tos); |
| 4539 | frame_->CallRuntime(Runtime::kMath_sqrt, 1); |
| 4540 | __ Move(tos, r0); |
| 4541 | |
| 4542 | done.Bind(); |
| 4543 | frame_->EmitPush(tos); |
| 4544 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4548 | class DeferredStringCharCodeAt : public DeferredCode { |
| 4549 | public: |
| 4550 | DeferredStringCharCodeAt(Register object, |
| 4551 | Register index, |
| 4552 | Register scratch, |
| 4553 | Register result) |
| 4554 | : result_(result), |
| 4555 | char_code_at_generator_(object, |
| 4556 | index, |
| 4557 | scratch, |
| 4558 | result, |
| 4559 | &need_conversion_, |
| 4560 | &need_conversion_, |
| 4561 | &index_out_of_range_, |
| 4562 | STRING_INDEX_IS_NUMBER) {} |
| 4563 | |
| 4564 | StringCharCodeAtGenerator* fast_case_generator() { |
| 4565 | return &char_code_at_generator_; |
| 4566 | } |
| 4567 | |
| 4568 | virtual void Generate() { |
| 4569 | VirtualFrameRuntimeCallHelper call_helper(frame_state()); |
| 4570 | char_code_at_generator_.GenerateSlow(masm(), call_helper); |
| 4571 | |
| 4572 | __ bind(&need_conversion_); |
| 4573 | // Move the undefined value into the result register, which will |
| 4574 | // trigger conversion. |
| 4575 | __ LoadRoot(result_, Heap::kUndefinedValueRootIndex); |
| 4576 | __ jmp(exit_label()); |
| 4577 | |
| 4578 | __ bind(&index_out_of_range_); |
| 4579 | // When the index is out of range, the spec requires us to return |
| 4580 | // NaN. |
| 4581 | __ LoadRoot(result_, Heap::kNanValueRootIndex); |
| 4582 | __ jmp(exit_label()); |
| 4583 | } |
| 4584 | |
| 4585 | private: |
| 4586 | Register result_; |
| 4587 | |
| 4588 | Label need_conversion_; |
| 4589 | Label index_out_of_range_; |
| 4590 | |
| 4591 | StringCharCodeAtGenerator char_code_at_generator_; |
| 4592 | }; |
| 4593 | |
| 4594 | |
| 4595 | // This generates code that performs a String.prototype.charCodeAt() call |
| 4596 | // or returns a smi in order to trigger conversion. |
| 4597 | void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4598 | Comment(masm_, "[ GenerateStringCharCodeAt"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4599 | ASSERT(args->length() == 2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4600 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4601 | Load(args->at(0)); |
| 4602 | Load(args->at(1)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4603 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4604 | Register index = frame_->PopToRegister(); |
| 4605 | Register object = frame_->PopToRegister(index); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4606 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4607 | // We need two extra registers. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4608 | Register scratch = VirtualFrame::scratch0(); |
| 4609 | Register result = VirtualFrame::scratch1(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4610 | |
| 4611 | DeferredStringCharCodeAt* deferred = |
| 4612 | new DeferredStringCharCodeAt(object, |
| 4613 | index, |
| 4614 | scratch, |
| 4615 | result); |
| 4616 | deferred->fast_case_generator()->GenerateFast(masm_); |
| 4617 | deferred->BindExit(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4618 | frame_->EmitPush(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4622 | class DeferredStringCharFromCode : public DeferredCode { |
| 4623 | public: |
| 4624 | DeferredStringCharFromCode(Register code, |
| 4625 | Register result) |
| 4626 | : char_from_code_generator_(code, result) {} |
| 4627 | |
| 4628 | StringCharFromCodeGenerator* fast_case_generator() { |
| 4629 | return &char_from_code_generator_; |
| 4630 | } |
| 4631 | |
| 4632 | virtual void Generate() { |
| 4633 | VirtualFrameRuntimeCallHelper call_helper(frame_state()); |
| 4634 | char_from_code_generator_.GenerateSlow(masm(), call_helper); |
| 4635 | } |
| 4636 | |
| 4637 | private: |
| 4638 | StringCharFromCodeGenerator char_from_code_generator_; |
| 4639 | }; |
| 4640 | |
| 4641 | |
| 4642 | // Generates code for creating a one-char string from a char code. |
| 4643 | void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4644 | Comment(masm_, "[ GenerateStringCharFromCode"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4645 | ASSERT(args->length() == 1); |
| 4646 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4647 | Load(args->at(0)); |
| 4648 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4649 | Register result = frame_->GetTOSRegister(); |
| 4650 | Register code = frame_->PopToRegister(result); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4651 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4652 | DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode( |
| 4653 | code, result); |
| 4654 | deferred->fast_case_generator()->GenerateFast(masm_); |
| 4655 | deferred->BindExit(); |
| 4656 | frame_->EmitPush(result); |
| 4657 | } |
| 4658 | |
| 4659 | |
| 4660 | class DeferredStringCharAt : public DeferredCode { |
| 4661 | public: |
| 4662 | DeferredStringCharAt(Register object, |
| 4663 | Register index, |
| 4664 | Register scratch1, |
| 4665 | Register scratch2, |
| 4666 | Register result) |
| 4667 | : result_(result), |
| 4668 | char_at_generator_(object, |
| 4669 | index, |
| 4670 | scratch1, |
| 4671 | scratch2, |
| 4672 | result, |
| 4673 | &need_conversion_, |
| 4674 | &need_conversion_, |
| 4675 | &index_out_of_range_, |
| 4676 | STRING_INDEX_IS_NUMBER) {} |
| 4677 | |
| 4678 | StringCharAtGenerator* fast_case_generator() { |
| 4679 | return &char_at_generator_; |
| 4680 | } |
| 4681 | |
| 4682 | virtual void Generate() { |
| 4683 | VirtualFrameRuntimeCallHelper call_helper(frame_state()); |
| 4684 | char_at_generator_.GenerateSlow(masm(), call_helper); |
| 4685 | |
| 4686 | __ bind(&need_conversion_); |
| 4687 | // Move smi zero into the result register, which will trigger |
| 4688 | // conversion. |
| 4689 | __ mov(result_, Operand(Smi::FromInt(0))); |
| 4690 | __ jmp(exit_label()); |
| 4691 | |
| 4692 | __ bind(&index_out_of_range_); |
| 4693 | // When the index is out of range, the spec requires us to return |
| 4694 | // the empty string. |
| 4695 | __ LoadRoot(result_, Heap::kEmptyStringRootIndex); |
| 4696 | __ jmp(exit_label()); |
| 4697 | } |
| 4698 | |
| 4699 | private: |
| 4700 | Register result_; |
| 4701 | |
| 4702 | Label need_conversion_; |
| 4703 | Label index_out_of_range_; |
| 4704 | |
| 4705 | StringCharAtGenerator char_at_generator_; |
| 4706 | }; |
| 4707 | |
| 4708 | |
| 4709 | // This generates code that performs a String.prototype.charAt() call |
| 4710 | // or returns a smi in order to trigger conversion. |
| 4711 | void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4712 | Comment(masm_, "[ GenerateStringCharAt"); |
| 4713 | ASSERT(args->length() == 2); |
| 4714 | |
| 4715 | Load(args->at(0)); |
| 4716 | Load(args->at(1)); |
| 4717 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4718 | Register index = frame_->PopToRegister(); |
| 4719 | Register object = frame_->PopToRegister(index); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4720 | |
| 4721 | // We need three extra registers. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4722 | Register scratch1 = VirtualFrame::scratch0(); |
| 4723 | Register scratch2 = VirtualFrame::scratch1(); |
| 4724 | // Use r6 without notifying the virtual frame. |
| 4725 | Register result = r6; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4726 | |
| 4727 | DeferredStringCharAt* deferred = |
| 4728 | new DeferredStringCharAt(object, |
| 4729 | index, |
| 4730 | scratch1, |
| 4731 | scratch2, |
| 4732 | result); |
| 4733 | deferred->fast_case_generator()->GenerateFast(masm_); |
| 4734 | deferred->BindExit(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4735 | frame_->EmitPush(result); |
| 4736 | } |
| 4737 | |
| 4738 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4739 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4740 | ASSERT(args->length() == 1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4741 | Load(args->at(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4742 | JumpTarget answer; |
| 4743 | // We need the CC bits to come out as not_equal in the case where the |
| 4744 | // object is a smi. This can't be done with the usual test opcode so |
| 4745 | // we use XOR to get the right CC bits. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4746 | Register possible_array = frame_->PopToRegister(); |
| 4747 | Register scratch = VirtualFrame::scratch0(); |
| 4748 | __ and_(scratch, possible_array, Operand(kSmiTagMask)); |
| 4749 | __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4750 | answer.Branch(ne); |
| 4751 | // It is a heap object - get the map. Check if the object is a JS array. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4752 | __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4753 | answer.Bind(); |
| 4754 | cc_reg_ = eq; |
| 4755 | } |
| 4756 | |
| 4757 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4758 | void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4759 | ASSERT(args->length() == 1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4760 | Load(args->at(0)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4761 | JumpTarget answer; |
| 4762 | // We need the CC bits to come out as not_equal in the case where the |
| 4763 | // object is a smi. This can't be done with the usual test opcode so |
| 4764 | // we use XOR to get the right CC bits. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4765 | Register possible_regexp = frame_->PopToRegister(); |
| 4766 | Register scratch = VirtualFrame::scratch0(); |
| 4767 | __ and_(scratch, possible_regexp, Operand(kSmiTagMask)); |
| 4768 | __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4769 | answer.Branch(ne); |
| 4770 | // It is a heap object - get the map. Check if the object is a regexp. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4771 | __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4772 | answer.Bind(); |
| 4773 | cc_reg_ = eq; |
| 4774 | } |
| 4775 | |
| 4776 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4777 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 4778 | // This generates a fast version of: |
| 4779 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4780 | ASSERT(args->length() == 1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4781 | Load(args->at(0)); |
| 4782 | Register possible_object = frame_->PopToRegister(); |
| 4783 | __ tst(possible_object, Operand(kSmiTagMask)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4784 | false_target()->Branch(eq); |
| 4785 | |
| 4786 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4787 | __ cmp(possible_object, ip); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4788 | true_target()->Branch(eq); |
| 4789 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4790 | Register map_reg = VirtualFrame::scratch0(); |
| 4791 | __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4792 | // Undetectable objects behave like undefined when tested with typeof. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4793 | __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 4794 | __ tst(possible_object, Operand(1 << Map::kIsUndetectable)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4795 | false_target()->Branch(ne); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4796 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4797 | __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4798 | __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4799 | false_target()->Branch(lt); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4800 | __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4801 | cc_reg_ = le; |
| 4802 | } |
| 4803 | |
| 4804 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 4805 | void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) { |
| 4806 | // This generates a fast version of: |
| 4807 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' || |
| 4808 | // typeof(arg) == function). |
| 4809 | // It includes undetectable objects (as opposed to IsObject). |
| 4810 | ASSERT(args->length() == 1); |
| 4811 | Load(args->at(0)); |
| 4812 | Register value = frame_->PopToRegister(); |
| 4813 | __ tst(value, Operand(kSmiTagMask)); |
| 4814 | false_target()->Branch(eq); |
| 4815 | // Check that this is an object. |
| 4816 | __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset)); |
| 4817 | __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset)); |
| 4818 | __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4819 | cc_reg_ = ge; |
| 4820 | } |
| 4821 | |
| 4822 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 4823 | // Deferred code to check whether the String JavaScript object is safe for using |
| 4824 | // default value of. This code is called after the bit caching this information |
| 4825 | // in the map has been checked with the map for the object in the map_result_ |
| 4826 | // register. On return the register map_result_ contains 1 for true and 0 for |
| 4827 | // false. |
| 4828 | class DeferredIsStringWrapperSafeForDefaultValueOf : public DeferredCode { |
| 4829 | public: |
| 4830 | DeferredIsStringWrapperSafeForDefaultValueOf(Register object, |
| 4831 | Register map_result, |
| 4832 | Register scratch1, |
| 4833 | Register scratch2) |
| 4834 | : object_(object), |
| 4835 | map_result_(map_result), |
| 4836 | scratch1_(scratch1), |
| 4837 | scratch2_(scratch2) { } |
| 4838 | |
| 4839 | virtual void Generate() { |
| 4840 | Label false_result; |
| 4841 | |
| 4842 | // Check that map is loaded as expected. |
| 4843 | if (FLAG_debug_code) { |
| 4844 | __ ldr(ip, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 4845 | __ cmp(map_result_, ip); |
| 4846 | __ Assert(eq, "Map not in expected register"); |
| 4847 | } |
| 4848 | |
| 4849 | // Check for fast case object. Generate false result for slow case object. |
| 4850 | __ ldr(scratch1_, FieldMemOperand(object_, JSObject::kPropertiesOffset)); |
| 4851 | __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset)); |
| 4852 | __ LoadRoot(ip, Heap::kHashTableMapRootIndex); |
| 4853 | __ cmp(scratch1_, ip); |
| 4854 | __ b(eq, &false_result); |
| 4855 | |
| 4856 | // Look for valueOf symbol in the descriptor array, and indicate false if |
| 4857 | // found. The type is not checked, so if it is a transition it is a false |
| 4858 | // negative. |
| 4859 | __ ldr(map_result_, |
| 4860 | FieldMemOperand(map_result_, Map::kInstanceDescriptorsOffset)); |
| 4861 | __ ldr(scratch2_, FieldMemOperand(map_result_, FixedArray::kLengthOffset)); |
| 4862 | // map_result_: descriptor array |
| 4863 | // scratch2_: length of descriptor array |
| 4864 | // Calculate the end of the descriptor array. |
| 4865 | STATIC_ASSERT(kSmiTag == 0); |
| 4866 | STATIC_ASSERT(kSmiTagSize == 1); |
| 4867 | STATIC_ASSERT(kPointerSize == 4); |
| 4868 | __ add(scratch1_, |
| 4869 | map_result_, |
| 4870 | Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 4871 | __ add(scratch1_, |
| 4872 | scratch1_, |
| 4873 | Operand(scratch2_, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4874 | |
| 4875 | // Calculate location of the first key name. |
| 4876 | __ add(map_result_, |
| 4877 | map_result_, |
| 4878 | Operand(FixedArray::kHeaderSize - kHeapObjectTag + |
| 4879 | DescriptorArray::kFirstIndex * kPointerSize)); |
| 4880 | // Loop through all the keys in the descriptor array. If one of these is the |
| 4881 | // symbol valueOf the result is false. |
| 4882 | Label entry, loop; |
| 4883 | // The use of ip to store the valueOf symbol asumes that it is not otherwise |
| 4884 | // used in the loop below. |
| 4885 | __ mov(ip, Operand(Factory::value_of_symbol())); |
| 4886 | __ jmp(&entry); |
| 4887 | __ bind(&loop); |
| 4888 | __ ldr(scratch2_, MemOperand(map_result_, 0)); |
| 4889 | __ cmp(scratch2_, ip); |
| 4890 | __ b(eq, &false_result); |
| 4891 | __ add(map_result_, map_result_, Operand(kPointerSize)); |
| 4892 | __ bind(&entry); |
| 4893 | __ cmp(map_result_, Operand(scratch1_)); |
| 4894 | __ b(ne, &loop); |
| 4895 | |
| 4896 | // Reload map as register map_result_ was used as temporary above. |
| 4897 | __ ldr(map_result_, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 4898 | |
| 4899 | // If a valueOf property is not found on the object check that it's |
| 4900 | // prototype is the un-modified String prototype. If not result is false. |
| 4901 | __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kPrototypeOffset)); |
| 4902 | __ tst(scratch1_, Operand(kSmiTagMask)); |
| 4903 | __ b(eq, &false_result); |
| 4904 | __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset)); |
| 4905 | __ ldr(scratch2_, |
| 4906 | CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 4907 | __ ldr(scratch2_, |
| 4908 | FieldMemOperand(scratch2_, GlobalObject::kGlobalContextOffset)); |
| 4909 | __ ldr(scratch2_, |
| 4910 | CodeGenerator::ContextOperand( |
| 4911 | scratch2_, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX)); |
| 4912 | __ cmp(scratch1_, scratch2_); |
| 4913 | __ b(ne, &false_result); |
| 4914 | |
| 4915 | // Set the bit in the map to indicate that it has been checked safe for |
| 4916 | // default valueOf and set true result. |
| 4917 | __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset)); |
| 4918 | __ orr(scratch1_, |
| 4919 | scratch1_, |
| 4920 | Operand(1 << Map::kStringWrapperSafeForDefaultValueOf)); |
| 4921 | __ str(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset)); |
| 4922 | __ mov(map_result_, Operand(1)); |
| 4923 | __ jmp(exit_label()); |
| 4924 | __ bind(&false_result); |
| 4925 | // Set false result. |
| 4926 | __ mov(map_result_, Operand(0)); |
| 4927 | } |
| 4928 | |
| 4929 | private: |
| 4930 | Register object_; |
| 4931 | Register map_result_; |
| 4932 | Register scratch1_; |
| 4933 | Register scratch2_; |
| 4934 | }; |
| 4935 | |
| 4936 | |
| 4937 | void CodeGenerator::GenerateIsStringWrapperSafeForDefaultValueOf( |
| 4938 | ZoneList<Expression*>* args) { |
| 4939 | ASSERT(args->length() == 1); |
| 4940 | Load(args->at(0)); |
| 4941 | Register obj = frame_->PopToRegister(); // Pop the string wrapper. |
| 4942 | if (FLAG_debug_code) { |
| 4943 | __ AbortIfSmi(obj); |
| 4944 | } |
| 4945 | |
| 4946 | // Check whether this map has already been checked to be safe for default |
| 4947 | // valueOf. |
| 4948 | Register map_result = VirtualFrame::scratch0(); |
| 4949 | __ ldr(map_result, FieldMemOperand(obj, HeapObject::kMapOffset)); |
| 4950 | __ ldrb(ip, FieldMemOperand(map_result, Map::kBitField2Offset)); |
| 4951 | __ tst(ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf)); |
| 4952 | true_target()->Branch(ne); |
| 4953 | |
| 4954 | // We need an additional two scratch registers for the deferred code. |
| 4955 | Register scratch1 = VirtualFrame::scratch1(); |
| 4956 | // Use r6 without notifying the virtual frame. |
| 4957 | Register scratch2 = r6; |
| 4958 | |
| 4959 | DeferredIsStringWrapperSafeForDefaultValueOf* deferred = |
| 4960 | new DeferredIsStringWrapperSafeForDefaultValueOf( |
| 4961 | obj, map_result, scratch1, scratch2); |
| 4962 | deferred->Branch(eq); |
| 4963 | deferred->BindExit(); |
| 4964 | __ tst(map_result, Operand(map_result)); |
| 4965 | cc_reg_ = ne; |
| 4966 | } |
| 4967 | |
| 4968 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4969 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 4970 | // This generates a fast version of: |
| 4971 | // (%_ClassOf(arg) === 'Function') |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4972 | ASSERT(args->length() == 1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4973 | Load(args->at(0)); |
| 4974 | Register possible_function = frame_->PopToRegister(); |
| 4975 | __ tst(possible_function, Operand(kSmiTagMask)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4976 | false_target()->Branch(eq); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4977 | Register map_reg = VirtualFrame::scratch0(); |
| 4978 | Register scratch = VirtualFrame::scratch1(); |
| 4979 | __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4980 | cc_reg_ = eq; |
| 4981 | } |
| 4982 | |
| 4983 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4984 | void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4985 | ASSERT(args->length() == 1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4986 | Load(args->at(0)); |
| 4987 | Register possible_undetectable = frame_->PopToRegister(); |
| 4988 | __ tst(possible_undetectable, Operand(kSmiTagMask)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4989 | false_target()->Branch(eq); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4990 | Register scratch = VirtualFrame::scratch0(); |
| 4991 | __ ldr(scratch, |
| 4992 | FieldMemOperand(possible_undetectable, HeapObject::kMapOffset)); |
| 4993 | __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset)); |
| 4994 | __ tst(scratch, Operand(1 << Map::kIsUndetectable)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4995 | cc_reg_ = ne; |
| 4996 | } |
| 4997 | |
| 4998 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4999 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5000 | ASSERT(args->length() == 0); |
| 5001 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5002 | Register scratch0 = VirtualFrame::scratch0(); |
| 5003 | Register scratch1 = VirtualFrame::scratch1(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5004 | // Get the frame pointer for the calling frame. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5005 | __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5006 | |
| 5007 | // Skip the arguments adaptor frame if it exists. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5008 | __ ldr(scratch1, |
| 5009 | MemOperand(scratch0, StandardFrameConstants::kContextOffset)); |
| 5010 | __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 5011 | __ ldr(scratch0, |
| 5012 | MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5013 | |
| 5014 | // Check the marker in the calling frame. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5015 | __ ldr(scratch1, |
| 5016 | MemOperand(scratch0, StandardFrameConstants::kMarkerOffset)); |
| 5017 | __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5018 | cc_reg_ = eq; |
| 5019 | } |
| 5020 | |
| 5021 | |
| 5022 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5023 | ASSERT(args->length() == 0); |
| 5024 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5025 | Register tos = frame_->GetTOSRegister(); |
| 5026 | Register scratch0 = VirtualFrame::scratch0(); |
| 5027 | Register scratch1 = VirtualFrame::scratch1(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5028 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5029 | // Check if the calling frame is an arguments adaptor frame. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5030 | __ ldr(scratch0, |
| 5031 | MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5032 | __ ldr(scratch1, |
| 5033 | MemOperand(scratch0, StandardFrameConstants::kContextOffset)); |
| 5034 | __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 5035 | |
| 5036 | // Get the number of formal parameters. |
| 5037 | __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5038 | |
| 5039 | // Arguments adaptor case: Read the arguments length from the |
| 5040 | // adaptor frame. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5041 | __ ldr(tos, |
| 5042 | MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset), |
| 5043 | eq); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5044 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5045 | frame_->EmitPush(tos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5046 | } |
| 5047 | |
| 5048 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5049 | void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5050 | ASSERT(args->length() == 1); |
| 5051 | |
| 5052 | // Satisfy contract with ArgumentsAccessStub: |
| 5053 | // Load the key into r1 and the formal parameters count into r0. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5054 | Load(args->at(0)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 5055 | frame_->PopToR1(); |
| 5056 | frame_->SpillAll(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 5057 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5058 | |
| 5059 | // Call the shared stub to get to arguments[key]. |
| 5060 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
| 5061 | frame_->CallStub(&stub, 0); |
| 5062 | frame_->EmitPush(r0); |
| 5063 | } |
| 5064 | |
| 5065 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5066 | void CodeGenerator::GenerateRandomHeapNumber( |
| 5067 | ZoneList<Expression*>* args) { |
| 5068 | VirtualFrame::SpilledScope spilled_scope(frame_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5069 | ASSERT(args->length() == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5070 | |
| 5071 | Label slow_allocate_heapnumber; |
| 5072 | Label heapnumber_allocated; |
| 5073 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5074 | __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex); |
| 5075 | __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5076 | __ jmp(&heapnumber_allocated); |
| 5077 | |
| 5078 | __ bind(&slow_allocate_heapnumber); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 5079 | // Allocate a heap number. |
| 5080 | __ CallRuntime(Runtime::kNumberAlloc, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5081 | __ mov(r4, Operand(r0)); |
| 5082 | |
| 5083 | __ bind(&heapnumber_allocated); |
| 5084 | |
| 5085 | // Convert 32 random bits in r0 to 0.(32 random bits) in a double |
| 5086 | // by computing: |
| 5087 | // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
| 5088 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5089 | __ PrepareCallCFunction(0, r1); |
| 5090 | __ CallCFunction(ExternalReference::random_uint32_function(), 0); |
| 5091 | |
| 5092 | CpuFeatures::Scope scope(VFP3); |
| 5093 | // 0x41300000 is the top half of 1.0 x 2^20 as a double. |
| 5094 | // Create this constant using mov/orr to avoid PC relative load. |
| 5095 | __ mov(r1, Operand(0x41000000)); |
| 5096 | __ orr(r1, r1, Operand(0x300000)); |
| 5097 | // Move 0x41300000xxxxxxxx (x = random bits) to VFP. |
| 5098 | __ vmov(d7, r0, r1); |
| 5099 | // Move 0x4130000000000000 to VFP. |
| 5100 | __ mov(r0, Operand(0)); |
| 5101 | __ vmov(d8, r0, r1); |
| 5102 | // Subtract and store the result in the heap number. |
| 5103 | __ vsub(d7, d7, d8); |
| 5104 | __ sub(r0, r4, Operand(kHeapObjectTag)); |
| 5105 | __ vstr(d7, r0, HeapNumber::kValueOffset); |
| 5106 | frame_->EmitPush(r4); |
| 5107 | } else { |
| 5108 | __ mov(r0, Operand(r4)); |
| 5109 | __ PrepareCallCFunction(1, r1); |
| 5110 | __ CallCFunction( |
| 5111 | ExternalReference::fill_heap_number_with_random_function(), 1); |
| 5112 | frame_->EmitPush(r0); |
| 5113 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5117 | void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) { |
| 5118 | ASSERT_EQ(2, args->length()); |
| 5119 | |
| 5120 | Load(args->at(0)); |
| 5121 | Load(args->at(1)); |
| 5122 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 5123 | StringAddStub stub(NO_STRING_ADD_FLAGS); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5124 | frame_->SpillAll(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 5125 | frame_->CallStub(&stub, 2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5126 | frame_->EmitPush(r0); |
| 5127 | } |
| 5128 | |
| 5129 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5130 | void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) { |
| 5131 | ASSERT_EQ(3, args->length()); |
| 5132 | |
| 5133 | Load(args->at(0)); |
| 5134 | Load(args->at(1)); |
| 5135 | Load(args->at(2)); |
| 5136 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 5137 | SubStringStub stub; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5138 | frame_->SpillAll(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 5139 | frame_->CallStub(&stub, 3); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5140 | frame_->EmitPush(r0); |
| 5141 | } |
| 5142 | |
| 5143 | |
| 5144 | void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) { |
| 5145 | ASSERT_EQ(2, args->length()); |
| 5146 | |
| 5147 | Load(args->at(0)); |
| 5148 | Load(args->at(1)); |
| 5149 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5150 | StringCompareStub stub; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5151 | frame_->SpillAll(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5152 | frame_->CallStub(&stub, 2); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5153 | frame_->EmitPush(r0); |
| 5154 | } |
| 5155 | |
| 5156 | |
| 5157 | void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) { |
| 5158 | ASSERT_EQ(4, args->length()); |
| 5159 | |
| 5160 | Load(args->at(0)); |
| 5161 | Load(args->at(1)); |
| 5162 | Load(args->at(2)); |
| 5163 | Load(args->at(3)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5164 | RegExpExecStub stub; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5165 | frame_->SpillAll(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5166 | frame_->CallStub(&stub, 4); |
| 5167 | frame_->EmitPush(r0); |
| 5168 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5169 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5170 | |
| 5171 | void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) { |
| 5172 | // No stub. This code only occurs a few times in regexp.js. |
| 5173 | const int kMaxInlineLength = 100; |
| 5174 | ASSERT_EQ(3, args->length()); |
| 5175 | Load(args->at(0)); // Size of array, smi. |
| 5176 | Load(args->at(1)); // "index" property value. |
| 5177 | Load(args->at(2)); // "input" property value. |
| 5178 | { |
| 5179 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 5180 | Label slowcase; |
| 5181 | Label done; |
| 5182 | __ ldr(r1, MemOperand(sp, kPointerSize * 2)); |
| 5183 | STATIC_ASSERT(kSmiTag == 0); |
| 5184 | STATIC_ASSERT(kSmiTagSize == 1); |
| 5185 | __ tst(r1, Operand(kSmiTagMask)); |
| 5186 | __ b(ne, &slowcase); |
| 5187 | __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength))); |
| 5188 | __ b(hi, &slowcase); |
| 5189 | // Smi-tagging is equivalent to multiplying by 2. |
| 5190 | // Allocate RegExpResult followed by FixedArray with size in ebx. |
| 5191 | // JSArray: [Map][empty properties][Elements][Length-smi][index][input] |
| 5192 | // Elements: [Map][Length][..elements..] |
| 5193 | // Size of JSArray with two in-object properties and the header of a |
| 5194 | // FixedArray. |
| 5195 | int objects_size = |
| 5196 | (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize; |
| 5197 | __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize)); |
| 5198 | __ add(r2, r5, Operand(objects_size)); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5199 | __ AllocateInNewSpace( |
| 5200 | r2, // In: Size, in words. |
| 5201 | r0, // Out: Start of allocation (tagged). |
| 5202 | r3, // Scratch register. |
| 5203 | r4, // Scratch register. |
| 5204 | &slowcase, |
| 5205 | static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5206 | // r0: Start of allocated area, object-tagged. |
| 5207 | // r1: Number of elements in array, as smi. |
| 5208 | // r5: Number of elements, untagged. |
| 5209 | |
| 5210 | // Set JSArray map to global.regexp_result_map(). |
| 5211 | // Set empty properties FixedArray. |
| 5212 | // Set elements to point to FixedArray allocated right after the JSArray. |
| 5213 | // Interleave operations for better latency. |
| 5214 | __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 5215 | __ add(r3, r0, Operand(JSRegExpResult::kSize)); |
| 5216 | __ mov(r4, Operand(Factory::empty_fixed_array())); |
| 5217 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 5218 | __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 5219 | __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX)); |
| 5220 | __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset)); |
| 5221 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 5222 | |
| 5223 | // Set input, index and length fields from arguments. |
| 5224 | __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit())); |
| 5225 | __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset)); |
| 5226 | __ add(sp, sp, Operand(kPointerSize)); |
| 5227 | __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset)); |
| 5228 | __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset)); |
| 5229 | |
| 5230 | // Fill out the elements FixedArray. |
| 5231 | // r0: JSArray, tagged. |
| 5232 | // r3: FixedArray, tagged. |
| 5233 | // r5: Number of elements in array, untagged. |
| 5234 | |
| 5235 | // Set map. |
| 5236 | __ mov(r2, Operand(Factory::fixed_array_map())); |
| 5237 | __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 5238 | // Set FixedArray length. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5239 | __ mov(r6, Operand(r5, LSL, kSmiTagSize)); |
| 5240 | __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5241 | // Fill contents of fixed-array with the-hole. |
| 5242 | __ mov(r2, Operand(Factory::the_hole_value())); |
| 5243 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 5244 | // Fill fixed array elements with hole. |
| 5245 | // r0: JSArray, tagged. |
| 5246 | // r2: the hole. |
| 5247 | // r3: Start of elements in FixedArray. |
| 5248 | // r5: Number of elements to fill. |
| 5249 | Label loop; |
| 5250 | __ tst(r5, Operand(r5)); |
| 5251 | __ bind(&loop); |
| 5252 | __ b(le, &done); // Jump if r1 is negative or zero. |
| 5253 | __ sub(r5, r5, Operand(1), SetCC); |
| 5254 | __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2)); |
| 5255 | __ jmp(&loop); |
| 5256 | |
| 5257 | __ bind(&slowcase); |
| 5258 | __ CallRuntime(Runtime::kRegExpConstructResult, 3); |
| 5259 | |
| 5260 | __ bind(&done); |
| 5261 | } |
| 5262 | frame_->Forget(3); |
| 5263 | frame_->EmitPush(r0); |
| 5264 | } |
| 5265 | |
| 5266 | |
| 5267 | class DeferredSearchCache: public DeferredCode { |
| 5268 | public: |
| 5269 | DeferredSearchCache(Register dst, Register cache, Register key) |
| 5270 | : dst_(dst), cache_(cache), key_(key) { |
| 5271 | set_comment("[ DeferredSearchCache"); |
| 5272 | } |
| 5273 | |
| 5274 | virtual void Generate(); |
| 5275 | |
| 5276 | private: |
| 5277 | Register dst_, cache_, key_; |
| 5278 | }; |
| 5279 | |
| 5280 | |
| 5281 | void DeferredSearchCache::Generate() { |
| 5282 | __ Push(cache_, key_); |
| 5283 | __ CallRuntime(Runtime::kGetFromCache, 2); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 5284 | __ Move(dst_, r0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5285 | } |
| 5286 | |
| 5287 | |
| 5288 | void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) { |
| 5289 | ASSERT_EQ(2, args->length()); |
| 5290 | |
| 5291 | ASSERT_NE(NULL, args->at(0)->AsLiteral()); |
| 5292 | int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value(); |
| 5293 | |
| 5294 | Handle<FixedArray> jsfunction_result_caches( |
| 5295 | Top::global_context()->jsfunction_result_caches()); |
| 5296 | if (jsfunction_result_caches->length() <= cache_id) { |
| 5297 | __ Abort("Attempt to use undefined cache."); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5298 | frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5299 | return; |
| 5300 | } |
| 5301 | |
| 5302 | Load(args->at(1)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5303 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 5304 | frame_->PopToR1(); |
| 5305 | frame_->SpillAll(); |
| 5306 | Register key = r1; // Just poped to r1 |
| 5307 | Register result = r0; // Free, as frame has just been spilled. |
| 5308 | Register scratch1 = VirtualFrame::scratch0(); |
| 5309 | Register scratch2 = VirtualFrame::scratch1(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5310 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 5311 | __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 5312 | __ ldr(scratch1, |
| 5313 | FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset)); |
| 5314 | __ ldr(scratch1, |
| 5315 | ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX)); |
| 5316 | __ ldr(scratch1, |
| 5317 | FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5318 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 5319 | DeferredSearchCache* deferred = |
| 5320 | new DeferredSearchCache(result, scratch1, key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5321 | |
| 5322 | const int kFingerOffset = |
| 5323 | FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 5324 | STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 5325 | __ ldr(result, FieldMemOperand(scratch1, kFingerOffset)); |
| 5326 | // result now holds finger offset as a smi. |
| 5327 | __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 5328 | // scratch2 now points to the start of fixed array elements. |
| 5329 | __ ldr(result, |
| 5330 | MemOperand( |
| 5331 | scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex)); |
| 5332 | // Note side effect of PreIndex: scratch2 now points to the key of the pair. |
| 5333 | __ cmp(key, result); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5334 | deferred->Branch(ne); |
| 5335 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 5336 | __ ldr(result, MemOperand(scratch2, kPointerSize)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5337 | |
| 5338 | deferred->BindExit(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 5339 | frame_->EmitPush(result); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
| 5342 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5343 | void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) { |
| 5344 | ASSERT_EQ(args->length(), 1); |
| 5345 | |
| 5346 | // Load the argument on the stack and jump to the runtime. |
| 5347 | Load(args->at(0)); |
| 5348 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5349 | NumberToStringStub stub; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5350 | frame_->SpillAll(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5351 | frame_->CallStub(&stub, 1); |
| 5352 | frame_->EmitPush(r0); |
| 5353 | } |
| 5354 | |
| 5355 | |
| 5356 | class DeferredSwapElements: public DeferredCode { |
| 5357 | public: |
| 5358 | DeferredSwapElements(Register object, Register index1, Register index2) |
| 5359 | : object_(object), index1_(index1), index2_(index2) { |
| 5360 | set_comment("[ DeferredSwapElements"); |
| 5361 | } |
| 5362 | |
| 5363 | virtual void Generate(); |
| 5364 | |
| 5365 | private: |
| 5366 | Register object_, index1_, index2_; |
| 5367 | }; |
| 5368 | |
| 5369 | |
| 5370 | void DeferredSwapElements::Generate() { |
| 5371 | __ push(object_); |
| 5372 | __ push(index1_); |
| 5373 | __ push(index2_); |
| 5374 | __ CallRuntime(Runtime::kSwapElements, 3); |
| 5375 | } |
| 5376 | |
| 5377 | |
| 5378 | void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) { |
| 5379 | Comment cmnt(masm_, "[ GenerateSwapElements"); |
| 5380 | |
| 5381 | ASSERT_EQ(3, args->length()); |
| 5382 | |
| 5383 | Load(args->at(0)); |
| 5384 | Load(args->at(1)); |
| 5385 | Load(args->at(2)); |
| 5386 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5387 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 5388 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5389 | Register index2 = r2; |
| 5390 | Register index1 = r1; |
| 5391 | Register object = r0; |
| 5392 | Register tmp1 = r3; |
| 5393 | Register tmp2 = r4; |
| 5394 | |
| 5395 | frame_->EmitPop(index2); |
| 5396 | frame_->EmitPop(index1); |
| 5397 | frame_->EmitPop(object); |
| 5398 | |
| 5399 | DeferredSwapElements* deferred = |
| 5400 | new DeferredSwapElements(object, index1, index2); |
| 5401 | |
| 5402 | // Fetch the map and check if array is in fast case. |
| 5403 | // Check that object doesn't require security checks and |
| 5404 | // has no indexed interceptor. |
| 5405 | __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE); |
| 5406 | deferred->Branch(lt); |
| 5407 | __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset)); |
| 5408 | __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask)); |
| 5409 | deferred->Branch(nz); |
| 5410 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 5411 | // Check the object's elements are in fast case and writable. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5412 | __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset)); |
| 5413 | __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset)); |
| 5414 | __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex); |
| 5415 | __ cmp(tmp2, ip); |
| 5416 | deferred->Branch(ne); |
| 5417 | |
| 5418 | // Smi-tagging is equivalent to multiplying by 2. |
| 5419 | STATIC_ASSERT(kSmiTag == 0); |
| 5420 | STATIC_ASSERT(kSmiTagSize == 1); |
| 5421 | |
| 5422 | // Check that both indices are smis. |
| 5423 | __ mov(tmp2, index1); |
| 5424 | __ orr(tmp2, tmp2, index2); |
| 5425 | __ tst(tmp2, Operand(kSmiTagMask)); |
| 5426 | deferred->Branch(nz); |
| 5427 | |
| 5428 | // Bring the offsets into the fixed array in tmp1 into index1 and |
| 5429 | // index2. |
| 5430 | __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 5431 | __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5432 | __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5433 | |
| 5434 | // Swap elements. |
| 5435 | Register tmp3 = object; |
| 5436 | object = no_reg; |
| 5437 | __ ldr(tmp3, MemOperand(tmp1, index1)); |
| 5438 | __ ldr(tmp2, MemOperand(tmp1, index2)); |
| 5439 | __ str(tmp3, MemOperand(tmp1, index2)); |
| 5440 | __ str(tmp2, MemOperand(tmp1, index1)); |
| 5441 | |
| 5442 | Label done; |
| 5443 | __ InNewSpace(tmp1, tmp2, eq, &done); |
| 5444 | // Possible optimization: do a check that both values are Smis |
| 5445 | // (or them and test against Smi mask.) |
| 5446 | |
| 5447 | __ mov(tmp2, tmp1); |
| 5448 | RecordWriteStub recordWrite1(tmp1, index1, tmp3); |
| 5449 | __ CallStub(&recordWrite1); |
| 5450 | |
| 5451 | RecordWriteStub recordWrite2(tmp2, index2, tmp3); |
| 5452 | __ CallStub(&recordWrite2); |
| 5453 | |
| 5454 | __ bind(&done); |
| 5455 | |
| 5456 | deferred->BindExit(); |
| 5457 | __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex); |
| 5458 | frame_->EmitPush(tmp1); |
| 5459 | } |
| 5460 | |
| 5461 | |
| 5462 | void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) { |
| 5463 | Comment cmnt(masm_, "[ GenerateCallFunction"); |
| 5464 | |
| 5465 | ASSERT(args->length() >= 2); |
| 5466 | |
| 5467 | int n_args = args->length() - 2; // for receiver and function. |
| 5468 | Load(args->at(0)); // receiver |
| 5469 | for (int i = 0; i < n_args; i++) { |
| 5470 | Load(args->at(i + 1)); |
| 5471 | } |
| 5472 | Load(args->at(n_args + 1)); // function |
| 5473 | frame_->CallJSFunction(n_args); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5474 | frame_->EmitPush(r0); |
| 5475 | } |
| 5476 | |
| 5477 | |
| 5478 | void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) { |
| 5479 | ASSERT_EQ(args->length(), 1); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5480 | Load(args->at(0)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5481 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5482 | TranscendentalCacheStub stub(TranscendentalCache::SIN); |
| 5483 | frame_->SpillAllButCopyTOSToR0(); |
| 5484 | frame_->CallStub(&stub, 1); |
| 5485 | } else { |
| 5486 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 5487 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5488 | frame_->EmitPush(r0); |
| 5489 | } |
| 5490 | |
| 5491 | |
| 5492 | void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) { |
| 5493 | ASSERT_EQ(args->length(), 1); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5494 | Load(args->at(0)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5495 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5496 | TranscendentalCacheStub stub(TranscendentalCache::COS); |
| 5497 | frame_->SpillAllButCopyTOSToR0(); |
| 5498 | frame_->CallStub(&stub, 1); |
| 5499 | } else { |
| 5500 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 5501 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5502 | frame_->EmitPush(r0); |
| 5503 | } |
| 5504 | |
| 5505 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5506 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5507 | ASSERT(args->length() == 2); |
| 5508 | |
| 5509 | // Load the two objects into registers and perform the comparison. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5510 | Load(args->at(0)); |
| 5511 | Load(args->at(1)); |
| 5512 | Register lhs = frame_->PopToRegister(); |
| 5513 | Register rhs = frame_->PopToRegister(lhs); |
| 5514 | __ cmp(lhs, rhs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5515 | cc_reg_ = eq; |
| 5516 | } |
| 5517 | |
| 5518 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5519 | void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) { |
| 5520 | ASSERT(args->length() == 2); |
| 5521 | |
| 5522 | // Load the two objects into registers and perform the comparison. |
| 5523 | Load(args->at(0)); |
| 5524 | Load(args->at(1)); |
| 5525 | Register right = frame_->PopToRegister(); |
| 5526 | Register left = frame_->PopToRegister(right); |
| 5527 | Register tmp = frame_->scratch0(); |
| 5528 | Register tmp2 = frame_->scratch1(); |
| 5529 | |
| 5530 | // Jumps to done must have the eq flag set if the test is successful |
| 5531 | // and clear if the test has failed. |
| 5532 | Label done; |
| 5533 | |
| 5534 | // Fail if either is a non-HeapObject. |
| 5535 | __ cmp(left, Operand(right)); |
| 5536 | __ b(eq, &done); |
| 5537 | __ and_(tmp, left, Operand(right)); |
| 5538 | __ eor(tmp, tmp, Operand(kSmiTagMask)); |
| 5539 | __ tst(tmp, Operand(kSmiTagMask)); |
| 5540 | __ b(ne, &done); |
| 5541 | __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset)); |
| 5542 | __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset)); |
| 5543 | __ cmp(tmp2, Operand(JS_REGEXP_TYPE)); |
| 5544 | __ b(ne, &done); |
| 5545 | __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset)); |
| 5546 | __ cmp(tmp, Operand(tmp2)); |
| 5547 | __ b(ne, &done); |
| 5548 | __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset)); |
| 5549 | __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset)); |
| 5550 | __ cmp(tmp, tmp2); |
| 5551 | __ bind(&done); |
| 5552 | cc_reg_ = eq; |
| 5553 | } |
| 5554 | |
| 5555 | |
| 5556 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5557 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
| 5558 | #ifdef DEBUG |
| 5559 | int original_height = frame_->height(); |
| 5560 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5561 | if (CheckForInlineRuntimeCall(node)) { |
| 5562 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 5563 | (!has_cc() && frame_->height() == original_height + 1)); |
| 5564 | return; |
| 5565 | } |
| 5566 | |
| 5567 | ZoneList<Expression*>* args = node->arguments(); |
| 5568 | Comment cmnt(masm_, "[ CallRuntime"); |
| 5569 | Runtime::Function* function = node->function(); |
| 5570 | |
| 5571 | if (function == NULL) { |
| 5572 | // Prepare stack for calling JS runtime function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5573 | // Push the builtins object found in the current global object. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5574 | Register scratch = VirtualFrame::scratch0(); |
| 5575 | __ ldr(scratch, GlobalObject()); |
| 5576 | Register builtins = frame_->GetTOSRegister(); |
| 5577 | __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset)); |
| 5578 | frame_->EmitPush(builtins); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | // Push the arguments ("left-to-right"). |
| 5582 | int arg_count = args->length(); |
| 5583 | for (int i = 0; i < arg_count; i++) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5584 | Load(args->at(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5585 | } |
| 5586 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5587 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 5588 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5589 | if (function == NULL) { |
| 5590 | // Call the JS runtime function. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5591 | __ mov(r2, Operand(node->name())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5592 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 5593 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
| 5594 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
| 5595 | __ ldr(cp, frame_->Context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5596 | frame_->EmitPush(r0); |
| 5597 | } else { |
| 5598 | // Call the C runtime function. |
| 5599 | frame_->CallRuntime(function, arg_count); |
| 5600 | frame_->EmitPush(r0); |
| 5601 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5602 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | |
| 5606 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
| 5607 | #ifdef DEBUG |
| 5608 | int original_height = frame_->height(); |
| 5609 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5610 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 5611 | |
| 5612 | Token::Value op = node->op(); |
| 5613 | |
| 5614 | if (op == Token::NOT) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5615 | LoadCondition(node->expression(), false_target(), true_target(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5616 | // LoadCondition may (and usually does) leave a test and branch to |
| 5617 | // be emitted by the caller. In that case, negate the condition. |
| 5618 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
| 5619 | |
| 5620 | } else if (op == Token::DELETE) { |
| 5621 | Property* property = node->expression()->AsProperty(); |
| 5622 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
| 5623 | if (property != NULL) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5624 | Load(property->obj()); |
| 5625 | Load(property->key()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5626 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5627 | frame_->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5628 | |
| 5629 | } else if (variable != NULL) { |
| 5630 | Slot* slot = variable->slot(); |
| 5631 | if (variable->is_global()) { |
| 5632 | LoadGlobal(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5633 | frame_->EmitPush(Operand(variable->name())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5634 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5635 | frame_->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5636 | |
| 5637 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 5638 | // lookup the context holding the named variable |
| 5639 | frame_->EmitPush(cp); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5640 | frame_->EmitPush(Operand(variable->name())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5641 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
| 5642 | // r0: context |
| 5643 | frame_->EmitPush(r0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5644 | frame_->EmitPush(Operand(variable->name())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5645 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5646 | frame_->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5647 | |
| 5648 | } else { |
| 5649 | // Default: Result of deleting non-global, not dynamically |
| 5650 | // introduced variables is false. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5651 | frame_->EmitPushRoot(Heap::kFalseValueRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
| 5654 | } else { |
| 5655 | // Default: Result of deleting expressions is true. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5656 | Load(node->expression()); // may have side-effects |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5657 | frame_->Drop(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5658 | frame_->EmitPushRoot(Heap::kTrueValueRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5659 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5660 | |
| 5661 | } else if (op == Token::TYPEOF) { |
| 5662 | // Special case for loading the typeof expression; see comment on |
| 5663 | // LoadTypeofExpression(). |
| 5664 | LoadTypeofExpression(node->expression()); |
| 5665 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 5666 | frame_->EmitPush(r0); // r0 has result |
| 5667 | |
| 5668 | } else { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 5669 | bool can_overwrite = |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5670 | (node->expression()->AsBinaryOperation() != NULL && |
| 5671 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 5672 | UnaryOverwriteMode overwrite = |
| 5673 | can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE; |
| 5674 | |
| 5675 | bool no_negative_zero = node->expression()->no_negative_zero(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5676 | Load(node->expression()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5677 | switch (op) { |
| 5678 | case Token::NOT: |
| 5679 | case Token::DELETE: |
| 5680 | case Token::TYPEOF: |
| 5681 | UNREACHABLE(); // handled above |
| 5682 | break; |
| 5683 | |
| 5684 | case Token::SUB: { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5685 | frame_->PopToR0(); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 5686 | GenericUnaryOpStub stub( |
| 5687 | Token::SUB, |
| 5688 | overwrite, |
| 5689 | no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5690 | frame_->CallStub(&stub, 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5691 | frame_->EmitPush(r0); // r0 has result |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5692 | break; |
| 5693 | } |
| 5694 | |
| 5695 | case Token::BIT_NOT: { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5696 | Register tos = frame_->PopToRegister(); |
| 5697 | JumpTarget not_smi_label; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5698 | JumpTarget continue_label; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5699 | // Smi check. |
| 5700 | __ tst(tos, Operand(kSmiTagMask)); |
| 5701 | not_smi_label.Branch(ne); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5702 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5703 | __ mvn(tos, Operand(tos)); |
| 5704 | __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag. |
| 5705 | frame_->EmitPush(tos); |
| 5706 | // The fast case is the first to jump to the continue label, so it gets |
| 5707 | // to decide the virtual frame layout. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5708 | continue_label.Jump(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5709 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5710 | not_smi_label.Bind(); |
| 5711 | frame_->SpillAll(); |
| 5712 | __ Move(r0, tos); |
| 5713 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 5714 | frame_->CallStub(&stub, 0); |
| 5715 | frame_->EmitPush(r0); |
| 5716 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5717 | continue_label.Bind(); |
| 5718 | break; |
| 5719 | } |
| 5720 | |
| 5721 | case Token::VOID: |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5722 | frame_->Drop(); |
| 5723 | frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5724 | break; |
| 5725 | |
| 5726 | case Token::ADD: { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5727 | Register tos = frame_->Peek(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5728 | // Smi check. |
| 5729 | JumpTarget continue_label; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5730 | __ tst(tos, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5731 | continue_label.Branch(eq); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5732 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5733 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5734 | frame_->EmitPush(r0); |
| 5735 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5736 | continue_label.Bind(); |
| 5737 | break; |
| 5738 | } |
| 5739 | default: |
| 5740 | UNREACHABLE(); |
| 5741 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5742 | } |
| 5743 | ASSERT(!has_valid_frame() || |
| 5744 | (has_cc() && frame_->height() == original_height) || |
| 5745 | (!has_cc() && frame_->height() == original_height + 1)); |
| 5746 | } |
| 5747 | |
| 5748 | |
| 5749 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
| 5750 | #ifdef DEBUG |
| 5751 | int original_height = frame_->height(); |
| 5752 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5753 | Comment cmnt(masm_, "[ CountOperation"); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5754 | VirtualFrame::RegisterAllocationScope scope(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5755 | |
| 5756 | bool is_postfix = node->is_postfix(); |
| 5757 | bool is_increment = node->op() == Token::INC; |
| 5758 | |
| 5759 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 5760 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5761 | bool is_slot = (var != NULL && var->mode() == Variable::VAR); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5762 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5763 | if (!is_const && is_slot && type_info(var->slot()).IsSmi()) { |
| 5764 | // The type info declares that this variable is always a Smi. That |
| 5765 | // means it is a Smi both before and after the increment/decrement. |
| 5766 | // Lets make use of that to make a very minimal count. |
| 5767 | Reference target(this, node->expression(), !is_const); |
| 5768 | ASSERT(!target.is_illegal()); |
| 5769 | target.GetValue(); // Pushes the value. |
| 5770 | Register value = frame_->PopToRegister(); |
| 5771 | if (is_postfix) frame_->EmitPush(value); |
| 5772 | if (is_increment) { |
| 5773 | __ add(value, value, Operand(Smi::FromInt(1))); |
| 5774 | } else { |
| 5775 | __ sub(value, value, Operand(Smi::FromInt(1))); |
| 5776 | } |
| 5777 | frame_->EmitPush(value); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5778 | target.SetValue(NOT_CONST_INIT, LIKELY_SMI); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5779 | if (is_postfix) frame_->Pop(); |
| 5780 | ASSERT_EQ(original_height + 1, frame_->height()); |
| 5781 | return; |
| 5782 | } |
| 5783 | |
| 5784 | // If it's a postfix expression and its result is not ignored and the |
| 5785 | // reference is non-trivial, then push a placeholder on the stack now |
| 5786 | // to hold the result of the expression. |
| 5787 | bool placeholder_pushed = false; |
| 5788 | if (!is_slot && is_postfix) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5789 | frame_->EmitPush(Operand(Smi::FromInt(0))); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5790 | placeholder_pushed = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5791 | } |
| 5792 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5793 | // A constant reference is not saved to, so a constant reference is not a |
| 5794 | // compound assignment reference. |
| 5795 | { Reference target(this, node->expression(), !is_const); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5796 | if (target.is_illegal()) { |
| 5797 | // Spoof the virtual frame to have the expected height (one higher |
| 5798 | // than on entry). |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5799 | if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5800 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5801 | return; |
| 5802 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5803 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5804 | // This pushes 0, 1 or 2 words on the object to be used later when updating |
| 5805 | // the target. It also pushes the current value of the target. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5806 | target.GetValue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5807 | |
| 5808 | JumpTarget slow; |
| 5809 | JumpTarget exit; |
| 5810 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5811 | Register value = frame_->PopToRegister(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5812 | |
| 5813 | // Postfix: Store the old value as the result. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5814 | if (placeholder_pushed) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5815 | frame_->SetElementAt(value, target.size()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5816 | } else if (is_postfix) { |
| 5817 | frame_->EmitPush(value); |
| 5818 | __ mov(VirtualFrame::scratch0(), value); |
| 5819 | value = VirtualFrame::scratch0(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5820 | } |
| 5821 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5822 | // Check for smi operand. |
| 5823 | __ tst(value, Operand(kSmiTagMask)); |
| 5824 | slow.Branch(ne); |
| 5825 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5826 | // Perform optimistic increment/decrement. |
| 5827 | if (is_increment) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5828 | __ add(value, value, Operand(Smi::FromInt(1)), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5829 | } else { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5830 | __ sub(value, value, Operand(Smi::FromInt(1)), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | // If the increment/decrement didn't overflow, we're done. |
| 5834 | exit.Branch(vc); |
| 5835 | |
| 5836 | // Revert optimistic increment/decrement. |
| 5837 | if (is_increment) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5838 | __ sub(value, value, Operand(Smi::FromInt(1))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5839 | } else { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5840 | __ add(value, value, Operand(Smi::FromInt(1))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5841 | } |
| 5842 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5843 | // Slow case: Convert to number. At this point the |
| 5844 | // value to be incremented is in the value register.. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5845 | slow.Bind(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5846 | |
| 5847 | // Convert the operand to a number. |
| 5848 | frame_->EmitPush(value); |
| 5849 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5850 | { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5851 | VirtualFrame::SpilledScope spilled(frame_); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5852 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5853 | |
| 5854 | if (is_postfix) { |
| 5855 | // Postfix: store to result (on the stack). |
| 5856 | __ str(r0, frame_->ElementAt(target.size())); |
| 5857 | } |
| 5858 | |
| 5859 | // Compute the new value. |
| 5860 | frame_->EmitPush(r0); |
| 5861 | frame_->EmitPush(Operand(Smi::FromInt(1))); |
| 5862 | if (is_increment) { |
| 5863 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
| 5864 | } else { |
| 5865 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
| 5866 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5867 | } |
| 5868 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5869 | __ Move(value, r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5870 | // Store the new value in the target if not const. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5871 | // At this point the answer is in the value register. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5872 | exit.Bind(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5873 | frame_->EmitPush(value); |
| 5874 | // Set the target with the result, leaving the result on |
| 5875 | // top of the stack. Removes the target from the stack if |
| 5876 | // it has a non-zero size. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5877 | if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
| 5880 | // Postfix: Discard the new value and use the old. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5881 | if (is_postfix) frame_->Pop(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5882 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
| 5885 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5886 | void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5887 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 5888 | // operators must yield the result of one of the two expressions |
| 5889 | // before any ToBoolean() conversions. This means that the value |
| 5890 | // produced by a && or || operator is not necessarily a boolean. |
| 5891 | |
| 5892 | // NOTE: If the left hand side produces a materialized value (not in |
| 5893 | // the CC register), we force the right hand side to do the |
| 5894 | // same. This is necessary because we may have to branch to the exit |
| 5895 | // after evaluating the left hand side (due to the shortcut |
| 5896 | // semantics), but the compiler must (statically) know if the result |
| 5897 | // of compiling the binary operation is materialized or not. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5898 | if (node->op() == Token::AND) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5899 | JumpTarget is_true; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5900 | LoadCondition(node->left(), &is_true, false_target(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5901 | if (has_valid_frame() && !has_cc()) { |
| 5902 | // The left-hand side result is on top of the virtual frame. |
| 5903 | JumpTarget pop_and_continue; |
| 5904 | JumpTarget exit; |
| 5905 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5906 | frame_->Dup(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5907 | // Avoid popping the result if it converts to 'false' using the |
| 5908 | // standard ToBoolean() conversion as described in ECMA-262, |
| 5909 | // section 9.2, page 30. |
| 5910 | ToBoolean(&pop_and_continue, &exit); |
| 5911 | Branch(false, &exit); |
| 5912 | |
| 5913 | // Pop the result of evaluating the first part. |
| 5914 | pop_and_continue.Bind(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5915 | frame_->Pop(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5916 | |
| 5917 | // Evaluate right side expression. |
| 5918 | is_true.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5919 | Load(node->right()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5920 | |
| 5921 | // Exit (always with a materialized value). |
| 5922 | exit.Bind(); |
| 5923 | } else if (has_cc() || is_true.is_linked()) { |
| 5924 | // The left-hand side is either (a) partially compiled to |
| 5925 | // control flow with a final branch left to emit or (b) fully |
| 5926 | // compiled to control flow and possibly true. |
| 5927 | if (has_cc()) { |
| 5928 | Branch(false, false_target()); |
| 5929 | } |
| 5930 | is_true.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5931 | LoadCondition(node->right(), true_target(), false_target(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5932 | } else { |
| 5933 | // Nothing to do. |
| 5934 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
| 5935 | } |
| 5936 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5937 | } else { |
| 5938 | ASSERT(node->op() == Token::OR); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5939 | JumpTarget is_false; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5940 | LoadCondition(node->left(), true_target(), &is_false, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5941 | if (has_valid_frame() && !has_cc()) { |
| 5942 | // The left-hand side result is on top of the virtual frame. |
| 5943 | JumpTarget pop_and_continue; |
| 5944 | JumpTarget exit; |
| 5945 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5946 | frame_->Dup(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5947 | // Avoid popping the result if it converts to 'true' using the |
| 5948 | // standard ToBoolean() conversion as described in ECMA-262, |
| 5949 | // section 9.2, page 30. |
| 5950 | ToBoolean(&exit, &pop_and_continue); |
| 5951 | Branch(true, &exit); |
| 5952 | |
| 5953 | // Pop the result of evaluating the first part. |
| 5954 | pop_and_continue.Bind(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5955 | frame_->Pop(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5956 | |
| 5957 | // Evaluate right side expression. |
| 5958 | is_false.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5959 | Load(node->right()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5960 | |
| 5961 | // Exit (always with a materialized value). |
| 5962 | exit.Bind(); |
| 5963 | } else if (has_cc() || is_false.is_linked()) { |
| 5964 | // The left-hand side is either (a) partially compiled to |
| 5965 | // control flow with a final branch left to emit or (b) fully |
| 5966 | // compiled to control flow and possibly false. |
| 5967 | if (has_cc()) { |
| 5968 | Branch(true, true_target()); |
| 5969 | } |
| 5970 | is_false.Bind(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5971 | LoadCondition(node->right(), true_target(), false_target(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5972 | } else { |
| 5973 | // Nothing to do. |
| 5974 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
| 5975 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5976 | } |
| 5977 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5978 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5979 | |
| 5980 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
| 5981 | #ifdef DEBUG |
| 5982 | int original_height = frame_->height(); |
| 5983 | #endif |
| 5984 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 5985 | |
| 5986 | if (node->op() == Token::AND || node->op() == Token::OR) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5987 | GenerateLogicalBooleanOperation(node); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5988 | } else { |
| 5989 | // Optimize for the case where (at least) one of the expressions |
| 5990 | // is a literal small integer. |
| 5991 | Literal* lliteral = node->left()->AsLiteral(); |
| 5992 | Literal* rliteral = node->right()->AsLiteral(); |
| 5993 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 5994 | // never return a constant/immutable object. |
| 5995 | bool overwrite_left = |
| 5996 | (node->left()->AsBinaryOperation() != NULL && |
| 5997 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 5998 | bool overwrite_right = |
| 5999 | (node->right()->AsBinaryOperation() != NULL && |
| 6000 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 6001 | |
| 6002 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6003 | VirtualFrame::RegisterAllocationScope scope(this); |
| 6004 | Load(node->left()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6005 | if (frame_->KnownSmiAt(0)) overwrite_left = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6006 | SmiOperation(node->op(), |
| 6007 | rliteral->handle(), |
| 6008 | false, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6009 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6010 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6011 | VirtualFrame::RegisterAllocationScope scope(this); |
| 6012 | Load(node->right()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6013 | if (frame_->KnownSmiAt(0)) overwrite_right = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6014 | SmiOperation(node->op(), |
| 6015 | lliteral->handle(), |
| 6016 | true, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6017 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6018 | } else { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6019 | GenerateInlineSmi inline_smi = |
| 6020 | loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI; |
| 6021 | if (lliteral != NULL) { |
| 6022 | ASSERT(!lliteral->handle()->IsSmi()); |
| 6023 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 6024 | } |
| 6025 | if (rliteral != NULL) { |
| 6026 | ASSERT(!rliteral->handle()->IsSmi()); |
| 6027 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 6028 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6029 | VirtualFrame::RegisterAllocationScope scope(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6030 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 6031 | if (overwrite_left) { |
| 6032 | overwrite_mode = OVERWRITE_LEFT; |
| 6033 | } else if (overwrite_right) { |
| 6034 | overwrite_mode = OVERWRITE_RIGHT; |
| 6035 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6036 | Load(node->left()); |
| 6037 | Load(node->right()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6038 | GenericBinaryOperation(node->op(), overwrite_mode, inline_smi); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6039 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6040 | } |
| 6041 | ASSERT(!has_valid_frame() || |
| 6042 | (has_cc() && frame_->height() == original_height) || |
| 6043 | (!has_cc() && frame_->height() == original_height + 1)); |
| 6044 | } |
| 6045 | |
| 6046 | |
| 6047 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
| 6048 | #ifdef DEBUG |
| 6049 | int original_height = frame_->height(); |
| 6050 | #endif |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6051 | frame_->EmitPush(MemOperand(frame_->Function())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6052 | ASSERT_EQ(original_height + 1, frame_->height()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6053 | } |
| 6054 | |
| 6055 | |
| 6056 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
| 6057 | #ifdef DEBUG |
| 6058 | int original_height = frame_->height(); |
| 6059 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6060 | Comment cmnt(masm_, "[ CompareOperation"); |
| 6061 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6062 | VirtualFrame::RegisterAllocationScope nonspilled_scope(this); |
| 6063 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6064 | // Get the expressions from the node. |
| 6065 | Expression* left = node->left(); |
| 6066 | Expression* right = node->right(); |
| 6067 | Token::Value op = node->op(); |
| 6068 | |
| 6069 | // To make null checks efficient, we check if either left or right is the |
| 6070 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 6071 | // instead of calling the (very) general runtime routine for checking |
| 6072 | // equality. |
| 6073 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
| 6074 | bool left_is_null = |
| 6075 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
| 6076 | bool right_is_null = |
| 6077 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 6078 | // The 'null' value can only be equal to 'null' or 'undefined'. |
| 6079 | if (left_is_null || right_is_null) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6080 | Load(left_is_null ? right : left); |
| 6081 | Register tos = frame_->PopToRegister(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6082 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6083 | __ cmp(tos, ip); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6084 | |
| 6085 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 6086 | // comparisons. |
| 6087 | if (op != Token::EQ_STRICT) { |
| 6088 | true_target()->Branch(eq); |
| 6089 | |
| 6090 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6091 | __ cmp(tos, Operand(ip)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6092 | true_target()->Branch(eq); |
| 6093 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6094 | __ tst(tos, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6095 | false_target()->Branch(eq); |
| 6096 | |
| 6097 | // It can be an undetectable object. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6098 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
| 6099 | __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 6100 | __ and_(tos, tos, Operand(1 << Map::kIsUndetectable)); |
| 6101 | __ cmp(tos, Operand(1 << Map::kIsUndetectable)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
| 6104 | cc_reg_ = eq; |
| 6105 | ASSERT(has_cc() && frame_->height() == original_height); |
| 6106 | return; |
| 6107 | } |
| 6108 | } |
| 6109 | |
| 6110 | // To make typeof testing for natives implemented in JavaScript really |
| 6111 | // efficient, we generate special code for expressions of the form: |
| 6112 | // 'typeof <expression> == <string>'. |
| 6113 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 6114 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 6115 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 6116 | (right->AsLiteral() != NULL && |
| 6117 | right->AsLiteral()->handle()->IsString())) { |
| 6118 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 6119 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6120 | // Load the operand, move it to a register. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6121 | LoadTypeofExpression(operation->expression()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6122 | Register tos = frame_->PopToRegister(); |
| 6123 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6124 | Register scratch = VirtualFrame::scratch0(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6125 | |
| 6126 | if (check->Equals(Heap::number_symbol())) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6127 | __ tst(tos, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6128 | true_target()->Branch(eq); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6129 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6130 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6131 | __ cmp(tos, ip); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6132 | cc_reg_ = eq; |
| 6133 | |
| 6134 | } else if (check->Equals(Heap::string_symbol())) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6135 | __ tst(tos, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6136 | false_target()->Branch(eq); |
| 6137 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6138 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6139 | |
| 6140 | // It can be an undetectable string object. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6141 | __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 6142 | __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable)); |
| 6143 | __ cmp(scratch, Operand(1 << Map::kIsUndetectable)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6144 | false_target()->Branch(eq); |
| 6145 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6146 | __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset)); |
| 6147 | __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6148 | cc_reg_ = lt; |
| 6149 | |
| 6150 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 6151 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6152 | __ cmp(tos, ip); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6153 | true_target()->Branch(eq); |
| 6154 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6155 | __ cmp(tos, ip); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6156 | cc_reg_ = eq; |
| 6157 | |
| 6158 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 6159 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6160 | __ cmp(tos, ip); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6161 | true_target()->Branch(eq); |
| 6162 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6163 | __ tst(tos, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6164 | false_target()->Branch(eq); |
| 6165 | |
| 6166 | // It can be an undetectable object. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6167 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
| 6168 | __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 6169 | __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable)); |
| 6170 | __ cmp(scratch, Operand(1 << Map::kIsUndetectable)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6171 | |
| 6172 | cc_reg_ = eq; |
| 6173 | |
| 6174 | } else if (check->Equals(Heap::function_symbol())) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6175 | __ tst(tos, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6176 | false_target()->Branch(eq); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6177 | Register map_reg = scratch; |
| 6178 | __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6179 | true_target()->Branch(eq); |
| 6180 | // Regular expressions are callable so typeof == 'function'. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6181 | __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6182 | cc_reg_ = eq; |
| 6183 | |
| 6184 | } else if (check->Equals(Heap::object_symbol())) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6185 | __ tst(tos, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6186 | false_target()->Branch(eq); |
| 6187 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6188 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6189 | __ cmp(tos, ip); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6190 | true_target()->Branch(eq); |
| 6191 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6192 | Register map_reg = scratch; |
| 6193 | __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6194 | false_target()->Branch(eq); |
| 6195 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6196 | // It can be an undetectable object. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6197 | __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 6198 | __ and_(tos, tos, Operand(1 << Map::kIsUndetectable)); |
| 6199 | __ cmp(tos, Operand(1 << Map::kIsUndetectable)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6200 | false_target()->Branch(eq); |
| 6201 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6202 | __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 6203 | __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6204 | false_target()->Branch(lt); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6205 | __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6206 | cc_reg_ = le; |
| 6207 | |
| 6208 | } else { |
| 6209 | // Uncommon case: typeof testing against a string literal that is |
| 6210 | // never returned from the typeof operator. |
| 6211 | false_target()->Jump(); |
| 6212 | } |
| 6213 | ASSERT(!has_valid_frame() || |
| 6214 | (has_cc() && frame_->height() == original_height)); |
| 6215 | return; |
| 6216 | } |
| 6217 | |
| 6218 | switch (op) { |
| 6219 | case Token::EQ: |
| 6220 | Comparison(eq, left, right, false); |
| 6221 | break; |
| 6222 | |
| 6223 | case Token::LT: |
| 6224 | Comparison(lt, left, right); |
| 6225 | break; |
| 6226 | |
| 6227 | case Token::GT: |
| 6228 | Comparison(gt, left, right); |
| 6229 | break; |
| 6230 | |
| 6231 | case Token::LTE: |
| 6232 | Comparison(le, left, right); |
| 6233 | break; |
| 6234 | |
| 6235 | case Token::GTE: |
| 6236 | Comparison(ge, left, right); |
| 6237 | break; |
| 6238 | |
| 6239 | case Token::EQ_STRICT: |
| 6240 | Comparison(eq, left, right, true); |
| 6241 | break; |
| 6242 | |
| 6243 | case Token::IN: { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6244 | Load(left); |
| 6245 | Load(right); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6246 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6247 | frame_->EmitPush(r0); |
| 6248 | break; |
| 6249 | } |
| 6250 | |
| 6251 | case Token::INSTANCEOF: { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6252 | Load(left); |
| 6253 | Load(right); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6254 | InstanceofStub stub; |
| 6255 | frame_->CallStub(&stub, 2); |
| 6256 | // At this point if instanceof succeeded then r0 == 0. |
| 6257 | __ tst(r0, Operand(r0)); |
| 6258 | cc_reg_ = eq; |
| 6259 | break; |
| 6260 | } |
| 6261 | |
| 6262 | default: |
| 6263 | UNREACHABLE(); |
| 6264 | } |
| 6265 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 6266 | (!has_cc() && frame_->height() == original_height + 1)); |
| 6267 | } |
| 6268 | |
| 6269 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6270 | class DeferredReferenceGetNamedValue: public DeferredCode { |
| 6271 | public: |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6272 | explicit DeferredReferenceGetNamedValue(Register receiver, |
| 6273 | Handle<String> name) |
| 6274 | : receiver_(receiver), name_(name) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6275 | set_comment("[ DeferredReferenceGetNamedValue"); |
| 6276 | } |
| 6277 | |
| 6278 | virtual void Generate(); |
| 6279 | |
| 6280 | private: |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6281 | Register receiver_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6282 | Handle<String> name_; |
| 6283 | }; |
| 6284 | |
| 6285 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6286 | // Convention for this is that on entry the receiver is in a register that |
| 6287 | // is not used by the stack. On exit the answer is found in that same |
| 6288 | // register and the stack has the same height. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6289 | void DeferredReferenceGetNamedValue::Generate() { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6290 | #ifdef DEBUG |
| 6291 | int expected_height = frame_state()->frame()->height(); |
| 6292 | #endif |
| 6293 | VirtualFrame copied_frame(*frame_state()->frame()); |
| 6294 | copied_frame.SpillAll(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6295 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6296 | Register scratch1 = VirtualFrame::scratch0(); |
| 6297 | Register scratch2 = VirtualFrame::scratch1(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6298 | ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6299 | __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2); |
| 6300 | __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2); |
| 6301 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6302 | // Ensure receiver in r0 and name in r2 to match load ic calling convention. |
| 6303 | __ Move(r0, receiver_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6304 | __ mov(r2, Operand(name_)); |
| 6305 | |
| 6306 | // The rest of the instructions in the deferred code must be together. |
| 6307 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 6308 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 6309 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 6310 | // The call must be followed by a nop(1) instruction to indicate that the |
| 6311 | // in-object has been inlined. |
| 6312 | __ nop(PROPERTY_ACCESS_INLINED); |
| 6313 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6314 | // At this point the answer is in r0. We move it to the expected register |
| 6315 | // if necessary. |
| 6316 | __ Move(receiver_, r0); |
| 6317 | |
| 6318 | // Now go back to the frame that we entered with. This will not overwrite |
| 6319 | // the receiver register since that register was not in use when we came |
| 6320 | // in. The instructions emitted by this merge are skipped over by the |
| 6321 | // inline load patching mechanism when looking for the branch instruction |
| 6322 | // that tells it where the code to patch is. |
| 6323 | copied_frame.MergeTo(frame_state()->frame()); |
| 6324 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6325 | // Block the constant pool for one more instruction after leaving this |
| 6326 | // constant pool block scope to include the branch instruction ending the |
| 6327 | // deferred code. |
| 6328 | __ BlockConstPoolFor(1); |
| 6329 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6330 | ASSERT_EQ(expected_height, frame_state()->frame()->height()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6331 | } |
| 6332 | |
| 6333 | |
| 6334 | class DeferredReferenceGetKeyedValue: public DeferredCode { |
| 6335 | public: |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6336 | DeferredReferenceGetKeyedValue(Register key, Register receiver) |
| 6337 | : key_(key), receiver_(receiver) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6338 | set_comment("[ DeferredReferenceGetKeyedValue"); |
| 6339 | } |
| 6340 | |
| 6341 | virtual void Generate(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6342 | |
| 6343 | private: |
| 6344 | Register key_; |
| 6345 | Register receiver_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6346 | }; |
| 6347 | |
| 6348 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6349 | // Takes key and register in r0 and r1 or vice versa. Returns result |
| 6350 | // in r0. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6351 | void DeferredReferenceGetKeyedValue::Generate() { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6352 | ASSERT((key_.is(r0) && receiver_.is(r1)) || |
| 6353 | (key_.is(r1) && receiver_.is(r0))); |
| 6354 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6355 | VirtualFrame copied_frame(*frame_state()->frame()); |
| 6356 | copied_frame.SpillAll(); |
| 6357 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6358 | Register scratch1 = VirtualFrame::scratch0(); |
| 6359 | Register scratch2 = VirtualFrame::scratch1(); |
| 6360 | __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2); |
| 6361 | __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2); |
| 6362 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6363 | // Ensure key in r0 and receiver in r1 to match keyed load ic calling |
| 6364 | // convention. |
| 6365 | if (key_.is(r1)) { |
| 6366 | __ Swap(r0, r1, ip); |
| 6367 | } |
| 6368 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6369 | // The rest of the instructions in the deferred code must be together. |
| 6370 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6371 | // Call keyed load IC. It has the arguments key and receiver in r0 and r1. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6372 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 6373 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 6374 | // The call must be followed by a nop instruction to indicate that the |
| 6375 | // keyed load has been inlined. |
| 6376 | __ nop(PROPERTY_ACCESS_INLINED); |
| 6377 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6378 | // Now go back to the frame that we entered with. This will not overwrite |
| 6379 | // the receiver or key registers since they were not in use when we came |
| 6380 | // in. The instructions emitted by this merge are skipped over by the |
| 6381 | // inline load patching mechanism when looking for the branch instruction |
| 6382 | // that tells it where the code to patch is. |
| 6383 | copied_frame.MergeTo(frame_state()->frame()); |
| 6384 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6385 | // Block the constant pool for one more instruction after leaving this |
| 6386 | // constant pool block scope to include the branch instruction ending the |
| 6387 | // deferred code. |
| 6388 | __ BlockConstPoolFor(1); |
| 6389 | } |
| 6390 | } |
| 6391 | |
| 6392 | |
| 6393 | class DeferredReferenceSetKeyedValue: public DeferredCode { |
| 6394 | public: |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6395 | DeferredReferenceSetKeyedValue(Register value, |
| 6396 | Register key, |
| 6397 | Register receiver) |
| 6398 | : value_(value), key_(key), receiver_(receiver) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6399 | set_comment("[ DeferredReferenceSetKeyedValue"); |
| 6400 | } |
| 6401 | |
| 6402 | virtual void Generate(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6403 | |
| 6404 | private: |
| 6405 | Register value_; |
| 6406 | Register key_; |
| 6407 | Register receiver_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6408 | }; |
| 6409 | |
| 6410 | |
| 6411 | void DeferredReferenceSetKeyedValue::Generate() { |
| 6412 | Register scratch1 = VirtualFrame::scratch0(); |
| 6413 | Register scratch2 = VirtualFrame::scratch1(); |
| 6414 | __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2); |
| 6415 | __ IncrementCounter( |
| 6416 | &Counters::keyed_store_inline_miss, 1, scratch1, scratch2); |
| 6417 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6418 | // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic |
| 6419 | // calling convention. |
| 6420 | if (value_.is(r1)) { |
| 6421 | __ Swap(r0, r1, ip); |
| 6422 | } |
| 6423 | ASSERT(receiver_.is(r2)); |
| 6424 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6425 | // The rest of the instructions in the deferred code must be together. |
| 6426 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6427 | // Call keyed store IC. It has the arguments value, key and receiver in r0, |
| 6428 | // r1 and r2. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6429 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 6430 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 6431 | // The call must be followed by a nop instruction to indicate that the |
| 6432 | // keyed store has been inlined. |
| 6433 | __ nop(PROPERTY_ACCESS_INLINED); |
| 6434 | |
| 6435 | // Block the constant pool for one more instruction after leaving this |
| 6436 | // constant pool block scope to include the branch instruction ending the |
| 6437 | // deferred code. |
| 6438 | __ BlockConstPoolFor(1); |
| 6439 | } |
| 6440 | } |
| 6441 | |
| 6442 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 6443 | class DeferredReferenceSetNamedValue: public DeferredCode { |
| 6444 | public: |
| 6445 | DeferredReferenceSetNamedValue(Register value, |
| 6446 | Register receiver, |
| 6447 | Handle<String> name) |
| 6448 | : value_(value), receiver_(receiver), name_(name) { |
| 6449 | set_comment("[ DeferredReferenceSetNamedValue"); |
| 6450 | } |
| 6451 | |
| 6452 | virtual void Generate(); |
| 6453 | |
| 6454 | private: |
| 6455 | Register value_; |
| 6456 | Register receiver_; |
| 6457 | Handle<String> name_; |
| 6458 | }; |
| 6459 | |
| 6460 | |
| 6461 | // Takes value in r0, receiver in r1 and returns the result (the |
| 6462 | // value) in r0. |
| 6463 | void DeferredReferenceSetNamedValue::Generate() { |
| 6464 | // Record the entry frame and spill. |
| 6465 | VirtualFrame copied_frame(*frame_state()->frame()); |
| 6466 | copied_frame.SpillAll(); |
| 6467 | |
| 6468 | // Ensure value in r0, receiver in r1 to match store ic calling |
| 6469 | // convention. |
| 6470 | ASSERT(value_.is(r0) && receiver_.is(r1)); |
| 6471 | __ mov(r2, Operand(name_)); |
| 6472 | |
| 6473 | // The rest of the instructions in the deferred code must be together. |
| 6474 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 6475 | // Call keyed store IC. It has the arguments value, key and receiver in r0, |
| 6476 | // r1 and r2. |
| 6477 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 6478 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 6479 | // The call must be followed by a nop instruction to indicate that the |
| 6480 | // named store has been inlined. |
| 6481 | __ nop(PROPERTY_ACCESS_INLINED); |
| 6482 | |
| 6483 | // Go back to the frame we entered with. The instructions |
| 6484 | // generated by this merge are skipped over by the inline store |
| 6485 | // patching mechanism when looking for the branch instruction that |
| 6486 | // tells it where the code to patch is. |
| 6487 | copied_frame.MergeTo(frame_state()->frame()); |
| 6488 | |
| 6489 | // Block the constant pool for one more instruction after leaving this |
| 6490 | // constant pool block scope to include the branch instruction ending the |
| 6491 | // deferred code. |
| 6492 | __ BlockConstPoolFor(1); |
| 6493 | } |
| 6494 | } |
| 6495 | |
| 6496 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6497 | // Consumes the top of stack (the receiver) and pushes the result instead. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6498 | void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) { |
| 6499 | if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) { |
| 6500 | Comment cmnt(masm(), "[ Load from named Property"); |
| 6501 | // Setup the name register and call load IC. |
| 6502 | frame_->CallLoadIC(name, |
| 6503 | is_contextual |
| 6504 | ? RelocInfo::CODE_TARGET_CONTEXT |
| 6505 | : RelocInfo::CODE_TARGET); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6506 | frame_->EmitPush(r0); // Push answer. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6507 | } else { |
| 6508 | // Inline the in-object property case. |
| 6509 | Comment cmnt(masm(), "[ Inlined named property load"); |
| 6510 | |
| 6511 | // Counter will be decremented in the deferred code. Placed here to avoid |
| 6512 | // having it in the instruction stream below where patching will occur. |
| 6513 | __ IncrementCounter(&Counters::named_load_inline, 1, |
| 6514 | frame_->scratch0(), frame_->scratch1()); |
| 6515 | |
| 6516 | // The following instructions are the inlined load of an in-object property. |
| 6517 | // Parts of this code is patched, so the exact instructions generated needs |
| 6518 | // to be fixed. Therefore the instruction pool is blocked when generating |
| 6519 | // this code |
| 6520 | |
| 6521 | // Load the receiver from the stack. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6522 | Register receiver = frame_->PopToRegister(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6523 | |
| 6524 | DeferredReferenceGetNamedValue* deferred = |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6525 | new DeferredReferenceGetNamedValue(receiver, name); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6526 | |
| 6527 | #ifdef DEBUG |
| 6528 | int kInlinedNamedLoadInstructions = 7; |
| 6529 | Label check_inlined_codesize; |
| 6530 | masm_->bind(&check_inlined_codesize); |
| 6531 | #endif |
| 6532 | |
| 6533 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 6534 | // Check that the receiver is a heap object. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6535 | __ tst(receiver, Operand(kSmiTagMask)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6536 | deferred->Branch(eq); |
| 6537 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6538 | Register scratch = VirtualFrame::scratch0(); |
| 6539 | Register scratch2 = VirtualFrame::scratch1(); |
| 6540 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6541 | // Check the map. The null map used below is patched by the inline cache |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6542 | // code. Therefore we can't use a LoadRoot call. |
| 6543 | __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
| 6544 | __ mov(scratch2, Operand(Factory::null_value())); |
| 6545 | __ cmp(scratch, scratch2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6546 | deferred->Branch(ne); |
| 6547 | |
| 6548 | // Initially use an invalid index. The index will be patched by the |
| 6549 | // inline cache code. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6550 | __ ldr(receiver, MemOperand(receiver, 0)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6551 | |
| 6552 | // Make sure that the expected number of instructions are generated. |
| 6553 | ASSERT_EQ(kInlinedNamedLoadInstructions, |
| 6554 | masm_->InstructionsGeneratedSince(&check_inlined_codesize)); |
| 6555 | } |
| 6556 | |
| 6557 | deferred->BindExit(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6558 | // At this point the receiver register has the result, either from the |
| 6559 | // deferred code or from the inlined code. |
| 6560 | frame_->EmitPush(receiver); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6561 | } |
| 6562 | } |
| 6563 | |
| 6564 | |
| 6565 | void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) { |
| 6566 | #ifdef DEBUG |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 6567 | int expected_height = frame()->height() - (is_contextual ? 1 : 2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6568 | #endif |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6569 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 6570 | Result result; |
| 6571 | if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) { |
| 6572 | frame()->CallStoreIC(name, is_contextual); |
| 6573 | } else { |
| 6574 | // Inline the in-object property case. |
| 6575 | JumpTarget slow, done; |
| 6576 | |
| 6577 | // Get the value and receiver from the stack. |
| 6578 | frame()->PopToR0(); |
| 6579 | Register value = r0; |
| 6580 | frame()->PopToR1(); |
| 6581 | Register receiver = r1; |
| 6582 | |
| 6583 | DeferredReferenceSetNamedValue* deferred = |
| 6584 | new DeferredReferenceSetNamedValue(value, receiver, name); |
| 6585 | |
| 6586 | // Check that the receiver is a heap object. |
| 6587 | __ tst(receiver, Operand(kSmiTagMask)); |
| 6588 | deferred->Branch(eq); |
| 6589 | |
| 6590 | // The following instructions are the part of the inlined |
| 6591 | // in-object property store code which can be patched. Therefore |
| 6592 | // the exact number of instructions generated must be fixed, so |
| 6593 | // the constant pool is blocked while generating this code. |
| 6594 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 6595 | Register scratch0 = VirtualFrame::scratch0(); |
| 6596 | Register scratch1 = VirtualFrame::scratch1(); |
| 6597 | |
| 6598 | // Check the map. Initially use an invalid map to force a |
| 6599 | // failure. The map check will be patched in the runtime system. |
| 6600 | __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
| 6601 | |
| 6602 | #ifdef DEBUG |
| 6603 | Label check_inlined_codesize; |
| 6604 | masm_->bind(&check_inlined_codesize); |
| 6605 | #endif |
| 6606 | __ mov(scratch0, Operand(Factory::null_value())); |
| 6607 | __ cmp(scratch0, scratch1); |
| 6608 | deferred->Branch(ne); |
| 6609 | |
| 6610 | int offset = 0; |
| 6611 | __ str(value, MemOperand(receiver, offset)); |
| 6612 | |
| 6613 | // Update the write barrier and record its size. We do not use |
| 6614 | // the RecordWrite macro here because we want the offset |
| 6615 | // addition instruction first to make it easy to patch. |
| 6616 | Label record_write_start, record_write_done; |
| 6617 | __ bind(&record_write_start); |
| 6618 | // Add offset into the object. |
| 6619 | __ add(scratch0, receiver, Operand(offset)); |
| 6620 | // Test that the object is not in the new space. We cannot set |
| 6621 | // region marks for new space pages. |
| 6622 | __ InNewSpace(receiver, scratch1, eq, &record_write_done); |
| 6623 | // Record the actual write. |
| 6624 | __ RecordWriteHelper(receiver, scratch0, scratch1); |
| 6625 | __ bind(&record_write_done); |
| 6626 | // Clobber all input registers when running with the debug-code flag |
| 6627 | // turned on to provoke errors. |
| 6628 | if (FLAG_debug_code) { |
| 6629 | __ mov(receiver, Operand(BitCast<int32_t>(kZapValue))); |
| 6630 | __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue))); |
| 6631 | __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue))); |
| 6632 | } |
| 6633 | // Check that this is the first inlined write barrier or that |
| 6634 | // this inlined write barrier has the same size as all the other |
| 6635 | // inlined write barriers. |
| 6636 | ASSERT((inlined_write_barrier_size_ == -1) || |
| 6637 | (inlined_write_barrier_size_ == |
| 6638 | masm()->InstructionsGeneratedSince(&record_write_start))); |
| 6639 | inlined_write_barrier_size_ = |
| 6640 | masm()->InstructionsGeneratedSince(&record_write_start); |
| 6641 | |
| 6642 | // Make sure that the expected number of instructions are generated. |
| 6643 | ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(), |
| 6644 | masm()->InstructionsGeneratedSince(&check_inlined_codesize)); |
| 6645 | } |
| 6646 | deferred->BindExit(); |
| 6647 | } |
| 6648 | ASSERT_EQ(expected_height, frame()->height()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6649 | } |
| 6650 | |
| 6651 | |
| 6652 | void CodeGenerator::EmitKeyedLoad() { |
| 6653 | if (loop_nesting() == 0) { |
| 6654 | Comment cmnt(masm_, "[ Load from keyed property"); |
| 6655 | frame_->CallKeyedLoadIC(); |
| 6656 | } else { |
| 6657 | // Inline the keyed load. |
| 6658 | Comment cmnt(masm_, "[ Inlined load from keyed property"); |
| 6659 | |
| 6660 | // Counter will be decremented in the deferred code. Placed here to avoid |
| 6661 | // having it in the instruction stream below where patching will occur. |
| 6662 | __ IncrementCounter(&Counters::keyed_load_inline, 1, |
| 6663 | frame_->scratch0(), frame_->scratch1()); |
| 6664 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6665 | // Load the key and receiver from the stack. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6666 | bool key_is_known_smi = frame_->KnownSmiAt(0); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6667 | Register key = frame_->PopToRegister(); |
| 6668 | Register receiver = frame_->PopToRegister(key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6669 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6670 | // The deferred code expects key and receiver in registers. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6671 | DeferredReferenceGetKeyedValue* deferred = |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6672 | new DeferredReferenceGetKeyedValue(key, receiver); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6673 | |
| 6674 | // Check that the receiver is a heap object. |
| 6675 | __ tst(receiver, Operand(kSmiTagMask)); |
| 6676 | deferred->Branch(eq); |
| 6677 | |
| 6678 | // The following instructions are the part of the inlined load keyed |
| 6679 | // property code which can be patched. Therefore the exact number of |
| 6680 | // instructions generated need to be fixed, so the constant pool is blocked |
| 6681 | // while generating this code. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6682 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 6683 | Register scratch1 = VirtualFrame::scratch0(); |
| 6684 | Register scratch2 = VirtualFrame::scratch1(); |
| 6685 | // Check the map. The null map used below is patched by the inline cache |
| 6686 | // code. |
| 6687 | __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6688 | |
| 6689 | // Check that the key is a smi. |
| 6690 | if (!key_is_known_smi) { |
| 6691 | __ tst(key, Operand(kSmiTagMask)); |
| 6692 | deferred->Branch(ne); |
| 6693 | } |
| 6694 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6695 | #ifdef DEBUG |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6696 | Label check_inlined_codesize; |
| 6697 | masm_->bind(&check_inlined_codesize); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6698 | #endif |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6699 | __ mov(scratch2, Operand(Factory::null_value())); |
| 6700 | __ cmp(scratch1, scratch2); |
| 6701 | deferred->Branch(ne); |
| 6702 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 6703 | // Get the elements array from the receiver. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6704 | __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 6705 | __ AssertFastElements(scratch1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6706 | |
| 6707 | // Check that key is within bounds. Use unsigned comparison to handle |
| 6708 | // negative keys. |
| 6709 | __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6710 | __ cmp(scratch2, key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6711 | deferred->Branch(ls); // Unsigned less equal. |
| 6712 | |
| 6713 | // Load and check that the result is not the hole (key is a smi). |
| 6714 | __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex); |
| 6715 | __ add(scratch1, |
| 6716 | scratch1, |
| 6717 | Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6718 | __ ldr(scratch1, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6719 | MemOperand(scratch1, key, LSL, |
| 6720 | kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize))); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6721 | __ cmp(scratch1, scratch2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6722 | deferred->Branch(eq); |
| 6723 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 6724 | __ mov(r0, scratch1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6725 | // Make sure that the expected number of instructions are generated. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6726 | ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6727 | masm_->InstructionsGeneratedSince(&check_inlined_codesize)); |
| 6728 | } |
| 6729 | |
| 6730 | deferred->BindExit(); |
| 6731 | } |
| 6732 | } |
| 6733 | |
| 6734 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6735 | void CodeGenerator::EmitKeyedStore(StaticType* key_type, |
| 6736 | WriteBarrierCharacter wb_info) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6737 | // Generate inlined version of the keyed store if the code is in a loop |
| 6738 | // and the key is likely to be a smi. |
| 6739 | if (loop_nesting() > 0 && key_type->IsLikelySmi()) { |
| 6740 | // Inline the keyed store. |
| 6741 | Comment cmnt(masm_, "[ Inlined store to keyed property"); |
| 6742 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6743 | Register scratch1 = VirtualFrame::scratch0(); |
| 6744 | Register scratch2 = VirtualFrame::scratch1(); |
| 6745 | Register scratch3 = r3; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6746 | |
| 6747 | // Counter will be decremented in the deferred code. Placed here to avoid |
| 6748 | // having it in the instruction stream below where patching will occur. |
| 6749 | __ IncrementCounter(&Counters::keyed_store_inline, 1, |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6750 | scratch1, scratch2); |
| 6751 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6752 | |
| 6753 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6754 | // Load the value, key and receiver from the stack. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6755 | bool value_is_harmless = frame_->KnownSmiAt(0); |
| 6756 | if (wb_info == NEVER_NEWSPACE) value_is_harmless = true; |
| 6757 | bool key_is_smi = frame_->KnownSmiAt(1); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6758 | Register value = frame_->PopToRegister(); |
| 6759 | Register key = frame_->PopToRegister(value); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6760 | VirtualFrame::SpilledScope spilled(frame_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6761 | Register receiver = r2; |
| 6762 | frame_->EmitPop(receiver); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6763 | |
| 6764 | #ifdef DEBUG |
| 6765 | bool we_remembered_the_write_barrier = value_is_harmless; |
| 6766 | #endif |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6767 | |
| 6768 | // The deferred code expects value, key and receiver in registers. |
| 6769 | DeferredReferenceSetKeyedValue* deferred = |
| 6770 | new DeferredReferenceSetKeyedValue(value, key, receiver); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6771 | |
| 6772 | // Check that the value is a smi. As this inlined code does not set the |
| 6773 | // write barrier it is only possible to store smi values. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6774 | if (!value_is_harmless) { |
| 6775 | // If the value is not likely to be a Smi then let's test the fixed array |
| 6776 | // for new space instead. See below. |
| 6777 | if (wb_info == LIKELY_SMI) { |
| 6778 | __ tst(value, Operand(kSmiTagMask)); |
| 6779 | deferred->Branch(ne); |
| 6780 | #ifdef DEBUG |
| 6781 | we_remembered_the_write_barrier = true; |
| 6782 | #endif |
| 6783 | } |
| 6784 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6785 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6786 | if (!key_is_smi) { |
| 6787 | // Check that the key is a smi. |
| 6788 | __ tst(key, Operand(kSmiTagMask)); |
| 6789 | deferred->Branch(ne); |
| 6790 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6791 | |
| 6792 | // Check that the receiver is a heap object. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6793 | __ tst(receiver, Operand(kSmiTagMask)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6794 | deferred->Branch(eq); |
| 6795 | |
| 6796 | // Check that the receiver is a JSArray. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6797 | __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6798 | deferred->Branch(ne); |
| 6799 | |
| 6800 | // Check that the key is within bounds. Both the key and the length of |
| 6801 | // the JSArray are smis. Use unsigned comparison to handle negative keys. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6802 | __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset)); |
| 6803 | __ cmp(scratch1, key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6804 | deferred->Branch(ls); // Unsigned less equal. |
| 6805 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6806 | // Get the elements array from the receiver. |
| 6807 | __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset)); |
| 6808 | if (!value_is_harmless && wb_info != LIKELY_SMI) { |
| 6809 | Label ok; |
| 6810 | __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask())); |
| 6811 | __ cmp(scratch2, Operand(ExternalReference::new_space_start())); |
| 6812 | __ tst(value, Operand(kSmiTagMask), ne); |
| 6813 | deferred->Branch(ne); |
| 6814 | #ifdef DEBUG |
| 6815 | we_remembered_the_write_barrier = true; |
| 6816 | #endif |
| 6817 | } |
| 6818 | // Check that the elements array is not a dictionary. |
| 6819 | __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6820 | // The following instructions are the part of the inlined store keyed |
| 6821 | // property code which can be patched. Therefore the exact number of |
| 6822 | // instructions generated need to be fixed, so the constant pool is blocked |
| 6823 | // while generating this code. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6824 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6825 | #ifdef DEBUG |
| 6826 | Label check_inlined_codesize; |
| 6827 | masm_->bind(&check_inlined_codesize); |
| 6828 | #endif |
| 6829 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6830 | // Read the fixed array map from the constant pool (not from the root |
| 6831 | // array) so that the value can be patched. When debugging, we patch this |
| 6832 | // comparison to always fail so that we will hit the IC call in the |
| 6833 | // deferred code which will allow the debugger to break for fast case |
| 6834 | // stores. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6835 | __ mov(scratch3, Operand(Factory::fixed_array_map())); |
| 6836 | __ cmp(scratch2, scratch3); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6837 | deferred->Branch(ne); |
| 6838 | |
| 6839 | // Store the value. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6840 | __ add(scratch1, scratch1, |
| 6841 | Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 6842 | __ str(value, |
| 6843 | MemOperand(scratch1, key, LSL, |
| 6844 | kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6845 | |
| 6846 | // Make sure that the expected number of instructions are generated. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6847 | ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6848 | masm_->InstructionsGeneratedSince(&check_inlined_codesize)); |
| 6849 | } |
| 6850 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6851 | ASSERT(we_remembered_the_write_barrier); |
| 6852 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6853 | deferred->BindExit(); |
| 6854 | } else { |
| 6855 | frame()->CallKeyedStoreIC(); |
| 6856 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6857 | } |
| 6858 | |
| 6859 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6860 | #ifdef DEBUG |
| 6861 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 6862 | #endif |
| 6863 | |
| 6864 | |
| 6865 | #undef __ |
| 6866 | #define __ ACCESS_MASM(masm) |
| 6867 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6868 | Handle<String> Reference::GetName() { |
| 6869 | ASSERT(type_ == NAMED); |
| 6870 | Property* property = expression_->AsProperty(); |
| 6871 | if (property == NULL) { |
| 6872 | // Global variable reference treated as a named property reference. |
| 6873 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 6874 | ASSERT(proxy->AsVariable() != NULL); |
| 6875 | ASSERT(proxy->AsVariable()->is_global()); |
| 6876 | return proxy->name(); |
| 6877 | } else { |
| 6878 | Literal* raw_name = property->key()->AsLiteral(); |
| 6879 | ASSERT(raw_name != NULL); |
| 6880 | return Handle<String>(String::cast(*raw_name->handle())); |
| 6881 | } |
| 6882 | } |
| 6883 | |
| 6884 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6885 | void Reference::DupIfPersist() { |
| 6886 | if (persist_after_get_) { |
| 6887 | switch (type_) { |
| 6888 | case KEYED: |
| 6889 | cgen_->frame()->Dup2(); |
| 6890 | break; |
| 6891 | case NAMED: |
| 6892 | cgen_->frame()->Dup(); |
| 6893 | // Fall through. |
| 6894 | case UNLOADED: |
| 6895 | case ILLEGAL: |
| 6896 | case SLOT: |
| 6897 | // Do nothing. |
| 6898 | ; |
| 6899 | } |
| 6900 | } else { |
| 6901 | set_unloaded(); |
| 6902 | } |
| 6903 | } |
| 6904 | |
| 6905 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6906 | void Reference::GetValue() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6907 | ASSERT(cgen_->HasValidEntryRegisters()); |
| 6908 | ASSERT(!is_illegal()); |
| 6909 | ASSERT(!cgen_->has_cc()); |
| 6910 | MacroAssembler* masm = cgen_->masm(); |
| 6911 | Property* property = expression_->AsProperty(); |
| 6912 | if (property != NULL) { |
| 6913 | cgen_->CodeForSourcePosition(property->position()); |
| 6914 | } |
| 6915 | |
| 6916 | switch (type_) { |
| 6917 | case SLOT: { |
| 6918 | Comment cmnt(masm, "[ Load from Slot"); |
| 6919 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 6920 | ASSERT(slot != NULL); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6921 | DupIfPersist(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6922 | cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6923 | break; |
| 6924 | } |
| 6925 | |
| 6926 | case NAMED: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6927 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6928 | bool is_global = var != NULL; |
| 6929 | ASSERT(!is_global || var->is_global()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6930 | Handle<String> name = GetName(); |
| 6931 | DupIfPersist(); |
| 6932 | cgen_->EmitNamedLoad(name, is_global); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6933 | break; |
| 6934 | } |
| 6935 | |
| 6936 | case KEYED: { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6937 | ASSERT(property != NULL); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6938 | DupIfPersist(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6939 | cgen_->EmitKeyedLoad(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6940 | cgen_->frame()->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6941 | break; |
| 6942 | } |
| 6943 | |
| 6944 | default: |
| 6945 | UNREACHABLE(); |
| 6946 | } |
| 6947 | } |
| 6948 | |
| 6949 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6950 | void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6951 | ASSERT(!is_illegal()); |
| 6952 | ASSERT(!cgen_->has_cc()); |
| 6953 | MacroAssembler* masm = cgen_->masm(); |
| 6954 | VirtualFrame* frame = cgen_->frame(); |
| 6955 | Property* property = expression_->AsProperty(); |
| 6956 | if (property != NULL) { |
| 6957 | cgen_->CodeForSourcePosition(property->position()); |
| 6958 | } |
| 6959 | |
| 6960 | switch (type_) { |
| 6961 | case SLOT: { |
| 6962 | Comment cmnt(masm, "[ Store to Slot"); |
| 6963 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6964 | cgen_->StoreToSlot(slot, init_state); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6965 | set_unloaded(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6966 | break; |
| 6967 | } |
| 6968 | |
| 6969 | case NAMED: { |
| 6970 | Comment cmnt(masm, "[ Store to named Property"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6971 | cgen_->EmitNamedStore(GetName(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6972 | frame->EmitPush(r0); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6973 | set_unloaded(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6974 | break; |
| 6975 | } |
| 6976 | |
| 6977 | case KEYED: { |
| 6978 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 6979 | Property* property = expression_->AsProperty(); |
| 6980 | ASSERT(property != NULL); |
| 6981 | cgen_->CodeForSourcePosition(property->position()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6982 | cgen_->EmitKeyedStore(property->key()->type(), wb_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6983 | frame->EmitPush(r0); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6984 | set_unloaded(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6985 | break; |
| 6986 | } |
| 6987 | |
| 6988 | default: |
| 6989 | UNREACHABLE(); |
| 6990 | } |
| 6991 | } |
| 6992 | |
| 6993 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6994 | void FastNewClosureStub::Generate(MacroAssembler* masm) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6995 | // Create a new closure from the given function info in new |
| 6996 | // space. Set the context to the current context in cp. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6997 | Label gc; |
| 6998 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6999 | // Pop the function info from the stack. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7000 | __ pop(r3); |
| 7001 | |
| 7002 | // Attempt to allocate new JSFunction in new space. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 7003 | __ AllocateInNewSpace(JSFunction::kSize, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7004 | r0, |
| 7005 | r1, |
| 7006 | r2, |
| 7007 | &gc, |
| 7008 | TAG_OBJECT); |
| 7009 | |
| 7010 | // Compute the function map in the current global context and set that |
| 7011 | // as the map of the allocated object. |
| 7012 | __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 7013 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 7014 | __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX))); |
| 7015 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7016 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7017 | // Initialize the rest of the function. We don't have to update the |
| 7018 | // write barrier because the allocated object is in new space. |
| 7019 | __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex); |
| 7020 | __ LoadRoot(r2, Heap::kTheHoleValueRootIndex); |
| 7021 | __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset)); |
| 7022 | __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 7023 | __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset)); |
| 7024 | __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 7025 | __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset)); |
| 7026 | __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7027 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 7028 | // Initialize the code pointer in the function to be the one |
| 7029 | // found in the shared function info object. |
| 7030 | __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kCodeOffset)); |
| 7031 | __ str(r3, FieldMemOperand(r0, JSFunction::kCodeOffset)); |
| 7032 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7033 | // Return result. The argument function info has been popped already. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7034 | __ Ret(); |
| 7035 | |
| 7036 | // Create a new closure through the slower runtime call. |
| 7037 | __ bind(&gc); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7038 | __ Push(cp, r3); |
| 7039 | __ TailCallRuntime(Runtime::kNewClosure, 2, 1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | |
| 7043 | void FastNewContextStub::Generate(MacroAssembler* masm) { |
| 7044 | // Try to allocate the context in new space. |
| 7045 | Label gc; |
| 7046 | int length = slots_ + Context::MIN_CONTEXT_SLOTS; |
| 7047 | |
| 7048 | // Attempt to allocate the context in new space. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 7049 | __ AllocateInNewSpace(FixedArray::SizeFor(length), |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7050 | r0, |
| 7051 | r1, |
| 7052 | r2, |
| 7053 | &gc, |
| 7054 | TAG_OBJECT); |
| 7055 | |
| 7056 | // Load the function from the stack. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7057 | __ ldr(r3, MemOperand(sp, 0)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7058 | |
| 7059 | // Setup the object header. |
| 7060 | __ LoadRoot(r2, Heap::kContextMapRootIndex); |
| 7061 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7062 | __ mov(r2, Operand(Smi::FromInt(length))); |
| 7063 | __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7064 | |
| 7065 | // Setup the fixed slots. |
| 7066 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 7067 | __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 7068 | __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 7069 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
| 7070 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX))); |
| 7071 | |
| 7072 | // Copy the global object from the surrounding context. |
| 7073 | __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 7074 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 7075 | |
| 7076 | // Initialize the rest of the slots to undefined. |
| 7077 | __ LoadRoot(r1, Heap::kUndefinedValueRootIndex); |
| 7078 | for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) { |
| 7079 | __ str(r1, MemOperand(r0, Context::SlotOffset(i))); |
| 7080 | } |
| 7081 | |
| 7082 | // Remove the on-stack argument and return. |
| 7083 | __ mov(cp, r0); |
| 7084 | __ pop(); |
| 7085 | __ Ret(); |
| 7086 | |
| 7087 | // Need to collect. Call into runtime system. |
| 7088 | __ bind(&gc); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7089 | __ TailCallRuntime(Runtime::kNewContext, 1, 1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7090 | } |
| 7091 | |
| 7092 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7093 | void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) { |
| 7094 | // Stack layout on entry: |
| 7095 | // |
| 7096 | // [sp]: constant elements. |
| 7097 | // [sp + kPointerSize]: literal index. |
| 7098 | // [sp + (2 * kPointerSize)]: literals array. |
| 7099 | |
| 7100 | // All sizes here are multiples of kPointerSize. |
| 7101 | int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0; |
| 7102 | int size = JSArray::kSize + elements_size; |
| 7103 | |
| 7104 | // Load boilerplate object into r3 and check if we need to create a |
| 7105 | // boilerplate. |
| 7106 | Label slow_case; |
| 7107 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 7108 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 7109 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 7110 | __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 7111 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 7112 | __ cmp(r3, ip); |
| 7113 | __ b(eq, &slow_case); |
| 7114 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 7115 | if (FLAG_debug_code) { |
| 7116 | const char* message; |
| 7117 | Heap::RootListIndex expected_map_index; |
| 7118 | if (mode_ == CLONE_ELEMENTS) { |
| 7119 | message = "Expected (writable) fixed array"; |
| 7120 | expected_map_index = Heap::kFixedArrayMapRootIndex; |
| 7121 | } else { |
| 7122 | ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS); |
| 7123 | message = "Expected copy-on-write fixed array"; |
| 7124 | expected_map_index = Heap::kFixedCOWArrayMapRootIndex; |
| 7125 | } |
| 7126 | __ push(r3); |
| 7127 | __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset)); |
| 7128 | __ ldr(r3, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 7129 | __ LoadRoot(ip, expected_map_index); |
| 7130 | __ cmp(r3, ip); |
| 7131 | __ Assert(eq, message); |
| 7132 | __ pop(r3); |
| 7133 | } |
| 7134 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7135 | // Allocate both the JS array and the elements array in one big |
| 7136 | // allocation. This avoids multiple limit checks. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 7137 | __ AllocateInNewSpace(size, |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7138 | r0, |
| 7139 | r1, |
| 7140 | r2, |
| 7141 | &slow_case, |
| 7142 | TAG_OBJECT); |
| 7143 | |
| 7144 | // Copy the JS array part. |
| 7145 | for (int i = 0; i < JSArray::kSize; i += kPointerSize) { |
| 7146 | if ((i != JSArray::kElementsOffset) || (length_ == 0)) { |
| 7147 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 7148 | __ str(r1, FieldMemOperand(r0, i)); |
| 7149 | } |
| 7150 | } |
| 7151 | |
| 7152 | if (length_ > 0) { |
| 7153 | // Get hold of the elements array of the boilerplate and setup the |
| 7154 | // elements pointer in the resulting object. |
| 7155 | __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset)); |
| 7156 | __ add(r2, r0, Operand(JSArray::kSize)); |
| 7157 | __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 7158 | |
| 7159 | // Copy the elements array. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 7160 | __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7161 | } |
| 7162 | |
| 7163 | // Return and remove the on-stack parameters. |
| 7164 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7165 | __ Ret(); |
| 7166 | |
| 7167 | __ bind(&slow_case); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7168 | __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
| 7171 | |
| 7172 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 7173 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 7174 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 7175 | // scratch register. Destroys the source register. No GC occurs during this |
| 7176 | // stub so you don't have to set up the frame. |
| 7177 | class ConvertToDoubleStub : public CodeStub { |
| 7178 | public: |
| 7179 | ConvertToDoubleStub(Register result_reg_1, |
| 7180 | Register result_reg_2, |
| 7181 | Register source_reg, |
| 7182 | Register scratch_reg) |
| 7183 | : result1_(result_reg_1), |
| 7184 | result2_(result_reg_2), |
| 7185 | source_(source_reg), |
| 7186 | zeros_(scratch_reg) { } |
| 7187 | |
| 7188 | private: |
| 7189 | Register result1_; |
| 7190 | Register result2_; |
| 7191 | Register source_; |
| 7192 | Register zeros_; |
| 7193 | |
| 7194 | // Minor key encoding in 16 bits. |
| 7195 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 7196 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 7197 | |
| 7198 | Major MajorKey() { return ConvertToDouble; } |
| 7199 | int MinorKey() { |
| 7200 | // Encode the parameters in a unique 16 bit value. |
| 7201 | return result1_.code() + |
| 7202 | (result2_.code() << 4) + |
| 7203 | (source_.code() << 8) + |
| 7204 | (zeros_.code() << 12); |
| 7205 | } |
| 7206 | |
| 7207 | void Generate(MacroAssembler* masm); |
| 7208 | |
| 7209 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 7210 | |
| 7211 | #ifdef DEBUG |
| 7212 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 7213 | #endif |
| 7214 | }; |
| 7215 | |
| 7216 | |
| 7217 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 7218 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 7219 | Register exponent = result1_; |
| 7220 | Register mantissa = result2_; |
| 7221 | #else |
| 7222 | Register exponent = result2_; |
| 7223 | Register mantissa = result1_; |
| 7224 | #endif |
| 7225 | Label not_special; |
| 7226 | // Convert from Smi to integer. |
| 7227 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 7228 | // Move sign bit from source to destination. This works because the sign bit |
| 7229 | // in the exponent word of the double has the same position and polarity as |
| 7230 | // the 2's complement sign bit in a Smi. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7231 | STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7232 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 7233 | // Subtract from 0 if source was negative. |
| 7234 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7235 | |
| 7236 | // We have -1, 0 or 1, which we treat specially. Register source_ contains |
| 7237 | // absolute value: it is either equal to 1 (special case of -1 and 1), |
| 7238 | // greater than 1 (not a special case) or less than 1 (special case of 0). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7239 | __ cmp(source_, Operand(1)); |
| 7240 | __ b(gt, ¬_special); |
| 7241 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7242 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 7243 | static const uint32_t exponent_word_for_1 = |
| 7244 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7245 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7246 | // 1, 0 and -1 all have 0 for the second word. |
| 7247 | __ mov(mantissa, Operand(0)); |
| 7248 | __ Ret(); |
| 7249 | |
| 7250 | __ bind(¬_special); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7251 | // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7252 | // Gets the wrong answer for 0, but we already checked for that case above. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7253 | __ CountLeadingZeros(zeros_, source_, mantissa); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7254 | // Compute exponent and or it into the exponent register. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 7255 | // We use mantissa as a scratch register here. Use a fudge factor to |
| 7256 | // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts |
| 7257 | // that fit in the ARM's constant field. |
| 7258 | int fudge = 0x400; |
| 7259 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge)); |
| 7260 | __ add(mantissa, mantissa, Operand(fudge)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7261 | __ orr(exponent, |
| 7262 | exponent, |
| 7263 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 7264 | // Shift up the source chopping the top bit off. |
| 7265 | __ add(zeros_, zeros_, Operand(1)); |
| 7266 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 7267 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 7268 | // Compute lower part of fraction (last 12 bits). |
| 7269 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 7270 | // And the top (top 20 bits). |
| 7271 | __ orr(exponent, |
| 7272 | exponent, |
| 7273 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 7274 | __ Ret(); |
| 7275 | } |
| 7276 | |
| 7277 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7278 | // See comment for class. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7279 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7280 | Label max_negative_int; |
| 7281 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 7282 | // We test for the special value that has a different exponent. This test |
| 7283 | // has the neat side effect of setting the flags according to the sign. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7284 | STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7285 | __ cmp(the_int_, Operand(0x80000000u)); |
| 7286 | __ b(eq, &max_negative_int); |
| 7287 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 7288 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 7289 | uint32_t non_smi_exponent = |
| 7290 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 7291 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 7292 | // Set the sign bit in scratch_ if the value was negative. |
| 7293 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 7294 | // Subtract from 0 if the value was negative. |
| 7295 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 7296 | // We should be masking the implict first digit of the mantissa away here, |
| 7297 | // but it just ends up combining harmlessly with the last digit of the |
| 7298 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 7299 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 7300 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 7301 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 7302 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 7303 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 7304 | HeapNumber::kExponentOffset)); |
| 7305 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 7306 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 7307 | HeapNumber::kMantissaOffset)); |
| 7308 | __ Ret(); |
| 7309 | |
| 7310 | __ bind(&max_negative_int); |
| 7311 | // The max negative int32 is stored as a positive number in the mantissa of |
| 7312 | // a double because it uses a sign bit instead of using two's complement. |
| 7313 | // The actual mantissa bits stored are all 0 because the implicit most |
| 7314 | // significant 1 bit is not stored. |
| 7315 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 7316 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 7317 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 7318 | __ mov(ip, Operand(0)); |
| 7319 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 7320 | __ Ret(); |
| 7321 | } |
| 7322 | |
| 7323 | |
| 7324 | // Handle the case where the lhs and rhs are the same object. |
| 7325 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 7326 | // for "identity and not NaN". |
| 7327 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 7328 | Label* slow, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7329 | Condition cc, |
| 7330 | bool never_nan_nan) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7331 | Label not_identical; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7332 | Label heap_number, return_equal; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7333 | __ cmp(r0, r1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7334 | __ b(ne, ¬_identical); |
| 7335 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7336 | // The two objects are identical. If we know that one of them isn't NaN then |
| 7337 | // we now know they test equal. |
| 7338 | if (cc != eq || !never_nan_nan) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7339 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 7340 | // so we do the second best thing - test it ourselves. |
| 7341 | // They are both equal and they are not both Smis so both of them are not |
| 7342 | // Smis. If it's not a heap number, then return equal. |
| 7343 | if (cc == lt || cc == gt) { |
| 7344 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7345 | __ b(ge, slow); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7346 | } else { |
| 7347 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 7348 | __ b(eq, &heap_number); |
| 7349 | // Comparing JS objects with <=, >= is complicated. |
| 7350 | if (cc != eq) { |
| 7351 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 7352 | __ b(ge, slow); |
| 7353 | // Normally here we fall through to return_equal, but undefined is |
| 7354 | // special: (undefined == undefined) == true, but |
| 7355 | // (undefined <= undefined) == false! See ECMAScript 11.8.5. |
| 7356 | if (cc == le || cc == ge) { |
| 7357 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 7358 | __ b(ne, &return_equal); |
| 7359 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7360 | __ cmp(r0, r2); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7361 | __ b(ne, &return_equal); |
| 7362 | if (cc == le) { |
| 7363 | // undefined <= undefined should fail. |
| 7364 | __ mov(r0, Operand(GREATER)); |
| 7365 | } else { |
| 7366 | // undefined >= undefined should fail. |
| 7367 | __ mov(r0, Operand(LESS)); |
| 7368 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7369 | __ Ret(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7370 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7371 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7372 | } |
| 7373 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7374 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7375 | __ bind(&return_equal); |
| 7376 | if (cc == lt) { |
| 7377 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 7378 | } else if (cc == gt) { |
| 7379 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 7380 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7381 | __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7382 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7383 | __ Ret(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7384 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7385 | if (cc != eq || !never_nan_nan) { |
| 7386 | // For less and greater we don't have to check for NaN since the result of |
| 7387 | // x < x is false regardless. For the others here is some code to check |
| 7388 | // for NaN. |
| 7389 | if (cc != lt && cc != gt) { |
| 7390 | __ bind(&heap_number); |
| 7391 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 7392 | // not NaN. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7393 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7394 | // The representation of NaN values has all exponent bits (52..62) set, |
| 7395 | // and not all mantissa bits (0..51) clear. |
| 7396 | // Read top bits of double representation (second word of value). |
| 7397 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 7398 | // Test that exponent bits are all set. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 7399 | __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits); |
| 7400 | // NaNs have all-one exponents so they sign extend to -1. |
| 7401 | __ cmp(r3, Operand(-1)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7402 | __ b(ne, &return_equal); |
| 7403 | |
| 7404 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 7405 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 7406 | // Or with all low-bits of mantissa. |
| 7407 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 7408 | __ orr(r0, r3, Operand(r2), SetCC); |
| 7409 | // For equal we already have the right value in r0: Return zero (equal) |
| 7410 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if |
| 7411 | // not (it's a NaN). For <= and >= we need to load r0 with the failing |
| 7412 | // value if it's a NaN. |
| 7413 | if (cc != eq) { |
| 7414 | // All-zero means Infinity means equal. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7415 | __ Ret(eq); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7416 | if (cc == le) { |
| 7417 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 7418 | } else { |
| 7419 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 7420 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7421 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7422 | __ Ret(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7423 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7424 | // No fall through here. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7425 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7426 | |
| 7427 | __ bind(¬_identical); |
| 7428 | } |
| 7429 | |
| 7430 | |
| 7431 | // See comment at call site. |
| 7432 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7433 | Register lhs, |
| 7434 | Register rhs, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7435 | Label* lhs_not_nan, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7436 | Label* slow, |
| 7437 | bool strict) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7438 | ASSERT((lhs.is(r0) && rhs.is(r1)) || |
| 7439 | (lhs.is(r1) && rhs.is(r0))); |
| 7440 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7441 | Label rhs_is_smi; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7442 | __ tst(rhs, Operand(kSmiTagMask)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7443 | __ b(eq, &rhs_is_smi); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7444 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7445 | // Lhs is a Smi. Check whether the rhs is a heap number. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7446 | __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7447 | if (strict) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7448 | // If rhs is not a number and lhs is a Smi then strict equality cannot |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7449 | // succeed. Return non-equal |
| 7450 | // If rhs is r0 then there is already a non zero value in it. |
| 7451 | if (!rhs.is(r0)) { |
| 7452 | __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne); |
| 7453 | } |
| 7454 | __ Ret(ne); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7455 | } else { |
| 7456 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 7457 | // the runtime. |
| 7458 | __ b(ne, slow); |
| 7459 | } |
| 7460 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7461 | // Lhs is a smi, rhs is a number. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7462 | if (CpuFeatures::IsSupported(VFP3)) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7463 | // Convert lhs to a double in d7. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7464 | CpuFeatures::Scope scope(VFP3); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7465 | __ SmiToDoubleVFPRegister(lhs, d7, r7, s15); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7466 | // Load the double from rhs, tagged HeapNumber r0, to d6. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7467 | __ sub(r7, rhs, Operand(kHeapObjectTag)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7468 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7469 | } else { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7470 | __ push(lr); |
| 7471 | // Convert lhs to a double in r2, r3. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7472 | __ mov(r7, Operand(lhs)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7473 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 7474 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7475 | // Load rhs to a double in r0, r1. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7476 | __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7477 | __ pop(lr); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7478 | } |
| 7479 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7480 | // We now have both loaded as doubles but we can skip the lhs nan check |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7481 | // since it's a smi. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7482 | __ jmp(lhs_not_nan); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7483 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7484 | __ bind(&rhs_is_smi); |
| 7485 | // Rhs is a smi. Check whether the non-smi lhs is a heap number. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7486 | __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7487 | if (strict) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7488 | // If lhs is not a number and rhs is a smi then strict equality cannot |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7489 | // succeed. Return non-equal. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7490 | // If lhs is r0 then there is already a non zero value in it. |
| 7491 | if (!lhs.is(r0)) { |
| 7492 | __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne); |
| 7493 | } |
| 7494 | __ Ret(ne); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7495 | } else { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7496 | // Smi compared non-strictly with a non-smi non-heap-number. Call |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7497 | // the runtime. |
| 7498 | __ b(ne, slow); |
| 7499 | } |
| 7500 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7501 | // Rhs is a smi, lhs is a heap number. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7502 | if (CpuFeatures::IsSupported(VFP3)) { |
| 7503 | CpuFeatures::Scope scope(VFP3); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7504 | // Load the double from lhs, tagged HeapNumber r1, to d7. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7505 | __ sub(r7, lhs, Operand(kHeapObjectTag)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7506 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7507 | // Convert rhs to a double in d6 . |
| 7508 | __ SmiToDoubleVFPRegister(rhs, d6, r7, s13); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7509 | } else { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7510 | __ push(lr); |
| 7511 | // Load lhs to a double in r2, r3. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7512 | __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7513 | // Convert rhs to a double in r0, r1. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7514 | __ mov(r7, Operand(rhs)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7515 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 7516 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7517 | __ pop(lr); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7518 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7519 | // Fall through to both_loaded_as_doubles. |
| 7520 | } |
| 7521 | |
| 7522 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7523 | void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7524 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7525 | Register rhs_exponent = exp_first ? r0 : r1; |
| 7526 | Register lhs_exponent = exp_first ? r2 : r3; |
| 7527 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 7528 | Register lhs_mantissa = exp_first ? r3 : r2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7529 | Label one_is_nan, neither_is_nan; |
| 7530 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 7531 | __ Sbfx(r4, |
| 7532 | lhs_exponent, |
| 7533 | HeapNumber::kExponentShift, |
| 7534 | HeapNumber::kExponentBits); |
| 7535 | // NaNs have all-one exponents so they sign extend to -1. |
| 7536 | __ cmp(r4, Operand(-1)); |
| 7537 | __ b(ne, lhs_not_nan); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7538 | __ mov(r4, |
| 7539 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 7540 | SetCC); |
| 7541 | __ b(ne, &one_is_nan); |
| 7542 | __ cmp(lhs_mantissa, Operand(0)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7543 | __ b(ne, &one_is_nan); |
| 7544 | |
| 7545 | __ bind(lhs_not_nan); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 7546 | __ Sbfx(r4, |
| 7547 | rhs_exponent, |
| 7548 | HeapNumber::kExponentShift, |
| 7549 | HeapNumber::kExponentBits); |
| 7550 | // NaNs have all-one exponents so they sign extend to -1. |
| 7551 | __ cmp(r4, Operand(-1)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7552 | __ b(ne, &neither_is_nan); |
| 7553 | __ mov(r4, |
| 7554 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 7555 | SetCC); |
| 7556 | __ b(ne, &one_is_nan); |
| 7557 | __ cmp(rhs_mantissa, Operand(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7558 | __ b(eq, &neither_is_nan); |
| 7559 | |
| 7560 | __ bind(&one_is_nan); |
| 7561 | // NaN comparisons always fail. |
| 7562 | // Load whatever we need in r0 to make the comparison fail. |
| 7563 | if (cc == lt || cc == le) { |
| 7564 | __ mov(r0, Operand(GREATER)); |
| 7565 | } else { |
| 7566 | __ mov(r0, Operand(LESS)); |
| 7567 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7568 | __ Ret(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7569 | |
| 7570 | __ bind(&neither_is_nan); |
| 7571 | } |
| 7572 | |
| 7573 | |
| 7574 | // See comment at call site. |
| 7575 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 7576 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7577 | Register rhs_exponent = exp_first ? r0 : r1; |
| 7578 | Register lhs_exponent = exp_first ? r2 : r3; |
| 7579 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 7580 | Register lhs_mantissa = exp_first ? r3 : r2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7581 | |
| 7582 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 7583 | if (cc == eq) { |
| 7584 | // Doubles are not equal unless they have the same bit pattern. |
| 7585 | // Exception: 0 and -0. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7586 | __ cmp(rhs_mantissa, Operand(lhs_mantissa)); |
| 7587 | __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7588 | // Return non-zero if the numbers are unequal. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7589 | __ Ret(ne); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7590 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7591 | __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7592 | // If exponents are equal then return 0. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7593 | __ Ret(eq); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7594 | |
| 7595 | // Exponents are unequal. The only way we can return that the numbers |
| 7596 | // are equal is if one is -0 and the other is 0. We already dealt |
| 7597 | // with the case where both are -0 or both are 0. |
| 7598 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 7599 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 7600 | // equal. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7601 | __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7602 | __ mov(r0, Operand(r4), LeaveCC, ne); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7603 | __ Ret(ne); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7604 | // Now they are equal if and only if the lhs exponent is zero in its |
| 7605 | // low 31 bits. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7606 | __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize)); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7607 | __ Ret(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7608 | } else { |
| 7609 | // Call a native function to do a comparison between two non-NaNs. |
| 7610 | // Call C routine that may not cause GC or other trouble. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7611 | __ push(lr); |
| 7612 | __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments. |
| 7613 | __ CallCFunction(ExternalReference::compare_doubles(), 4); |
| 7614 | __ pop(pc); // Return. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7615 | } |
| 7616 | } |
| 7617 | |
| 7618 | |
| 7619 | // See comment at call site. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7620 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm, |
| 7621 | Register lhs, |
| 7622 | Register rhs) { |
| 7623 | ASSERT((lhs.is(r0) && rhs.is(r1)) || |
| 7624 | (lhs.is(r1) && rhs.is(r0))); |
| 7625 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7626 | // If either operand is a JSObject or an oddball value, then they are |
| 7627 | // not equal since their pointers are different. |
| 7628 | // There is no test for undetectability in strict equality. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7629 | STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7630 | Label first_non_object; |
| 7631 | // Get the type of the first operand into r2 and compare it with |
| 7632 | // FIRST_JS_OBJECT_TYPE. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7633 | __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7634 | __ b(lt, &first_non_object); |
| 7635 | |
| 7636 | // Return non-zero (r0 is not zero) |
| 7637 | Label return_not_equal; |
| 7638 | __ bind(&return_not_equal); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7639 | __ Ret(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7640 | |
| 7641 | __ bind(&first_non_object); |
| 7642 | // Check for oddballs: true, false, null, undefined. |
| 7643 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 7644 | __ b(eq, &return_not_equal); |
| 7645 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7646 | __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7647 | __ b(ge, &return_not_equal); |
| 7648 | |
| 7649 | // Check for oddballs: true, false, null, undefined. |
| 7650 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 7651 | __ b(eq, &return_not_equal); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7652 | |
| 7653 | // Now that we have the types we might as well check for symbol-symbol. |
| 7654 | // Ensure that no non-strings have the symbol bit set. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7655 | STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask); |
| 7656 | STATIC_ASSERT(kSymbolTag != 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7657 | __ and_(r2, r2, Operand(r3)); |
| 7658 | __ tst(r2, Operand(kIsSymbolMask)); |
| 7659 | __ b(ne, &return_not_equal); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7660 | } |
| 7661 | |
| 7662 | |
| 7663 | // See comment at call site. |
| 7664 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7665 | Register lhs, |
| 7666 | Register rhs, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7667 | Label* both_loaded_as_doubles, |
| 7668 | Label* not_heap_numbers, |
| 7669 | Label* slow) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7670 | ASSERT((lhs.is(r0) && rhs.is(r1)) || |
| 7671 | (lhs.is(r1) && rhs.is(r0))); |
| 7672 | |
| 7673 | __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7674 | __ b(ne, not_heap_numbers); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7675 | __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7676 | __ cmp(r2, r3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7677 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 7678 | |
| 7679 | // Both are heap numbers. Load them up then jump to the code we have |
| 7680 | // for that. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7681 | if (CpuFeatures::IsSupported(VFP3)) { |
| 7682 | CpuFeatures::Scope scope(VFP3); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7683 | __ sub(r7, rhs, Operand(kHeapObjectTag)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7684 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7685 | __ sub(r7, lhs, Operand(kHeapObjectTag)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7686 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 7687 | } else { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7688 | __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset)); |
| 7689 | __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7690 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7691 | __ jmp(both_loaded_as_doubles); |
| 7692 | } |
| 7693 | |
| 7694 | |
| 7695 | // Fast negative check for symbol-to-symbol equality. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7696 | static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7697 | Register lhs, |
| 7698 | Register rhs, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7699 | Label* possible_strings, |
| 7700 | Label* not_both_strings) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7701 | ASSERT((lhs.is(r0) && rhs.is(r1)) || |
| 7702 | (lhs.is(r1) && rhs.is(r0))); |
| 7703 | |
| 7704 | // r2 is object type of rhs. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7705 | // Ensure that no non-strings have the symbol bit set. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7706 | Label object_test; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7707 | STATIC_ASSERT(kSymbolTag != 0); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7708 | __ tst(r2, Operand(kIsNotStringMask)); |
| 7709 | __ b(ne, &object_test); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7710 | __ tst(r2, Operand(kIsSymbolMask)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7711 | __ b(eq, possible_strings); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7712 | __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7713 | __ b(ge, not_both_strings); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7714 | __ tst(r3, Operand(kIsSymbolMask)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7715 | __ b(eq, possible_strings); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7716 | |
| 7717 | // Both are symbols. We already checked they weren't the same pointer |
| 7718 | // so they are not equal. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7719 | __ mov(r0, Operand(NOT_EQUAL)); |
| 7720 | __ Ret(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7721 | |
| 7722 | __ bind(&object_test); |
| 7723 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
| 7724 | __ b(lt, not_both_strings); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7725 | __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7726 | __ b(lt, not_both_strings); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7727 | // If both objects are undetectable, they are equal. Otherwise, they |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7728 | // are not equal, since they are different objects and an object is not |
| 7729 | // equal to undefined. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7730 | __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7731 | __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 7732 | __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset)); |
| 7733 | __ and_(r0, r2, Operand(r3)); |
| 7734 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 7735 | __ eor(r0, r0, Operand(1 << Map::kIsUndetectable)); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7736 | __ Ret(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7737 | } |
| 7738 | |
| 7739 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7740 | void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm, |
| 7741 | Register object, |
| 7742 | Register result, |
| 7743 | Register scratch1, |
| 7744 | Register scratch2, |
| 7745 | Register scratch3, |
| 7746 | bool object_is_smi, |
| 7747 | Label* not_found) { |
| 7748 | // Use of registers. Register result is used as a temporary. |
| 7749 | Register number_string_cache = result; |
| 7750 | Register mask = scratch3; |
| 7751 | |
| 7752 | // Load the number string cache. |
| 7753 | __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex); |
| 7754 | |
| 7755 | // Make the hash mask from the length of the number string cache. It |
| 7756 | // contains two elements (number and string) for each cache entry. |
| 7757 | __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7758 | // Divide length by two (length is a smi). |
| 7759 | __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7760 | __ sub(mask, mask, Operand(1)); // Make mask. |
| 7761 | |
| 7762 | // Calculate the entry in the number string cache. The hash value in the |
| 7763 | // number string cache for smis is just the smi value, and the hash for |
| 7764 | // doubles is the xor of the upper and lower words. See |
| 7765 | // Heap::GetNumberStringCache. |
| 7766 | Label is_smi; |
| 7767 | Label load_result_from_cache; |
| 7768 | if (!object_is_smi) { |
| 7769 | __ BranchOnSmi(object, &is_smi); |
| 7770 | if (CpuFeatures::IsSupported(VFP3)) { |
| 7771 | CpuFeatures::Scope scope(VFP3); |
| 7772 | __ CheckMap(object, |
| 7773 | scratch1, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7774 | Heap::kHeapNumberMapRootIndex, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7775 | not_found, |
| 7776 | true); |
| 7777 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7778 | STATIC_ASSERT(8 == kDoubleSize); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7779 | __ add(scratch1, |
| 7780 | object, |
| 7781 | Operand(HeapNumber::kValueOffset - kHeapObjectTag)); |
| 7782 | __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit()); |
| 7783 | __ eor(scratch1, scratch1, Operand(scratch2)); |
| 7784 | __ and_(scratch1, scratch1, Operand(mask)); |
| 7785 | |
| 7786 | // Calculate address of entry in string cache: each entry consists |
| 7787 | // of two pointer sized fields. |
| 7788 | __ add(scratch1, |
| 7789 | number_string_cache, |
| 7790 | Operand(scratch1, LSL, kPointerSizeLog2 + 1)); |
| 7791 | |
| 7792 | Register probe = mask; |
| 7793 | __ ldr(probe, |
| 7794 | FieldMemOperand(scratch1, FixedArray::kHeaderSize)); |
| 7795 | __ BranchOnSmi(probe, not_found); |
| 7796 | __ sub(scratch2, object, Operand(kHeapObjectTag)); |
| 7797 | __ vldr(d0, scratch2, HeapNumber::kValueOffset); |
| 7798 | __ sub(probe, probe, Operand(kHeapObjectTag)); |
| 7799 | __ vldr(d1, probe, HeapNumber::kValueOffset); |
| 7800 | __ vcmp(d0, d1); |
| 7801 | __ vmrs(pc); |
| 7802 | __ b(ne, not_found); // The cache did not contain this value. |
| 7803 | __ b(&load_result_from_cache); |
| 7804 | } else { |
| 7805 | __ b(not_found); |
| 7806 | } |
| 7807 | } |
| 7808 | |
| 7809 | __ bind(&is_smi); |
| 7810 | Register scratch = scratch1; |
| 7811 | __ and_(scratch, mask, Operand(object, ASR, 1)); |
| 7812 | // Calculate address of entry in string cache: each entry consists |
| 7813 | // of two pointer sized fields. |
| 7814 | __ add(scratch, |
| 7815 | number_string_cache, |
| 7816 | Operand(scratch, LSL, kPointerSizeLog2 + 1)); |
| 7817 | |
| 7818 | // Check if the entry is the smi we are looking for. |
| 7819 | Register probe = mask; |
| 7820 | __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize)); |
| 7821 | __ cmp(object, probe); |
| 7822 | __ b(ne, not_found); |
| 7823 | |
| 7824 | // Get the result from the cache. |
| 7825 | __ bind(&load_result_from_cache); |
| 7826 | __ ldr(result, |
| 7827 | FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize)); |
| 7828 | __ IncrementCounter(&Counters::number_to_string_native, |
| 7829 | 1, |
| 7830 | scratch1, |
| 7831 | scratch2); |
| 7832 | } |
| 7833 | |
| 7834 | |
| 7835 | void NumberToStringStub::Generate(MacroAssembler* masm) { |
| 7836 | Label runtime; |
| 7837 | |
| 7838 | __ ldr(r1, MemOperand(sp, 0)); |
| 7839 | |
| 7840 | // Generate code to lookup number in the number string cache. |
| 7841 | GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime); |
| 7842 | __ add(sp, sp, Operand(1 * kPointerSize)); |
| 7843 | __ Ret(); |
| 7844 | |
| 7845 | __ bind(&runtime); |
| 7846 | // Handle number to string in the runtime system if not found in the cache. |
| 7847 | __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1); |
| 7848 | } |
| 7849 | |
| 7850 | |
| 7851 | void RecordWriteStub::Generate(MacroAssembler* masm) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7852 | __ add(offset_, object_, Operand(offset_)); |
| 7853 | __ RecordWriteHelper(object_, offset_, scratch_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7854 | __ Ret(); |
| 7855 | } |
| 7856 | |
| 7857 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7858 | // On entry lhs_ and rhs_ are the values to be compared. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7859 | // On exit r0 is 0, positive or negative to indicate the result of |
| 7860 | // the comparison. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7861 | void CompareStub::Generate(MacroAssembler* masm) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7862 | ASSERT((lhs_.is(r0) && rhs_.is(r1)) || |
| 7863 | (lhs_.is(r1) && rhs_.is(r0))); |
| 7864 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7865 | Label slow; // Call builtin. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7866 | Label not_smis, both_loaded_as_doubles, lhs_not_nan; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7867 | |
| 7868 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 7869 | // it is certain that at least one operand isn't a smi. |
| 7870 | |
| 7871 | // Handle the case where the objects are identical. Either returns the answer |
| 7872 | // or goes to slow. Only falls through if the objects were not identical. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7873 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7874 | |
| 7875 | // If either is a Smi (we know that not both are), then they can only |
| 7876 | // be strictly equal if the other is a HeapNumber. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7877 | STATIC_ASSERT(kSmiTag == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7878 | ASSERT_EQ(0, Smi::FromInt(0)); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7879 | __ and_(r2, lhs_, Operand(rhs_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7880 | __ tst(r2, Operand(kSmiTagMask)); |
| 7881 | __ b(ne, ¬_smis); |
| 7882 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 7883 | // 1) Return the answer. |
| 7884 | // 2) Go to slow. |
| 7885 | // 3) Fall through to both_loaded_as_doubles. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7886 | // 4) Jump to lhs_not_nan. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7887 | // In cases 3 and 4 we have found out we were dealing with a number-number |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7888 | // comparison. If VFP3 is supported the double values of the numbers have |
| 7889 | // been loaded into d7 and d6. Otherwise, the double values have been loaded |
| 7890 | // into r0, r1, r2, and r3. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7891 | EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7892 | |
| 7893 | __ bind(&both_loaded_as_doubles); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7894 | // The arguments have been converted to doubles and stored in d6 and d7, if |
| 7895 | // VFP3 is supported, or in r0, r1, r2, and r3. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7896 | if (CpuFeatures::IsSupported(VFP3)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7897 | __ bind(&lhs_not_nan); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7898 | CpuFeatures::Scope scope(VFP3); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7899 | Label no_nan; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7900 | // ARMv7 VFP3 instructions to implement double precision comparison. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7901 | __ vcmp(d7, d6); |
| 7902 | __ vmrs(pc); // Move vector status bits to normal status bits. |
| 7903 | Label nan; |
| 7904 | __ b(vs, &nan); |
| 7905 | __ mov(r0, Operand(EQUAL), LeaveCC, eq); |
| 7906 | __ mov(r0, Operand(LESS), LeaveCC, lt); |
| 7907 | __ mov(r0, Operand(GREATER), LeaveCC, gt); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7908 | __ Ret(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7909 | |
| 7910 | __ bind(&nan); |
| 7911 | // If one of the sides was a NaN then the v flag is set. Load r0 with |
| 7912 | // whatever it takes to make the comparison fail, since comparisons with NaN |
| 7913 | // always fail. |
| 7914 | if (cc_ == lt || cc_ == le) { |
| 7915 | __ mov(r0, Operand(GREATER)); |
| 7916 | } else { |
| 7917 | __ mov(r0, Operand(LESS)); |
| 7918 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7919 | __ Ret(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7920 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7921 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 7922 | // fall through if neither is a NaN. Also binds lhs_not_nan. |
| 7923 | EmitNanCheck(masm, &lhs_not_nan, cc_); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7924 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 7925 | // answer. Never falls through. |
| 7926 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 7927 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7928 | |
| 7929 | __ bind(¬_smis); |
| 7930 | // At this point we know we are dealing with two different objects, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7931 | // and neither of them is a Smi. The objects are in rhs_ and lhs_. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7932 | if (strict_) { |
| 7933 | // This returns non-equal for some object types, or falls through if it |
| 7934 | // was not lucky. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7935 | EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7936 | } |
| 7937 | |
| 7938 | Label check_for_symbols; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7939 | Label flat_string_check; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7940 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 7941 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 7942 | // that case. If the inputs are not doubles then jumps to check_for_symbols. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7943 | // In this case r2 will contain the type of rhs_. Never falls through. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7944 | EmitCheckForTwoHeapNumbers(masm, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7945 | lhs_, |
| 7946 | rhs_, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7947 | &both_loaded_as_doubles, |
| 7948 | &check_for_symbols, |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7949 | &flat_string_check); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7950 | |
| 7951 | __ bind(&check_for_symbols); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7952 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 7953 | // symbols. |
| 7954 | if (cc_ == eq && !strict_) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7955 | // Returns an answer for two symbols or two detectable objects. |
| 7956 | // Otherwise jumps to string case or not both strings case. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7957 | // Assumes that r2 is the type of rhs_ on entry. |
| 7958 | EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7959 | } |
| 7960 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7961 | // Check for both being sequential ASCII strings, and inline if that is the |
| 7962 | // case. |
| 7963 | __ bind(&flat_string_check); |
| 7964 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7965 | __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7966 | |
| 7967 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 7968 | StringCompareStub::GenerateCompareFlatAsciiStrings(masm, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7969 | lhs_, |
| 7970 | rhs_, |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7971 | r2, |
| 7972 | r3, |
| 7973 | r4, |
| 7974 | r5); |
| 7975 | // Never falls through to here. |
| 7976 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7977 | __ bind(&slow); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 7978 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7979 | __ Push(lhs_, rhs_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7980 | // Figure out which native to call and setup the arguments. |
| 7981 | Builtins::JavaScript native; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7982 | if (cc_ == eq) { |
| 7983 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 7984 | } else { |
| 7985 | native = Builtins::COMPARE; |
| 7986 | int ncr; // NaN compare result |
| 7987 | if (cc_ == lt || cc_ == le) { |
| 7988 | ncr = GREATER; |
| 7989 | } else { |
| 7990 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 7991 | ncr = LESS; |
| 7992 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7993 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 7994 | __ push(r0); |
| 7995 | } |
| 7996 | |
| 7997 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 7998 | // tagged as a small integer. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7999 | __ InvokeBuiltin(native, JUMP_JS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8000 | } |
| 8001 | |
| 8002 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 8003 | // This stub does not handle the inlined cases (Smis, Booleans, undefined). |
| 8004 | // The stub returns zero for false, and a non-zero value for true. |
| 8005 | void ToBooleanStub::Generate(MacroAssembler* masm) { |
| 8006 | Label false_result; |
| 8007 | Label not_heap_number; |
| 8008 | Register scratch0 = VirtualFrame::scratch0(); |
| 8009 | |
| 8010 | // HeapNumber => false iff +0, -0, or NaN. |
| 8011 | __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset)); |
| 8012 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 8013 | __ cmp(scratch0, ip); |
| 8014 | __ b(¬_heap_number, ne); |
| 8015 | |
| 8016 | __ sub(ip, tos_, Operand(kHeapObjectTag)); |
| 8017 | __ vldr(d1, ip, HeapNumber::kValueOffset); |
| 8018 | __ vcmp(d1, 0.0); |
| 8019 | __ vmrs(pc); |
| 8020 | // "tos_" is a register, and contains a non zero value by default. |
| 8021 | // Hence we only need to overwrite "tos_" with zero to return false for |
| 8022 | // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true. |
| 8023 | __ mov(tos_, Operand(0), LeaveCC, eq); // for FP_ZERO |
| 8024 | __ mov(tos_, Operand(0), LeaveCC, vs); // for FP_NAN |
| 8025 | __ Ret(); |
| 8026 | |
| 8027 | __ bind(¬_heap_number); |
| 8028 | |
| 8029 | // Check if the value is 'null'. |
| 8030 | // 'null' => false. |
| 8031 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 8032 | __ cmp(tos_, ip); |
| 8033 | __ b(&false_result, eq); |
| 8034 | |
| 8035 | // It can be an undetectable object. |
| 8036 | // Undetectable => false. |
| 8037 | __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset)); |
| 8038 | __ ldrb(scratch0, FieldMemOperand(ip, Map::kBitFieldOffset)); |
| 8039 | __ and_(scratch0, scratch0, Operand(1 << Map::kIsUndetectable)); |
| 8040 | __ cmp(scratch0, Operand(1 << Map::kIsUndetectable)); |
| 8041 | __ b(&false_result, eq); |
| 8042 | |
| 8043 | // JavaScript object => true. |
| 8044 | __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset)); |
| 8045 | __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset)); |
| 8046 | __ cmp(scratch0, Operand(FIRST_JS_OBJECT_TYPE)); |
| 8047 | // "tos_" is a register and contains a non-zero value. |
| 8048 | // Hence we implicitly return true if the greater than |
| 8049 | // condition is satisfied. |
| 8050 | __ Ret(gt); |
| 8051 | |
| 8052 | // Check for string |
| 8053 | __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset)); |
| 8054 | __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset)); |
| 8055 | __ cmp(scratch0, Operand(FIRST_NONSTRING_TYPE)); |
| 8056 | // "tos_" is a register and contains a non-zero value. |
| 8057 | // Hence we implicitly return true if the greater than |
| 8058 | // condition is satisfied. |
| 8059 | __ Ret(gt); |
| 8060 | |
| 8061 | // String value => false iff empty, i.e., length is zero |
| 8062 | __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset)); |
| 8063 | // If length is zero, "tos_" contains zero ==> false. |
| 8064 | // If length is not zero, "tos_" contains a non-zero value ==> true. |
| 8065 | __ Ret(); |
| 8066 | |
| 8067 | // Return 0 in "tos_" for false . |
| 8068 | __ bind(&false_result); |
| 8069 | __ mov(tos_, Operand(0)); |
| 8070 | __ Ret(); |
| 8071 | } |
| 8072 | |
| 8073 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8074 | // We fall into this code if the operands were Smis, but the result was |
| 8075 | // not (eg. overflow). We branch into this code (to the not_smi label) if |
| 8076 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 8077 | // to call the C-implemented binary fp operation routines we need to end up |
| 8078 | // with the double precision floating point operands in r0 and r1 (for the |
| 8079 | // value in r1) and r2 and r3 (for the value in r0). |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8080 | void GenericBinaryOpStub::HandleBinaryOpSlowCases( |
| 8081 | MacroAssembler* masm, |
| 8082 | Label* not_smi, |
| 8083 | Register lhs, |
| 8084 | Register rhs, |
| 8085 | const Builtins::JavaScript& builtin) { |
| 8086 | Label slow, slow_reverse, do_the_call; |
| 8087 | bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8088 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8089 | ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8090 | Register heap_number_map = r6; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8091 | |
| 8092 | if (ShouldGenerateSmiCode()) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8093 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 8094 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8095 | // Smi-smi case (overflow). |
| 8096 | // Since both are Smis there is no heap number to overwrite, so allocate. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8097 | // The new heap number is in r5. r3 and r7 are scratch. |
| 8098 | __ AllocateHeapNumber( |
| 8099 | r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8100 | |
| 8101 | // If we have floating point hardware, inline ADD, SUB, MUL, and DIV, |
| 8102 | // using registers d7 and d6 for the double values. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8103 | if (CpuFeatures::IsSupported(VFP3)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8104 | CpuFeatures::Scope scope(VFP3); |
| 8105 | __ mov(r7, Operand(rhs, ASR, kSmiTagSize)); |
| 8106 | __ vmov(s15, r7); |
| 8107 | __ vcvt_f64_s32(d7, s15); |
| 8108 | __ mov(r7, Operand(lhs, ASR, kSmiTagSize)); |
| 8109 | __ vmov(s13, r7); |
| 8110 | __ vcvt_f64_s32(d6, s13); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8111 | if (!use_fp_registers) { |
| 8112 | __ vmov(r2, r3, d7); |
| 8113 | __ vmov(r0, r1, d6); |
| 8114 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8115 | } else { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8116 | // Write Smi from rhs to r3 and r2 in double format. r9 is scratch. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8117 | __ mov(r7, Operand(rhs)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8118 | ConvertToDoubleStub stub1(r3, r2, r7, r9); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8119 | __ push(lr); |
| 8120 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8121 | // Write Smi from lhs to r1 and r0 in double format. r9 is scratch. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8122 | __ mov(r7, Operand(lhs)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8123 | ConvertToDoubleStub stub2(r1, r0, r7, r9); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8124 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 8125 | __ pop(lr); |
| 8126 | } |
| 8127 | __ jmp(&do_the_call); // Tail call. No return. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8128 | } |
| 8129 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8130 | // We branch here if at least one of r0 and r1 is not a Smi. |
| 8131 | __ bind(not_smi); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8132 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8133 | |
| 8134 | // After this point we have the left hand side in r1 and the right hand side |
| 8135 | // in r0. |
| 8136 | if (lhs.is(r0)) { |
| 8137 | __ Swap(r0, r1, ip); |
| 8138 | } |
| 8139 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8140 | // The type transition also calculates the answer. |
| 8141 | bool generate_code_to_calculate_answer = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8142 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8143 | if (ShouldGenerateFPCode()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8144 | if (runtime_operands_type_ == BinaryOpIC::DEFAULT) { |
| 8145 | switch (op_) { |
| 8146 | case Token::ADD: |
| 8147 | case Token::SUB: |
| 8148 | case Token::MUL: |
| 8149 | case Token::DIV: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8150 | GenerateTypeTransition(masm); // Tail call. |
| 8151 | generate_code_to_calculate_answer = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8152 | break; |
| 8153 | |
| 8154 | default: |
| 8155 | break; |
| 8156 | } |
| 8157 | } |
| 8158 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8159 | if (generate_code_to_calculate_answer) { |
| 8160 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 8161 | if (mode_ == NO_OVERWRITE) { |
| 8162 | // In the case where there is no chance of an overwritable float we may |
| 8163 | // as well do the allocation immediately while r0 and r1 are untouched. |
| 8164 | __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8165 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8166 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8167 | // Move r0 to a double in r2-r3. |
| 8168 | __ tst(r0, Operand(kSmiTagMask)); |
| 8169 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 8170 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8171 | __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 8172 | __ cmp(r4, heap_number_map); |
| 8173 | __ b(ne, &slow); |
| 8174 | if (mode_ == OVERWRITE_RIGHT) { |
| 8175 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8176 | } |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8177 | if (use_fp_registers) { |
| 8178 | CpuFeatures::Scope scope(VFP3); |
| 8179 | // Load the double from tagged HeapNumber r0 to d7. |
| 8180 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 8181 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8182 | } else { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8183 | // Calling convention says that second double is in r2 and r3. |
| 8184 | __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8185 | } |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8186 | __ jmp(&finished_loading_r0); |
| 8187 | __ bind(&r0_is_smi); |
| 8188 | if (mode_ == OVERWRITE_RIGHT) { |
| 8189 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 8190 | __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow); |
| 8191 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8192 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8193 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8194 | CpuFeatures::Scope scope(VFP3); |
| 8195 | // Convert smi in r0 to double in d7. |
| 8196 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 8197 | __ vmov(s15, r7); |
| 8198 | __ vcvt_f64_s32(d7, s15); |
| 8199 | if (!use_fp_registers) { |
| 8200 | __ vmov(r2, r3, d7); |
| 8201 | } |
| 8202 | } else { |
| 8203 | // Write Smi from r0 to r3 and r2 in double format. |
| 8204 | __ mov(r7, Operand(r0)); |
| 8205 | ConvertToDoubleStub stub3(r3, r2, r7, r4); |
| 8206 | __ push(lr); |
| 8207 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 8208 | __ pop(lr); |
| 8209 | } |
| 8210 | |
| 8211 | // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis. |
| 8212 | // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC. |
| 8213 | Label r1_is_not_smi; |
| 8214 | if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) { |
| 8215 | __ tst(r1, Operand(kSmiTagMask)); |
| 8216 | __ b(ne, &r1_is_not_smi); |
| 8217 | GenerateTypeTransition(masm); // Tail call. |
| 8218 | } |
| 8219 | |
| 8220 | __ bind(&finished_loading_r0); |
| 8221 | |
| 8222 | // Move r1 to a double in r0-r1. |
| 8223 | __ tst(r1, Operand(kSmiTagMask)); |
| 8224 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 8225 | __ bind(&r1_is_not_smi); |
| 8226 | __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset)); |
| 8227 | __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 8228 | __ cmp(r4, heap_number_map); |
| 8229 | __ b(ne, &slow); |
| 8230 | if (mode_ == OVERWRITE_LEFT) { |
| 8231 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
| 8232 | } |
| 8233 | if (use_fp_registers) { |
| 8234 | CpuFeatures::Scope scope(VFP3); |
| 8235 | // Load the double from tagged HeapNumber r1 to d6. |
| 8236 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 8237 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 8238 | } else { |
| 8239 | // Calling convention says that first double is in r0 and r1. |
| 8240 | __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 8241 | } |
| 8242 | __ jmp(&finished_loading_r1); |
| 8243 | __ bind(&r1_is_smi); |
| 8244 | if (mode_ == OVERWRITE_LEFT) { |
| 8245 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 8246 | __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow); |
| 8247 | } |
| 8248 | |
| 8249 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8250 | CpuFeatures::Scope scope(VFP3); |
| 8251 | // Convert smi in r1 to double in d6. |
| 8252 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 8253 | __ vmov(s13, r7); |
| 8254 | __ vcvt_f64_s32(d6, s13); |
| 8255 | if (!use_fp_registers) { |
| 8256 | __ vmov(r0, r1, d6); |
| 8257 | } |
| 8258 | } else { |
| 8259 | // Write Smi from r1 to r1 and r0 in double format. |
| 8260 | __ mov(r7, Operand(r1)); |
| 8261 | ConvertToDoubleStub stub4(r1, r0, r7, r9); |
| 8262 | __ push(lr); |
| 8263 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 8264 | __ pop(lr); |
| 8265 | } |
| 8266 | |
| 8267 | __ bind(&finished_loading_r1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8268 | } |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 8269 | |
| 8270 | if (generate_code_to_calculate_answer || do_the_call.is_linked()) { |
| 8271 | __ bind(&do_the_call); |
| 8272 | // If we are inlining the operation using VFP3 instructions for |
| 8273 | // add, subtract, multiply, or divide, the arguments are in d6 and d7. |
| 8274 | if (use_fp_registers) { |
| 8275 | CpuFeatures::Scope scope(VFP3); |
| 8276 | // ARMv7 VFP3 instructions to implement |
| 8277 | // double precision, add, subtract, multiply, divide. |
| 8278 | |
| 8279 | if (Token::MUL == op_) { |
| 8280 | __ vmul(d5, d6, d7); |
| 8281 | } else if (Token::DIV == op_) { |
| 8282 | __ vdiv(d5, d6, d7); |
| 8283 | } else if (Token::ADD == op_) { |
| 8284 | __ vadd(d5, d6, d7); |
| 8285 | } else if (Token::SUB == op_) { |
| 8286 | __ vsub(d5, d6, d7); |
| 8287 | } else { |
| 8288 | UNREACHABLE(); |
| 8289 | } |
| 8290 | __ sub(r0, r5, Operand(kHeapObjectTag)); |
| 8291 | __ vstr(d5, r0, HeapNumber::kValueOffset); |
| 8292 | __ add(r0, r0, Operand(kHeapObjectTag)); |
| 8293 | __ mov(pc, lr); |
| 8294 | } else { |
| 8295 | // If we did not inline the operation, then the arguments are in: |
| 8296 | // r0: Left value (least significant part of mantissa). |
| 8297 | // r1: Left value (sign, exponent, top of mantissa). |
| 8298 | // r2: Right value (least significant part of mantissa). |
| 8299 | // r3: Right value (sign, exponent, top of mantissa). |
| 8300 | // r5: Address of heap number for result. |
| 8301 | |
| 8302 | __ push(lr); // For later. |
| 8303 | __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments. |
| 8304 | // Call C routine that may not cause GC or other trouble. r5 is callee |
| 8305 | // save. |
| 8306 | __ CallCFunction(ExternalReference::double_fp_operation(op_), 4); |
| 8307 | // Store answer in the overwritable heap number. |
| 8308 | #if !defined(USE_ARM_EABI) |
| 8309 | // Double returned in fp coprocessor register 0 and 1, encoded as |
| 8310 | // register cr8. Offsets must be divisible by 4 for coprocessor so we |
| 8311 | // need to substract the tag from r5. |
| 8312 | __ sub(r4, r5, Operand(kHeapObjectTag)); |
| 8313 | __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset)); |
| 8314 | #else |
| 8315 | // Double returned in registers 0 and 1. |
| 8316 | __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset)); |
| 8317 | #endif |
| 8318 | __ mov(r0, Operand(r5)); |
| 8319 | // And we are done. |
| 8320 | __ pop(pc); |
| 8321 | } |
| 8322 | } |
| 8323 | } |
| 8324 | |
| 8325 | if (!generate_code_to_calculate_answer && |
| 8326 | !slow_reverse.is_linked() && |
| 8327 | !slow.is_linked()) { |
| 8328 | return; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8329 | } |
| 8330 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8331 | if (lhs.is(r0)) { |
| 8332 | __ b(&slow); |
| 8333 | __ bind(&slow_reverse); |
| 8334 | __ Swap(r0, r1, ip); |
| 8335 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8336 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8337 | heap_number_map = no_reg; // Don't use this any more from here on. |
| 8338 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8339 | // We jump to here if something goes wrong (one param is not a number of any |
| 8340 | // sort or new-space allocation fails). |
| 8341 | __ bind(&slow); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8342 | |
| 8343 | // Push arguments to the stack |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8344 | __ Push(r1, r0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8345 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8346 | if (Token::ADD == op_) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8347 | // Test for string arguments before calling runtime. |
| 8348 | // r1 : first argument |
| 8349 | // r0 : second argument |
| 8350 | // sp[0] : second argument |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 8351 | // sp[4] : first argument |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8352 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8353 | Label not_strings, not_string1, string1, string1_smi2; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8354 | __ tst(r1, Operand(kSmiTagMask)); |
| 8355 | __ b(eq, ¬_string1); |
| 8356 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 8357 | __ b(ge, ¬_string1); |
| 8358 | |
| 8359 | // First argument is a a string, test second. |
| 8360 | __ tst(r0, Operand(kSmiTagMask)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8361 | __ b(eq, &string1_smi2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8362 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 8363 | __ b(ge, &string1); |
| 8364 | |
| 8365 | // First and second argument are strings. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8366 | StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB); |
| 8367 | __ TailCallStub(&string_add_stub); |
| 8368 | |
| 8369 | __ bind(&string1_smi2); |
| 8370 | // First argument is a string, second is a smi. Try to lookup the number |
| 8371 | // string for the smi in the number string cache. |
| 8372 | NumberToStringStub::GenerateLookupNumberStringCache( |
| 8373 | masm, r0, r2, r4, r5, r6, true, &string1); |
| 8374 | |
| 8375 | // Replace second argument on stack and tailcall string add stub to make |
| 8376 | // the result. |
| 8377 | __ str(r2, MemOperand(sp, 0)); |
| 8378 | __ TailCallStub(&string_add_stub); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8379 | |
| 8380 | // Only first argument is a string. |
| 8381 | __ bind(&string1); |
| 8382 | __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS); |
| 8383 | |
| 8384 | // First argument was not a string, test second. |
| 8385 | __ bind(¬_string1); |
| 8386 | __ tst(r0, Operand(kSmiTagMask)); |
| 8387 | __ b(eq, ¬_strings); |
| 8388 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 8389 | __ b(ge, ¬_strings); |
| 8390 | |
| 8391 | // Only second argument is a string. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8392 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 8393 | |
| 8394 | __ bind(¬_strings); |
| 8395 | } |
| 8396 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8397 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8398 | } |
| 8399 | |
| 8400 | |
| 8401 | // Tries to get a signed int32 out of a double precision floating point heap |
| 8402 | // number. Rounds towards 0. Fastest for doubles that are in the ranges |
| 8403 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 8404 | // almost to the range of signed int32 values that are not Smis. Jumps to the |
| 8405 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 8406 | // (excluding the endpoints). |
| 8407 | static void GetInt32(MacroAssembler* masm, |
| 8408 | Register source, |
| 8409 | Register dest, |
| 8410 | Register scratch, |
| 8411 | Register scratch2, |
| 8412 | Label* slow) { |
| 8413 | Label right_exponent, done; |
| 8414 | // Get exponent word. |
| 8415 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 8416 | // Get exponent alone in scratch2. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8417 | __ Ubfx(scratch2, |
| 8418 | scratch, |
| 8419 | HeapNumber::kExponentShift, |
| 8420 | HeapNumber::kExponentBits); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8421 | // Load dest with zero. We use this either for the final shift or |
| 8422 | // for the answer. |
| 8423 | __ mov(dest, Operand(0)); |
| 8424 | // Check whether the exponent matches a 32 bit signed int that is not a Smi. |
| 8425 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 8426 | // the exponent that we are fastest at and also the highest exponent we can |
| 8427 | // handle here. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8428 | const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30; |
| 8429 | // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we |
| 8430 | // split it up to avoid a constant pool entry. You can't do that in general |
| 8431 | // for cmp because of the overflow flag, but we know the exponent is in the |
| 8432 | // range 0-2047 so there is no overflow. |
| 8433 | int fudge_factor = 0x400; |
| 8434 | __ sub(scratch2, scratch2, Operand(fudge_factor)); |
| 8435 | __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8436 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 8437 | __ b(eq, &right_exponent); |
| 8438 | // If the exponent is higher than that then go to slow case. This catches |
| 8439 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 8440 | __ b(gt, slow); |
| 8441 | |
| 8442 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 8443 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 8444 | // it rounds to zero. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8445 | const uint32_t zero_exponent = HeapNumber::kExponentBias + 0; |
| 8446 | __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8447 | // Dest already has a Smi zero. |
| 8448 | __ b(lt, &done); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8449 | if (!CpuFeatures::IsSupported(VFP3)) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8450 | // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to |
| 8451 | // get how much to shift down. |
| 8452 | __ rsb(dest, scratch2, Operand(30)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8453 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8454 | __ bind(&right_exponent); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8455 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8456 | CpuFeatures::Scope scope(VFP3); |
| 8457 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 8458 | // conversion using round to zero. |
| 8459 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8460 | __ vmov(d7, scratch2, scratch); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8461 | __ vcvt_s32_f64(s15, d7); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8462 | __ vmov(dest, s15); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8463 | } else { |
| 8464 | // Get the top bits of the mantissa. |
| 8465 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 8466 | // Put back the implicit 1. |
| 8467 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 8468 | // Shift up the mantissa bits to take up the space the exponent used to |
| 8469 | // take. We just orred in the implicit bit so that took care of one and |
| 8470 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 8471 | // distance. |
| 8472 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 8473 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 8474 | // Put sign in zero flag. |
| 8475 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 8476 | // Get the second half of the double. For some exponents we don't |
| 8477 | // actually need this because the bits get shifted out again, but |
| 8478 | // it's probably slower to test than just to do it. |
| 8479 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 8480 | // Shift down 22 bits to get the last 10 bits. |
| 8481 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 8482 | // Move down according to the exponent. |
| 8483 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 8484 | // Fix sign if sign bit was set. |
| 8485 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 8486 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8487 | __ bind(&done); |
| 8488 | } |
| 8489 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8490 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 8491 | // whether both inputs are either Smis or at least heap numbers that can be |
| 8492 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 8493 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 8494 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 8495 | // write the answer into (either by allocating or by overwriting). |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8496 | // On entry the operands are in lhs and rhs. On exit the answer is in r0. |
| 8497 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm, |
| 8498 | Register lhs, |
| 8499 | Register rhs) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8500 | Label slow, result_not_a_smi; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8501 | Label rhs_is_smi, lhs_is_smi; |
| 8502 | Label done_checking_rhs, done_checking_lhs; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8503 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8504 | Register heap_number_map = r6; |
| 8505 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 8506 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8507 | __ tst(lhs, Operand(kSmiTagMask)); |
| 8508 | __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8509 | __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset)); |
| 8510 | __ cmp(r4, heap_number_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8511 | __ b(ne, &slow); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8512 | GetInt32(masm, lhs, r3, r5, r4, &slow); |
| 8513 | __ jmp(&done_checking_lhs); |
| 8514 | __ bind(&lhs_is_smi); |
| 8515 | __ mov(r3, Operand(lhs, ASR, 1)); |
| 8516 | __ bind(&done_checking_lhs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8517 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8518 | __ tst(rhs, Operand(kSmiTagMask)); |
| 8519 | __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8520 | __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset)); |
| 8521 | __ cmp(r4, heap_number_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8522 | __ b(ne, &slow); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8523 | GetInt32(masm, rhs, r2, r5, r4, &slow); |
| 8524 | __ jmp(&done_checking_rhs); |
| 8525 | __ bind(&rhs_is_smi); |
| 8526 | __ mov(r2, Operand(rhs, ASR, 1)); |
| 8527 | __ bind(&done_checking_rhs); |
| 8528 | |
| 8529 | ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8530 | |
| 8531 | // r0 and r1: Original operands (Smi or heap numbers). |
| 8532 | // r2 and r3: Signed int32 operands. |
| 8533 | switch (op_) { |
| 8534 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 8535 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 8536 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 8537 | case Token::SAR: |
| 8538 | // Use only the 5 least significant bits of the shift count. |
| 8539 | __ and_(r2, r2, Operand(0x1f)); |
| 8540 | __ mov(r2, Operand(r3, ASR, r2)); |
| 8541 | break; |
| 8542 | case Token::SHR: |
| 8543 | // Use only the 5 least significant bits of the shift count. |
| 8544 | __ and_(r2, r2, Operand(0x1f)); |
| 8545 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 8546 | // SHR is special because it is required to produce a positive answer. |
| 8547 | // The code below for writing into heap numbers isn't capable of writing |
| 8548 | // the register as an unsigned int so we go to slow case if we hit this |
| 8549 | // case. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8550 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8551 | __ b(mi, &result_not_a_smi); |
| 8552 | } else { |
| 8553 | __ b(mi, &slow); |
| 8554 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8555 | break; |
| 8556 | case Token::SHL: |
| 8557 | // Use only the 5 least significant bits of the shift count. |
| 8558 | __ and_(r2, r2, Operand(0x1f)); |
| 8559 | __ mov(r2, Operand(r3, LSL, r2)); |
| 8560 | break; |
| 8561 | default: UNREACHABLE(); |
| 8562 | } |
| 8563 | // check that the *signed* result fits in a smi |
| 8564 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 8565 | __ b(mi, &result_not_a_smi); |
| 8566 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 8567 | __ Ret(); |
| 8568 | |
| 8569 | Label have_to_allocate, got_a_heap_number; |
| 8570 | __ bind(&result_not_a_smi); |
| 8571 | switch (mode_) { |
| 8572 | case OVERWRITE_RIGHT: { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8573 | __ tst(rhs, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8574 | __ b(eq, &have_to_allocate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8575 | __ mov(r5, Operand(rhs)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8576 | break; |
| 8577 | } |
| 8578 | case OVERWRITE_LEFT: { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8579 | __ tst(lhs, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8580 | __ b(eq, &have_to_allocate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8581 | __ mov(r5, Operand(lhs)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8582 | break; |
| 8583 | } |
| 8584 | case NO_OVERWRITE: { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8585 | // Get a new heap number in r5. r4 and r7 are scratch. |
| 8586 | __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8587 | } |
| 8588 | default: break; |
| 8589 | } |
| 8590 | __ bind(&got_a_heap_number); |
| 8591 | // r2: Answer as signed int32. |
| 8592 | // r5: Heap number to write answer into. |
| 8593 | |
| 8594 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 8595 | // result. |
| 8596 | __ mov(r0, Operand(r5)); |
| 8597 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8598 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8599 | // Convert the int32 in r2 to the heap number in r0. r3 is corrupted. |
| 8600 | CpuFeatures::Scope scope(VFP3); |
| 8601 | __ vmov(s0, r2); |
| 8602 | if (op_ == Token::SHR) { |
| 8603 | __ vcvt_f64_u32(d0, s0); |
| 8604 | } else { |
| 8605 | __ vcvt_f64_s32(d0, s0); |
| 8606 | } |
| 8607 | __ sub(r3, r0, Operand(kHeapObjectTag)); |
| 8608 | __ vstr(d0, r3, HeapNumber::kValueOffset); |
| 8609 | __ Ret(); |
| 8610 | } else { |
| 8611 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 8612 | // r3 as scratch. r0 is preserved and returned. |
| 8613 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 8614 | __ TailCallStub(&stub); |
| 8615 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8616 | |
| 8617 | if (mode_ != NO_OVERWRITE) { |
| 8618 | __ bind(&have_to_allocate); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8619 | // Get a new heap number in r5. r4 and r7 are scratch. |
| 8620 | __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8621 | __ jmp(&got_a_heap_number); |
| 8622 | } |
| 8623 | |
| 8624 | // If all else failed then we go to the runtime system. |
| 8625 | __ bind(&slow); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8626 | __ Push(lhs, rhs); // Restore stack. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8627 | switch (op_) { |
| 8628 | case Token::BIT_OR: |
| 8629 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 8630 | break; |
| 8631 | case Token::BIT_AND: |
| 8632 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 8633 | break; |
| 8634 | case Token::BIT_XOR: |
| 8635 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 8636 | break; |
| 8637 | case Token::SAR: |
| 8638 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 8639 | break; |
| 8640 | case Token::SHR: |
| 8641 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 8642 | break; |
| 8643 | case Token::SHL: |
| 8644 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 8645 | break; |
| 8646 | default: |
| 8647 | UNREACHABLE(); |
| 8648 | } |
| 8649 | } |
| 8650 | |
| 8651 | |
| 8652 | // Can we multiply by x with max two shifts and an add. |
| 8653 | // This answers yes to all integers from 2 to 10. |
| 8654 | static bool IsEasyToMultiplyBy(int x) { |
| 8655 | if (x < 2) return false; // Avoid special cases. |
| 8656 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 8657 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 8658 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 8659 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 8660 | return false; |
| 8661 | } |
| 8662 | |
| 8663 | |
| 8664 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 8665 | // Source and destination may be the same register. This routine does |
| 8666 | // not set carry and overflow the way a mul instruction would. |
| 8667 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 8668 | Register source, |
| 8669 | Register destination, |
| 8670 | int known_int) { |
| 8671 | if (IsPowerOf2(known_int)) { |
| 8672 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 8673 | } else if (PopCountLessThanEqual2(known_int)) { |
| 8674 | int first_bit = BitPosition(known_int); |
| 8675 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 8676 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 8677 | if (first_bit != 0) { |
| 8678 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 8679 | } |
| 8680 | } else { |
| 8681 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 8682 | int the_bit = BitPosition(known_int + 1); |
| 8683 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 8684 | } |
| 8685 | } |
| 8686 | |
| 8687 | |
| 8688 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 8689 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 8690 | // a result that needs shifting. |
| 8691 | static void MultiplyByKnownInt2( |
| 8692 | MacroAssembler* masm, |
| 8693 | Register result, |
| 8694 | Register source, |
| 8695 | Register known_int_register, // Smi tagged. |
| 8696 | int known_int, |
| 8697 | int* required_shift) { // Including Smi tag shift |
| 8698 | switch (known_int) { |
| 8699 | case 3: |
| 8700 | __ add(result, source, Operand(source, LSL, 1)); |
| 8701 | *required_shift = 1; |
| 8702 | break; |
| 8703 | case 5: |
| 8704 | __ add(result, source, Operand(source, LSL, 2)); |
| 8705 | *required_shift = 1; |
| 8706 | break; |
| 8707 | case 6: |
| 8708 | __ add(result, source, Operand(source, LSL, 1)); |
| 8709 | *required_shift = 2; |
| 8710 | break; |
| 8711 | case 7: |
| 8712 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 8713 | *required_shift = 1; |
| 8714 | break; |
| 8715 | case 9: |
| 8716 | __ add(result, source, Operand(source, LSL, 3)); |
| 8717 | *required_shift = 1; |
| 8718 | break; |
| 8719 | case 10: |
| 8720 | __ add(result, source, Operand(source, LSL, 2)); |
| 8721 | *required_shift = 2; |
| 8722 | break; |
| 8723 | default: |
| 8724 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 8725 | __ mul(result, source, known_int_register); |
| 8726 | *required_shift = 0; |
| 8727 | } |
| 8728 | } |
| 8729 | |
| 8730 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8731 | // This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3 |
| 8732 | // trick. See http://en.wikipedia.org/wiki/Divisibility_rule |
| 8733 | // Takes the sum of the digits base (mask + 1) repeatedly until we have a |
| 8734 | // number from 0 to mask. On exit the 'eq' condition flags are set if the |
| 8735 | // answer is exactly the mask. |
| 8736 | void IntegerModStub::DigitSum(MacroAssembler* masm, |
| 8737 | Register lhs, |
| 8738 | int mask, |
| 8739 | int shift, |
| 8740 | Label* entry) { |
| 8741 | ASSERT(mask > 0); |
| 8742 | ASSERT(mask <= 0xff); // This ensures we don't need ip to use it. |
| 8743 | Label loop; |
| 8744 | __ bind(&loop); |
| 8745 | __ and_(ip, lhs, Operand(mask)); |
| 8746 | __ add(lhs, ip, Operand(lhs, LSR, shift)); |
| 8747 | __ bind(entry); |
| 8748 | __ cmp(lhs, Operand(mask)); |
| 8749 | __ b(gt, &loop); |
| 8750 | } |
| 8751 | |
| 8752 | |
| 8753 | void IntegerModStub::DigitSum(MacroAssembler* masm, |
| 8754 | Register lhs, |
| 8755 | Register scratch, |
| 8756 | int mask, |
| 8757 | int shift1, |
| 8758 | int shift2, |
| 8759 | Label* entry) { |
| 8760 | ASSERT(mask > 0); |
| 8761 | ASSERT(mask <= 0xff); // This ensures we don't need ip to use it. |
| 8762 | Label loop; |
| 8763 | __ bind(&loop); |
| 8764 | __ bic(scratch, lhs, Operand(mask)); |
| 8765 | __ and_(ip, lhs, Operand(mask)); |
| 8766 | __ add(lhs, ip, Operand(lhs, LSR, shift1)); |
| 8767 | __ add(lhs, lhs, Operand(scratch, LSR, shift2)); |
| 8768 | __ bind(entry); |
| 8769 | __ cmp(lhs, Operand(mask)); |
| 8770 | __ b(gt, &loop); |
| 8771 | } |
| 8772 | |
| 8773 | |
| 8774 | // Splits the number into two halves (bottom half has shift bits). The top |
| 8775 | // half is subtracted from the bottom half. If the result is negative then |
| 8776 | // rhs is added. |
| 8777 | void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm, |
| 8778 | Register lhs, |
| 8779 | int shift, |
| 8780 | int rhs) { |
| 8781 | int mask = (1 << shift) - 1; |
| 8782 | __ and_(ip, lhs, Operand(mask)); |
| 8783 | __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC); |
| 8784 | __ add(lhs, lhs, Operand(rhs), LeaveCC, mi); |
| 8785 | } |
| 8786 | |
| 8787 | |
| 8788 | void IntegerModStub::ModReduce(MacroAssembler* masm, |
| 8789 | Register lhs, |
| 8790 | int max, |
| 8791 | int denominator) { |
| 8792 | int limit = denominator; |
| 8793 | while (limit * 2 <= max) limit *= 2; |
| 8794 | while (limit >= denominator) { |
| 8795 | __ cmp(lhs, Operand(limit)); |
| 8796 | __ sub(lhs, lhs, Operand(limit), LeaveCC, ge); |
| 8797 | limit >>= 1; |
| 8798 | } |
| 8799 | } |
| 8800 | |
| 8801 | |
| 8802 | void IntegerModStub::ModAnswer(MacroAssembler* masm, |
| 8803 | Register result, |
| 8804 | Register shift_distance, |
| 8805 | Register mask_bits, |
| 8806 | Register sum_of_digits) { |
| 8807 | __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance)); |
| 8808 | __ Ret(); |
| 8809 | } |
| 8810 | |
| 8811 | |
| 8812 | // See comment for class. |
| 8813 | void IntegerModStub::Generate(MacroAssembler* masm) { |
| 8814 | __ mov(lhs_, Operand(lhs_, LSR, shift_distance_)); |
| 8815 | __ bic(odd_number_, odd_number_, Operand(1)); |
| 8816 | __ mov(odd_number_, Operand(odd_number_, LSL, 1)); |
| 8817 | // We now have (odd_number_ - 1) * 2 in the register. |
| 8818 | // Build a switch out of branches instead of data because it avoids |
| 8819 | // having to teach the assembler about intra-code-object pointers |
| 8820 | // that are not in relative branch instructions. |
| 8821 | Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19; |
| 8822 | Label mod21, mod23, mod25; |
| 8823 | { Assembler::BlockConstPoolScope block_const_pool(masm); |
| 8824 | __ add(pc, pc, Operand(odd_number_)); |
| 8825 | // When you read pc it is always 8 ahead, but when you write it you always |
| 8826 | // write the actual value. So we put in two nops to take up the slack. |
| 8827 | __ nop(); |
| 8828 | __ nop(); |
| 8829 | __ b(&mod3); |
| 8830 | __ b(&mod5); |
| 8831 | __ b(&mod7); |
| 8832 | __ b(&mod9); |
| 8833 | __ b(&mod11); |
| 8834 | __ b(&mod13); |
| 8835 | __ b(&mod15); |
| 8836 | __ b(&mod17); |
| 8837 | __ b(&mod19); |
| 8838 | __ b(&mod21); |
| 8839 | __ b(&mod23); |
| 8840 | __ b(&mod25); |
| 8841 | } |
| 8842 | |
| 8843 | // For each denominator we find a multiple that is almost only ones |
| 8844 | // when expressed in binary. Then we do the sum-of-digits trick for |
| 8845 | // that number. If the multiple is not 1 then we have to do a little |
| 8846 | // more work afterwards to get the answer into the 0-denominator-1 |
| 8847 | // range. |
| 8848 | DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11. |
| 8849 | __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq); |
| 8850 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8851 | |
| 8852 | DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111. |
| 8853 | ModGetInRangeBySubtraction(masm, lhs_, 2, 5); |
| 8854 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8855 | |
| 8856 | DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111. |
| 8857 | __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq); |
| 8858 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8859 | |
| 8860 | DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111. |
| 8861 | ModGetInRangeBySubtraction(masm, lhs_, 3, 9); |
| 8862 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8863 | |
| 8864 | DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111. |
| 8865 | ModReduce(masm, lhs_, 0x3f, 11); |
| 8866 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8867 | |
| 8868 | DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111. |
| 8869 | ModReduce(masm, lhs_, 0xff, 13); |
| 8870 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8871 | |
| 8872 | DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111. |
| 8873 | __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq); |
| 8874 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8875 | |
| 8876 | DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111. |
| 8877 | ModGetInRangeBySubtraction(masm, lhs_, 4, 17); |
| 8878 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8879 | |
| 8880 | DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111. |
| 8881 | ModReduce(masm, lhs_, 0xff, 19); |
| 8882 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8883 | |
| 8884 | DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111. |
| 8885 | ModReduce(masm, lhs_, 0x3f, 21); |
| 8886 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8887 | |
| 8888 | DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101. |
| 8889 | ModReduce(masm, lhs_, 0xff, 23); |
| 8890 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8891 | |
| 8892 | DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101. |
| 8893 | ModReduce(masm, lhs_, 0x7f, 25); |
| 8894 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8895 | } |
| 8896 | |
| 8897 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8898 | const char* GenericBinaryOpStub::GetName() { |
| 8899 | if (name_ != NULL) return name_; |
| 8900 | const int len = 100; |
| 8901 | name_ = Bootstrapper::AllocateAutoDeletedArray(len); |
| 8902 | if (name_ == NULL) return "OOM"; |
| 8903 | const char* op_name = Token::Name(op_); |
| 8904 | const char* overwrite_name; |
| 8905 | switch (mode_) { |
| 8906 | case NO_OVERWRITE: overwrite_name = "Alloc"; break; |
| 8907 | case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; |
| 8908 | case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; |
| 8909 | default: overwrite_name = "UnknownOverwrite"; break; |
| 8910 | } |
| 8911 | |
| 8912 | OS::SNPrintF(Vector<char>(name_, len), |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8913 | "GenericBinaryOpStub_%s_%s%s_%s", |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8914 | op_name, |
| 8915 | overwrite_name, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8916 | specialized_on_rhs_ ? "_ConstantRhs" : "", |
| 8917 | BinaryOpIC::GetName(runtime_operands_type_)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8918 | return name_; |
| 8919 | } |
| 8920 | |
| 8921 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 8922 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8923 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8924 | // lhs_ : x |
| 8925 | // rhs_ : y |
| 8926 | // r0 : result |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8927 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8928 | Register result = r0; |
| 8929 | Register lhs = lhs_; |
| 8930 | Register rhs = rhs_; |
| 8931 | |
| 8932 | // This code can't cope with other register allocations yet. |
| 8933 | ASSERT(result.is(r0) && |
| 8934 | ((lhs.is(r0) && rhs.is(r1)) || |
| 8935 | (lhs.is(r1) && rhs.is(r0)))); |
| 8936 | |
| 8937 | Register smi_test_reg = VirtualFrame::scratch0(); |
| 8938 | Register scratch = VirtualFrame::scratch1(); |
| 8939 | |
| 8940 | // All ops need to know whether we are dealing with two Smis. Set up |
| 8941 | // smi_test_reg to tell us that. |
| 8942 | if (ShouldGenerateSmiCode()) { |
| 8943 | __ orr(smi_test_reg, lhs, Operand(rhs)); |
| 8944 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8945 | |
| 8946 | switch (op_) { |
| 8947 | case Token::ADD: { |
| 8948 | Label not_smi; |
| 8949 | // Fast path. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8950 | if (ShouldGenerateSmiCode()) { |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 8951 | STATIC_ASSERT(kSmiTag == 0); // Adjust code below. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8952 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 8953 | __ b(ne, ¬_smi); |
| 8954 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 8955 | // Return if no overflow. |
| 8956 | __ Ret(vc); |
| 8957 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 8958 | } |
| 8959 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::ADD); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8960 | break; |
| 8961 | } |
| 8962 | |
| 8963 | case Token::SUB: { |
| 8964 | Label not_smi; |
| 8965 | // Fast path. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8966 | if (ShouldGenerateSmiCode()) { |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 8967 | STATIC_ASSERT(kSmiTag == 0); // Adjust code below. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8968 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 8969 | __ b(ne, ¬_smi); |
| 8970 | if (lhs.is(r1)) { |
| 8971 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 8972 | // Return if no overflow. |
| 8973 | __ Ret(vc); |
| 8974 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 8975 | } else { |
| 8976 | __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically. |
| 8977 | // Return if no overflow. |
| 8978 | __ Ret(vc); |
| 8979 | __ add(r0, r0, Operand(r1)); // Revert optimistic subtract. |
| 8980 | } |
| 8981 | } |
| 8982 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::SUB); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8983 | break; |
| 8984 | } |
| 8985 | |
| 8986 | case Token::MUL: { |
| 8987 | Label not_smi, slow; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8988 | if (ShouldGenerateSmiCode()) { |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 8989 | STATIC_ASSERT(kSmiTag == 0); // adjust code below |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8990 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 8991 | Register scratch2 = smi_test_reg; |
| 8992 | smi_test_reg = no_reg; |
| 8993 | __ b(ne, ¬_smi); |
| 8994 | // Remove tag from one operand (but keep sign), so that result is Smi. |
| 8995 | __ mov(ip, Operand(rhs, ASR, kSmiTagSize)); |
| 8996 | // Do multiplication |
| 8997 | // scratch = lower 32 bits of ip * lhs. |
| 8998 | __ smull(scratch, scratch2, lhs, ip); |
| 8999 | // Go slow on overflows (overflow bit is not set). |
| 9000 | __ mov(ip, Operand(scratch, ASR, 31)); |
| 9001 | // No overflow if higher 33 bits are identical. |
| 9002 | __ cmp(ip, Operand(scratch2)); |
| 9003 | __ b(ne, &slow); |
| 9004 | // Go slow on zero result to handle -0. |
| 9005 | __ tst(scratch, Operand(scratch)); |
| 9006 | __ mov(result, Operand(scratch), LeaveCC, ne); |
| 9007 | __ Ret(ne); |
| 9008 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 9009 | // We know one of them was zero. |
| 9010 | __ add(scratch2, rhs, Operand(lhs), SetCC); |
| 9011 | __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 9012 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 9013 | // Slow case. We fall through here if we multiplied a negative number |
| 9014 | // with 0, because that would mean we should produce -0. |
| 9015 | __ bind(&slow); |
| 9016 | } |
| 9017 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::MUL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9018 | break; |
| 9019 | } |
| 9020 | |
| 9021 | case Token::DIV: |
| 9022 | case Token::MOD: { |
| 9023 | Label not_smi; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9024 | if (ShouldGenerateSmiCode() && specialized_on_rhs_) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9025 | Label lhs_is_unsuitable; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9026 | __ BranchOnNotSmi(lhs, ¬_smi); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9027 | if (IsPowerOf2(constant_rhs_)) { |
| 9028 | if (op_ == Token::MOD) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9029 | __ and_(rhs, |
| 9030 | lhs, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9031 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 9032 | SetCC); |
| 9033 | // We now have the answer, but if the input was negative we also |
| 9034 | // have the sign bit. Our work is done if the result is |
| 9035 | // positive or zero: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9036 | if (!rhs.is(r0)) { |
| 9037 | __ mov(r0, rhs, LeaveCC, pl); |
| 9038 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9039 | __ Ret(pl); |
| 9040 | // A mod of a negative left hand side must return a negative number. |
| 9041 | // Unfortunately if the answer is 0 then we must return -0. And we |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9042 | // already optimistically trashed rhs so we may need to restore it. |
| 9043 | __ eor(rhs, rhs, Operand(0x80000000u), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9044 | // Next two instructions are conditional on the answer being -0. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9045 | __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9046 | __ b(eq, &lhs_is_unsuitable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9047 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9048 | __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9049 | } else { |
| 9050 | ASSERT(op_ == Token::DIV); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9051 | __ tst(lhs, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9052 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9053 | __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9054 | int shift = 0; |
| 9055 | int d = constant_rhs_; |
| 9056 | while ((d & 1) == 0) { |
| 9057 | d >>= 1; |
| 9058 | shift++; |
| 9059 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9060 | __ mov(r0, Operand(lhs, LSR, shift)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9061 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 9062 | } |
| 9063 | } else { |
| 9064 | // Not a power of 2. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9065 | __ tst(lhs, Operand(0x80000000u)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9066 | __ b(ne, &lhs_is_unsuitable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9067 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 9068 | // multiplying. |
| 9069 | double divisor = 1.0 / constant_rhs_; |
| 9070 | int shift = 32; |
| 9071 | double scale = 4294967296.0; // 1 << 32. |
| 9072 | uint32_t mul; |
| 9073 | // Maximise the precision of the fixed point reciprocal. |
| 9074 | while (true) { |
| 9075 | mul = static_cast<uint32_t>(scale * divisor); |
| 9076 | if (mul >= 0x7fffffff) break; |
| 9077 | scale *= 2.0; |
| 9078 | shift++; |
| 9079 | } |
| 9080 | mul++; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9081 | Register scratch2 = smi_test_reg; |
| 9082 | smi_test_reg = no_reg; |
| 9083 | __ mov(scratch2, Operand(mul)); |
| 9084 | __ umull(scratch, scratch2, scratch2, lhs); |
| 9085 | __ mov(scratch2, Operand(scratch2, LSR, shift - 31)); |
| 9086 | // scratch2 is lhs / rhs. scratch2 is not Smi tagged. |
| 9087 | // rhs is still the known rhs. rhs is Smi tagged. |
| 9088 | // lhs is still the unkown lhs. lhs is Smi tagged. |
| 9089 | int required_scratch_shift = 0; // Including the Smi tag shift of 1. |
| 9090 | // scratch = scratch2 * rhs. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9091 | MultiplyByKnownInt2(masm, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9092 | scratch, |
| 9093 | scratch2, |
| 9094 | rhs, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9095 | constant_rhs_, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9096 | &required_scratch_shift); |
| 9097 | // scratch << required_scratch_shift is now the Smi tagged rhs * |
| 9098 | // (lhs / rhs) where / indicates integer division. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9099 | if (op_ == Token::DIV) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9100 | __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9101 | __ b(ne, &lhs_is_unsuitable); // There was a remainder. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9102 | __ mov(result, Operand(scratch2, LSL, kSmiTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9103 | } else { |
| 9104 | ASSERT(op_ == Token::MOD); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9105 | __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9106 | } |
| 9107 | } |
| 9108 | __ Ret(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9109 | __ bind(&lhs_is_unsuitable); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9110 | } else if (op_ == Token::MOD && |
| 9111 | runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS && |
| 9112 | runtime_operands_type_ != BinaryOpIC::STRINGS) { |
| 9113 | // Do generate a bit of smi code for modulus even though the default for |
| 9114 | // modulus is not to do it, but as the ARM processor has no coprocessor |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9115 | // support for modulus checking for smis makes sense. We can handle |
| 9116 | // 1 to 25 times any power of 2. This covers over half the numbers from |
| 9117 | // 1 to 100 including all of the first 25. (Actually the constants < 10 |
| 9118 | // are handled above by reciprocal multiplication. We only get here for |
| 9119 | // those cases if the right hand side is not a constant or for cases |
| 9120 | // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod |
| 9121 | // stub.) |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9122 | Label slow; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9123 | Label not_power_of_2; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9124 | ASSERT(!ShouldGenerateSmiCode()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9125 | STATIC_ASSERT(kSmiTag == 0); // Adjust code below. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9126 | // Check for two positive smis. |
| 9127 | __ orr(smi_test_reg, lhs, Operand(rhs)); |
| 9128 | __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask)); |
| 9129 | __ b(ne, &slow); |
| 9130 | // Check that rhs is a power of two and not zero. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9131 | Register mask_bits = r3; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9132 | __ sub(scratch, rhs, Operand(1), SetCC); |
| 9133 | __ b(mi, &slow); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9134 | __ and_(mask_bits, rhs, Operand(scratch), SetCC); |
| 9135 | __ b(ne, ¬_power_of_2); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9136 | // Calculate power of two modulus. |
| 9137 | __ and_(result, lhs, Operand(scratch)); |
| 9138 | __ Ret(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9139 | |
| 9140 | __ bind(¬_power_of_2); |
| 9141 | __ eor(scratch, scratch, Operand(mask_bits)); |
| 9142 | // At least two bits are set in the modulus. The high one(s) are in |
| 9143 | // mask_bits and the low one is scratch + 1. |
| 9144 | __ and_(mask_bits, scratch, Operand(lhs)); |
| 9145 | Register shift_distance = scratch; |
| 9146 | scratch = no_reg; |
| 9147 | |
| 9148 | // The rhs consists of a power of 2 multiplied by some odd number. |
| 9149 | // The power-of-2 part we handle by putting the corresponding bits |
| 9150 | // from the lhs in the mask_bits register, and the power in the |
| 9151 | // shift_distance register. Shift distance is never 0 due to Smi |
| 9152 | // tagging. |
| 9153 | __ CountLeadingZeros(r4, shift_distance, shift_distance); |
| 9154 | __ rsb(shift_distance, r4, Operand(32)); |
| 9155 | |
| 9156 | // Now we need to find out what the odd number is. The last bit is |
| 9157 | // always 1. |
| 9158 | Register odd_number = r4; |
| 9159 | __ mov(odd_number, Operand(rhs, LSR, shift_distance)); |
| 9160 | __ cmp(odd_number, Operand(25)); |
| 9161 | __ b(gt, &slow); |
| 9162 | |
| 9163 | IntegerModStub stub( |
| 9164 | result, shift_distance, odd_number, mask_bits, lhs, r5); |
| 9165 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call. |
| 9166 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9167 | __ bind(&slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9168 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9169 | HandleBinaryOpSlowCases( |
| 9170 | masm, |
| 9171 | ¬_smi, |
| 9172 | lhs, |
| 9173 | rhs, |
| 9174 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9175 | break; |
| 9176 | } |
| 9177 | |
| 9178 | case Token::BIT_OR: |
| 9179 | case Token::BIT_AND: |
| 9180 | case Token::BIT_XOR: |
| 9181 | case Token::SAR: |
| 9182 | case Token::SHR: |
| 9183 | case Token::SHL: { |
| 9184 | Label slow; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9185 | STATIC_ASSERT(kSmiTag == 0); // adjust code below |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9186 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9187 | __ b(ne, &slow); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9188 | Register scratch2 = smi_test_reg; |
| 9189 | smi_test_reg = no_reg; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9190 | switch (op_) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9191 | case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break; |
| 9192 | case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break; |
| 9193 | case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9194 | case Token::SAR: |
| 9195 | // Remove tags from right operand. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9196 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 9197 | __ mov(result, Operand(lhs, ASR, scratch2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9198 | // Smi tag result. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9199 | __ bic(result, result, Operand(kSmiTagMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9200 | break; |
| 9201 | case Token::SHR: |
| 9202 | // Remove tags from operands. We can't do this on a 31 bit number |
| 9203 | // because then the 0s get shifted into bit 30 instead of bit 31. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9204 | __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x |
| 9205 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 9206 | __ mov(scratch, Operand(scratch, LSR, scratch2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9207 | // Unsigned shift is not allowed to produce a negative number, so |
| 9208 | // check the sign bit and the sign bit after Smi tagging. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9209 | __ tst(scratch, Operand(0xc0000000)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9210 | __ b(ne, &slow); |
| 9211 | // Smi tag result. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9212 | __ mov(result, Operand(scratch, LSL, kSmiTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9213 | break; |
| 9214 | case Token::SHL: |
| 9215 | // Remove tags from operands. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9216 | __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x |
| 9217 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 9218 | __ mov(scratch, Operand(scratch, LSL, scratch2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9219 | // Check that the signed result fits in a Smi. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9220 | __ add(scratch2, scratch, Operand(0x40000000), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9221 | __ b(mi, &slow); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9222 | __ mov(result, Operand(scratch, LSL, kSmiTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9223 | break; |
| 9224 | default: UNREACHABLE(); |
| 9225 | } |
| 9226 | __ Ret(); |
| 9227 | __ bind(&slow); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9228 | HandleNonSmiBitwiseOp(masm, lhs, rhs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9229 | break; |
| 9230 | } |
| 9231 | |
| 9232 | default: UNREACHABLE(); |
| 9233 | } |
| 9234 | // This code should be unreachable. |
| 9235 | __ stop("Unreachable"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9236 | |
| 9237 | // Generate an unreachable reference to the DEFAULT stub so that it can be |
| 9238 | // found at the end of this stub when clearing ICs at GC. |
| 9239 | // TODO(kaznacheev): Check performance impact and get rid of this. |
| 9240 | if (runtime_operands_type_ != BinaryOpIC::DEFAULT) { |
| 9241 | GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT); |
| 9242 | __ CallStub(&uninit); |
| 9243 | } |
| 9244 | } |
| 9245 | |
| 9246 | |
| 9247 | void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) { |
| 9248 | Label get_result; |
| 9249 | |
| 9250 | __ Push(r1, r0); |
| 9251 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 9252 | __ mov(r2, Operand(Smi::FromInt(MinorKey()))); |
| 9253 | __ mov(r1, Operand(Smi::FromInt(op_))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9254 | __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_))); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 9255 | __ Push(r2, r1, r0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9256 | |
| 9257 | __ TailCallExternalReference( |
| 9258 | ExternalReference(IC_Utility(IC::kBinaryOp_Patch)), |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 9259 | 5, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9260 | 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9261 | } |
| 9262 | |
| 9263 | |
| 9264 | Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) { |
| 9265 | GenericBinaryOpStub stub(key, type_info); |
| 9266 | return stub.GetCode(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9267 | } |
| 9268 | |
| 9269 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9270 | void TranscendentalCacheStub::Generate(MacroAssembler* masm) { |
| 9271 | // Argument is a number and is on stack and in r0. |
| 9272 | Label runtime_call; |
| 9273 | Label input_not_smi; |
| 9274 | Label loaded; |
| 9275 | |
| 9276 | if (CpuFeatures::IsSupported(VFP3)) { |
| 9277 | // Load argument and check if it is a smi. |
| 9278 | __ BranchOnNotSmi(r0, &input_not_smi); |
| 9279 | |
| 9280 | CpuFeatures::Scope scope(VFP3); |
| 9281 | // Input is a smi. Convert to double and load the low and high words |
| 9282 | // of the double into r2, r3. |
| 9283 | __ IntegerToDoubleConversionWithVFP3(r0, r3, r2); |
| 9284 | __ b(&loaded); |
| 9285 | |
| 9286 | __ bind(&input_not_smi); |
| 9287 | // Check if input is a HeapNumber. |
| 9288 | __ CheckMap(r0, |
| 9289 | r1, |
| 9290 | Heap::kHeapNumberMapRootIndex, |
| 9291 | &runtime_call, |
| 9292 | true); |
| 9293 | // Input is a HeapNumber. Load it to a double register and store the |
| 9294 | // low and high words into r2, r3. |
| 9295 | __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 9296 | |
| 9297 | __ bind(&loaded); |
| 9298 | // r2 = low 32 bits of double value |
| 9299 | // r3 = high 32 bits of double value |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9300 | // Compute hash (the shifts are arithmetic): |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9301 | // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1); |
| 9302 | __ eor(r1, r2, Operand(r3)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9303 | __ eor(r1, r1, Operand(r1, ASR, 16)); |
| 9304 | __ eor(r1, r1, Operand(r1, ASR, 8)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9305 | ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9306 | __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9307 | |
| 9308 | // r2 = low 32 bits of double value. |
| 9309 | // r3 = high 32 bits of double value. |
| 9310 | // r1 = TranscendentalCache::hash(double value). |
| 9311 | __ mov(r0, |
| 9312 | Operand(ExternalReference::transcendental_cache_array_address())); |
| 9313 | // r0 points to cache array. |
| 9314 | __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0]))); |
| 9315 | // r0 points to the cache for the type type_. |
| 9316 | // If NULL, the cache hasn't been initialized yet, so go through runtime. |
| 9317 | __ cmp(r0, Operand(0)); |
| 9318 | __ b(eq, &runtime_call); |
| 9319 | |
| 9320 | #ifdef DEBUG |
| 9321 | // Check that the layout of cache elements match expectations. |
| 9322 | { TranscendentalCache::Element test_elem[2]; |
| 9323 | char* elem_start = reinterpret_cast<char*>(&test_elem[0]); |
| 9324 | char* elem2_start = reinterpret_cast<char*>(&test_elem[1]); |
| 9325 | char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0])); |
| 9326 | char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1])); |
| 9327 | char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output)); |
| 9328 | CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer. |
| 9329 | CHECK_EQ(0, elem_in0 - elem_start); |
| 9330 | CHECK_EQ(kIntSize, elem_in1 - elem_start); |
| 9331 | CHECK_EQ(2 * kIntSize, elem_out - elem_start); |
| 9332 | } |
| 9333 | #endif |
| 9334 | |
| 9335 | // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12]. |
| 9336 | __ add(r1, r1, Operand(r1, LSL, 1)); |
| 9337 | __ add(r0, r0, Operand(r1, LSL, 2)); |
| 9338 | // Check if cache matches: Double value is stored in uint32_t[2] array. |
| 9339 | __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit()); |
| 9340 | __ cmp(r2, r4); |
| 9341 | __ b(ne, &runtime_call); |
| 9342 | __ cmp(r3, r5); |
| 9343 | __ b(ne, &runtime_call); |
| 9344 | // Cache hit. Load result, pop argument and return. |
| 9345 | __ mov(r0, Operand(r6)); |
| 9346 | __ pop(); |
| 9347 | __ Ret(); |
| 9348 | } |
| 9349 | |
| 9350 | __ bind(&runtime_call); |
| 9351 | __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1); |
| 9352 | } |
| 9353 | |
| 9354 | |
| 9355 | Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() { |
| 9356 | switch (type_) { |
| 9357 | // Add more cases when necessary. |
| 9358 | case TranscendentalCache::SIN: return Runtime::kMath_sin; |
| 9359 | case TranscendentalCache::COS: return Runtime::kMath_cos; |
| 9360 | default: |
| 9361 | UNIMPLEMENTED(); |
| 9362 | return Runtime::kAbort; |
| 9363 | } |
| 9364 | } |
| 9365 | |
| 9366 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9367 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 9368 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 9369 | // argument, so give it a Smi. |
| 9370 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 9371 | __ push(r0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9372 | __ TailCallRuntime(Runtime::kStackGuard, 1, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9373 | |
| 9374 | __ StubReturn(1); |
| 9375 | } |
| 9376 | |
| 9377 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9378 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9379 | Label slow, done; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9380 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9381 | Register heap_number_map = r6; |
| 9382 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 9383 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9384 | if (op_ == Token::SUB) { |
| 9385 | // Check whether the value is a smi. |
| 9386 | Label try_float; |
| 9387 | __ tst(r0, Operand(kSmiTagMask)); |
| 9388 | __ b(ne, &try_float); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9389 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9390 | // Go slow case if the value of the expression is zero |
| 9391 | // to make sure that we switch between 0 and -0. |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 9392 | if (negative_zero_ == kStrictNegativeZero) { |
| 9393 | // If we have to check for zero, then we can check for the max negative |
| 9394 | // smi while we are at it. |
| 9395 | __ bic(ip, r0, Operand(0x80000000), SetCC); |
| 9396 | __ b(eq, &slow); |
| 9397 | __ rsb(r0, r0, Operand(0)); |
| 9398 | __ StubReturn(1); |
| 9399 | } else { |
| 9400 | // The value of the expression is a smi and 0 is OK for -0. Try |
| 9401 | // optimistic subtraction '0 - value'. |
| 9402 | __ rsb(r0, r0, Operand(0), SetCC); |
| 9403 | __ StubReturn(1, vc); |
| 9404 | // We don't have to reverse the optimistic neg since the only case |
| 9405 | // where we fall through is the minimum negative Smi, which is the case |
| 9406 | // where the neg leaves the register unchanged. |
| 9407 | __ jmp(&slow); // Go slow on max negative Smi. |
| 9408 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9409 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9410 | __ bind(&try_float); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9411 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 9412 | __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 9413 | __ cmp(r1, heap_number_map); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9414 | __ b(ne, &slow); |
| 9415 | // r0 is a heap number. Get a new heap number in r1. |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 9416 | if (overwrite_ == UNARY_OVERWRITE) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9417 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 9418 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 9419 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 9420 | } else { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9421 | __ AllocateHeapNumber(r1, r2, r3, r6, &slow); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9422 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 9423 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 9424 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 9425 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 9426 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 9427 | __ mov(r0, Operand(r1)); |
| 9428 | } |
| 9429 | } else if (op_ == Token::BIT_NOT) { |
| 9430 | // Check if the operand is a heap number. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9431 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 9432 | __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 9433 | __ cmp(r1, heap_number_map); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9434 | __ b(ne, &slow); |
| 9435 | |
| 9436 | // Convert the heap number is r0 to an untagged integer in r1. |
| 9437 | GetInt32(masm, r0, r1, r2, r3, &slow); |
| 9438 | |
| 9439 | // Do the bitwise operation (move negated) and check if the result |
| 9440 | // fits in a smi. |
| 9441 | Label try_float; |
| 9442 | __ mvn(r1, Operand(r1)); |
| 9443 | __ add(r2, r1, Operand(0x40000000), SetCC); |
| 9444 | __ b(mi, &try_float); |
| 9445 | __ mov(r0, Operand(r1, LSL, kSmiTagSize)); |
| 9446 | __ b(&done); |
| 9447 | |
| 9448 | __ bind(&try_float); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 9449 | if (!overwrite_ == UNARY_OVERWRITE) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9450 | // Allocate a fresh heap number, but don't overwrite r0 until |
| 9451 | // we're sure we can do it without going through the slow case |
| 9452 | // that needs the value in r0. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9453 | __ AllocateHeapNumber(r2, r3, r4, r6, &slow); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9454 | __ mov(r0, Operand(r2)); |
| 9455 | } |
| 9456 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9457 | if (CpuFeatures::IsSupported(VFP3)) { |
| 9458 | // Convert the int32 in r1 to the heap number in r0. r2 is corrupted. |
| 9459 | CpuFeatures::Scope scope(VFP3); |
| 9460 | __ vmov(s0, r1); |
| 9461 | __ vcvt_f64_s32(d0, s0); |
| 9462 | __ sub(r2, r0, Operand(kHeapObjectTag)); |
| 9463 | __ vstr(d0, r2, HeapNumber::kValueOffset); |
| 9464 | } else { |
| 9465 | // WriteInt32ToHeapNumberStub does not trigger GC, so we do not |
| 9466 | // have to set up a frame. |
| 9467 | WriteInt32ToHeapNumberStub stub(r1, r0, r2); |
| 9468 | __ push(lr); |
| 9469 | __ Call(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 9470 | __ pop(lr); |
| 9471 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9472 | } else { |
| 9473 | UNIMPLEMENTED(); |
| 9474 | } |
| 9475 | |
| 9476 | __ bind(&done); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9477 | __ StubReturn(1); |
| 9478 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9479 | // Handle the slow case by jumping to the JavaScript builtin. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9480 | __ bind(&slow); |
| 9481 | __ push(r0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9482 | switch (op_) { |
| 9483 | case Token::SUB: |
| 9484 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 9485 | break; |
| 9486 | case Token::BIT_NOT: |
| 9487 | __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS); |
| 9488 | break; |
| 9489 | default: |
| 9490 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9491 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9492 | } |
| 9493 | |
| 9494 | |
| 9495 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
| 9496 | // r0 holds the exception. |
| 9497 | |
| 9498 | // Adjust this code if not the case. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9499 | STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9500 | |
| 9501 | // Drop the sp to the top of the handler. |
| 9502 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 9503 | __ ldr(sp, MemOperand(r3)); |
| 9504 | |
| 9505 | // Restore the next handler and frame pointer, discard handler state. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9506 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9507 | __ pop(r2); |
| 9508 | __ str(r2, MemOperand(r3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9509 | STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9510 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 9511 | |
| 9512 | // Before returning we restore the context from the frame pointer if |
| 9513 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 9514 | // JS entry frame. |
| 9515 | __ cmp(fp, Operand(0)); |
| 9516 | // Set cp to NULL if fp is NULL. |
| 9517 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 9518 | // Restore cp otherwise. |
| 9519 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 9520 | #ifdef DEBUG |
| 9521 | if (FLAG_debug_code) { |
| 9522 | __ mov(lr, Operand(pc)); |
| 9523 | } |
| 9524 | #endif |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9525 | STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9526 | __ pop(pc); |
| 9527 | } |
| 9528 | |
| 9529 | |
| 9530 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 9531 | UncatchableExceptionType type) { |
| 9532 | // Adjust this code if not the case. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9533 | STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9534 | |
| 9535 | // Drop sp to the top stack handler. |
| 9536 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 9537 | __ ldr(sp, MemOperand(r3)); |
| 9538 | |
| 9539 | // Unwind the handlers until the ENTRY handler is found. |
| 9540 | Label loop, done; |
| 9541 | __ bind(&loop); |
| 9542 | // Load the type of the current stack handler. |
| 9543 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 9544 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
| 9545 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 9546 | __ b(eq, &done); |
| 9547 | // Fetch the next handler in the list. |
| 9548 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 9549 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
| 9550 | __ jmp(&loop); |
| 9551 | __ bind(&done); |
| 9552 | |
| 9553 | // Set the top handler address to next handler past the current ENTRY handler. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9554 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9555 | __ pop(r2); |
| 9556 | __ str(r2, MemOperand(r3)); |
| 9557 | |
| 9558 | if (type == OUT_OF_MEMORY) { |
| 9559 | // Set external caught exception to false. |
| 9560 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 9561 | __ mov(r0, Operand(false)); |
| 9562 | __ mov(r2, Operand(external_caught)); |
| 9563 | __ str(r0, MemOperand(r2)); |
| 9564 | |
| 9565 | // Set pending exception and r0 to out of memory exception. |
| 9566 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 9567 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 9568 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 9569 | __ str(r0, MemOperand(r2)); |
| 9570 | } |
| 9571 | |
| 9572 | // Stack layout at this point. See also StackHandlerConstants. |
| 9573 | // sp -> state (ENTRY) |
| 9574 | // fp |
| 9575 | // lr |
| 9576 | |
| 9577 | // Discard handler state (r2 is not used) and restore frame pointer. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9578 | STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9579 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 9580 | // Before returning we restore the context from the frame pointer if |
| 9581 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 9582 | // JS entry frame. |
| 9583 | __ cmp(fp, Operand(0)); |
| 9584 | // Set cp to NULL if fp is NULL. |
| 9585 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 9586 | // Restore cp otherwise. |
| 9587 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 9588 | #ifdef DEBUG |
| 9589 | if (FLAG_debug_code) { |
| 9590 | __ mov(lr, Operand(pc)); |
| 9591 | } |
| 9592 | #endif |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9593 | STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9594 | __ pop(pc); |
| 9595 | } |
| 9596 | |
| 9597 | |
| 9598 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 9599 | Label* throw_normal_exception, |
| 9600 | Label* throw_termination_exception, |
| 9601 | Label* throw_out_of_memory_exception, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9602 | bool do_gc, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9603 | bool always_allocate, |
| 9604 | int frame_alignment_skew) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9605 | // r0: result parameter for PerformGC, if any |
| 9606 | // r4: number of arguments including receiver (C callee-saved) |
| 9607 | // r5: pointer to builtin function (C callee-saved) |
| 9608 | // r6: pointer to the first argument (C callee-saved) |
| 9609 | |
| 9610 | if (do_gc) { |
| 9611 | // Passing r0. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9612 | __ PrepareCallCFunction(1, r1); |
| 9613 | __ CallCFunction(ExternalReference::perform_gc_function(), 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9614 | } |
| 9615 | |
| 9616 | ExternalReference scope_depth = |
| 9617 | ExternalReference::heap_always_allocate_scope_depth(); |
| 9618 | if (always_allocate) { |
| 9619 | __ mov(r0, Operand(scope_depth)); |
| 9620 | __ ldr(r1, MemOperand(r0)); |
| 9621 | __ add(r1, r1, Operand(1)); |
| 9622 | __ str(r1, MemOperand(r0)); |
| 9623 | } |
| 9624 | |
| 9625 | // Call C built-in. |
| 9626 | // r0 = argc, r1 = argv |
| 9627 | __ mov(r0, Operand(r4)); |
| 9628 | __ mov(r1, Operand(r6)); |
| 9629 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9630 | int frame_alignment = MacroAssembler::ActivationFrameAlignment(); |
| 9631 | int frame_alignment_mask = frame_alignment - 1; |
| 9632 | #if defined(V8_HOST_ARCH_ARM) |
| 9633 | if (FLAG_debug_code) { |
| 9634 | if (frame_alignment > kPointerSize) { |
| 9635 | Label alignment_as_expected; |
| 9636 | ASSERT(IsPowerOf2(frame_alignment)); |
| 9637 | __ sub(r2, sp, Operand(frame_alignment_skew)); |
| 9638 | __ tst(r2, Operand(frame_alignment_mask)); |
| 9639 | __ b(eq, &alignment_as_expected); |
| 9640 | // Don't use Check here, as it will call Runtime_Abort re-entering here. |
| 9641 | __ stop("Unexpected alignment"); |
| 9642 | __ bind(&alignment_as_expected); |
| 9643 | } |
| 9644 | } |
| 9645 | #endif |
| 9646 | |
| 9647 | // Just before the call (jump) below lr is pushed, so the actual alignment is |
| 9648 | // adding one to the current skew. |
| 9649 | int alignment_before_call = |
| 9650 | (frame_alignment_skew + kPointerSize) & frame_alignment_mask; |
| 9651 | if (alignment_before_call > 0) { |
| 9652 | // Push until the alignment before the call is met. |
| 9653 | __ mov(r2, Operand(0)); |
| 9654 | for (int i = alignment_before_call; |
| 9655 | (i & frame_alignment_mask) != 0; |
| 9656 | i += kPointerSize) { |
| 9657 | __ push(r2); |
| 9658 | } |
| 9659 | } |
| 9660 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9661 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 9662 | // frames, we need to know where the return address is. Right now, |
| 9663 | // we push it on the stack to be able to find it again, but we never |
| 9664 | // restore from it in case of changes, which makes it impossible to |
| 9665 | // support moving the C entry code stub. This should be fixed, but currently |
| 9666 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 9667 | // sequence that it is not moving ever. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9668 | masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9669 | masm->push(lr); |
| 9670 | masm->Jump(r5); |
| 9671 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9672 | // Restore sp back to before aligning the stack. |
| 9673 | if (alignment_before_call > 0) { |
| 9674 | __ add(sp, sp, Operand(alignment_before_call)); |
| 9675 | } |
| 9676 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9677 | if (always_allocate) { |
| 9678 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 9679 | // though (contain the result). |
| 9680 | __ mov(r2, Operand(scope_depth)); |
| 9681 | __ ldr(r3, MemOperand(r2)); |
| 9682 | __ sub(r3, r3, Operand(1)); |
| 9683 | __ str(r3, MemOperand(r2)); |
| 9684 | } |
| 9685 | |
| 9686 | // check for failure result |
| 9687 | Label failure_returned; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9688 | STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9689 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 9690 | __ add(r2, r0, Operand(1)); |
| 9691 | __ tst(r2, Operand(kFailureTagMask)); |
| 9692 | __ b(eq, &failure_returned); |
| 9693 | |
| 9694 | // Exit C frame and return. |
| 9695 | // r0:r1: result |
| 9696 | // sp: stack pointer |
| 9697 | // fp: frame pointer |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9698 | __ LeaveExitFrame(mode_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9699 | |
| 9700 | // check if we should retry or throw exception |
| 9701 | Label retry; |
| 9702 | __ bind(&failure_returned); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 9703 | STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9704 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 9705 | __ b(eq, &retry); |
| 9706 | |
| 9707 | // Special handling of out of memory exceptions. |
| 9708 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 9709 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 9710 | __ b(eq, throw_out_of_memory_exception); |
| 9711 | |
| 9712 | // Retrieve the pending exception and clear the variable. |
| 9713 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 9714 | __ ldr(r3, MemOperand(ip)); |
| 9715 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 9716 | __ ldr(r0, MemOperand(ip)); |
| 9717 | __ str(r3, MemOperand(ip)); |
| 9718 | |
| 9719 | // Special handling of termination exceptions which are uncatchable |
| 9720 | // by javascript code. |
| 9721 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 9722 | __ b(eq, throw_termination_exception); |
| 9723 | |
| 9724 | // Handle normal exception. |
| 9725 | __ jmp(throw_normal_exception); |
| 9726 | |
| 9727 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 9728 | } |
| 9729 | |
| 9730 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9731 | void CEntryStub::Generate(MacroAssembler* masm) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9732 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 9733 | // r0: number of arguments including receiver |
| 9734 | // r1: pointer to builtin function |
| 9735 | // fp: frame pointer (restored after C call) |
| 9736 | // sp: stack pointer (restored as callee's sp after C call) |
| 9737 | // cp: current context (C callee-saved) |
| 9738 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9739 | // Result returned in r0 or r0+r1 by default. |
| 9740 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9741 | // NOTE: Invocations of builtins may return failure objects |
| 9742 | // instead of a proper result. The builtin entry handles |
| 9743 | // this by performing a garbage collection and retrying the |
| 9744 | // builtin once. |
| 9745 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9746 | // Enter the exit frame that transitions from JavaScript to C++. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9747 | __ EnterExitFrame(mode_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9748 | |
| 9749 | // r4: number of arguments (C callee-saved) |
| 9750 | // r5: pointer to builtin function (C callee-saved) |
| 9751 | // r6: pointer to first argument (C callee-saved) |
| 9752 | |
| 9753 | Label throw_normal_exception; |
| 9754 | Label throw_termination_exception; |
| 9755 | Label throw_out_of_memory_exception; |
| 9756 | |
| 9757 | // Call into the runtime system. |
| 9758 | GenerateCore(masm, |
| 9759 | &throw_normal_exception, |
| 9760 | &throw_termination_exception, |
| 9761 | &throw_out_of_memory_exception, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9762 | false, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9763 | false, |
| 9764 | -kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9765 | |
| 9766 | // Do space-specific GC and retry runtime call. |
| 9767 | GenerateCore(masm, |
| 9768 | &throw_normal_exception, |
| 9769 | &throw_termination_exception, |
| 9770 | &throw_out_of_memory_exception, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9771 | true, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9772 | false, |
| 9773 | 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9774 | |
| 9775 | // Do full GC and retry runtime call one final time. |
| 9776 | Failure* failure = Failure::InternalError(); |
| 9777 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 9778 | GenerateCore(masm, |
| 9779 | &throw_normal_exception, |
| 9780 | &throw_termination_exception, |
| 9781 | &throw_out_of_memory_exception, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9782 | true, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9783 | true, |
| 9784 | kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9785 | |
| 9786 | __ bind(&throw_out_of_memory_exception); |
| 9787 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 9788 | |
| 9789 | __ bind(&throw_termination_exception); |
| 9790 | GenerateThrowUncatchable(masm, TERMINATION); |
| 9791 | |
| 9792 | __ bind(&throw_normal_exception); |
| 9793 | GenerateThrowTOS(masm); |
| 9794 | } |
| 9795 | |
| 9796 | |
| 9797 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 9798 | // r0: code entry |
| 9799 | // r1: function |
| 9800 | // r2: receiver |
| 9801 | // r3: argc |
| 9802 | // [sp+0]: argv |
| 9803 | |
| 9804 | Label invoke, exit; |
| 9805 | |
| 9806 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 9807 | // No need to save register-passed args |
| 9808 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
| 9809 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 9810 | |
| 9811 | // Get address of argv, see stm above. |
| 9812 | // r0: code entry |
| 9813 | // r1: function |
| 9814 | // r2: receiver |
| 9815 | // r3: argc |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9816 | __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9817 | |
| 9818 | // Push a frame with special values setup to mark it as an entry frame. |
| 9819 | // r0: code entry |
| 9820 | // r1: function |
| 9821 | // r2: receiver |
| 9822 | // r3: argc |
| 9823 | // r4: argv |
| 9824 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 9825 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 9826 | __ mov(r7, Operand(Smi::FromInt(marker))); |
| 9827 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 9828 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 9829 | __ ldr(r5, MemOperand(r5)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9830 | __ Push(r8, r7, r6, r5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9831 | |
| 9832 | // Setup frame pointer for the frame to be pushed. |
| 9833 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 9834 | |
| 9835 | // Call a faked try-block that does the invoke. |
| 9836 | __ bl(&invoke); |
| 9837 | |
| 9838 | // Caught exception: Store result (exception) in the pending |
| 9839 | // exception field in the JSEnv and return a failure sentinel. |
| 9840 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 9841 | // sets it to 0 to signal the existence of the JSEntry frame. |
| 9842 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 9843 | __ str(r0, MemOperand(ip)); |
| 9844 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 9845 | __ b(&exit); |
| 9846 | |
| 9847 | // Invoke: Link this frame into the handler chain. |
| 9848 | __ bind(&invoke); |
| 9849 | // Must preserve r0-r4, r5-r7 are available. |
| 9850 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
| 9851 | // If an exception not caught by another handler occurs, this handler |
| 9852 | // returns control to the code after the bl(&invoke) above, which |
| 9853 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 9854 | // saved values before returning a failure to C. |
| 9855 | |
| 9856 | // Clear any pending exceptions. |
| 9857 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 9858 | __ ldr(r5, MemOperand(ip)); |
| 9859 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 9860 | __ str(r5, MemOperand(ip)); |
| 9861 | |
| 9862 | // Invoke the function by calling through JS entry trampoline builtin. |
| 9863 | // Notice that we cannot store a reference to the trampoline code directly in |
| 9864 | // this stub, because runtime stubs are not traversed when doing GC. |
| 9865 | |
| 9866 | // Expected registers by Builtins::JSEntryTrampoline |
| 9867 | // r0: code entry |
| 9868 | // r1: function |
| 9869 | // r2: receiver |
| 9870 | // r3: argc |
| 9871 | // r4: argv |
| 9872 | if (is_construct) { |
| 9873 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 9874 | __ mov(ip, Operand(construct_entry)); |
| 9875 | } else { |
| 9876 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 9877 | __ mov(ip, Operand(entry)); |
| 9878 | } |
| 9879 | __ ldr(ip, MemOperand(ip)); // deref address |
| 9880 | |
| 9881 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 9882 | // macro for the add instruction because we don't want the coverage tool |
| 9883 | // inserting instructions here after we read the pc. |
| 9884 | __ mov(lr, Operand(pc)); |
| 9885 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 9886 | |
| 9887 | // Unlink this frame from the handler chain. When reading the |
| 9888 | // address of the next handler, there is no need to use the address |
| 9889 | // displacement since the current stack pointer (sp) points directly |
| 9890 | // to the stack handler. |
| 9891 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 9892 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 9893 | __ str(r3, MemOperand(ip)); |
| 9894 | // No need to restore registers |
| 9895 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 9896 | |
| 9897 | |
| 9898 | __ bind(&exit); // r0 holds result |
| 9899 | // Restore the top frame descriptors from the stack. |
| 9900 | __ pop(r3); |
| 9901 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 9902 | __ str(r3, MemOperand(ip)); |
| 9903 | |
| 9904 | // Reset the stack to the callee saved registers. |
| 9905 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 9906 | |
| 9907 | // Restore callee-saved registers and return. |
| 9908 | #ifdef DEBUG |
| 9909 | if (FLAG_debug_code) { |
| 9910 | __ mov(lr, Operand(pc)); |
| 9911 | } |
| 9912 | #endif |
| 9913 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 9914 | } |
| 9915 | |
| 9916 | |
| 9917 | // This stub performs an instanceof, calling the builtin function if |
| 9918 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 9919 | // be an instance of (these are fetched from the stack). |
| 9920 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 9921 | // Get the object - slow case for smis (we may need to throw an exception |
| 9922 | // depending on the rhs). |
| 9923 | Label slow, loop, is_instance, is_not_instance; |
| 9924 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 9925 | __ BranchOnSmi(r0, &slow); |
| 9926 | |
| 9927 | // Check that the left hand is a JS object and put map in r3. |
| 9928 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 9929 | __ b(lt, &slow); |
| 9930 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 9931 | __ b(gt, &slow); |
| 9932 | |
| 9933 | // Get the prototype of the function (r4 is result, r2 is scratch). |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 9934 | __ ldr(r1, MemOperand(sp, 0)); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 9935 | // r1 is function, r3 is map. |
| 9936 | |
| 9937 | // Look up the function and the map in the instanceof cache. |
| 9938 | Label miss; |
| 9939 | __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex); |
| 9940 | __ cmp(r1, ip); |
| 9941 | __ b(ne, &miss); |
| 9942 | __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex); |
| 9943 | __ cmp(r3, ip); |
| 9944 | __ b(ne, &miss); |
| 9945 | __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex); |
| 9946 | __ pop(); |
| 9947 | __ pop(); |
| 9948 | __ mov(pc, Operand(lr)); |
| 9949 | |
| 9950 | __ bind(&miss); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9951 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 9952 | |
| 9953 | // Check that the function prototype is a JS object. |
| 9954 | __ BranchOnSmi(r4, &slow); |
| 9955 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 9956 | __ b(lt, &slow); |
| 9957 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 9958 | __ b(gt, &slow); |
| 9959 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 9960 | __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex); |
| 9961 | __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex); |
| 9962 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9963 | // Register mapping: r3 is object map and r4 is function prototype. |
| 9964 | // Get prototype of object into r2. |
| 9965 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 9966 | |
| 9967 | // Loop through the prototype chain looking for the function prototype. |
| 9968 | __ bind(&loop); |
| 9969 | __ cmp(r2, Operand(r4)); |
| 9970 | __ b(eq, &is_instance); |
| 9971 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 9972 | __ cmp(r2, ip); |
| 9973 | __ b(eq, &is_not_instance); |
| 9974 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 9975 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 9976 | __ jmp(&loop); |
| 9977 | |
| 9978 | __ bind(&is_instance); |
| 9979 | __ mov(r0, Operand(Smi::FromInt(0))); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 9980 | __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9981 | __ pop(); |
| 9982 | __ pop(); |
| 9983 | __ mov(pc, Operand(lr)); // Return. |
| 9984 | |
| 9985 | __ bind(&is_not_instance); |
| 9986 | __ mov(r0, Operand(Smi::FromInt(1))); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 9987 | __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9988 | __ pop(); |
| 9989 | __ pop(); |
| 9990 | __ mov(pc, Operand(lr)); // Return. |
| 9991 | |
| 9992 | // Slow-case. Tail call builtin. |
| 9993 | __ bind(&slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9994 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 9995 | } |
| 9996 | |
| 9997 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9998 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 9999 | // The displacement is the offset of the last parameter (if any) |
| 10000 | // relative to the frame pointer. |
| 10001 | static const int kDisplacement = |
| 10002 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
| 10003 | |
| 10004 | // Check that the key is a smi. |
| 10005 | Label slow; |
| 10006 | __ BranchOnNotSmi(r1, &slow); |
| 10007 | |
| 10008 | // Check if the calling frame is an arguments adaptor frame. |
| 10009 | Label adaptor; |
| 10010 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 10011 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 10012 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 10013 | __ b(eq, &adaptor); |
| 10014 | |
| 10015 | // Check index against formal parameters count limit passed in |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 10016 | // through register r0. Use unsigned comparison to get negative |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10017 | // check for free. |
| 10018 | __ cmp(r1, r0); |
| 10019 | __ b(cs, &slow); |
| 10020 | |
| 10021 | // Read the argument from the stack and return it. |
| 10022 | __ sub(r3, r0, r1); |
| 10023 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 10024 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 10025 | __ Jump(lr); |
| 10026 | |
| 10027 | // Arguments adaptor case: Check index against actual arguments |
| 10028 | // limit found in the arguments adaptor frame. Use unsigned |
| 10029 | // comparison to get negative check for free. |
| 10030 | __ bind(&adaptor); |
| 10031 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 10032 | __ cmp(r1, r0); |
| 10033 | __ b(cs, &slow); |
| 10034 | |
| 10035 | // Read the argument from the adaptor frame and return it. |
| 10036 | __ sub(r3, r0, r1); |
| 10037 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 10038 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 10039 | __ Jump(lr); |
| 10040 | |
| 10041 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 10042 | // by calling the runtime system. |
| 10043 | __ bind(&slow); |
| 10044 | __ push(r1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10045 | __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10046 | } |
| 10047 | |
| 10048 | |
| 10049 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10050 | // sp[0] : number of parameters |
| 10051 | // sp[4] : receiver displacement |
| 10052 | // sp[8] : function |
| 10053 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10054 | // Check if the calling frame is an arguments adaptor frame. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10055 | Label adaptor_frame, try_allocate, runtime; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10056 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 10057 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 10058 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10059 | __ b(eq, &adaptor_frame); |
| 10060 | |
| 10061 | // Get the length from the frame. |
| 10062 | __ ldr(r1, MemOperand(sp, 0)); |
| 10063 | __ b(&try_allocate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10064 | |
| 10065 | // Patch the arguments.length and the parameters pointer. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10066 | __ bind(&adaptor_frame); |
| 10067 | __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 10068 | __ str(r1, MemOperand(sp, 0)); |
| 10069 | __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10070 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 10071 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 10072 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10073 | // Try the new space allocation. Start out with computing the size |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 10074 | // of the arguments object and the elements array in words. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10075 | Label add_arguments_object; |
| 10076 | __ bind(&try_allocate); |
| 10077 | __ cmp(r1, Operand(0)); |
| 10078 | __ b(eq, &add_arguments_object); |
| 10079 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 10080 | __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize)); |
| 10081 | __ bind(&add_arguments_object); |
| 10082 | __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize)); |
| 10083 | |
| 10084 | // Do the allocation of both objects in one go. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 10085 | __ AllocateInNewSpace( |
| 10086 | r1, |
| 10087 | r0, |
| 10088 | r2, |
| 10089 | r3, |
| 10090 | &runtime, |
| 10091 | static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10092 | |
| 10093 | // Get the arguments boilerplate from the current (global) context. |
| 10094 | int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX); |
| 10095 | __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 10096 | __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset)); |
| 10097 | __ ldr(r4, MemOperand(r4, offset)); |
| 10098 | |
| 10099 | // Copy the JS object part. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 10100 | __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10101 | |
| 10102 | // Setup the callee in-object property. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10103 | STATIC_ASSERT(Heap::arguments_callee_index == 0); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10104 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 10105 | __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize)); |
| 10106 | |
| 10107 | // Get the length (smi tagged) and set that as an in-object property too. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10108 | STATIC_ASSERT(Heap::arguments_length_index == 1); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10109 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 10110 | __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize)); |
| 10111 | |
| 10112 | // If there are no actual arguments, we're done. |
| 10113 | Label done; |
| 10114 | __ cmp(r1, Operand(0)); |
| 10115 | __ b(eq, &done); |
| 10116 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10117 | // Get the parameters pointer from the stack. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10118 | __ ldr(r2, MemOperand(sp, 1 * kPointerSize)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10119 | |
| 10120 | // Setup the elements pointer in the allocated arguments object and |
| 10121 | // initialize the header in the elements fixed array. |
| 10122 | __ add(r4, r0, Operand(Heap::kArgumentsObjectSize)); |
| 10123 | __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 10124 | __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex); |
| 10125 | __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset)); |
| 10126 | __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10127 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10128 | |
| 10129 | // Copy the fixed array slots. |
| 10130 | Label loop; |
| 10131 | // Setup r4 to point to the first array slot. |
| 10132 | __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 10133 | __ bind(&loop); |
| 10134 | // Pre-decrement r2 with kPointerSize on each iteration. |
| 10135 | // Pre-decrement in order to skip receiver. |
| 10136 | __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex)); |
| 10137 | // Post-increment r4 with kPointerSize on each iteration. |
| 10138 | __ str(r3, MemOperand(r4, kPointerSize, PostIndex)); |
| 10139 | __ sub(r1, r1, Operand(1)); |
| 10140 | __ cmp(r1, Operand(0)); |
| 10141 | __ b(ne, &loop); |
| 10142 | |
| 10143 | // Return and remove the on-stack parameters. |
| 10144 | __ bind(&done); |
| 10145 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 10146 | __ Ret(); |
| 10147 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10148 | // Do the runtime call to allocate the arguments object. |
| 10149 | __ bind(&runtime); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10150 | __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1); |
| 10151 | } |
| 10152 | |
| 10153 | |
| 10154 | void RegExpExecStub::Generate(MacroAssembler* masm) { |
| 10155 | // Just jump directly to runtime if native RegExp is not selected at compile |
| 10156 | // time or if regexp entry in generated code is turned off runtime switch or |
| 10157 | // at compilation. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 10158 | #ifdef V8_INTERPRETED_REGEXP |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10159 | __ TailCallRuntime(Runtime::kRegExpExec, 4, 1); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 10160 | #else // V8_INTERPRETED_REGEXP |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10161 | if (!FLAG_regexp_entry_native) { |
| 10162 | __ TailCallRuntime(Runtime::kRegExpExec, 4, 1); |
| 10163 | return; |
| 10164 | } |
| 10165 | |
| 10166 | // Stack frame on entry. |
| 10167 | // sp[0]: last_match_info (expected JSArray) |
| 10168 | // sp[4]: previous index |
| 10169 | // sp[8]: subject string |
| 10170 | // sp[12]: JSRegExp object |
| 10171 | |
| 10172 | static const int kLastMatchInfoOffset = 0 * kPointerSize; |
| 10173 | static const int kPreviousIndexOffset = 1 * kPointerSize; |
| 10174 | static const int kSubjectOffset = 2 * kPointerSize; |
| 10175 | static const int kJSRegExpOffset = 3 * kPointerSize; |
| 10176 | |
| 10177 | Label runtime, invoke_regexp; |
| 10178 | |
| 10179 | // Allocation of registers for this function. These are in callee save |
| 10180 | // registers and will be preserved by the call to the native RegExp code, as |
| 10181 | // this code is called using the normal C calling convention. When calling |
| 10182 | // directly from generated code the native RegExp code will not do a GC and |
| 10183 | // therefore the content of these registers are safe to use after the call. |
| 10184 | Register subject = r4; |
| 10185 | Register regexp_data = r5; |
| 10186 | Register last_match_info_elements = r6; |
| 10187 | |
| 10188 | // Ensure that a RegExp stack is allocated. |
| 10189 | ExternalReference address_of_regexp_stack_memory_address = |
| 10190 | ExternalReference::address_of_regexp_stack_memory_address(); |
| 10191 | ExternalReference address_of_regexp_stack_memory_size = |
| 10192 | ExternalReference::address_of_regexp_stack_memory_size(); |
| 10193 | __ mov(r0, Operand(address_of_regexp_stack_memory_size)); |
| 10194 | __ ldr(r0, MemOperand(r0, 0)); |
| 10195 | __ tst(r0, Operand(r0)); |
| 10196 | __ b(eq, &runtime); |
| 10197 | |
| 10198 | // Check that the first argument is a JSRegExp object. |
| 10199 | __ ldr(r0, MemOperand(sp, kJSRegExpOffset)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10200 | STATIC_ASSERT(kSmiTag == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10201 | __ tst(r0, Operand(kSmiTagMask)); |
| 10202 | __ b(eq, &runtime); |
| 10203 | __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE); |
| 10204 | __ b(ne, &runtime); |
| 10205 | |
| 10206 | // Check that the RegExp has been compiled (data contains a fixed array). |
| 10207 | __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset)); |
| 10208 | if (FLAG_debug_code) { |
| 10209 | __ tst(regexp_data, Operand(kSmiTagMask)); |
| 10210 | __ Check(nz, "Unexpected type for RegExp data, FixedArray expected"); |
| 10211 | __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE); |
| 10212 | __ Check(eq, "Unexpected type for RegExp data, FixedArray expected"); |
| 10213 | } |
| 10214 | |
| 10215 | // regexp_data: RegExp data (FixedArray) |
| 10216 | // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP. |
| 10217 | __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset)); |
| 10218 | __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP))); |
| 10219 | __ b(ne, &runtime); |
| 10220 | |
| 10221 | // regexp_data: RegExp data (FixedArray) |
| 10222 | // Check that the number of captures fit in the static offsets vector buffer. |
| 10223 | __ ldr(r2, |
| 10224 | FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset)); |
| 10225 | // Calculate number of capture registers (number_of_captures + 1) * 2. This |
| 10226 | // uses the asumption that smis are 2 * their untagged value. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10227 | STATIC_ASSERT(kSmiTag == 0); |
| 10228 | STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10229 | __ add(r2, r2, Operand(2)); // r2 was a smi. |
| 10230 | // Check that the static offsets vector buffer is large enough. |
| 10231 | __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize)); |
| 10232 | __ b(hi, &runtime); |
| 10233 | |
| 10234 | // r2: Number of capture registers |
| 10235 | // regexp_data: RegExp data (FixedArray) |
| 10236 | // Check that the second argument is a string. |
| 10237 | __ ldr(subject, MemOperand(sp, kSubjectOffset)); |
| 10238 | __ tst(subject, Operand(kSmiTagMask)); |
| 10239 | __ b(eq, &runtime); |
| 10240 | Condition is_string = masm->IsObjectStringType(subject, r0); |
| 10241 | __ b(NegateCondition(is_string), &runtime); |
| 10242 | // Get the length of the string to r3. |
| 10243 | __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset)); |
| 10244 | |
| 10245 | // r2: Number of capture registers |
| 10246 | // r3: Length of subject string as a smi |
| 10247 | // subject: Subject string |
| 10248 | // regexp_data: RegExp data (FixedArray) |
| 10249 | // Check that the third argument is a positive smi less than the subject |
| 10250 | // string length. A negative value will be greater (unsigned comparison). |
| 10251 | __ ldr(r0, MemOperand(sp, kPreviousIndexOffset)); |
| 10252 | __ tst(r0, Operand(kSmiTagMask)); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 10253 | __ b(ne, &runtime); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10254 | __ cmp(r3, Operand(r0)); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 10255 | __ b(ls, &runtime); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10256 | |
| 10257 | // r2: Number of capture registers |
| 10258 | // subject: Subject string |
| 10259 | // regexp_data: RegExp data (FixedArray) |
| 10260 | // Check that the fourth object is a JSArray object. |
| 10261 | __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset)); |
| 10262 | __ tst(r0, Operand(kSmiTagMask)); |
| 10263 | __ b(eq, &runtime); |
| 10264 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
| 10265 | __ b(ne, &runtime); |
| 10266 | // Check that the JSArray is in fast case. |
| 10267 | __ ldr(last_match_info_elements, |
| 10268 | FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 10269 | __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10270 | __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10271 | __ cmp(r0, ip); |
| 10272 | __ b(ne, &runtime); |
| 10273 | // Check that the last match info has space for the capture registers and the |
| 10274 | // additional information. |
| 10275 | __ ldr(r0, |
| 10276 | FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset)); |
| 10277 | __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10278 | __ cmp(r2, Operand(r0, ASR, kSmiTagSize)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10279 | __ b(gt, &runtime); |
| 10280 | |
| 10281 | // subject: Subject string |
| 10282 | // regexp_data: RegExp data (FixedArray) |
| 10283 | // Check the representation and encoding of the subject string. |
| 10284 | Label seq_string; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10285 | __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset)); |
| 10286 | __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 10287 | // First check for flat string. |
| 10288 | __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10289 | STATIC_ASSERT((kStringTag | kSeqStringTag) == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10290 | __ b(eq, &seq_string); |
| 10291 | |
| 10292 | // subject: Subject string |
| 10293 | // regexp_data: RegExp data (FixedArray) |
| 10294 | // Check for flat cons string. |
| 10295 | // A flat cons string is a cons string where the second part is the empty |
| 10296 | // string. In that case the subject string is just the first part of the cons |
| 10297 | // string. Also in this case the first part of the cons string is known to be |
| 10298 | // a sequential string or an external string. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10299 | STATIC_ASSERT(kExternalStringTag !=0); |
| 10300 | STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 10301 | __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10302 | __ b(ne, &runtime); |
| 10303 | __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset)); |
| 10304 | __ LoadRoot(r1, Heap::kEmptyStringRootIndex); |
| 10305 | __ cmp(r0, r1); |
| 10306 | __ b(ne, &runtime); |
| 10307 | __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset)); |
| 10308 | __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset)); |
| 10309 | __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 10310 | // Is first part a flat string? |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10311 | STATIC_ASSERT(kSeqStringTag == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10312 | __ tst(r0, Operand(kStringRepresentationMask)); |
| 10313 | __ b(nz, &runtime); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10314 | |
| 10315 | __ bind(&seq_string); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10316 | // subject: Subject string |
| 10317 | // regexp_data: RegExp data (FixedArray) |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 10318 | // r0: Instance type of subject string |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10319 | STATIC_ASSERT(4 == kAsciiStringTag); |
| 10320 | STATIC_ASSERT(kTwoByteStringTag == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10321 | // Find the code object based on the assumptions above. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 10322 | __ and_(r0, r0, Operand(kStringEncodingMask)); |
| 10323 | __ mov(r3, Operand(r0, ASR, 2), SetCC); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10324 | __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne); |
| 10325 | __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq); |
| 10326 | |
| 10327 | // Check that the irregexp code has been generated for the actual string |
| 10328 | // encoding. If it has, the field contains a code object otherwise it contains |
| 10329 | // the hole. |
| 10330 | __ CompareObjectType(r7, r0, r0, CODE_TYPE); |
| 10331 | __ b(ne, &runtime); |
| 10332 | |
| 10333 | // r3: encoding of subject string (1 if ascii, 0 if two_byte); |
| 10334 | // r7: code |
| 10335 | // subject: Subject string |
| 10336 | // regexp_data: RegExp data (FixedArray) |
| 10337 | // Load used arguments before starting to push arguments for call to native |
| 10338 | // RegExp code to avoid handling changing stack height. |
| 10339 | __ ldr(r1, MemOperand(sp, kPreviousIndexOffset)); |
| 10340 | __ mov(r1, Operand(r1, ASR, kSmiTagSize)); |
| 10341 | |
| 10342 | // r1: previous index |
| 10343 | // r3: encoding of subject string (1 if ascii, 0 if two_byte); |
| 10344 | // r7: code |
| 10345 | // subject: Subject string |
| 10346 | // regexp_data: RegExp data (FixedArray) |
| 10347 | // All checks done. Now push arguments for native regexp code. |
| 10348 | __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2); |
| 10349 | |
| 10350 | static const int kRegExpExecuteArguments = 7; |
| 10351 | __ push(lr); |
| 10352 | __ PrepareCallCFunction(kRegExpExecuteArguments, r0); |
| 10353 | |
| 10354 | // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript. |
| 10355 | __ mov(r0, Operand(1)); |
| 10356 | __ str(r0, MemOperand(sp, 2 * kPointerSize)); |
| 10357 | |
| 10358 | // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area. |
| 10359 | __ mov(r0, Operand(address_of_regexp_stack_memory_address)); |
| 10360 | __ ldr(r0, MemOperand(r0, 0)); |
| 10361 | __ mov(r2, Operand(address_of_regexp_stack_memory_size)); |
| 10362 | __ ldr(r2, MemOperand(r2, 0)); |
| 10363 | __ add(r0, r0, Operand(r2)); |
| 10364 | __ str(r0, MemOperand(sp, 1 * kPointerSize)); |
| 10365 | |
| 10366 | // Argument 5 (sp[0]): static offsets vector buffer. |
| 10367 | __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector())); |
| 10368 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 10369 | |
| 10370 | // For arguments 4 and 3 get string length, calculate start of string data and |
| 10371 | // calculate the shift of the index (0 for ASCII and 1 for two byte). |
| 10372 | __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset)); |
| 10373 | __ mov(r0, Operand(r0, ASR, kSmiTagSize)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10374 | STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10375 | __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 10376 | __ eor(r3, r3, Operand(1)); |
| 10377 | // Argument 4 (r3): End of string data |
| 10378 | // Argument 3 (r2): Start of string data |
| 10379 | __ add(r2, r9, Operand(r1, LSL, r3)); |
| 10380 | __ add(r3, r9, Operand(r0, LSL, r3)); |
| 10381 | |
| 10382 | // Argument 2 (r1): Previous index. |
| 10383 | // Already there |
| 10384 | |
| 10385 | // Argument 1 (r0): Subject string. |
| 10386 | __ mov(r0, subject); |
| 10387 | |
| 10388 | // Locate the code entry and call it. |
| 10389 | __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 10390 | __ CallCFunction(r7, kRegExpExecuteArguments); |
| 10391 | __ pop(lr); |
| 10392 | |
| 10393 | // r0: result |
| 10394 | // subject: subject string (callee saved) |
| 10395 | // regexp_data: RegExp data (callee saved) |
| 10396 | // last_match_info_elements: Last match info elements (callee saved) |
| 10397 | |
| 10398 | // Check the result. |
| 10399 | Label success; |
| 10400 | __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS)); |
| 10401 | __ b(eq, &success); |
| 10402 | Label failure; |
| 10403 | __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE)); |
| 10404 | __ b(eq, &failure); |
| 10405 | __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION)); |
| 10406 | // If not exception it can only be retry. Handle that in the runtime system. |
| 10407 | __ b(ne, &runtime); |
| 10408 | // Result must now be exception. If there is no pending exception already a |
| 10409 | // stack overflow (on the backtrack stack) was detected in RegExp code but |
| 10410 | // haven't created the exception yet. Handle that in the runtime system. |
| 10411 | // TODO(592): Rerunning the RegExp to get the stack overflow exception. |
| 10412 | __ mov(r0, Operand(ExternalReference::the_hole_value_location())); |
| 10413 | __ ldr(r0, MemOperand(r0, 0)); |
| 10414 | __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 10415 | __ ldr(r1, MemOperand(r1, 0)); |
| 10416 | __ cmp(r0, r1); |
| 10417 | __ b(eq, &runtime); |
| 10418 | __ bind(&failure); |
| 10419 | // For failure and exception return null. |
| 10420 | __ mov(r0, Operand(Factory::null_value())); |
| 10421 | __ add(sp, sp, Operand(4 * kPointerSize)); |
| 10422 | __ Ret(); |
| 10423 | |
| 10424 | // Process the result from the native regexp code. |
| 10425 | __ bind(&success); |
| 10426 | __ ldr(r1, |
| 10427 | FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset)); |
| 10428 | // Calculate number of capture registers (number_of_captures + 1) * 2. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10429 | STATIC_ASSERT(kSmiTag == 0); |
| 10430 | STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10431 | __ add(r1, r1, Operand(2)); // r1 was a smi. |
| 10432 | |
| 10433 | // r1: number of capture registers |
| 10434 | // r4: subject string |
| 10435 | // Store the capture count. |
| 10436 | __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi. |
| 10437 | __ str(r2, FieldMemOperand(last_match_info_elements, |
| 10438 | RegExpImpl::kLastCaptureCountOffset)); |
| 10439 | // Store last subject and last input. |
| 10440 | __ mov(r3, last_match_info_elements); // Moved up to reduce latency. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10441 | __ str(subject, |
| 10442 | FieldMemOperand(last_match_info_elements, |
| 10443 | RegExpImpl::kLastSubjectOffset)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 10444 | __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10445 | __ str(subject, |
| 10446 | FieldMemOperand(last_match_info_elements, |
| 10447 | RegExpImpl::kLastInputOffset)); |
| 10448 | __ mov(r3, last_match_info_elements); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 10449 | __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10450 | |
| 10451 | // Get the static offsets vector filled by the native regexp code. |
| 10452 | ExternalReference address_of_static_offsets_vector = |
| 10453 | ExternalReference::address_of_static_offsets_vector(); |
| 10454 | __ mov(r2, Operand(address_of_static_offsets_vector)); |
| 10455 | |
| 10456 | // r1: number of capture registers |
| 10457 | // r2: offsets vector |
| 10458 | Label next_capture, done; |
| 10459 | // Capture register counter starts from number of capture registers and |
| 10460 | // counts down until wraping after zero. |
| 10461 | __ add(r0, |
| 10462 | last_match_info_elements, |
| 10463 | Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag)); |
| 10464 | __ bind(&next_capture); |
| 10465 | __ sub(r1, r1, Operand(1), SetCC); |
| 10466 | __ b(mi, &done); |
| 10467 | // Read the value from the static offsets vector buffer. |
| 10468 | __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex)); |
| 10469 | // Store the smi value in the last match info. |
| 10470 | __ mov(r3, Operand(r3, LSL, kSmiTagSize)); |
| 10471 | __ str(r3, MemOperand(r0, kPointerSize, PostIndex)); |
| 10472 | __ jmp(&next_capture); |
| 10473 | __ bind(&done); |
| 10474 | |
| 10475 | // Return last match info. |
| 10476 | __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset)); |
| 10477 | __ add(sp, sp, Operand(4 * kPointerSize)); |
| 10478 | __ Ret(); |
| 10479 | |
| 10480 | // Do the runtime call to execute the regexp. |
| 10481 | __ bind(&runtime); |
| 10482 | __ TailCallRuntime(Runtime::kRegExpExec, 4, 1); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 10483 | #endif // V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10484 | } |
| 10485 | |
| 10486 | |
| 10487 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 10488 | Label slow; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 10489 | |
| 10490 | // If the receiver might be a value (string, number or boolean) check for this |
| 10491 | // and box it if it is. |
| 10492 | if (ReceiverMightBeValue()) { |
| 10493 | // Get the receiver from the stack. |
| 10494 | // function, receiver [, arguments] |
| 10495 | Label receiver_is_value, receiver_is_js_object; |
| 10496 | __ ldr(r1, MemOperand(sp, argc_ * kPointerSize)); |
| 10497 | |
| 10498 | // Check if receiver is a smi (which is a number value). |
| 10499 | __ BranchOnSmi(r1, &receiver_is_value); |
| 10500 | |
| 10501 | // Check if the receiver is a valid JS object. |
| 10502 | __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 10503 | __ b(ge, &receiver_is_js_object); |
| 10504 | |
| 10505 | // Call the runtime to box the value. |
| 10506 | __ bind(&receiver_is_value); |
| 10507 | __ EnterInternalFrame(); |
| 10508 | __ push(r1); |
| 10509 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
| 10510 | __ LeaveInternalFrame(); |
| 10511 | __ str(r0, MemOperand(sp, argc_ * kPointerSize)); |
| 10512 | |
| 10513 | __ bind(&receiver_is_js_object); |
| 10514 | } |
| 10515 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10516 | // Get the function to call from the stack. |
| 10517 | // function, receiver [, arguments] |
| 10518 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 10519 | |
| 10520 | // Check that the function is really a JavaScript function. |
| 10521 | // r1: pushed function (to be verified) |
| 10522 | __ BranchOnSmi(r1, &slow); |
| 10523 | // Get the map of the function object. |
| 10524 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
| 10525 | __ b(ne, &slow); |
| 10526 | |
| 10527 | // Fast-case: Invoke the function now. |
| 10528 | // r1: pushed function |
| 10529 | ParameterCount actual(argc_); |
| 10530 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 10531 | |
| 10532 | // Slow-case: Non-function called. |
| 10533 | __ bind(&slow); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 10534 | // CALL_NON_FUNCTION expects the non-function callee as receiver (instead |
| 10535 | // of the original receiver from the call site). |
| 10536 | __ str(r1, MemOperand(sp, argc_ * kPointerSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10537 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
| 10538 | __ mov(r2, Operand(0)); |
| 10539 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 10540 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 10541 | RelocInfo::CODE_TARGET); |
| 10542 | } |
| 10543 | |
| 10544 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10545 | // Unfortunately you have to run without snapshots to see most of these |
| 10546 | // names in the profile since most compare stubs end up in the snapshot. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 10547 | const char* CompareStub::GetName() { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10548 | ASSERT((lhs_.is(r0) && rhs_.is(r1)) || |
| 10549 | (lhs_.is(r1) && rhs_.is(r0))); |
| 10550 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10551 | if (name_ != NULL) return name_; |
| 10552 | const int kMaxNameLength = 100; |
| 10553 | name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength); |
| 10554 | if (name_ == NULL) return "OOM"; |
| 10555 | |
| 10556 | const char* cc_name; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 10557 | switch (cc_) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10558 | case lt: cc_name = "LT"; break; |
| 10559 | case gt: cc_name = "GT"; break; |
| 10560 | case le: cc_name = "LE"; break; |
| 10561 | case ge: cc_name = "GE"; break; |
| 10562 | case eq: cc_name = "EQ"; break; |
| 10563 | case ne: cc_name = "NE"; break; |
| 10564 | default: cc_name = "UnknownCondition"; break; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 10565 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10566 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10567 | const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1"; |
| 10568 | const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1"; |
| 10569 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10570 | const char* strict_name = ""; |
| 10571 | if (strict_ && (cc_ == eq || cc_ == ne)) { |
| 10572 | strict_name = "_STRICT"; |
| 10573 | } |
| 10574 | |
| 10575 | const char* never_nan_nan_name = ""; |
| 10576 | if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) { |
| 10577 | never_nan_nan_name = "_NO_NAN"; |
| 10578 | } |
| 10579 | |
| 10580 | const char* include_number_compare_name = ""; |
| 10581 | if (!include_number_compare_) { |
| 10582 | include_number_compare_name = "_NO_NUMBER"; |
| 10583 | } |
| 10584 | |
| 10585 | OS::SNPrintF(Vector<char>(name_, kMaxNameLength), |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10586 | "CompareStub_%s%s%s%s%s%s", |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10587 | cc_name, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10588 | lhs_name, |
| 10589 | rhs_name, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10590 | strict_name, |
| 10591 | never_nan_nan_name, |
| 10592 | include_number_compare_name); |
| 10593 | return name_; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 10594 | } |
| 10595 | |
| 10596 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10597 | int CompareStub::MinorKey() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10598 | // Encode the three parameters in a unique 16 bit value. To avoid duplicate |
| 10599 | // stubs the never NaN NaN condition is only taken into account if the |
| 10600 | // condition is equals. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10601 | ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12)); |
| 10602 | ASSERT((lhs_.is(r0) && rhs_.is(r1)) || |
| 10603 | (lhs_.is(r1) && rhs_.is(r0))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10604 | return ConditionField::encode(static_cast<unsigned>(cc_) >> 28) |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10605 | | RegisterField::encode(lhs_.is(r0)) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10606 | | StrictField::encode(strict_) |
| 10607 | | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false) |
| 10608 | | IncludeNumberCompareField::encode(include_number_compare_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10609 | } |
| 10610 | |
| 10611 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10612 | // StringCharCodeAtGenerator |
| 10613 | |
| 10614 | void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) { |
| 10615 | Label flat_string; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10616 | Label ascii_string; |
| 10617 | Label got_char_code; |
| 10618 | |
| 10619 | // If the receiver is a smi trigger the non-string case. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10620 | __ BranchOnSmi(object_, receiver_not_string_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10621 | |
| 10622 | // Fetch the instance type of the receiver into result register. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10623 | __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 10624 | __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10625 | // If the receiver is not a string trigger the non-string case. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10626 | __ tst(result_, Operand(kIsNotStringMask)); |
| 10627 | __ b(ne, receiver_not_string_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10628 | |
| 10629 | // If the index is non-smi trigger the non-smi case. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10630 | __ BranchOnNotSmi(index_, &index_not_smi_); |
| 10631 | |
| 10632 | // Put smi-tagged index into scratch register. |
| 10633 | __ mov(scratch_, index_); |
| 10634 | __ bind(&got_smi_index_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10635 | |
| 10636 | // Check for index out of range. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10637 | __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset)); |
| 10638 | __ cmp(ip, Operand(scratch_)); |
| 10639 | __ b(ls, index_out_of_range_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10640 | |
| 10641 | // We need special handling for non-flat strings. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10642 | STATIC_ASSERT(kSeqStringTag == 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10643 | __ tst(result_, Operand(kStringRepresentationMask)); |
| 10644 | __ b(eq, &flat_string); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10645 | |
| 10646 | // Handle non-flat strings. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10647 | __ tst(result_, Operand(kIsConsStringMask)); |
| 10648 | __ b(eq, &call_runtime_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10649 | |
| 10650 | // ConsString. |
| 10651 | // Check whether the right hand side is the empty string (i.e. if |
| 10652 | // this is really a flat string in a cons string). If that is not |
| 10653 | // the case we would rather go to the runtime system now to flatten |
| 10654 | // the string. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10655 | __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset)); |
| 10656 | __ LoadRoot(ip, Heap::kEmptyStringRootIndex); |
| 10657 | __ cmp(result_, Operand(ip)); |
| 10658 | __ b(ne, &call_runtime_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10659 | // Get the first of the two strings and load its instance type. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10660 | __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset)); |
| 10661 | __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 10662 | __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset)); |
| 10663 | // If the first cons component is also non-flat, then go to runtime. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10664 | STATIC_ASSERT(kSeqStringTag == 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10665 | __ tst(result_, Operand(kStringRepresentationMask)); |
| 10666 | __ b(nz, &call_runtime_); |
| 10667 | |
| 10668 | // Check for 1-byte or 2-byte string. |
| 10669 | __ bind(&flat_string); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10670 | STATIC_ASSERT(kAsciiStringTag != 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10671 | __ tst(result_, Operand(kStringEncodingMask)); |
| 10672 | __ b(nz, &ascii_string); |
| 10673 | |
| 10674 | // 2-byte string. |
| 10675 | // Load the 2-byte character code into the result register. We can |
| 10676 | // add without shifting since the smi tag size is the log2 of the |
| 10677 | // number of bytes in a two-byte character. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10678 | STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10679 | __ add(scratch_, object_, Operand(scratch_)); |
| 10680 | __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize)); |
| 10681 | __ jmp(&got_char_code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10682 | |
| 10683 | // ASCII string. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10684 | // Load the byte into the result register. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10685 | __ bind(&ascii_string); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10686 | __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize)); |
| 10687 | __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10688 | |
| 10689 | __ bind(&got_char_code); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10690 | __ mov(result_, Operand(result_, LSL, kSmiTagSize)); |
| 10691 | __ bind(&exit_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10692 | } |
| 10693 | |
| 10694 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10695 | void StringCharCodeAtGenerator::GenerateSlow( |
| 10696 | MacroAssembler* masm, const RuntimeCallHelper& call_helper) { |
| 10697 | __ Abort("Unexpected fallthrough to CharCodeAt slow case"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10698 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10699 | // Index is not a smi. |
| 10700 | __ bind(&index_not_smi_); |
| 10701 | // If index is a heap number, try converting it to an integer. |
| 10702 | __ CheckMap(index_, |
| 10703 | scratch_, |
| 10704 | Heap::kHeapNumberMapRootIndex, |
| 10705 | index_not_number_, |
| 10706 | true); |
| 10707 | call_helper.BeforeCall(masm); |
| 10708 | __ Push(object_, index_); |
| 10709 | __ push(index_); // Consumed by runtime conversion function. |
| 10710 | if (index_flags_ == STRING_INDEX_IS_NUMBER) { |
| 10711 | __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1); |
| 10712 | } else { |
| 10713 | ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX); |
| 10714 | // NumberToSmi discards numbers that are not exact integers. |
| 10715 | __ CallRuntime(Runtime::kNumberToSmi, 1); |
| 10716 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 10717 | // Save the conversion result before the pop instructions below |
| 10718 | // have a chance to overwrite it. |
| 10719 | __ Move(scratch_, r0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10720 | __ pop(index_); |
| 10721 | __ pop(object_); |
| 10722 | // Reload the instance type. |
| 10723 | __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 10724 | __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset)); |
| 10725 | call_helper.AfterCall(masm); |
| 10726 | // If index is still not a smi, it must be out of range. |
| 10727 | __ BranchOnNotSmi(scratch_, index_out_of_range_); |
| 10728 | // Otherwise, return to the fast path. |
| 10729 | __ jmp(&got_smi_index_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10730 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10731 | // Call runtime. We get here when the receiver is a string and the |
| 10732 | // index is a number, but the code of getting the actual character |
| 10733 | // is too complex (e.g., when the string needs to be flattened). |
| 10734 | __ bind(&call_runtime_); |
| 10735 | call_helper.BeforeCall(masm); |
| 10736 | __ Push(object_, index_); |
| 10737 | __ CallRuntime(Runtime::kStringCharCodeAt, 2); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 10738 | __ Move(result_, r0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10739 | call_helper.AfterCall(masm); |
| 10740 | __ jmp(&exit_); |
| 10741 | |
| 10742 | __ Abort("Unexpected fallthrough from CharCodeAt slow case"); |
| 10743 | } |
| 10744 | |
| 10745 | |
| 10746 | // ------------------------------------------------------------------------- |
| 10747 | // StringCharFromCodeGenerator |
| 10748 | |
| 10749 | void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10750 | // Fast case of Heap::LookupSingleCharacterStringFromCode. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10751 | STATIC_ASSERT(kSmiTag == 0); |
| 10752 | STATIC_ASSERT(kSmiShiftSize == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10753 | ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10754 | __ tst(code_, |
| 10755 | Operand(kSmiTagMask | |
| 10756 | ((~String::kMaxAsciiCharCode) << kSmiTagSize))); |
| 10757 | __ b(nz, &slow_case_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10758 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10759 | __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex); |
| 10760 | // At this point code register contains smi tagged ascii char code. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10761 | STATIC_ASSERT(kSmiTag == 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10762 | __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 10763 | __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize)); |
| 10764 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 10765 | __ cmp(result_, Operand(ip)); |
| 10766 | __ b(eq, &slow_case_); |
| 10767 | __ bind(&exit_); |
| 10768 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10769 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10770 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10771 | void StringCharFromCodeGenerator::GenerateSlow( |
| 10772 | MacroAssembler* masm, const RuntimeCallHelper& call_helper) { |
| 10773 | __ Abort("Unexpected fallthrough to CharFromCode slow case"); |
| 10774 | |
| 10775 | __ bind(&slow_case_); |
| 10776 | call_helper.BeforeCall(masm); |
| 10777 | __ push(code_); |
| 10778 | __ CallRuntime(Runtime::kCharFromCode, 1); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 10779 | __ Move(result_, r0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10780 | call_helper.AfterCall(masm); |
| 10781 | __ jmp(&exit_); |
| 10782 | |
| 10783 | __ Abort("Unexpected fallthrough from CharFromCode slow case"); |
| 10784 | } |
| 10785 | |
| 10786 | |
| 10787 | // ------------------------------------------------------------------------- |
| 10788 | // StringCharAtGenerator |
| 10789 | |
| 10790 | void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) { |
| 10791 | char_code_at_generator_.GenerateFast(masm); |
| 10792 | char_from_code_generator_.GenerateFast(masm); |
| 10793 | } |
| 10794 | |
| 10795 | |
| 10796 | void StringCharAtGenerator::GenerateSlow( |
| 10797 | MacroAssembler* masm, const RuntimeCallHelper& call_helper) { |
| 10798 | char_code_at_generator_.GenerateSlow(masm, call_helper); |
| 10799 | char_from_code_generator_.GenerateSlow(masm, call_helper); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10800 | } |
| 10801 | |
| 10802 | |
| 10803 | void StringHelper::GenerateCopyCharacters(MacroAssembler* masm, |
| 10804 | Register dest, |
| 10805 | Register src, |
| 10806 | Register count, |
| 10807 | Register scratch, |
| 10808 | bool ascii) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 10809 | Label loop; |
| 10810 | Label done; |
| 10811 | // This loop just copies one character at a time, as it is only used for very |
| 10812 | // short strings. |
| 10813 | if (!ascii) { |
| 10814 | __ add(count, count, Operand(count), SetCC); |
| 10815 | } else { |
| 10816 | __ cmp(count, Operand(0)); |
| 10817 | } |
| 10818 | __ b(eq, &done); |
| 10819 | |
| 10820 | __ bind(&loop); |
| 10821 | __ ldrb(scratch, MemOperand(src, 1, PostIndex)); |
| 10822 | // Perform sub between load and dependent store to get the load time to |
| 10823 | // complete. |
| 10824 | __ sub(count, count, Operand(1), SetCC); |
| 10825 | __ strb(scratch, MemOperand(dest, 1, PostIndex)); |
| 10826 | // last iteration. |
| 10827 | __ b(gt, &loop); |
| 10828 | |
| 10829 | __ bind(&done); |
| 10830 | } |
| 10831 | |
| 10832 | |
| 10833 | enum CopyCharactersFlags { |
| 10834 | COPY_ASCII = 1, |
| 10835 | DEST_ALWAYS_ALIGNED = 2 |
| 10836 | }; |
| 10837 | |
| 10838 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10839 | void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm, |
| 10840 | Register dest, |
| 10841 | Register src, |
| 10842 | Register count, |
| 10843 | Register scratch1, |
| 10844 | Register scratch2, |
| 10845 | Register scratch3, |
| 10846 | Register scratch4, |
| 10847 | Register scratch5, |
| 10848 | int flags) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 10849 | bool ascii = (flags & COPY_ASCII) != 0; |
| 10850 | bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0; |
| 10851 | |
| 10852 | if (dest_always_aligned && FLAG_debug_code) { |
| 10853 | // Check that destination is actually word aligned if the flag says |
| 10854 | // that it is. |
| 10855 | __ tst(dest, Operand(kPointerAlignmentMask)); |
| 10856 | __ Check(eq, "Destination of copy not aligned."); |
| 10857 | } |
| 10858 | |
| 10859 | const int kReadAlignment = 4; |
| 10860 | const int kReadAlignmentMask = kReadAlignment - 1; |
| 10861 | // Ensure that reading an entire aligned word containing the last character |
| 10862 | // of a string will not read outside the allocated area (because we pad up |
| 10863 | // to kObjectAlignment). |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 10864 | STATIC_ASSERT(kObjectAlignment >= kReadAlignment); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 10865 | // Assumes word reads and writes are little endian. |
| 10866 | // Nothing to do for zero characters. |
| 10867 | Label done; |
| 10868 | if (!ascii) { |
| 10869 | __ add(count, count, Operand(count), SetCC); |
| 10870 | } else { |
| 10871 | __ cmp(count, Operand(0)); |
| 10872 | } |
| 10873 | __ b(eq, &done); |
| 10874 | |
| 10875 | // Assume that you cannot read (or write) unaligned. |
| 10876 | Label byte_loop; |
| 10877 | // Must copy at least eight bytes, otherwise just do it one byte at a time. |
| 10878 | __ cmp(count, Operand(8)); |
| 10879 | __ add(count, dest, Operand(count)); |
| 10880 | Register limit = count; // Read until src equals this. |
| 10881 | __ b(lt, &byte_loop); |
| 10882 | |
| 10883 | if (!dest_always_aligned) { |
| 10884 | // Align dest by byte copying. Copies between zero and three bytes. |
| 10885 | __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC); |
| 10886 | Label dest_aligned; |
| 10887 | __ b(eq, &dest_aligned); |
| 10888 | __ cmp(scratch4, Operand(2)); |
| 10889 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex)); |
| 10890 | __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le); |
| 10891 | __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt); |
| 10892 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 10893 | __ strb(scratch2, MemOperand(dest, 1, PostIndex), le); |
| 10894 | __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt); |
| 10895 | __ bind(&dest_aligned); |
| 10896 | } |
| 10897 | |
| 10898 | Label simple_loop; |
| 10899 | |
| 10900 | __ sub(scratch4, dest, Operand(src)); |
| 10901 | __ and_(scratch4, scratch4, Operand(0x03), SetCC); |
| 10902 | __ b(eq, &simple_loop); |
| 10903 | // Shift register is number of bits in a source word that |
| 10904 | // must be combined with bits in the next source word in order |
| 10905 | // to create a destination word. |
| 10906 | |
| 10907 | // Complex loop for src/dst that are not aligned the same way. |
| 10908 | { |
| 10909 | Label loop; |
| 10910 | __ mov(scratch4, Operand(scratch4, LSL, 3)); |
| 10911 | Register left_shift = scratch4; |
| 10912 | __ and_(src, src, Operand(~3)); // Round down to load previous word. |
| 10913 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 10914 | // Store the "shift" most significant bits of scratch in the least |
| 10915 | // signficant bits (i.e., shift down by (32-shift)). |
| 10916 | __ rsb(scratch2, left_shift, Operand(32)); |
| 10917 | Register right_shift = scratch2; |
| 10918 | __ mov(scratch1, Operand(scratch1, LSR, right_shift)); |
| 10919 | |
| 10920 | __ bind(&loop); |
| 10921 | __ ldr(scratch3, MemOperand(src, 4, PostIndex)); |
| 10922 | __ sub(scratch5, limit, Operand(dest)); |
| 10923 | __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift)); |
| 10924 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 10925 | __ mov(scratch1, Operand(scratch3, LSR, right_shift)); |
| 10926 | // Loop if four or more bytes left to copy. |
| 10927 | // Compare to eight, because we did the subtract before increasing dst. |
| 10928 | __ sub(scratch5, scratch5, Operand(8), SetCC); |
| 10929 | __ b(ge, &loop); |
| 10930 | } |
| 10931 | // There is now between zero and three bytes left to copy (negative that |
| 10932 | // number is in scratch5), and between one and three bytes already read into |
| 10933 | // scratch1 (eight times that number in scratch4). We may have read past |
| 10934 | // the end of the string, but because objects are aligned, we have not read |
| 10935 | // past the end of the object. |
| 10936 | // Find the minimum of remaining characters to move and preloaded characters |
| 10937 | // and write those as bytes. |
| 10938 | __ add(scratch5, scratch5, Operand(4), SetCC); |
| 10939 | __ b(eq, &done); |
| 10940 | __ cmp(scratch4, Operand(scratch5, LSL, 3), ne); |
| 10941 | // Move minimum of bytes read and bytes left to copy to scratch4. |
| 10942 | __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt); |
| 10943 | // Between one and three (value in scratch5) characters already read into |
| 10944 | // scratch ready to write. |
| 10945 | __ cmp(scratch5, Operand(2)); |
| 10946 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 10947 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge); |
| 10948 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge); |
| 10949 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt); |
| 10950 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt); |
| 10951 | // Copy any remaining bytes. |
| 10952 | __ b(&byte_loop); |
| 10953 | |
| 10954 | // Simple loop. |
| 10955 | // Copy words from src to dst, until less than four bytes left. |
| 10956 | // Both src and dest are word aligned. |
| 10957 | __ bind(&simple_loop); |
| 10958 | { |
| 10959 | Label loop; |
| 10960 | __ bind(&loop); |
| 10961 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 10962 | __ sub(scratch3, limit, Operand(dest)); |
| 10963 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 10964 | // Compare to 8, not 4, because we do the substraction before increasing |
| 10965 | // dest. |
| 10966 | __ cmp(scratch3, Operand(8)); |
| 10967 | __ b(ge, &loop); |
| 10968 | } |
| 10969 | |
| 10970 | // Copy bytes from src to dst until dst hits limit. |
| 10971 | __ bind(&byte_loop); |
| 10972 | __ cmp(dest, Operand(limit)); |
| 10973 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt); |
| 10974 | __ b(ge, &done); |
| 10975 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 10976 | __ b(&byte_loop); |
| 10977 | |
| 10978 | __ bind(&done); |
| 10979 | } |
| 10980 | |
| 10981 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10982 | void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm, |
| 10983 | Register c1, |
| 10984 | Register c2, |
| 10985 | Register scratch1, |
| 10986 | Register scratch2, |
| 10987 | Register scratch3, |
| 10988 | Register scratch4, |
| 10989 | Register scratch5, |
| 10990 | Label* not_found) { |
| 10991 | // Register scratch3 is the general scratch register in this function. |
| 10992 | Register scratch = scratch3; |
| 10993 | |
| 10994 | // Make sure that both characters are not digits as such strings has a |
| 10995 | // different hash algorithm. Don't try to look for these in the symbol table. |
| 10996 | Label not_array_index; |
| 10997 | __ sub(scratch, c1, Operand(static_cast<int>('0'))); |
| 10998 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 10999 | __ b(hi, ¬_array_index); |
| 11000 | __ sub(scratch, c2, Operand(static_cast<int>('0'))); |
| 11001 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 11002 | |
| 11003 | // If check failed combine both characters into single halfword. |
| 11004 | // This is required by the contract of the method: code at the |
| 11005 | // not_found branch expects this combination in c1 register |
| 11006 | __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls); |
| 11007 | __ b(ls, not_found); |
| 11008 | |
| 11009 | __ bind(¬_array_index); |
| 11010 | // Calculate the two character string hash. |
| 11011 | Register hash = scratch1; |
| 11012 | StringHelper::GenerateHashInit(masm, hash, c1); |
| 11013 | StringHelper::GenerateHashAddCharacter(masm, hash, c2); |
| 11014 | StringHelper::GenerateHashGetHash(masm, hash); |
| 11015 | |
| 11016 | // Collect the two characters in a register. |
| 11017 | Register chars = c1; |
| 11018 | __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte)); |
| 11019 | |
| 11020 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 11021 | // hash: hash of two character string. |
| 11022 | |
| 11023 | // Load symbol table |
| 11024 | // Load address of first element of the symbol table. |
| 11025 | Register symbol_table = c2; |
| 11026 | __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex); |
| 11027 | |
| 11028 | // Load undefined value |
| 11029 | Register undefined = scratch4; |
| 11030 | __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex); |
| 11031 | |
| 11032 | // Calculate capacity mask from the symbol table capacity. |
| 11033 | Register mask = scratch2; |
| 11034 | __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset)); |
| 11035 | __ mov(mask, Operand(mask, ASR, 1)); |
| 11036 | __ sub(mask, mask, Operand(1)); |
| 11037 | |
| 11038 | // Calculate untagged address of the first element of the symbol table. |
| 11039 | Register first_symbol_table_element = symbol_table; |
| 11040 | __ add(first_symbol_table_element, symbol_table, |
| 11041 | Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag)); |
| 11042 | |
| 11043 | // Registers |
| 11044 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 11045 | // hash: hash of two character string |
| 11046 | // mask: capacity mask |
| 11047 | // first_symbol_table_element: address of the first element of |
| 11048 | // the symbol table |
| 11049 | // scratch: - |
| 11050 | |
| 11051 | // Perform a number of probes in the symbol table. |
| 11052 | static const int kProbes = 4; |
| 11053 | Label found_in_symbol_table; |
| 11054 | Label next_probe[kProbes]; |
| 11055 | for (int i = 0; i < kProbes; i++) { |
| 11056 | Register candidate = scratch5; // Scratch register contains candidate. |
| 11057 | |
| 11058 | // Calculate entry in symbol table. |
| 11059 | if (i > 0) { |
| 11060 | __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i))); |
| 11061 | } else { |
| 11062 | __ mov(candidate, hash); |
| 11063 | } |
| 11064 | |
| 11065 | __ and_(candidate, candidate, Operand(mask)); |
| 11066 | |
| 11067 | // Load the entry from the symble table. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11068 | STATIC_ASSERT(SymbolTable::kEntrySize == 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11069 | __ ldr(candidate, |
| 11070 | MemOperand(first_symbol_table_element, |
| 11071 | candidate, |
| 11072 | LSL, |
| 11073 | kPointerSizeLog2)); |
| 11074 | |
| 11075 | // If entry is undefined no string with this hash can be found. |
| 11076 | __ cmp(candidate, undefined); |
| 11077 | __ b(eq, not_found); |
| 11078 | |
| 11079 | // If length is not 2 the string is not a candidate. |
| 11080 | __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset)); |
| 11081 | __ cmp(scratch, Operand(Smi::FromInt(2))); |
| 11082 | __ b(ne, &next_probe[i]); |
| 11083 | |
| 11084 | // Check that the candidate is a non-external ascii string. |
| 11085 | __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset)); |
| 11086 | __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset)); |
| 11087 | __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, |
| 11088 | &next_probe[i]); |
| 11089 | |
| 11090 | // Check if the two characters match. |
| 11091 | // Assumes that word load is little endian. |
| 11092 | __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize)); |
| 11093 | __ cmp(chars, scratch); |
| 11094 | __ b(eq, &found_in_symbol_table); |
| 11095 | __ bind(&next_probe[i]); |
| 11096 | } |
| 11097 | |
| 11098 | // No matching 2 character string found by probing. |
| 11099 | __ jmp(not_found); |
| 11100 | |
| 11101 | // Scratch register contains result when we fall through to here. |
| 11102 | Register result = scratch; |
| 11103 | __ bind(&found_in_symbol_table); |
| 11104 | __ Move(r0, result); |
| 11105 | } |
| 11106 | |
| 11107 | |
| 11108 | void StringHelper::GenerateHashInit(MacroAssembler* masm, |
| 11109 | Register hash, |
| 11110 | Register character) { |
| 11111 | // hash = character + (character << 10); |
| 11112 | __ add(hash, character, Operand(character, LSL, 10)); |
| 11113 | // hash ^= hash >> 6; |
| 11114 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 11115 | } |
| 11116 | |
| 11117 | |
| 11118 | void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm, |
| 11119 | Register hash, |
| 11120 | Register character) { |
| 11121 | // hash += character; |
| 11122 | __ add(hash, hash, Operand(character)); |
| 11123 | // hash += hash << 10; |
| 11124 | __ add(hash, hash, Operand(hash, LSL, 10)); |
| 11125 | // hash ^= hash >> 6; |
| 11126 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 11127 | } |
| 11128 | |
| 11129 | |
| 11130 | void StringHelper::GenerateHashGetHash(MacroAssembler* masm, |
| 11131 | Register hash) { |
| 11132 | // hash += hash << 3; |
| 11133 | __ add(hash, hash, Operand(hash, LSL, 3)); |
| 11134 | // hash ^= hash >> 11; |
| 11135 | __ eor(hash, hash, Operand(hash, ASR, 11)); |
| 11136 | // hash += hash << 15; |
| 11137 | __ add(hash, hash, Operand(hash, LSL, 15), SetCC); |
| 11138 | |
| 11139 | // if (hash == 0) hash = 27; |
| 11140 | __ mov(hash, Operand(27), LeaveCC, nz); |
| 11141 | } |
| 11142 | |
| 11143 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11144 | void SubStringStub::Generate(MacroAssembler* masm) { |
| 11145 | Label runtime; |
| 11146 | |
| 11147 | // Stack frame on entry. |
| 11148 | // lr: return address |
| 11149 | // sp[0]: to |
| 11150 | // sp[4]: from |
| 11151 | // sp[8]: string |
| 11152 | |
| 11153 | // This stub is called from the native-call %_SubString(...), so |
| 11154 | // nothing can be assumed about the arguments. It is tested that: |
| 11155 | // "string" is a sequential string, |
| 11156 | // both "from" and "to" are smis, and |
| 11157 | // 0 <= from <= to <= string.length. |
| 11158 | // If any of these assumptions fail, we call the runtime system. |
| 11159 | |
| 11160 | static const int kToOffset = 0 * kPointerSize; |
| 11161 | static const int kFromOffset = 1 * kPointerSize; |
| 11162 | static const int kStringOffset = 2 * kPointerSize; |
| 11163 | |
| 11164 | |
| 11165 | // Check bounds and smi-ness. |
| 11166 | __ ldr(r7, MemOperand(sp, kToOffset)); |
| 11167 | __ ldr(r6, MemOperand(sp, kFromOffset)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11168 | STATIC_ASSERT(kSmiTag == 0); |
| 11169 | STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11170 | // I.e., arithmetic shift right by one un-smi-tags. |
| 11171 | __ mov(r2, Operand(r7, ASR, 1), SetCC); |
| 11172 | __ mov(r3, Operand(r6, ASR, 1), SetCC, cc); |
| 11173 | // If either r2 or r6 had the smi tag bit set, then carry is set now. |
| 11174 | __ b(cs, &runtime); // Either "from" or "to" is not a smi. |
| 11175 | __ b(mi, &runtime); // From is negative. |
| 11176 | |
| 11177 | __ sub(r2, r2, Operand(r3), SetCC); |
| 11178 | __ b(mi, &runtime); // Fail if from > to. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11179 | // Special handling of sub-strings of length 1 and 2. One character strings |
| 11180 | // are handled in the runtime system (looked up in the single character |
| 11181 | // cache). Two character strings are looked for in the symbol cache. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11182 | __ cmp(r2, Operand(2)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11183 | __ b(lt, &runtime); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11184 | |
| 11185 | // r2: length |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11186 | // r3: from index (untaged smi) |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11187 | // r6: from (smi) |
| 11188 | // r7: to (smi) |
| 11189 | |
| 11190 | // Make sure first argument is a sequential (or flat) string. |
| 11191 | __ ldr(r5, MemOperand(sp, kStringOffset)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11192 | STATIC_ASSERT(kSmiTag == 0); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11193 | __ tst(r5, Operand(kSmiTagMask)); |
| 11194 | __ b(eq, &runtime); |
| 11195 | Condition is_string = masm->IsObjectStringType(r5, r1); |
| 11196 | __ b(NegateCondition(is_string), &runtime); |
| 11197 | |
| 11198 | // r1: instance type |
| 11199 | // r2: length |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11200 | // r3: from index (untaged smi) |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11201 | // r5: string |
| 11202 | // r6: from (smi) |
| 11203 | // r7: to (smi) |
| 11204 | Label seq_string; |
| 11205 | __ and_(r4, r1, Operand(kStringRepresentationMask)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11206 | STATIC_ASSERT(kSeqStringTag < kConsStringTag); |
| 11207 | STATIC_ASSERT(kConsStringTag < kExternalStringTag); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11208 | __ cmp(r4, Operand(kConsStringTag)); |
| 11209 | __ b(gt, &runtime); // External strings go to runtime. |
| 11210 | __ b(lt, &seq_string); // Sequential strings are handled directly. |
| 11211 | |
| 11212 | // Cons string. Try to recurse (once) on the first substring. |
| 11213 | // (This adds a little more generality than necessary to handle flattened |
| 11214 | // cons strings, but not much). |
| 11215 | __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset)); |
| 11216 | __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset)); |
| 11217 | __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11218 | __ tst(r1, Operand(kStringRepresentationMask)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11219 | STATIC_ASSERT(kSeqStringTag == 0); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11220 | __ b(ne, &runtime); // Cons and External strings go to runtime. |
| 11221 | |
| 11222 | // Definitly a sequential string. |
| 11223 | __ bind(&seq_string); |
| 11224 | |
| 11225 | // r1: instance type. |
| 11226 | // r2: length |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11227 | // r3: from index (untaged smi) |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11228 | // r5: string |
| 11229 | // r6: from (smi) |
| 11230 | // r7: to (smi) |
| 11231 | __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11232 | __ cmp(r4, Operand(r7)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11233 | __ b(lt, &runtime); // Fail if to > length. |
| 11234 | |
| 11235 | // r1: instance type. |
| 11236 | // r2: result string length. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11237 | // r3: from index (untaged smi) |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11238 | // r5: string. |
| 11239 | // r6: from offset (smi) |
| 11240 | // Check for flat ascii string. |
| 11241 | Label non_ascii_flat; |
| 11242 | __ tst(r1, Operand(kStringEncodingMask)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11243 | STATIC_ASSERT(kTwoByteStringTag == 0); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11244 | __ b(eq, &non_ascii_flat); |
| 11245 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11246 | Label result_longer_than_two; |
| 11247 | __ cmp(r2, Operand(2)); |
| 11248 | __ b(gt, &result_longer_than_two); |
| 11249 | |
| 11250 | // Sub string of length 2 requested. |
| 11251 | // Get the two characters forming the sub string. |
| 11252 | __ add(r5, r5, Operand(r3)); |
| 11253 | __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize)); |
| 11254 | __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1)); |
| 11255 | |
| 11256 | // Try to lookup two character string in symbol table. |
| 11257 | Label make_two_character_string; |
| 11258 | StringHelper::GenerateTwoCharacterSymbolTableProbe( |
| 11259 | masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string); |
| 11260 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 11261 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 11262 | __ Ret(); |
| 11263 | |
| 11264 | // r2: result string length. |
| 11265 | // r3: two characters combined into halfword in little endian byte order. |
| 11266 | __ bind(&make_two_character_string); |
| 11267 | __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime); |
| 11268 | __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 11269 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 11270 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 11271 | __ Ret(); |
| 11272 | |
| 11273 | __ bind(&result_longer_than_two); |
| 11274 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11275 | // Allocate the result. |
| 11276 | __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime); |
| 11277 | |
| 11278 | // r0: result string. |
| 11279 | // r2: result string length. |
| 11280 | // r5: string. |
| 11281 | // r6: from offset (smi) |
| 11282 | // Locate first character of result. |
| 11283 | __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11284 | // Locate 'from' character of string. |
| 11285 | __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11286 | __ add(r5, r5, Operand(r6, ASR, 1)); |
| 11287 | |
| 11288 | // r0: result string. |
| 11289 | // r1: first character of result string. |
| 11290 | // r2: result string length. |
| 11291 | // r5: first character of sub string to copy. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11292 | STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11293 | StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 11294 | COPY_ASCII | DEST_ALWAYS_ALIGNED); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11295 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 11296 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 11297 | __ Ret(); |
| 11298 | |
| 11299 | __ bind(&non_ascii_flat); |
| 11300 | // r2: result string length. |
| 11301 | // r5: string. |
| 11302 | // r6: from offset (smi) |
| 11303 | // Check for flat two byte string. |
| 11304 | |
| 11305 | // Allocate the result. |
| 11306 | __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime); |
| 11307 | |
| 11308 | // r0: result string. |
| 11309 | // r2: result string length. |
| 11310 | // r5: string. |
| 11311 | // Locate first character of result. |
| 11312 | __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11313 | // Locate 'from' character of string. |
| 11314 | __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11315 | // As "from" is a smi it is 2 times the value which matches the size of a two |
| 11316 | // byte character. |
| 11317 | __ add(r5, r5, Operand(r6)); |
| 11318 | |
| 11319 | // r0: result string. |
| 11320 | // r1: first character of result. |
| 11321 | // r2: result length. |
| 11322 | // r5: first character of string to copy. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11323 | STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11324 | StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 11325 | DEST_ALWAYS_ALIGNED); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11326 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 11327 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 11328 | __ Ret(); |
| 11329 | |
| 11330 | // Just jump to runtime to create the sub string. |
| 11331 | __ bind(&runtime); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11332 | __ TailCallRuntime(Runtime::kSubString, 3, 1); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11333 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 11334 | |
| 11335 | |
| 11336 | void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 11337 | Register left, |
| 11338 | Register right, |
| 11339 | Register scratch1, |
| 11340 | Register scratch2, |
| 11341 | Register scratch3, |
| 11342 | Register scratch4) { |
| 11343 | Label compare_lengths; |
| 11344 | // Find minimum length and length difference. |
| 11345 | __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset)); |
| 11346 | __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset)); |
| 11347 | __ sub(scratch3, scratch1, Operand(scratch2), SetCC); |
| 11348 | Register length_delta = scratch3; |
| 11349 | __ mov(scratch1, scratch2, LeaveCC, gt); |
| 11350 | Register min_length = scratch1; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11351 | STATIC_ASSERT(kSmiTag == 0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 11352 | __ tst(min_length, Operand(min_length)); |
| 11353 | __ b(eq, &compare_lengths); |
| 11354 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11355 | // Untag smi. |
| 11356 | __ mov(min_length, Operand(min_length, ASR, kSmiTagSize)); |
| 11357 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 11358 | // Setup registers so that we only need to increment one register |
| 11359 | // in the loop. |
| 11360 | __ add(scratch2, min_length, |
| 11361 | Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11362 | __ add(left, left, Operand(scratch2)); |
| 11363 | __ add(right, right, Operand(scratch2)); |
| 11364 | // Registers left and right points to the min_length character of strings. |
| 11365 | __ rsb(min_length, min_length, Operand(-1)); |
| 11366 | Register index = min_length; |
| 11367 | // Index starts at -min_length. |
| 11368 | |
| 11369 | { |
| 11370 | // Compare loop. |
| 11371 | Label loop; |
| 11372 | __ bind(&loop); |
| 11373 | // Compare characters. |
| 11374 | __ add(index, index, Operand(1), SetCC); |
| 11375 | __ ldrb(scratch2, MemOperand(left, index), ne); |
| 11376 | __ ldrb(scratch4, MemOperand(right, index), ne); |
| 11377 | // Skip to compare lengths with eq condition true. |
| 11378 | __ b(eq, &compare_lengths); |
| 11379 | __ cmp(scratch2, scratch4); |
| 11380 | __ b(eq, &loop); |
| 11381 | // Fallthrough with eq condition false. |
| 11382 | } |
| 11383 | // Compare lengths - strings up to min-length are equal. |
| 11384 | __ bind(&compare_lengths); |
| 11385 | ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0)); |
| 11386 | // Use zero length_delta as result. |
| 11387 | __ mov(r0, Operand(length_delta), SetCC, eq); |
| 11388 | // Fall through to here if characters compare not-equal. |
| 11389 | __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt); |
| 11390 | __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt); |
| 11391 | __ Ret(); |
| 11392 | } |
| 11393 | |
| 11394 | |
| 11395 | void StringCompareStub::Generate(MacroAssembler* masm) { |
| 11396 | Label runtime; |
| 11397 | |
| 11398 | // Stack frame on entry. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11399 | // sp[0]: right string |
| 11400 | // sp[4]: left string |
| 11401 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left |
| 11402 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 11403 | |
| 11404 | Label not_same; |
| 11405 | __ cmp(r0, r1); |
| 11406 | __ b(ne, ¬_same); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11407 | STATIC_ASSERT(EQUAL == 0); |
| 11408 | STATIC_ASSERT(kSmiTag == 0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 11409 | __ mov(r0, Operand(Smi::FromInt(EQUAL))); |
| 11410 | __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2); |
| 11411 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11412 | __ Ret(); |
| 11413 | |
| 11414 | __ bind(¬_same); |
| 11415 | |
| 11416 | // Check that both objects are sequential ascii strings. |
| 11417 | __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime); |
| 11418 | |
| 11419 | // Compare flat ascii strings natively. Remove arguments from stack first. |
| 11420 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 11421 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11422 | GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5); |
| 11423 | |
| 11424 | // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater) |
| 11425 | // tagged as a small integer. |
| 11426 | __ bind(&runtime); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11427 | __ TailCallRuntime(Runtime::kStringCompare, 2, 1); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 11428 | } |
| 11429 | |
| 11430 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11431 | void StringAddStub::Generate(MacroAssembler* masm) { |
| 11432 | Label string_add_runtime; |
| 11433 | // Stack on entry: |
| 11434 | // sp[0]: second argument. |
| 11435 | // sp[4]: first argument. |
| 11436 | |
| 11437 | // Load the two arguments. |
| 11438 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument. |
| 11439 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument. |
| 11440 | |
| 11441 | // Make sure that both arguments are strings if not known in advance. |
| 11442 | if (string_check_) { |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11443 | STATIC_ASSERT(kSmiTag == 0); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11444 | __ JumpIfEitherSmi(r0, r1, &string_add_runtime); |
| 11445 | // Load instance types. |
| 11446 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 11447 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 11448 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11449 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11450 | STATIC_ASSERT(kStringTag == 0); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11451 | // If either is not a string, go to runtime. |
| 11452 | __ tst(r4, Operand(kIsNotStringMask)); |
| 11453 | __ tst(r5, Operand(kIsNotStringMask), eq); |
| 11454 | __ b(ne, &string_add_runtime); |
| 11455 | } |
| 11456 | |
| 11457 | // Both arguments are strings. |
| 11458 | // r0: first string |
| 11459 | // r1: second string |
| 11460 | // r4: first string instance type (if string_check_) |
| 11461 | // r5: second string instance type (if string_check_) |
| 11462 | { |
| 11463 | Label strings_not_empty; |
| 11464 | // Check if either of the strings are empty. In that case return the other. |
| 11465 | __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset)); |
| 11466 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11467 | STATIC_ASSERT(kSmiTag == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11468 | __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11469 | __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11470 | STATIC_ASSERT(kSmiTag == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11471 | // Else test if second string is empty. |
| 11472 | __ cmp(r3, Operand(Smi::FromInt(0)), ne); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11473 | __ b(ne, &strings_not_empty); // If either string was empty, return r0. |
| 11474 | |
| 11475 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11476 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11477 | __ Ret(); |
| 11478 | |
| 11479 | __ bind(&strings_not_empty); |
| 11480 | } |
| 11481 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11482 | __ mov(r2, Operand(r2, ASR, kSmiTagSize)); |
| 11483 | __ mov(r3, Operand(r3, ASR, kSmiTagSize)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11484 | // Both strings are non-empty. |
| 11485 | // r0: first string |
| 11486 | // r1: second string |
| 11487 | // r2: length of first string |
| 11488 | // r3: length of second string |
| 11489 | // r4: first string instance type (if string_check_) |
| 11490 | // r5: second string instance type (if string_check_) |
| 11491 | // Look at the length of the result of adding the two strings. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11492 | Label string_add_flat_result, longer_than_two; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11493 | // Adding two lengths can't overflow. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11494 | STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11495 | __ add(r6, r2, Operand(r3)); |
| 11496 | // Use the runtime system when adding two one character strings, as it |
| 11497 | // contains optimizations for this specific case using the symbol table. |
| 11498 | __ cmp(r6, Operand(2)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11499 | __ b(ne, &longer_than_two); |
| 11500 | |
| 11501 | // Check that both strings are non-external ascii strings. |
| 11502 | if (!string_check_) { |
| 11503 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 11504 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 11505 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11506 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 11507 | } |
| 11508 | __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7, |
| 11509 | &string_add_runtime); |
| 11510 | |
| 11511 | // Get the two characters forming the sub string. |
| 11512 | __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 11513 | __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 11514 | |
| 11515 | // Try to lookup two character string in symbol table. If it is not found |
| 11516 | // just allocate a new one. |
| 11517 | Label make_two_character_string; |
| 11518 | StringHelper::GenerateTwoCharacterSymbolTableProbe( |
| 11519 | masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string); |
| 11520 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11521 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11522 | __ Ret(); |
| 11523 | |
| 11524 | __ bind(&make_two_character_string); |
| 11525 | // Resulting string has length 2 and first chars of two strings |
| 11526 | // are combined into single halfword in r2 register. |
| 11527 | // So we can fill resulting string without two loops by a single |
| 11528 | // halfword store instruction (which assumes that processor is |
| 11529 | // in a little endian mode) |
| 11530 | __ mov(r6, Operand(2)); |
| 11531 | __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime); |
| 11532 | __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 11533 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11534 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11535 | __ Ret(); |
| 11536 | |
| 11537 | __ bind(&longer_than_two); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11538 | // Check if resulting string will be flat. |
| 11539 | __ cmp(r6, Operand(String::kMinNonFlatLength)); |
| 11540 | __ b(lt, &string_add_flat_result); |
| 11541 | // Handle exceptionally long strings in the runtime system. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11542 | STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11543 | ASSERT(IsPowerOf2(String::kMaxLength + 1)); |
| 11544 | // kMaxLength + 1 is representable as shifted literal, kMaxLength is not. |
| 11545 | __ cmp(r6, Operand(String::kMaxLength + 1)); |
| 11546 | __ b(hs, &string_add_runtime); |
| 11547 | |
| 11548 | // If result is not supposed to be flat, allocate a cons string object. |
| 11549 | // If both strings are ascii the result is an ascii cons string. |
| 11550 | if (!string_check_) { |
| 11551 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 11552 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 11553 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11554 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 11555 | } |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 11556 | Label non_ascii, allocated, ascii_data; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11557 | STATIC_ASSERT(kTwoByteStringTag == 0); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11558 | __ tst(r4, Operand(kStringEncodingMask)); |
| 11559 | __ tst(r5, Operand(kStringEncodingMask), ne); |
| 11560 | __ b(eq, &non_ascii); |
| 11561 | |
| 11562 | // Allocate an ASCII cons string. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 11563 | __ bind(&ascii_data); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11564 | __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime); |
| 11565 | __ bind(&allocated); |
| 11566 | // Fill the fields of the cons string. |
| 11567 | __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset)); |
| 11568 | __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset)); |
| 11569 | __ mov(r0, Operand(r7)); |
| 11570 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11571 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11572 | __ Ret(); |
| 11573 | |
| 11574 | __ bind(&non_ascii); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 11575 | // At least one of the strings is two-byte. Check whether it happens |
| 11576 | // to contain only ascii characters. |
| 11577 | // r4: first instance type. |
| 11578 | // r5: second instance type. |
| 11579 | __ tst(r4, Operand(kAsciiDataHintMask)); |
| 11580 | __ tst(r5, Operand(kAsciiDataHintMask), ne); |
| 11581 | __ b(ne, &ascii_data); |
| 11582 | __ eor(r4, r4, Operand(r5)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11583 | STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 11584 | __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag)); |
| 11585 | __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag)); |
| 11586 | __ b(eq, &ascii_data); |
| 11587 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11588 | // Allocate a two byte cons string. |
| 11589 | __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime); |
| 11590 | __ jmp(&allocated); |
| 11591 | |
| 11592 | // Handle creating a flat result. First check that both strings are |
| 11593 | // sequential and that they have the same encoding. |
| 11594 | // r0: first string |
| 11595 | // r1: second string |
| 11596 | // r2: length of first string |
| 11597 | // r3: length of second string |
| 11598 | // r4: first string instance type (if string_check_) |
| 11599 | // r5: second string instance type (if string_check_) |
| 11600 | // r6: sum of lengths. |
| 11601 | __ bind(&string_add_flat_result); |
| 11602 | if (!string_check_) { |
| 11603 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 11604 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 11605 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11606 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 11607 | } |
| 11608 | // Check that both strings are sequential. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 11609 | STATIC_ASSERT(kSeqStringTag == 0); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11610 | __ tst(r4, Operand(kStringRepresentationMask)); |
| 11611 | __ tst(r5, Operand(kStringRepresentationMask), eq); |
| 11612 | __ b(ne, &string_add_runtime); |
| 11613 | // Now check if both strings have the same encoding (ASCII/Two-byte). |
| 11614 | // r0: first string. |
| 11615 | // r1: second string. |
| 11616 | // r2: length of first string. |
| 11617 | // r3: length of second string. |
| 11618 | // r6: sum of lengths.. |
| 11619 | Label non_ascii_string_add_flat_result; |
| 11620 | ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test. |
| 11621 | __ eor(r7, r4, Operand(r5)); |
| 11622 | __ tst(r7, Operand(kStringEncodingMask)); |
| 11623 | __ b(ne, &string_add_runtime); |
| 11624 | // And see if it's ASCII or two-byte. |
| 11625 | __ tst(r4, Operand(kStringEncodingMask)); |
| 11626 | __ b(eq, &non_ascii_string_add_flat_result); |
| 11627 | |
| 11628 | // Both strings are sequential ASCII strings. We also know that they are |
| 11629 | // short (since the sum of the lengths is less than kMinNonFlatLength). |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11630 | // r6: length of resulting flat string |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11631 | __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 11632 | // Locate first character of result. |
| 11633 | __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11634 | // Locate first character of first argument. |
| 11635 | __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11636 | // r0: first character of first string. |
| 11637 | // r1: second string. |
| 11638 | // r2: length of first string. |
| 11639 | // r3: length of second string. |
| 11640 | // r6: first character of result. |
| 11641 | // r7: result string. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11642 | StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11643 | |
| 11644 | // Load second argument and locate first character. |
| 11645 | __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11646 | // r1: first character of second string. |
| 11647 | // r3: length of second string. |
| 11648 | // r6: next character of result. |
| 11649 | // r7: result string. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11650 | StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11651 | __ mov(r0, Operand(r7)); |
| 11652 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11653 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11654 | __ Ret(); |
| 11655 | |
| 11656 | __ bind(&non_ascii_string_add_flat_result); |
| 11657 | // Both strings are sequential two byte strings. |
| 11658 | // r0: first string. |
| 11659 | // r1: second string. |
| 11660 | // r2: length of first string. |
| 11661 | // r3: length of second string. |
| 11662 | // r6: sum of length of strings. |
| 11663 | __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 11664 | // r0: first string. |
| 11665 | // r1: second string. |
| 11666 | // r2: length of first string. |
| 11667 | // r3: length of second string. |
| 11668 | // r7: result string. |
| 11669 | |
| 11670 | // Locate first character of result. |
| 11671 | __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11672 | // Locate first character of first argument. |
| 11673 | __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11674 | |
| 11675 | // r0: first character of first string. |
| 11676 | // r1: second string. |
| 11677 | // r2: length of first string. |
| 11678 | // r3: length of second string. |
| 11679 | // r6: first character of result. |
| 11680 | // r7: result string. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11681 | StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11682 | |
| 11683 | // Locate first character of second argument. |
| 11684 | __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11685 | |
| 11686 | // r1: first character of second string. |
| 11687 | // r3: length of second string. |
| 11688 | // r6: next character of result (after copy of first string). |
| 11689 | // r7: result string. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11690 | StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11691 | |
| 11692 | __ mov(r0, Operand(r7)); |
| 11693 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11694 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11695 | __ Ret(); |
| 11696 | |
| 11697 | // Just jump to runtime to add the two strings. |
| 11698 | __ bind(&string_add_runtime); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11699 | __ TailCallRuntime(Runtime::kStringAdd, 2, 1); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 11700 | } |
| 11701 | |
| 11702 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11703 | #undef __ |
| 11704 | |
| 11705 | } } // namespace v8::internal |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 11706 | |
| 11707 | #endif // V8_TARGET_ARCH_ARM |