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 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | #include "debug.h" |
| 34 | #include "parser.h" |
| 35 | #include "register-allocator-inl.h" |
| 36 | #include "runtime.h" |
| 37 | #include "scopes.h" |
| 38 | |
| 39 | |
| 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
| 43 | #define __ ACCESS_MASM(masm_) |
| 44 | |
| 45 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 46 | Label* slow, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 47 | Condition cc, |
| 48 | bool never_nan_nan); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 50 | Label* lhs_not_nan, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | Label* slow, |
| 52 | bool strict); |
| 53 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
| 54 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm); |
| 55 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 56 | Register source, |
| 57 | Register destination, |
| 58 | int known_int); |
| 59 | static bool IsEasyToMultiplyBy(int x); |
| 60 | |
| 61 | |
| 62 | |
| 63 | // ------------------------------------------------------------------------- |
| 64 | // Platform-specific DeferredCode functions. |
| 65 | |
| 66 | void DeferredCode::SaveRegisters() { |
| 67 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 68 | int action = registers_[i]; |
| 69 | if (action == kPush) { |
| 70 | __ push(RegisterAllocator::ToRegister(i)); |
| 71 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 72 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void DeferredCode::RestoreRegisters() { |
| 79 | // Restore registers in reverse order due to the stack. |
| 80 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 81 | int action = registers_[i]; |
| 82 | if (action == kPush) { |
| 83 | __ pop(RegisterAllocator::ToRegister(i)); |
| 84 | } else if (action != kIgnore) { |
| 85 | action &= ~kSyncedFlag; |
| 86 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | // ------------------------------------------------------------------------- |
| 93 | // CodeGenState implementation. |
| 94 | |
| 95 | CodeGenState::CodeGenState(CodeGenerator* owner) |
| 96 | : owner_(owner), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | true_target_(NULL), |
| 98 | false_target_(NULL), |
| 99 | previous_(NULL) { |
| 100 | owner_->set_state(this); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | CodeGenState::CodeGenState(CodeGenerator* owner, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | JumpTarget* true_target, |
| 106 | JumpTarget* false_target) |
| 107 | : owner_(owner), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | true_target_(true_target), |
| 109 | false_target_(false_target), |
| 110 | previous_(owner->state()) { |
| 111 | owner_->set_state(this); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | CodeGenState::~CodeGenState() { |
| 116 | ASSERT(owner_->state() == this); |
| 117 | owner_->set_state(previous_); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | // ------------------------------------------------------------------------- |
| 122 | // CodeGenerator implementation |
| 123 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 124 | CodeGenerator::CodeGenerator(MacroAssembler* masm, |
| 125 | Handle<Script> script, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 126 | bool is_eval) |
| 127 | : is_eval_(is_eval), |
| 128 | script_(script), |
| 129 | deferred_(8), |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 130 | masm_(masm), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 131 | scope_(NULL), |
| 132 | frame_(NULL), |
| 133 | allocator_(NULL), |
| 134 | cc_reg_(al), |
| 135 | state_(NULL), |
| 136 | function_return_is_shadowed_(false) { |
| 137 | } |
| 138 | |
| 139 | |
| 140 | // Calling conventions: |
| 141 | // fp: caller's frame pointer |
| 142 | // sp: stack pointer |
| 143 | // r1: called JS function |
| 144 | // cp: callee's context |
| 145 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 146 | void CodeGenerator::Generate(FunctionLiteral* fun, |
| 147 | Mode mode, |
| 148 | CompilationInfo* info) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 149 | // Record the position for debugging purposes. |
| 150 | CodeForFunctionPosition(fun); |
| 151 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | ZoneList<Statement*>* body = fun->body(); |
| 153 | |
| 154 | // Initialize state. |
| 155 | ASSERT(scope_ == NULL); |
| 156 | scope_ = fun->scope(); |
| 157 | ASSERT(allocator_ == NULL); |
| 158 | RegisterAllocator register_allocator(this); |
| 159 | allocator_ = ®ister_allocator; |
| 160 | ASSERT(frame_ == NULL); |
| 161 | frame_ = new VirtualFrame(); |
| 162 | cc_reg_ = al; |
| 163 | { |
| 164 | CodeGenState state(this); |
| 165 | |
| 166 | // Entry: |
| 167 | // Stack: receiver, arguments |
| 168 | // lr: return address |
| 169 | // fp: caller's frame pointer |
| 170 | // sp: stack pointer |
| 171 | // r1: called JS function |
| 172 | // cp: callee's context |
| 173 | allocator_->Initialize(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 174 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | #ifdef DEBUG |
| 176 | if (strlen(FLAG_stop_at) > 0 && |
| 177 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
| 178 | frame_->SpillAll(); |
| 179 | __ stop("stop-at"); |
| 180 | } |
| 181 | #endif |
| 182 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 183 | if (mode == PRIMARY) { |
| 184 | frame_->Enter(); |
| 185 | // tos: code slot |
| 186 | |
| 187 | // Allocate space for locals and initialize them. This also checks |
| 188 | // for stack overflow. |
| 189 | frame_->AllocateStackSlots(); |
| 190 | |
| 191 | VirtualFrame::SpilledScope spilled_scope; |
| 192 | int heap_slots = scope_->num_heap_slots(); |
| 193 | if (heap_slots > 0) { |
| 194 | // Allocate local context. |
| 195 | // Get outer context and create a new context based on it. |
| 196 | __ ldr(r0, frame_->Function()); |
| 197 | frame_->EmitPush(r0); |
| 198 | if (heap_slots <= FastNewContextStub::kMaximumSlots) { |
| 199 | FastNewContextStub stub(heap_slots); |
| 200 | frame_->CallStub(&stub, 1); |
| 201 | } else { |
| 202 | frame_->CallRuntime(Runtime::kNewContext, 1); |
| 203 | } |
| 204 | |
| 205 | #ifdef DEBUG |
| 206 | JumpTarget verified_true; |
| 207 | __ cmp(r0, Operand(cp)); |
| 208 | verified_true.Branch(eq); |
| 209 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 210 | verified_true.Bind(); |
| 211 | #endif |
| 212 | // Update context local. |
| 213 | __ str(cp, frame_->Context()); |
| 214 | } |
| 215 | |
| 216 | // TODO(1241774): Improve this code: |
| 217 | // 1) only needed if we have a context |
| 218 | // 2) no need to recompute context ptr every single time |
| 219 | // 3) don't copy parameter operand code from SlotOperand! |
| 220 | { |
| 221 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 222 | |
| 223 | // Note that iteration order is relevant here! If we have the same |
| 224 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 225 | // needs to be copied into the context, it must be the last argument |
| 226 | // passed to the parameter that needs to be copied. This is a rare |
| 227 | // case so we don't check for it, instead we rely on the copying |
| 228 | // order: such a parameter is copied repeatedly into the same |
| 229 | // context location and thus the last value is what is seen inside |
| 230 | // the function. |
| 231 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 232 | Variable* par = scope_->parameter(i); |
| 233 | Slot* slot = par->slot(); |
| 234 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
| 235 | // No parameters in global scope. |
| 236 | ASSERT(!scope_->is_global_scope()); |
| 237 | __ ldr(r1, frame_->ParameterAt(i)); |
| 238 | // Loads r2 with context; used below in RecordWrite. |
| 239 | __ str(r1, SlotOperand(slot, r2)); |
| 240 | // Load the offset into r3. |
| 241 | int slot_offset = |
| 242 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 243 | __ mov(r3, Operand(slot_offset)); |
| 244 | __ RecordWrite(r2, r3, r1); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Store the arguments object. This must happen after context |
| 250 | // initialization because the arguments object may be stored in the |
| 251 | // context. |
| 252 | if (scope_->arguments() != NULL) { |
| 253 | Comment cmnt(masm_, "[ allocate arguments object"); |
| 254 | ASSERT(scope_->arguments_shadow() != NULL); |
| 255 | Variable* arguments = scope_->arguments()->var(); |
| 256 | Variable* shadow = scope_->arguments_shadow()->var(); |
| 257 | ASSERT(arguments != NULL && arguments->slot() != NULL); |
| 258 | ASSERT(shadow != NULL && shadow->slot() != NULL); |
| 259 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
| 260 | __ ldr(r2, frame_->Function()); |
| 261 | // The receiver is below the arguments, the return address, and the |
| 262 | // frame pointer on the stack. |
| 263 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
| 264 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
| 265 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 266 | frame_->Adjust(3); |
| 267 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
| 268 | frame_->CallStub(&stub, 3); |
| 269 | frame_->EmitPush(r0); |
| 270 | StoreToSlot(arguments->slot(), NOT_CONST_INIT); |
| 271 | StoreToSlot(shadow->slot(), NOT_CONST_INIT); |
| 272 | frame_->Drop(); // Value is no longer needed. |
| 273 | } |
| 274 | |
| 275 | // Initialize ThisFunction reference if present. |
| 276 | if (scope_->is_function_scope() && scope_->function() != NULL) { |
| 277 | __ mov(ip, Operand(Factory::the_hole_value())); |
| 278 | frame_->EmitPush(ip); |
| 279 | StoreToSlot(scope_->function()->slot(), NOT_CONST_INIT); |
| 280 | } |
| 281 | } else { |
| 282 | // When used as the secondary compiler for splitting, r1, cp, |
| 283 | // fp, and lr have been pushed on the stack. Adjust the virtual |
| 284 | // frame to match this state. |
| 285 | frame_->Adjust(4); |
| 286 | allocator_->Unuse(r1); |
| 287 | allocator_->Unuse(lr); |
| 288 | } |
| 289 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | // Initialize the function return target after the locals are set |
| 291 | // up, because it needs the expected frame height from the frame. |
| 292 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
| 293 | function_return_is_shadowed_ = false; |
| 294 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 295 | // Generate code to 'execute' declarations and initialize functions |
| 296 | // (source elements). In case of an illegal redeclaration we need to |
| 297 | // handle that instead of processing the declarations. |
| 298 | if (scope_->HasIllegalRedeclaration()) { |
| 299 | Comment cmnt(masm_, "[ illegal redeclarations"); |
| 300 | scope_->VisitIllegalRedeclaration(this); |
| 301 | } else { |
| 302 | Comment cmnt(masm_, "[ declarations"); |
| 303 | ProcessDeclarations(scope_->declarations()); |
| 304 | // Bail out if a stack-overflow exception occurred when processing |
| 305 | // declarations. |
| 306 | if (HasStackOverflow()) return; |
| 307 | } |
| 308 | |
| 309 | if (FLAG_trace) { |
| 310 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
| 311 | // Ignore the return value. |
| 312 | } |
| 313 | |
| 314 | // Compile the body of the function in a vanilla state. Don't |
| 315 | // bother compiling all the code if the scope has an illegal |
| 316 | // redeclaration. |
| 317 | if (!scope_->HasIllegalRedeclaration()) { |
| 318 | Comment cmnt(masm_, "[ function body"); |
| 319 | #ifdef DEBUG |
| 320 | bool is_builtin = Bootstrapper::IsActive(); |
| 321 | bool should_trace = |
| 322 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
| 323 | if (should_trace) { |
| 324 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
| 325 | // Ignore the return value. |
| 326 | } |
| 327 | #endif |
| 328 | VisitStatementsAndSpill(body); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // Generate the return sequence if necessary. |
| 333 | if (has_valid_frame() || function_return_.is_linked()) { |
| 334 | if (!function_return_.is_linked()) { |
| 335 | CodeForReturnPosition(fun); |
| 336 | } |
| 337 | // exit |
| 338 | // r0: result |
| 339 | // sp: stack pointer |
| 340 | // fp: frame pointer |
| 341 | // cp: callee's context |
| 342 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
| 343 | |
| 344 | function_return_.Bind(); |
| 345 | if (FLAG_trace) { |
| 346 | // Push the return value on the stack as the parameter. |
| 347 | // Runtime::TraceExit returns the parameter as it is. |
| 348 | frame_->EmitPush(r0); |
| 349 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 350 | } |
| 351 | |
| 352 | // Add a label for checking the size of the code used for returning. |
| 353 | Label check_exit_codesize; |
| 354 | masm_->bind(&check_exit_codesize); |
| 355 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 356 | // Calculate the exact length of the return sequence and make sure that |
| 357 | // the constant pool is not emitted inside of the return sequence. |
| 358 | int32_t sp_delta = (scope_->num_parameters() + 1) * kPointerSize; |
| 359 | int return_sequence_length = Assembler::kJSReturnSequenceLength; |
| 360 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 361 | // Additional mov instruction generated. |
| 362 | return_sequence_length++; |
| 363 | } |
| 364 | masm_->BlockConstPoolFor(return_sequence_length); |
| 365 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | // Tear down the frame which will restore the caller's frame pointer and |
| 367 | // the link register. |
| 368 | frame_->Exit(); |
| 369 | |
| 370 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 371 | // tool from instrumenting as we rely on the code size here. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 372 | masm_->add(sp, sp, Operand(sp_delta)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 373 | masm_->Jump(lr); |
| 374 | |
| 375 | // Check that the size of the code used for returning matches what is |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 376 | // expected by the debugger. The add instruction above is an addressing |
| 377 | // mode 1 instruction where there are restrictions on which immediate values |
| 378 | // can be encoded in the instruction and which immediate values requires |
| 379 | // use of an additional instruction for moving the immediate to a temporary |
| 380 | // register. |
| 381 | ASSERT_EQ(return_sequence_length, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 382 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
| 383 | } |
| 384 | |
| 385 | // Code generation state must be reset. |
| 386 | ASSERT(!has_cc()); |
| 387 | ASSERT(state_ == NULL); |
| 388 | ASSERT(!function_return_is_shadowed_); |
| 389 | function_return_.Unuse(); |
| 390 | DeleteFrame(); |
| 391 | |
| 392 | // Process any deferred code using the register allocator. |
| 393 | if (!HasStackOverflow()) { |
| 394 | ProcessDeferred(); |
| 395 | } |
| 396 | |
| 397 | allocator_ = NULL; |
| 398 | scope_ = NULL; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 403 | // Currently, this assertion will fail if we try to assign to |
| 404 | // a constant variable that is constant because it is read-only |
| 405 | // (such as the variable referring to a named function expression). |
| 406 | // We need to implement assignments to read-only variables. |
| 407 | // Ideally, we should do this during AST generation (by converting |
| 408 | // such assignments into expression statements); however, in general |
| 409 | // we may not be able to make the decision until past AST generation, |
| 410 | // that is when the entire program is known. |
| 411 | ASSERT(slot != NULL); |
| 412 | int index = slot->index(); |
| 413 | switch (slot->type()) { |
| 414 | case Slot::PARAMETER: |
| 415 | return frame_->ParameterAt(index); |
| 416 | |
| 417 | case Slot::LOCAL: |
| 418 | return frame_->LocalAt(index); |
| 419 | |
| 420 | case Slot::CONTEXT: { |
| 421 | // Follow the context chain if necessary. |
| 422 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 423 | Register context = cp; |
| 424 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
| 425 | for (int i = 0; i < chain_length; i++) { |
| 426 | // Load the closure. |
| 427 | // (All contexts, even 'with' contexts, have a closure, |
| 428 | // and it is the same for all contexts inside a function. |
| 429 | // There is no need to go to the function context first.) |
| 430 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 431 | // Load the function context (which is the incoming, outer context). |
| 432 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 433 | context = tmp; |
| 434 | } |
| 435 | // We may have a 'with' context now. Get the function context. |
| 436 | // (In fact this mov may never be the needed, since the scope analysis |
| 437 | // may not permit a direct context access in this case and thus we are |
| 438 | // always at a function context. However it is safe to dereference be- |
| 439 | // cause the function context of a function context is itself. Before |
| 440 | // deleting this mov we should try to create a counter-example first, |
| 441 | // though...) |
| 442 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 443 | return ContextOperand(tmp, index); |
| 444 | } |
| 445 | |
| 446 | default: |
| 447 | UNREACHABLE(); |
| 448 | return MemOperand(r0, 0); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 454 | Slot* slot, |
| 455 | Register tmp, |
| 456 | Register tmp2, |
| 457 | JumpTarget* slow) { |
| 458 | ASSERT(slot->type() == Slot::CONTEXT); |
| 459 | Register context = cp; |
| 460 | |
| 461 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 462 | if (s->num_heap_slots() > 0) { |
| 463 | if (s->calls_eval()) { |
| 464 | // Check that extension is NULL. |
| 465 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 466 | __ tst(tmp2, tmp2); |
| 467 | slow->Branch(ne); |
| 468 | } |
| 469 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 470 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 471 | context = tmp; |
| 472 | } |
| 473 | } |
| 474 | // Check that last extension is NULL. |
| 475 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 476 | __ tst(tmp2, tmp2); |
| 477 | slow->Branch(ne); |
| 478 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 479 | return ContextOperand(tmp, slot->index()); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 484 | // (partially) translated into branches, or it may have set the condition |
| 485 | // code register. If force_cc is set, the value is forced to set the |
| 486 | // condition code register and no value is pushed. If the condition code |
| 487 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 488 | // test for 'true'. |
| 489 | void CodeGenerator::LoadCondition(Expression* x, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 490 | JumpTarget* true_target, |
| 491 | JumpTarget* false_target, |
| 492 | bool force_cc) { |
| 493 | ASSERT(!has_cc()); |
| 494 | int original_height = frame_->height(); |
| 495 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 496 | { CodeGenState new_state(this, true_target, false_target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 497 | Visit(x); |
| 498 | |
| 499 | // If we hit a stack overflow, we may not have actually visited |
| 500 | // the expression. In that case, we ensure that we have a |
| 501 | // valid-looking frame state because we will continue to generate |
| 502 | // code as we unwind the C++ stack. |
| 503 | // |
| 504 | // It's possible to have both a stack overflow and a valid frame |
| 505 | // state (eg, a subexpression overflowed, visiting it returned |
| 506 | // with a dummied frame state, and visiting this expression |
| 507 | // returned with a normal-looking state). |
| 508 | if (HasStackOverflow() && |
| 509 | has_valid_frame() && |
| 510 | !has_cc() && |
| 511 | frame_->height() == original_height) { |
| 512 | true_target->Jump(); |
| 513 | } |
| 514 | } |
| 515 | if (force_cc && frame_ != NULL && !has_cc()) { |
| 516 | // Convert the TOS value to a boolean in the condition code register. |
| 517 | ToBoolean(true_target, false_target); |
| 518 | } |
| 519 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 520 | ASSERT(!has_valid_frame() || |
| 521 | (has_cc() && frame_->height() == original_height) || |
| 522 | (!has_cc() && frame_->height() == original_height + 1)); |
| 523 | } |
| 524 | |
| 525 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 526 | void CodeGenerator::Load(Expression* expr) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 527 | #ifdef DEBUG |
| 528 | int original_height = frame_->height(); |
| 529 | #endif |
| 530 | JumpTarget true_target; |
| 531 | JumpTarget false_target; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 532 | LoadCondition(expr, &true_target, &false_target, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 533 | |
| 534 | if (has_cc()) { |
| 535 | // Convert cc_reg_ into a boolean value. |
| 536 | JumpTarget loaded; |
| 537 | JumpTarget materialize_true; |
| 538 | materialize_true.Branch(cc_reg_); |
| 539 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
| 540 | frame_->EmitPush(r0); |
| 541 | loaded.Jump(); |
| 542 | materialize_true.Bind(); |
| 543 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
| 544 | frame_->EmitPush(r0); |
| 545 | loaded.Bind(); |
| 546 | cc_reg_ = al; |
| 547 | } |
| 548 | |
| 549 | if (true_target.is_linked() || false_target.is_linked()) { |
| 550 | // We have at least one condition value that has been "translated" |
| 551 | // into a branch, thus it needs to be loaded explicitly. |
| 552 | JumpTarget loaded; |
| 553 | if (frame_ != NULL) { |
| 554 | loaded.Jump(); // Don't lose the current TOS. |
| 555 | } |
| 556 | bool both = true_target.is_linked() && false_target.is_linked(); |
| 557 | // Load "true" if necessary. |
| 558 | if (true_target.is_linked()) { |
| 559 | true_target.Bind(); |
| 560 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
| 561 | frame_->EmitPush(r0); |
| 562 | } |
| 563 | // If both "true" and "false" need to be loaded jump across the code for |
| 564 | // "false". |
| 565 | if (both) { |
| 566 | loaded.Jump(); |
| 567 | } |
| 568 | // Load "false" if necessary. |
| 569 | if (false_target.is_linked()) { |
| 570 | false_target.Bind(); |
| 571 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
| 572 | frame_->EmitPush(r0); |
| 573 | } |
| 574 | // A value is loaded on all paths reaching this point. |
| 575 | loaded.Bind(); |
| 576 | } |
| 577 | ASSERT(has_valid_frame()); |
| 578 | ASSERT(!has_cc()); |
| 579 | ASSERT(frame_->height() == original_height + 1); |
| 580 | } |
| 581 | |
| 582 | |
| 583 | void CodeGenerator::LoadGlobal() { |
| 584 | VirtualFrame::SpilledScope spilled_scope; |
| 585 | __ ldr(r0, GlobalObject()); |
| 586 | frame_->EmitPush(r0); |
| 587 | } |
| 588 | |
| 589 | |
| 590 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
| 591 | VirtualFrame::SpilledScope spilled_scope; |
| 592 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 593 | __ ldr(scratch, |
| 594 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
| 595 | frame_->EmitPush(scratch); |
| 596 | } |
| 597 | |
| 598 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 599 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 600 | // Special handling of identifiers as subexpressions of typeof. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 601 | VirtualFrame::SpilledScope spilled_scope; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 602 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 603 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 604 | // For a global variable we build the property reference |
| 605 | // <global>.<variable> and perform a (regular non-contextual) property |
| 606 | // load to make sure we do not get reference errors. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 607 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 608 | Literal key(variable->name()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 609 | Property property(&global, &key, RelocInfo::kNoPosition); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 610 | Reference ref(this, &property); |
| 611 | ref.GetValueAndSpill(); |
| 612 | } else if (variable != NULL && variable->slot() != NULL) { |
| 613 | // For a variable that rewrites to a slot, we signal it is the immediate |
| 614 | // subexpression of a typeof. |
| 615 | LoadFromSlot(variable->slot(), INSIDE_TYPEOF); |
| 616 | frame_->SpillAll(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 617 | } else { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 618 | // Anything else can be handled normally. |
| 619 | LoadAndSpill(expr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 624 | Reference::Reference(CodeGenerator* cgen, |
| 625 | Expression* expression, |
| 626 | bool persist_after_get) |
| 627 | : cgen_(cgen), |
| 628 | expression_(expression), |
| 629 | type_(ILLEGAL), |
| 630 | persist_after_get_(persist_after_get) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 631 | cgen->LoadReference(this); |
| 632 | } |
| 633 | |
| 634 | |
| 635 | Reference::~Reference() { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 636 | ASSERT(is_unloaded() || is_illegal()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
| 640 | void CodeGenerator::LoadReference(Reference* ref) { |
| 641 | VirtualFrame::SpilledScope spilled_scope; |
| 642 | Comment cmnt(masm_, "[ LoadReference"); |
| 643 | Expression* e = ref->expression(); |
| 644 | Property* property = e->AsProperty(); |
| 645 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 646 | |
| 647 | if (property != NULL) { |
| 648 | // The expression is either a property or a variable proxy that rewrites |
| 649 | // to a property. |
| 650 | LoadAndSpill(property->obj()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 651 | if (property->key()->IsPropertyName()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 652 | ref->set_type(Reference::NAMED); |
| 653 | } else { |
| 654 | LoadAndSpill(property->key()); |
| 655 | ref->set_type(Reference::KEYED); |
| 656 | } |
| 657 | } else if (var != NULL) { |
| 658 | // The expression is a variable proxy that does not rewrite to a |
| 659 | // property. Global variables are treated as named property references. |
| 660 | if (var->is_global()) { |
| 661 | LoadGlobal(); |
| 662 | ref->set_type(Reference::NAMED); |
| 663 | } else { |
| 664 | ASSERT(var->slot() != NULL); |
| 665 | ref->set_type(Reference::SLOT); |
| 666 | } |
| 667 | } else { |
| 668 | // Anything else is a runtime error. |
| 669 | LoadAndSpill(e); |
| 670 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | |
| 675 | void CodeGenerator::UnloadReference(Reference* ref) { |
| 676 | VirtualFrame::SpilledScope spilled_scope; |
| 677 | // Pop a reference from the stack while preserving TOS. |
| 678 | Comment cmnt(masm_, "[ UnloadReference"); |
| 679 | int size = ref->size(); |
| 680 | if (size > 0) { |
| 681 | frame_->EmitPop(r0); |
| 682 | frame_->Drop(size); |
| 683 | frame_->EmitPush(r0); |
| 684 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 685 | ref->set_unloaded(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | |
| 689 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 690 | // register to a boolean in the condition code register. The code |
| 691 | // may jump to 'false_target' in case the register converts to 'false'. |
| 692 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 693 | JumpTarget* false_target) { |
| 694 | VirtualFrame::SpilledScope spilled_scope; |
| 695 | // Note: The generated code snippet does not change stack variables. |
| 696 | // Only the condition code should be set. |
| 697 | frame_->EmitPop(r0); |
| 698 | |
| 699 | // Fast case checks |
| 700 | |
| 701 | // Check if the value is 'false'. |
| 702 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 703 | __ cmp(r0, ip); |
| 704 | false_target->Branch(eq); |
| 705 | |
| 706 | // Check if the value is 'true'. |
| 707 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 708 | __ cmp(r0, ip); |
| 709 | true_target->Branch(eq); |
| 710 | |
| 711 | // Check if the value is 'undefined'. |
| 712 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 713 | __ cmp(r0, ip); |
| 714 | false_target->Branch(eq); |
| 715 | |
| 716 | // Check if the value is a smi. |
| 717 | __ cmp(r0, Operand(Smi::FromInt(0))); |
| 718 | false_target->Branch(eq); |
| 719 | __ tst(r0, Operand(kSmiTagMask)); |
| 720 | true_target->Branch(eq); |
| 721 | |
| 722 | // Slow case: call the runtime. |
| 723 | frame_->EmitPush(r0); |
| 724 | frame_->CallRuntime(Runtime::kToBool, 1); |
| 725 | // Convert the result (r0) to a condition code. |
| 726 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 727 | __ cmp(r0, ip); |
| 728 | |
| 729 | cc_reg_ = ne; |
| 730 | } |
| 731 | |
| 732 | |
| 733 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
| 734 | OverwriteMode overwrite_mode, |
| 735 | int constant_rhs) { |
| 736 | VirtualFrame::SpilledScope spilled_scope; |
| 737 | // sp[0] : y |
| 738 | // sp[1] : x |
| 739 | // result : r0 |
| 740 | |
| 741 | // Stub is entered with a call: 'return address' is in lr. |
| 742 | switch (op) { |
| 743 | case Token::ADD: // fall through. |
| 744 | case Token::SUB: // fall through. |
| 745 | case Token::MUL: |
| 746 | case Token::DIV: |
| 747 | case Token::MOD: |
| 748 | case Token::BIT_OR: |
| 749 | case Token::BIT_AND: |
| 750 | case Token::BIT_XOR: |
| 751 | case Token::SHL: |
| 752 | case Token::SHR: |
| 753 | case Token::SAR: { |
| 754 | frame_->EmitPop(r0); // r0 : y |
| 755 | frame_->EmitPop(r1); // r1 : x |
| 756 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
| 757 | frame_->CallStub(&stub, 0); |
| 758 | break; |
| 759 | } |
| 760 | |
| 761 | case Token::COMMA: |
| 762 | frame_->EmitPop(r0); |
| 763 | // simply discard left value |
| 764 | frame_->Drop(); |
| 765 | break; |
| 766 | |
| 767 | default: |
| 768 | // Other cases should have been handled before this point. |
| 769 | UNREACHABLE(); |
| 770 | break; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | |
| 775 | class DeferredInlineSmiOperation: public DeferredCode { |
| 776 | public: |
| 777 | DeferredInlineSmiOperation(Token::Value op, |
| 778 | int value, |
| 779 | bool reversed, |
| 780 | OverwriteMode overwrite_mode) |
| 781 | : op_(op), |
| 782 | value_(value), |
| 783 | reversed_(reversed), |
| 784 | overwrite_mode_(overwrite_mode) { |
| 785 | set_comment("[ DeferredInlinedSmiOperation"); |
| 786 | } |
| 787 | |
| 788 | virtual void Generate(); |
| 789 | |
| 790 | private: |
| 791 | Token::Value op_; |
| 792 | int value_; |
| 793 | bool reversed_; |
| 794 | OverwriteMode overwrite_mode_; |
| 795 | }; |
| 796 | |
| 797 | |
| 798 | void DeferredInlineSmiOperation::Generate() { |
| 799 | switch (op_) { |
| 800 | case Token::ADD: { |
| 801 | // Revert optimistic add. |
| 802 | if (reversed_) { |
| 803 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 804 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 805 | } else { |
| 806 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 807 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 808 | } |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | case Token::SUB: { |
| 813 | // Revert optimistic sub. |
| 814 | if (reversed_) { |
| 815 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 816 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 817 | } else { |
| 818 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 819 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 820 | } |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | // For these operations there is no optimistic operation that needs to be |
| 825 | // reverted. |
| 826 | case Token::MUL: |
| 827 | case Token::MOD: |
| 828 | case Token::BIT_OR: |
| 829 | case Token::BIT_XOR: |
| 830 | case Token::BIT_AND: { |
| 831 | if (reversed_) { |
| 832 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 833 | } else { |
| 834 | __ mov(r1, Operand(r0)); |
| 835 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 836 | } |
| 837 | break; |
| 838 | } |
| 839 | |
| 840 | case Token::SHL: |
| 841 | case Token::SHR: |
| 842 | case Token::SAR: { |
| 843 | if (!reversed_) { |
| 844 | __ mov(r1, Operand(r0)); |
| 845 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 846 | } else { |
| 847 | UNREACHABLE(); // Should have been handled in SmiOperation. |
| 848 | } |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | default: |
| 853 | // Other cases should have been handled before this point. |
| 854 | UNREACHABLE(); |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
| 859 | __ CallStub(&stub); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 864 | x &= x - 1; |
| 865 | return (x & (x - 1)) == 0; |
| 866 | } |
| 867 | |
| 868 | |
| 869 | // Returns the index of the lowest bit set. |
| 870 | static int BitPosition(unsigned x) { |
| 871 | int bit_posn = 0; |
| 872 | while ((x & 0xf) == 0) { |
| 873 | bit_posn += 4; |
| 874 | x >>= 4; |
| 875 | } |
| 876 | while ((x & 1) == 0) { |
| 877 | bit_posn++; |
| 878 | x >>= 1; |
| 879 | } |
| 880 | return bit_posn; |
| 881 | } |
| 882 | |
| 883 | |
| 884 | void CodeGenerator::SmiOperation(Token::Value op, |
| 885 | Handle<Object> value, |
| 886 | bool reversed, |
| 887 | OverwriteMode mode) { |
| 888 | VirtualFrame::SpilledScope spilled_scope; |
| 889 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 890 | // some possible smi operations (like + and -) when (at least) one |
| 891 | // of the operands is a literal smi. With this optimization, the |
| 892 | // performance of the system is increased by ~15%, and the generated |
| 893 | // code size is increased by ~1% (measured on a combination of |
| 894 | // different benchmarks). |
| 895 | |
| 896 | // sp[0] : operand |
| 897 | |
| 898 | int int_value = Smi::cast(*value)->value(); |
| 899 | |
| 900 | JumpTarget exit; |
| 901 | frame_->EmitPop(r0); |
| 902 | |
| 903 | bool something_to_inline = true; |
| 904 | switch (op) { |
| 905 | case Token::ADD: { |
| 906 | DeferredCode* deferred = |
| 907 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 908 | |
| 909 | __ add(r0, r0, Operand(value), SetCC); |
| 910 | deferred->Branch(vs); |
| 911 | __ tst(r0, Operand(kSmiTagMask)); |
| 912 | deferred->Branch(ne); |
| 913 | deferred->BindExit(); |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | case Token::SUB: { |
| 918 | DeferredCode* deferred = |
| 919 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 920 | |
| 921 | if (reversed) { |
| 922 | __ rsb(r0, r0, Operand(value), SetCC); |
| 923 | } else { |
| 924 | __ sub(r0, r0, Operand(value), SetCC); |
| 925 | } |
| 926 | deferred->Branch(vs); |
| 927 | __ tst(r0, Operand(kSmiTagMask)); |
| 928 | deferred->Branch(ne); |
| 929 | deferred->BindExit(); |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | |
| 934 | case Token::BIT_OR: |
| 935 | case Token::BIT_XOR: |
| 936 | case Token::BIT_AND: { |
| 937 | DeferredCode* deferred = |
| 938 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 939 | __ tst(r0, Operand(kSmiTagMask)); |
| 940 | deferred->Branch(ne); |
| 941 | switch (op) { |
| 942 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 943 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 944 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 945 | default: UNREACHABLE(); |
| 946 | } |
| 947 | deferred->BindExit(); |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | case Token::SHL: |
| 952 | case Token::SHR: |
| 953 | case Token::SAR: { |
| 954 | if (reversed) { |
| 955 | something_to_inline = false; |
| 956 | break; |
| 957 | } |
| 958 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 959 | DeferredCode* deferred = |
| 960 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
| 961 | __ tst(r0, Operand(kSmiTagMask)); |
| 962 | deferred->Branch(ne); |
| 963 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 964 | switch (op) { |
| 965 | case Token::SHL: { |
| 966 | if (shift_value != 0) { |
| 967 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 968 | } |
| 969 | // check that the *unsigned* result fits in a smi |
| 970 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 971 | deferred->Branch(mi); |
| 972 | break; |
| 973 | } |
| 974 | case Token::SHR: { |
| 975 | // LSR by immediate 0 means shifting 32 bits. |
| 976 | if (shift_value != 0) { |
| 977 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 978 | } |
| 979 | // check that the *unsigned* result fits in a smi |
| 980 | // neither of the two high-order bits can be set: |
| 981 | // - 0x80000000: high bit would be lost when smi tagging |
| 982 | // - 0x40000000: this number would convert to negative when |
| 983 | // smi tagging these two cases can only happen with shifts |
| 984 | // by 0 or 1 when handed a valid smi |
| 985 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 986 | deferred->Branch(ne); |
| 987 | break; |
| 988 | } |
| 989 | case Token::SAR: { |
| 990 | if (shift_value != 0) { |
| 991 | // ASR by immediate 0 means shifting 32 bits. |
| 992 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 993 | } |
| 994 | break; |
| 995 | } |
| 996 | default: UNREACHABLE(); |
| 997 | } |
| 998 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 999 | deferred->BindExit(); |
| 1000 | break; |
| 1001 | } |
| 1002 | |
| 1003 | case Token::MOD: { |
| 1004 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 1005 | something_to_inline = false; |
| 1006 | break; |
| 1007 | } |
| 1008 | DeferredCode* deferred = |
| 1009 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1010 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1011 | __ tst(r0, Operand(mask)); |
| 1012 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1013 | mask = (int_value << kSmiTagSize) - 1; |
| 1014 | __ and_(r0, r0, Operand(mask)); |
| 1015 | deferred->BindExit(); |
| 1016 | break; |
| 1017 | } |
| 1018 | |
| 1019 | case Token::MUL: { |
| 1020 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1021 | something_to_inline = false; |
| 1022 | break; |
| 1023 | } |
| 1024 | DeferredCode* deferred = |
| 1025 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1026 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1027 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1028 | unsigned mask = 0x80000000u; |
| 1029 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1030 | mask |= mask >> 1; |
| 1031 | } |
| 1032 | mask |= kSmiTagMask; |
| 1033 | // This does a single mask that checks for a too high value in a |
| 1034 | // conservative way and for a non-Smi. It also filters out negative |
| 1035 | // numbers, unfortunately, but since this code is inline we prefer |
| 1036 | // brevity to comprehensiveness. |
| 1037 | __ tst(r0, Operand(mask)); |
| 1038 | deferred->Branch(ne); |
| 1039 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 1040 | deferred->BindExit(); |
| 1041 | break; |
| 1042 | } |
| 1043 | |
| 1044 | default: |
| 1045 | something_to_inline = false; |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | if (!something_to_inline) { |
| 1050 | if (!reversed) { |
| 1051 | frame_->EmitPush(r0); |
| 1052 | __ mov(r0, Operand(value)); |
| 1053 | frame_->EmitPush(r0); |
| 1054 | GenericBinaryOperation(op, mode, int_value); |
| 1055 | } else { |
| 1056 | __ mov(ip, Operand(value)); |
| 1057 | frame_->EmitPush(ip); |
| 1058 | frame_->EmitPush(r0); |
| 1059 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | exit.Bind(); |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | void CodeGenerator::Comparison(Condition cc, |
| 1068 | Expression* left, |
| 1069 | Expression* right, |
| 1070 | bool strict) { |
| 1071 | if (left != NULL) LoadAndSpill(left); |
| 1072 | if (right != NULL) LoadAndSpill(right); |
| 1073 | |
| 1074 | VirtualFrame::SpilledScope spilled_scope; |
| 1075 | // sp[0] : y |
| 1076 | // sp[1] : x |
| 1077 | // result : cc register |
| 1078 | |
| 1079 | // Strict only makes sense for equality comparisons. |
| 1080 | ASSERT(!strict || cc == eq); |
| 1081 | |
| 1082 | JumpTarget exit; |
| 1083 | JumpTarget smi; |
| 1084 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1085 | if (cc == gt || cc == le) { |
| 1086 | cc = ReverseCondition(cc); |
| 1087 | frame_->EmitPop(r1); |
| 1088 | frame_->EmitPop(r0); |
| 1089 | } else { |
| 1090 | frame_->EmitPop(r0); |
| 1091 | frame_->EmitPop(r1); |
| 1092 | } |
| 1093 | __ orr(r2, r0, Operand(r1)); |
| 1094 | __ tst(r2, Operand(kSmiTagMask)); |
| 1095 | smi.Branch(eq); |
| 1096 | |
| 1097 | // Perform non-smi comparison by stub. |
| 1098 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1099 | // We call with 0 args because there are 0 on the stack. |
| 1100 | CompareStub stub(cc, strict); |
| 1101 | frame_->CallStub(&stub, 0); |
| 1102 | __ cmp(r0, Operand(0)); |
| 1103 | exit.Jump(); |
| 1104 | |
| 1105 | // Do smi comparisons by pointer comparison. |
| 1106 | smi.Bind(); |
| 1107 | __ cmp(r1, Operand(r0)); |
| 1108 | |
| 1109 | exit.Bind(); |
| 1110 | cc_reg_ = cc; |
| 1111 | } |
| 1112 | |
| 1113 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1114 | // Call the function on the stack with the given arguments. |
| 1115 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1116 | CallFunctionFlags flags, |
| 1117 | int position) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1118 | VirtualFrame::SpilledScope spilled_scope; |
| 1119 | // Push the arguments ("left-to-right") on the stack. |
| 1120 | int arg_count = args->length(); |
| 1121 | for (int i = 0; i < arg_count; i++) { |
| 1122 | LoadAndSpill(args->at(i)); |
| 1123 | } |
| 1124 | |
| 1125 | // Record the position for debugging purposes. |
| 1126 | CodeForSourcePosition(position); |
| 1127 | |
| 1128 | // Use the shared code stub to call the function. |
| 1129 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1130 | CallFunctionStub call_function(arg_count, in_loop, flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1131 | frame_->CallStub(&call_function, arg_count + 1); |
| 1132 | |
| 1133 | // Restore context and pop function from the stack. |
| 1134 | __ ldr(cp, frame_->Context()); |
| 1135 | frame_->Drop(); // discard the TOS |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
| 1140 | VirtualFrame::SpilledScope spilled_scope; |
| 1141 | ASSERT(has_cc()); |
| 1142 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
| 1143 | target->Branch(cc); |
| 1144 | cc_reg_ = al; |
| 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | void CodeGenerator::CheckStack() { |
| 1149 | VirtualFrame::SpilledScope spilled_scope; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1150 | Comment cmnt(masm_, "[ check stack"); |
| 1151 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1152 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1153 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1154 | // gives a return address 12 bytes down. |
| 1155 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1156 | masm_->cmp(sp, Operand(ip)); |
| 1157 | StackCheckStub stub; |
| 1158 | // Call the stub if lower. |
| 1159 | masm_->mov(pc, |
| 1160 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1161 | RelocInfo::CODE_TARGET), |
| 1162 | LeaveCC, |
| 1163 | lo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1168 | #ifdef DEBUG |
| 1169 | int original_height = frame_->height(); |
| 1170 | #endif |
| 1171 | VirtualFrame::SpilledScope spilled_scope; |
| 1172 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1173 | VisitAndSpill(statements->at(i)); |
| 1174 | } |
| 1175 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | void CodeGenerator::VisitBlock(Block* node) { |
| 1180 | #ifdef DEBUG |
| 1181 | int original_height = frame_->height(); |
| 1182 | #endif |
| 1183 | VirtualFrame::SpilledScope spilled_scope; |
| 1184 | Comment cmnt(masm_, "[ Block"); |
| 1185 | CodeForStatementPosition(node); |
| 1186 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1187 | VisitStatementsAndSpill(node->statements()); |
| 1188 | if (node->break_target()->is_linked()) { |
| 1189 | node->break_target()->Bind(); |
| 1190 | } |
| 1191 | node->break_target()->Unuse(); |
| 1192 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1193 | } |
| 1194 | |
| 1195 | |
| 1196 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
| 1197 | VirtualFrame::SpilledScope spilled_scope; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1198 | frame_->EmitPush(cp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1199 | __ mov(r0, Operand(pairs)); |
| 1200 | frame_->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1201 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
| 1202 | frame_->EmitPush(r0); |
| 1203 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
| 1204 | // The result is discarded. |
| 1205 | } |
| 1206 | |
| 1207 | |
| 1208 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
| 1209 | #ifdef DEBUG |
| 1210 | int original_height = frame_->height(); |
| 1211 | #endif |
| 1212 | VirtualFrame::SpilledScope spilled_scope; |
| 1213 | Comment cmnt(masm_, "[ Declaration"); |
| 1214 | Variable* var = node->proxy()->var(); |
| 1215 | ASSERT(var != NULL); // must have been resolved |
| 1216 | Slot* slot = var->slot(); |
| 1217 | |
| 1218 | // If it was not possible to allocate the variable at compile time, |
| 1219 | // we need to "declare" it at runtime to make sure it actually |
| 1220 | // exists in the local context. |
| 1221 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1222 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1223 | // during variable resolution and must have mode DYNAMIC. |
| 1224 | ASSERT(var->is_dynamic()); |
| 1225 | // For now, just do a runtime call. |
| 1226 | frame_->EmitPush(cp); |
| 1227 | __ mov(r0, Operand(var->name())); |
| 1228 | frame_->EmitPush(r0); |
| 1229 | // Declaration nodes are always declared in only two modes. |
| 1230 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1231 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
| 1232 | __ mov(r0, Operand(Smi::FromInt(attr))); |
| 1233 | frame_->EmitPush(r0); |
| 1234 | // Push initial value, if any. |
| 1235 | // Note: For variables we must not push an initial value (such as |
| 1236 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1237 | // must not destroy the current value. |
| 1238 | if (node->mode() == Variable::CONST) { |
| 1239 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
| 1240 | frame_->EmitPush(r0); |
| 1241 | } else if (node->fun() != NULL) { |
| 1242 | LoadAndSpill(node->fun()); |
| 1243 | } else { |
| 1244 | __ mov(r0, Operand(0)); // no initial value! |
| 1245 | frame_->EmitPush(r0); |
| 1246 | } |
| 1247 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
| 1248 | // Ignore the return value (declarations are statements). |
| 1249 | ASSERT(frame_->height() == original_height); |
| 1250 | return; |
| 1251 | } |
| 1252 | |
| 1253 | ASSERT(!var->is_global()); |
| 1254 | |
| 1255 | // If we have a function or a constant, we need to initialize the variable. |
| 1256 | Expression* val = NULL; |
| 1257 | if (node->mode() == Variable::CONST) { |
| 1258 | val = new Literal(Factory::the_hole_value()); |
| 1259 | } else { |
| 1260 | val = node->fun(); // NULL if we don't have a function |
| 1261 | } |
| 1262 | |
| 1263 | if (val != NULL) { |
| 1264 | { |
| 1265 | // Set initial value. |
| 1266 | Reference target(this, node->proxy()); |
| 1267 | LoadAndSpill(val); |
| 1268 | target.SetValue(NOT_CONST_INIT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1269 | } |
| 1270 | // Get rid of the assigned value (declarations are statements). |
| 1271 | frame_->Drop(); |
| 1272 | } |
| 1273 | ASSERT(frame_->height() == original_height); |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
| 1278 | #ifdef DEBUG |
| 1279 | int original_height = frame_->height(); |
| 1280 | #endif |
| 1281 | VirtualFrame::SpilledScope spilled_scope; |
| 1282 | Comment cmnt(masm_, "[ ExpressionStatement"); |
| 1283 | CodeForStatementPosition(node); |
| 1284 | Expression* expression = node->expression(); |
| 1285 | expression->MarkAsStatement(); |
| 1286 | LoadAndSpill(expression); |
| 1287 | frame_->Drop(); |
| 1288 | ASSERT(frame_->height() == original_height); |
| 1289 | } |
| 1290 | |
| 1291 | |
| 1292 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
| 1293 | #ifdef DEBUG |
| 1294 | int original_height = frame_->height(); |
| 1295 | #endif |
| 1296 | VirtualFrame::SpilledScope spilled_scope; |
| 1297 | Comment cmnt(masm_, "// EmptyStatement"); |
| 1298 | CodeForStatementPosition(node); |
| 1299 | // nothing to do |
| 1300 | ASSERT(frame_->height() == original_height); |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
| 1305 | #ifdef DEBUG |
| 1306 | int original_height = frame_->height(); |
| 1307 | #endif |
| 1308 | VirtualFrame::SpilledScope spilled_scope; |
| 1309 | Comment cmnt(masm_, "[ IfStatement"); |
| 1310 | // Generate different code depending on which parts of the if statement |
| 1311 | // are present or not. |
| 1312 | bool has_then_stm = node->HasThenStatement(); |
| 1313 | bool has_else_stm = node->HasElseStatement(); |
| 1314 | |
| 1315 | CodeForStatementPosition(node); |
| 1316 | |
| 1317 | JumpTarget exit; |
| 1318 | if (has_then_stm && has_else_stm) { |
| 1319 | Comment cmnt(masm_, "[ IfThenElse"); |
| 1320 | JumpTarget then; |
| 1321 | JumpTarget else_; |
| 1322 | // if (cond) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1323 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1324 | if (frame_ != NULL) { |
| 1325 | Branch(false, &else_); |
| 1326 | } |
| 1327 | // then |
| 1328 | if (frame_ != NULL || then.is_linked()) { |
| 1329 | then.Bind(); |
| 1330 | VisitAndSpill(node->then_statement()); |
| 1331 | } |
| 1332 | if (frame_ != NULL) { |
| 1333 | exit.Jump(); |
| 1334 | } |
| 1335 | // else |
| 1336 | if (else_.is_linked()) { |
| 1337 | else_.Bind(); |
| 1338 | VisitAndSpill(node->else_statement()); |
| 1339 | } |
| 1340 | |
| 1341 | } else if (has_then_stm) { |
| 1342 | Comment cmnt(masm_, "[ IfThen"); |
| 1343 | ASSERT(!has_else_stm); |
| 1344 | JumpTarget then; |
| 1345 | // if (cond) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1346 | LoadConditionAndSpill(node->condition(), &then, &exit, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1347 | if (frame_ != NULL) { |
| 1348 | Branch(false, &exit); |
| 1349 | } |
| 1350 | // then |
| 1351 | if (frame_ != NULL || then.is_linked()) { |
| 1352 | then.Bind(); |
| 1353 | VisitAndSpill(node->then_statement()); |
| 1354 | } |
| 1355 | |
| 1356 | } else if (has_else_stm) { |
| 1357 | Comment cmnt(masm_, "[ IfElse"); |
| 1358 | ASSERT(!has_then_stm); |
| 1359 | JumpTarget else_; |
| 1360 | // if (!cond) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1361 | LoadConditionAndSpill(node->condition(), &exit, &else_, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1362 | if (frame_ != NULL) { |
| 1363 | Branch(true, &exit); |
| 1364 | } |
| 1365 | // else |
| 1366 | if (frame_ != NULL || else_.is_linked()) { |
| 1367 | else_.Bind(); |
| 1368 | VisitAndSpill(node->else_statement()); |
| 1369 | } |
| 1370 | |
| 1371 | } else { |
| 1372 | Comment cmnt(masm_, "[ If"); |
| 1373 | ASSERT(!has_then_stm && !has_else_stm); |
| 1374 | // if (cond) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1375 | LoadConditionAndSpill(node->condition(), &exit, &exit, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1376 | if (frame_ != NULL) { |
| 1377 | if (has_cc()) { |
| 1378 | cc_reg_ = al; |
| 1379 | } else { |
| 1380 | frame_->Drop(); |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | // end |
| 1386 | if (exit.is_linked()) { |
| 1387 | exit.Bind(); |
| 1388 | } |
| 1389 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1390 | } |
| 1391 | |
| 1392 | |
| 1393 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
| 1394 | VirtualFrame::SpilledScope spilled_scope; |
| 1395 | Comment cmnt(masm_, "[ ContinueStatement"); |
| 1396 | CodeForStatementPosition(node); |
| 1397 | node->target()->continue_target()->Jump(); |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
| 1402 | VirtualFrame::SpilledScope spilled_scope; |
| 1403 | Comment cmnt(masm_, "[ BreakStatement"); |
| 1404 | CodeForStatementPosition(node); |
| 1405 | node->target()->break_target()->Jump(); |
| 1406 | } |
| 1407 | |
| 1408 | |
| 1409 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
| 1410 | VirtualFrame::SpilledScope spilled_scope; |
| 1411 | Comment cmnt(masm_, "[ ReturnStatement"); |
| 1412 | |
| 1413 | CodeForStatementPosition(node); |
| 1414 | LoadAndSpill(node->expression()); |
| 1415 | if (function_return_is_shadowed_) { |
| 1416 | frame_->EmitPop(r0); |
| 1417 | function_return_.Jump(); |
| 1418 | } else { |
| 1419 | // Pop the result from the frame and prepare the frame for |
| 1420 | // returning thus making it easier to merge. |
| 1421 | frame_->EmitPop(r0); |
| 1422 | frame_->PrepareForReturn(); |
| 1423 | |
| 1424 | function_return_.Jump(); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
| 1430 | #ifdef DEBUG |
| 1431 | int original_height = frame_->height(); |
| 1432 | #endif |
| 1433 | VirtualFrame::SpilledScope spilled_scope; |
| 1434 | Comment cmnt(masm_, "[ WithEnterStatement"); |
| 1435 | CodeForStatementPosition(node); |
| 1436 | LoadAndSpill(node->expression()); |
| 1437 | if (node->is_catch_block()) { |
| 1438 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
| 1439 | } else { |
| 1440 | frame_->CallRuntime(Runtime::kPushContext, 1); |
| 1441 | } |
| 1442 | #ifdef DEBUG |
| 1443 | JumpTarget verified_true; |
| 1444 | __ cmp(r0, Operand(cp)); |
| 1445 | verified_true.Branch(eq); |
| 1446 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1447 | verified_true.Bind(); |
| 1448 | #endif |
| 1449 | // Update context local. |
| 1450 | __ str(cp, frame_->Context()); |
| 1451 | ASSERT(frame_->height() == original_height); |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
| 1456 | #ifdef DEBUG |
| 1457 | int original_height = frame_->height(); |
| 1458 | #endif |
| 1459 | VirtualFrame::SpilledScope spilled_scope; |
| 1460 | Comment cmnt(masm_, "[ WithExitStatement"); |
| 1461 | CodeForStatementPosition(node); |
| 1462 | // Pop context. |
| 1463 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1464 | // Update context local. |
| 1465 | __ str(cp, frame_->Context()); |
| 1466 | ASSERT(frame_->height() == original_height); |
| 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
| 1471 | #ifdef DEBUG |
| 1472 | int original_height = frame_->height(); |
| 1473 | #endif |
| 1474 | VirtualFrame::SpilledScope spilled_scope; |
| 1475 | Comment cmnt(masm_, "[ SwitchStatement"); |
| 1476 | CodeForStatementPosition(node); |
| 1477 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1478 | |
| 1479 | LoadAndSpill(node->tag()); |
| 1480 | |
| 1481 | JumpTarget next_test; |
| 1482 | JumpTarget fall_through; |
| 1483 | JumpTarget default_entry; |
| 1484 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
| 1485 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1486 | int length = cases->length(); |
| 1487 | CaseClause* default_clause = NULL; |
| 1488 | |
| 1489 | for (int i = 0; i < length; i++) { |
| 1490 | CaseClause* clause = cases->at(i); |
| 1491 | if (clause->is_default()) { |
| 1492 | // Remember the default clause and compile it at the end. |
| 1493 | default_clause = clause; |
| 1494 | continue; |
| 1495 | } |
| 1496 | |
| 1497 | Comment cmnt(masm_, "[ Case clause"); |
| 1498 | // Compile the test. |
| 1499 | next_test.Bind(); |
| 1500 | next_test.Unuse(); |
| 1501 | // Duplicate TOS. |
| 1502 | __ ldr(r0, frame_->Top()); |
| 1503 | frame_->EmitPush(r0); |
| 1504 | Comparison(eq, NULL, clause->label(), true); |
| 1505 | Branch(false, &next_test); |
| 1506 | |
| 1507 | // Before entering the body from the test, remove the switch value from |
| 1508 | // the stack. |
| 1509 | frame_->Drop(); |
| 1510 | |
| 1511 | // Label the body so that fall through is enabled. |
| 1512 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1513 | default_exit.Bind(); |
| 1514 | } else { |
| 1515 | fall_through.Bind(); |
| 1516 | fall_through.Unuse(); |
| 1517 | } |
| 1518 | VisitStatementsAndSpill(clause->statements()); |
| 1519 | |
| 1520 | // If control flow can fall through from the body, jump to the next body |
| 1521 | // or the end of the statement. |
| 1522 | if (frame_ != NULL) { |
| 1523 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1524 | default_entry.Jump(); |
| 1525 | } else { |
| 1526 | fall_through.Jump(); |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | // The final "test" removes the switch value. |
| 1532 | next_test.Bind(); |
| 1533 | frame_->Drop(); |
| 1534 | |
| 1535 | // If there is a default clause, compile it. |
| 1536 | if (default_clause != NULL) { |
| 1537 | Comment cmnt(masm_, "[ Default clause"); |
| 1538 | default_entry.Bind(); |
| 1539 | VisitStatementsAndSpill(default_clause->statements()); |
| 1540 | // If control flow can fall out of the default and there is a case after |
| 1541 | // it, jup to that case's body. |
| 1542 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1543 | default_exit.Jump(); |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | if (fall_through.is_linked()) { |
| 1548 | fall_through.Bind(); |
| 1549 | } |
| 1550 | |
| 1551 | if (node->break_target()->is_linked()) { |
| 1552 | node->break_target()->Bind(); |
| 1553 | } |
| 1554 | node->break_target()->Unuse(); |
| 1555 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1556 | } |
| 1557 | |
| 1558 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1559 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1560 | #ifdef DEBUG |
| 1561 | int original_height = frame_->height(); |
| 1562 | #endif |
| 1563 | VirtualFrame::SpilledScope spilled_scope; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1564 | Comment cmnt(masm_, "[ DoWhileStatement"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1565 | CodeForStatementPosition(node); |
| 1566 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1567 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1568 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1569 | // Label the top of the loop for the backward CFG edge. If the test |
| 1570 | // is always true we can use the continue target, and if the test is |
| 1571 | // always false there is no need. |
| 1572 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1573 | switch (info) { |
| 1574 | case ALWAYS_TRUE: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1575 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1576 | node->continue_target()->Bind(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1577 | break; |
| 1578 | case ALWAYS_FALSE: |
| 1579 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1580 | break; |
| 1581 | case DONT_KNOW: |
| 1582 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1583 | body.Bind(); |
| 1584 | break; |
| 1585 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1586 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1587 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1588 | VisitAndSpill(node->body()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1589 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1590 | // Compile the test. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1591 | switch (info) { |
| 1592 | case ALWAYS_TRUE: |
| 1593 | // If control can fall off the end of the body, jump back to the |
| 1594 | // top. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1595 | if (has_valid_frame()) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1596 | node->continue_target()->Jump(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1597 | } |
| 1598 | break; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1599 | case ALWAYS_FALSE: |
| 1600 | // If we have a continue in the body, we only have to bind its |
| 1601 | // jump target. |
| 1602 | if (node->continue_target()->is_linked()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1603 | node->continue_target()->Bind(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1604 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1605 | break; |
| 1606 | case DONT_KNOW: |
| 1607 | // We have to compile the test expression if it can be reached by |
| 1608 | // control flow falling out of the body or via continue. |
| 1609 | if (node->continue_target()->is_linked()) { |
| 1610 | node->continue_target()->Bind(); |
| 1611 | } |
| 1612 | if (has_valid_frame()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1613 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 1614 | CodeForDoWhileConditionPosition(node); |
| 1615 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1616 | if (has_valid_frame()) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1617 | // A invalid frame here indicates that control did not |
| 1618 | // fall out of the test expression. |
| 1619 | Branch(true, &body); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | if (node->break_target()->is_linked()) { |
| 1626 | node->break_target()->Bind(); |
| 1627 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1628 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1629 | } |
| 1630 | |
| 1631 | |
| 1632 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 1633 | #ifdef DEBUG |
| 1634 | int original_height = frame_->height(); |
| 1635 | #endif |
| 1636 | VirtualFrame::SpilledScope spilled_scope; |
| 1637 | Comment cmnt(masm_, "[ WhileStatement"); |
| 1638 | CodeForStatementPosition(node); |
| 1639 | |
| 1640 | // If the test is never true and has no side effects there is no need |
| 1641 | // to compile the test or body. |
| 1642 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1643 | if (info == ALWAYS_FALSE) return; |
| 1644 | |
| 1645 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1646 | |
| 1647 | // Label the top of the loop with the continue target for the backward |
| 1648 | // CFG edge. |
| 1649 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1650 | node->continue_target()->Bind(); |
| 1651 | |
| 1652 | if (info == DONT_KNOW) { |
| 1653 | JumpTarget body; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1654 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1655 | if (has_valid_frame()) { |
| 1656 | // A NULL frame indicates that control did not fall out of the |
| 1657 | // test expression. |
| 1658 | Branch(false, node->break_target()); |
| 1659 | } |
| 1660 | if (has_valid_frame() || body.is_linked()) { |
| 1661 | body.Bind(); |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | if (has_valid_frame()) { |
| 1666 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1667 | VisitAndSpill(node->body()); |
| 1668 | |
| 1669 | // If control flow can fall out of the body, jump back to the top. |
| 1670 | if (has_valid_frame()) { |
| 1671 | node->continue_target()->Jump(); |
| 1672 | } |
| 1673 | } |
| 1674 | if (node->break_target()->is_linked()) { |
| 1675 | node->break_target()->Bind(); |
| 1676 | } |
| 1677 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1678 | } |
| 1679 | |
| 1680 | |
| 1681 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 1682 | #ifdef DEBUG |
| 1683 | int original_height = frame_->height(); |
| 1684 | #endif |
| 1685 | VirtualFrame::SpilledScope spilled_scope; |
| 1686 | Comment cmnt(masm_, "[ ForStatement"); |
| 1687 | CodeForStatementPosition(node); |
| 1688 | if (node->init() != NULL) { |
| 1689 | VisitAndSpill(node->init()); |
| 1690 | } |
| 1691 | |
| 1692 | // If the test is never true there is no need to compile the test or |
| 1693 | // body. |
| 1694 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1695 | if (info == ALWAYS_FALSE) return; |
| 1696 | |
| 1697 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1698 | |
| 1699 | // If there is no update statement, label the top of the loop with the |
| 1700 | // continue target, otherwise with the loop target. |
| 1701 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1702 | if (node->next() == NULL) { |
| 1703 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1704 | node->continue_target()->Bind(); |
| 1705 | } else { |
| 1706 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1707 | loop.Bind(); |
| 1708 | } |
| 1709 | |
| 1710 | // If the test is always true, there is no need to compile it. |
| 1711 | if (info == DONT_KNOW) { |
| 1712 | JumpTarget body; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1713 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1714 | if (has_valid_frame()) { |
| 1715 | Branch(false, node->break_target()); |
| 1716 | } |
| 1717 | if (has_valid_frame() || body.is_linked()) { |
| 1718 | body.Bind(); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | if (has_valid_frame()) { |
| 1723 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1724 | VisitAndSpill(node->body()); |
| 1725 | |
| 1726 | if (node->next() == NULL) { |
| 1727 | // If there is no update statement and control flow can fall out |
| 1728 | // of the loop, jump directly to the continue label. |
| 1729 | if (has_valid_frame()) { |
| 1730 | node->continue_target()->Jump(); |
| 1731 | } |
| 1732 | } else { |
| 1733 | // If there is an update statement and control flow can reach it |
| 1734 | // via falling out of the body of the loop or continuing, we |
| 1735 | // compile the update statement. |
| 1736 | if (node->continue_target()->is_linked()) { |
| 1737 | node->continue_target()->Bind(); |
| 1738 | } |
| 1739 | if (has_valid_frame()) { |
| 1740 | // Record source position of the statement as this code which is |
| 1741 | // after the code for the body actually belongs to the loop |
| 1742 | // statement and not the body. |
| 1743 | CodeForStatementPosition(node); |
| 1744 | VisitAndSpill(node->next()); |
| 1745 | loop.Jump(); |
| 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | if (node->break_target()->is_linked()) { |
| 1750 | node->break_target()->Bind(); |
| 1751 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1752 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1753 | } |
| 1754 | |
| 1755 | |
| 1756 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
| 1757 | #ifdef DEBUG |
| 1758 | int original_height = frame_->height(); |
| 1759 | #endif |
| 1760 | VirtualFrame::SpilledScope spilled_scope; |
| 1761 | Comment cmnt(masm_, "[ ForInStatement"); |
| 1762 | CodeForStatementPosition(node); |
| 1763 | |
| 1764 | JumpTarget primitive; |
| 1765 | JumpTarget jsobject; |
| 1766 | JumpTarget fixed_array; |
| 1767 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1768 | JumpTarget end_del_check; |
| 1769 | JumpTarget exit; |
| 1770 | |
| 1771 | // Get the object to enumerate over (converted to JSObject). |
| 1772 | LoadAndSpill(node->enumerable()); |
| 1773 | |
| 1774 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1775 | // to the specification. 12.6.4 mandates a call to ToObject. |
| 1776 | frame_->EmitPop(r0); |
| 1777 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 1778 | __ cmp(r0, ip); |
| 1779 | exit.Branch(eq); |
| 1780 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1781 | __ cmp(r0, ip); |
| 1782 | exit.Branch(eq); |
| 1783 | |
| 1784 | // Stack layout in body: |
| 1785 | // [iteration counter (Smi)] |
| 1786 | // [length of array] |
| 1787 | // [FixedArray] |
| 1788 | // [Map or 0] |
| 1789 | // [Object] |
| 1790 | |
| 1791 | // Check if enumerable is already a JSObject |
| 1792 | __ tst(r0, Operand(kSmiTagMask)); |
| 1793 | primitive.Branch(eq); |
| 1794 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
| 1795 | jsobject.Branch(hs); |
| 1796 | |
| 1797 | primitive.Bind(); |
| 1798 | frame_->EmitPush(r0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1799 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1800 | |
| 1801 | jsobject.Bind(); |
| 1802 | // Get the set of properties (as a FixedArray or Map). |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1803 | // r0: value to be iterated over |
| 1804 | frame_->EmitPush(r0); // Push the object being iterated over. |
| 1805 | |
| 1806 | // Check cache validity in generated code. This is a fast case for |
| 1807 | // the JSObject::IsSimpleEnum cache validity checks. If we cannot |
| 1808 | // guarantee cache validity, call the runtime system to check cache |
| 1809 | // validity or get the property names in a fixed array. |
| 1810 | JumpTarget call_runtime; |
| 1811 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1812 | JumpTarget check_prototype; |
| 1813 | JumpTarget use_cache; |
| 1814 | __ mov(r1, Operand(r0)); |
| 1815 | loop.Bind(); |
| 1816 | // Check that there are no elements. |
| 1817 | __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 1818 | __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex); |
| 1819 | __ cmp(r2, r4); |
| 1820 | call_runtime.Branch(ne); |
| 1821 | // Check that instance descriptors are not empty so that we can |
| 1822 | // check for an enum cache. Leave the map in r3 for the subsequent |
| 1823 | // prototype load. |
| 1824 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1825 | __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset)); |
| 1826 | __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex); |
| 1827 | __ cmp(r2, ip); |
| 1828 | call_runtime.Branch(eq); |
| 1829 | // Check that there in an enum cache in the non-empty instance |
| 1830 | // descriptors. This is the case if the next enumeration index |
| 1831 | // field does not contain a smi. |
| 1832 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset)); |
| 1833 | __ tst(r2, Operand(kSmiTagMask)); |
| 1834 | call_runtime.Branch(eq); |
| 1835 | // For all objects but the receiver, check that the cache is empty. |
| 1836 | // r4: empty fixed array root. |
| 1837 | __ cmp(r1, r0); |
| 1838 | check_prototype.Branch(eq); |
| 1839 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1840 | __ cmp(r2, r4); |
| 1841 | call_runtime.Branch(ne); |
| 1842 | check_prototype.Bind(); |
| 1843 | // Load the prototype from the map and loop if non-null. |
| 1844 | __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 1845 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1846 | __ cmp(r1, ip); |
| 1847 | loop.Branch(ne); |
| 1848 | // The enum cache is valid. Load the map of the object being |
| 1849 | // iterated over and use the cache for the iteration. |
| 1850 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1851 | use_cache.Jump(); |
| 1852 | |
| 1853 | call_runtime.Bind(); |
| 1854 | // Call the runtime to get the property names for the object. |
| 1855 | frame_->EmitPush(r0); // push the object (slot 4) for the runtime call |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1856 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
| 1857 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1858 | // If we got a map from the runtime call, we can do a fast |
| 1859 | // modification check. Otherwise, we got a fixed array, and we have |
| 1860 | // to do a slow check. |
| 1861 | // r0: map or fixed array (result from call to |
| 1862 | // Runtime::kGetPropertyNamesFast) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1863 | __ mov(r2, Operand(r0)); |
| 1864 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1865 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 1866 | __ cmp(r1, ip); |
| 1867 | fixed_array.Branch(ne); |
| 1868 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1869 | use_cache.Bind(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1870 | // Get enum cache |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1871 | // r0: map (either the result from a call to |
| 1872 | // Runtime::kGetPropertyNamesFast or has been fetched directly from |
| 1873 | // the object) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1874 | __ mov(r1, Operand(r0)); |
| 1875 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1876 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1877 | __ ldr(r2, |
| 1878 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1879 | |
| 1880 | frame_->EmitPush(r0); // map |
| 1881 | frame_->EmitPush(r2); // enum cache bridge cache |
| 1882 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
| 1883 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 1884 | frame_->EmitPush(r0); |
| 1885 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 1886 | frame_->EmitPush(r0); |
| 1887 | entry.Jump(); |
| 1888 | |
| 1889 | fixed_array.Bind(); |
| 1890 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 1891 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1892 | frame_->EmitPush(r0); |
| 1893 | |
| 1894 | // Push the length of the array and the initial index onto the stack. |
| 1895 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
| 1896 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 1897 | frame_->EmitPush(r0); |
| 1898 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
| 1899 | frame_->EmitPush(r0); |
| 1900 | |
| 1901 | // Condition. |
| 1902 | entry.Bind(); |
| 1903 | // sp[0] : index |
| 1904 | // sp[1] : array/enum cache length |
| 1905 | // sp[2] : array or enum cache |
| 1906 | // sp[3] : 0 or map |
| 1907 | // sp[4] : enumerable |
| 1908 | // Grab the current frame's height for the break and continue |
| 1909 | // targets only after all the state is pushed on the frame. |
| 1910 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1911 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1912 | |
| 1913 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1914 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1915 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1916 | node->break_target()->Branch(hs); |
| 1917 | |
| 1918 | __ ldr(r0, frame_->ElementAt(0)); |
| 1919 | |
| 1920 | // Get the i'th entry of the array. |
| 1921 | __ ldr(r2, frame_->ElementAt(2)); |
| 1922 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1923 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1924 | |
| 1925 | // Get Map or 0. |
| 1926 | __ ldr(r2, frame_->ElementAt(3)); |
| 1927 | // Check if this (still) matches the map of the enumerable. |
| 1928 | // If not, we have to filter the key. |
| 1929 | __ ldr(r1, frame_->ElementAt(4)); |
| 1930 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1931 | __ cmp(r1, Operand(r2)); |
| 1932 | end_del_check.Branch(eq); |
| 1933 | |
| 1934 | // Convert the entry to a string (or null if it isn't a property anymore). |
| 1935 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1936 | frame_->EmitPush(r0); |
| 1937 | frame_->EmitPush(r3); // push entry |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1938 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1939 | __ mov(r3, Operand(r0)); |
| 1940 | |
| 1941 | // If the property has been removed while iterating, we just skip it. |
| 1942 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1943 | __ cmp(r3, ip); |
| 1944 | node->continue_target()->Branch(eq); |
| 1945 | |
| 1946 | end_del_check.Bind(); |
| 1947 | // Store the entry in the 'each' expression and take another spin in the |
| 1948 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1949 | frame_->EmitPush(r3); // push entry |
| 1950 | { Reference each(this, node->each()); |
| 1951 | if (!each.is_illegal()) { |
| 1952 | if (each.size() > 0) { |
| 1953 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1954 | frame_->EmitPush(r0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1955 | each.SetValue(NOT_CONST_INIT); |
| 1956 | frame_->Drop(2); |
| 1957 | } else { |
| 1958 | // If the reference was to a slot we rely on the convenient property |
| 1959 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1960 | // right on top of or right underneath a zero-sized reference. |
| 1961 | each.SetValue(NOT_CONST_INIT); |
| 1962 | frame_->Drop(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1966 | // Body. |
| 1967 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1968 | VisitAndSpill(node->body()); |
| 1969 | |
| 1970 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1971 | // a continue in the body. |
| 1972 | node->continue_target()->Bind(); |
| 1973 | frame_->SpillAll(); |
| 1974 | frame_->EmitPop(r0); |
| 1975 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1976 | frame_->EmitPush(r0); |
| 1977 | entry.Jump(); |
| 1978 | |
| 1979 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1980 | // any frame. |
| 1981 | node->break_target()->Bind(); |
| 1982 | frame_->Drop(5); |
| 1983 | |
| 1984 | // Exit. |
| 1985 | exit.Bind(); |
| 1986 | node->continue_target()->Unuse(); |
| 1987 | node->break_target()->Unuse(); |
| 1988 | ASSERT(frame_->height() == original_height); |
| 1989 | } |
| 1990 | |
| 1991 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1992 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1993 | #ifdef DEBUG |
| 1994 | int original_height = frame_->height(); |
| 1995 | #endif |
| 1996 | VirtualFrame::SpilledScope spilled_scope; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1997 | Comment cmnt(masm_, "[ TryCatchStatement"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1998 | CodeForStatementPosition(node); |
| 1999 | |
| 2000 | JumpTarget try_block; |
| 2001 | JumpTarget exit; |
| 2002 | |
| 2003 | try_block.Call(); |
| 2004 | // --- Catch block --- |
| 2005 | frame_->EmitPush(r0); |
| 2006 | |
| 2007 | // Store the caught exception in the catch variable. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2008 | Variable* catch_var = node->catch_var()->var(); |
| 2009 | ASSERT(catch_var != NULL && catch_var->slot() != NULL); |
| 2010 | StoreToSlot(catch_var->slot(), NOT_CONST_INIT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2011 | |
| 2012 | // Remove the exception from the stack. |
| 2013 | frame_->Drop(); |
| 2014 | |
| 2015 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2016 | if (frame_ != NULL) { |
| 2017 | exit.Jump(); |
| 2018 | } |
| 2019 | |
| 2020 | |
| 2021 | // --- Try block --- |
| 2022 | try_block.Bind(); |
| 2023 | |
| 2024 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2025 | int handler_height = frame_->height(); |
| 2026 | |
| 2027 | // Shadow the labels for all escapes from the try block, including |
| 2028 | // returns. During shadowing, the original label is hidden as the |
| 2029 | // LabelShadow and operations on the original actually affect the |
| 2030 | // shadowing label. |
| 2031 | // |
| 2032 | // We should probably try to unify the escaping labels and the return |
| 2033 | // label. |
| 2034 | int nof_escapes = node->escaping_targets()->length(); |
| 2035 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2036 | |
| 2037 | // Add the shadow target for the function return. |
| 2038 | static const int kReturnShadowIndex = 0; |
| 2039 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2040 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2041 | function_return_is_shadowed_ = true; |
| 2042 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2043 | |
| 2044 | // Add the remaining shadow targets. |
| 2045 | for (int i = 0; i < nof_escapes; i++) { |
| 2046 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
| 2047 | } |
| 2048 | |
| 2049 | // Generate code for the statements in the try block. |
| 2050 | VisitStatementsAndSpill(node->try_block()->statements()); |
| 2051 | |
| 2052 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2053 | // After shadowing stops, the original labels are unshadowed and the |
| 2054 | // LabelShadows represent the formerly shadowing labels. |
| 2055 | bool has_unlinks = false; |
| 2056 | for (int i = 0; i < shadows.length(); i++) { |
| 2057 | shadows[i]->StopShadowing(); |
| 2058 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2059 | } |
| 2060 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2061 | |
| 2062 | // Get an external reference to the handler address. |
| 2063 | ExternalReference handler_address(Top::k_handler_address); |
| 2064 | |
| 2065 | // If we can fall off the end of the try block, unlink from try chain. |
| 2066 | if (has_valid_frame()) { |
| 2067 | // The next handler address is on top of the frame. Unlink from |
| 2068 | // the handler list and drop the rest of this handler from the |
| 2069 | // frame. |
| 2070 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2071 | frame_->EmitPop(r1); |
| 2072 | __ mov(r3, Operand(handler_address)); |
| 2073 | __ str(r1, MemOperand(r3)); |
| 2074 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
| 2075 | if (has_unlinks) { |
| 2076 | exit.Jump(); |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | // Generate unlink code for the (formerly) shadowing labels that have been |
| 2081 | // jumped to. Deallocate each shadow target. |
| 2082 | for (int i = 0; i < shadows.length(); i++) { |
| 2083 | if (shadows[i]->is_linked()) { |
| 2084 | // Unlink from try chain; |
| 2085 | shadows[i]->Bind(); |
| 2086 | // Because we can be jumping here (to spilled code) from unspilled |
| 2087 | // code, we need to reestablish a spilled frame at this block. |
| 2088 | frame_->SpillAll(); |
| 2089 | |
| 2090 | // Reload sp from the top handler, because some statements that we |
| 2091 | // break from (eg, for...in) may have left stuff on the stack. |
| 2092 | __ mov(r3, Operand(handler_address)); |
| 2093 | __ ldr(sp, MemOperand(r3)); |
| 2094 | frame_->Forget(frame_->height() - handler_height); |
| 2095 | |
| 2096 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2097 | frame_->EmitPop(r1); |
| 2098 | __ str(r1, MemOperand(r3)); |
| 2099 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
| 2100 | |
| 2101 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2102 | frame_->PrepareForReturn(); |
| 2103 | } |
| 2104 | shadows[i]->other_target()->Jump(); |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | exit.Bind(); |
| 2109 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2110 | } |
| 2111 | |
| 2112 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2113 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2114 | #ifdef DEBUG |
| 2115 | int original_height = frame_->height(); |
| 2116 | #endif |
| 2117 | VirtualFrame::SpilledScope spilled_scope; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2118 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2119 | CodeForStatementPosition(node); |
| 2120 | |
| 2121 | // State: Used to keep track of reason for entering the finally |
| 2122 | // block. Should probably be extended to hold information for |
| 2123 | // break/continue from within the try block. |
| 2124 | enum { FALLING, THROWING, JUMPING }; |
| 2125 | |
| 2126 | JumpTarget try_block; |
| 2127 | JumpTarget finally_block; |
| 2128 | |
| 2129 | try_block.Call(); |
| 2130 | |
| 2131 | frame_->EmitPush(r0); // save exception object on the stack |
| 2132 | // In case of thrown exceptions, this is where we continue. |
| 2133 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
| 2134 | finally_block.Jump(); |
| 2135 | |
| 2136 | // --- Try block --- |
| 2137 | try_block.Bind(); |
| 2138 | |
| 2139 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2140 | int handler_height = frame_->height(); |
| 2141 | |
| 2142 | // Shadow the labels for all escapes from the try block, including |
| 2143 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2144 | // operations on the original actually affect the shadowing label. |
| 2145 | // |
| 2146 | // We should probably try to unify the escaping labels and the return |
| 2147 | // label. |
| 2148 | int nof_escapes = node->escaping_targets()->length(); |
| 2149 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2150 | |
| 2151 | // Add the shadow target for the function return. |
| 2152 | static const int kReturnShadowIndex = 0; |
| 2153 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2154 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2155 | function_return_is_shadowed_ = true; |
| 2156 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2157 | |
| 2158 | // Add the remaining shadow targets. |
| 2159 | for (int i = 0; i < nof_escapes; i++) { |
| 2160 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
| 2161 | } |
| 2162 | |
| 2163 | // Generate code for the statements in the try block. |
| 2164 | VisitStatementsAndSpill(node->try_block()->statements()); |
| 2165 | |
| 2166 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2167 | // After shadowing stops, the original labels are unshadowed and the |
| 2168 | // LabelShadows represent the formerly shadowing labels. |
| 2169 | int nof_unlinks = 0; |
| 2170 | for (int i = 0; i < shadows.length(); i++) { |
| 2171 | shadows[i]->StopShadowing(); |
| 2172 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2173 | } |
| 2174 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2175 | |
| 2176 | // Get an external reference to the handler address. |
| 2177 | ExternalReference handler_address(Top::k_handler_address); |
| 2178 | |
| 2179 | // If we can fall off the end of the try block, unlink from the try |
| 2180 | // chain and set the state on the frame to FALLING. |
| 2181 | if (has_valid_frame()) { |
| 2182 | // The next handler address is on top of the frame. |
| 2183 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2184 | frame_->EmitPop(r1); |
| 2185 | __ mov(r3, Operand(handler_address)); |
| 2186 | __ str(r1, MemOperand(r3)); |
| 2187 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
| 2188 | |
| 2189 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2190 | // state in r2, then jump around the unlink blocks if any. |
| 2191 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
| 2192 | frame_->EmitPush(r0); |
| 2193 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2194 | if (nof_unlinks > 0) { |
| 2195 | finally_block.Jump(); |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | // Generate code to unlink and set the state for the (formerly) |
| 2200 | // shadowing targets that have been jumped to. |
| 2201 | for (int i = 0; i < shadows.length(); i++) { |
| 2202 | if (shadows[i]->is_linked()) { |
| 2203 | // If we have come from the shadowed return, the return value is |
| 2204 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2205 | // until it is pushed. |
| 2206 | // |
| 2207 | // Because we can be jumping here (to spilled code) from |
| 2208 | // unspilled code, we need to reestablish a spilled frame at |
| 2209 | // this block. |
| 2210 | shadows[i]->Bind(); |
| 2211 | frame_->SpillAll(); |
| 2212 | |
| 2213 | // Reload sp from the top handler, because some statements that |
| 2214 | // we break from (eg, for...in) may have left stuff on the |
| 2215 | // stack. |
| 2216 | __ mov(r3, Operand(handler_address)); |
| 2217 | __ ldr(sp, MemOperand(r3)); |
| 2218 | frame_->Forget(frame_->height() - handler_height); |
| 2219 | |
| 2220 | // Unlink this handler and drop it from the frame. The next |
| 2221 | // handler address is currently on top of the frame. |
| 2222 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2223 | frame_->EmitPop(r1); |
| 2224 | __ str(r1, MemOperand(r3)); |
| 2225 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
| 2226 | |
| 2227 | if (i == kReturnShadowIndex) { |
| 2228 | // If this label shadowed the function return, materialize the |
| 2229 | // return value on the stack. |
| 2230 | frame_->EmitPush(r0); |
| 2231 | } else { |
| 2232 | // Fake TOS for targets that shadowed breaks and continues. |
| 2233 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
| 2234 | frame_->EmitPush(r0); |
| 2235 | } |
| 2236 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2237 | if (--nof_unlinks > 0) { |
| 2238 | // If this is not the last unlink block, jump around the next. |
| 2239 | finally_block.Jump(); |
| 2240 | } |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | // --- Finally block --- |
| 2245 | finally_block.Bind(); |
| 2246 | |
| 2247 | // Push the state on the stack. |
| 2248 | frame_->EmitPush(r2); |
| 2249 | |
| 2250 | // We keep two elements on the stack - the (possibly faked) result |
| 2251 | // and the state - while evaluating the finally block. |
| 2252 | // |
| 2253 | // Generate code for the statements in the finally block. |
| 2254 | VisitStatementsAndSpill(node->finally_block()->statements()); |
| 2255 | |
| 2256 | if (has_valid_frame()) { |
| 2257 | // Restore state and return value or faked TOS. |
| 2258 | frame_->EmitPop(r2); |
| 2259 | frame_->EmitPop(r0); |
| 2260 | } |
| 2261 | |
| 2262 | // Generate code to jump to the right destination for all used |
| 2263 | // formerly shadowing targets. Deallocate each shadow target. |
| 2264 | for (int i = 0; i < shadows.length(); i++) { |
| 2265 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2266 | JumpTarget* original = shadows[i]->other_target(); |
| 2267 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2268 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2269 | JumpTarget skip; |
| 2270 | skip.Branch(ne); |
| 2271 | frame_->PrepareForReturn(); |
| 2272 | original->Jump(); |
| 2273 | skip.Bind(); |
| 2274 | } else { |
| 2275 | original->Branch(eq); |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | if (has_valid_frame()) { |
| 2281 | // Check if we need to rethrow the exception. |
| 2282 | JumpTarget exit; |
| 2283 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2284 | exit.Branch(ne); |
| 2285 | |
| 2286 | // Rethrow exception. |
| 2287 | frame_->EmitPush(r0); |
| 2288 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2289 | |
| 2290 | // Done. |
| 2291 | exit.Bind(); |
| 2292 | } |
| 2293 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2294 | } |
| 2295 | |
| 2296 | |
| 2297 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
| 2298 | #ifdef DEBUG |
| 2299 | int original_height = frame_->height(); |
| 2300 | #endif |
| 2301 | VirtualFrame::SpilledScope spilled_scope; |
| 2302 | Comment cmnt(masm_, "[ DebuggerStatament"); |
| 2303 | CodeForStatementPosition(node); |
| 2304 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 2305 | DebuggerStatementStub ces; |
| 2306 | frame_->CallStub(&ces, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2307 | #endif |
| 2308 | // Ignore the return value. |
| 2309 | ASSERT(frame_->height() == original_height); |
| 2310 | } |
| 2311 | |
| 2312 | |
| 2313 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
| 2314 | VirtualFrame::SpilledScope spilled_scope; |
| 2315 | ASSERT(boilerplate->IsBoilerplate()); |
| 2316 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2317 | __ mov(r0, Operand(boilerplate)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2318 | // Use the fast case closure allocation code that allocates in new |
| 2319 | // space for nested functions that don't need literals cloning. |
| 2320 | if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) { |
| 2321 | FastNewClosureStub stub; |
| 2322 | frame_->EmitPush(r0); |
| 2323 | frame_->CallStub(&stub, 1); |
| 2324 | frame_->EmitPush(r0); |
| 2325 | } else { |
| 2326 | // Create a new closure. |
| 2327 | frame_->EmitPush(cp); |
| 2328 | frame_->EmitPush(r0); |
| 2329 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2330 | frame_->EmitPush(r0); |
| 2331 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | |
| 2335 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
| 2336 | #ifdef DEBUG |
| 2337 | int original_height = frame_->height(); |
| 2338 | #endif |
| 2339 | VirtualFrame::SpilledScope spilled_scope; |
| 2340 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2341 | |
| 2342 | // Build the function boilerplate and instantiate it. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2343 | Handle<JSFunction> boilerplate = |
| 2344 | Compiler::BuildBoilerplate(node, script_, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2345 | // Check for stack-overflow exception. |
| 2346 | if (HasStackOverflow()) { |
| 2347 | ASSERT(frame_->height() == original_height); |
| 2348 | return; |
| 2349 | } |
| 2350 | InstantiateBoilerplate(boilerplate); |
| 2351 | ASSERT(frame_->height() == original_height + 1); |
| 2352 | } |
| 2353 | |
| 2354 | |
| 2355 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
| 2356 | FunctionBoilerplateLiteral* node) { |
| 2357 | #ifdef DEBUG |
| 2358 | int original_height = frame_->height(); |
| 2359 | #endif |
| 2360 | VirtualFrame::SpilledScope spilled_scope; |
| 2361 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2362 | InstantiateBoilerplate(node->boilerplate()); |
| 2363 | ASSERT(frame_->height() == original_height + 1); |
| 2364 | } |
| 2365 | |
| 2366 | |
| 2367 | void CodeGenerator::VisitConditional(Conditional* node) { |
| 2368 | #ifdef DEBUG |
| 2369 | int original_height = frame_->height(); |
| 2370 | #endif |
| 2371 | VirtualFrame::SpilledScope spilled_scope; |
| 2372 | Comment cmnt(masm_, "[ Conditional"); |
| 2373 | JumpTarget then; |
| 2374 | JumpTarget else_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2375 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2376 | if (has_valid_frame()) { |
| 2377 | Branch(false, &else_); |
| 2378 | } |
| 2379 | if (has_valid_frame() || then.is_linked()) { |
| 2380 | then.Bind(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2381 | LoadAndSpill(node->then_expression()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2382 | } |
| 2383 | if (else_.is_linked()) { |
| 2384 | JumpTarget exit; |
| 2385 | if (has_valid_frame()) exit.Jump(); |
| 2386 | else_.Bind(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2387 | LoadAndSpill(node->else_expression()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2388 | if (exit.is_linked()) exit.Bind(); |
| 2389 | } |
| 2390 | ASSERT(frame_->height() == original_height + 1); |
| 2391 | } |
| 2392 | |
| 2393 | |
| 2394 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
| 2395 | VirtualFrame::SpilledScope spilled_scope; |
| 2396 | if (slot->type() == Slot::LOOKUP) { |
| 2397 | ASSERT(slot->var()->is_dynamic()); |
| 2398 | |
| 2399 | JumpTarget slow; |
| 2400 | JumpTarget done; |
| 2401 | |
| 2402 | // Generate fast-case code for variables that might be shadowed by |
| 2403 | // eval-introduced variables. Eval is used a lot without |
| 2404 | // introducing variables. In those cases, we do not want to |
| 2405 | // perform a runtime call for all variables in the scope |
| 2406 | // containing the eval. |
| 2407 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2408 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
| 2409 | // If there was no control flow to slow, we can exit early. |
| 2410 | if (!slow.is_linked()) { |
| 2411 | frame_->EmitPush(r0); |
| 2412 | return; |
| 2413 | } |
| 2414 | |
| 2415 | done.Jump(); |
| 2416 | |
| 2417 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2418 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2419 | // Only generate the fast case for locals that rewrite to slots. |
| 2420 | // This rules out argument loads. |
| 2421 | if (potential_slot != NULL) { |
| 2422 | __ ldr(r0, |
| 2423 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2424 | r1, |
| 2425 | r2, |
| 2426 | &slow)); |
| 2427 | if (potential_slot->var()->mode() == Variable::CONST) { |
| 2428 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2429 | __ cmp(r0, ip); |
| 2430 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
| 2431 | } |
| 2432 | // There is always control flow to slow from |
| 2433 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2434 | // it. |
| 2435 | done.Jump(); |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | slow.Bind(); |
| 2440 | frame_->EmitPush(cp); |
| 2441 | __ mov(r0, Operand(slot->var()->name())); |
| 2442 | frame_->EmitPush(r0); |
| 2443 | |
| 2444 | if (typeof_state == INSIDE_TYPEOF) { |
| 2445 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
| 2446 | } else { |
| 2447 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
| 2448 | } |
| 2449 | |
| 2450 | done.Bind(); |
| 2451 | frame_->EmitPush(r0); |
| 2452 | |
| 2453 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2454 | // Special handling for locals allocated in registers. |
| 2455 | __ ldr(r0, SlotOperand(slot, r2)); |
| 2456 | frame_->EmitPush(r0); |
| 2457 | if (slot->var()->mode() == Variable::CONST) { |
| 2458 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2459 | // initialized yet) which needs to be converted into the 'undefined' |
| 2460 | // value. |
| 2461 | Comment cmnt(masm_, "[ Unhole const"); |
| 2462 | frame_->EmitPop(r0); |
| 2463 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2464 | __ cmp(r0, ip); |
| 2465 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
| 2466 | frame_->EmitPush(r0); |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2472 | void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) { |
| 2473 | ASSERT(slot != NULL); |
| 2474 | if (slot->type() == Slot::LOOKUP) { |
| 2475 | ASSERT(slot->var()->is_dynamic()); |
| 2476 | |
| 2477 | // For now, just do a runtime call. |
| 2478 | frame_->EmitPush(cp); |
| 2479 | __ mov(r0, Operand(slot->var()->name())); |
| 2480 | frame_->EmitPush(r0); |
| 2481 | |
| 2482 | if (init_state == CONST_INIT) { |
| 2483 | // Same as the case for a normal store, but ignores attribute |
| 2484 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 2485 | // const properties (introduced via eval("const foo = (some |
| 2486 | // expr);")). Also, uses the current function context instead of |
| 2487 | // the top context. |
| 2488 | // |
| 2489 | // Note that we must declare the foo upon entry of eval(), via a |
| 2490 | // context slot declaration, but we cannot initialize it at the |
| 2491 | // same time, because the const declaration may be at the end of |
| 2492 | // the eval code (sigh...) and the const variable may have been |
| 2493 | // used before (where its value is 'undefined'). Thus, we can only |
| 2494 | // do the initialization when we actually encounter the expression |
| 2495 | // and when the expression operands are defined and valid, and |
| 2496 | // thus we need the split into 2 operations: declaration of the |
| 2497 | // context slot followed by initialization. |
| 2498 | frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 2499 | } else { |
| 2500 | frame_->CallRuntime(Runtime::kStoreContextSlot, 3); |
| 2501 | } |
| 2502 | // Storing a variable must keep the (new) value on the expression |
| 2503 | // stack. This is necessary for compiling assignment expressions. |
| 2504 | frame_->EmitPush(r0); |
| 2505 | |
| 2506 | } else { |
| 2507 | ASSERT(!slot->var()->is_dynamic()); |
| 2508 | |
| 2509 | JumpTarget exit; |
| 2510 | if (init_state == CONST_INIT) { |
| 2511 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 2512 | // Only the first const initialization must be executed (the slot |
| 2513 | // still contains 'the hole' value). When the assignment is |
| 2514 | // executed, the code is identical to a normal store (see below). |
| 2515 | Comment cmnt(masm_, "[ Init const"); |
| 2516 | __ ldr(r2, SlotOperand(slot, r2)); |
| 2517 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2518 | __ cmp(r2, ip); |
| 2519 | exit.Branch(ne); |
| 2520 | } |
| 2521 | |
| 2522 | // We must execute the store. Storing a variable must keep the |
| 2523 | // (new) value on the stack. This is necessary for compiling |
| 2524 | // assignment expressions. |
| 2525 | // |
| 2526 | // Note: We will reach here even with slot->var()->mode() == |
| 2527 | // Variable::CONST because of const declarations which will |
| 2528 | // initialize consts to 'the hole' value and by doing so, end up |
| 2529 | // calling this code. r2 may be loaded with context; used below in |
| 2530 | // RecordWrite. |
| 2531 | frame_->EmitPop(r0); |
| 2532 | __ str(r0, SlotOperand(slot, r2)); |
| 2533 | frame_->EmitPush(r0); |
| 2534 | if (slot->type() == Slot::CONTEXT) { |
| 2535 | // Skip write barrier if the written value is a smi. |
| 2536 | __ tst(r0, Operand(kSmiTagMask)); |
| 2537 | exit.Branch(eq); |
| 2538 | // r2 is loaded with context when calling SlotOperand above. |
| 2539 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 2540 | __ mov(r3, Operand(offset)); |
| 2541 | __ RecordWrite(r2, r3, r1); |
| 2542 | } |
| 2543 | // If we definitely did not jump over the assignment, we do not need |
| 2544 | // to bind the exit label. Doing so can defeat peephole |
| 2545 | // optimization. |
| 2546 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
| 2547 | exit.Bind(); |
| 2548 | } |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2553 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2554 | TypeofState typeof_state, |
| 2555 | Register tmp, |
| 2556 | Register tmp2, |
| 2557 | JumpTarget* slow) { |
| 2558 | // Check that no extension objects have been created by calls to |
| 2559 | // eval from the current scope to the global scope. |
| 2560 | Register context = cp; |
| 2561 | Scope* s = scope(); |
| 2562 | while (s != NULL) { |
| 2563 | if (s->num_heap_slots() > 0) { |
| 2564 | if (s->calls_eval()) { |
| 2565 | // Check that extension is NULL. |
| 2566 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2567 | __ tst(tmp2, tmp2); |
| 2568 | slow->Branch(ne); |
| 2569 | } |
| 2570 | // Load next context in chain. |
| 2571 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2572 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2573 | context = tmp; |
| 2574 | } |
| 2575 | // If no outer scope calls eval, we do not need to check more |
| 2576 | // context extensions. |
| 2577 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2578 | s = s->outer_scope(); |
| 2579 | } |
| 2580 | |
| 2581 | if (s->is_eval_scope()) { |
| 2582 | Label next, fast; |
| 2583 | if (!context.is(tmp)) { |
| 2584 | __ mov(tmp, Operand(context)); |
| 2585 | } |
| 2586 | __ bind(&next); |
| 2587 | // Terminate at global context. |
| 2588 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 2589 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2590 | __ cmp(tmp2, ip); |
| 2591 | __ b(eq, &fast); |
| 2592 | // Check that extension is NULL. |
| 2593 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2594 | __ tst(tmp2, tmp2); |
| 2595 | slow->Branch(ne); |
| 2596 | // Load next context in chain. |
| 2597 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2598 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2599 | __ b(&next); |
| 2600 | __ bind(&fast); |
| 2601 | } |
| 2602 | |
| 2603 | // All extension objects were empty and it is safe to use a global |
| 2604 | // load IC call. |
| 2605 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2606 | // Load the global object. |
| 2607 | LoadGlobal(); |
| 2608 | // Setup the name register. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2609 | __ mov(r2, Operand(slot->var()->name())); |
| 2610 | // Call IC stub. |
| 2611 | if (typeof_state == INSIDE_TYPEOF) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 2612 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2613 | } else { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 2614 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | // Drop the global object. The result is in r0. |
| 2618 | frame_->Drop(); |
| 2619 | } |
| 2620 | |
| 2621 | |
| 2622 | void CodeGenerator::VisitSlot(Slot* node) { |
| 2623 | #ifdef DEBUG |
| 2624 | int original_height = frame_->height(); |
| 2625 | #endif |
| 2626 | VirtualFrame::SpilledScope spilled_scope; |
| 2627 | Comment cmnt(masm_, "[ Slot"); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2628 | LoadFromSlot(node, NOT_INSIDE_TYPEOF); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2629 | ASSERT(frame_->height() == original_height + 1); |
| 2630 | } |
| 2631 | |
| 2632 | |
| 2633 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
| 2634 | #ifdef DEBUG |
| 2635 | int original_height = frame_->height(); |
| 2636 | #endif |
| 2637 | VirtualFrame::SpilledScope spilled_scope; |
| 2638 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2639 | |
| 2640 | Variable* var = node->var(); |
| 2641 | Expression* expr = var->rewrite(); |
| 2642 | if (expr != NULL) { |
| 2643 | Visit(expr); |
| 2644 | } else { |
| 2645 | ASSERT(var->is_global()); |
| 2646 | Reference ref(this, node); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2647 | ref.GetValueAndSpill(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2648 | } |
| 2649 | ASSERT(frame_->height() == original_height + 1); |
| 2650 | } |
| 2651 | |
| 2652 | |
| 2653 | void CodeGenerator::VisitLiteral(Literal* node) { |
| 2654 | #ifdef DEBUG |
| 2655 | int original_height = frame_->height(); |
| 2656 | #endif |
| 2657 | VirtualFrame::SpilledScope spilled_scope; |
| 2658 | Comment cmnt(masm_, "[ Literal"); |
| 2659 | __ mov(r0, Operand(node->handle())); |
| 2660 | frame_->EmitPush(r0); |
| 2661 | ASSERT(frame_->height() == original_height + 1); |
| 2662 | } |
| 2663 | |
| 2664 | |
| 2665 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
| 2666 | #ifdef DEBUG |
| 2667 | int original_height = frame_->height(); |
| 2668 | #endif |
| 2669 | VirtualFrame::SpilledScope spilled_scope; |
| 2670 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2671 | |
| 2672 | // Retrieve the literal array and check the allocated entry. |
| 2673 | |
| 2674 | // Load the function of this activation. |
| 2675 | __ ldr(r1, frame_->Function()); |
| 2676 | |
| 2677 | // Load the literals array of the function. |
| 2678 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2679 | |
| 2680 | // Load the literal at the ast saved index. |
| 2681 | int literal_offset = |
| 2682 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2683 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2684 | |
| 2685 | JumpTarget done; |
| 2686 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2687 | __ cmp(r2, ip); |
| 2688 | done.Branch(ne); |
| 2689 | |
| 2690 | // If the entry is undefined we call the runtime system to computed |
| 2691 | // the literal. |
| 2692 | frame_->EmitPush(r1); // literal array (0) |
| 2693 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
| 2694 | frame_->EmitPush(r0); // literal index (1) |
| 2695 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
| 2696 | frame_->EmitPush(r0); |
| 2697 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
| 2698 | frame_->EmitPush(r0); |
| 2699 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
| 2700 | __ mov(r2, Operand(r0)); |
| 2701 | |
| 2702 | done.Bind(); |
| 2703 | // Push the literal. |
| 2704 | frame_->EmitPush(r2); |
| 2705 | ASSERT(frame_->height() == original_height + 1); |
| 2706 | } |
| 2707 | |
| 2708 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2709 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
| 2710 | #ifdef DEBUG |
| 2711 | int original_height = frame_->height(); |
| 2712 | #endif |
| 2713 | VirtualFrame::SpilledScope spilled_scope; |
| 2714 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2715 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2716 | // Load the function of this activation. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2717 | __ ldr(r2, frame_->Function()); |
| 2718 | // Literal array. |
| 2719 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
| 2720 | // Literal index. |
| 2721 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
| 2722 | // Constant properties. |
| 2723 | __ mov(r0, Operand(node->constant_properties())); |
| 2724 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
| 2725 | if (node->depth() > 1) { |
| 2726 | frame_->CallRuntime(Runtime::kCreateObjectLiteral, 3); |
| 2727 | } else { |
| 2728 | frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2729 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2730 | frame_->EmitPush(r0); // save the result |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2731 | // r0: created object literal |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2732 | |
| 2733 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2734 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2735 | Literal* key = property->key(); |
| 2736 | Expression* value = property->value(); |
| 2737 | switch (property->kind()) { |
| 2738 | case ObjectLiteral::Property::CONSTANT: |
| 2739 | break; |
| 2740 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2741 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2742 | // else fall through |
| 2743 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2744 | case ObjectLiteral::Property::PROTOTYPE: { |
| 2745 | frame_->EmitPush(r0); // dup the result |
| 2746 | LoadAndSpill(key); |
| 2747 | LoadAndSpill(value); |
| 2748 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
| 2749 | // restore r0 |
| 2750 | __ ldr(r0, frame_->Top()); |
| 2751 | break; |
| 2752 | } |
| 2753 | case ObjectLiteral::Property::SETTER: { |
| 2754 | frame_->EmitPush(r0); |
| 2755 | LoadAndSpill(key); |
| 2756 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 2757 | frame_->EmitPush(r0); |
| 2758 | LoadAndSpill(value); |
| 2759 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
| 2760 | __ ldr(r0, frame_->Top()); |
| 2761 | break; |
| 2762 | } |
| 2763 | case ObjectLiteral::Property::GETTER: { |
| 2764 | frame_->EmitPush(r0); |
| 2765 | LoadAndSpill(key); |
| 2766 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2767 | frame_->EmitPush(r0); |
| 2768 | LoadAndSpill(value); |
| 2769 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
| 2770 | __ ldr(r0, frame_->Top()); |
| 2771 | break; |
| 2772 | } |
| 2773 | } |
| 2774 | } |
| 2775 | ASSERT(frame_->height() == original_height + 1); |
| 2776 | } |
| 2777 | |
| 2778 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2779 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
| 2780 | #ifdef DEBUG |
| 2781 | int original_height = frame_->height(); |
| 2782 | #endif |
| 2783 | VirtualFrame::SpilledScope spilled_scope; |
| 2784 | Comment cmnt(masm_, "[ ArrayLiteral"); |
| 2785 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2786 | // Load the function of this activation. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2787 | __ ldr(r2, frame_->Function()); |
| 2788 | // Literals array. |
| 2789 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
| 2790 | // Literal index. |
| 2791 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
| 2792 | // Constant elements. |
| 2793 | __ mov(r0, Operand(node->constant_elements())); |
| 2794 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
| 2795 | if (node->depth() > 1) { |
| 2796 | frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3); |
| 2797 | } else { |
| 2798 | frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2799 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2800 | frame_->EmitPush(r0); // save the result |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2801 | // r0: created object literal |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2802 | |
| 2803 | // Generate code to set the elements in the array that are not |
| 2804 | // literals. |
| 2805 | for (int i = 0; i < node->values()->length(); i++) { |
| 2806 | Expression* value = node->values()->at(i); |
| 2807 | |
| 2808 | // If value is a literal the property value is already set in the |
| 2809 | // boilerplate object. |
| 2810 | if (value->AsLiteral() != NULL) continue; |
| 2811 | // If value is a materialized literal the property value is already set |
| 2812 | // in the boilerplate object if it is simple. |
| 2813 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
| 2814 | |
| 2815 | // The property must be set by generated code. |
| 2816 | LoadAndSpill(value); |
| 2817 | frame_->EmitPop(r0); |
| 2818 | |
| 2819 | // Fetch the object literal. |
| 2820 | __ ldr(r1, frame_->Top()); |
| 2821 | // Get the elements array. |
| 2822 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 2823 | |
| 2824 | // Write to the indexed properties array. |
| 2825 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
| 2826 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2827 | |
| 2828 | // Update the write barrier for the array address. |
| 2829 | __ mov(r3, Operand(offset)); |
| 2830 | __ RecordWrite(r1, r3, r2); |
| 2831 | } |
| 2832 | ASSERT(frame_->height() == original_height + 1); |
| 2833 | } |
| 2834 | |
| 2835 | |
| 2836 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
| 2837 | #ifdef DEBUG |
| 2838 | int original_height = frame_->height(); |
| 2839 | #endif |
| 2840 | VirtualFrame::SpilledScope spilled_scope; |
| 2841 | // Call runtime routine to allocate the catch extension object and |
| 2842 | // assign the exception value to the catch variable. |
| 2843 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2844 | LoadAndSpill(node->key()); |
| 2845 | LoadAndSpill(node->value()); |
| 2846 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2847 | frame_->EmitPush(r0); |
| 2848 | ASSERT(frame_->height() == original_height + 1); |
| 2849 | } |
| 2850 | |
| 2851 | |
| 2852 | void CodeGenerator::VisitAssignment(Assignment* node) { |
| 2853 | #ifdef DEBUG |
| 2854 | int original_height = frame_->height(); |
| 2855 | #endif |
| 2856 | VirtualFrame::SpilledScope spilled_scope; |
| 2857 | Comment cmnt(masm_, "[ Assignment"); |
| 2858 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2859 | { Reference target(this, node->target(), node->is_compound()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2860 | if (target.is_illegal()) { |
| 2861 | // Fool the virtual frame into thinking that we left the assignment's |
| 2862 | // value on the frame. |
| 2863 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2864 | frame_->EmitPush(r0); |
| 2865 | ASSERT(frame_->height() == original_height + 1); |
| 2866 | return; |
| 2867 | } |
| 2868 | |
| 2869 | if (node->op() == Token::ASSIGN || |
| 2870 | node->op() == Token::INIT_VAR || |
| 2871 | node->op() == Token::INIT_CONST) { |
| 2872 | LoadAndSpill(node->value()); |
| 2873 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2874 | } else { // Assignment is a compound assignment. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2875 | // Get the old value of the lhs. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2876 | target.GetValueAndSpill(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2877 | Literal* literal = node->value()->AsLiteral(); |
| 2878 | bool overwrite = |
| 2879 | (node->value()->AsBinaryOperation() != NULL && |
| 2880 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 2881 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 2882 | SmiOperation(node->binary_op(), |
| 2883 | literal->handle(), |
| 2884 | false, |
| 2885 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 2886 | frame_->EmitPush(r0); |
| 2887 | |
| 2888 | } else { |
| 2889 | LoadAndSpill(node->value()); |
| 2890 | GenericBinaryOperation(node->binary_op(), |
| 2891 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 2892 | frame_->EmitPush(r0); |
| 2893 | } |
| 2894 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2895 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2896 | if (var != NULL && |
| 2897 | (var->mode() == Variable::CONST) && |
| 2898 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2899 | // Assignment ignored - leave the value on the stack. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2900 | UnloadReference(&target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2901 | } else { |
| 2902 | CodeForSourcePosition(node->position()); |
| 2903 | if (node->op() == Token::INIT_CONST) { |
| 2904 | // Dynamic constant initializations must use the function context |
| 2905 | // and initialize the actual constant declared. Dynamic variable |
| 2906 | // initializations are simply assignments and use SetValue. |
| 2907 | target.SetValue(CONST_INIT); |
| 2908 | } else { |
| 2909 | target.SetValue(NOT_CONST_INIT); |
| 2910 | } |
| 2911 | } |
| 2912 | } |
| 2913 | ASSERT(frame_->height() == original_height + 1); |
| 2914 | } |
| 2915 | |
| 2916 | |
| 2917 | void CodeGenerator::VisitThrow(Throw* node) { |
| 2918 | #ifdef DEBUG |
| 2919 | int original_height = frame_->height(); |
| 2920 | #endif |
| 2921 | VirtualFrame::SpilledScope spilled_scope; |
| 2922 | Comment cmnt(masm_, "[ Throw"); |
| 2923 | |
| 2924 | LoadAndSpill(node->exception()); |
| 2925 | CodeForSourcePosition(node->position()); |
| 2926 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2927 | frame_->EmitPush(r0); |
| 2928 | ASSERT(frame_->height() == original_height + 1); |
| 2929 | } |
| 2930 | |
| 2931 | |
| 2932 | void CodeGenerator::VisitProperty(Property* node) { |
| 2933 | #ifdef DEBUG |
| 2934 | int original_height = frame_->height(); |
| 2935 | #endif |
| 2936 | VirtualFrame::SpilledScope spilled_scope; |
| 2937 | Comment cmnt(masm_, "[ Property"); |
| 2938 | |
| 2939 | { Reference property(this, node); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2940 | property.GetValueAndSpill(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2941 | } |
| 2942 | ASSERT(frame_->height() == original_height + 1); |
| 2943 | } |
| 2944 | |
| 2945 | |
| 2946 | void CodeGenerator::VisitCall(Call* node) { |
| 2947 | #ifdef DEBUG |
| 2948 | int original_height = frame_->height(); |
| 2949 | #endif |
| 2950 | VirtualFrame::SpilledScope spilled_scope; |
| 2951 | Comment cmnt(masm_, "[ Call"); |
| 2952 | |
| 2953 | Expression* function = node->expression(); |
| 2954 | ZoneList<Expression*>* args = node->arguments(); |
| 2955 | |
| 2956 | // Standard function call. |
| 2957 | // Check if the function is a variable or a property. |
| 2958 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2959 | Property* property = function->AsProperty(); |
| 2960 | |
| 2961 | // ------------------------------------------------------------------------ |
| 2962 | // Fast-case: Use inline caching. |
| 2963 | // --- |
| 2964 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2965 | // must be resolved after the arguments have been evaluated. The IC code |
| 2966 | // automatically handles this by loading the arguments before the function |
| 2967 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2968 | // ------------------------------------------------------------------------ |
| 2969 | |
| 2970 | if (var != NULL && var->is_possibly_eval()) { |
| 2971 | // ---------------------------------- |
| 2972 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 2973 | // ---------------------------------- |
| 2974 | |
| 2975 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 2976 | // resolve the function we need to call and the receiver of the |
| 2977 | // call. Then we call the resolved function using the given |
| 2978 | // arguments. |
| 2979 | // Prepare stack for call to resolved function. |
| 2980 | LoadAndSpill(function); |
| 2981 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 2982 | frame_->EmitPush(r2); // Slot for receiver |
| 2983 | int arg_count = args->length(); |
| 2984 | for (int i = 0; i < arg_count; i++) { |
| 2985 | LoadAndSpill(args->at(i)); |
| 2986 | } |
| 2987 | |
| 2988 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 2989 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 2990 | frame_->EmitPush(r1); |
| 2991 | if (arg_count > 0) { |
| 2992 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 2993 | frame_->EmitPush(r1); |
| 2994 | } else { |
| 2995 | frame_->EmitPush(r2); |
| 2996 | } |
| 2997 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2998 | // Push the receiver. |
| 2999 | __ ldr(r1, frame_->Receiver()); |
| 3000 | frame_->EmitPush(r1); |
| 3001 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3002 | // Resolve the call. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3003 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3004 | |
| 3005 | // 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] | 3006 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3007 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3008 | |
| 3009 | // Call the function. |
| 3010 | CodeForSourcePosition(node->position()); |
| 3011 | |
| 3012 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3013 | CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3014 | frame_->CallStub(&call_function, arg_count + 1); |
| 3015 | |
| 3016 | __ ldr(cp, frame_->Context()); |
| 3017 | // Remove the function from the stack. |
| 3018 | frame_->Drop(); |
| 3019 | frame_->EmitPush(r0); |
| 3020 | |
| 3021 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
| 3022 | // ---------------------------------- |
| 3023 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3024 | // ---------------------------------- |
| 3025 | |
| 3026 | // Push the name of the function and the receiver onto the stack. |
| 3027 | __ mov(r0, Operand(var->name())); |
| 3028 | frame_->EmitPush(r0); |
| 3029 | |
| 3030 | // Pass the global object as the receiver and let the IC stub |
| 3031 | // patch the stack to use the global proxy as 'this' in the |
| 3032 | // invoked function. |
| 3033 | LoadGlobal(); |
| 3034 | |
| 3035 | // Load the arguments. |
| 3036 | int arg_count = args->length(); |
| 3037 | for (int i = 0; i < arg_count; i++) { |
| 3038 | LoadAndSpill(args->at(i)); |
| 3039 | } |
| 3040 | |
| 3041 | // Setup the receiver register and call the IC initialization code. |
| 3042 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3043 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
| 3044 | CodeForSourcePosition(node->position()); |
| 3045 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3046 | arg_count + 1); |
| 3047 | __ ldr(cp, frame_->Context()); |
| 3048 | // Remove the function from the stack. |
| 3049 | frame_->Drop(); |
| 3050 | frame_->EmitPush(r0); |
| 3051 | |
| 3052 | } else if (var != NULL && var->slot() != NULL && |
| 3053 | var->slot()->type() == Slot::LOOKUP) { |
| 3054 | // ---------------------------------- |
| 3055 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3056 | // ---------------------------------- |
| 3057 | |
| 3058 | // Load the function |
| 3059 | frame_->EmitPush(cp); |
| 3060 | __ mov(r0, Operand(var->name())); |
| 3061 | frame_->EmitPush(r0); |
| 3062 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
| 3063 | // r0: slot value; r1: receiver |
| 3064 | |
| 3065 | // Load the receiver. |
| 3066 | frame_->EmitPush(r0); // function |
| 3067 | frame_->EmitPush(r1); // receiver |
| 3068 | |
| 3069 | // Call the function. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3070 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3071 | frame_->EmitPush(r0); |
| 3072 | |
| 3073 | } else if (property != NULL) { |
| 3074 | // Check if the key is a literal string. |
| 3075 | Literal* literal = property->key()->AsLiteral(); |
| 3076 | |
| 3077 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3078 | // ------------------------------------------------------------------ |
| 3079 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3080 | // ------------------------------------------------------------------ |
| 3081 | |
| 3082 | // Push the name of the function and the receiver onto the stack. |
| 3083 | __ mov(r0, Operand(literal->handle())); |
| 3084 | frame_->EmitPush(r0); |
| 3085 | LoadAndSpill(property->obj()); |
| 3086 | |
| 3087 | // Load the arguments. |
| 3088 | int arg_count = args->length(); |
| 3089 | for (int i = 0; i < arg_count; i++) { |
| 3090 | LoadAndSpill(args->at(i)); |
| 3091 | } |
| 3092 | |
| 3093 | // Set the receiver register and call the IC initialization code. |
| 3094 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3095 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
| 3096 | CodeForSourcePosition(node->position()); |
| 3097 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
| 3098 | __ ldr(cp, frame_->Context()); |
| 3099 | |
| 3100 | // Remove the function from the stack. |
| 3101 | frame_->Drop(); |
| 3102 | |
| 3103 | frame_->EmitPush(r0); // push after get rid of function from the stack |
| 3104 | |
| 3105 | } else { |
| 3106 | // ------------------------------------------- |
| 3107 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3108 | // ------------------------------------------- |
| 3109 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3110 | LoadAndSpill(property->obj()); |
| 3111 | LoadAndSpill(property->key()); |
| 3112 | EmitKeyedLoad(false); |
| 3113 | frame_->Drop(); // key |
| 3114 | // Put the function below the receiver. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3115 | if (property->is_synthetic()) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3116 | // Use the global receiver. |
| 3117 | frame_->Drop(); |
| 3118 | frame_->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3119 | LoadGlobalReceiver(r0); |
| 3120 | } else { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3121 | frame_->EmitPop(r1); // receiver |
| 3122 | frame_->EmitPush(r0); // function |
| 3123 | frame_->EmitPush(r1); // receiver |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | // Call the function. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3127 | CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3128 | frame_->EmitPush(r0); |
| 3129 | } |
| 3130 | |
| 3131 | } else { |
| 3132 | // ---------------------------------- |
| 3133 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3134 | // ---------------------------------- |
| 3135 | |
| 3136 | // Load the function. |
| 3137 | LoadAndSpill(function); |
| 3138 | |
| 3139 | // Pass the global proxy as the receiver. |
| 3140 | LoadGlobalReceiver(r0); |
| 3141 | |
| 3142 | // Call the function. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3143 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3144 | frame_->EmitPush(r0); |
| 3145 | } |
| 3146 | ASSERT(frame_->height() == original_height + 1); |
| 3147 | } |
| 3148 | |
| 3149 | |
| 3150 | void CodeGenerator::VisitCallNew(CallNew* node) { |
| 3151 | #ifdef DEBUG |
| 3152 | int original_height = frame_->height(); |
| 3153 | #endif |
| 3154 | VirtualFrame::SpilledScope spilled_scope; |
| 3155 | Comment cmnt(masm_, "[ CallNew"); |
| 3156 | |
| 3157 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3158 | // expression in new calls must be evaluated before the |
| 3159 | // arguments. This is different from ordinary calls, where the |
| 3160 | // actual function to call is resolved after the arguments have been |
| 3161 | // evaluated. |
| 3162 | |
| 3163 | // Compute function to call and use the global object as the |
| 3164 | // receiver. There is no need to use the global proxy here because |
| 3165 | // it will always be replaced with a newly allocated object. |
| 3166 | LoadAndSpill(node->expression()); |
| 3167 | LoadGlobal(); |
| 3168 | |
| 3169 | // Push the arguments ("left-to-right") on the stack. |
| 3170 | ZoneList<Expression*>* args = node->arguments(); |
| 3171 | int arg_count = args->length(); |
| 3172 | for (int i = 0; i < arg_count; i++) { |
| 3173 | LoadAndSpill(args->at(i)); |
| 3174 | } |
| 3175 | |
| 3176 | // r0: the number of arguments. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3177 | __ mov(r0, Operand(arg_count)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3178 | // Load the function into r1 as per calling convention. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3179 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
| 3180 | |
| 3181 | // Call the construct call builtin that handles allocation and |
| 3182 | // constructor invocation. |
| 3183 | CodeForSourcePosition(node->position()); |
| 3184 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 3185 | frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3186 | |
| 3187 | // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)). |
| 3188 | __ str(r0, frame_->Top()); |
| 3189 | ASSERT(frame_->height() == original_height + 1); |
| 3190 | } |
| 3191 | |
| 3192 | |
| 3193 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3194 | VirtualFrame::SpilledScope spilled_scope; |
| 3195 | ASSERT(args->length() == 1); |
| 3196 | JumpTarget leave, null, function, non_function_constructor; |
| 3197 | |
| 3198 | // Load the object into r0. |
| 3199 | LoadAndSpill(args->at(0)); |
| 3200 | frame_->EmitPop(r0); |
| 3201 | |
| 3202 | // If the object is a smi, we return null. |
| 3203 | __ tst(r0, Operand(kSmiTagMask)); |
| 3204 | null.Branch(eq); |
| 3205 | |
| 3206 | // Check that the object is a JS object but take special care of JS |
| 3207 | // functions to make sure they have 'Function' as their class. |
| 3208 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3209 | null.Branch(lt); |
| 3210 | |
| 3211 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3212 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3213 | // LAST_JS_OBJECT_TYPE. |
| 3214 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3215 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3216 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3217 | function.Branch(eq); |
| 3218 | |
| 3219 | // Check if the constructor in the map is a function. |
| 3220 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3221 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3222 | non_function_constructor.Branch(ne); |
| 3223 | |
| 3224 | // The r0 register now contains the constructor function. Grab the |
| 3225 | // instance class name from there. |
| 3226 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3227 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
| 3228 | frame_->EmitPush(r0); |
| 3229 | leave.Jump(); |
| 3230 | |
| 3231 | // Functions have class 'Function'. |
| 3232 | function.Bind(); |
| 3233 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3234 | frame_->EmitPush(r0); |
| 3235 | leave.Jump(); |
| 3236 | |
| 3237 | // Objects with a non-function constructor have class 'Object'. |
| 3238 | non_function_constructor.Bind(); |
| 3239 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3240 | frame_->EmitPush(r0); |
| 3241 | leave.Jump(); |
| 3242 | |
| 3243 | // Non-JS objects have class null. |
| 3244 | null.Bind(); |
| 3245 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
| 3246 | frame_->EmitPush(r0); |
| 3247 | |
| 3248 | // All done. |
| 3249 | leave.Bind(); |
| 3250 | } |
| 3251 | |
| 3252 | |
| 3253 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
| 3254 | VirtualFrame::SpilledScope spilled_scope; |
| 3255 | ASSERT(args->length() == 1); |
| 3256 | JumpTarget leave; |
| 3257 | LoadAndSpill(args->at(0)); |
| 3258 | frame_->EmitPop(r0); // r0 contains object. |
| 3259 | // if (object->IsSmi()) return the object. |
| 3260 | __ tst(r0, Operand(kSmiTagMask)); |
| 3261 | leave.Branch(eq); |
| 3262 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3263 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
| 3264 | leave.Branch(ne); |
| 3265 | // Load the value. |
| 3266 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
| 3267 | leave.Bind(); |
| 3268 | frame_->EmitPush(r0); |
| 3269 | } |
| 3270 | |
| 3271 | |
| 3272 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
| 3273 | VirtualFrame::SpilledScope spilled_scope; |
| 3274 | ASSERT(args->length() == 2); |
| 3275 | JumpTarget leave; |
| 3276 | LoadAndSpill(args->at(0)); // Load the object. |
| 3277 | LoadAndSpill(args->at(1)); // Load the value. |
| 3278 | frame_->EmitPop(r0); // r0 contains value |
| 3279 | frame_->EmitPop(r1); // r1 contains object |
| 3280 | // if (object->IsSmi()) return object. |
| 3281 | __ tst(r1, Operand(kSmiTagMask)); |
| 3282 | leave.Branch(eq); |
| 3283 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3284 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
| 3285 | leave.Branch(ne); |
| 3286 | // Store the value. |
| 3287 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3288 | // Update the write barrier. |
| 3289 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3290 | __ RecordWrite(r1, r2, r3); |
| 3291 | // Leave. |
| 3292 | leave.Bind(); |
| 3293 | frame_->EmitPush(r0); |
| 3294 | } |
| 3295 | |
| 3296 | |
| 3297 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
| 3298 | VirtualFrame::SpilledScope spilled_scope; |
| 3299 | ASSERT(args->length() == 1); |
| 3300 | LoadAndSpill(args->at(0)); |
| 3301 | frame_->EmitPop(r0); |
| 3302 | __ tst(r0, Operand(kSmiTagMask)); |
| 3303 | cc_reg_ = eq; |
| 3304 | } |
| 3305 | |
| 3306 | |
| 3307 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
| 3308 | VirtualFrame::SpilledScope spilled_scope; |
| 3309 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3310 | ASSERT_EQ(args->length(), 3); |
| 3311 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 3312 | if (ShouldGenerateLog(args->at(0))) { |
| 3313 | LoadAndSpill(args->at(1)); |
| 3314 | LoadAndSpill(args->at(2)); |
| 3315 | __ CallRuntime(Runtime::kLog, 2); |
| 3316 | } |
| 3317 | #endif |
| 3318 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
| 3319 | frame_->EmitPush(r0); |
| 3320 | } |
| 3321 | |
| 3322 | |
| 3323 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
| 3324 | VirtualFrame::SpilledScope spilled_scope; |
| 3325 | ASSERT(args->length() == 1); |
| 3326 | LoadAndSpill(args->at(0)); |
| 3327 | frame_->EmitPop(r0); |
| 3328 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
| 3329 | cc_reg_ = eq; |
| 3330 | } |
| 3331 | |
| 3332 | |
| 3333 | // This should generate code that performs a charCodeAt() call or returns |
| 3334 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3335 | // It is not yet implemented on ARM, so it always goes to the slow case. |
| 3336 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
| 3337 | VirtualFrame::SpilledScope spilled_scope; |
| 3338 | ASSERT(args->length() == 2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3339 | Comment(masm_, "[ GenerateFastCharCodeAt"); |
| 3340 | |
| 3341 | LoadAndSpill(args->at(0)); |
| 3342 | LoadAndSpill(args->at(1)); |
| 3343 | frame_->EmitPop(r0); // Index. |
| 3344 | frame_->EmitPop(r1); // String. |
| 3345 | |
| 3346 | Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string; |
| 3347 | |
| 3348 | __ tst(r1, Operand(kSmiTagMask)); |
| 3349 | __ b(eq, &slow); // The 'string' was a Smi. |
| 3350 | |
| 3351 | ASSERT(kSmiTag == 0); |
| 3352 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
| 3353 | __ b(ne, &slow); // The index was negative or not a Smi. |
| 3354 | |
| 3355 | __ bind(&try_again_with_new_string); |
| 3356 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 3357 | __ b(ge, &slow); |
| 3358 | |
| 3359 | // Now r2 has the string type. |
| 3360 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
| 3361 | // Now r3 has the length of the string. Compare with the index. |
| 3362 | __ cmp(r3, Operand(r0, LSR, kSmiTagSize)); |
| 3363 | __ b(le, &slow); |
| 3364 | |
| 3365 | // Here we know the index is in range. Check that string is sequential. |
| 3366 | ASSERT_EQ(0, kSeqStringTag); |
| 3367 | __ tst(r2, Operand(kStringRepresentationMask)); |
| 3368 | __ b(ne, ¬_a_flat_string); |
| 3369 | |
| 3370 | // Check whether it is an ASCII string. |
| 3371 | ASSERT_EQ(0, kTwoByteStringTag); |
| 3372 | __ tst(r2, Operand(kStringEncodingMask)); |
| 3373 | __ b(ne, &ascii_string); |
| 3374 | |
| 3375 | // 2-byte string. We can add without shifting since the Smi tag size is the |
| 3376 | // log2 of the number of bytes in a two-byte character. |
| 3377 | ASSERT_EQ(1, kSmiTagSize); |
| 3378 | ASSERT_EQ(0, kSmiShiftSize); |
| 3379 | __ add(r1, r1, Operand(r0)); |
| 3380 | __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize)); |
| 3381 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3382 | __ jmp(&end); |
| 3383 | |
| 3384 | __ bind(&ascii_string); |
| 3385 | __ add(r1, r1, Operand(r0, LSR, kSmiTagSize)); |
| 3386 | __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 3387 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3388 | __ jmp(&end); |
| 3389 | |
| 3390 | __ bind(¬_a_flat_string); |
| 3391 | __ and_(r2, r2, Operand(kStringRepresentationMask)); |
| 3392 | __ cmp(r2, Operand(kConsStringTag)); |
| 3393 | __ b(ne, &slow); |
| 3394 | |
| 3395 | // ConsString. |
| 3396 | // Check that the right hand side is the empty string (ie if this is really a |
| 3397 | // flat string in a cons string). If that is not the case we would rather go |
| 3398 | // to the runtime system now, to flatten the string. |
| 3399 | __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset)); |
| 3400 | __ LoadRoot(r3, Heap::kEmptyStringRootIndex); |
| 3401 | __ cmp(r2, Operand(r3)); |
| 3402 | __ b(ne, &slow); |
| 3403 | |
| 3404 | // Get the first of the two strings. |
| 3405 | __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset)); |
| 3406 | __ jmp(&try_again_with_new_string); |
| 3407 | |
| 3408 | __ bind(&slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3409 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3410 | |
| 3411 | __ bind(&end); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3412 | frame_->EmitPush(r0); |
| 3413 | } |
| 3414 | |
| 3415 | |
| 3416 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
| 3417 | VirtualFrame::SpilledScope spilled_scope; |
| 3418 | ASSERT(args->length() == 1); |
| 3419 | LoadAndSpill(args->at(0)); |
| 3420 | JumpTarget answer; |
| 3421 | // We need the CC bits to come out as not_equal in the case where the |
| 3422 | // object is a smi. This can't be done with the usual test opcode so |
| 3423 | // we use XOR to get the right CC bits. |
| 3424 | frame_->EmitPop(r0); |
| 3425 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3426 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
| 3427 | answer.Branch(ne); |
| 3428 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3429 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
| 3430 | answer.Bind(); |
| 3431 | cc_reg_ = eq; |
| 3432 | } |
| 3433 | |
| 3434 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3435 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 3436 | // This generates a fast version of: |
| 3437 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
| 3438 | VirtualFrame::SpilledScope spilled_scope; |
| 3439 | ASSERT(args->length() == 1); |
| 3440 | LoadAndSpill(args->at(0)); |
| 3441 | frame_->EmitPop(r1); |
| 3442 | __ tst(r1, Operand(kSmiTagMask)); |
| 3443 | false_target()->Branch(eq); |
| 3444 | |
| 3445 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 3446 | __ cmp(r1, ip); |
| 3447 | true_target()->Branch(eq); |
| 3448 | |
| 3449 | Register map_reg = r2; |
| 3450 | __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3451 | // Undetectable objects behave like undefined when tested with typeof. |
| 3452 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 3453 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3454 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 3455 | false_target()->Branch(eq); |
| 3456 | |
| 3457 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 3458 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3459 | false_target()->Branch(lt); |
| 3460 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
| 3461 | cc_reg_ = le; |
| 3462 | } |
| 3463 | |
| 3464 | |
| 3465 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 3466 | // This generates a fast version of: |
| 3467 | // (%_ClassOf(arg) === 'Function') |
| 3468 | VirtualFrame::SpilledScope spilled_scope; |
| 3469 | ASSERT(args->length() == 1); |
| 3470 | LoadAndSpill(args->at(0)); |
| 3471 | frame_->EmitPop(r0); |
| 3472 | __ tst(r0, Operand(kSmiTagMask)); |
| 3473 | false_target()->Branch(eq); |
| 3474 | Register map_reg = r2; |
| 3475 | __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE); |
| 3476 | cc_reg_ = eq; |
| 3477 | } |
| 3478 | |
| 3479 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3480 | void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) { |
| 3481 | VirtualFrame::SpilledScope spilled_scope; |
| 3482 | ASSERT(args->length() == 1); |
| 3483 | LoadAndSpill(args->at(0)); |
| 3484 | frame_->EmitPop(r0); |
| 3485 | __ tst(r0, Operand(kSmiTagMask)); |
| 3486 | false_target()->Branch(eq); |
| 3487 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3488 | __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3489 | __ tst(r1, Operand(1 << Map::kIsUndetectable)); |
| 3490 | cc_reg_ = ne; |
| 3491 | } |
| 3492 | |
| 3493 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3494 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3495 | VirtualFrame::SpilledScope spilled_scope; |
| 3496 | ASSERT(args->length() == 0); |
| 3497 | |
| 3498 | // Get the frame pointer for the calling frame. |
| 3499 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3500 | |
| 3501 | // Skip the arguments adaptor frame if it exists. |
| 3502 | Label check_frame_marker; |
| 3503 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 3504 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 3505 | __ b(ne, &check_frame_marker); |
| 3506 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3507 | |
| 3508 | // Check the marker in the calling frame. |
| 3509 | __ bind(&check_frame_marker); |
| 3510 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3511 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3512 | cc_reg_ = eq; |
| 3513 | } |
| 3514 | |
| 3515 | |
| 3516 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
| 3517 | VirtualFrame::SpilledScope spilled_scope; |
| 3518 | ASSERT(args->length() == 0); |
| 3519 | |
| 3520 | // Seed the result with the formal parameters count, which will be used |
| 3521 | // in case no arguments adaptor frame is found below the current frame. |
| 3522 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3523 | |
| 3524 | // Call the shared stub to get to the arguments.length. |
| 3525 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
| 3526 | frame_->CallStub(&stub, 0); |
| 3527 | frame_->EmitPush(r0); |
| 3528 | } |
| 3529 | |
| 3530 | |
| 3531 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
| 3532 | VirtualFrame::SpilledScope spilled_scope; |
| 3533 | ASSERT(args->length() == 1); |
| 3534 | |
| 3535 | // Satisfy contract with ArgumentsAccessStub: |
| 3536 | // Load the key into r1 and the formal parameters count into r0. |
| 3537 | LoadAndSpill(args->at(0)); |
| 3538 | frame_->EmitPop(r1); |
| 3539 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3540 | |
| 3541 | // Call the shared stub to get to arguments[key]. |
| 3542 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
| 3543 | frame_->CallStub(&stub, 0); |
| 3544 | frame_->EmitPush(r0); |
| 3545 | } |
| 3546 | |
| 3547 | |
| 3548 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3549 | VirtualFrame::SpilledScope spilled_scope; |
| 3550 | ASSERT(args->length() == 0); |
| 3551 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3552 | RelocInfo::RUNTIME_ENTRY); |
| 3553 | frame_->EmitPush(r0); |
| 3554 | } |
| 3555 | |
| 3556 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3557 | void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) { |
| 3558 | ASSERT_EQ(2, args->length()); |
| 3559 | |
| 3560 | Load(args->at(0)); |
| 3561 | Load(args->at(1)); |
| 3562 | |
| 3563 | frame_->CallRuntime(Runtime::kStringAdd, 2); |
| 3564 | frame_->EmitPush(r0); |
| 3565 | } |
| 3566 | |
| 3567 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3568 | void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) { |
| 3569 | ASSERT_EQ(3, args->length()); |
| 3570 | |
| 3571 | Load(args->at(0)); |
| 3572 | Load(args->at(1)); |
| 3573 | Load(args->at(2)); |
| 3574 | |
| 3575 | frame_->CallRuntime(Runtime::kSubString, 3); |
| 3576 | frame_->EmitPush(r0); |
| 3577 | } |
| 3578 | |
| 3579 | |
| 3580 | void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) { |
| 3581 | ASSERT_EQ(2, args->length()); |
| 3582 | |
| 3583 | Load(args->at(0)); |
| 3584 | Load(args->at(1)); |
| 3585 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3586 | StringCompareStub stub; |
| 3587 | frame_->CallStub(&stub, 2); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3588 | frame_->EmitPush(r0); |
| 3589 | } |
| 3590 | |
| 3591 | |
| 3592 | void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) { |
| 3593 | ASSERT_EQ(4, args->length()); |
| 3594 | |
| 3595 | Load(args->at(0)); |
| 3596 | Load(args->at(1)); |
| 3597 | Load(args->at(2)); |
| 3598 | Load(args->at(3)); |
| 3599 | |
| 3600 | frame_->CallRuntime(Runtime::kRegExpExec, 4); |
| 3601 | frame_->EmitPush(r0); |
| 3602 | } |
| 3603 | |
| 3604 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3605 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
| 3606 | VirtualFrame::SpilledScope spilled_scope; |
| 3607 | ASSERT(args->length() == 2); |
| 3608 | |
| 3609 | // Load the two objects into registers and perform the comparison. |
| 3610 | LoadAndSpill(args->at(0)); |
| 3611 | LoadAndSpill(args->at(1)); |
| 3612 | frame_->EmitPop(r0); |
| 3613 | frame_->EmitPop(r1); |
| 3614 | __ cmp(r0, Operand(r1)); |
| 3615 | cc_reg_ = eq; |
| 3616 | } |
| 3617 | |
| 3618 | |
| 3619 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
| 3620 | #ifdef DEBUG |
| 3621 | int original_height = frame_->height(); |
| 3622 | #endif |
| 3623 | VirtualFrame::SpilledScope spilled_scope; |
| 3624 | if (CheckForInlineRuntimeCall(node)) { |
| 3625 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3626 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3627 | return; |
| 3628 | } |
| 3629 | |
| 3630 | ZoneList<Expression*>* args = node->arguments(); |
| 3631 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3632 | Runtime::Function* function = node->function(); |
| 3633 | |
| 3634 | if (function == NULL) { |
| 3635 | // Prepare stack for calling JS runtime function. |
| 3636 | __ mov(r0, Operand(node->name())); |
| 3637 | frame_->EmitPush(r0); |
| 3638 | // Push the builtins object found in the current global object. |
| 3639 | __ ldr(r1, GlobalObject()); |
| 3640 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
| 3641 | frame_->EmitPush(r0); |
| 3642 | } |
| 3643 | |
| 3644 | // Push the arguments ("left-to-right"). |
| 3645 | int arg_count = args->length(); |
| 3646 | for (int i = 0; i < arg_count; i++) { |
| 3647 | LoadAndSpill(args->at(i)); |
| 3648 | } |
| 3649 | |
| 3650 | if (function == NULL) { |
| 3651 | // Call the JS runtime function. |
| 3652 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3653 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
| 3654 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
| 3655 | __ ldr(cp, frame_->Context()); |
| 3656 | frame_->Drop(); |
| 3657 | frame_->EmitPush(r0); |
| 3658 | } else { |
| 3659 | // Call the C runtime function. |
| 3660 | frame_->CallRuntime(function, arg_count); |
| 3661 | frame_->EmitPush(r0); |
| 3662 | } |
| 3663 | ASSERT(frame_->height() == original_height + 1); |
| 3664 | } |
| 3665 | |
| 3666 | |
| 3667 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
| 3668 | #ifdef DEBUG |
| 3669 | int original_height = frame_->height(); |
| 3670 | #endif |
| 3671 | VirtualFrame::SpilledScope spilled_scope; |
| 3672 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3673 | |
| 3674 | Token::Value op = node->op(); |
| 3675 | |
| 3676 | if (op == Token::NOT) { |
| 3677 | LoadConditionAndSpill(node->expression(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3678 | false_target(), |
| 3679 | true_target(), |
| 3680 | true); |
| 3681 | // LoadCondition may (and usually does) leave a test and branch to |
| 3682 | // be emitted by the caller. In that case, negate the condition. |
| 3683 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
| 3684 | |
| 3685 | } else if (op == Token::DELETE) { |
| 3686 | Property* property = node->expression()->AsProperty(); |
| 3687 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
| 3688 | if (property != NULL) { |
| 3689 | LoadAndSpill(property->obj()); |
| 3690 | LoadAndSpill(property->key()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3691 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3692 | |
| 3693 | } else if (variable != NULL) { |
| 3694 | Slot* slot = variable->slot(); |
| 3695 | if (variable->is_global()) { |
| 3696 | LoadGlobal(); |
| 3697 | __ mov(r0, Operand(variable->name())); |
| 3698 | frame_->EmitPush(r0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3699 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3700 | |
| 3701 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3702 | // lookup the context holding the named variable |
| 3703 | frame_->EmitPush(cp); |
| 3704 | __ mov(r0, Operand(variable->name())); |
| 3705 | frame_->EmitPush(r0); |
| 3706 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
| 3707 | // r0: context |
| 3708 | frame_->EmitPush(r0); |
| 3709 | __ mov(r0, Operand(variable->name())); |
| 3710 | frame_->EmitPush(r0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3711 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3712 | |
| 3713 | } else { |
| 3714 | // Default: Result of deleting non-global, not dynamically |
| 3715 | // introduced variables is false. |
| 3716 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
| 3717 | } |
| 3718 | |
| 3719 | } else { |
| 3720 | // Default: Result of deleting expressions is true. |
| 3721 | LoadAndSpill(node->expression()); // may have side-effects |
| 3722 | frame_->Drop(); |
| 3723 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
| 3724 | } |
| 3725 | frame_->EmitPush(r0); |
| 3726 | |
| 3727 | } else if (op == Token::TYPEOF) { |
| 3728 | // Special case for loading the typeof expression; see comment on |
| 3729 | // LoadTypeofExpression(). |
| 3730 | LoadTypeofExpression(node->expression()); |
| 3731 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3732 | frame_->EmitPush(r0); // r0 has result |
| 3733 | |
| 3734 | } else { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 3735 | bool overwrite = |
| 3736 | (node->expression()->AsBinaryOperation() != NULL && |
| 3737 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3738 | LoadAndSpill(node->expression()); |
| 3739 | frame_->EmitPop(r0); |
| 3740 | switch (op) { |
| 3741 | case Token::NOT: |
| 3742 | case Token::DELETE: |
| 3743 | case Token::TYPEOF: |
| 3744 | UNREACHABLE(); // handled above |
| 3745 | break; |
| 3746 | |
| 3747 | case Token::SUB: { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3748 | GenericUnaryOpStub stub(Token::SUB, overwrite); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3749 | frame_->CallStub(&stub, 0); |
| 3750 | break; |
| 3751 | } |
| 3752 | |
| 3753 | case Token::BIT_NOT: { |
| 3754 | // smi check |
| 3755 | JumpTarget smi_label; |
| 3756 | JumpTarget continue_label; |
| 3757 | __ tst(r0, Operand(kSmiTagMask)); |
| 3758 | smi_label.Branch(eq); |
| 3759 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 3760 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 3761 | frame_->CallStub(&stub, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3762 | continue_label.Jump(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 3763 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3764 | smi_label.Bind(); |
| 3765 | __ mvn(r0, Operand(r0)); |
| 3766 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
| 3767 | continue_label.Bind(); |
| 3768 | break; |
| 3769 | } |
| 3770 | |
| 3771 | case Token::VOID: |
| 3772 | // since the stack top is cached in r0, popping and then |
| 3773 | // pushing a value can be done by just writing to r0. |
| 3774 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
| 3775 | break; |
| 3776 | |
| 3777 | case Token::ADD: { |
| 3778 | // Smi check. |
| 3779 | JumpTarget continue_label; |
| 3780 | __ tst(r0, Operand(kSmiTagMask)); |
| 3781 | continue_label.Branch(eq); |
| 3782 | frame_->EmitPush(r0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3783 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3784 | continue_label.Bind(); |
| 3785 | break; |
| 3786 | } |
| 3787 | default: |
| 3788 | UNREACHABLE(); |
| 3789 | } |
| 3790 | frame_->EmitPush(r0); // r0 has result |
| 3791 | } |
| 3792 | ASSERT(!has_valid_frame() || |
| 3793 | (has_cc() && frame_->height() == original_height) || |
| 3794 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3795 | } |
| 3796 | |
| 3797 | |
| 3798 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
| 3799 | #ifdef DEBUG |
| 3800 | int original_height = frame_->height(); |
| 3801 | #endif |
| 3802 | VirtualFrame::SpilledScope spilled_scope; |
| 3803 | Comment cmnt(masm_, "[ CountOperation"); |
| 3804 | |
| 3805 | bool is_postfix = node->is_postfix(); |
| 3806 | bool is_increment = node->op() == Token::INC; |
| 3807 | |
| 3808 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3809 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3810 | |
| 3811 | // Postfix: Make room for the result. |
| 3812 | if (is_postfix) { |
| 3813 | __ mov(r0, Operand(0)); |
| 3814 | frame_->EmitPush(r0); |
| 3815 | } |
| 3816 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3817 | // A constant reference is not saved to, so a constant reference is not a |
| 3818 | // compound assignment reference. |
| 3819 | { Reference target(this, node->expression(), !is_const); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3820 | if (target.is_illegal()) { |
| 3821 | // Spoof the virtual frame to have the expected height (one higher |
| 3822 | // than on entry). |
| 3823 | if (!is_postfix) { |
| 3824 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3825 | frame_->EmitPush(r0); |
| 3826 | } |
| 3827 | ASSERT(frame_->height() == original_height + 1); |
| 3828 | return; |
| 3829 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3830 | target.GetValueAndSpill(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3831 | frame_->EmitPop(r0); |
| 3832 | |
| 3833 | JumpTarget slow; |
| 3834 | JumpTarget exit; |
| 3835 | |
| 3836 | // Load the value (1) into register r1. |
| 3837 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3838 | |
| 3839 | // Check for smi operand. |
| 3840 | __ tst(r0, Operand(kSmiTagMask)); |
| 3841 | slow.Branch(ne); |
| 3842 | |
| 3843 | // Postfix: Store the old value as the result. |
| 3844 | if (is_postfix) { |
| 3845 | __ str(r0, frame_->ElementAt(target.size())); |
| 3846 | } |
| 3847 | |
| 3848 | // Perform optimistic increment/decrement. |
| 3849 | if (is_increment) { |
| 3850 | __ add(r0, r0, Operand(r1), SetCC); |
| 3851 | } else { |
| 3852 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3853 | } |
| 3854 | |
| 3855 | // If the increment/decrement didn't overflow, we're done. |
| 3856 | exit.Branch(vc); |
| 3857 | |
| 3858 | // Revert optimistic increment/decrement. |
| 3859 | if (is_increment) { |
| 3860 | __ sub(r0, r0, Operand(r1)); |
| 3861 | } else { |
| 3862 | __ add(r0, r0, Operand(r1)); |
| 3863 | } |
| 3864 | |
| 3865 | // Slow case: Convert to number. |
| 3866 | slow.Bind(); |
| 3867 | { |
| 3868 | // Convert the operand to a number. |
| 3869 | frame_->EmitPush(r0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3870 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3871 | } |
| 3872 | if (is_postfix) { |
| 3873 | // Postfix: store to result (on the stack). |
| 3874 | __ str(r0, frame_->ElementAt(target.size())); |
| 3875 | } |
| 3876 | |
| 3877 | // Compute the new value. |
| 3878 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3879 | frame_->EmitPush(r0); |
| 3880 | frame_->EmitPush(r1); |
| 3881 | if (is_increment) { |
| 3882 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
| 3883 | } else { |
| 3884 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
| 3885 | } |
| 3886 | |
| 3887 | // Store the new value in the target if not const. |
| 3888 | exit.Bind(); |
| 3889 | frame_->EmitPush(r0); |
| 3890 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
| 3891 | } |
| 3892 | |
| 3893 | // Postfix: Discard the new value and use the old. |
| 3894 | if (is_postfix) frame_->EmitPop(r0); |
| 3895 | ASSERT(frame_->height() == original_height + 1); |
| 3896 | } |
| 3897 | |
| 3898 | |
| 3899 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
| 3900 | #ifdef DEBUG |
| 3901 | int original_height = frame_->height(); |
| 3902 | #endif |
| 3903 | VirtualFrame::SpilledScope spilled_scope; |
| 3904 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3905 | Token::Value op = node->op(); |
| 3906 | |
| 3907 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3908 | // operators must yield the result of one of the two expressions |
| 3909 | // before any ToBoolean() conversions. This means that the value |
| 3910 | // produced by a && or || operator is not necessarily a boolean. |
| 3911 | |
| 3912 | // NOTE: If the left hand side produces a materialized value (not in |
| 3913 | // the CC register), we force the right hand side to do the |
| 3914 | // same. This is necessary because we may have to branch to the exit |
| 3915 | // after evaluating the left hand side (due to the shortcut |
| 3916 | // semantics), but the compiler must (statically) know if the result |
| 3917 | // of compiling the binary operation is materialized or not. |
| 3918 | |
| 3919 | if (op == Token::AND) { |
| 3920 | JumpTarget is_true; |
| 3921 | LoadConditionAndSpill(node->left(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3922 | &is_true, |
| 3923 | false_target(), |
| 3924 | false); |
| 3925 | if (has_valid_frame() && !has_cc()) { |
| 3926 | // The left-hand side result is on top of the virtual frame. |
| 3927 | JumpTarget pop_and_continue; |
| 3928 | JumpTarget exit; |
| 3929 | |
| 3930 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
| 3931 | frame_->EmitPush(r0); |
| 3932 | // Avoid popping the result if it converts to 'false' using the |
| 3933 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3934 | // section 9.2, page 30. |
| 3935 | ToBoolean(&pop_and_continue, &exit); |
| 3936 | Branch(false, &exit); |
| 3937 | |
| 3938 | // Pop the result of evaluating the first part. |
| 3939 | pop_and_continue.Bind(); |
| 3940 | frame_->EmitPop(r0); |
| 3941 | |
| 3942 | // Evaluate right side expression. |
| 3943 | is_true.Bind(); |
| 3944 | LoadAndSpill(node->right()); |
| 3945 | |
| 3946 | // Exit (always with a materialized value). |
| 3947 | exit.Bind(); |
| 3948 | } else if (has_cc() || is_true.is_linked()) { |
| 3949 | // The left-hand side is either (a) partially compiled to |
| 3950 | // control flow with a final branch left to emit or (b) fully |
| 3951 | // compiled to control flow and possibly true. |
| 3952 | if (has_cc()) { |
| 3953 | Branch(false, false_target()); |
| 3954 | } |
| 3955 | is_true.Bind(); |
| 3956 | LoadConditionAndSpill(node->right(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3957 | true_target(), |
| 3958 | false_target(), |
| 3959 | false); |
| 3960 | } else { |
| 3961 | // Nothing to do. |
| 3962 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
| 3963 | } |
| 3964 | |
| 3965 | } else if (op == Token::OR) { |
| 3966 | JumpTarget is_false; |
| 3967 | LoadConditionAndSpill(node->left(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3968 | true_target(), |
| 3969 | &is_false, |
| 3970 | false); |
| 3971 | if (has_valid_frame() && !has_cc()) { |
| 3972 | // The left-hand side result is on top of the virtual frame. |
| 3973 | JumpTarget pop_and_continue; |
| 3974 | JumpTarget exit; |
| 3975 | |
| 3976 | __ ldr(r0, frame_->Top()); |
| 3977 | frame_->EmitPush(r0); |
| 3978 | // Avoid popping the result if it converts to 'true' using the |
| 3979 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3980 | // section 9.2, page 30. |
| 3981 | ToBoolean(&exit, &pop_and_continue); |
| 3982 | Branch(true, &exit); |
| 3983 | |
| 3984 | // Pop the result of evaluating the first part. |
| 3985 | pop_and_continue.Bind(); |
| 3986 | frame_->EmitPop(r0); |
| 3987 | |
| 3988 | // Evaluate right side expression. |
| 3989 | is_false.Bind(); |
| 3990 | LoadAndSpill(node->right()); |
| 3991 | |
| 3992 | // Exit (always with a materialized value). |
| 3993 | exit.Bind(); |
| 3994 | } else if (has_cc() || is_false.is_linked()) { |
| 3995 | // The left-hand side is either (a) partially compiled to |
| 3996 | // control flow with a final branch left to emit or (b) fully |
| 3997 | // compiled to control flow and possibly false. |
| 3998 | if (has_cc()) { |
| 3999 | Branch(true, true_target()); |
| 4000 | } |
| 4001 | is_false.Bind(); |
| 4002 | LoadConditionAndSpill(node->right(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4003 | true_target(), |
| 4004 | false_target(), |
| 4005 | false); |
| 4006 | } else { |
| 4007 | // Nothing to do. |
| 4008 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
| 4009 | } |
| 4010 | |
| 4011 | } else { |
| 4012 | // Optimize for the case where (at least) one of the expressions |
| 4013 | // is a literal small integer. |
| 4014 | Literal* lliteral = node->left()->AsLiteral(); |
| 4015 | Literal* rliteral = node->right()->AsLiteral(); |
| 4016 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 4017 | // never return a constant/immutable object. |
| 4018 | bool overwrite_left = |
| 4019 | (node->left()->AsBinaryOperation() != NULL && |
| 4020 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 4021 | bool overwrite_right = |
| 4022 | (node->right()->AsBinaryOperation() != NULL && |
| 4023 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 4024 | |
| 4025 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
| 4026 | LoadAndSpill(node->left()); |
| 4027 | SmiOperation(node->op(), |
| 4028 | rliteral->handle(), |
| 4029 | false, |
| 4030 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 4031 | |
| 4032 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
| 4033 | LoadAndSpill(node->right()); |
| 4034 | SmiOperation(node->op(), |
| 4035 | lliteral->handle(), |
| 4036 | true, |
| 4037 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
| 4038 | |
| 4039 | } else { |
| 4040 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 4041 | if (overwrite_left) { |
| 4042 | overwrite_mode = OVERWRITE_LEFT; |
| 4043 | } else if (overwrite_right) { |
| 4044 | overwrite_mode = OVERWRITE_RIGHT; |
| 4045 | } |
| 4046 | LoadAndSpill(node->left()); |
| 4047 | LoadAndSpill(node->right()); |
| 4048 | GenericBinaryOperation(node->op(), overwrite_mode); |
| 4049 | } |
| 4050 | frame_->EmitPush(r0); |
| 4051 | } |
| 4052 | ASSERT(!has_valid_frame() || |
| 4053 | (has_cc() && frame_->height() == original_height) || |
| 4054 | (!has_cc() && frame_->height() == original_height + 1)); |
| 4055 | } |
| 4056 | |
| 4057 | |
| 4058 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
| 4059 | #ifdef DEBUG |
| 4060 | int original_height = frame_->height(); |
| 4061 | #endif |
| 4062 | VirtualFrame::SpilledScope spilled_scope; |
| 4063 | __ ldr(r0, frame_->Function()); |
| 4064 | frame_->EmitPush(r0); |
| 4065 | ASSERT(frame_->height() == original_height + 1); |
| 4066 | } |
| 4067 | |
| 4068 | |
| 4069 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
| 4070 | #ifdef DEBUG |
| 4071 | int original_height = frame_->height(); |
| 4072 | #endif |
| 4073 | VirtualFrame::SpilledScope spilled_scope; |
| 4074 | Comment cmnt(masm_, "[ CompareOperation"); |
| 4075 | |
| 4076 | // Get the expressions from the node. |
| 4077 | Expression* left = node->left(); |
| 4078 | Expression* right = node->right(); |
| 4079 | Token::Value op = node->op(); |
| 4080 | |
| 4081 | // To make null checks efficient, we check if either left or right is the |
| 4082 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 4083 | // instead of calling the (very) general runtime routine for checking |
| 4084 | // equality. |
| 4085 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
| 4086 | bool left_is_null = |
| 4087 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
| 4088 | bool right_is_null = |
| 4089 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4090 | // The 'null' value can only be equal to 'null' or 'undefined'. |
| 4091 | if (left_is_null || right_is_null) { |
| 4092 | LoadAndSpill(left_is_null ? right : left); |
| 4093 | frame_->EmitPop(r0); |
| 4094 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4095 | __ cmp(r0, ip); |
| 4096 | |
| 4097 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4098 | // comparisons. |
| 4099 | if (op != Token::EQ_STRICT) { |
| 4100 | true_target()->Branch(eq); |
| 4101 | |
| 4102 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4103 | __ cmp(r0, Operand(ip)); |
| 4104 | true_target()->Branch(eq); |
| 4105 | |
| 4106 | __ tst(r0, Operand(kSmiTagMask)); |
| 4107 | false_target()->Branch(eq); |
| 4108 | |
| 4109 | // It can be an undetectable object. |
| 4110 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4111 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 4112 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 4113 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
| 4114 | } |
| 4115 | |
| 4116 | cc_reg_ = eq; |
| 4117 | ASSERT(has_cc() && frame_->height() == original_height); |
| 4118 | return; |
| 4119 | } |
| 4120 | } |
| 4121 | |
| 4122 | // To make typeof testing for natives implemented in JavaScript really |
| 4123 | // efficient, we generate special code for expressions of the form: |
| 4124 | // 'typeof <expression> == <string>'. |
| 4125 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 4126 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4127 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4128 | (right->AsLiteral() != NULL && |
| 4129 | right->AsLiteral()->handle()->IsString())) { |
| 4130 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4131 | |
| 4132 | // Load the operand, move it to register r1. |
| 4133 | LoadTypeofExpression(operation->expression()); |
| 4134 | frame_->EmitPop(r1); |
| 4135 | |
| 4136 | if (check->Equals(Heap::number_symbol())) { |
| 4137 | __ tst(r1, Operand(kSmiTagMask)); |
| 4138 | true_target()->Branch(eq); |
| 4139 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4140 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 4141 | __ cmp(r1, ip); |
| 4142 | cc_reg_ = eq; |
| 4143 | |
| 4144 | } else if (check->Equals(Heap::string_symbol())) { |
| 4145 | __ tst(r1, Operand(kSmiTagMask)); |
| 4146 | false_target()->Branch(eq); |
| 4147 | |
| 4148 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4149 | |
| 4150 | // It can be an undetectable string object. |
| 4151 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4152 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4153 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4154 | false_target()->Branch(eq); |
| 4155 | |
| 4156 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 4157 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 4158 | cc_reg_ = lt; |
| 4159 | |
| 4160 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 4161 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 4162 | __ cmp(r1, ip); |
| 4163 | true_target()->Branch(eq); |
| 4164 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 4165 | __ cmp(r1, ip); |
| 4166 | cc_reg_ = eq; |
| 4167 | |
| 4168 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 4169 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4170 | __ cmp(r1, ip); |
| 4171 | true_target()->Branch(eq); |
| 4172 | |
| 4173 | __ tst(r1, Operand(kSmiTagMask)); |
| 4174 | false_target()->Branch(eq); |
| 4175 | |
| 4176 | // It can be an undetectable object. |
| 4177 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4178 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4179 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4180 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4181 | |
| 4182 | cc_reg_ = eq; |
| 4183 | |
| 4184 | } else if (check->Equals(Heap::function_symbol())) { |
| 4185 | __ tst(r1, Operand(kSmiTagMask)); |
| 4186 | false_target()->Branch(eq); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4187 | Register map_reg = r2; |
| 4188 | __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE); |
| 4189 | true_target()->Branch(eq); |
| 4190 | // Regular expressions are callable so typeof == 'function'. |
| 4191 | __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4192 | cc_reg_ = eq; |
| 4193 | |
| 4194 | } else if (check->Equals(Heap::object_symbol())) { |
| 4195 | __ tst(r1, Operand(kSmiTagMask)); |
| 4196 | false_target()->Branch(eq); |
| 4197 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4198 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4199 | __ cmp(r1, ip); |
| 4200 | true_target()->Branch(eq); |
| 4201 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4202 | Register map_reg = r2; |
| 4203 | __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE); |
| 4204 | false_target()->Branch(eq); |
| 4205 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4206 | // It can be an undetectable object. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4207 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4208 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4209 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 4210 | false_target()->Branch(eq); |
| 4211 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4212 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4213 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4214 | false_target()->Branch(lt); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4215 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4216 | cc_reg_ = le; |
| 4217 | |
| 4218 | } else { |
| 4219 | // Uncommon case: typeof testing against a string literal that is |
| 4220 | // never returned from the typeof operator. |
| 4221 | false_target()->Jump(); |
| 4222 | } |
| 4223 | ASSERT(!has_valid_frame() || |
| 4224 | (has_cc() && frame_->height() == original_height)); |
| 4225 | return; |
| 4226 | } |
| 4227 | |
| 4228 | switch (op) { |
| 4229 | case Token::EQ: |
| 4230 | Comparison(eq, left, right, false); |
| 4231 | break; |
| 4232 | |
| 4233 | case Token::LT: |
| 4234 | Comparison(lt, left, right); |
| 4235 | break; |
| 4236 | |
| 4237 | case Token::GT: |
| 4238 | Comparison(gt, left, right); |
| 4239 | break; |
| 4240 | |
| 4241 | case Token::LTE: |
| 4242 | Comparison(le, left, right); |
| 4243 | break; |
| 4244 | |
| 4245 | case Token::GTE: |
| 4246 | Comparison(ge, left, right); |
| 4247 | break; |
| 4248 | |
| 4249 | case Token::EQ_STRICT: |
| 4250 | Comparison(eq, left, right, true); |
| 4251 | break; |
| 4252 | |
| 4253 | case Token::IN: { |
| 4254 | LoadAndSpill(left); |
| 4255 | LoadAndSpill(right); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4256 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4257 | frame_->EmitPush(r0); |
| 4258 | break; |
| 4259 | } |
| 4260 | |
| 4261 | case Token::INSTANCEOF: { |
| 4262 | LoadAndSpill(left); |
| 4263 | LoadAndSpill(right); |
| 4264 | InstanceofStub stub; |
| 4265 | frame_->CallStub(&stub, 2); |
| 4266 | // At this point if instanceof succeeded then r0 == 0. |
| 4267 | __ tst(r0, Operand(r0)); |
| 4268 | cc_reg_ = eq; |
| 4269 | break; |
| 4270 | } |
| 4271 | |
| 4272 | default: |
| 4273 | UNREACHABLE(); |
| 4274 | } |
| 4275 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4276 | (!has_cc() && frame_->height() == original_height + 1)); |
| 4277 | } |
| 4278 | |
| 4279 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4280 | void CodeGenerator::EmitKeyedLoad(bool is_global) { |
| 4281 | Comment cmnt(masm_, "[ Load from keyed Property"); |
| 4282 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 4283 | RelocInfo::Mode rmode = is_global |
| 4284 | ? RelocInfo::CODE_TARGET_CONTEXT |
| 4285 | : RelocInfo::CODE_TARGET; |
| 4286 | frame_->CallCodeObject(ic, rmode, 0); |
| 4287 | } |
| 4288 | |
| 4289 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4290 | #ifdef DEBUG |
| 4291 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4292 | #endif |
| 4293 | |
| 4294 | |
| 4295 | #undef __ |
| 4296 | #define __ ACCESS_MASM(masm) |
| 4297 | |
| 4298 | |
| 4299 | Handle<String> Reference::GetName() { |
| 4300 | ASSERT(type_ == NAMED); |
| 4301 | Property* property = expression_->AsProperty(); |
| 4302 | if (property == NULL) { |
| 4303 | // Global variable reference treated as a named property reference. |
| 4304 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4305 | ASSERT(proxy->AsVariable() != NULL); |
| 4306 | ASSERT(proxy->AsVariable()->is_global()); |
| 4307 | return proxy->name(); |
| 4308 | } else { |
| 4309 | Literal* raw_name = property->key()->AsLiteral(); |
| 4310 | ASSERT(raw_name != NULL); |
| 4311 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4312 | } |
| 4313 | } |
| 4314 | |
| 4315 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4316 | void Reference::GetValue() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4317 | ASSERT(cgen_->HasValidEntryRegisters()); |
| 4318 | ASSERT(!is_illegal()); |
| 4319 | ASSERT(!cgen_->has_cc()); |
| 4320 | MacroAssembler* masm = cgen_->masm(); |
| 4321 | Property* property = expression_->AsProperty(); |
| 4322 | if (property != NULL) { |
| 4323 | cgen_->CodeForSourcePosition(property->position()); |
| 4324 | } |
| 4325 | |
| 4326 | switch (type_) { |
| 4327 | case SLOT: { |
| 4328 | Comment cmnt(masm, "[ Load from Slot"); |
| 4329 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4330 | ASSERT(slot != NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4331 | cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4332 | break; |
| 4333 | } |
| 4334 | |
| 4335 | case NAMED: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4336 | VirtualFrame* frame = cgen_->frame(); |
| 4337 | Comment cmnt(masm, "[ Load from named Property"); |
| 4338 | Handle<String> name(GetName()); |
| 4339 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 4340 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4341 | // Setup the name register. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4342 | __ mov(r2, Operand(name)); |
| 4343 | ASSERT(var == NULL || var->is_global()); |
| 4344 | RelocInfo::Mode rmode = (var == NULL) |
| 4345 | ? RelocInfo::CODE_TARGET |
| 4346 | : RelocInfo::CODE_TARGET_CONTEXT; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 4347 | frame->CallCodeObject(ic, rmode, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4348 | frame->EmitPush(r0); |
| 4349 | break; |
| 4350 | } |
| 4351 | |
| 4352 | case KEYED: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4353 | // TODO(181): Implement inlined version of array indexing once |
| 4354 | // loop nesting is properly tracked on ARM. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4355 | ASSERT(property != NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4356 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 4357 | ASSERT(var == NULL || var->is_global()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4358 | cgen_->EmitKeyedLoad(var != NULL); |
| 4359 | cgen_->frame()->EmitPush(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4360 | break; |
| 4361 | } |
| 4362 | |
| 4363 | default: |
| 4364 | UNREACHABLE(); |
| 4365 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4366 | |
| 4367 | if (!persist_after_get_) { |
| 4368 | cgen_->UnloadReference(this); |
| 4369 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | |
| 4373 | void Reference::SetValue(InitState init_state) { |
| 4374 | ASSERT(!is_illegal()); |
| 4375 | ASSERT(!cgen_->has_cc()); |
| 4376 | MacroAssembler* masm = cgen_->masm(); |
| 4377 | VirtualFrame* frame = cgen_->frame(); |
| 4378 | Property* property = expression_->AsProperty(); |
| 4379 | if (property != NULL) { |
| 4380 | cgen_->CodeForSourcePosition(property->position()); |
| 4381 | } |
| 4382 | |
| 4383 | switch (type_) { |
| 4384 | case SLOT: { |
| 4385 | Comment cmnt(masm, "[ Store to Slot"); |
| 4386 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4387 | cgen_->StoreToSlot(slot, init_state); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 4388 | cgen_->UnloadReference(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4389 | break; |
| 4390 | } |
| 4391 | |
| 4392 | case NAMED: { |
| 4393 | Comment cmnt(masm, "[ Store to named Property"); |
| 4394 | // Call the appropriate IC code. |
| 4395 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 4396 | Handle<String> name(GetName()); |
| 4397 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4398 | frame->EmitPop(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4399 | // Setup the name register. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4400 | __ mov(r2, Operand(name)); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 4401 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4402 | frame->EmitPush(r0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 4403 | cgen_->UnloadReference(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4404 | break; |
| 4405 | } |
| 4406 | |
| 4407 | case KEYED: { |
| 4408 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4409 | Property* property = expression_->AsProperty(); |
| 4410 | ASSERT(property != NULL); |
| 4411 | cgen_->CodeForSourcePosition(property->position()); |
| 4412 | |
| 4413 | // Call IC code. |
| 4414 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4415 | // TODO(1222589): Make the IC grab the values from the stack. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4416 | frame->EmitPop(r0); // value |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 4417 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4418 | frame->EmitPush(r0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 4419 | cgen_->UnloadReference(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4420 | break; |
| 4421 | } |
| 4422 | |
| 4423 | default: |
| 4424 | UNREACHABLE(); |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4429 | void FastNewClosureStub::Generate(MacroAssembler* masm) { |
| 4430 | // Clone the boilerplate in new space. Set the context to the |
| 4431 | // current context in cp. |
| 4432 | Label gc; |
| 4433 | |
| 4434 | // Pop the boilerplate function from the stack. |
| 4435 | __ pop(r3); |
| 4436 | |
| 4437 | // Attempt to allocate new JSFunction in new space. |
| 4438 | __ AllocateInNewSpace(JSFunction::kSize / kPointerSize, |
| 4439 | r0, |
| 4440 | r1, |
| 4441 | r2, |
| 4442 | &gc, |
| 4443 | TAG_OBJECT); |
| 4444 | |
| 4445 | // Compute the function map in the current global context and set that |
| 4446 | // as the map of the allocated object. |
| 4447 | __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4448 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 4449 | __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX))); |
| 4450 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4451 | |
| 4452 | // Clone the rest of the boilerplate fields. We don't have to update |
| 4453 | // the write barrier because the allocated object is in new space. |
| 4454 | for (int offset = kPointerSize; |
| 4455 | offset < JSFunction::kSize; |
| 4456 | offset += kPointerSize) { |
| 4457 | if (offset == JSFunction::kContextOffset) { |
| 4458 | __ str(cp, FieldMemOperand(r0, offset)); |
| 4459 | } else { |
| 4460 | __ ldr(r1, FieldMemOperand(r3, offset)); |
| 4461 | __ str(r1, FieldMemOperand(r0, offset)); |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | // Return result. The argument boilerplate has been popped already. |
| 4466 | __ Ret(); |
| 4467 | |
| 4468 | // Create a new closure through the slower runtime call. |
| 4469 | __ bind(&gc); |
| 4470 | __ push(cp); |
| 4471 | __ push(r3); |
| 4472 | __ TailCallRuntime(ExternalReference(Runtime::kNewClosure), 2, 1); |
| 4473 | } |
| 4474 | |
| 4475 | |
| 4476 | void FastNewContextStub::Generate(MacroAssembler* masm) { |
| 4477 | // Try to allocate the context in new space. |
| 4478 | Label gc; |
| 4479 | int length = slots_ + Context::MIN_CONTEXT_SLOTS; |
| 4480 | |
| 4481 | // Attempt to allocate the context in new space. |
| 4482 | __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize), |
| 4483 | r0, |
| 4484 | r1, |
| 4485 | r2, |
| 4486 | &gc, |
| 4487 | TAG_OBJECT); |
| 4488 | |
| 4489 | // Load the function from the stack. |
| 4490 | __ ldr(r3, MemOperand(sp, 0 * kPointerSize)); |
| 4491 | |
| 4492 | // Setup the object header. |
| 4493 | __ LoadRoot(r2, Heap::kContextMapRootIndex); |
| 4494 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4495 | __ mov(r2, Operand(length)); |
| 4496 | __ str(r2, FieldMemOperand(r0, Array::kLengthOffset)); |
| 4497 | |
| 4498 | // Setup the fixed slots. |
| 4499 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 4500 | __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 4501 | __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 4502 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
| 4503 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX))); |
| 4504 | |
| 4505 | // Copy the global object from the surrounding context. |
| 4506 | __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4507 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4508 | |
| 4509 | // Initialize the rest of the slots to undefined. |
| 4510 | __ LoadRoot(r1, Heap::kUndefinedValueRootIndex); |
| 4511 | for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) { |
| 4512 | __ str(r1, MemOperand(r0, Context::SlotOffset(i))); |
| 4513 | } |
| 4514 | |
| 4515 | // Remove the on-stack argument and return. |
| 4516 | __ mov(cp, r0); |
| 4517 | __ pop(); |
| 4518 | __ Ret(); |
| 4519 | |
| 4520 | // Need to collect. Call into runtime system. |
| 4521 | __ bind(&gc); |
| 4522 | __ TailCallRuntime(ExternalReference(Runtime::kNewContext), 1, 1); |
| 4523 | } |
| 4524 | |
| 4525 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4526 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4527 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4528 | // (31 instead of 32). |
| 4529 | static void CountLeadingZeros( |
| 4530 | MacroAssembler* masm, |
| 4531 | Register source, |
| 4532 | Register scratch, |
| 4533 | Register zeros) { |
| 4534 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
| 4535 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4536 | #else |
| 4537 | __ mov(zeros, Operand(0)); |
| 4538 | __ mov(scratch, source); |
| 4539 | // Top 16. |
| 4540 | __ tst(scratch, Operand(0xffff0000)); |
| 4541 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4542 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4543 | // Top 8. |
| 4544 | __ tst(scratch, Operand(0xff000000)); |
| 4545 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4546 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4547 | // Top 4. |
| 4548 | __ tst(scratch, Operand(0xf0000000)); |
| 4549 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4550 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4551 | // Top 2. |
| 4552 | __ tst(scratch, Operand(0xc0000000)); |
| 4553 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4554 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4555 | // Top bit. |
| 4556 | __ tst(scratch, Operand(0x80000000u)); |
| 4557 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4558 | #endif |
| 4559 | } |
| 4560 | |
| 4561 | |
| 4562 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4563 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4564 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4565 | // scratch register. Destroys the source register. No GC occurs during this |
| 4566 | // stub so you don't have to set up the frame. |
| 4567 | class ConvertToDoubleStub : public CodeStub { |
| 4568 | public: |
| 4569 | ConvertToDoubleStub(Register result_reg_1, |
| 4570 | Register result_reg_2, |
| 4571 | Register source_reg, |
| 4572 | Register scratch_reg) |
| 4573 | : result1_(result_reg_1), |
| 4574 | result2_(result_reg_2), |
| 4575 | source_(source_reg), |
| 4576 | zeros_(scratch_reg) { } |
| 4577 | |
| 4578 | private: |
| 4579 | Register result1_; |
| 4580 | Register result2_; |
| 4581 | Register source_; |
| 4582 | Register zeros_; |
| 4583 | |
| 4584 | // Minor key encoding in 16 bits. |
| 4585 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4586 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4587 | |
| 4588 | Major MajorKey() { return ConvertToDouble; } |
| 4589 | int MinorKey() { |
| 4590 | // Encode the parameters in a unique 16 bit value. |
| 4591 | return result1_.code() + |
| 4592 | (result2_.code() << 4) + |
| 4593 | (source_.code() << 8) + |
| 4594 | (zeros_.code() << 12); |
| 4595 | } |
| 4596 | |
| 4597 | void Generate(MacroAssembler* masm); |
| 4598 | |
| 4599 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4600 | |
| 4601 | #ifdef DEBUG |
| 4602 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4603 | #endif |
| 4604 | }; |
| 4605 | |
| 4606 | |
| 4607 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4608 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4609 | Register exponent = result1_; |
| 4610 | Register mantissa = result2_; |
| 4611 | #else |
| 4612 | Register exponent = result2_; |
| 4613 | Register mantissa = result1_; |
| 4614 | #endif |
| 4615 | Label not_special; |
| 4616 | // Convert from Smi to integer. |
| 4617 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4618 | // Move sign bit from source to destination. This works because the sign bit |
| 4619 | // in the exponent word of the double has the same position and polarity as |
| 4620 | // the 2's complement sign bit in a Smi. |
| 4621 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4622 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4623 | // Subtract from 0 if source was negative. |
| 4624 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4625 | __ cmp(source_, Operand(1)); |
| 4626 | __ b(gt, ¬_special); |
| 4627 | |
| 4628 | // We have -1, 0 or 1, which we treat specially. |
| 4629 | __ cmp(source_, Operand(0)); |
| 4630 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4631 | static const uint32_t exponent_word_for_1 = |
| 4632 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4633 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4634 | // 1, 0 and -1 all have 0 for the second word. |
| 4635 | __ mov(mantissa, Operand(0)); |
| 4636 | __ Ret(); |
| 4637 | |
| 4638 | __ bind(¬_special); |
| 4639 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4640 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4641 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4642 | // Compute exponent and or it into the exponent register. |
| 4643 | // We use result2 as a scratch register here. |
| 4644 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4645 | __ orr(exponent, |
| 4646 | exponent, |
| 4647 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4648 | // Shift up the source chopping the top bit off. |
| 4649 | __ add(zeros_, zeros_, Operand(1)); |
| 4650 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4651 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4652 | // Compute lower part of fraction (last 12 bits). |
| 4653 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4654 | // And the top (top 20 bits). |
| 4655 | __ orr(exponent, |
| 4656 | exponent, |
| 4657 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4658 | __ Ret(); |
| 4659 | } |
| 4660 | |
| 4661 | |
| 4662 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4663 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4664 | // so you don't have to set up the frame. |
| 4665 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4666 | public: |
| 4667 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4668 | Register the_heap_number, |
| 4669 | Register scratch) |
| 4670 | : the_int_(the_int), |
| 4671 | the_heap_number_(the_heap_number), |
| 4672 | scratch_(scratch) { } |
| 4673 | |
| 4674 | private: |
| 4675 | Register the_int_; |
| 4676 | Register the_heap_number_; |
| 4677 | Register scratch_; |
| 4678 | |
| 4679 | // Minor key encoding in 16 bits. |
| 4680 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4681 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4682 | |
| 4683 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4684 | int MinorKey() { |
| 4685 | // Encode the parameters in a unique 16 bit value. |
| 4686 | return the_int_.code() + |
| 4687 | (the_heap_number_.code() << 4) + |
| 4688 | (scratch_.code() << 8); |
| 4689 | } |
| 4690 | |
| 4691 | void Generate(MacroAssembler* masm); |
| 4692 | |
| 4693 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4694 | |
| 4695 | #ifdef DEBUG |
| 4696 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4697 | #endif |
| 4698 | }; |
| 4699 | |
| 4700 | |
| 4701 | // See comment for class. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4702 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4703 | Label max_negative_int; |
| 4704 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4705 | // We test for the special value that has a different exponent. This test |
| 4706 | // has the neat side effect of setting the flags according to the sign. |
| 4707 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4708 | __ cmp(the_int_, Operand(0x80000000u)); |
| 4709 | __ b(eq, &max_negative_int); |
| 4710 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4711 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4712 | uint32_t non_smi_exponent = |
| 4713 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4714 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4715 | // Set the sign bit in scratch_ if the value was negative. |
| 4716 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4717 | // Subtract from 0 if the value was negative. |
| 4718 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4719 | // We should be masking the implict first digit of the mantissa away here, |
| 4720 | // but it just ends up combining harmlessly with the last digit of the |
| 4721 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4722 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4723 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4724 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4725 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4726 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4727 | HeapNumber::kExponentOffset)); |
| 4728 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4729 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4730 | HeapNumber::kMantissaOffset)); |
| 4731 | __ Ret(); |
| 4732 | |
| 4733 | __ bind(&max_negative_int); |
| 4734 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4735 | // a double because it uses a sign bit instead of using two's complement. |
| 4736 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4737 | // significant 1 bit is not stored. |
| 4738 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4739 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4740 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4741 | __ mov(ip, Operand(0)); |
| 4742 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4743 | __ Ret(); |
| 4744 | } |
| 4745 | |
| 4746 | |
| 4747 | // Handle the case where the lhs and rhs are the same object. |
| 4748 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4749 | // for "identity and not NaN". |
| 4750 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4751 | Label* slow, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4752 | Condition cc, |
| 4753 | bool never_nan_nan) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4754 | Label not_identical; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4755 | Label heap_number, return_equal; |
| 4756 | Register exp_mask_reg = r5; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4757 | __ cmp(r0, Operand(r1)); |
| 4758 | __ b(ne, ¬_identical); |
| 4759 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4760 | // The two objects are identical. If we know that one of them isn't NaN then |
| 4761 | // we now know they test equal. |
| 4762 | if (cc != eq || !never_nan_nan) { |
| 4763 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4764 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4765 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4766 | // so we do the second best thing - test it ourselves. |
| 4767 | // They are both equal and they are not both Smis so both of them are not |
| 4768 | // Smis. If it's not a heap number, then return equal. |
| 4769 | if (cc == lt || cc == gt) { |
| 4770 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4771 | __ b(ge, slow); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4772 | } else { |
| 4773 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4774 | __ b(eq, &heap_number); |
| 4775 | // Comparing JS objects with <=, >= is complicated. |
| 4776 | if (cc != eq) { |
| 4777 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4778 | __ b(ge, slow); |
| 4779 | // Normally here we fall through to return_equal, but undefined is |
| 4780 | // special: (undefined == undefined) == true, but |
| 4781 | // (undefined <= undefined) == false! See ECMAScript 11.8.5. |
| 4782 | if (cc == le || cc == ge) { |
| 4783 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 4784 | __ b(ne, &return_equal); |
| 4785 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 4786 | __ cmp(r0, Operand(r2)); |
| 4787 | __ b(ne, &return_equal); |
| 4788 | if (cc == le) { |
| 4789 | // undefined <= undefined should fail. |
| 4790 | __ mov(r0, Operand(GREATER)); |
| 4791 | } else { |
| 4792 | // undefined >= undefined should fail. |
| 4793 | __ mov(r0, Operand(LESS)); |
| 4794 | } |
| 4795 | __ mov(pc, Operand(lr)); // Return. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4796 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4797 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4798 | } |
| 4799 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4800 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4801 | __ bind(&return_equal); |
| 4802 | if (cc == lt) { |
| 4803 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4804 | } else if (cc == gt) { |
| 4805 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4806 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4807 | __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4808 | } |
| 4809 | __ mov(pc, Operand(lr)); // Return. |
| 4810 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4811 | if (cc != eq || !never_nan_nan) { |
| 4812 | // For less and greater we don't have to check for NaN since the result of |
| 4813 | // x < x is false regardless. For the others here is some code to check |
| 4814 | // for NaN. |
| 4815 | if (cc != lt && cc != gt) { |
| 4816 | __ bind(&heap_number); |
| 4817 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4818 | // not NaN. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4819 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4820 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4821 | // and not all mantissa bits (0..51) clear. |
| 4822 | // Read top bits of double representation (second word of value). |
| 4823 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4824 | // Test that exponent bits are all set. |
| 4825 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4826 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4827 | __ b(ne, &return_equal); |
| 4828 | |
| 4829 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4830 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4831 | // Or with all low-bits of mantissa. |
| 4832 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4833 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4834 | // For equal we already have the right value in r0: Return zero (equal) |
| 4835 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if |
| 4836 | // not (it's a NaN). For <= and >= we need to load r0 with the failing |
| 4837 | // value if it's a NaN. |
| 4838 | if (cc != eq) { |
| 4839 | // All-zero means Infinity means equal. |
| 4840 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 4841 | if (cc == le) { |
| 4842 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 4843 | } else { |
| 4844 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 4845 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4846 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4847 | __ mov(pc, Operand(lr)); // Return. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4848 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4849 | // No fall through here. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4850 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4851 | |
| 4852 | __ bind(¬_identical); |
| 4853 | } |
| 4854 | |
| 4855 | |
| 4856 | // See comment at call site. |
| 4857 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4858 | Label* lhs_not_nan, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4859 | Label* slow, |
| 4860 | bool strict) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4861 | Label rhs_is_smi; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4862 | __ tst(r0, Operand(kSmiTagMask)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4863 | __ b(eq, &rhs_is_smi); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4864 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4865 | // Lhs is a Smi. Check whether the rhs is a heap number. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4866 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4867 | if (strict) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4868 | // If rhs is not a number and lhs is a Smi then strict equality cannot |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4869 | // succeed. Return non-equal (r0 is already not zero) |
| 4870 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4871 | } else { |
| 4872 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4873 | // the runtime. |
| 4874 | __ b(ne, slow); |
| 4875 | } |
| 4876 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4877 | // Lhs (r1) is a smi, rhs (r0) is a number. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4878 | if (CpuFeatures::IsSupported(VFP3)) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4879 | // Convert lhs to a double in d7 . |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4880 | CpuFeatures::Scope scope(VFP3); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4881 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 4882 | __ vmov(s15, r7); |
| 4883 | __ vcvt(d7, s15); |
| 4884 | // Load the double from rhs, tagged HeapNumber r0, to d6. |
| 4885 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 4886 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4887 | } else { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4888 | __ push(lr); |
| 4889 | // Convert lhs to a double in r2, r3. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4890 | __ mov(r7, Operand(r1)); |
| 4891 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4892 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4893 | // Load rhs to a double in r0, r1. |
| 4894 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4895 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4896 | __ pop(lr); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4899 | // 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] | 4900 | // since it's a smi. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4901 | __ jmp(lhs_not_nan); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4902 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4903 | __ bind(&rhs_is_smi); |
| 4904 | // Rhs is a smi. Check whether the non-smi lhs is a heap number. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4905 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4906 | if (strict) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4907 | // 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] | 4908 | // succeed. Return non-equal. |
| 4909 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 4910 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4911 | } else { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4912 | // Smi compared non-strictly with a non-smi non-heap-number. Call |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4913 | // the runtime. |
| 4914 | __ b(ne, slow); |
| 4915 | } |
| 4916 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4917 | // Rhs (r0) is a smi, lhs (r1) is a heap number. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4918 | if (CpuFeatures::IsSupported(VFP3)) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4919 | // Convert rhs to a double in d6 . |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4920 | CpuFeatures::Scope scope(VFP3); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4921 | // Load the double from lhs, tagged HeapNumber r1, to d7. |
| 4922 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 4923 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 4924 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 4925 | __ vmov(s13, r7); |
| 4926 | __ vcvt(d6, s13); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4927 | } else { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4928 | __ push(lr); |
| 4929 | // Load lhs to a double in r2, r3. |
| 4930 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4931 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4932 | // Convert rhs to a double in r0, r1. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4933 | __ mov(r7, Operand(r0)); |
| 4934 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4935 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4936 | __ pop(lr); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4937 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4938 | // Fall through to both_loaded_as_doubles. |
| 4939 | } |
| 4940 | |
| 4941 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4942 | void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4943 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4944 | Register rhs_exponent = exp_first ? r0 : r1; |
| 4945 | Register lhs_exponent = exp_first ? r2 : r3; |
| 4946 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 4947 | Register lhs_mantissa = exp_first ? r3 : r2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4948 | Label one_is_nan, neither_is_nan; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4949 | Label lhs_not_nan_exp_mask_is_loaded; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4950 | |
| 4951 | Register exp_mask_reg = r5; |
| 4952 | |
| 4953 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4954 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 4955 | __ cmp(r4, Operand(exp_mask_reg)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4956 | __ b(ne, &lhs_not_nan_exp_mask_is_loaded); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4957 | __ mov(r4, |
| 4958 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4959 | SetCC); |
| 4960 | __ b(ne, &one_is_nan); |
| 4961 | __ cmp(lhs_mantissa, Operand(0)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4962 | __ b(ne, &one_is_nan); |
| 4963 | |
| 4964 | __ bind(lhs_not_nan); |
| 4965 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4966 | __ bind(&lhs_not_nan_exp_mask_is_loaded); |
| 4967 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 4968 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4969 | __ b(ne, &neither_is_nan); |
| 4970 | __ mov(r4, |
| 4971 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4972 | SetCC); |
| 4973 | __ b(ne, &one_is_nan); |
| 4974 | __ cmp(rhs_mantissa, Operand(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4975 | __ b(eq, &neither_is_nan); |
| 4976 | |
| 4977 | __ bind(&one_is_nan); |
| 4978 | // NaN comparisons always fail. |
| 4979 | // Load whatever we need in r0 to make the comparison fail. |
| 4980 | if (cc == lt || cc == le) { |
| 4981 | __ mov(r0, Operand(GREATER)); |
| 4982 | } else { |
| 4983 | __ mov(r0, Operand(LESS)); |
| 4984 | } |
| 4985 | __ mov(pc, Operand(lr)); // Return. |
| 4986 | |
| 4987 | __ bind(&neither_is_nan); |
| 4988 | } |
| 4989 | |
| 4990 | |
| 4991 | // See comment at call site. |
| 4992 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 4993 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4994 | Register rhs_exponent = exp_first ? r0 : r1; |
| 4995 | Register lhs_exponent = exp_first ? r2 : r3; |
| 4996 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 4997 | Register lhs_mantissa = exp_first ? r3 : r2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4998 | |
| 4999 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 5000 | if (cc == eq) { |
| 5001 | // Doubles are not equal unless they have the same bit pattern. |
| 5002 | // Exception: 0 and -0. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5003 | __ cmp(rhs_mantissa, Operand(lhs_mantissa)); |
| 5004 | __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5005 | // Return non-zero if the numbers are unequal. |
| 5006 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 5007 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5008 | __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5009 | // If exponents are equal then return 0. |
| 5010 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 5011 | |
| 5012 | // Exponents are unequal. The only way we can return that the numbers |
| 5013 | // are equal is if one is -0 and the other is 0. We already dealt |
| 5014 | // with the case where both are -0 or both are 0. |
| 5015 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 5016 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 5017 | // equal. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5018 | __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5019 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 5020 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 5021 | // Now they are equal if and only if the lhs exponent is zero in its |
| 5022 | // low 31 bits. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5023 | __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5024 | __ mov(pc, Operand(lr)); |
| 5025 | } else { |
| 5026 | // Call a native function to do a comparison between two non-NaNs. |
| 5027 | // Call C routine that may not cause GC or other trouble. |
| 5028 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 5029 | __ Jump(r5); // Tail call. |
| 5030 | } |
| 5031 | } |
| 5032 | |
| 5033 | |
| 5034 | // See comment at call site. |
| 5035 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 5036 | // If either operand is a JSObject or an oddball value, then they are |
| 5037 | // not equal since their pointers are different. |
| 5038 | // There is no test for undetectability in strict equality. |
| 5039 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 5040 | Label first_non_object; |
| 5041 | // Get the type of the first operand into r2 and compare it with |
| 5042 | // FIRST_JS_OBJECT_TYPE. |
| 5043 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 5044 | __ b(lt, &first_non_object); |
| 5045 | |
| 5046 | // Return non-zero (r0 is not zero) |
| 5047 | Label return_not_equal; |
| 5048 | __ bind(&return_not_equal); |
| 5049 | __ mov(pc, Operand(lr)); // Return. |
| 5050 | |
| 5051 | __ bind(&first_non_object); |
| 5052 | // Check for oddballs: true, false, null, undefined. |
| 5053 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 5054 | __ b(eq, &return_not_equal); |
| 5055 | |
| 5056 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 5057 | __ b(ge, &return_not_equal); |
| 5058 | |
| 5059 | // Check for oddballs: true, false, null, undefined. |
| 5060 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 5061 | __ b(eq, &return_not_equal); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5062 | |
| 5063 | // Now that we have the types we might as well check for symbol-symbol. |
| 5064 | // Ensure that no non-strings have the symbol bit set. |
| 5065 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5066 | ASSERT(kSymbolTag != 0); |
| 5067 | __ and_(r2, r2, Operand(r3)); |
| 5068 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5069 | __ b(ne, &return_not_equal); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | |
| 5073 | // See comment at call site. |
| 5074 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 5075 | Label* both_loaded_as_doubles, |
| 5076 | Label* not_heap_numbers, |
| 5077 | Label* slow) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5078 | __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5079 | __ b(ne, not_heap_numbers); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5080 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5081 | __ cmp(r2, r3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5082 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 5083 | |
| 5084 | // Both are heap numbers. Load them up then jump to the code we have |
| 5085 | // for that. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5086 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5087 | CpuFeatures::Scope scope(VFP3); |
| 5088 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5089 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5090 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5091 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5092 | } else { |
| 5093 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5094 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5095 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5096 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5097 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5098 | __ jmp(both_loaded_as_doubles); |
| 5099 | } |
| 5100 | |
| 5101 | |
| 5102 | // Fast negative check for symbol-to-symbol equality. |
| 5103 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 5104 | // r2 is object type of r0. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5105 | // Ensure that no non-strings have the symbol bit set. |
| 5106 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5107 | ASSERT(kSymbolTag != 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5108 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5109 | __ b(eq, slow); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5110 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5111 | __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5112 | __ tst(r3, Operand(kIsSymbolMask)); |
| 5113 | __ b(eq, slow); |
| 5114 | |
| 5115 | // Both are symbols. We already checked they weren't the same pointer |
| 5116 | // so they are not equal. |
| 5117 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 5118 | __ mov(pc, Operand(lr)); // Return. |
| 5119 | } |
| 5120 | |
| 5121 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5122 | // On entry r0 (rhs) and r1 (lhs) are the values to be compared. |
| 5123 | // On exit r0 is 0, positive or negative to indicate the result of |
| 5124 | // the comparison. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5125 | void CompareStub::Generate(MacroAssembler* masm) { |
| 5126 | Label slow; // Call builtin. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5127 | Label not_smis, both_loaded_as_doubles, lhs_not_nan; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5128 | |
| 5129 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 5130 | // it is certain that at least one operand isn't a smi. |
| 5131 | |
| 5132 | // Handle the case where the objects are identical. Either returns the answer |
| 5133 | // 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] | 5134 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5135 | |
| 5136 | // If either is a Smi (we know that not both are), then they can only |
| 5137 | // be strictly equal if the other is a HeapNumber. |
| 5138 | ASSERT_EQ(0, kSmiTag); |
| 5139 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 5140 | __ and_(r2, r0, Operand(r1)); |
| 5141 | __ tst(r2, Operand(kSmiTagMask)); |
| 5142 | __ b(ne, ¬_smis); |
| 5143 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 5144 | // 1) Return the answer. |
| 5145 | // 2) Go to slow. |
| 5146 | // 3) Fall through to both_loaded_as_doubles. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5147 | // 4) Jump to lhs_not_nan. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5148 | // 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] | 5149 | // comparison. If VFP3 is supported the double values of the numbers have |
| 5150 | // been loaded into d7 and d6. Otherwise, the double values have been loaded |
| 5151 | // into r0, r1, r2, and r3. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5152 | EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5153 | |
| 5154 | __ bind(&both_loaded_as_doubles); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5155 | // The arguments have been converted to doubles and stored in d6 and d7, if |
| 5156 | // VFP3 is supported, or in r0, r1, r2, and r3. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5157 | if (CpuFeatures::IsSupported(VFP3)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5158 | __ bind(&lhs_not_nan); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5159 | CpuFeatures::Scope scope(VFP3); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5160 | Label no_nan; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5161 | // ARMv7 VFP3 instructions to implement double precision comparison. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5162 | __ vcmp(d7, d6); |
| 5163 | __ vmrs(pc); // Move vector status bits to normal status bits. |
| 5164 | Label nan; |
| 5165 | __ b(vs, &nan); |
| 5166 | __ mov(r0, Operand(EQUAL), LeaveCC, eq); |
| 5167 | __ mov(r0, Operand(LESS), LeaveCC, lt); |
| 5168 | __ mov(r0, Operand(GREATER), LeaveCC, gt); |
| 5169 | __ mov(pc, Operand(lr)); |
| 5170 | |
| 5171 | __ bind(&nan); |
| 5172 | // If one of the sides was a NaN then the v flag is set. Load r0 with |
| 5173 | // whatever it takes to make the comparison fail, since comparisons with NaN |
| 5174 | // always fail. |
| 5175 | if (cc_ == lt || cc_ == le) { |
| 5176 | __ mov(r0, Operand(GREATER)); |
| 5177 | } else { |
| 5178 | __ mov(r0, Operand(LESS)); |
| 5179 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5180 | __ mov(pc, Operand(lr)); |
| 5181 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5182 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 5183 | // fall through if neither is a NaN. Also binds lhs_not_nan. |
| 5184 | EmitNanCheck(masm, &lhs_not_nan, cc_); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5185 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 5186 | // answer. Never falls through. |
| 5187 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 5188 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5189 | |
| 5190 | __ bind(¬_smis); |
| 5191 | // At this point we know we are dealing with two different objects, |
| 5192 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 5193 | if (strict_) { |
| 5194 | // This returns non-equal for some object types, or falls through if it |
| 5195 | // was not lucky. |
| 5196 | EmitStrictTwoHeapObjectCompare(masm); |
| 5197 | } |
| 5198 | |
| 5199 | Label check_for_symbols; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5200 | Label flat_string_check; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5201 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 5202 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 5203 | // that case. If the inputs are not doubles then jumps to check_for_symbols. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5204 | // In this case r2 will contain the type of r0. Never falls through. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5205 | EmitCheckForTwoHeapNumbers(masm, |
| 5206 | &both_loaded_as_doubles, |
| 5207 | &check_for_symbols, |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5208 | &flat_string_check); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5209 | |
| 5210 | __ bind(&check_for_symbols); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5211 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 5212 | // symbols. |
| 5213 | if (cc_ == eq && !strict_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5214 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5215 | // of r0 on entry. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5216 | EmitCheckForSymbols(masm, &flat_string_check); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5219 | // Check for both being sequential ASCII strings, and inline if that is the |
| 5220 | // case. |
| 5221 | __ bind(&flat_string_check); |
| 5222 | |
| 5223 | __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow); |
| 5224 | |
| 5225 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 5226 | StringCompareStub::GenerateCompareFlatAsciiStrings(masm, |
| 5227 | r1, |
| 5228 | r0, |
| 5229 | r2, |
| 5230 | r3, |
| 5231 | r4, |
| 5232 | r5); |
| 5233 | // Never falls through to here. |
| 5234 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5235 | __ bind(&slow); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5236 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5237 | __ push(r1); |
| 5238 | __ push(r0); |
| 5239 | // Figure out which native to call and setup the arguments. |
| 5240 | Builtins::JavaScript native; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5241 | if (cc_ == eq) { |
| 5242 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 5243 | } else { |
| 5244 | native = Builtins::COMPARE; |
| 5245 | int ncr; // NaN compare result |
| 5246 | if (cc_ == lt || cc_ == le) { |
| 5247 | ncr = GREATER; |
| 5248 | } else { |
| 5249 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 5250 | ncr = LESS; |
| 5251 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5252 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 5253 | __ push(r0); |
| 5254 | } |
| 5255 | |
| 5256 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 5257 | // tagged as a small integer. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5258 | __ InvokeBuiltin(native, JUMP_JS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | |
| 5262 | // Allocates a heap number or jumps to the label if the young space is full and |
| 5263 | // a scavenge is needed. |
| 5264 | static void AllocateHeapNumber( |
| 5265 | MacroAssembler* masm, |
| 5266 | Label* need_gc, // Jump here if young space is full. |
| 5267 | Register result, // The tagged address of the new heap number. |
| 5268 | Register scratch1, // A scratch register. |
| 5269 | Register scratch2) { // Another scratch register. |
| 5270 | // Allocate an object in the heap for the heap number and tag it as a heap |
| 5271 | // object. |
| 5272 | __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize, |
| 5273 | result, |
| 5274 | scratch1, |
| 5275 | scratch2, |
| 5276 | need_gc, |
| 5277 | TAG_OBJECT); |
| 5278 | |
| 5279 | // Get heap number map and store it in the allocated object. |
| 5280 | __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex); |
| 5281 | __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset)); |
| 5282 | } |
| 5283 | |
| 5284 | |
| 5285 | // We fall into this code if the operands were Smis, but the result was |
| 5286 | // not (eg. overflow). We branch into this code (to the not_smi label) if |
| 5287 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 5288 | // to call the C-implemented binary fp operation routines we need to end up |
| 5289 | // with the double precision floating point operands in r0 and r1 (for the |
| 5290 | // value in r1) and r2 and r3 (for the value in r0). |
| 5291 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 5292 | Label* not_smi, |
| 5293 | const Builtins::JavaScript& builtin, |
| 5294 | Token::Value operation, |
| 5295 | OverwriteMode mode) { |
| 5296 | Label slow, slow_pop_2_first, do_the_call; |
| 5297 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 5298 | // Smi-smi case (overflow). |
| 5299 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 5300 | // The new heap number is in r5. r6 and r7 are scratch. |
| 5301 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5302 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5303 | // If we have floating point hardware, inline ADD, SUB, MUL, and DIV, |
| 5304 | // using registers d7 and d6 for the double values. |
| 5305 | bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && |
| 5306 | Token::MOD != operation; |
| 5307 | if (use_fp_registers) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5308 | CpuFeatures::Scope scope(VFP3); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5309 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5310 | __ vmov(s15, r7); |
| 5311 | __ vcvt(d7, s15); |
| 5312 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5313 | __ vmov(s13, r7); |
| 5314 | __ vcvt(d6, s13); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5315 | } else { |
| 5316 | // Write Smi from r0 to r3 and r2 in double format. r6 is scratch. |
| 5317 | __ mov(r7, Operand(r0)); |
| 5318 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5319 | __ push(lr); |
| 5320 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 5321 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 5322 | __ mov(r7, Operand(r1)); |
| 5323 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5324 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 5325 | __ pop(lr); |
| 5326 | } |
| 5327 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5328 | __ jmp(&do_the_call); // Tail call. No return. |
| 5329 | |
| 5330 | // We jump to here if something goes wrong (one param is not a number of any |
| 5331 | // sort or new-space allocation fails). |
| 5332 | __ bind(&slow); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5333 | |
| 5334 | // Push arguments to the stack |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5335 | __ push(r1); |
| 5336 | __ push(r0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5337 | |
| 5338 | if (Token::ADD == operation) { |
| 5339 | // Test for string arguments before calling runtime. |
| 5340 | // r1 : first argument |
| 5341 | // r0 : second argument |
| 5342 | // sp[0] : second argument |
| 5343 | // sp[1] : first argument |
| 5344 | |
| 5345 | Label not_strings, not_string1, string1; |
| 5346 | __ tst(r1, Operand(kSmiTagMask)); |
| 5347 | __ b(eq, ¬_string1); |
| 5348 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 5349 | __ b(ge, ¬_string1); |
| 5350 | |
| 5351 | // First argument is a a string, test second. |
| 5352 | __ tst(r0, Operand(kSmiTagMask)); |
| 5353 | __ b(eq, &string1); |
| 5354 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5355 | __ b(ge, &string1); |
| 5356 | |
| 5357 | // First and second argument are strings. |
| 5358 | __ TailCallRuntime(ExternalReference(Runtime::kStringAdd), 2, 1); |
| 5359 | |
| 5360 | // Only first argument is a string. |
| 5361 | __ bind(&string1); |
| 5362 | __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS); |
| 5363 | |
| 5364 | // First argument was not a string, test second. |
| 5365 | __ bind(¬_string1); |
| 5366 | __ tst(r0, Operand(kSmiTagMask)); |
| 5367 | __ b(eq, ¬_strings); |
| 5368 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5369 | __ b(ge, ¬_strings); |
| 5370 | |
| 5371 | // Only second argument is a string. |
| 5372 | __ b(¬_strings); |
| 5373 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 5374 | |
| 5375 | __ bind(¬_strings); |
| 5376 | } |
| 5377 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5378 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
| 5379 | |
| 5380 | // We branch here if at least one of r0 and r1 is not a Smi. |
| 5381 | __ bind(not_smi); |
| 5382 | if (mode == NO_OVERWRITE) { |
| 5383 | // In the case where there is no chance of an overwritable float we may as |
| 5384 | // well do the allocation immediately while r0 and r1 are untouched. |
| 5385 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5386 | } |
| 5387 | |
| 5388 | // Move r0 to a double in r2-r3. |
| 5389 | __ tst(r0, Operand(kSmiTagMask)); |
| 5390 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5391 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5392 | __ b(ne, &slow); |
| 5393 | if (mode == OVERWRITE_RIGHT) { |
| 5394 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5395 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5396 | if (use_fp_registers) { |
| 5397 | CpuFeatures::Scope scope(VFP3); |
| 5398 | // Load the double from tagged HeapNumber r0 to d7. |
| 5399 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5400 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5401 | } else { |
| 5402 | // Calling convention says that second double is in r2 and r3. |
| 5403 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5404 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5405 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5406 | __ jmp(&finished_loading_r0); |
| 5407 | __ bind(&r0_is_smi); |
| 5408 | if (mode == OVERWRITE_RIGHT) { |
| 5409 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5410 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5411 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5412 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5413 | if (use_fp_registers) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5414 | CpuFeatures::Scope scope(VFP3); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5415 | // Convert smi in r0 to double in d7. |
| 5416 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5417 | __ vmov(s15, r7); |
| 5418 | __ vcvt(d7, s15); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5419 | } else { |
| 5420 | // Write Smi from r0 to r3 and r2 in double format. |
| 5421 | __ mov(r7, Operand(r0)); |
| 5422 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5423 | __ push(lr); |
| 5424 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5425 | __ pop(lr); |
| 5426 | } |
| 5427 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5428 | __ bind(&finished_loading_r0); |
| 5429 | |
| 5430 | // Move r1 to a double in r0-r1. |
| 5431 | __ tst(r1, Operand(kSmiTagMask)); |
| 5432 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5433 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5434 | __ b(ne, &slow); |
| 5435 | if (mode == OVERWRITE_LEFT) { |
| 5436 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
| 5437 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5438 | if (use_fp_registers) { |
| 5439 | CpuFeatures::Scope scope(VFP3); |
| 5440 | // Load the double from tagged HeapNumber r1 to d6. |
| 5441 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5442 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5443 | } else { |
| 5444 | // Calling convention says that first double is in r0 and r1. |
| 5445 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5446 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5447 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5448 | __ jmp(&finished_loading_r1); |
| 5449 | __ bind(&r1_is_smi); |
| 5450 | if (mode == OVERWRITE_LEFT) { |
| 5451 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5452 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5453 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5454 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5455 | if (use_fp_registers) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5456 | CpuFeatures::Scope scope(VFP3); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5457 | // Convert smi in r1 to double in d6. |
| 5458 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5459 | __ vmov(s13, r7); |
| 5460 | __ vcvt(d6, s13); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5461 | } else { |
| 5462 | // Write Smi from r1 to r1 and r0 in double format. |
| 5463 | __ mov(r7, Operand(r1)); |
| 5464 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5465 | __ push(lr); |
| 5466 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5467 | __ pop(lr); |
| 5468 | } |
| 5469 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5470 | __ bind(&finished_loading_r1); |
| 5471 | |
| 5472 | __ bind(&do_the_call); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5473 | // If we are inlining the operation using VFP3 instructions for |
| 5474 | // add, subtract, multiply, or divide, the arguments are in d6 and d7. |
| 5475 | if (use_fp_registers) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5476 | CpuFeatures::Scope scope(VFP3); |
| 5477 | // ARMv7 VFP3 instructions to implement |
| 5478 | // double precision, add, subtract, multiply, divide. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5479 | |
| 5480 | if (Token::MUL == operation) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5481 | __ vmul(d5, d6, d7); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5482 | } else if (Token::DIV == operation) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5483 | __ vdiv(d5, d6, d7); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5484 | } else if (Token::ADD == operation) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5485 | __ vadd(d5, d6, d7); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5486 | } else if (Token::SUB == operation) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5487 | __ vsub(d5, d6, d7); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5488 | } else { |
| 5489 | UNREACHABLE(); |
| 5490 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5491 | __ sub(r0, r5, Operand(kHeapObjectTag)); |
| 5492 | __ vstr(d5, r0, HeapNumber::kValueOffset); |
| 5493 | __ add(r0, r0, Operand(kHeapObjectTag)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5494 | __ mov(pc, lr); |
| 5495 | return; |
| 5496 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5497 | |
| 5498 | // If we did not inline the operation, then the arguments are in: |
| 5499 | // r0: Left value (least significant part of mantissa). |
| 5500 | // r1: Left value (sign, exponent, top of mantissa). |
| 5501 | // r2: Right value (least significant part of mantissa). |
| 5502 | // r3: Right value (sign, exponent, top of mantissa). |
| 5503 | // r5: Address of heap number for result. |
| 5504 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5505 | __ push(lr); // For later. |
| 5506 | __ push(r5); // Address of heap number that is answer. |
| 5507 | __ AlignStack(0); |
| 5508 | // Call C routine that may not cause GC or other trouble. |
| 5509 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
| 5510 | __ Call(r5); |
| 5511 | __ pop(r4); // Address of heap number. |
| 5512 | __ cmp(r4, Operand(Smi::FromInt(0))); |
| 5513 | __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push. |
| 5514 | // Store answer in the overwritable heap number. |
| 5515 | #if !defined(USE_ARM_EABI) |
| 5516 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5517 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5518 | // substract the tag from r4. |
| 5519 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5520 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5521 | #else |
| 5522 | // Double returned in registers 0 and 1. |
| 5523 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
| 5524 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
| 5525 | #endif |
| 5526 | __ mov(r0, Operand(r4)); |
| 5527 | // And we are done. |
| 5528 | __ pop(pc); |
| 5529 | } |
| 5530 | |
| 5531 | |
| 5532 | // Tries to get a signed int32 out of a double precision floating point heap |
| 5533 | // number. Rounds towards 0. Fastest for doubles that are in the ranges |
| 5534 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5535 | // almost to the range of signed int32 values that are not Smis. Jumps to the |
| 5536 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5537 | // (excluding the endpoints). |
| 5538 | static void GetInt32(MacroAssembler* masm, |
| 5539 | Register source, |
| 5540 | Register dest, |
| 5541 | Register scratch, |
| 5542 | Register scratch2, |
| 5543 | Label* slow) { |
| 5544 | Label right_exponent, done; |
| 5545 | // Get exponent word. |
| 5546 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5547 | // Get exponent alone in scratch2. |
| 5548 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
| 5549 | // Load dest with zero. We use this either for the final shift or |
| 5550 | // for the answer. |
| 5551 | __ mov(dest, Operand(0)); |
| 5552 | // Check whether the exponent matches a 32 bit signed int that is not a Smi. |
| 5553 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5554 | // the exponent that we are fastest at and also the highest exponent we can |
| 5555 | // handle here. |
| 5556 | const uint32_t non_smi_exponent = |
| 5557 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5558 | __ cmp(scratch2, Operand(non_smi_exponent)); |
| 5559 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5560 | __ b(eq, &right_exponent); |
| 5561 | // If the exponent is higher than that then go to slow case. This catches |
| 5562 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5563 | __ b(gt, slow); |
| 5564 | |
| 5565 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5566 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5567 | // it rounds to zero. |
| 5568 | const uint32_t zero_exponent = |
| 5569 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5570 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5571 | // Dest already has a Smi zero. |
| 5572 | __ b(lt, &done); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5573 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 5574 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5575 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5576 | // We now have the exponent in dest. Subtract from 30 to get |
| 5577 | // how much to shift down. |
| 5578 | __ rsb(dest, dest, Operand(30)); |
| 5579 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5580 | __ bind(&right_exponent); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5581 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5582 | CpuFeatures::Scope scope(VFP3); |
| 5583 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 5584 | // conversion using round to zero. |
| 5585 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5586 | __ vmov(d7, scratch2, scratch); |
| 5587 | __ vcvt(s15, d7); |
| 5588 | __ vmov(dest, s15); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5589 | } else { |
| 5590 | // Get the top bits of the mantissa. |
| 5591 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5592 | // Put back the implicit 1. |
| 5593 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5594 | // Shift up the mantissa bits to take up the space the exponent used to |
| 5595 | // take. We just orred in the implicit bit so that took care of one and |
| 5596 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 5597 | // distance. |
| 5598 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5599 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5600 | // Put sign in zero flag. |
| 5601 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 5602 | // Get the second half of the double. For some exponents we don't |
| 5603 | // actually need this because the bits get shifted out again, but |
| 5604 | // it's probably slower to test than just to do it. |
| 5605 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5606 | // Shift down 22 bits to get the last 10 bits. |
| 5607 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5608 | // Move down according to the exponent. |
| 5609 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 5610 | // Fix sign if sign bit was set. |
| 5611 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 5612 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5613 | __ bind(&done); |
| 5614 | } |
| 5615 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5616 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5617 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5618 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5619 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5620 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5621 | // write the answer into (either by allocating or by overwriting). |
| 5622 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5623 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5624 | Label slow, result_not_a_smi; |
| 5625 | Label r0_is_smi, r1_is_smi; |
| 5626 | Label done_checking_r0, done_checking_r1; |
| 5627 | |
| 5628 | __ tst(r1, Operand(kSmiTagMask)); |
| 5629 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5630 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5631 | __ b(ne, &slow); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5632 | GetInt32(masm, r1, r3, r5, r4, &slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5633 | __ jmp(&done_checking_r1); |
| 5634 | __ bind(&r1_is_smi); |
| 5635 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5636 | __ bind(&done_checking_r1); |
| 5637 | |
| 5638 | __ tst(r0, Operand(kSmiTagMask)); |
| 5639 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5640 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5641 | __ b(ne, &slow); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5642 | GetInt32(masm, r0, r2, r5, r4, &slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5643 | __ jmp(&done_checking_r0); |
| 5644 | __ bind(&r0_is_smi); |
| 5645 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5646 | __ bind(&done_checking_r0); |
| 5647 | |
| 5648 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5649 | // r2 and r3: Signed int32 operands. |
| 5650 | switch (op_) { |
| 5651 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5652 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5653 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5654 | case Token::SAR: |
| 5655 | // Use only the 5 least significant bits of the shift count. |
| 5656 | __ and_(r2, r2, Operand(0x1f)); |
| 5657 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5658 | break; |
| 5659 | case Token::SHR: |
| 5660 | // Use only the 5 least significant bits of the shift count. |
| 5661 | __ and_(r2, r2, Operand(0x1f)); |
| 5662 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5663 | // SHR is special because it is required to produce a positive answer. |
| 5664 | // The code below for writing into heap numbers isn't capable of writing |
| 5665 | // the register as an unsigned int so we go to slow case if we hit this |
| 5666 | // case. |
| 5667 | __ b(mi, &slow); |
| 5668 | break; |
| 5669 | case Token::SHL: |
| 5670 | // Use only the 5 least significant bits of the shift count. |
| 5671 | __ and_(r2, r2, Operand(0x1f)); |
| 5672 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5673 | break; |
| 5674 | default: UNREACHABLE(); |
| 5675 | } |
| 5676 | // check that the *signed* result fits in a smi |
| 5677 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5678 | __ b(mi, &result_not_a_smi); |
| 5679 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5680 | __ Ret(); |
| 5681 | |
| 5682 | Label have_to_allocate, got_a_heap_number; |
| 5683 | __ bind(&result_not_a_smi); |
| 5684 | switch (mode_) { |
| 5685 | case OVERWRITE_RIGHT: { |
| 5686 | __ tst(r0, Operand(kSmiTagMask)); |
| 5687 | __ b(eq, &have_to_allocate); |
| 5688 | __ mov(r5, Operand(r0)); |
| 5689 | break; |
| 5690 | } |
| 5691 | case OVERWRITE_LEFT: { |
| 5692 | __ tst(r1, Operand(kSmiTagMask)); |
| 5693 | __ b(eq, &have_to_allocate); |
| 5694 | __ mov(r5, Operand(r1)); |
| 5695 | break; |
| 5696 | } |
| 5697 | case NO_OVERWRITE: { |
| 5698 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5699 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5700 | } |
| 5701 | default: break; |
| 5702 | } |
| 5703 | __ bind(&got_a_heap_number); |
| 5704 | // r2: Answer as signed int32. |
| 5705 | // r5: Heap number to write answer into. |
| 5706 | |
| 5707 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5708 | // result. |
| 5709 | __ mov(r0, Operand(r5)); |
| 5710 | |
| 5711 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5712 | // r3 as scratch. r0 is preserved and returned. |
| 5713 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5714 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5715 | |
| 5716 | if (mode_ != NO_OVERWRITE) { |
| 5717 | __ bind(&have_to_allocate); |
| 5718 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5719 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5720 | __ jmp(&got_a_heap_number); |
| 5721 | } |
| 5722 | |
| 5723 | // If all else failed then we go to the runtime system. |
| 5724 | __ bind(&slow); |
| 5725 | __ push(r1); // restore stack |
| 5726 | __ push(r0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5727 | switch (op_) { |
| 5728 | case Token::BIT_OR: |
| 5729 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5730 | break; |
| 5731 | case Token::BIT_AND: |
| 5732 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5733 | break; |
| 5734 | case Token::BIT_XOR: |
| 5735 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5736 | break; |
| 5737 | case Token::SAR: |
| 5738 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5739 | break; |
| 5740 | case Token::SHR: |
| 5741 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5742 | break; |
| 5743 | case Token::SHL: |
| 5744 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5745 | break; |
| 5746 | default: |
| 5747 | UNREACHABLE(); |
| 5748 | } |
| 5749 | } |
| 5750 | |
| 5751 | |
| 5752 | // Can we multiply by x with max two shifts and an add. |
| 5753 | // This answers yes to all integers from 2 to 10. |
| 5754 | static bool IsEasyToMultiplyBy(int x) { |
| 5755 | if (x < 2) return false; // Avoid special cases. |
| 5756 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 5757 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 5758 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 5759 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 5760 | return false; |
| 5761 | } |
| 5762 | |
| 5763 | |
| 5764 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 5765 | // Source and destination may be the same register. This routine does |
| 5766 | // not set carry and overflow the way a mul instruction would. |
| 5767 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 5768 | Register source, |
| 5769 | Register destination, |
| 5770 | int known_int) { |
| 5771 | if (IsPowerOf2(known_int)) { |
| 5772 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 5773 | } else if (PopCountLessThanEqual2(known_int)) { |
| 5774 | int first_bit = BitPosition(known_int); |
| 5775 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 5776 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 5777 | if (first_bit != 0) { |
| 5778 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 5779 | } |
| 5780 | } else { |
| 5781 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 5782 | int the_bit = BitPosition(known_int + 1); |
| 5783 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 5784 | } |
| 5785 | } |
| 5786 | |
| 5787 | |
| 5788 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 5789 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 5790 | // a result that needs shifting. |
| 5791 | static void MultiplyByKnownInt2( |
| 5792 | MacroAssembler* masm, |
| 5793 | Register result, |
| 5794 | Register source, |
| 5795 | Register known_int_register, // Smi tagged. |
| 5796 | int known_int, |
| 5797 | int* required_shift) { // Including Smi tag shift |
| 5798 | switch (known_int) { |
| 5799 | case 3: |
| 5800 | __ add(result, source, Operand(source, LSL, 1)); |
| 5801 | *required_shift = 1; |
| 5802 | break; |
| 5803 | case 5: |
| 5804 | __ add(result, source, Operand(source, LSL, 2)); |
| 5805 | *required_shift = 1; |
| 5806 | break; |
| 5807 | case 6: |
| 5808 | __ add(result, source, Operand(source, LSL, 1)); |
| 5809 | *required_shift = 2; |
| 5810 | break; |
| 5811 | case 7: |
| 5812 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 5813 | *required_shift = 1; |
| 5814 | break; |
| 5815 | case 9: |
| 5816 | __ add(result, source, Operand(source, LSL, 3)); |
| 5817 | *required_shift = 1; |
| 5818 | break; |
| 5819 | case 10: |
| 5820 | __ add(result, source, Operand(source, LSL, 2)); |
| 5821 | *required_shift = 2; |
| 5822 | break; |
| 5823 | default: |
| 5824 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 5825 | __ mul(result, source, known_int_register); |
| 5826 | *required_shift = 0; |
| 5827 | } |
| 5828 | } |
| 5829 | |
| 5830 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5831 | const char* GenericBinaryOpStub::GetName() { |
| 5832 | if (name_ != NULL) return name_; |
| 5833 | const int len = 100; |
| 5834 | name_ = Bootstrapper::AllocateAutoDeletedArray(len); |
| 5835 | if (name_ == NULL) return "OOM"; |
| 5836 | const char* op_name = Token::Name(op_); |
| 5837 | const char* overwrite_name; |
| 5838 | switch (mode_) { |
| 5839 | case NO_OVERWRITE: overwrite_name = "Alloc"; break; |
| 5840 | case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; |
| 5841 | case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; |
| 5842 | default: overwrite_name = "UnknownOverwrite"; break; |
| 5843 | } |
| 5844 | |
| 5845 | OS::SNPrintF(Vector<char>(name_, len), |
| 5846 | "GenericBinaryOpStub_%s_%s%s", |
| 5847 | op_name, |
| 5848 | overwrite_name, |
| 5849 | specialized_on_rhs_ ? "_ConstantRhs" : 0); |
| 5850 | return name_; |
| 5851 | } |
| 5852 | |
| 5853 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5854 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5855 | // r1 : x |
| 5856 | // r0 : y |
| 5857 | // result : r0 |
| 5858 | |
| 5859 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 5860 | // tell us that. |
| 5861 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 5862 | |
| 5863 | switch (op_) { |
| 5864 | case Token::ADD: { |
| 5865 | Label not_smi; |
| 5866 | // Fast path. |
| 5867 | ASSERT(kSmiTag == 0); // Adjust code below. |
| 5868 | __ tst(r2, Operand(kSmiTagMask)); |
| 5869 | __ b(ne, ¬_smi); |
| 5870 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 5871 | // Return if no overflow. |
| 5872 | __ Ret(vc); |
| 5873 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 5874 | |
| 5875 | HandleBinaryOpSlowCases(masm, |
| 5876 | ¬_smi, |
| 5877 | Builtins::ADD, |
| 5878 | Token::ADD, |
| 5879 | mode_); |
| 5880 | break; |
| 5881 | } |
| 5882 | |
| 5883 | case Token::SUB: { |
| 5884 | Label not_smi; |
| 5885 | // Fast path. |
| 5886 | ASSERT(kSmiTag == 0); // Adjust code below. |
| 5887 | __ tst(r2, Operand(kSmiTagMask)); |
| 5888 | __ b(ne, ¬_smi); |
| 5889 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 5890 | // Return if no overflow. |
| 5891 | __ Ret(vc); |
| 5892 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 5893 | |
| 5894 | HandleBinaryOpSlowCases(masm, |
| 5895 | ¬_smi, |
| 5896 | Builtins::SUB, |
| 5897 | Token::SUB, |
| 5898 | mode_); |
| 5899 | break; |
| 5900 | } |
| 5901 | |
| 5902 | case Token::MUL: { |
| 5903 | Label not_smi, slow; |
| 5904 | ASSERT(kSmiTag == 0); // adjust code below |
| 5905 | __ tst(r2, Operand(kSmiTagMask)); |
| 5906 | __ b(ne, ¬_smi); |
| 5907 | // Remove tag from one operand (but keep sign), so that result is Smi. |
| 5908 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
| 5909 | // Do multiplication |
| 5910 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 5911 | // Go slow on overflows (overflow bit is not set). |
| 5912 | __ mov(ip, Operand(r3, ASR, 31)); |
| 5913 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 5914 | __ b(ne, &slow); |
| 5915 | // Go slow on zero result to handle -0. |
| 5916 | __ tst(r3, Operand(r3)); |
| 5917 | __ mov(r0, Operand(r3), LeaveCC, ne); |
| 5918 | __ Ret(ne); |
| 5919 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 5920 | // We know one of them was zero. |
| 5921 | __ add(r2, r0, Operand(r1), SetCC); |
| 5922 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 5923 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 5924 | // Slow case. We fall through here if we multiplied a negative number |
| 5925 | // with 0, because that would mean we should produce -0. |
| 5926 | __ bind(&slow); |
| 5927 | |
| 5928 | HandleBinaryOpSlowCases(masm, |
| 5929 | ¬_smi, |
| 5930 | Builtins::MUL, |
| 5931 | Token::MUL, |
| 5932 | mode_); |
| 5933 | break; |
| 5934 | } |
| 5935 | |
| 5936 | case Token::DIV: |
| 5937 | case Token::MOD: { |
| 5938 | Label not_smi; |
| 5939 | if (specialized_on_rhs_) { |
| 5940 | Label smi_is_unsuitable; |
| 5941 | __ BranchOnNotSmi(r1, ¬_smi); |
| 5942 | if (IsPowerOf2(constant_rhs_)) { |
| 5943 | if (op_ == Token::MOD) { |
| 5944 | __ and_(r0, |
| 5945 | r1, |
| 5946 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 5947 | SetCC); |
| 5948 | // We now have the answer, but if the input was negative we also |
| 5949 | // have the sign bit. Our work is done if the result is |
| 5950 | // positive or zero: |
| 5951 | __ Ret(pl); |
| 5952 | // A mod of a negative left hand side must return a negative number. |
| 5953 | // Unfortunately if the answer is 0 then we must return -0. And we |
| 5954 | // already optimistically trashed r0 so we may need to restore it. |
| 5955 | __ eor(r0, r0, Operand(0x80000000u), SetCC); |
| 5956 | // Next two instructions are conditional on the answer being -0. |
| 5957 | __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
| 5958 | __ b(eq, &smi_is_unsuitable); |
| 5959 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
| 5960 | __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_))); |
| 5961 | } else { |
| 5962 | ASSERT(op_ == Token::DIV); |
| 5963 | __ tst(r1, |
| 5964 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 5965 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 5966 | int shift = 0; |
| 5967 | int d = constant_rhs_; |
| 5968 | while ((d & 1) == 0) { |
| 5969 | d >>= 1; |
| 5970 | shift++; |
| 5971 | } |
| 5972 | __ mov(r0, Operand(r1, LSR, shift)); |
| 5973 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 5974 | } |
| 5975 | } else { |
| 5976 | // Not a power of 2. |
| 5977 | __ tst(r1, Operand(0x80000000u)); |
| 5978 | __ b(ne, &smi_is_unsuitable); |
| 5979 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 5980 | // multiplying. |
| 5981 | double divisor = 1.0 / constant_rhs_; |
| 5982 | int shift = 32; |
| 5983 | double scale = 4294967296.0; // 1 << 32. |
| 5984 | uint32_t mul; |
| 5985 | // Maximise the precision of the fixed point reciprocal. |
| 5986 | while (true) { |
| 5987 | mul = static_cast<uint32_t>(scale * divisor); |
| 5988 | if (mul >= 0x7fffffff) break; |
| 5989 | scale *= 2.0; |
| 5990 | shift++; |
| 5991 | } |
| 5992 | mul++; |
| 5993 | __ mov(r2, Operand(mul)); |
| 5994 | __ umull(r3, r2, r2, r1); |
| 5995 | __ mov(r2, Operand(r2, LSR, shift - 31)); |
| 5996 | // r2 is r1 / rhs. r2 is not Smi tagged. |
| 5997 | // r0 is still the known rhs. r0 is Smi tagged. |
| 5998 | // r1 is still the unkown lhs. r1 is Smi tagged. |
| 5999 | int required_r4_shift = 0; // Including the Smi tag shift of 1. |
| 6000 | // r4 = r2 * r0. |
| 6001 | MultiplyByKnownInt2(masm, |
| 6002 | r4, |
| 6003 | r2, |
| 6004 | r0, |
| 6005 | constant_rhs_, |
| 6006 | &required_r4_shift); |
| 6007 | // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs). |
| 6008 | if (op_ == Token::DIV) { |
| 6009 | __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC); |
| 6010 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
| 6011 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 6012 | } else { |
| 6013 | ASSERT(op_ == Token::MOD); |
| 6014 | __ sub(r0, r1, Operand(r4, LSL, required_r4_shift)); |
| 6015 | } |
| 6016 | } |
| 6017 | __ Ret(); |
| 6018 | __ bind(&smi_is_unsuitable); |
| 6019 | } else { |
| 6020 | __ jmp(¬_smi); |
| 6021 | } |
| 6022 | HandleBinaryOpSlowCases(masm, |
| 6023 | ¬_smi, |
| 6024 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV, |
| 6025 | op_, |
| 6026 | mode_); |
| 6027 | break; |
| 6028 | } |
| 6029 | |
| 6030 | case Token::BIT_OR: |
| 6031 | case Token::BIT_AND: |
| 6032 | case Token::BIT_XOR: |
| 6033 | case Token::SAR: |
| 6034 | case Token::SHR: |
| 6035 | case Token::SHL: { |
| 6036 | Label slow; |
| 6037 | ASSERT(kSmiTag == 0); // adjust code below |
| 6038 | __ tst(r2, Operand(kSmiTagMask)); |
| 6039 | __ b(ne, &slow); |
| 6040 | switch (op_) { |
| 6041 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 6042 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 6043 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
| 6044 | case Token::SAR: |
| 6045 | // Remove tags from right operand. |
| 6046 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 6047 | // Use only the 5 least significant bits of the shift count. |
| 6048 | __ and_(r2, r2, Operand(0x1f)); |
| 6049 | __ mov(r0, Operand(r1, ASR, r2)); |
| 6050 | // Smi tag result. |
| 6051 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 6052 | break; |
| 6053 | case Token::SHR: |
| 6054 | // Remove tags from operands. We can't do this on a 31 bit number |
| 6055 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 6056 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 6057 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 6058 | // Use only the 5 least significant bits of the shift count. |
| 6059 | __ and_(r2, r2, Operand(0x1f)); |
| 6060 | __ mov(r3, Operand(r3, LSR, r2)); |
| 6061 | // Unsigned shift is not allowed to produce a negative number, so |
| 6062 | // check the sign bit and the sign bit after Smi tagging. |
| 6063 | __ tst(r3, Operand(0xc0000000)); |
| 6064 | __ b(ne, &slow); |
| 6065 | // Smi tag result. |
| 6066 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
| 6067 | break; |
| 6068 | case Token::SHL: |
| 6069 | // Remove tags from operands. |
| 6070 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 6071 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 6072 | // Use only the 5 least significant bits of the shift count. |
| 6073 | __ and_(r2, r2, Operand(0x1f)); |
| 6074 | __ mov(r3, Operand(r3, LSL, r2)); |
| 6075 | // Check that the signed result fits in a Smi. |
| 6076 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 6077 | __ b(mi, &slow); |
| 6078 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
| 6079 | break; |
| 6080 | default: UNREACHABLE(); |
| 6081 | } |
| 6082 | __ Ret(); |
| 6083 | __ bind(&slow); |
| 6084 | HandleNonSmiBitwiseOp(masm); |
| 6085 | break; |
| 6086 | } |
| 6087 | |
| 6088 | default: UNREACHABLE(); |
| 6089 | } |
| 6090 | // This code should be unreachable. |
| 6091 | __ stop("Unreachable"); |
| 6092 | } |
| 6093 | |
| 6094 | |
| 6095 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 6096 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 6097 | // argument, so give it a Smi. |
| 6098 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6099 | __ push(r0); |
| 6100 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1); |
| 6101 | |
| 6102 | __ StubReturn(1); |
| 6103 | } |
| 6104 | |
| 6105 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6106 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6107 | Label slow, done; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6108 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6109 | if (op_ == Token::SUB) { |
| 6110 | // Check whether the value is a smi. |
| 6111 | Label try_float; |
| 6112 | __ tst(r0, Operand(kSmiTagMask)); |
| 6113 | __ b(ne, &try_float); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6114 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6115 | // Go slow case if the value of the expression is zero |
| 6116 | // to make sure that we switch between 0 and -0. |
| 6117 | __ cmp(r0, Operand(0)); |
| 6118 | __ b(eq, &slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6119 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6120 | // The value of the expression is a smi that is not zero. Try |
| 6121 | // optimistic subtraction '0 - value'. |
| 6122 | __ rsb(r1, r0, Operand(0), SetCC); |
| 6123 | __ b(vs, &slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6124 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6125 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 6126 | __ b(&done); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6127 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6128 | __ bind(&try_float); |
| 6129 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6130 | __ b(ne, &slow); |
| 6131 | // r0 is a heap number. Get a new heap number in r1. |
| 6132 | if (overwrite_) { |
| 6133 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6134 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6135 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6136 | } else { |
| 6137 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
| 6138 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 6139 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6140 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 6141 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6142 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 6143 | __ mov(r0, Operand(r1)); |
| 6144 | } |
| 6145 | } else if (op_ == Token::BIT_NOT) { |
| 6146 | // Check if the operand is a heap number. |
| 6147 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6148 | __ b(ne, &slow); |
| 6149 | |
| 6150 | // Convert the heap number is r0 to an untagged integer in r1. |
| 6151 | GetInt32(masm, r0, r1, r2, r3, &slow); |
| 6152 | |
| 6153 | // Do the bitwise operation (move negated) and check if the result |
| 6154 | // fits in a smi. |
| 6155 | Label try_float; |
| 6156 | __ mvn(r1, Operand(r1)); |
| 6157 | __ add(r2, r1, Operand(0x40000000), SetCC); |
| 6158 | __ b(mi, &try_float); |
| 6159 | __ mov(r0, Operand(r1, LSL, kSmiTagSize)); |
| 6160 | __ b(&done); |
| 6161 | |
| 6162 | __ bind(&try_float); |
| 6163 | if (!overwrite_) { |
| 6164 | // Allocate a fresh heap number, but don't overwrite r0 until |
| 6165 | // we're sure we can do it without going through the slow case |
| 6166 | // that needs the value in r0. |
| 6167 | AllocateHeapNumber(masm, &slow, r2, r3, r4); |
| 6168 | __ mov(r0, Operand(r2)); |
| 6169 | } |
| 6170 | |
| 6171 | // WriteInt32ToHeapNumberStub does not trigger GC, so we do not |
| 6172 | // have to set up a frame. |
| 6173 | WriteInt32ToHeapNumberStub stub(r1, r0, r2); |
| 6174 | __ push(lr); |
| 6175 | __ Call(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 6176 | __ pop(lr); |
| 6177 | } else { |
| 6178 | UNIMPLEMENTED(); |
| 6179 | } |
| 6180 | |
| 6181 | __ bind(&done); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6182 | __ StubReturn(1); |
| 6183 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6184 | // Handle the slow case by jumping to the JavaScript builtin. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6185 | __ bind(&slow); |
| 6186 | __ push(r0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6187 | switch (op_) { |
| 6188 | case Token::SUB: |
| 6189 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 6190 | break; |
| 6191 | case Token::BIT_NOT: |
| 6192 | __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS); |
| 6193 | break; |
| 6194 | default: |
| 6195 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6196 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | |
| 6200 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
| 6201 | // r0 holds the exception. |
| 6202 | |
| 6203 | // Adjust this code if not the case. |
| 6204 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6205 | |
| 6206 | // Drop the sp to the top of the handler. |
| 6207 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 6208 | __ ldr(sp, MemOperand(r3)); |
| 6209 | |
| 6210 | // Restore the next handler and frame pointer, discard handler state. |
| 6211 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 6212 | __ pop(r2); |
| 6213 | __ str(r2, MemOperand(r3)); |
| 6214 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6215 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 6216 | |
| 6217 | // Before returning we restore the context from the frame pointer if |
| 6218 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6219 | // JS entry frame. |
| 6220 | __ cmp(fp, Operand(0)); |
| 6221 | // Set cp to NULL if fp is NULL. |
| 6222 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6223 | // Restore cp otherwise. |
| 6224 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 6225 | #ifdef DEBUG |
| 6226 | if (FLAG_debug_code) { |
| 6227 | __ mov(lr, Operand(pc)); |
| 6228 | } |
| 6229 | #endif |
| 6230 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
| 6231 | __ pop(pc); |
| 6232 | } |
| 6233 | |
| 6234 | |
| 6235 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 6236 | UncatchableExceptionType type) { |
| 6237 | // Adjust this code if not the case. |
| 6238 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6239 | |
| 6240 | // Drop sp to the top stack handler. |
| 6241 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 6242 | __ ldr(sp, MemOperand(r3)); |
| 6243 | |
| 6244 | // Unwind the handlers until the ENTRY handler is found. |
| 6245 | Label loop, done; |
| 6246 | __ bind(&loop); |
| 6247 | // Load the type of the current stack handler. |
| 6248 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 6249 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
| 6250 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 6251 | __ b(eq, &done); |
| 6252 | // Fetch the next handler in the list. |
| 6253 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 6254 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
| 6255 | __ jmp(&loop); |
| 6256 | __ bind(&done); |
| 6257 | |
| 6258 | // Set the top handler address to next handler past the current ENTRY handler. |
| 6259 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 6260 | __ pop(r2); |
| 6261 | __ str(r2, MemOperand(r3)); |
| 6262 | |
| 6263 | if (type == OUT_OF_MEMORY) { |
| 6264 | // Set external caught exception to false. |
| 6265 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 6266 | __ mov(r0, Operand(false)); |
| 6267 | __ mov(r2, Operand(external_caught)); |
| 6268 | __ str(r0, MemOperand(r2)); |
| 6269 | |
| 6270 | // Set pending exception and r0 to out of memory exception. |
| 6271 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6272 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6273 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 6274 | __ str(r0, MemOperand(r2)); |
| 6275 | } |
| 6276 | |
| 6277 | // Stack layout at this point. See also StackHandlerConstants. |
| 6278 | // sp -> state (ENTRY) |
| 6279 | // fp |
| 6280 | // lr |
| 6281 | |
| 6282 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 6283 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6284 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 6285 | // Before returning we restore the context from the frame pointer if |
| 6286 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6287 | // JS entry frame. |
| 6288 | __ cmp(fp, Operand(0)); |
| 6289 | // Set cp to NULL if fp is NULL. |
| 6290 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6291 | // Restore cp otherwise. |
| 6292 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 6293 | #ifdef DEBUG |
| 6294 | if (FLAG_debug_code) { |
| 6295 | __ mov(lr, Operand(pc)); |
| 6296 | } |
| 6297 | #endif |
| 6298 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
| 6299 | __ pop(pc); |
| 6300 | } |
| 6301 | |
| 6302 | |
| 6303 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 6304 | Label* throw_normal_exception, |
| 6305 | Label* throw_termination_exception, |
| 6306 | Label* throw_out_of_memory_exception, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6307 | bool do_gc, |
| 6308 | bool always_allocate) { |
| 6309 | // r0: result parameter for PerformGC, if any |
| 6310 | // r4: number of arguments including receiver (C callee-saved) |
| 6311 | // r5: pointer to builtin function (C callee-saved) |
| 6312 | // r6: pointer to the first argument (C callee-saved) |
| 6313 | |
| 6314 | if (do_gc) { |
| 6315 | // Passing r0. |
| 6316 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 6317 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
| 6318 | } |
| 6319 | |
| 6320 | ExternalReference scope_depth = |
| 6321 | ExternalReference::heap_always_allocate_scope_depth(); |
| 6322 | if (always_allocate) { |
| 6323 | __ mov(r0, Operand(scope_depth)); |
| 6324 | __ ldr(r1, MemOperand(r0)); |
| 6325 | __ add(r1, r1, Operand(1)); |
| 6326 | __ str(r1, MemOperand(r0)); |
| 6327 | } |
| 6328 | |
| 6329 | // Call C built-in. |
| 6330 | // r0 = argc, r1 = argv |
| 6331 | __ mov(r0, Operand(r4)); |
| 6332 | __ mov(r1, Operand(r6)); |
| 6333 | |
| 6334 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 6335 | // frames, we need to know where the return address is. Right now, |
| 6336 | // we push it on the stack to be able to find it again, but we never |
| 6337 | // restore from it in case of changes, which makes it impossible to |
| 6338 | // support moving the C entry code stub. This should be fixed, but currently |
| 6339 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 6340 | // sequence that it is not moving ever. |
| 6341 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 6342 | masm->push(lr); |
| 6343 | masm->Jump(r5); |
| 6344 | |
| 6345 | if (always_allocate) { |
| 6346 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 6347 | // though (contain the result). |
| 6348 | __ mov(r2, Operand(scope_depth)); |
| 6349 | __ ldr(r3, MemOperand(r2)); |
| 6350 | __ sub(r3, r3, Operand(1)); |
| 6351 | __ str(r3, MemOperand(r2)); |
| 6352 | } |
| 6353 | |
| 6354 | // check for failure result |
| 6355 | Label failure_returned; |
| 6356 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 6357 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 6358 | __ add(r2, r0, Operand(1)); |
| 6359 | __ tst(r2, Operand(kFailureTagMask)); |
| 6360 | __ b(eq, &failure_returned); |
| 6361 | |
| 6362 | // Exit C frame and return. |
| 6363 | // r0:r1: result |
| 6364 | // sp: stack pointer |
| 6365 | // fp: frame pointer |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6366 | __ LeaveExitFrame(mode_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6367 | |
| 6368 | // check if we should retry or throw exception |
| 6369 | Label retry; |
| 6370 | __ bind(&failure_returned); |
| 6371 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 6372 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 6373 | __ b(eq, &retry); |
| 6374 | |
| 6375 | // Special handling of out of memory exceptions. |
| 6376 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6377 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6378 | __ b(eq, throw_out_of_memory_exception); |
| 6379 | |
| 6380 | // Retrieve the pending exception and clear the variable. |
| 6381 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6382 | __ ldr(r3, MemOperand(ip)); |
| 6383 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 6384 | __ ldr(r0, MemOperand(ip)); |
| 6385 | __ str(r3, MemOperand(ip)); |
| 6386 | |
| 6387 | // Special handling of termination exceptions which are uncatchable |
| 6388 | // by javascript code. |
| 6389 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 6390 | __ b(eq, throw_termination_exception); |
| 6391 | |
| 6392 | // Handle normal exception. |
| 6393 | __ jmp(throw_normal_exception); |
| 6394 | |
| 6395 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 6396 | } |
| 6397 | |
| 6398 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6399 | void CEntryStub::Generate(MacroAssembler* masm) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6400 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 6401 | // r0: number of arguments including receiver |
| 6402 | // r1: pointer to builtin function |
| 6403 | // fp: frame pointer (restored after C call) |
| 6404 | // sp: stack pointer (restored as callee's sp after C call) |
| 6405 | // cp: current context (C callee-saved) |
| 6406 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6407 | // Result returned in r0 or r0+r1 by default. |
| 6408 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6409 | // NOTE: Invocations of builtins may return failure objects |
| 6410 | // instead of a proper result. The builtin entry handles |
| 6411 | // this by performing a garbage collection and retrying the |
| 6412 | // builtin once. |
| 6413 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6414 | // Enter the exit frame that transitions from JavaScript to C++. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6415 | __ EnterExitFrame(mode_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6416 | |
| 6417 | // r4: number of arguments (C callee-saved) |
| 6418 | // r5: pointer to builtin function (C callee-saved) |
| 6419 | // r6: pointer to first argument (C callee-saved) |
| 6420 | |
| 6421 | Label throw_normal_exception; |
| 6422 | Label throw_termination_exception; |
| 6423 | Label throw_out_of_memory_exception; |
| 6424 | |
| 6425 | // Call into the runtime system. |
| 6426 | GenerateCore(masm, |
| 6427 | &throw_normal_exception, |
| 6428 | &throw_termination_exception, |
| 6429 | &throw_out_of_memory_exception, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6430 | false, |
| 6431 | false); |
| 6432 | |
| 6433 | // Do space-specific GC and retry runtime call. |
| 6434 | GenerateCore(masm, |
| 6435 | &throw_normal_exception, |
| 6436 | &throw_termination_exception, |
| 6437 | &throw_out_of_memory_exception, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6438 | true, |
| 6439 | false); |
| 6440 | |
| 6441 | // Do full GC and retry runtime call one final time. |
| 6442 | Failure* failure = Failure::InternalError(); |
| 6443 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 6444 | GenerateCore(masm, |
| 6445 | &throw_normal_exception, |
| 6446 | &throw_termination_exception, |
| 6447 | &throw_out_of_memory_exception, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6448 | true, |
| 6449 | true); |
| 6450 | |
| 6451 | __ bind(&throw_out_of_memory_exception); |
| 6452 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 6453 | |
| 6454 | __ bind(&throw_termination_exception); |
| 6455 | GenerateThrowUncatchable(masm, TERMINATION); |
| 6456 | |
| 6457 | __ bind(&throw_normal_exception); |
| 6458 | GenerateThrowTOS(masm); |
| 6459 | } |
| 6460 | |
| 6461 | |
| 6462 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 6463 | // r0: code entry |
| 6464 | // r1: function |
| 6465 | // r2: receiver |
| 6466 | // r3: argc |
| 6467 | // [sp+0]: argv |
| 6468 | |
| 6469 | Label invoke, exit; |
| 6470 | |
| 6471 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 6472 | // No need to save register-passed args |
| 6473 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
| 6474 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 6475 | |
| 6476 | // Get address of argv, see stm above. |
| 6477 | // r0: code entry |
| 6478 | // r1: function |
| 6479 | // r2: receiver |
| 6480 | // r3: argc |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame^] | 6481 | __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6482 | |
| 6483 | // Push a frame with special values setup to mark it as an entry frame. |
| 6484 | // r0: code entry |
| 6485 | // r1: function |
| 6486 | // r2: receiver |
| 6487 | // r3: argc |
| 6488 | // r4: argv |
| 6489 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 6490 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 6491 | __ mov(r7, Operand(Smi::FromInt(marker))); |
| 6492 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 6493 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6494 | __ ldr(r5, MemOperand(r5)); |
| 6495 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 6496 | |
| 6497 | // Setup frame pointer for the frame to be pushed. |
| 6498 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6499 | |
| 6500 | // Call a faked try-block that does the invoke. |
| 6501 | __ bl(&invoke); |
| 6502 | |
| 6503 | // Caught exception: Store result (exception) in the pending |
| 6504 | // exception field in the JSEnv and return a failure sentinel. |
| 6505 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 6506 | // sets it to 0 to signal the existence of the JSEntry frame. |
| 6507 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 6508 | __ str(r0, MemOperand(ip)); |
| 6509 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 6510 | __ b(&exit); |
| 6511 | |
| 6512 | // Invoke: Link this frame into the handler chain. |
| 6513 | __ bind(&invoke); |
| 6514 | // Must preserve r0-r4, r5-r7 are available. |
| 6515 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
| 6516 | // If an exception not caught by another handler occurs, this handler |
| 6517 | // returns control to the code after the bl(&invoke) above, which |
| 6518 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 6519 | // saved values before returning a failure to C. |
| 6520 | |
| 6521 | // Clear any pending exceptions. |
| 6522 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6523 | __ ldr(r5, MemOperand(ip)); |
| 6524 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 6525 | __ str(r5, MemOperand(ip)); |
| 6526 | |
| 6527 | // Invoke the function by calling through JS entry trampoline builtin. |
| 6528 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6529 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6530 | |
| 6531 | // Expected registers by Builtins::JSEntryTrampoline |
| 6532 | // r0: code entry |
| 6533 | // r1: function |
| 6534 | // r2: receiver |
| 6535 | // r3: argc |
| 6536 | // r4: argv |
| 6537 | if (is_construct) { |
| 6538 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6539 | __ mov(ip, Operand(construct_entry)); |
| 6540 | } else { |
| 6541 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6542 | __ mov(ip, Operand(entry)); |
| 6543 | } |
| 6544 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6545 | |
| 6546 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6547 | // macro for the add instruction because we don't want the coverage tool |
| 6548 | // inserting instructions here after we read the pc. |
| 6549 | __ mov(lr, Operand(pc)); |
| 6550 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 6551 | |
| 6552 | // Unlink this frame from the handler chain. When reading the |
| 6553 | // address of the next handler, there is no need to use the address |
| 6554 | // displacement since the current stack pointer (sp) points directly |
| 6555 | // to the stack handler. |
| 6556 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6557 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6558 | __ str(r3, MemOperand(ip)); |
| 6559 | // No need to restore registers |
| 6560 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6561 | |
| 6562 | |
| 6563 | __ bind(&exit); // r0 holds result |
| 6564 | // Restore the top frame descriptors from the stack. |
| 6565 | __ pop(r3); |
| 6566 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6567 | __ str(r3, MemOperand(ip)); |
| 6568 | |
| 6569 | // Reset the stack to the callee saved registers. |
| 6570 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6571 | |
| 6572 | // Restore callee-saved registers and return. |
| 6573 | #ifdef DEBUG |
| 6574 | if (FLAG_debug_code) { |
| 6575 | __ mov(lr, Operand(pc)); |
| 6576 | } |
| 6577 | #endif |
| 6578 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6579 | } |
| 6580 | |
| 6581 | |
| 6582 | // This stub performs an instanceof, calling the builtin function if |
| 6583 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6584 | // be an instance of (these are fetched from the stack). |
| 6585 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6586 | // Get the object - slow case for smis (we may need to throw an exception |
| 6587 | // depending on the rhs). |
| 6588 | Label slow, loop, is_instance, is_not_instance; |
| 6589 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6590 | __ BranchOnSmi(r0, &slow); |
| 6591 | |
| 6592 | // Check that the left hand is a JS object and put map in r3. |
| 6593 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6594 | __ b(lt, &slow); |
| 6595 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6596 | __ b(gt, &slow); |
| 6597 | |
| 6598 | // Get the prototype of the function (r4 is result, r2 is scratch). |
| 6599 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 6600 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6601 | |
| 6602 | // Check that the function prototype is a JS object. |
| 6603 | __ BranchOnSmi(r4, &slow); |
| 6604 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6605 | __ b(lt, &slow); |
| 6606 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6607 | __ b(gt, &slow); |
| 6608 | |
| 6609 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6610 | // Get prototype of object into r2. |
| 6611 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6612 | |
| 6613 | // Loop through the prototype chain looking for the function prototype. |
| 6614 | __ bind(&loop); |
| 6615 | __ cmp(r2, Operand(r4)); |
| 6616 | __ b(eq, &is_instance); |
| 6617 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 6618 | __ cmp(r2, ip); |
| 6619 | __ b(eq, &is_not_instance); |
| 6620 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6621 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6622 | __ jmp(&loop); |
| 6623 | |
| 6624 | __ bind(&is_instance); |
| 6625 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6626 | __ pop(); |
| 6627 | __ pop(); |
| 6628 | __ mov(pc, Operand(lr)); // Return. |
| 6629 | |
| 6630 | __ bind(&is_not_instance); |
| 6631 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6632 | __ pop(); |
| 6633 | __ pop(); |
| 6634 | __ mov(pc, Operand(lr)); // Return. |
| 6635 | |
| 6636 | // Slow-case. Tail call builtin. |
| 6637 | __ bind(&slow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6638 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6639 | } |
| 6640 | |
| 6641 | |
| 6642 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
| 6643 | // Check if the calling frame is an arguments adaptor frame. |
| 6644 | Label adaptor; |
| 6645 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6646 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6647 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 6648 | __ b(eq, &adaptor); |
| 6649 | |
| 6650 | // Nothing to do: The formal number of parameters has already been |
| 6651 | // passed in register r0 by calling function. Just return it. |
| 6652 | __ Jump(lr); |
| 6653 | |
| 6654 | // Arguments adaptor case: Read the arguments length from the |
| 6655 | // adaptor frame and return it. |
| 6656 | __ bind(&adaptor); |
| 6657 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6658 | __ Jump(lr); |
| 6659 | } |
| 6660 | |
| 6661 | |
| 6662 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6663 | // The displacement is the offset of the last parameter (if any) |
| 6664 | // relative to the frame pointer. |
| 6665 | static const int kDisplacement = |
| 6666 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
| 6667 | |
| 6668 | // Check that the key is a smi. |
| 6669 | Label slow; |
| 6670 | __ BranchOnNotSmi(r1, &slow); |
| 6671 | |
| 6672 | // Check if the calling frame is an arguments adaptor frame. |
| 6673 | Label adaptor; |
| 6674 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6675 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6676 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 6677 | __ b(eq, &adaptor); |
| 6678 | |
| 6679 | // Check index against formal parameters count limit passed in |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6680 | // through register r0. Use unsigned comparison to get negative |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6681 | // check for free. |
| 6682 | __ cmp(r1, r0); |
| 6683 | __ b(cs, &slow); |
| 6684 | |
| 6685 | // Read the argument from the stack and return it. |
| 6686 | __ sub(r3, r0, r1); |
| 6687 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6688 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 6689 | __ Jump(lr); |
| 6690 | |
| 6691 | // Arguments adaptor case: Check index against actual arguments |
| 6692 | // limit found in the arguments adaptor frame. Use unsigned |
| 6693 | // comparison to get negative check for free. |
| 6694 | __ bind(&adaptor); |
| 6695 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6696 | __ cmp(r1, r0); |
| 6697 | __ b(cs, &slow); |
| 6698 | |
| 6699 | // Read the argument from the adaptor frame and return it. |
| 6700 | __ sub(r3, r0, r1); |
| 6701 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6702 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 6703 | __ Jump(lr); |
| 6704 | |
| 6705 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6706 | // by calling the runtime system. |
| 6707 | __ bind(&slow); |
| 6708 | __ push(r1); |
| 6709 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1); |
| 6710 | } |
| 6711 | |
| 6712 | |
| 6713 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 6714 | // Check if the calling frame is an arguments adaptor frame. |
| 6715 | Label runtime; |
| 6716 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6717 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6718 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 6719 | __ b(ne, &runtime); |
| 6720 | |
| 6721 | // Patch the arguments.length and the parameters pointer. |
| 6722 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6723 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 6724 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6725 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6726 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6727 | |
| 6728 | // Do the runtime call to allocate the arguments object. |
| 6729 | __ bind(&runtime); |
| 6730 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1); |
| 6731 | } |
| 6732 | |
| 6733 | |
| 6734 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6735 | Label slow; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6736 | |
| 6737 | // If the receiver might be a value (string, number or boolean) check for this |
| 6738 | // and box it if it is. |
| 6739 | if (ReceiverMightBeValue()) { |
| 6740 | // Get the receiver from the stack. |
| 6741 | // function, receiver [, arguments] |
| 6742 | Label receiver_is_value, receiver_is_js_object; |
| 6743 | __ ldr(r1, MemOperand(sp, argc_ * kPointerSize)); |
| 6744 | |
| 6745 | // Check if receiver is a smi (which is a number value). |
| 6746 | __ BranchOnSmi(r1, &receiver_is_value); |
| 6747 | |
| 6748 | // Check if the receiver is a valid JS object. |
| 6749 | __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 6750 | __ b(ge, &receiver_is_js_object); |
| 6751 | |
| 6752 | // Call the runtime to box the value. |
| 6753 | __ bind(&receiver_is_value); |
| 6754 | __ EnterInternalFrame(); |
| 6755 | __ push(r1); |
| 6756 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
| 6757 | __ LeaveInternalFrame(); |
| 6758 | __ str(r0, MemOperand(sp, argc_ * kPointerSize)); |
| 6759 | |
| 6760 | __ bind(&receiver_is_js_object); |
| 6761 | } |
| 6762 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6763 | // Get the function to call from the stack. |
| 6764 | // function, receiver [, arguments] |
| 6765 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6766 | |
| 6767 | // Check that the function is really a JavaScript function. |
| 6768 | // r1: pushed function (to be verified) |
| 6769 | __ BranchOnSmi(r1, &slow); |
| 6770 | // Get the map of the function object. |
| 6771 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
| 6772 | __ b(ne, &slow); |
| 6773 | |
| 6774 | // Fast-case: Invoke the function now. |
| 6775 | // r1: pushed function |
| 6776 | ParameterCount actual(argc_); |
| 6777 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6778 | |
| 6779 | // Slow-case: Non-function called. |
| 6780 | __ bind(&slow); |
| 6781 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
| 6782 | __ mov(r2, Operand(0)); |
| 6783 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6784 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6785 | RelocInfo::CODE_TARGET); |
| 6786 | } |
| 6787 | |
| 6788 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6789 | const char* CompareStub::GetName() { |
| 6790 | switch (cc_) { |
| 6791 | case lt: return "CompareStub_LT"; |
| 6792 | case gt: return "CompareStub_GT"; |
| 6793 | case le: return "CompareStub_LE"; |
| 6794 | case ge: return "CompareStub_GE"; |
| 6795 | case ne: { |
| 6796 | if (strict_) { |
| 6797 | if (never_nan_nan_) { |
| 6798 | return "CompareStub_NE_STRICT_NO_NAN"; |
| 6799 | } else { |
| 6800 | return "CompareStub_NE_STRICT"; |
| 6801 | } |
| 6802 | } else { |
| 6803 | if (never_nan_nan_) { |
| 6804 | return "CompareStub_NE_NO_NAN"; |
| 6805 | } else { |
| 6806 | return "CompareStub_NE"; |
| 6807 | } |
| 6808 | } |
| 6809 | } |
| 6810 | case eq: { |
| 6811 | if (strict_) { |
| 6812 | if (never_nan_nan_) { |
| 6813 | return "CompareStub_EQ_STRICT_NO_NAN"; |
| 6814 | } else { |
| 6815 | return "CompareStub_EQ_STRICT"; |
| 6816 | } |
| 6817 | } else { |
| 6818 | if (never_nan_nan_) { |
| 6819 | return "CompareStub_EQ_NO_NAN"; |
| 6820 | } else { |
| 6821 | return "CompareStub_EQ"; |
| 6822 | } |
| 6823 | } |
| 6824 | } |
| 6825 | default: return "CompareStub"; |
| 6826 | } |
| 6827 | } |
| 6828 | |
| 6829 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6830 | int CompareStub::MinorKey() { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6831 | // Encode the three parameters in a unique 16 bit value. |
| 6832 | ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16)); |
| 6833 | int nnn_value = (never_nan_nan_ ? 2 : 0); |
| 6834 | if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs. |
| 6835 | return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6836 | } |
| 6837 | |
| 6838 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6839 | |
| 6840 | |
| 6841 | void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 6842 | Register left, |
| 6843 | Register right, |
| 6844 | Register scratch1, |
| 6845 | Register scratch2, |
| 6846 | Register scratch3, |
| 6847 | Register scratch4) { |
| 6848 | Label compare_lengths; |
| 6849 | // Find minimum length and length difference. |
| 6850 | __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset)); |
| 6851 | __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset)); |
| 6852 | __ sub(scratch3, scratch1, Operand(scratch2), SetCC); |
| 6853 | Register length_delta = scratch3; |
| 6854 | __ mov(scratch1, scratch2, LeaveCC, gt); |
| 6855 | Register min_length = scratch1; |
| 6856 | __ tst(min_length, Operand(min_length)); |
| 6857 | __ b(eq, &compare_lengths); |
| 6858 | |
| 6859 | // Setup registers so that we only need to increment one register |
| 6860 | // in the loop. |
| 6861 | __ add(scratch2, min_length, |
| 6862 | Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 6863 | __ add(left, left, Operand(scratch2)); |
| 6864 | __ add(right, right, Operand(scratch2)); |
| 6865 | // Registers left and right points to the min_length character of strings. |
| 6866 | __ rsb(min_length, min_length, Operand(-1)); |
| 6867 | Register index = min_length; |
| 6868 | // Index starts at -min_length. |
| 6869 | |
| 6870 | { |
| 6871 | // Compare loop. |
| 6872 | Label loop; |
| 6873 | __ bind(&loop); |
| 6874 | // Compare characters. |
| 6875 | __ add(index, index, Operand(1), SetCC); |
| 6876 | __ ldrb(scratch2, MemOperand(left, index), ne); |
| 6877 | __ ldrb(scratch4, MemOperand(right, index), ne); |
| 6878 | // Skip to compare lengths with eq condition true. |
| 6879 | __ b(eq, &compare_lengths); |
| 6880 | __ cmp(scratch2, scratch4); |
| 6881 | __ b(eq, &loop); |
| 6882 | // Fallthrough with eq condition false. |
| 6883 | } |
| 6884 | // Compare lengths - strings up to min-length are equal. |
| 6885 | __ bind(&compare_lengths); |
| 6886 | ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0)); |
| 6887 | // Use zero length_delta as result. |
| 6888 | __ mov(r0, Operand(length_delta), SetCC, eq); |
| 6889 | // Fall through to here if characters compare not-equal. |
| 6890 | __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt); |
| 6891 | __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt); |
| 6892 | __ Ret(); |
| 6893 | } |
| 6894 | |
| 6895 | |
| 6896 | void StringCompareStub::Generate(MacroAssembler* masm) { |
| 6897 | Label runtime; |
| 6898 | |
| 6899 | // Stack frame on entry. |
| 6900 | // sp[0]: return address |
| 6901 | // sp[4]: right string |
| 6902 | // sp[8]: left string |
| 6903 | |
| 6904 | __ ldr(r0, MemOperand(sp, 2 * kPointerSize)); // left |
| 6905 | __ ldr(r1, MemOperand(sp, 1 * kPointerSize)); // right |
| 6906 | |
| 6907 | Label not_same; |
| 6908 | __ cmp(r0, r1); |
| 6909 | __ b(ne, ¬_same); |
| 6910 | ASSERT_EQ(0, EQUAL); |
| 6911 | ASSERT_EQ(0, kSmiTag); |
| 6912 | __ mov(r0, Operand(Smi::FromInt(EQUAL))); |
| 6913 | __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2); |
| 6914 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 6915 | __ Ret(); |
| 6916 | |
| 6917 | __ bind(¬_same); |
| 6918 | |
| 6919 | // Check that both objects are sequential ascii strings. |
| 6920 | __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime); |
| 6921 | |
| 6922 | // Compare flat ascii strings natively. Remove arguments from stack first. |
| 6923 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 6924 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 6925 | GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5); |
| 6926 | |
| 6927 | // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater) |
| 6928 | // tagged as a small integer. |
| 6929 | __ bind(&runtime); |
| 6930 | __ TailCallRuntime(ExternalReference(Runtime::kStringCompare), 2, 1); |
| 6931 | } |
| 6932 | |
| 6933 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6934 | #undef __ |
| 6935 | |
| 6936 | } } // namespace v8::internal |