sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "debug.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 34 | #include "parser.h" |
| 35 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 37 | #include "scopes.h" |
| 38 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 43 | #define __ ACCESS_MASM(masm_) |
| 44 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 45 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 46 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 47 | Condition cc, |
| 48 | bool never_nan_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 49 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 50 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 51 | Label* slow, |
| 52 | bool strict); |
| 53 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
| 54 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 55 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 56 | Register source, |
| 57 | Register destination, |
| 58 | int known_int); |
| 59 | static bool IsEasyToMultiplyBy(int x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 91 | |
| 92 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 93 | // CodeGenState implementation. |
| 94 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 95 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 96 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 97 | true_target_(NULL), |
| 98 | false_target_(NULL), |
| 99 | previous_(NULL) { |
| 100 | owner_->set_state(this); |
| 101 | } |
| 102 | |
| 103 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 104 | CodeGenState::CodeGenState(CodeGenerator* owner, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 105 | JumpTarget* true_target, |
| 106 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 107 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +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 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 115 | CodeGenState::~CodeGenState() { |
| 116 | ASSERT(owner_->state() == this); |
| 117 | owner_->set_state(previous_); |
| 118 | } |
| 119 | |
| 120 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 121 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 122 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 124 | CodeGenerator::CodeGenerator(MacroAssembler* masm, |
| 125 | Handle<Script> script, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 126 | bool is_eval) |
| 127 | : is_eval_(is_eval), |
| 128 | script_(script), |
| 129 | deferred_(8), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 130 | masm_(masm), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 132 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 133 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | cc_reg_(al), |
| 135 | state_(NULL), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 136 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 141 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 143 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | // cp: callee's context |
| 145 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 146 | void CodeGenerator::Generate(FunctionLiteral* fun, |
| 147 | Mode mode, |
| 148 | CompilationInfo* info) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 149 | // Record the position for debugging purposes. |
| 150 | CodeForFunctionPosition(fun); |
| 151 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | ZoneList<Statement*>* body = fun->body(); |
| 153 | |
| 154 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 155 | ASSERT(scope_ == NULL); |
| 156 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 157 | ASSERT(allocator_ == NULL); |
| 158 | RegisterAllocator register_allocator(this); |
| 159 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 160 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 161 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 162 | cc_reg_ = al; |
| 163 | { |
| 164 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 166 | // Entry: |
| 167 | // Stack: receiver, arguments |
| 168 | // lr: return address |
| 169 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 171 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 173 | allocator_->Initialize(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 174 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | #ifdef DEBUG |
| 176 | if (strlen(FLAG_stop_at) > 0 && |
| 177 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 178 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 179 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | } |
| 181 | #endif |
| 182 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +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. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 292 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 293 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 294 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +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()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 300 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | } else { |
| 302 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 303 | ProcessDeclarations(scope_->declarations()); |
| 304 | // Bail out if a stack-overflow exception occurred when processing |
| 305 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 306 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 307 | } |
| 308 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 309 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 310 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 311 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 312 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 317 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 323 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 324 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 325 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 326 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 328 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 329 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 330 | } |
| 331 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 332 | // Generate the return sequence if necessary. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 333 | if (has_valid_frame() || function_return_.is_linked()) { |
| 334 | if (!function_return_.is_linked()) { |
| 335 | CodeForReturnPosition(fun); |
| 336 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 337 | // exit |
| 338 | // r0: result |
| 339 | // sp: stack pointer |
| 340 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 341 | // cp: callee's context |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 342 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 343 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 359 | int return_sequence_length = Assembler::kJSReturnSequenceLength; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 360 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 361 | // Additional mov instruction generated. |
| 362 | return_sequence_length++; |
| 363 | } |
| 364 | masm_->BlockConstPoolFor(return_sequence_length); |
| 365 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +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 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 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. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 372 | masm_->add(sp, sp, Operand(sp_delta)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 373 | masm_->Jump(lr); |
| 374 | |
| 375 | // Check that the size of the code used for returning matches what is |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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, |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 382 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 383 | } |
| 384 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 385 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | ASSERT(!has_cc()); |
| 387 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 388 | ASSERT(!function_return_is_shadowed_); |
| 389 | function_return_.Unuse(); |
| 390 | DeleteFrame(); |
| 391 | |
| 392 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 393 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 394 | ProcessDeferred(); |
| 395 | } |
| 396 | |
| 397 | allocator_ = NULL; |
| 398 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 415 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 416 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 417 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 418 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 425 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 453 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 454 | Slot* slot, |
| 455 | Register tmp, |
| 456 | Register tmp2, |
| 457 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 458 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 459 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 460 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 467 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 477 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 478 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 479 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 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'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 489 | void CodeGenerator::LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 490 | JumpTarget* true_target, |
| 491 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 492 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 493 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 494 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 495 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 496 | { CodeGenState new_state(this, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 497 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 514 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 515 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 516 | // Convert the TOS value to a boolean in the condition code register. |
| 517 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 526 | void CodeGenerator::Load(Expression* expr) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 527 | #ifdef DEBUG |
| 528 | int original_height = frame_->height(); |
| 529 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 530 | JumpTarget true_target; |
| 531 | JumpTarget false_target; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 532 | LoadCondition(expr, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 533 | |
| 534 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 535 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 536 | JumpTarget loaded; |
| 537 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 538 | materialize_true.Branch(cc_reg_); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 539 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 540 | frame_->EmitPush(r0); |
| 541 | loaded.Jump(); |
| 542 | materialize_true.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 543 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 544 | frame_->EmitPush(r0); |
| 545 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 546 | cc_reg_ = al; |
| 547 | } |
| 548 | |
| 549 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 550 | // We have at least one condition value that has been "translated" |
| 551 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 552 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 553 | if (frame_ != NULL) { |
| 554 | loaded.Jump(); // Don't lose the current TOS. |
| 555 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 556 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 557 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 558 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 559 | true_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 560 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 561 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 562 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 569 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 570 | false_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 571 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 572 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 573 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 574 | // A value is loaded on all paths reaching this point. |
| 575 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 576 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 577 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 578 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 579 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 583 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 584 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 585 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 586 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 590 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 591 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 592 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 593 | __ ldr(scratch, |
| 594 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 595 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 599 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 600 | // Special handling of identifiers as subexpressions of typeof. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 601 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 602 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 608 | Literal key(variable->name()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 609 | Property property(&global, &key, RelocInfo::kNoPosition); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 617 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 618 | // Anything else can be handled normally. |
| 619 | LoadAndSpill(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 631 | cgen->LoadReference(this); |
| 632 | } |
| 633 | |
| 634 | |
| 635 | Reference::~Reference() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 636 | ASSERT(is_unloaded() || is_illegal()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 640 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 641 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 642 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 643 | Expression* e = ref->expression(); |
| 644 | Property* property = e->AsProperty(); |
| 645 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 646 | |
| 647 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 648 | // The expression is either a property or a variable proxy that rewrites |
| 649 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 650 | LoadAndSpill(property->obj()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 651 | if (property->key()->IsPropertyName()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 652 | ref->set_type(Reference::NAMED); |
| 653 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 654 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 655 | ref->set_type(Reference::KEYED); |
| 656 | } |
| 657 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 658 | // The expression is a variable proxy that does not rewrite to a |
| 659 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 660 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 661 | LoadGlobal(); |
| 662 | ref->set_type(Reference::NAMED); |
| 663 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 664 | ASSERT(var->slot() != NULL); |
| 665 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 666 | } |
| 667 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 668 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 669 | LoadAndSpill(e); |
| 670 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 675 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 676 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 677 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 678 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 679 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 680 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 681 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 682 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 683 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 684 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 685 | ref->set_unloaded(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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'. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 692 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 693 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 694 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 695 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 696 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 697 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | |
| 699 | // Fast case checks |
| 700 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 701 | // Check if the value is 'false'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 702 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 703 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 704 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 705 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 706 | // Check if the value is 'true'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 707 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 708 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 709 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 710 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 711 | // Check if the value is 'undefined'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 712 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 713 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 714 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 715 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 716 | // Check if the value is a smi. |
| 717 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 718 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 719 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 720 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 721 | |
| 722 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 723 | frame_->EmitPush(r0); |
| 724 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 725 | // Convert the result (r0) to a condition code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 726 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 727 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 728 | |
| 729 | cc_reg_ = ne; |
| 730 | } |
| 731 | |
| 732 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 733 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 734 | OverwriteMode overwrite_mode, |
| 735 | int constant_rhs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 736 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 737 | // sp[0] : y |
| 738 | // sp[1] : x |
| 739 | // result : r0 |
| 740 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 745 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 746 | case Token::DIV: |
| 747 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 754 | frame_->EmitPop(r0); // r0 : y |
| 755 | frame_->EmitPop(r1); // r1 : x |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 756 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 757 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 758 | break; |
| 759 | } |
| 760 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 761 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 762 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 763 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 764 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 765 | break; |
| 766 | |
| 767 | default: |
| 768 | // Other cases should have been handled before this point. |
| 769 | UNREACHABLE(); |
| 770 | break; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 775 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 776 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 777 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 778 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 779 | bool reversed, |
| 780 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 781 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 782 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 783 | reversed_(reversed), |
| 784 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 785 | set_comment("[ DeferredInlinedSmiOperation"); |
| 786 | } |
| 787 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 788 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 789 | |
| 790 | private: |
| 791 | Token::Value op_; |
| 792 | int value_; |
| 793 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 794 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 795 | }; |
| 796 | |
| 797 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 798 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 799 | switch (op_) { |
| 800 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 801 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 802 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 803 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 804 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 805 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 806 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 807 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 808 | } |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 813 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 814 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 824 | // For these operations there is no optimistic operation that needs to be |
| 825 | // reverted. |
| 826 | case Token::MUL: |
| 827 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 847 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 848 | } |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 853 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 854 | UNREACHABLE(); |
| 855 | break; |
| 856 | } |
| 857 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 858 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 859 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 884 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 885 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 886 | bool reversed, |
| 887 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 888 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 896 | // sp[0] : operand |
| 897 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 898 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 899 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 900 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 901 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 902 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 903 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 904 | switch (op) { |
| 905 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 906 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 907 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 908 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 909 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 910 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 911 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 912 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 913 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 914 | break; |
| 915 | } |
| 916 | |
| 917 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 918 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 919 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 920 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 921 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 922 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 923 | } else { |
| 924 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 925 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 926 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 927 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 928 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 929 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 930 | break; |
| 931 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 932 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 933 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 934 | case Token::BIT_OR: |
| 935 | case Token::BIT_XOR: |
| 936 | case Token::BIT_AND: { |
| 937 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 938 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 939 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 940 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 947 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 948 | break; |
| 949 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 950 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 951 | case Token::SHL: |
| 952 | case Token::SHR: |
| 953 | case Token::SAR: { |
| 954 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 955 | something_to_inline = false; |
| 956 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 957 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 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(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1041 | break; |
| 1042 | } |
| 1043 | |
| 1044 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1045 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1046 | break; |
| 1047 | } |
| 1048 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1063 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1074 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1075 | // sp[0] : y |
| 1076 | // sp[1] : x |
| 1077 | // result : cc register |
| 1078 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1079 | // Strict only makes sense for equality comparisons. |
| 1080 | ASSERT(!strict || cc == eq); |
| 1081 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1082 | JumpTarget exit; |
| 1083 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1084 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1085 | if (cc == gt || cc == le) { |
| 1086 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1087 | frame_->EmitPop(r1); |
| 1088 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1089 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1090 | frame_->EmitPop(r0); |
| 1091 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1092 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1093 | __ orr(r2, r0, Operand(r1)); |
| 1094 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1095 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1096 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 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); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1102 | __ cmp(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1103 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1104 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1105 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1106 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1107 | __ cmp(r1, Operand(r0)); |
| 1108 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1109 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1110 | cc_reg_ = cc; |
| 1111 | } |
| 1112 | |
| 1113 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1114 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1115 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 1116 | CallFunctionFlags flags, |
| 1117 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1118 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1119 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1120 | int arg_count = args->length(); |
| 1121 | for (int i = 0; i < arg_count; i++) { |
| 1122 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1123 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1124 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1125 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1126 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1127 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1128 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1129 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 1130 | CallFunctionStub call_function(arg_count, in_loop, flags); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1131 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1134 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1135 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1139 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1140 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1141 | ASSERT(has_cc()); |
| 1142 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1143 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1144 | cc_reg_ = al; |
| 1145 | } |
| 1146 | |
| 1147 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1148 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1149 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1167 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1168 | #ifdef DEBUG |
| 1169 | int original_height = frame_->height(); |
| 1170 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1171 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1179 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1180 | #ifdef DEBUG |
| 1181 | int original_height = frame_->height(); |
| 1182 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1183 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1184 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1185 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1186 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1196 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1197 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1198 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1199 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1200 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1201 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1202 | frame_->EmitPush(r0); |
| 1203 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1204 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1208 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1209 | #ifdef DEBUG |
| 1210 | int original_height = frame_->height(); |
| 1211 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1212 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1224 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1225 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1226 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1227 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1228 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1232 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1233 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1239 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1240 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1241 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1242 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1243 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1244 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1245 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1246 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1247 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1248 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1249 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1264 | { |
| 1265 | // Set initial value. |
| 1266 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1267 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1268 | target.SetValue(NOT_CONST_INIT); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1269 | } |
| 1270 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1271 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1272 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1273 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1277 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1278 | #ifdef DEBUG |
| 1279 | int original_height = frame_->height(); |
| 1280 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1281 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1282 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1283 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1284 | Expression* expression = node->expression(); |
| 1285 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1286 | LoadAndSpill(expression); |
| 1287 | frame_->Drop(); |
| 1288 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1292 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1293 | #ifdef DEBUG |
| 1294 | int original_height = frame_->height(); |
| 1295 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1296 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1297 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1298 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1299 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1300 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1304 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1305 | #ifdef DEBUG |
| 1306 | int original_height = frame_->height(); |
| 1307 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1308 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1309 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1310 | // Generate different code depending on which parts of the if statement |
| 1311 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1312 | bool has_then_stm = node->HasThenStatement(); |
| 1313 | bool has_else_stm = node->HasElseStatement(); |
| 1314 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1315 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1316 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1317 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1318 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1319 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1320 | JumpTarget then; |
| 1321 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1322 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1323 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1324 | if (frame_ != NULL) { |
| 1325 | Branch(false, &else_); |
| 1326 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1327 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1328 | if (frame_ != NULL || then.is_linked()) { |
| 1329 | then.Bind(); |
| 1330 | VisitAndSpill(node->then_statement()); |
| 1331 | } |
| 1332 | if (frame_ != NULL) { |
| 1333 | exit.Jump(); |
| 1334 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1335 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1336 | if (else_.is_linked()) { |
| 1337 | else_.Bind(); |
| 1338 | VisitAndSpill(node->else_statement()); |
| 1339 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1340 | |
| 1341 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1342 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1343 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1344 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1345 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1346 | LoadConditionAndSpill(node->condition(), &then, &exit, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1347 | if (frame_ != NULL) { |
| 1348 | Branch(false, &exit); |
| 1349 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1350 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1351 | if (frame_ != NULL || then.is_linked()) { |
| 1352 | then.Bind(); |
| 1353 | VisitAndSpill(node->then_statement()); |
| 1354 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1355 | |
| 1356 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1357 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1358 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1359 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1360 | // if (!cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1361 | LoadConditionAndSpill(node->condition(), &exit, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1362 | if (frame_ != NULL) { |
| 1363 | Branch(true, &exit); |
| 1364 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1365 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1366 | if (frame_ != NULL || else_.is_linked()) { |
| 1367 | else_.Bind(); |
| 1368 | VisitAndSpill(node->else_statement()); |
| 1369 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1370 | |
| 1371 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1372 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1373 | ASSERT(!has_then_stm && !has_else_stm); |
| 1374 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1375 | LoadConditionAndSpill(node->condition(), &exit, &exit, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1376 | if (frame_ != NULL) { |
| 1377 | if (has_cc()) { |
| 1378 | cc_reg_ = al; |
| 1379 | } else { |
| 1380 | frame_->Drop(); |
| 1381 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1386 | if (exit.is_linked()) { |
| 1387 | exit.Bind(); |
| 1388 | } |
| 1389 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1393 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1394 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1395 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1396 | CodeForStatementPosition(node); |
| 1397 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1401 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1402 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1403 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1404 | CodeForStatementPosition(node); |
| 1405 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1409 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1410 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1411 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1412 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1413 | CodeForStatementPosition(node); |
| 1414 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1415 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1416 | frame_->EmitPop(r0); |
| 1417 | function_return_.Jump(); |
| 1418 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1429 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1430 | #ifdef DEBUG |
| 1431 | int original_height = frame_->height(); |
| 1432 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1433 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1434 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1435 | CodeForStatementPosition(node); |
| 1436 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1437 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1438 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1439 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1440 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1441 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1442 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1443 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 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 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1450 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1451 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1455 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1456 | #ifdef DEBUG |
| 1457 | int original_height = frame_->height(); |
| 1458 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1459 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1460 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1461 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1462 | // Pop context. |
| 1463 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1464 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1465 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1466 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1470 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1471 | #ifdef DEBUG |
| 1472 | int original_height = frame_->height(); |
| 1473 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1474 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1475 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1476 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1477 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1478 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1479 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1480 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1481 | JumpTarget next_test; |
| 1482 | JumpTarget fall_through; |
| 1483 | JumpTarget default_entry; |
| 1484 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1485 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1486 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1487 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1488 | |
| 1489 | for (int i = 0; i < length; i++) { |
| 1490 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1491 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1492 | // Remember the default clause and compile it at the end. |
| 1493 | default_clause = clause; |
| 1494 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1504 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1505 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1506 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1545 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1546 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1559 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1560 | #ifdef DEBUG |
| 1561 | int original_height = frame_->height(); |
| 1562 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1563 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1564 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1565 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1566 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1567 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1568 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1575 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1576 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1586 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1587 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1588 | VisitAndSpill(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1589 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1590 | // Compile the test. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1595 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1596 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1597 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1598 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1603 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1604 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1613 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 1614 | CodeForDoWhileConditionPosition(node); |
| 1615 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1616 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1617 | // A invalid frame here indicates that control did not |
| 1618 | // fall out of the test expression. |
| 1619 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1620 | } |
| 1621 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1622 | break; |
| 1623 | } |
| 1624 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1625 | if (node->break_target()->is_linked()) { |
| 1626 | node->break_target()->Bind(); |
| 1627 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1654 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1713 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +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 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1752 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1756 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1757 | #ifdef DEBUG |
| 1758 | int original_height = frame_->height(); |
| 1759 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1760 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1761 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1762 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1763 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1764 | JumpTarget primitive; |
| 1765 | JumpTarget jsobject; |
| 1766 | JumpTarget fixed_array; |
| 1767 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1768 | JumpTarget end_del_check; |
| 1769 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1772 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1773 | |
| 1774 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1775 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1776 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1777 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 1778 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1779 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1780 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1781 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1782 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1793 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1794 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1795 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1796 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1797 | primitive.Bind(); |
| 1798 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1799 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1800 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1801 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1802 | // Get the set of properties (as a FixedArray or Map). |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +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 |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1856 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1857 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +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) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1863 | __ mov(r2, Operand(r0)); |
| 1864 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1865 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 1866 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1867 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1869 | use_cache.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1870 | // Get enum cache |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +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) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1880 | frame_->EmitPush(r0); // map |
| 1881 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1882 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1883 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1884 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1885 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1886 | frame_->EmitPush(r0); |
| 1887 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1888 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1889 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1890 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1891 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1892 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1893 | |
| 1894 | // Push the length of the array and the initial index onto the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1895 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1896 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1897 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1898 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1899 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1900 | |
| 1901 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1902 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 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 |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1908 | // Grab the current frame's height for the break and continue |
| 1909 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1910 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1911 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1912 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1919 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1920 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1921 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1922 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1923 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1924 | |
| 1925 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1926 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1927 | // Check if this (still) matches the map of the enumerable. |
| 1928 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1929 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1930 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1931 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1932 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1933 | |
| 1934 | // Convert the entry to a string (or null if it isn't a property anymore). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1935 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1936 | frame_->EmitPush(r0); |
| 1937 | frame_->EmitPush(r3); // push entry |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1938 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1939 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1940 | |
| 1941 | // If the property has been removed while iterating, we just skip it. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1942 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1943 | __ cmp(r3, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1944 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1945 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | { Reference each(this, node->each()); |
| 1951 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1952 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1953 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1954 | frame_->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1963 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1964 | } |
| 1965 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1966 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1967 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1968 | VisitAndSpill(node->body()); |
| 1969 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1970 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1971 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1972 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1973 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1974 | frame_->EmitPop(r0); |
| 1975 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1976 | frame_->EmitPush(r0); |
| 1977 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1978 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1979 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1980 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1981 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1982 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1983 | |
| 1984 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1985 | exit.Bind(); |
| 1986 | node->continue_target()->Unuse(); |
| 1987 | node->break_target()->Unuse(); |
| 1988 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1992 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1993 | #ifdef DEBUG |
| 1994 | int original_height = frame_->height(); |
| 1995 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1996 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1997 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1998 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1999 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2000 | JumpTarget try_block; |
| 2001 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2002 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2003 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2004 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2005 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2006 | |
| 2007 | // Store the caught exception in the catch variable. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2011 | |
| 2012 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2013 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2014 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2015 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2016 | if (frame_ != NULL) { |
| 2017 | exit.Jump(); |
| 2018 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2019 | |
| 2020 | |
| 2021 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2022 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2023 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2024 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2025 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2026 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2046 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2050 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | |
| 2052 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2053 | // After shadowing stops, the original labels are unshadowed and the |
| 2054 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2055 | bool has_unlinks = false; |
| 2056 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2057 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2065 | // If we can fall off the end of the try block, unlink from try chain. |
| 2066 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2072 | __ mov(r3, Operand(handler_address)); |
| 2073 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2074 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2075 | if (has_unlinks) { |
| 2076 | exit.Jump(); |
| 2077 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2080 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2081 | // jumped to. Deallocate each shadow target. |
| 2082 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2083 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2084 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2089 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2092 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2093 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2094 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2095 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2096 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2097 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2098 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2099 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2100 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2101 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2102 | frame_->PrepareForReturn(); |
| 2103 | } |
| 2104 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2108 | exit.Bind(); |
| 2109 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2113 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2114 | #ifdef DEBUG |
| 2115 | int original_height = frame_->height(); |
| 2116 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2117 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2118 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2119 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2126 | JumpTarget try_block; |
| 2127 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2128 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2129 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2130 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2131 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2132 | // In case of thrown exceptions, this is where we continue. |
| 2133 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2134 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2135 | |
| 2136 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2137 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2138 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2139 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2140 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2141 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2159 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2160 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2164 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2165 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2169 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2170 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2171 | shadows[i]->StopShadowing(); |
| 2172 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2173 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2174 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2175 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2176 | // Get an external reference to the handler address. |
| 2177 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2178 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2182 | // The next handler address is on top of the frame. |
| 2183 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2184 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2185 | __ mov(r3, Operand(handler_address)); |
| 2186 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2187 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2191 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2202 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2218 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2219 | |
| 2220 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2221 | // handler address is currently on top of the frame. |
| 2222 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2223 | frame_->EmitPop(r1); |
| 2224 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2225 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2226 | |
| 2227 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2228 | // If this label shadowed the function return, materialize the |
| 2229 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2230 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2231 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2232 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2233 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2234 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2235 | } |
| 2236 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2237 | if (--nof_unlinks > 0) { |
| 2238 | // If this is not the last unlink block, jump around the next. |
| 2239 | finally_block.Jump(); |
| 2240 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2244 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2245 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2246 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2247 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2248 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2249 | |
| 2250 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2251 | // and the state - while evaluating the finally block. |
| 2252 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2253 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2254 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2255 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2256 | if (has_valid_frame()) { |
| 2257 | // Restore state and return value or faked TOS. |
| 2258 | frame_->EmitPop(r2); |
| 2259 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2269 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2270 | skip.Branch(ne); |
| 2271 | frame_->PrepareForReturn(); |
| 2272 | original->Jump(); |
| 2273 | skip.Bind(); |
| 2274 | } else { |
| 2275 | original->Branch(eq); |
| 2276 | } |
| 2277 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2278 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2279 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2280 | if (has_valid_frame()) { |
| 2281 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2282 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2283 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2284 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2285 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2297 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2298 | #ifdef DEBUG |
| 2299 | int original_height = frame_->height(); |
| 2300 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2301 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2302 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2303 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2304 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 2305 | DebuggerStatementStub ces; |
| 2306 | frame_->CallStub(&ces, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2307 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2308 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2309 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2313 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2314 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2315 | ASSERT(boilerplate->IsBoilerplate()); |
| 2316 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2317 | __ mov(r0, Operand(boilerplate)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2335 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2336 | #ifdef DEBUG |
| 2337 | int original_height = frame_->height(); |
| 2338 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2339 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2340 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2341 | |
| 2342 | // Build the function boilerplate and instantiate it. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2343 | Handle<JSFunction> boilerplate = |
| 2344 | Compiler::BuildBoilerplate(node, script_, this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2345 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2346 | if (HasStackOverflow()) { |
| 2347 | ASSERT(frame_->height() == original_height); |
| 2348 | return; |
| 2349 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2350 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2351 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2355 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2356 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2357 | #ifdef DEBUG |
| 2358 | int original_height = frame_->height(); |
| 2359 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2360 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2361 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2362 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2363 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2367 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2368 | #ifdef DEBUG |
| 2369 | int original_height = frame_->height(); |
| 2370 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2371 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2372 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2373 | JumpTarget then; |
| 2374 | JumpTarget else_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2375 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2376 | if (has_valid_frame()) { |
| 2377 | Branch(false, &else_); |
| 2378 | } |
| 2379 | if (has_valid_frame() || then.is_linked()) { |
| 2380 | then.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2381 | LoadAndSpill(node->then_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2382 | } |
| 2383 | if (else_.is_linked()) { |
| 2384 | JumpTarget exit; |
| 2385 | if (has_valid_frame()) exit.Jump(); |
| 2386 | else_.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2387 | LoadAndSpill(node->else_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2388 | if (exit.is_linked()) exit.Bind(); |
| 2389 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2390 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2394 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2395 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2396 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2397 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2398 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2399 | JumpTarget slow; |
| 2400 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2427 | if (potential_slot->var()->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2428 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2429 | __ cmp(r0, ip); |
| 2430 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2431 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2432 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2433 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2434 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2435 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2439 | slow.Bind(); |
| 2440 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2441 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2442 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2443 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2444 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2445 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2446 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2447 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2448 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2449 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2450 | done.Bind(); |
| 2451 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2452 | |
| 2453 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2454 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2455 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2456 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2457 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 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"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2462 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2463 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2464 | __ cmp(r0, ip); |
| 2465 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2466 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2553 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2554 | TypeofState typeof_state, |
| 2555 | Register tmp, |
| 2556 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2557 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2568 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 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; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2583 | if (!context.is(tmp)) { |
| 2584 | __ mov(tmp, Operand(context)); |
| 2585 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2586 | __ bind(&next); |
| 2587 | // Terminate at global context. |
| 2588 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2589 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2590 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2591 | __ b(eq, &fast); |
| 2592 | // Check that extension is NULL. |
| 2593 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2594 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2595 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2609 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2610 | // Call IC stub. |
| 2611 | if (typeof_state == INSIDE_TYPEOF) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 2612 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2613 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 2614 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2617 | // Drop the global object. The result is in r0. |
| 2618 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2622 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2623 | #ifdef DEBUG |
| 2624 | int original_height = frame_->height(); |
| 2625 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2626 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2627 | Comment cmnt(masm_, "[ Slot"); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2628 | LoadFromSlot(node, NOT_INSIDE_TYPEOF); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2629 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2630 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2631 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2632 | |
| 2633 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2634 | #ifdef DEBUG |
| 2635 | int original_height = frame_->height(); |
| 2636 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2637 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2638 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2639 | |
| 2640 | Variable* var = node->var(); |
| 2641 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2642 | if (expr != NULL) { |
| 2643 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2644 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2645 | ASSERT(var->is_global()); |
| 2646 | Reference ref(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2647 | ref.GetValueAndSpill(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2648 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2649 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2653 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2654 | #ifdef DEBUG |
| 2655 | int original_height = frame_->height(); |
| 2656 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2657 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2658 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2659 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2660 | frame_->EmitPush(r0); |
| 2661 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2665 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2666 | #ifdef DEBUG |
| 2667 | int original_height = frame_->height(); |
| 2668 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2669 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2675 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2685 | JumpTarget done; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2686 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2687 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2688 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2689 | |
| 2690 | // If the entry is undefined we call the runtime system to computed |
| 2691 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2692 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2693 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2694 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2695 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2696 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2697 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2698 | frame_->EmitPush(r0); |
| 2699 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2700 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2701 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2702 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2703 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2704 | frame_->EmitPush(r2); |
| 2705 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2709 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2710 | #ifdef DEBUG |
| 2711 | int original_height = frame_->height(); |
| 2712 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2713 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2714 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2715 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2716 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2729 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2730 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2731 | // r0: created object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2738 | case ObjectLiteral::Property::CONSTANT: |
| 2739 | break; |
| 2740 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2741 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2742 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2743 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2744 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2745 | frame_->EmitPush(r0); // dup the result |
| 2746 | LoadAndSpill(key); |
| 2747 | LoadAndSpill(value); |
| 2748 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2749 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2750 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2751 | break; |
| 2752 | } |
| 2753 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2754 | frame_->EmitPush(r0); |
| 2755 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2756 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2757 | frame_->EmitPush(r0); |
| 2758 | LoadAndSpill(value); |
| 2759 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2760 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2761 | break; |
| 2762 | } |
| 2763 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2764 | frame_->EmitPush(r0); |
| 2765 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2766 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2767 | frame_->EmitPush(r0); |
| 2768 | LoadAndSpill(value); |
| 2769 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2770 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2771 | break; |
| 2772 | } |
| 2773 | } |
| 2774 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2775 | ASSERT(frame_->height() == original_height + 1); |
| 2776 | } |
| 2777 | |
| 2778 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2779 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2780 | #ifdef DEBUG |
| 2781 | int original_height = frame_->height(); |
| 2782 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2783 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2784 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2785 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2786 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2799 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2800 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2801 | // r0: created object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2802 | |
| 2803 | // Generate code to set the elements in the array that are not |
| 2804 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2805 | for (int i = 0; i < node->values()->length(); i++) { |
| 2806 | Expression* value = node->values()->at(i); |
| 2807 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 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; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2814 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2815 | // The property must be set by generated code. |
| 2816 | LoadAndSpill(value); |
| 2817 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2818 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2819 | // Fetch the object literal. |
| 2820 | __ ldr(r1, frame_->Top()); |
| 2821 | // Get the elements array. |
| 2822 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2823 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2824 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2825 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2831 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2832 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2836 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2837 | #ifdef DEBUG |
| 2838 | int original_height = frame_->height(); |
| 2839 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2840 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2841 | // Call runtime routine to allocate the catch extension object and |
| 2842 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2843 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2844 | LoadAndSpill(node->key()); |
| 2845 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2846 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2847 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2848 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2852 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2853 | #ifdef DEBUG |
| 2854 | int original_height = frame_->height(); |
| 2855 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2856 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2857 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2858 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 2859 | { Reference target(this, node->target(), node->is_compound()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +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 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2868 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2869 | if (node->op() == Token::ASSIGN || |
| 2870 | node->op() == Token::INIT_VAR || |
| 2871 | node->op() == Token::INIT_CONST) { |
| 2872 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2873 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 2874 | } else { // Assignment is a compound assignment. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2875 | // Get the old value of the lhs. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2876 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2877 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2878 | bool overwrite = |
| 2879 | (node->value()->AsBinaryOperation() != NULL && |
| 2880 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2881 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2882 | SmiOperation(node->binary_op(), |
| 2883 | literal->handle(), |
| 2884 | false, |
| 2885 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2886 | frame_->EmitPush(r0); |
| 2887 | |
| 2888 | } else { |
| 2889 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2890 | GenericBinaryOperation(node->binary_op(), |
| 2891 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2892 | frame_->EmitPush(r0); |
| 2893 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2894 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +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. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 2900 | UnloadReference(&target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2901 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2911 | } |
| 2912 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2913 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2917 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2918 | #ifdef DEBUG |
| 2919 | int original_height = frame_->height(); |
| 2920 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2921 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2922 | Comment cmnt(masm_, "[ Throw"); |
| 2923 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2924 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2925 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2926 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2927 | frame_->EmitPush(r0); |
| 2928 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2932 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2933 | #ifdef DEBUG |
| 2934 | int original_height = frame_->height(); |
| 2935 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2936 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2937 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2938 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2939 | { Reference property(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2940 | property.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2941 | } |
| 2942 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2946 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2947 | #ifdef DEBUG |
| 2948 | int original_height = frame_->height(); |
| 2949 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2950 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2951 | Comment cmnt(masm_, "[ Call"); |
| 2952 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2953 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2954 | ZoneList<Expression*>* args = node->arguments(); |
| 2955 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2956 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2957 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 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 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2998 | // Push the receiver. |
| 2999 | __ ldr(r1, frame_->Receiver()); |
| 3000 | frame_->EmitPush(r1); |
| 3001 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3002 | // Resolve the call. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3003 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3004 | |
| 3005 | // Touch up stack with the right values for the function and the receiver. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3006 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +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; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3013 | CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +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()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3027 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3028 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3029 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 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(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3034 | |
| 3035 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3036 | int arg_count = args->length(); |
| 3037 | for (int i = 0; i < arg_count; i++) { |
| 3038 | LoadAndSpill(args->at(i)); |
| 3039 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3040 | |
| 3041 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3042 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3043 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3044 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3045 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3046 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3047 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3048 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3049 | frame_->Drop(); |
| 3050 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3059 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3060 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3061 | frame_->EmitPush(r0); |
| 3062 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3063 | // r0: slot value; r1: receiver |
| 3064 | |
| 3065 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3066 | frame_->EmitPush(r0); // function |
| 3067 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3068 | |
| 3069 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3070 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3071 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3083 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3084 | frame_->EmitPush(r0); |
| 3085 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3086 | |
| 3087 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3088 | int arg_count = args->length(); |
| 3089 | for (int i = 0; i < arg_count; i++) { |
| 3090 | LoadAndSpill(args->at(i)); |
| 3091 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3092 | |
| 3093 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3094 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3095 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3096 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3097 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3098 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3099 | |
| 3100 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3101 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3102 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3103 | frame_->EmitPush(r0); // push after get rid of function from the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3104 | |
| 3105 | } else { |
| 3106 | // ------------------------------------------- |
| 3107 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3108 | // ------------------------------------------- |
| 3109 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3115 | if (property->is_synthetic()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3116 | // Use the global receiver. |
| 3117 | frame_->Drop(); |
| 3118 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3119 | LoadGlobalReceiver(r0); |
| 3120 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3121 | frame_->EmitPop(r1); // receiver |
| 3122 | frame_->EmitPush(r0); // function |
| 3123 | frame_->EmitPush(r1); // receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3126 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3127 | CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3128 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | } else { |
| 3132 | // ---------------------------------- |
| 3133 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3134 | // ---------------------------------- |
| 3135 | |
| 3136 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3137 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3138 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3139 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3140 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3141 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3142 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3143 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3144 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3145 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3146 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3150 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3151 | #ifdef DEBUG |
| 3152 | int original_height = frame_->height(); |
| 3153 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3154 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3164 | // receiver. There is no need to use the global proxy here because |
| 3165 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3166 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3167 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3168 | |
| 3169 | // Push the arguments ("left-to-right") on the stack. |
| 3170 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3171 | int arg_count = args->length(); |
| 3172 | for (int i = 0; i < arg_count; i++) { |
| 3173 | LoadAndSpill(args->at(i)); |
| 3174 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3175 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3176 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3177 | __ mov(r0, Operand(arg_count)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3178 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3179 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3180 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3181 | // Call the construct call builtin that handles allocation and |
| 3182 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3183 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3184 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3185 | frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3186 | |
| 3187 | // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)). |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3188 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3189 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3193 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3194 | VirtualFrame::SpilledScope spilled_scope; |
| 3195 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3228 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 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(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3245 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3246 | frame_->EmitPush(r0); |
| 3247 | |
| 3248 | // All done. |
| 3249 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3253 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3254 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3255 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3256 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3257 | LoadAndSpill(args->at(0)); |
| 3258 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3259 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3260 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3261 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3262 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3263 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3264 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3265 | // Load the value. |
| 3266 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3267 | leave.Bind(); |
| 3268 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3272 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3273 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3274 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3275 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3280 | // if (object->IsSmi()) return object. |
| 3281 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3282 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3283 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3284 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3285 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3292 | leave.Bind(); |
| 3293 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3297 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3298 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3299 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3300 | LoadAndSpill(args->at(0)); |
| 3301 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3302 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3303 | cc_reg_ = eq; |
| 3304 | } |
| 3305 | |
| 3306 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3307 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3308 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3309 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3310 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3311 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3312 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3313 | LoadAndSpill(args->at(1)); |
| 3314 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3315 | __ CallRuntime(Runtime::kLog, 2); |
| 3316 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3317 | #endif |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3318 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3319 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3323 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3324 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3325 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3326 | LoadAndSpill(args->at(0)); |
| 3327 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3328 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3329 | cc_reg_ = eq; |
| 3330 | } |
| 3331 | |
| 3332 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 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. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3336 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3337 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3338 | ASSERT(args->length() == 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 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); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3409 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3410 | |
| 3411 | __ bind(&end); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3412 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3416 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3417 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3418 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3419 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3420 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3424 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3425 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3426 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3427 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3430 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3431 | cc_reg_ = eq; |
| 3432 | } |
| 3433 | |
| 3434 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +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 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3494 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3495 | VirtualFrame::SpilledScope spilled_scope; |
| 3496 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 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)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3504 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 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; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3516 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3517 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3518 | ASSERT(args->length() == 0); |
| 3519 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3522 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3523 | |
| 3524 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3525 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3526 | frame_->CallStub(&stub, 0); |
| 3527 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3531 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3532 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3533 | ASSERT(args->length() == 1); |
| 3534 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3535 | // Satisfy contract with ArgumentsAccessStub: |
| 3536 | // Load the key into r1 and the formal parameters count into r0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3537 | LoadAndSpill(args->at(0)); |
| 3538 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3539 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3540 | |
| 3541 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3542 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3543 | frame_->CallStub(&stub, 0); |
| 3544 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +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 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3586 | StringCompareStub stub; |
| 3587 | frame_->CallStub(&stub, 2); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3605 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3606 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3607 | ASSERT(args->length() == 2); |
| 3608 | |
| 3609 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3610 | LoadAndSpill(args->at(0)); |
| 3611 | LoadAndSpill(args->at(1)); |
| 3612 | frame_->EmitPop(r0); |
| 3613 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3614 | __ cmp(r0, Operand(r1)); |
| 3615 | cc_reg_ = eq; |
| 3616 | } |
| 3617 | |
| 3618 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3619 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3620 | #ifdef DEBUG |
| 3621 | int original_height = frame_->height(); |
| 3622 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3623 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3624 | if (CheckForInlineRuntimeCall(node)) { |
| 3625 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3626 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3627 | return; |
| 3628 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3629 | |
| 3630 | ZoneList<Expression*>* args = node->arguments(); |
| 3631 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3632 | Runtime::Function* function = node->function(); |
| 3633 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3634 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3635 | // Prepare stack for calling JS runtime function. |
| 3636 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3637 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3638 | // Push the builtins object found in the current global object. |
| 3639 | __ ldr(r1, GlobalObject()); |
| 3640 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3641 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3642 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3643 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 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 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3649 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3650 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3651 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3652 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3653 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3654 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3655 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3656 | frame_->Drop(); |
| 3657 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3658 | } else { |
| 3659 | // Call the C runtime function. |
| 3660 | frame_->CallRuntime(function, arg_count); |
| 3661 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3662 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3663 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3667 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3668 | #ifdef DEBUG |
| 3669 | int original_height = frame_->height(); |
| 3670 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3671 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3672 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3673 | |
| 3674 | Token::Value op = node->op(); |
| 3675 | |
| 3676 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3677 | LoadConditionAndSpill(node->expression(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3678 | false_target(), |
| 3679 | true_target(), |
| 3680 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 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_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3684 | |
| 3685 | } else if (op == Token::DELETE) { |
| 3686 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3687 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3688 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3689 | LoadAndSpill(property->obj()); |
| 3690 | LoadAndSpill(property->key()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3691 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3692 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3693 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3694 | Slot* slot = variable->slot(); |
| 3695 | if (variable->is_global()) { |
| 3696 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3697 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3698 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3699 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3700 | |
| 3701 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3702 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3703 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3704 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3705 | frame_->EmitPush(r0); |
| 3706 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3707 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3708 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3709 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3710 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3711 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3712 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3713 | } else { |
| 3714 | // Default: Result of deleting non-global, not dynamically |
| 3715 | // introduced variables is false. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3716 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3717 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3718 | |
| 3719 | } else { |
| 3720 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3721 | LoadAndSpill(node->expression()); // may have side-effects |
| 3722 | frame_->Drop(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3723 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3724 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3725 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3726 | |
| 3727 | } else if (op == Token::TYPEOF) { |
| 3728 | // Special case for loading the typeof expression; see comment on |
| 3729 | // LoadTypeofExpression(). |
| 3730 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3731 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3732 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3733 | |
| 3734 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3735 | bool overwrite = |
| 3736 | (node->expression()->AsBinaryOperation() != NULL && |
| 3737 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3738 | LoadAndSpill(node->expression()); |
| 3739 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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: { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3748 | GenericUnaryOpStub stub(Token::SUB, overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3749 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3750 | break; |
| 3751 | } |
| 3752 | |
| 3753 | case Token::BIT_NOT: { |
| 3754 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3755 | JumpTarget smi_label; |
| 3756 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3757 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3758 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3759 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3760 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 3761 | frame_->CallStub(&stub, 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3762 | continue_label.Jump(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 3763 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3764 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3765 | __ mvn(r0, Operand(r0)); |
| 3766 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3767 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3774 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3775 | break; |
| 3776 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3777 | case Token::ADD: { |
| 3778 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3779 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3780 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3781 | continue_label.Branch(eq); |
| 3782 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3783 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3784 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3785 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3786 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3787 | default: |
| 3788 | UNREACHABLE(); |
| 3789 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3790 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3791 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3792 | ASSERT(!has_valid_frame() || |
| 3793 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3794 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3798 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3799 | #ifdef DEBUG |
| 3800 | int original_height = frame_->height(); |
| 3801 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3802 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3812 | if (is_postfix) { |
| 3813 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3814 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3815 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3816 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +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 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3830 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3831 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3832 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3833 | JumpTarget slow; |
| 3834 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3841 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3842 | |
| 3843 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3844 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3845 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3846 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3856 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3866 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3867 | { |
| 3868 | // Convert the operand to a number. |
| 3869 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3870 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3871 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3872 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3873 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3874 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3877 | // Compute the new value. |
| 3878 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3879 | frame_->EmitPush(r0); |
| 3880 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3881 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3882 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3883 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3884 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3888 | exit.Bind(); |
| 3889 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3890 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3894 | if (is_postfix) frame_->EmitPop(r0); |
| 3895 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
| 3898 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3899 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3900 | #ifdef DEBUG |
| 3901 | int original_height = frame_->height(); |
| 3902 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3903 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3920 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3921 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3922 | &is_true, |
| 3923 | false_target(), |
| 3924 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3925 | if (has_valid_frame() && !has_cc()) { |
| 3926 | // The left-hand side result is on top of the virtual frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3927 | JumpTarget pop_and_continue; |
| 3928 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3929 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3930 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3931 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3935 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3936 | Branch(false, &exit); |
| 3937 | |
| 3938 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3939 | pop_and_continue.Bind(); |
| 3940 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3941 | |
| 3942 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3943 | is_true.Bind(); |
| 3944 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3945 | |
| 3946 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3947 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 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(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +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()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3966 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3967 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3968 | true_target(), |
| 3969 | &is_false, |
| 3970 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3971 | if (has_valid_frame() && !has_cc()) { |
| 3972 | // The left-hand side result is on top of the virtual frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3973 | JumpTarget pop_and_continue; |
| 3974 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3975 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3976 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3977 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3981 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3982 | Branch(true, &exit); |
| 3983 | |
| 3984 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3985 | pop_and_continue.Bind(); |
| 3986 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3987 | |
| 3988 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3989 | is_false.Bind(); |
| 3990 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3991 | |
| 3992 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3993 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 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(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +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()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 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()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4024 | |
| 4025 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4026 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4027 | SmiOperation(node->op(), |
| 4028 | rliteral->handle(), |
| 4029 | false, |
| 4030 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4031 | |
| 4032 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4033 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4034 | SmiOperation(node->op(), |
| 4035 | lliteral->handle(), |
| 4036 | true, |
| 4037 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4038 | |
| 4039 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4046 | LoadAndSpill(node->left()); |
| 4047 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4048 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4049 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4050 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4051 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4052 | ASSERT(!has_valid_frame() || |
| 4053 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4054 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4058 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4059 | #ifdef DEBUG |
| 4060 | int original_height = frame_->height(); |
| 4061 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4062 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4063 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4064 | frame_->EmitPush(r0); |
| 4065 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4069 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4070 | #ifdef DEBUG |
| 4071 | int original_height = frame_->height(); |
| 4072 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4073 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4085 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4086 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4087 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4088 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4089 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4090 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4091 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4092 | LoadAndSpill(left_is_null ? right : left); |
| 4093 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4094 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4095 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4096 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4097 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4098 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4099 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4100 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4101 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4102 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4103 | __ cmp(r0, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4104 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4105 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4106 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4107 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4108 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 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)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4116 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4117 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4118 | return; |
| 4119 | } |
| 4120 | } |
| 4121 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 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>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4132 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4133 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4134 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4135 | |
| 4136 | if (check->Equals(Heap::number_symbol())) { |
| 4137 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4138 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4139 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4140 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 4141 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4142 | cc_reg_ = eq; |
| 4143 | |
| 4144 | } else if (check->Equals(Heap::string_symbol())) { |
| 4145 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4146 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4147 | |
| 4148 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4149 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4150 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4151 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4152 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4153 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4154 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4161 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 4162 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4163 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4164 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 4165 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4166 | cc_reg_ = eq; |
| 4167 | |
| 4168 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4169 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4170 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4171 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4172 | |
| 4173 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4174 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4175 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4176 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4186 | false_target()->Branch(eq); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4192 | cc_reg_ = eq; |
| 4193 | |
| 4194 | } else if (check->Equals(Heap::object_symbol())) { |
| 4195 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4196 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4197 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4198 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4199 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4200 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4201 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4202 | Register map_reg = r2; |
| 4203 | __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE); |
| 4204 | false_target()->Branch(eq); |
| 4205 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4206 | // It can be an undetectable object. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4207 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4208 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4209 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4210 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4211 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4212 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4213 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4214 | false_target()->Branch(lt); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4215 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4216 | cc_reg_ = le; |
| 4217 | |
| 4218 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4219 | // Uncommon case: typeof testing against a string literal that is |
| 4220 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4221 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4222 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4223 | ASSERT(!has_valid_frame() || |
| 4224 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4225 | return; |
| 4226 | } |
| 4227 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4228 | switch (op) { |
| 4229 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4230 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4231 | break; |
| 4232 | |
| 4233 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4234 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4235 | break; |
| 4236 | |
| 4237 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4238 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4239 | break; |
| 4240 | |
| 4241 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4242 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4243 | break; |
| 4244 | |
| 4245 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4246 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4247 | break; |
| 4248 | |
| 4249 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4250 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4251 | break; |
| 4252 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4253 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4254 | LoadAndSpill(left); |
| 4255 | LoadAndSpill(right); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4256 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4257 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4258 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4259 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4260 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4261 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4262 | LoadAndSpill(left); |
| 4263 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4264 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4265 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4266 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4267 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4268 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4269 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4270 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4271 | |
| 4272 | default: |
| 4273 | UNREACHABLE(); |
| 4274 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4275 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4276 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4290 | #ifdef DEBUG |
| 4291 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4292 | #endif |
| 4293 | |
| 4294 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4295 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4296 | #define __ ACCESS_MASM(masm) |
| 4297 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4298 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4314 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4315 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4316 | void Reference::GetValue() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4317 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4318 | ASSERT(!is_illegal()); |
| 4319 | ASSERT(!cgen_->has_cc()); |
| 4320 | MacroAssembler* masm = cgen_->masm(); |
| 4321 | Property* property = expression_->AsProperty(); |
| 4322 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4323 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4331 | cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4332 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4335 | case NAMED: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4336 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4337 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4338 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4339 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4340 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4341 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4342 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4343 | ASSERT(var == NULL || var->is_global()); |
| 4344 | RelocInfo::Mode rmode = (var == NULL) |
| 4345 | ? RelocInfo::CODE_TARGET |
| 4346 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4347 | frame->CallCodeObject(ic, rmode, 0); |
| 4348 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4349 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4352 | case KEYED: { |
| 4353 | // TODO(181): Implement inlined version of array indexing once |
| 4354 | // loop nesting is properly tracked on ARM. |
| 4355 | ASSERT(property != NULL); |
| 4356 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 4357 | ASSERT(var == NULL || var->is_global()); |
| 4358 | cgen_->EmitKeyedLoad(var != NULL); |
| 4359 | cgen_->frame()->EmitPush(r0); |
| 4360 | break; |
| 4361 | } |
| 4362 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4363 | default: |
| 4364 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4365 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4366 | |
| 4367 | if (!persist_after_get_) { |
| 4368 | cgen_->UnloadReference(this); |
| 4369 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4373 | void Reference::SetValue(InitState init_state) { |
| 4374 | ASSERT(!is_illegal()); |
| 4375 | ASSERT(!cgen_->has_cc()); |
| 4376 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4377 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4378 | Property* property = expression_->AsProperty(); |
| 4379 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4380 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4381 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4382 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4383 | switch (type_) { |
| 4384 | case SLOT: { |
| 4385 | Comment cmnt(masm, "[ Store to Slot"); |
| 4386 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4387 | cgen_->StoreToSlot(slot, init_state); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4388 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4389 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4392 | case NAMED: { |
| 4393 | Comment cmnt(masm, "[ Store to named Property"); |
| 4394 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4395 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4396 | Handle<String> name(GetName()); |
| 4397 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4398 | frame->EmitPop(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4399 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4400 | __ mov(r2, Operand(name)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4401 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4402 | frame->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4403 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4404 | break; |
| 4405 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4406 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4407 | case KEYED: { |
| 4408 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4409 | Property* property = expression_->AsProperty(); |
| 4410 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4411 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4416 | frame->EmitPop(r0); // value |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4417 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4418 | frame->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4419 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4420 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4421 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4422 | |
| 4423 | default: |
| 4424 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4534 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4556 | __ tst(scratch, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4702 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4708 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 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, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4752 | Condition cc, |
| 4753 | bool never_nan_nan) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4754 | Label not_identical; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4755 | Label heap_number, return_equal; |
| 4756 | Register exp_mask_reg = r5; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4757 | __ cmp(r0, Operand(r1)); |
| 4758 | __ b(ne, ¬_identical); |
| 4759 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4764 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4771 | __ b(ge, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4796 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4797 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4798 | } |
| 4799 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4800 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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 { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4807 | __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4808 | } |
| 4809 | __ mov(pc, Operand(lr)); // Return. |
| 4810 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4819 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4828 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 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) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 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. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4846 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4847 | __ mov(pc, Operand(lr)); // Return. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4848 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4849 | // No fall through here. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4850 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4851 | |
| 4852 | __ bind(¬_identical); |
| 4853 | } |
| 4854 | |
| 4855 | |
| 4856 | // See comment at call site. |
| 4857 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4858 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4859 | Label* slow, |
| 4860 | bool strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4861 | Label rhs_is_smi; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4862 | __ tst(r0, Operand(kSmiTagMask)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4863 | __ b(eq, &rhs_is_smi); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4864 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4865 | // Lhs is a Smi. Check whether the rhs is a heap number. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4866 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4867 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4868 | // If rhs is not a number and lhs is a Smi then strict equality cannot |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4877 | // Lhs (r1) is a smi, rhs (r0) is a number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4878 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4879 | // Convert lhs to a double in d7 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4880 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4887 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4888 | __ push(lr); |
| 4889 | // Convert lhs to a double in r2, r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4890 | __ mov(r7, Operand(r1)); |
| 4891 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4892 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4899 | // We now have both loaded as doubles but we can skip the lhs nan check |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4900 | // since it's a smi. |
| 4901 | __ jmp(lhs_not_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4902 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4903 | __ bind(&rhs_is_smi); |
| 4904 | // Rhs is a smi. Check whether the non-smi lhs is a heap number. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4905 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4906 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4907 | // If lhs is not a number and rhs is a smi then strict equality cannot |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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 { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4912 | // Smi compared non-strictly with a non-smi non-heap-number. Call |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4913 | // the runtime. |
| 4914 | __ b(ne, slow); |
| 4915 | } |
| 4916 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4917 | // Rhs (r0) is a smi, lhs (r1) is a heap number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4918 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4919 | // Convert rhs to a double in d6 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4920 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4927 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4933 | __ mov(r7, Operand(r0)); |
| 4934 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4935 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4936 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4937 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4938 | // Fall through to both_loaded_as_doubles. |
| 4939 | } |
| 4940 | |
| 4941 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4942 | void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4943 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4948 | Label one_is_nan, neither_is_nan; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4949 | Label lhs_not_nan_exp_mask_is_loaded; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4950 | |
| 4951 | Register exp_mask_reg = r5; |
| 4952 | |
| 4953 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4954 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 4955 | __ cmp(r4, Operand(exp_mask_reg)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 4956 | __ b(ne, &lhs_not_nan_exp_mask_is_loaded); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5003 | __ cmp(rhs_mantissa, Operand(lhs_mantissa)); |
| 5004 | __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5005 | // Return non-zero if the numbers are unequal. |
| 5006 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 5007 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5008 | __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5018 | __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5023 | __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +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); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5078 | __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5079 | __ b(ne, not_heap_numbers); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5080 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5081 | __ cmp(r2, r3); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5105 | // Ensure that no non-strings have the symbol bit set. |
| 5106 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5107 | ASSERT(kSymbolTag != 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5108 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5109 | __ b(eq, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5110 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5111 | __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5125 | void CompareStub::Generate(MacroAssembler* masm) { |
| 5126 | Label slow; // Call builtin. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5127 | Label not_smis, both_loaded_as_doubles, lhs_not_nan; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5134 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5147 | // 4) Jump to lhs_not_nan. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5148 | // In cases 3 and 4 we have found out we were dealing with a number-number |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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. |
| 5152 | EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5153 | |
| 5154 | __ bind(&both_loaded_as_doubles); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5157 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5158 | __ bind(&lhs_not_nan); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5159 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5160 | Label no_nan; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5161 | // ARMv7 VFP3 instructions to implement double precision comparison. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5170 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 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 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5180 | __ mov(pc, Operand(lr)); |
| 5181 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5200 | Label flat_string_check; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5204 | // In this case r2 will contain the type of r0. Never falls through. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5205 | EmitCheckForTwoHeapNumbers(masm, |
| 5206 | &both_loaded_as_doubles, |
| 5207 | &check_for_symbols, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5208 | &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5209 | |
| 5210 | __ bind(&check_for_symbols); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5211 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 5212 | // symbols. |
| 5213 | if (cc_ == eq && !strict_) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5214 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5215 | // of r0 on entry. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5216 | EmitCheckForSymbols(masm, &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5235 | __ bind(&slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5236 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5237 | __ push(r1); |
| 5238 | __ push(r0); |
| 5239 | // Figure out which native to call and setup the arguments. |
| 5240 | Builtins::JavaScript native; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +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. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5258 | __ InvokeBuiltin(native, JUMP_JS); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5262 | // Allocates a heap number or jumps to the label if the young space is full and |
| 5263 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5264 | static void AllocateHeapNumber( |
| 5265 | MacroAssembler* masm, |
| 5266 | Label* need_gc, // Jump here if young space is full. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5267 | Register result, // The tagged address of the new heap number. |
| 5268 | Register scratch1, // A scratch register. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5269 | Register scratch2) { // Another scratch register. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5270 | // Allocate an object in the heap for the heap number and tag it as a heap |
| 5271 | // object. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5272 | __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize, |
| 5273 | result, |
| 5274 | scratch1, |
| 5275 | scratch2, |
| 5276 | need_gc, |
| 5277 | TAG_OBJECT); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5278 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5279 | // Get heap number map and store it in the allocated object. |
| 5280 | __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex); |
| 5281 | __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 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 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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). |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5291 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 5292 | Label* not_smi, |
| 5293 | const Builtins::JavaScript& builtin, |
| 5294 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5295 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5302 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5308 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5332 | __ bind(&slow); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5333 | |
| 5334 | // Push arguments to the stack |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5335 | __ push(r1); |
| 5336 | __ push(r0); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +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); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 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); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5373 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 5374 | |
| 5375 | __ bind(¬_strings); |
| 5376 | } |
| 5377 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5378 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5379 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5380 | // We branch here if at least one of r0 and r1 is not a Smi. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5381 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5389 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5392 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5393 | if (mode == OVERWRITE_RIGHT) { |
| 5394 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5395 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5410 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5411 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5412 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5413 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5414 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5437 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5454 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5455 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5456 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5470 | __ bind(&finished_loading_r1); |
| 5471 | |
| 5472 | __ bind(&do_the_call); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5476 | CpuFeatures::Scope scope(VFP3); |
| 5477 | // ARMv7 VFP3 instructions to implement |
| 5478 | // double precision, add, subtract, multiply, divide. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5479 | |
| 5480 | if (Token::MUL == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5481 | __ vmul(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5482 | } else if (Token::DIV == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5483 | __ vdiv(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5484 | } else if (Token::ADD == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5485 | __ vadd(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5486 | } else if (Token::SUB == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5487 | __ vsub(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5488 | } else { |
| 5489 | UNREACHABLE(); |
| 5490 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 5491 | __ sub(r0, r5, Operand(kHeapObjectTag)); |
| 5492 | __ vstr(d5, r0, HeapNumber::kValueOffset); |
| 5493 | __ add(r0, r0, Operand(kHeapObjectTag)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5494 | __ mov(pc, lr); |
| 5495 | return; |
| 5496 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5505 | __ push(lr); // For later. |
| 5506 | __ push(r5); // Address of heap number that is answer. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5507 | __ AlignStack(0); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5508 | // Call C routine that may not cause GC or other trouble. |
| 5509 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5510 | __ Call(r5); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5514 | // Store answer in the overwritable heap number. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5515 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 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 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5522 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5523 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5524 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5525 | #endif |
| 5526 | __ mov(r0, Operand(r4)); |
| 5527 | // And we are done. |
| 5528 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5532 | // Tries to get a signed int32 out of a double precision floating point heap |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5533 | // number. Rounds towards 0. Fastest for doubles that are in the ranges |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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 |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5536 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5537 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5538 | static void GetInt32(MacroAssembler* masm, |
| 5539 | Register source, |
| 5540 | Register dest, |
| 5541 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5542 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5543 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5544 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5545 | // Get exponent word. |
| 5546 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5547 | // Get exponent alone in scratch2. |
| 5548 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5549 | // Load dest with zero. We use this either for the final shift or |
| 5550 | // for the answer. |
| 5551 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5552 | // Check whether the exponent matches a 32 bit signed int that is not a Smi. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 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. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5556 | const uint32_t non_smi_exponent = |
| 5557 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5558 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5580 | __ bind(&right_exponent); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5586 | __ vmov(d7, scratch2, scratch); |
| 5587 | __ vcvt(s15, d7); |
| 5588 | __ vmov(dest, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5613 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5632 | GetInt32(masm, r1, r3, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5642 | GetInt32(masm, r0, r2, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +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 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 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 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +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 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5854 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5855 | // r1 : x |
| 5856 | // r0 : y |
| 5857 | // result : r0 |
| 5858 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5863 | switch (op_) { |
| 5864 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5865 | Label not_smi; |
| 5866 | // Fast path. |
| 5867 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5868 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 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, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5879 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5880 | break; |
| 5881 | } |
| 5882 | |
| 5883 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5884 | Label not_smi; |
| 5885 | // Fast path. |
| 5886 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5887 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 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, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5898 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5899 | break; |
| 5900 | } |
| 5901 | |
| 5902 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5903 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5904 | ASSERT(kSmiTag == 0); // adjust code below |
| 5905 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5906 | __ b(ne, ¬_smi); |
| 5907 | // Remove tag from one operand (but keep sign), so that result is Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5908 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 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). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5912 | __ mov(ip, Operand(r3, ASR, 31)); |
| 5913 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 5914 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5915 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5916 | __ tst(r3, Operand(r3)); |
| 5917 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5918 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5926 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5927 | |
| 5928 | HandleBinaryOpSlowCases(masm, |
| 5929 | ¬_smi, |
| 5930 | Builtins::MUL, |
| 5931 | Token::MUL, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 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_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6027 | break; |
| 6028 | } |
| 6029 | |
| 6030 | case Token::BIT_OR: |
| 6031 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6032 | case Token::BIT_XOR: |
| 6033 | case Token::SAR: |
| 6034 | case Token::SHR: |
| 6035 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6036 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6044 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6051 | __ bic(r0, r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6052 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6053 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6060 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6064 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6065 | // Smi tag result. |
| 6066 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6067 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6068 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6074 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6075 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6076 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 6077 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6078 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6079 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6080 | default: UNREACHABLE(); |
| 6081 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6082 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6083 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6084 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6085 | break; |
| 6086 | } |
| 6087 | |
| 6088 | default: UNREACHABLE(); |
| 6089 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6090 | // This code should be unreachable. |
| 6091 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | |
| 6095 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 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))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6099 | __ push(r0); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6100 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6101 | |
| 6102 | __ StubReturn(1); |
| 6103 | } |
| 6104 | |
| 6105 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6106 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 6107 | Label slow, done; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6108 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6114 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6119 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6124 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 6125 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 6126 | __ b(&done); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6127 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6182 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6183 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 6184 | // Handle the slow case by jumping to the JavaScript builtin. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6185 | __ bind(&slow); |
| 6186 | __ push(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6196 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6200 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6207 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 6208 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6209 | |
| 6210 | // Restore the next handler and frame pointer, discard handler state. |
| 6211 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 6212 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6213 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6225 | #ifdef DEBUG |
| 6226 | if (FLAG_debug_code) { |
| 6227 | __ mov(lr, Operand(pc)); |
| 6228 | } |
| 6229 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6230 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6231 | __ pop(pc); |
| 6232 | } |
| 6233 | |
| 6234 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6235 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 6236 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6237 | // Adjust this code if not the case. |
| 6238 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6239 | |
| 6240 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6241 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6242 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6248 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 6249 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6250 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 6251 | __ b(eq, &done); |
| 6252 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6253 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 6254 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6255 | __ jmp(&loop); |
| 6256 | __ bind(&done); |
| 6257 | |
| 6258 | // Set the top handler address to next handler past the current ENTRY handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6259 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6260 | __ pop(r2); |
| 6261 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6262 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6269 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6276 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6277 | // Stack layout at this point. See also StackHandlerConstants. |
| 6278 | // sp -> state (ENTRY) |
| 6279 | // fp |
| 6280 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6281 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6293 | #ifdef DEBUG |
| 6294 | if (FLAG_debug_code) { |
| 6295 | __ mov(lr, Operand(pc)); |
| 6296 | } |
| 6297 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6298 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6299 | __ pop(pc); |
| 6300 | } |
| 6301 | |
| 6302 | |
| 6303 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 6304 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6305 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6306 | Label* throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6307 | bool do_gc, |
| 6308 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6316 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 6317 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6318 | } |
| 6319 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6341 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 6342 | masm->push(lr); |
| 6343 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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 |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 6366 | __ LeaveExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +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 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6379 | |
| 6380 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6381 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6382 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6383 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6384 | __ ldr(r0, MemOperand(ip)); |
| 6385 | __ str(r3, MemOperand(ip)); |
| 6386 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 6399 | void CEntryStub::Generate(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +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) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6404 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6405 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6406 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 6407 | // Result returned in r0 or r0+r1 by default. |
| 6408 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +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 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6414 | // Enter the exit frame that transitions from JavaScript to C++. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 6415 | __ EnterExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +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 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6421 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6422 | Label throw_termination_exception; |
| 6423 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6424 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6425 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6426 | GenerateCore(masm, |
| 6427 | &throw_normal_exception, |
| 6428 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6429 | &throw_out_of_memory_exception, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6430 | false, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6431 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6432 | |
| 6433 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6434 | GenerateCore(masm, |
| 6435 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6436 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6437 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +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, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6446 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6447 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6448 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6449 | true); |
| 6450 | |
| 6451 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6452 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 6453 | |
| 6454 | __ bind(&throw_termination_exception); |
| 6455 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6473 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 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 |
| 6481 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 6482 | __ ldr(r4, MemOperand(r4)); // argv |
| 6483 | |
| 6484 | // Push a frame with special values setup to mark it as an entry frame. |
| 6485 | // r0: code entry |
| 6486 | // r1: function |
| 6487 | // r2: receiver |
| 6488 | // r3: argc |
| 6489 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6490 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6491 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 6492 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6493 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 6494 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6495 | __ ldr(r5, MemOperand(r5)); |
| 6496 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 6497 | |
| 6498 | // Setup frame pointer for the frame to be pushed. |
| 6499 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6500 | |
| 6501 | // Call a faked try-block that does the invoke. |
| 6502 | __ bl(&invoke); |
| 6503 | |
| 6504 | // Caught exception: Store result (exception) in the pending |
| 6505 | // exception field in the JSEnv and return a failure sentinel. |
| 6506 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 6507 | // sets it to 0 to signal the existence of the JSEntry frame. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6508 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6509 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6510 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6511 | __ b(&exit); |
| 6512 | |
| 6513 | // Invoke: Link this frame into the handler chain. |
| 6514 | __ bind(&invoke); |
| 6515 | // Must preserve r0-r4, r5-r7 are available. |
| 6516 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6517 | // If an exception not caught by another handler occurs, this handler |
| 6518 | // returns control to the code after the bl(&invoke) above, which |
| 6519 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 6520 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6521 | |
| 6522 | // Clear any pending exceptions. |
| 6523 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6524 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6525 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6526 | __ str(r5, MemOperand(ip)); |
| 6527 | |
| 6528 | // Invoke the function by calling through JS entry trampoline builtin. |
| 6529 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6530 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6531 | |
| 6532 | // Expected registers by Builtins::JSEntryTrampoline |
| 6533 | // r0: code entry |
| 6534 | // r1: function |
| 6535 | // r2: receiver |
| 6536 | // r3: argc |
| 6537 | // r4: argv |
| 6538 | if (is_construct) { |
| 6539 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6540 | __ mov(ip, Operand(construct_entry)); |
| 6541 | } else { |
| 6542 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6543 | __ mov(ip, Operand(entry)); |
| 6544 | } |
| 6545 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6546 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6547 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6548 | // macro for the add instruction because we don't want the coverage tool |
| 6549 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6550 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6551 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6552 | |
| 6553 | // Unlink this frame from the handler chain. When reading the |
| 6554 | // address of the next handler, there is no need to use the address |
| 6555 | // displacement since the current stack pointer (sp) points directly |
| 6556 | // to the stack handler. |
| 6557 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6558 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6559 | __ str(r3, MemOperand(ip)); |
| 6560 | // No need to restore registers |
| 6561 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6562 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6563 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6564 | __ bind(&exit); // r0 holds result |
| 6565 | // Restore the top frame descriptors from the stack. |
| 6566 | __ pop(r3); |
| 6567 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6568 | __ str(r3, MemOperand(ip)); |
| 6569 | |
| 6570 | // Reset the stack to the callee saved registers. |
| 6571 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6572 | |
| 6573 | // Restore callee-saved registers and return. |
| 6574 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6575 | if (FLAG_debug_code) { |
| 6576 | __ mov(lr, Operand(pc)); |
| 6577 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6578 | #endif |
| 6579 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6580 | } |
| 6581 | |
| 6582 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6583 | // This stub performs an instanceof, calling the builtin function if |
| 6584 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6585 | // be an instance of (these are fetched from the stack). |
| 6586 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6587 | // Get the object - slow case for smis (we may need to throw an exception |
| 6588 | // depending on the rhs). |
| 6589 | Label slow, loop, is_instance, is_not_instance; |
| 6590 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6591 | __ BranchOnSmi(r0, &slow); |
| 6592 | |
| 6593 | // Check that the left hand is a JS object and put map in r3. |
| 6594 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6595 | __ b(lt, &slow); |
| 6596 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6597 | __ b(gt, &slow); |
| 6598 | |
| 6599 | // Get the prototype of the function (r4 is result, r2 is scratch). |
| 6600 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 6601 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6602 | |
| 6603 | // Check that the function prototype is a JS object. |
| 6604 | __ BranchOnSmi(r4, &slow); |
| 6605 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6606 | __ b(lt, &slow); |
| 6607 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6608 | __ b(gt, &slow); |
| 6609 | |
| 6610 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6611 | // Get prototype of object into r2. |
| 6612 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6613 | |
| 6614 | // Loop through the prototype chain looking for the function prototype. |
| 6615 | __ bind(&loop); |
| 6616 | __ cmp(r2, Operand(r4)); |
| 6617 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6618 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 6619 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6620 | __ b(eq, &is_not_instance); |
| 6621 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6622 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6623 | __ jmp(&loop); |
| 6624 | |
| 6625 | __ bind(&is_instance); |
| 6626 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6627 | __ pop(); |
| 6628 | __ pop(); |
| 6629 | __ mov(pc, Operand(lr)); // Return. |
| 6630 | |
| 6631 | __ bind(&is_not_instance); |
| 6632 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6633 | __ pop(); |
| 6634 | __ pop(); |
| 6635 | __ mov(pc, Operand(lr)); // Return. |
| 6636 | |
| 6637 | // Slow-case. Tail call builtin. |
| 6638 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6639 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6640 | } |
| 6641 | |
| 6642 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6643 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6644 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6645 | Label adaptor; |
| 6646 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6647 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6648 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6649 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6650 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6651 | // Nothing to do: The formal number of parameters has already been |
| 6652 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6653 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6654 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6655 | // Arguments adaptor case: Read the arguments length from the |
| 6656 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6657 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6658 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6659 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6660 | } |
| 6661 | |
| 6662 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6663 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6664 | // The displacement is the offset of the last parameter (if any) |
| 6665 | // relative to the frame pointer. |
| 6666 | static const int kDisplacement = |
| 6667 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6668 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6669 | // Check that the key is a smi. |
| 6670 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6671 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6672 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6673 | // Check if the calling frame is an arguments adaptor frame. |
| 6674 | Label adaptor; |
| 6675 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6676 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6677 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6678 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6679 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6680 | // Check index against formal parameters count limit passed in |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6681 | // through register r0. Use unsigned comparison to get negative |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6682 | // check for free. |
| 6683 | __ cmp(r1, r0); |
| 6684 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6685 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6686 | // Read the argument from the stack and return it. |
| 6687 | __ sub(r3, r0, r1); |
| 6688 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6689 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6690 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6691 | |
| 6692 | // Arguments adaptor case: Check index against actual arguments |
| 6693 | // limit found in the arguments adaptor frame. Use unsigned |
| 6694 | // comparison to get negative check for free. |
| 6695 | __ bind(&adaptor); |
| 6696 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6697 | __ cmp(r1, r0); |
| 6698 | __ b(cs, &slow); |
| 6699 | |
| 6700 | // Read the argument from the adaptor frame and return it. |
| 6701 | __ sub(r3, r0, r1); |
| 6702 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6703 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6704 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6705 | |
| 6706 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6707 | // by calling the runtime system. |
| 6708 | __ bind(&slow); |
| 6709 | __ push(r1); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6710 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | |
| 6714 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 6715 | // Check if the calling frame is an arguments adaptor frame. |
| 6716 | Label runtime; |
| 6717 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6718 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6719 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6720 | __ b(ne, &runtime); |
| 6721 | |
| 6722 | // Patch the arguments.length and the parameters pointer. |
| 6723 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6724 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 6725 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6726 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6727 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6728 | |
| 6729 | // Do the runtime call to allocate the arguments object. |
| 6730 | __ bind(&runtime); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6731 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6732 | } |
| 6733 | |
| 6734 | |
| 6735 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6736 | Label slow; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 6737 | |
| 6738 | // If the receiver might be a value (string, number or boolean) check for this |
| 6739 | // and box it if it is. |
| 6740 | if (ReceiverMightBeValue()) { |
| 6741 | // Get the receiver from the stack. |
| 6742 | // function, receiver [, arguments] |
| 6743 | Label receiver_is_value, receiver_is_js_object; |
| 6744 | __ ldr(r1, MemOperand(sp, argc_ * kPointerSize)); |
| 6745 | |
| 6746 | // Check if receiver is a smi (which is a number value). |
| 6747 | __ BranchOnSmi(r1, &receiver_is_value); |
| 6748 | |
| 6749 | // Check if the receiver is a valid JS object. |
| 6750 | __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 6751 | __ b(ge, &receiver_is_js_object); |
| 6752 | |
| 6753 | // Call the runtime to box the value. |
| 6754 | __ bind(&receiver_is_value); |
| 6755 | __ EnterInternalFrame(); |
| 6756 | __ push(r1); |
| 6757 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
| 6758 | __ LeaveInternalFrame(); |
| 6759 | __ str(r0, MemOperand(sp, argc_ * kPointerSize)); |
| 6760 | |
| 6761 | __ bind(&receiver_is_js_object); |
| 6762 | } |
| 6763 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6764 | // Get the function to call from the stack. |
| 6765 | // function, receiver [, arguments] |
| 6766 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6767 | |
| 6768 | // Check that the function is really a JavaScript function. |
| 6769 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6770 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6771 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6772 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6773 | __ b(ne, &slow); |
| 6774 | |
| 6775 | // Fast-case: Invoke the function now. |
| 6776 | // r1: pushed function |
| 6777 | ParameterCount actual(argc_); |
| 6778 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6779 | |
| 6780 | // Slow-case: Non-function called. |
| 6781 | __ bind(&slow); |
| 6782 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6783 | __ mov(r2, Operand(0)); |
| 6784 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6785 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6786 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6787 | } |
| 6788 | |
| 6789 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6790 | const char* CompareStub::GetName() { |
| 6791 | switch (cc_) { |
| 6792 | case lt: return "CompareStub_LT"; |
| 6793 | case gt: return "CompareStub_GT"; |
| 6794 | case le: return "CompareStub_LE"; |
| 6795 | case ge: return "CompareStub_GE"; |
| 6796 | case ne: { |
| 6797 | if (strict_) { |
| 6798 | if (never_nan_nan_) { |
| 6799 | return "CompareStub_NE_STRICT_NO_NAN"; |
| 6800 | } else { |
| 6801 | return "CompareStub_NE_STRICT"; |
| 6802 | } |
| 6803 | } else { |
| 6804 | if (never_nan_nan_) { |
| 6805 | return "CompareStub_NE_NO_NAN"; |
| 6806 | } else { |
| 6807 | return "CompareStub_NE"; |
| 6808 | } |
| 6809 | } |
| 6810 | } |
| 6811 | case eq: { |
| 6812 | if (strict_) { |
| 6813 | if (never_nan_nan_) { |
| 6814 | return "CompareStub_EQ_STRICT_NO_NAN"; |
| 6815 | } else { |
| 6816 | return "CompareStub_EQ_STRICT"; |
| 6817 | } |
| 6818 | } else { |
| 6819 | if (never_nan_nan_) { |
| 6820 | return "CompareStub_EQ_NO_NAN"; |
| 6821 | } else { |
| 6822 | return "CompareStub_EQ"; |
| 6823 | } |
| 6824 | } |
| 6825 | } |
| 6826 | default: return "CompareStub"; |
| 6827 | } |
| 6828 | } |
| 6829 | |
| 6830 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6831 | int CompareStub::MinorKey() { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6832 | // Encode the three parameters in a unique 16 bit value. |
| 6833 | ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16)); |
| 6834 | int nnn_value = (never_nan_nan_ ? 2 : 0); |
| 6835 | if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs. |
| 6836 | return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6837 | } |
| 6838 | |
| 6839 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 6840 | |
| 6841 | |
| 6842 | void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 6843 | Register left, |
| 6844 | Register right, |
| 6845 | Register scratch1, |
| 6846 | Register scratch2, |
| 6847 | Register scratch3, |
| 6848 | Register scratch4) { |
| 6849 | Label compare_lengths; |
| 6850 | // Find minimum length and length difference. |
| 6851 | __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset)); |
| 6852 | __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset)); |
| 6853 | __ sub(scratch3, scratch1, Operand(scratch2), SetCC); |
| 6854 | Register length_delta = scratch3; |
| 6855 | __ mov(scratch1, scratch2, LeaveCC, gt); |
| 6856 | Register min_length = scratch1; |
| 6857 | __ tst(min_length, Operand(min_length)); |
| 6858 | __ b(eq, &compare_lengths); |
| 6859 | |
| 6860 | // Setup registers so that we only need to increment one register |
| 6861 | // in the loop. |
| 6862 | __ add(scratch2, min_length, |
| 6863 | Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 6864 | __ add(left, left, Operand(scratch2)); |
| 6865 | __ add(right, right, Operand(scratch2)); |
| 6866 | // Registers left and right points to the min_length character of strings. |
| 6867 | __ rsb(min_length, min_length, Operand(-1)); |
| 6868 | Register index = min_length; |
| 6869 | // Index starts at -min_length. |
| 6870 | |
| 6871 | { |
| 6872 | // Compare loop. |
| 6873 | Label loop; |
| 6874 | __ bind(&loop); |
| 6875 | // Compare characters. |
| 6876 | __ add(index, index, Operand(1), SetCC); |
| 6877 | __ ldrb(scratch2, MemOperand(left, index), ne); |
| 6878 | __ ldrb(scratch4, MemOperand(right, index), ne); |
| 6879 | // Skip to compare lengths with eq condition true. |
| 6880 | __ b(eq, &compare_lengths); |
| 6881 | __ cmp(scratch2, scratch4); |
| 6882 | __ b(eq, &loop); |
| 6883 | // Fallthrough with eq condition false. |
| 6884 | } |
| 6885 | // Compare lengths - strings up to min-length are equal. |
| 6886 | __ bind(&compare_lengths); |
| 6887 | ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0)); |
| 6888 | // Use zero length_delta as result. |
| 6889 | __ mov(r0, Operand(length_delta), SetCC, eq); |
| 6890 | // Fall through to here if characters compare not-equal. |
| 6891 | __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt); |
| 6892 | __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt); |
| 6893 | __ Ret(); |
| 6894 | } |
| 6895 | |
| 6896 | |
| 6897 | void StringCompareStub::Generate(MacroAssembler* masm) { |
| 6898 | Label runtime; |
| 6899 | |
| 6900 | // Stack frame on entry. |
| 6901 | // sp[0]: return address |
| 6902 | // sp[4]: right string |
| 6903 | // sp[8]: left string |
| 6904 | |
| 6905 | __ ldr(r0, MemOperand(sp, 2 * kPointerSize)); // left |
| 6906 | __ ldr(r1, MemOperand(sp, 1 * kPointerSize)); // right |
| 6907 | |
| 6908 | Label not_same; |
| 6909 | __ cmp(r0, r1); |
| 6910 | __ b(ne, ¬_same); |
| 6911 | ASSERT_EQ(0, EQUAL); |
| 6912 | ASSERT_EQ(0, kSmiTag); |
| 6913 | __ mov(r0, Operand(Smi::FromInt(EQUAL))); |
| 6914 | __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2); |
| 6915 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 6916 | __ Ret(); |
| 6917 | |
| 6918 | __ bind(¬_same); |
| 6919 | |
| 6920 | // Check that both objects are sequential ascii strings. |
| 6921 | __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime); |
| 6922 | |
| 6923 | // Compare flat ascii strings natively. Remove arguments from stack first. |
| 6924 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 6925 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 6926 | GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5); |
| 6927 | |
| 6928 | // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater) |
| 6929 | // tagged as a small integer. |
| 6930 | __ bind(&runtime); |
| 6931 | __ TailCallRuntime(ExternalReference(Runtime::kStringCompare), 2, 1); |
| 6932 | } |
| 6933 | |
| 6934 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6935 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6936 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6937 | } } // namespace v8::internal |