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" |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame^] | 38 | #include "virtual-frame-inl.h" |
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 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 124 | CodeGenerator::CodeGenerator(MacroAssembler* masm) |
| 125 | : deferred_(8), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 126 | masm_(masm), |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 127 | info_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 128 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 129 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | cc_reg_(al), |
| 131 | state_(NULL), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 132 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
| 136 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 137 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 139 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | // cp: callee's context |
| 141 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 142 | void CodeGenerator::Generate(CompilationInfo* info) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 143 | // Record the position for debugging purposes. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 144 | CodeForFunctionPosition(info->function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | |
| 146 | // Initialize state. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 147 | info_ = info; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 148 | ASSERT(allocator_ == NULL); |
| 149 | RegisterAllocator register_allocator(this); |
| 150 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 151 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 152 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 153 | cc_reg_ = al; |
| 154 | { |
| 155 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 156 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 157 | // Entry: |
| 158 | // Stack: receiver, arguments |
| 159 | // lr: return address |
| 160 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 161 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 162 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 163 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 164 | allocator_->Initialize(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 165 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 166 | #ifdef DEBUG |
| 167 | if (strlen(FLAG_stop_at) > 0 && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 168 | info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 169 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 170 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 171 | } |
| 172 | #endif |
| 173 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 174 | if (info->mode() == CompilationInfo::PRIMARY) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 175 | frame_->Enter(); |
| 176 | // tos: code slot |
| 177 | |
| 178 | // Allocate space for locals and initialize them. This also checks |
| 179 | // for stack overflow. |
| 180 | frame_->AllocateStackSlots(); |
| 181 | |
| 182 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 183 | int heap_slots = scope()->num_heap_slots(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 184 | if (heap_slots > 0) { |
| 185 | // Allocate local context. |
| 186 | // Get outer context and create a new context based on it. |
| 187 | __ ldr(r0, frame_->Function()); |
| 188 | frame_->EmitPush(r0); |
| 189 | if (heap_slots <= FastNewContextStub::kMaximumSlots) { |
| 190 | FastNewContextStub stub(heap_slots); |
| 191 | frame_->CallStub(&stub, 1); |
| 192 | } else { |
| 193 | frame_->CallRuntime(Runtime::kNewContext, 1); |
| 194 | } |
| 195 | |
| 196 | #ifdef DEBUG |
| 197 | JumpTarget verified_true; |
| 198 | __ cmp(r0, Operand(cp)); |
| 199 | verified_true.Branch(eq); |
| 200 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 201 | verified_true.Bind(); |
| 202 | #endif |
| 203 | // Update context local. |
| 204 | __ str(cp, frame_->Context()); |
| 205 | } |
| 206 | |
| 207 | // TODO(1241774): Improve this code: |
| 208 | // 1) only needed if we have a context |
| 209 | // 2) no need to recompute context ptr every single time |
| 210 | // 3) don't copy parameter operand code from SlotOperand! |
| 211 | { |
| 212 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 213 | // Note that iteration order is relevant here! If we have the same |
| 214 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 215 | // needs to be copied into the context, it must be the last argument |
| 216 | // passed to the parameter that needs to be copied. This is a rare |
| 217 | // case so we don't check for it, instead we rely on the copying |
| 218 | // order: such a parameter is copied repeatedly into the same |
| 219 | // context location and thus the last value is what is seen inside |
| 220 | // the function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 221 | for (int i = 0; i < scope()->num_parameters(); i++) { |
| 222 | Variable* par = scope()->parameter(i); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 223 | Slot* slot = par->slot(); |
| 224 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 225 | ASSERT(!scope()->is_global_scope()); // No params in global scope. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 226 | __ ldr(r1, frame_->ParameterAt(i)); |
| 227 | // Loads r2 with context; used below in RecordWrite. |
| 228 | __ str(r1, SlotOperand(slot, r2)); |
| 229 | // Load the offset into r3. |
| 230 | int slot_offset = |
| 231 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 232 | __ mov(r3, Operand(slot_offset)); |
| 233 | __ RecordWrite(r2, r3, r1); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Store the arguments object. This must happen after context |
| 239 | // initialization because the arguments object may be stored in the |
| 240 | // context. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 241 | if (scope()->arguments() != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 242 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 243 | ASSERT(scope()->arguments_shadow() != NULL); |
| 244 | Variable* arguments = scope()->arguments()->var(); |
| 245 | Variable* shadow = scope()->arguments_shadow()->var(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 246 | ASSERT(arguments != NULL && arguments->slot() != NULL); |
| 247 | ASSERT(shadow != NULL && shadow->slot() != NULL); |
| 248 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
| 249 | __ ldr(r2, frame_->Function()); |
| 250 | // The receiver is below the arguments, the return address, and the |
| 251 | // frame pointer on the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 252 | const int kReceiverDisplacement = 2 + scope()->num_parameters(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 253 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 254 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 255 | frame_->Adjust(3); |
| 256 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
| 257 | frame_->CallStub(&stub, 3); |
| 258 | frame_->EmitPush(r0); |
| 259 | StoreToSlot(arguments->slot(), NOT_CONST_INIT); |
| 260 | StoreToSlot(shadow->slot(), NOT_CONST_INIT); |
| 261 | frame_->Drop(); // Value is no longer needed. |
| 262 | } |
| 263 | |
| 264 | // Initialize ThisFunction reference if present. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 265 | if (scope()->is_function_scope() && scope()->function() != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 266 | __ mov(ip, Operand(Factory::the_hole_value())); |
| 267 | frame_->EmitPush(ip); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 268 | StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 269 | } |
| 270 | } else { |
| 271 | // When used as the secondary compiler for splitting, r1, cp, |
| 272 | // fp, and lr have been pushed on the stack. Adjust the virtual |
| 273 | // frame to match this state. |
| 274 | frame_->Adjust(4); |
| 275 | allocator_->Unuse(r1); |
| 276 | allocator_->Unuse(lr); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 277 | |
| 278 | // Bind all the bailout labels to the beginning of the function. |
| 279 | List<CompilationInfo::Bailout*>* bailouts = info->bailouts(); |
| 280 | for (int i = 0; i < bailouts->length(); i++) { |
| 281 | __ bind(bailouts->at(i)->label()); |
| 282 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 283 | } |
| 284 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 285 | // Initialize the function return target after the locals are set |
| 286 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 287 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 288 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 289 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 290 | // Generate code to 'execute' declarations and initialize functions |
| 291 | // (source elements). In case of an illegal redeclaration we need to |
| 292 | // handle that instead of processing the declarations. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 293 | if (scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 294 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 295 | scope()->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | } else { |
| 297 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 298 | ProcessDeclarations(scope()->declarations()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 299 | // Bail out if a stack-overflow exception occurred when processing |
| 300 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 301 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | } |
| 303 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 304 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 305 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 306 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 307 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 308 | |
| 309 | // Compile the body of the function in a vanilla state. Don't |
| 310 | // bother compiling all the code if the scope has an illegal |
| 311 | // redeclaration. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 312 | if (!scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 313 | Comment cmnt(masm_, "[ function body"); |
| 314 | #ifdef DEBUG |
| 315 | bool is_builtin = Bootstrapper::IsActive(); |
| 316 | bool should_trace = |
| 317 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 318 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 319 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 320 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 321 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | #endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 323 | VisitStatementsAndSpill(info->function()->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 325 | } |
| 326 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 327 | // Generate the return sequence if necessary. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 328 | if (has_valid_frame() || function_return_.is_linked()) { |
| 329 | if (!function_return_.is_linked()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 330 | CodeForReturnPosition(info->function()); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 331 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 332 | // exit |
| 333 | // r0: result |
| 334 | // sp: stack pointer |
| 335 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 336 | // cp: callee's context |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 337 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 338 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 339 | function_return_.Bind(); |
| 340 | if (FLAG_trace) { |
| 341 | // Push the return value on the stack as the parameter. |
| 342 | // Runtime::TraceExit returns the parameter as it is. |
| 343 | frame_->EmitPush(r0); |
| 344 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 345 | } |
| 346 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 347 | // Add a label for checking the size of the code used for returning. |
| 348 | Label check_exit_codesize; |
| 349 | masm_->bind(&check_exit_codesize); |
| 350 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 351 | // Calculate the exact length of the return sequence and make sure that |
| 352 | // the constant pool is not emitted inside of the return sequence. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 353 | int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 354 | int return_sequence_length = Assembler::kJSReturnSequenceLength; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 355 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 356 | // Additional mov instruction generated. |
| 357 | return_sequence_length++; |
| 358 | } |
| 359 | masm_->BlockConstPoolFor(return_sequence_length); |
| 360 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 361 | // Tear down the frame which will restore the caller's frame pointer and |
| 362 | // the link register. |
| 363 | frame_->Exit(); |
| 364 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 365 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 366 | // tool from instrumenting as we rely on the code size here. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 367 | masm_->add(sp, sp, Operand(sp_delta)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 368 | masm_->Jump(lr); |
| 369 | |
| 370 | // 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] | 371 | // expected by the debugger. The add instruction above is an addressing |
| 372 | // mode 1 instruction where there are restrictions on which immediate values |
| 373 | // can be encoded in the instruction and which immediate values requires |
| 374 | // use of an additional instruction for moving the immediate to a temporary |
| 375 | // register. |
| 376 | ASSERT_EQ(return_sequence_length, |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 377 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 378 | } |
| 379 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 380 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | ASSERT(!has_cc()); |
| 382 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 383 | ASSERT(!function_return_is_shadowed_); |
| 384 | function_return_.Unuse(); |
| 385 | DeleteFrame(); |
| 386 | |
| 387 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 388 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 389 | ProcessDeferred(); |
| 390 | } |
| 391 | |
| 392 | allocator_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 396 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 397 | // Currently, this assertion will fail if we try to assign to |
| 398 | // a constant variable that is constant because it is read-only |
| 399 | // (such as the variable referring to a named function expression). |
| 400 | // We need to implement assignments to read-only variables. |
| 401 | // Ideally, we should do this during AST generation (by converting |
| 402 | // such assignments into expression statements); however, in general |
| 403 | // we may not be able to make the decision until past AST generation, |
| 404 | // that is when the entire program is known. |
| 405 | ASSERT(slot != NULL); |
| 406 | int index = slot->index(); |
| 407 | switch (slot->type()) { |
| 408 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 409 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 410 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 411 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 412 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 413 | |
| 414 | case Slot::CONTEXT: { |
| 415 | // Follow the context chain if necessary. |
| 416 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 417 | Register context = cp; |
| 418 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 419 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 420 | // Load the closure. |
| 421 | // (All contexts, even 'with' contexts, have a closure, |
| 422 | // and it is the same for all contexts inside a function. |
| 423 | // There is no need to go to the function context first.) |
| 424 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 425 | // Load the function context (which is the incoming, outer context). |
| 426 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 427 | context = tmp; |
| 428 | } |
| 429 | // We may have a 'with' context now. Get the function context. |
| 430 | // (In fact this mov may never be the needed, since the scope analysis |
| 431 | // may not permit a direct context access in this case and thus we are |
| 432 | // always at a function context. However it is safe to dereference be- |
| 433 | // cause the function context of a function context is itself. Before |
| 434 | // deleting this mov we should try to create a counter-example first, |
| 435 | // though...) |
| 436 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 437 | return ContextOperand(tmp, index); |
| 438 | } |
| 439 | |
| 440 | default: |
| 441 | UNREACHABLE(); |
| 442 | return MemOperand(r0, 0); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 447 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 448 | Slot* slot, |
| 449 | Register tmp, |
| 450 | Register tmp2, |
| 451 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 452 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 453 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 454 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 455 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 456 | if (s->num_heap_slots() > 0) { |
| 457 | if (s->calls_eval()) { |
| 458 | // Check that extension is NULL. |
| 459 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 460 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 461 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 462 | } |
| 463 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 464 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 465 | context = tmp; |
| 466 | } |
| 467 | } |
| 468 | // Check that last extension is NULL. |
| 469 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 470 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 471 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 472 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 473 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 477 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 478 | // (partially) translated into branches, or it may have set the condition |
| 479 | // code register. If force_cc is set, the value is forced to set the |
| 480 | // condition code register and no value is pushed. If the condition code |
| 481 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 482 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 483 | void CodeGenerator::LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 484 | JumpTarget* true_target, |
| 485 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 486 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 487 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 488 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 489 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 490 | { CodeGenState new_state(this, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 491 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 492 | |
| 493 | // If we hit a stack overflow, we may not have actually visited |
| 494 | // the expression. In that case, we ensure that we have a |
| 495 | // valid-looking frame state because we will continue to generate |
| 496 | // code as we unwind the C++ stack. |
| 497 | // |
| 498 | // It's possible to have both a stack overflow and a valid frame |
| 499 | // state (eg, a subexpression overflowed, visiting it returned |
| 500 | // with a dummied frame state, and visiting this expression |
| 501 | // returned with a normal-looking state). |
| 502 | if (HasStackOverflow() && |
| 503 | has_valid_frame() && |
| 504 | !has_cc() && |
| 505 | frame_->height() == original_height) { |
| 506 | true_target->Jump(); |
| 507 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 508 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 509 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 510 | // Convert the TOS value to a boolean in the condition code register. |
| 511 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 512 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 513 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 514 | ASSERT(!has_valid_frame() || |
| 515 | (has_cc() && frame_->height() == original_height) || |
| 516 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 520 | void CodeGenerator::Load(Expression* expr) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 521 | #ifdef DEBUG |
| 522 | int original_height = frame_->height(); |
| 523 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 524 | JumpTarget true_target; |
| 525 | JumpTarget false_target; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 526 | LoadCondition(expr, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 527 | |
| 528 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 529 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 530 | JumpTarget loaded; |
| 531 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 532 | materialize_true.Branch(cc_reg_); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 533 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 534 | frame_->EmitPush(r0); |
| 535 | loaded.Jump(); |
| 536 | materialize_true.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 537 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 538 | frame_->EmitPush(r0); |
| 539 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | cc_reg_ = al; |
| 541 | } |
| 542 | |
| 543 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 544 | // We have at least one condition value that has been "translated" |
| 545 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 546 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 547 | if (frame_ != NULL) { |
| 548 | loaded.Jump(); // Don't lose the current TOS. |
| 549 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 551 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 552 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 553 | true_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 554 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 555 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 556 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 557 | // If both "true" and "false" need to be loaded jump across the code for |
| 558 | // "false". |
| 559 | if (both) { |
| 560 | loaded.Jump(); |
| 561 | } |
| 562 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 563 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 564 | false_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 565 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 566 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 567 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 568 | // A value is loaded on all paths reaching this point. |
| 569 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 570 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 571 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 572 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 573 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 577 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 578 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 579 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 580 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 584 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 585 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 586 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 587 | __ ldr(scratch, |
| 588 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 589 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 593 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 594 | // Special handling of identifiers as subexpressions of typeof. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 595 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 596 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 598 | // For a global variable we build the property reference |
| 599 | // <global>.<variable> and perform a (regular non-contextual) property |
| 600 | // load to make sure we do not get reference errors. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 601 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 602 | Literal key(variable->name()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 603 | Property property(&global, &key, RelocInfo::kNoPosition); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 604 | Reference ref(this, &property); |
| 605 | ref.GetValueAndSpill(); |
| 606 | } else if (variable != NULL && variable->slot() != NULL) { |
| 607 | // For a variable that rewrites to a slot, we signal it is the immediate |
| 608 | // subexpression of a typeof. |
| 609 | LoadFromSlot(variable->slot(), INSIDE_TYPEOF); |
| 610 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 611 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 612 | // Anything else can be handled normally. |
| 613 | LoadAndSpill(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
| 617 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 618 | Reference::Reference(CodeGenerator* cgen, |
| 619 | Expression* expression, |
| 620 | bool persist_after_get) |
| 621 | : cgen_(cgen), |
| 622 | expression_(expression), |
| 623 | type_(ILLEGAL), |
| 624 | persist_after_get_(persist_after_get) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | cgen->LoadReference(this); |
| 626 | } |
| 627 | |
| 628 | |
| 629 | Reference::~Reference() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 630 | ASSERT(is_unloaded() || is_illegal()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 634 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 635 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 636 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 637 | Expression* e = ref->expression(); |
| 638 | Property* property = e->AsProperty(); |
| 639 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 640 | |
| 641 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 642 | // The expression is either a property or a variable proxy that rewrites |
| 643 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 644 | LoadAndSpill(property->obj()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 645 | if (property->key()->IsPropertyName()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 646 | ref->set_type(Reference::NAMED); |
| 647 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 648 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 649 | ref->set_type(Reference::KEYED); |
| 650 | } |
| 651 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 652 | // The expression is a variable proxy that does not rewrite to a |
| 653 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 654 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 655 | LoadGlobal(); |
| 656 | ref->set_type(Reference::NAMED); |
| 657 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 658 | ASSERT(var->slot() != NULL); |
| 659 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 660 | } |
| 661 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 662 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 663 | LoadAndSpill(e); |
| 664 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
| 668 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 669 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 670 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 671 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 672 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 673 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 674 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 675 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 676 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 677 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 679 | ref->set_unloaded(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 683 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 684 | // register to a boolean in the condition code register. The code |
| 685 | // 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] | 686 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 687 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 688 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 689 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 690 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 691 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 692 | |
| 693 | // Fast case checks |
| 694 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 695 | // Check if the value is 'false'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 696 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 697 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 698 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 699 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 700 | // Check if the value is 'true'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 701 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 702 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 703 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 704 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 705 | // Check if the value is 'undefined'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 706 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 707 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 708 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 709 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 710 | // Check if the value is a smi. |
| 711 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 712 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 713 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 714 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 715 | |
| 716 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 717 | frame_->EmitPush(r0); |
| 718 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 719 | // Convert the result (r0) to a condition code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 720 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 721 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 722 | |
| 723 | cc_reg_ = ne; |
| 724 | } |
| 725 | |
| 726 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 727 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 728 | OverwriteMode overwrite_mode, |
| 729 | int constant_rhs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 730 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 731 | // sp[0] : y |
| 732 | // sp[1] : x |
| 733 | // result : r0 |
| 734 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 735 | // Stub is entered with a call: 'return address' is in lr. |
| 736 | switch (op) { |
| 737 | case Token::ADD: // fall through. |
| 738 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 739 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 740 | case Token::DIV: |
| 741 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 742 | case Token::BIT_OR: |
| 743 | case Token::BIT_AND: |
| 744 | case Token::BIT_XOR: |
| 745 | case Token::SHL: |
| 746 | case Token::SHR: |
| 747 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 748 | frame_->EmitPop(r0); // r0 : y |
| 749 | frame_->EmitPop(r1); // r1 : x |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 750 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 751 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 752 | break; |
| 753 | } |
| 754 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 755 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 756 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 757 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 758 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 759 | break; |
| 760 | |
| 761 | default: |
| 762 | // Other cases should have been handled before this point. |
| 763 | UNREACHABLE(); |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 769 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 770 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 771 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 772 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 773 | bool reversed, |
| 774 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 775 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 776 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 777 | reversed_(reversed), |
| 778 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 779 | set_comment("[ DeferredInlinedSmiOperation"); |
| 780 | } |
| 781 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 782 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 783 | |
| 784 | private: |
| 785 | Token::Value op_; |
| 786 | int value_; |
| 787 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 788 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 789 | }; |
| 790 | |
| 791 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 792 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 793 | switch (op_) { |
| 794 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 795 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 796 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 797 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 798 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 799 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 800 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 801 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 802 | } |
| 803 | break; |
| 804 | } |
| 805 | |
| 806 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 807 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 808 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 809 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 810 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 811 | } else { |
| 812 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 813 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 814 | } |
| 815 | break; |
| 816 | } |
| 817 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 818 | // For these operations there is no optimistic operation that needs to be |
| 819 | // reverted. |
| 820 | case Token::MUL: |
| 821 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 822 | case Token::BIT_OR: |
| 823 | case Token::BIT_XOR: |
| 824 | case Token::BIT_AND: { |
| 825 | if (reversed_) { |
| 826 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 827 | } else { |
| 828 | __ mov(r1, Operand(r0)); |
| 829 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 830 | } |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | case Token::SHL: |
| 835 | case Token::SHR: |
| 836 | case Token::SAR: { |
| 837 | if (!reversed_) { |
| 838 | __ mov(r1, Operand(r0)); |
| 839 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 840 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 841 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 842 | } |
| 843 | break; |
| 844 | } |
| 845 | |
| 846 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 847 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 848 | UNREACHABLE(); |
| 849 | break; |
| 850 | } |
| 851 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 852 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 853 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 857 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 858 | x &= x - 1; |
| 859 | return (x & (x - 1)) == 0; |
| 860 | } |
| 861 | |
| 862 | |
| 863 | // Returns the index of the lowest bit set. |
| 864 | static int BitPosition(unsigned x) { |
| 865 | int bit_posn = 0; |
| 866 | while ((x & 0xf) == 0) { |
| 867 | bit_posn += 4; |
| 868 | x >>= 4; |
| 869 | } |
| 870 | while ((x & 1) == 0) { |
| 871 | bit_posn++; |
| 872 | x >>= 1; |
| 873 | } |
| 874 | return bit_posn; |
| 875 | } |
| 876 | |
| 877 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 878 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 879 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 880 | bool reversed, |
| 881 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 882 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 883 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 884 | // some possible smi operations (like + and -) when (at least) one |
| 885 | // of the operands is a literal smi. With this optimization, the |
| 886 | // performance of the system is increased by ~15%, and the generated |
| 887 | // code size is increased by ~1% (measured on a combination of |
| 888 | // different benchmarks). |
| 889 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 890 | // sp[0] : operand |
| 891 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 892 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 893 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 894 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 895 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 896 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 897 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 898 | switch (op) { |
| 899 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 900 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 901 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 902 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 903 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 904 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 905 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 906 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 907 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 908 | break; |
| 909 | } |
| 910 | |
| 911 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 912 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 913 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 914 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 915 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 916 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 917 | } else { |
| 918 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 919 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 920 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 921 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 922 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 923 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 924 | break; |
| 925 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 926 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 927 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 928 | case Token::BIT_OR: |
| 929 | case Token::BIT_XOR: |
| 930 | case Token::BIT_AND: { |
| 931 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 932 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 933 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 934 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 935 | switch (op) { |
| 936 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 937 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 938 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 939 | default: UNREACHABLE(); |
| 940 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 941 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 942 | break; |
| 943 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 944 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 945 | case Token::SHL: |
| 946 | case Token::SHR: |
| 947 | case Token::SAR: { |
| 948 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 949 | something_to_inline = false; |
| 950 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 951 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 952 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 953 | DeferredCode* deferred = |
| 954 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
| 955 | __ tst(r0, Operand(kSmiTagMask)); |
| 956 | deferred->Branch(ne); |
| 957 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 958 | switch (op) { |
| 959 | case Token::SHL: { |
| 960 | if (shift_value != 0) { |
| 961 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 962 | } |
| 963 | // check that the *unsigned* result fits in a smi |
| 964 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 965 | deferred->Branch(mi); |
| 966 | break; |
| 967 | } |
| 968 | case Token::SHR: { |
| 969 | // LSR by immediate 0 means shifting 32 bits. |
| 970 | if (shift_value != 0) { |
| 971 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 972 | } |
| 973 | // check that the *unsigned* result fits in a smi |
| 974 | // neither of the two high-order bits can be set: |
| 975 | // - 0x80000000: high bit would be lost when smi tagging |
| 976 | // - 0x40000000: this number would convert to negative when |
| 977 | // smi tagging these two cases can only happen with shifts |
| 978 | // by 0 or 1 when handed a valid smi |
| 979 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 980 | deferred->Branch(ne); |
| 981 | break; |
| 982 | } |
| 983 | case Token::SAR: { |
| 984 | if (shift_value != 0) { |
| 985 | // ASR by immediate 0 means shifting 32 bits. |
| 986 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 987 | } |
| 988 | break; |
| 989 | } |
| 990 | default: UNREACHABLE(); |
| 991 | } |
| 992 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 993 | deferred->BindExit(); |
| 994 | break; |
| 995 | } |
| 996 | |
| 997 | case Token::MOD: { |
| 998 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 999 | something_to_inline = false; |
| 1000 | break; |
| 1001 | } |
| 1002 | DeferredCode* deferred = |
| 1003 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1004 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1005 | __ tst(r0, Operand(mask)); |
| 1006 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1007 | mask = (int_value << kSmiTagSize) - 1; |
| 1008 | __ and_(r0, r0, Operand(mask)); |
| 1009 | deferred->BindExit(); |
| 1010 | break; |
| 1011 | } |
| 1012 | |
| 1013 | case Token::MUL: { |
| 1014 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1015 | something_to_inline = false; |
| 1016 | break; |
| 1017 | } |
| 1018 | DeferredCode* deferred = |
| 1019 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1020 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1021 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1022 | unsigned mask = 0x80000000u; |
| 1023 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1024 | mask |= mask >> 1; |
| 1025 | } |
| 1026 | mask |= kSmiTagMask; |
| 1027 | // This does a single mask that checks for a too high value in a |
| 1028 | // conservative way and for a non-Smi. It also filters out negative |
| 1029 | // numbers, unfortunately, but since this code is inline we prefer |
| 1030 | // brevity to comprehensiveness. |
| 1031 | __ tst(r0, Operand(mask)); |
| 1032 | deferred->Branch(ne); |
| 1033 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 1034 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
| 1037 | |
| 1038 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1039 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1040 | break; |
| 1041 | } |
| 1042 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1043 | if (!something_to_inline) { |
| 1044 | if (!reversed) { |
| 1045 | frame_->EmitPush(r0); |
| 1046 | __ mov(r0, Operand(value)); |
| 1047 | frame_->EmitPush(r0); |
| 1048 | GenericBinaryOperation(op, mode, int_value); |
| 1049 | } else { |
| 1050 | __ mov(ip, Operand(value)); |
| 1051 | frame_->EmitPush(ip); |
| 1052 | frame_->EmitPush(r0); |
| 1053 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1054 | } |
| 1055 | } |
| 1056 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1057 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1061 | void CodeGenerator::Comparison(Condition cc, |
| 1062 | Expression* left, |
| 1063 | Expression* right, |
| 1064 | bool strict) { |
| 1065 | if (left != NULL) LoadAndSpill(left); |
| 1066 | if (right != NULL) LoadAndSpill(right); |
| 1067 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1068 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1069 | // sp[0] : y |
| 1070 | // sp[1] : x |
| 1071 | // result : cc register |
| 1072 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1073 | // Strict only makes sense for equality comparisons. |
| 1074 | ASSERT(!strict || cc == eq); |
| 1075 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1076 | JumpTarget exit; |
| 1077 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1078 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1079 | if (cc == gt || cc == le) { |
| 1080 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1081 | frame_->EmitPop(r1); |
| 1082 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1083 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1084 | frame_->EmitPop(r0); |
| 1085 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1086 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1087 | __ orr(r2, r0, Operand(r1)); |
| 1088 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1089 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1090 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1091 | // Perform non-smi comparison by stub. |
| 1092 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1093 | // We call with 0 args because there are 0 on the stack. |
| 1094 | CompareStub stub(cc, strict); |
| 1095 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1096 | __ cmp(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1097 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1099 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1100 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1101 | __ cmp(r1, Operand(r0)); |
| 1102 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1103 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1104 | cc_reg_ = cc; |
| 1105 | } |
| 1106 | |
| 1107 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1108 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1109 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1110 | CallFunctionFlags flags, |
| 1111 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1112 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1113 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1114 | int arg_count = args->length(); |
| 1115 | for (int i = 0; i < arg_count; i++) { |
| 1116 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1117 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1118 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1119 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1120 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1121 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1122 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1123 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1124 | CallFunctionStub call_function(arg_count, in_loop, flags); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1125 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1126 | |
| 1127 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1128 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1129 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1133 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1134 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1135 | ASSERT(has_cc()); |
| 1136 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1137 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1138 | cc_reg_ = al; |
| 1139 | } |
| 1140 | |
| 1141 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1142 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1143 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1144 | Comment cmnt(masm_, "[ check stack"); |
| 1145 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1146 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1147 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1148 | // gives a return address 12 bytes down. |
| 1149 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1150 | masm_->cmp(sp, Operand(ip)); |
| 1151 | StackCheckStub stub; |
| 1152 | // Call the stub if lower. |
| 1153 | masm_->mov(pc, |
| 1154 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1155 | RelocInfo::CODE_TARGET), |
| 1156 | LeaveCC, |
| 1157 | lo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1161 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1162 | #ifdef DEBUG |
| 1163 | int original_height = frame_->height(); |
| 1164 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1165 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1166 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1167 | VisitAndSpill(statements->at(i)); |
| 1168 | } |
| 1169 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1170 | } |
| 1171 | |
| 1172 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1173 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1174 | #ifdef DEBUG |
| 1175 | int original_height = frame_->height(); |
| 1176 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1177 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1178 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1179 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1180 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1181 | VisitStatementsAndSpill(node->statements()); |
| 1182 | if (node->break_target()->is_linked()) { |
| 1183 | node->break_target()->Bind(); |
| 1184 | } |
| 1185 | node->break_target()->Unuse(); |
| 1186 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1190 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1191 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1192 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1193 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1194 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1195 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1196 | frame_->EmitPush(r0); |
| 1197 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1198 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1202 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1203 | #ifdef DEBUG |
| 1204 | int original_height = frame_->height(); |
| 1205 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1206 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1207 | Comment cmnt(masm_, "[ Declaration"); |
| 1208 | Variable* var = node->proxy()->var(); |
| 1209 | ASSERT(var != NULL); // must have been resolved |
| 1210 | Slot* slot = var->slot(); |
| 1211 | |
| 1212 | // If it was not possible to allocate the variable at compile time, |
| 1213 | // we need to "declare" it at runtime to make sure it actually |
| 1214 | // exists in the local context. |
| 1215 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1216 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1217 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1218 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1219 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1220 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1221 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1222 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1223 | // Declaration nodes are always declared in only two modes. |
| 1224 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1225 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1226 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1227 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1228 | // Push initial value, if any. |
| 1229 | // Note: For variables we must not push an initial value (such as |
| 1230 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1231 | // must not destroy the current value. |
| 1232 | if (node->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1233 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1234 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1235 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1236 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1237 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1238 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1239 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1240 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1241 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1242 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1243 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | ASSERT(!var->is_global()); |
| 1248 | |
| 1249 | // If we have a function or a constant, we need to initialize the variable. |
| 1250 | Expression* val = NULL; |
| 1251 | if (node->mode() == Variable::CONST) { |
| 1252 | val = new Literal(Factory::the_hole_value()); |
| 1253 | } else { |
| 1254 | val = node->fun(); // NULL if we don't have a function |
| 1255 | } |
| 1256 | |
| 1257 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1258 | { |
| 1259 | // Set initial value. |
| 1260 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1261 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1262 | target.SetValue(NOT_CONST_INIT); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1263 | } |
| 1264 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1265 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1266 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1267 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1271 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1272 | #ifdef DEBUG |
| 1273 | int original_height = frame_->height(); |
| 1274 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1275 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1276 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1277 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1278 | Expression* expression = node->expression(); |
| 1279 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1280 | LoadAndSpill(expression); |
| 1281 | frame_->Drop(); |
| 1282 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1286 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1287 | #ifdef DEBUG |
| 1288 | int original_height = frame_->height(); |
| 1289 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1290 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1291 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1292 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1294 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1298 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1299 | #ifdef DEBUG |
| 1300 | int original_height = frame_->height(); |
| 1301 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1302 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1303 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1304 | // Generate different code depending on which parts of the if statement |
| 1305 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1306 | bool has_then_stm = node->HasThenStatement(); |
| 1307 | bool has_else_stm = node->HasElseStatement(); |
| 1308 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1309 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1310 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1311 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1312 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1313 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1314 | JumpTarget then; |
| 1315 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1316 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1317 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1318 | if (frame_ != NULL) { |
| 1319 | Branch(false, &else_); |
| 1320 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1321 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1322 | if (frame_ != NULL || then.is_linked()) { |
| 1323 | then.Bind(); |
| 1324 | VisitAndSpill(node->then_statement()); |
| 1325 | } |
| 1326 | if (frame_ != NULL) { |
| 1327 | exit.Jump(); |
| 1328 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1329 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1330 | if (else_.is_linked()) { |
| 1331 | else_.Bind(); |
| 1332 | VisitAndSpill(node->else_statement()); |
| 1333 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1334 | |
| 1335 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1336 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1337 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1338 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1339 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1340 | LoadConditionAndSpill(node->condition(), &then, &exit, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1341 | if (frame_ != NULL) { |
| 1342 | Branch(false, &exit); |
| 1343 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1344 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1345 | if (frame_ != NULL || then.is_linked()) { |
| 1346 | then.Bind(); |
| 1347 | VisitAndSpill(node->then_statement()); |
| 1348 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1349 | |
| 1350 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1351 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1352 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1353 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1354 | // if (!cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1355 | LoadConditionAndSpill(node->condition(), &exit, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1356 | if (frame_ != NULL) { |
| 1357 | Branch(true, &exit); |
| 1358 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1359 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1360 | if (frame_ != NULL || else_.is_linked()) { |
| 1361 | else_.Bind(); |
| 1362 | VisitAndSpill(node->else_statement()); |
| 1363 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1364 | |
| 1365 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1366 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1367 | ASSERT(!has_then_stm && !has_else_stm); |
| 1368 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1369 | LoadConditionAndSpill(node->condition(), &exit, &exit, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1370 | if (frame_ != NULL) { |
| 1371 | if (has_cc()) { |
| 1372 | cc_reg_ = al; |
| 1373 | } else { |
| 1374 | frame_->Drop(); |
| 1375 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1380 | if (exit.is_linked()) { |
| 1381 | exit.Bind(); |
| 1382 | } |
| 1383 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1387 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1388 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1389 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1390 | CodeForStatementPosition(node); |
| 1391 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1395 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1396 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1397 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1398 | CodeForStatementPosition(node); |
| 1399 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1403 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1404 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1405 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1406 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1407 | CodeForStatementPosition(node); |
| 1408 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1409 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1410 | frame_->EmitPop(r0); |
| 1411 | function_return_.Jump(); |
| 1412 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1413 | // Pop the result from the frame and prepare the frame for |
| 1414 | // returning thus making it easier to merge. |
| 1415 | frame_->EmitPop(r0); |
| 1416 | frame_->PrepareForReturn(); |
| 1417 | |
| 1418 | function_return_.Jump(); |
| 1419 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1423 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1424 | #ifdef DEBUG |
| 1425 | int original_height = frame_->height(); |
| 1426 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1427 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1428 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1429 | CodeForStatementPosition(node); |
| 1430 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1431 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1432 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1433 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1434 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1435 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1436 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1437 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1438 | __ cmp(r0, Operand(cp)); |
| 1439 | verified_true.Branch(eq); |
| 1440 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1441 | verified_true.Bind(); |
| 1442 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1443 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1444 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1445 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1449 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1450 | #ifdef DEBUG |
| 1451 | int original_height = frame_->height(); |
| 1452 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1453 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1454 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1455 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1456 | // Pop context. |
| 1457 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1458 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1459 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1460 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1464 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1465 | #ifdef DEBUG |
| 1466 | int original_height = frame_->height(); |
| 1467 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1468 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1469 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1470 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1471 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1472 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1473 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1474 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1475 | JumpTarget next_test; |
| 1476 | JumpTarget fall_through; |
| 1477 | JumpTarget default_entry; |
| 1478 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1479 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1480 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1481 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1482 | |
| 1483 | for (int i = 0; i < length; i++) { |
| 1484 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1485 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1486 | // Remember the default clause and compile it at the end. |
| 1487 | default_clause = clause; |
| 1488 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1491 | Comment cmnt(masm_, "[ Case clause"); |
| 1492 | // Compile the test. |
| 1493 | next_test.Bind(); |
| 1494 | next_test.Unuse(); |
| 1495 | // Duplicate TOS. |
| 1496 | __ ldr(r0, frame_->Top()); |
| 1497 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1498 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1499 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1500 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1501 | // Before entering the body from the test, remove the switch value from |
| 1502 | // the stack. |
| 1503 | frame_->Drop(); |
| 1504 | |
| 1505 | // Label the body so that fall through is enabled. |
| 1506 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1507 | default_exit.Bind(); |
| 1508 | } else { |
| 1509 | fall_through.Bind(); |
| 1510 | fall_through.Unuse(); |
| 1511 | } |
| 1512 | VisitStatementsAndSpill(clause->statements()); |
| 1513 | |
| 1514 | // If control flow can fall through from the body, jump to the next body |
| 1515 | // or the end of the statement. |
| 1516 | if (frame_ != NULL) { |
| 1517 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1518 | default_entry.Jump(); |
| 1519 | } else { |
| 1520 | fall_through.Jump(); |
| 1521 | } |
| 1522 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1525 | // The final "test" removes the switch value. |
| 1526 | next_test.Bind(); |
| 1527 | frame_->Drop(); |
| 1528 | |
| 1529 | // If there is a default clause, compile it. |
| 1530 | if (default_clause != NULL) { |
| 1531 | Comment cmnt(masm_, "[ Default clause"); |
| 1532 | default_entry.Bind(); |
| 1533 | VisitStatementsAndSpill(default_clause->statements()); |
| 1534 | // If control flow can fall out of the default and there is a case after |
| 1535 | // it, jup to that case's body. |
| 1536 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1537 | default_exit.Jump(); |
| 1538 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1539 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1540 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1541 | if (fall_through.is_linked()) { |
| 1542 | fall_through.Bind(); |
| 1543 | } |
| 1544 | |
| 1545 | if (node->break_target()->is_linked()) { |
| 1546 | node->break_target()->Bind(); |
| 1547 | } |
| 1548 | node->break_target()->Unuse(); |
| 1549 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1553 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1554 | #ifdef DEBUG |
| 1555 | int original_height = frame_->height(); |
| 1556 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1557 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1558 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1559 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1560 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1561 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1562 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1563 | // Label the top of the loop for the backward CFG edge. If the test |
| 1564 | // is always true we can use the continue target, and if the test is |
| 1565 | // always false there is no need. |
| 1566 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1567 | switch (info) { |
| 1568 | case ALWAYS_TRUE: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1569 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1570 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1571 | break; |
| 1572 | case ALWAYS_FALSE: |
| 1573 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1574 | break; |
| 1575 | case DONT_KNOW: |
| 1576 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1577 | body.Bind(); |
| 1578 | break; |
| 1579 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1580 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1581 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1582 | VisitAndSpill(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1583 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1584 | // Compile the test. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1585 | switch (info) { |
| 1586 | case ALWAYS_TRUE: |
| 1587 | // If control can fall off the end of the body, jump back to the |
| 1588 | // top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1589 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1590 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1591 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1592 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1593 | case ALWAYS_FALSE: |
| 1594 | // If we have a continue in the body, we only have to bind its |
| 1595 | // jump target. |
| 1596 | if (node->continue_target()->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1597 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1598 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1599 | break; |
| 1600 | case DONT_KNOW: |
| 1601 | // We have to compile the test expression if it can be reached by |
| 1602 | // control flow falling out of the body or via continue. |
| 1603 | if (node->continue_target()->is_linked()) { |
| 1604 | node->continue_target()->Bind(); |
| 1605 | } |
| 1606 | if (has_valid_frame()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1607 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 1608 | CodeForDoWhileConditionPosition(node); |
| 1609 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1610 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1611 | // A invalid frame here indicates that control did not |
| 1612 | // fall out of the test expression. |
| 1613 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1614 | } |
| 1615 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1616 | break; |
| 1617 | } |
| 1618 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1619 | if (node->break_target()->is_linked()) { |
| 1620 | node->break_target()->Bind(); |
| 1621 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1622 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1623 | } |
| 1624 | |
| 1625 | |
| 1626 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 1627 | #ifdef DEBUG |
| 1628 | int original_height = frame_->height(); |
| 1629 | #endif |
| 1630 | VirtualFrame::SpilledScope spilled_scope; |
| 1631 | Comment cmnt(masm_, "[ WhileStatement"); |
| 1632 | CodeForStatementPosition(node); |
| 1633 | |
| 1634 | // If the test is never true and has no side effects there is no need |
| 1635 | // to compile the test or body. |
| 1636 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1637 | if (info == ALWAYS_FALSE) return; |
| 1638 | |
| 1639 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1640 | |
| 1641 | // Label the top of the loop with the continue target for the backward |
| 1642 | // CFG edge. |
| 1643 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1644 | node->continue_target()->Bind(); |
| 1645 | |
| 1646 | if (info == DONT_KNOW) { |
| 1647 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1648 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1649 | if (has_valid_frame()) { |
| 1650 | // A NULL frame indicates that control did not fall out of the |
| 1651 | // test expression. |
| 1652 | Branch(false, node->break_target()); |
| 1653 | } |
| 1654 | if (has_valid_frame() || body.is_linked()) { |
| 1655 | body.Bind(); |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | if (has_valid_frame()) { |
| 1660 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1661 | VisitAndSpill(node->body()); |
| 1662 | |
| 1663 | // If control flow can fall out of the body, jump back to the top. |
| 1664 | if (has_valid_frame()) { |
| 1665 | node->continue_target()->Jump(); |
| 1666 | } |
| 1667 | } |
| 1668 | if (node->break_target()->is_linked()) { |
| 1669 | node->break_target()->Bind(); |
| 1670 | } |
| 1671 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1672 | } |
| 1673 | |
| 1674 | |
| 1675 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 1676 | #ifdef DEBUG |
| 1677 | int original_height = frame_->height(); |
| 1678 | #endif |
| 1679 | VirtualFrame::SpilledScope spilled_scope; |
| 1680 | Comment cmnt(masm_, "[ ForStatement"); |
| 1681 | CodeForStatementPosition(node); |
| 1682 | if (node->init() != NULL) { |
| 1683 | VisitAndSpill(node->init()); |
| 1684 | } |
| 1685 | |
| 1686 | // If the test is never true there is no need to compile the test or |
| 1687 | // body. |
| 1688 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1689 | if (info == ALWAYS_FALSE) return; |
| 1690 | |
| 1691 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1692 | |
| 1693 | // If there is no update statement, label the top of the loop with the |
| 1694 | // continue target, otherwise with the loop target. |
| 1695 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1696 | if (node->next() == NULL) { |
| 1697 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1698 | node->continue_target()->Bind(); |
| 1699 | } else { |
| 1700 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1701 | loop.Bind(); |
| 1702 | } |
| 1703 | |
| 1704 | // If the test is always true, there is no need to compile it. |
| 1705 | if (info == DONT_KNOW) { |
| 1706 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1707 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1708 | if (has_valid_frame()) { |
| 1709 | Branch(false, node->break_target()); |
| 1710 | } |
| 1711 | if (has_valid_frame() || body.is_linked()) { |
| 1712 | body.Bind(); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | if (has_valid_frame()) { |
| 1717 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1718 | VisitAndSpill(node->body()); |
| 1719 | |
| 1720 | if (node->next() == NULL) { |
| 1721 | // If there is no update statement and control flow can fall out |
| 1722 | // of the loop, jump directly to the continue label. |
| 1723 | if (has_valid_frame()) { |
| 1724 | node->continue_target()->Jump(); |
| 1725 | } |
| 1726 | } else { |
| 1727 | // If there is an update statement and control flow can reach it |
| 1728 | // via falling out of the body of the loop or continuing, we |
| 1729 | // compile the update statement. |
| 1730 | if (node->continue_target()->is_linked()) { |
| 1731 | node->continue_target()->Bind(); |
| 1732 | } |
| 1733 | if (has_valid_frame()) { |
| 1734 | // Record source position of the statement as this code which is |
| 1735 | // after the code for the body actually belongs to the loop |
| 1736 | // statement and not the body. |
| 1737 | CodeForStatementPosition(node); |
| 1738 | VisitAndSpill(node->next()); |
| 1739 | loop.Jump(); |
| 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | if (node->break_target()->is_linked()) { |
| 1744 | node->break_target()->Bind(); |
| 1745 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1746 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1750 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1751 | #ifdef DEBUG |
| 1752 | int original_height = frame_->height(); |
| 1753 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1754 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1755 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1756 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1757 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1758 | JumpTarget primitive; |
| 1759 | JumpTarget jsobject; |
| 1760 | JumpTarget fixed_array; |
| 1761 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1762 | JumpTarget end_del_check; |
| 1763 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1764 | |
| 1765 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1766 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1767 | |
| 1768 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1769 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1770 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1771 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 1772 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1773 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1774 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1775 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1776 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1777 | |
| 1778 | // Stack layout in body: |
| 1779 | // [iteration counter (Smi)] |
| 1780 | // [length of array] |
| 1781 | // [FixedArray] |
| 1782 | // [Map or 0] |
| 1783 | // [Object] |
| 1784 | |
| 1785 | // Check if enumerable is already a JSObject |
| 1786 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1787 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1788 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1789 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1790 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1791 | primitive.Bind(); |
| 1792 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1793 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1794 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1795 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1796 | // Get the set of properties (as a FixedArray or Map). |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1797 | // r0: value to be iterated over |
| 1798 | frame_->EmitPush(r0); // Push the object being iterated over. |
| 1799 | |
| 1800 | // Check cache validity in generated code. This is a fast case for |
| 1801 | // the JSObject::IsSimpleEnum cache validity checks. If we cannot |
| 1802 | // guarantee cache validity, call the runtime system to check cache |
| 1803 | // validity or get the property names in a fixed array. |
| 1804 | JumpTarget call_runtime; |
| 1805 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1806 | JumpTarget check_prototype; |
| 1807 | JumpTarget use_cache; |
| 1808 | __ mov(r1, Operand(r0)); |
| 1809 | loop.Bind(); |
| 1810 | // Check that there are no elements. |
| 1811 | __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 1812 | __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex); |
| 1813 | __ cmp(r2, r4); |
| 1814 | call_runtime.Branch(ne); |
| 1815 | // Check that instance descriptors are not empty so that we can |
| 1816 | // check for an enum cache. Leave the map in r3 for the subsequent |
| 1817 | // prototype load. |
| 1818 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1819 | __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset)); |
| 1820 | __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex); |
| 1821 | __ cmp(r2, ip); |
| 1822 | call_runtime.Branch(eq); |
| 1823 | // Check that there in an enum cache in the non-empty instance |
| 1824 | // descriptors. This is the case if the next enumeration index |
| 1825 | // field does not contain a smi. |
| 1826 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset)); |
| 1827 | __ tst(r2, Operand(kSmiTagMask)); |
| 1828 | call_runtime.Branch(eq); |
| 1829 | // For all objects but the receiver, check that the cache is empty. |
| 1830 | // r4: empty fixed array root. |
| 1831 | __ cmp(r1, r0); |
| 1832 | check_prototype.Branch(eq); |
| 1833 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1834 | __ cmp(r2, r4); |
| 1835 | call_runtime.Branch(ne); |
| 1836 | check_prototype.Bind(); |
| 1837 | // Load the prototype from the map and loop if non-null. |
| 1838 | __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 1839 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1840 | __ cmp(r1, ip); |
| 1841 | loop.Branch(ne); |
| 1842 | // The enum cache is valid. Load the map of the object being |
| 1843 | // iterated over and use the cache for the iteration. |
| 1844 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1845 | use_cache.Jump(); |
| 1846 | |
| 1847 | call_runtime.Bind(); |
| 1848 | // Call the runtime to get the property names for the object. |
| 1849 | 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] | 1850 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1851 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1852 | // If we got a map from the runtime call, we can do a fast |
| 1853 | // modification check. Otherwise, we got a fixed array, and we have |
| 1854 | // to do a slow check. |
| 1855 | // r0: map or fixed array (result from call to |
| 1856 | // Runtime::kGetPropertyNamesFast) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1857 | __ mov(r2, Operand(r0)); |
| 1858 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1859 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 1860 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1861 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1862 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1863 | use_cache.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1864 | // Get enum cache |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1865 | // r0: map (either the result from a call to |
| 1866 | // Runtime::kGetPropertyNamesFast or has been fetched directly from |
| 1867 | // the object) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | __ mov(r1, Operand(r0)); |
| 1869 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1870 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1871 | __ ldr(r2, |
| 1872 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1873 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1874 | frame_->EmitPush(r0); // map |
| 1875 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1876 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1877 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1878 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1879 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1880 | frame_->EmitPush(r0); |
| 1881 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1882 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1883 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1885 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1886 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1887 | |
| 1888 | // 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] | 1889 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1890 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1891 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1892 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1893 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1894 | |
| 1895 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1896 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1897 | // sp[0] : index |
| 1898 | // sp[1] : array/enum cache length |
| 1899 | // sp[2] : array or enum cache |
| 1900 | // sp[3] : 0 or map |
| 1901 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1902 | // Grab the current frame's height for the break and continue |
| 1903 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1904 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1905 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1906 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1907 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1908 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1909 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1910 | node->break_target()->Branch(hs); |
| 1911 | |
| 1912 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1913 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1914 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1915 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1916 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1917 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1918 | |
| 1919 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1920 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1921 | // Check if this (still) matches the map of the enumerable. |
| 1922 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1923 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1924 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1925 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1926 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1927 | |
| 1928 | // 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] | 1929 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1930 | frame_->EmitPush(r0); |
| 1931 | frame_->EmitPush(r3); // push entry |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1932 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1933 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1934 | |
| 1935 | // 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] | 1936 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1937 | __ cmp(r3, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1938 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1939 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1940 | end_del_check.Bind(); |
| 1941 | // Store the entry in the 'each' expression and take another spin in the |
| 1942 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1943 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1944 | { Reference each(this, node->each()); |
| 1945 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1946 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1947 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1948 | frame_->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1949 | each.SetValue(NOT_CONST_INIT); |
| 1950 | frame_->Drop(2); |
| 1951 | } else { |
| 1952 | // If the reference was to a slot we rely on the convenient property |
| 1953 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1954 | // right on top of or right underneath a zero-sized reference. |
| 1955 | each.SetValue(NOT_CONST_INIT); |
| 1956 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1957 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1958 | } |
| 1959 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1960 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1961 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1962 | VisitAndSpill(node->body()); |
| 1963 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1964 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1965 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1966 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1967 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1968 | frame_->EmitPop(r0); |
| 1969 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1970 | frame_->EmitPush(r0); |
| 1971 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1972 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1973 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1974 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1975 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1976 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1977 | |
| 1978 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1979 | exit.Bind(); |
| 1980 | node->continue_target()->Unuse(); |
| 1981 | node->break_target()->Unuse(); |
| 1982 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1986 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1987 | #ifdef DEBUG |
| 1988 | int original_height = frame_->height(); |
| 1989 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1990 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1991 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1992 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1993 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1994 | JumpTarget try_block; |
| 1995 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1996 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1997 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1998 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1999 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2000 | |
| 2001 | // Store the caught exception in the catch variable. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2002 | Variable* catch_var = node->catch_var()->var(); |
| 2003 | ASSERT(catch_var != NULL && catch_var->slot() != NULL); |
| 2004 | StoreToSlot(catch_var->slot(), NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2005 | |
| 2006 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2007 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2008 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2009 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2010 | if (frame_ != NULL) { |
| 2011 | exit.Jump(); |
| 2012 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2013 | |
| 2014 | |
| 2015 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2016 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2017 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2018 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2019 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2020 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2021 | // Shadow the labels for all escapes from the try block, including |
| 2022 | // returns. During shadowing, the original label is hidden as the |
| 2023 | // LabelShadow and operations on the original actually affect the |
| 2024 | // shadowing label. |
| 2025 | // |
| 2026 | // We should probably try to unify the escaping labels and the return |
| 2027 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2028 | int nof_escapes = node->escaping_targets()->length(); |
| 2029 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2030 | |
| 2031 | // Add the shadow target for the function return. |
| 2032 | static const int kReturnShadowIndex = 0; |
| 2033 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2034 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2035 | function_return_is_shadowed_ = true; |
| 2036 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2037 | |
| 2038 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2039 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2040 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2044 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | |
| 2046 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2047 | // After shadowing stops, the original labels are unshadowed and the |
| 2048 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2049 | bool has_unlinks = false; |
| 2050 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2052 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2053 | } |
| 2054 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2055 | |
| 2056 | // Get an external reference to the handler address. |
| 2057 | ExternalReference handler_address(Top::k_handler_address); |
| 2058 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2059 | // If we can fall off the end of the try block, unlink from try chain. |
| 2060 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2061 | // The next handler address is on top of the frame. Unlink from |
| 2062 | // the handler list and drop the rest of this handler from the |
| 2063 | // frame. |
| 2064 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2065 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2066 | __ mov(r3, Operand(handler_address)); |
| 2067 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2068 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2069 | if (has_unlinks) { |
| 2070 | exit.Jump(); |
| 2071 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2074 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2075 | // jumped to. Deallocate each shadow target. |
| 2076 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2077 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2078 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2079 | shadows[i]->Bind(); |
| 2080 | // Because we can be jumping here (to spilled code) from unspilled |
| 2081 | // code, we need to reestablish a spilled frame at this block. |
| 2082 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2083 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2084 | // Reload sp from the top handler, because some statements that we |
| 2085 | // 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] | 2086 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2087 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2088 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2089 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2090 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2091 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2093 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2094 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2095 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2096 | frame_->PrepareForReturn(); |
| 2097 | } |
| 2098 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2102 | exit.Bind(); |
| 2103 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2107 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2108 | #ifdef DEBUG |
| 2109 | int original_height = frame_->height(); |
| 2110 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2111 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2112 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2113 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2114 | |
| 2115 | // State: Used to keep track of reason for entering the finally |
| 2116 | // block. Should probably be extended to hold information for |
| 2117 | // break/continue from within the try block. |
| 2118 | enum { FALLING, THROWING, JUMPING }; |
| 2119 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2120 | JumpTarget try_block; |
| 2121 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2122 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2123 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2124 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2125 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2126 | // In case of thrown exceptions, this is where we continue. |
| 2127 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2128 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2129 | |
| 2130 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2131 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2132 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2133 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2134 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2135 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2136 | // Shadow the labels for all escapes from the try block, including |
| 2137 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2138 | // operations on the original actually affect the shadowing label. |
| 2139 | // |
| 2140 | // We should probably try to unify the escaping labels and the return |
| 2141 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2142 | int nof_escapes = node->escaping_targets()->length(); |
| 2143 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2144 | |
| 2145 | // Add the shadow target for the function return. |
| 2146 | static const int kReturnShadowIndex = 0; |
| 2147 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2148 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2149 | function_return_is_shadowed_ = true; |
| 2150 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2151 | |
| 2152 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2153 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2154 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2158 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2159 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2160 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2161 | // After shadowing stops, the original labels are unshadowed and the |
| 2162 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2163 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2164 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2165 | shadows[i]->StopShadowing(); |
| 2166 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2167 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2168 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2169 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2170 | // Get an external reference to the handler address. |
| 2171 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2172 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2173 | // If we can fall off the end of the try block, unlink from the try |
| 2174 | // chain and set the state on the frame to FALLING. |
| 2175 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2176 | // The next handler address is on top of the frame. |
| 2177 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2178 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2179 | __ mov(r3, Operand(handler_address)); |
| 2180 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2181 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2182 | |
| 2183 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2184 | // state in r2, then jump around the unlink blocks if any. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2185 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2186 | frame_->EmitPush(r0); |
| 2187 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2188 | if (nof_unlinks > 0) { |
| 2189 | finally_block.Jump(); |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | // Generate code to unlink and set the state for the (formerly) |
| 2194 | // shadowing targets that have been jumped to. |
| 2195 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2196 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2197 | // If we have come from the shadowed return, the return value is |
| 2198 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2199 | // until it is pushed. |
| 2200 | // |
| 2201 | // Because we can be jumping here (to spilled code) from |
| 2202 | // unspilled code, we need to reestablish a spilled frame at |
| 2203 | // this block. |
| 2204 | shadows[i]->Bind(); |
| 2205 | frame_->SpillAll(); |
| 2206 | |
| 2207 | // Reload sp from the top handler, because some statements that |
| 2208 | // we break from (eg, for...in) may have left stuff on the |
| 2209 | // stack. |
| 2210 | __ mov(r3, Operand(handler_address)); |
| 2211 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2212 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2213 | |
| 2214 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2215 | // handler address is currently on top of the frame. |
| 2216 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2217 | frame_->EmitPop(r1); |
| 2218 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2219 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2220 | |
| 2221 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2222 | // If this label shadowed the function return, materialize the |
| 2223 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2224 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2225 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2226 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2227 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2228 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2229 | } |
| 2230 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2231 | if (--nof_unlinks > 0) { |
| 2232 | // If this is not the last unlink block, jump around the next. |
| 2233 | finally_block.Jump(); |
| 2234 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2235 | } |
| 2236 | } |
| 2237 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2238 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2239 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2240 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2241 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2242 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2243 | |
| 2244 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2245 | // and the state - while evaluating the finally block. |
| 2246 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2247 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2248 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2249 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2250 | if (has_valid_frame()) { |
| 2251 | // Restore state and return value or faked TOS. |
| 2252 | frame_->EmitPop(r2); |
| 2253 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2256 | // Generate code to jump to the right destination for all used |
| 2257 | // formerly shadowing targets. Deallocate each shadow target. |
| 2258 | for (int i = 0; i < shadows.length(); i++) { |
| 2259 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2260 | JumpTarget* original = shadows[i]->other_target(); |
| 2261 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2262 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2263 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2264 | skip.Branch(ne); |
| 2265 | frame_->PrepareForReturn(); |
| 2266 | original->Jump(); |
| 2267 | skip.Bind(); |
| 2268 | } else { |
| 2269 | original->Branch(eq); |
| 2270 | } |
| 2271 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2272 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2273 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2274 | if (has_valid_frame()) { |
| 2275 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2276 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2277 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2278 | exit.Branch(ne); |
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 | // Rethrow exception. |
| 2281 | frame_->EmitPush(r0); |
| 2282 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2283 | |
| 2284 | // Done. |
| 2285 | exit.Bind(); |
| 2286 | } |
| 2287 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2291 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2292 | #ifdef DEBUG |
| 2293 | int original_height = frame_->height(); |
| 2294 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2295 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2296 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2297 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2298 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2299 | frame_->DebugBreak(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2300 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2301 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2302 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2306 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2307 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2308 | ASSERT(boilerplate->IsBoilerplate()); |
| 2309 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2310 | __ mov(r0, Operand(boilerplate)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2311 | // Use the fast case closure allocation code that allocates in new |
| 2312 | // space for nested functions that don't need literals cloning. |
| 2313 | if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) { |
| 2314 | FastNewClosureStub stub; |
| 2315 | frame_->EmitPush(r0); |
| 2316 | frame_->CallStub(&stub, 1); |
| 2317 | frame_->EmitPush(r0); |
| 2318 | } else { |
| 2319 | // Create a new closure. |
| 2320 | frame_->EmitPush(cp); |
| 2321 | frame_->EmitPush(r0); |
| 2322 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2323 | frame_->EmitPush(r0); |
| 2324 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2328 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2329 | #ifdef DEBUG |
| 2330 | int original_height = frame_->height(); |
| 2331 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2332 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2333 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2334 | |
| 2335 | // Build the function boilerplate and instantiate it. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2336 | Handle<JSFunction> boilerplate = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2337 | Compiler::BuildBoilerplate(node, script(), this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2338 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2339 | if (HasStackOverflow()) { |
| 2340 | ASSERT(frame_->height() == original_height); |
| 2341 | return; |
| 2342 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2343 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2344 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2348 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2349 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2350 | #ifdef DEBUG |
| 2351 | int original_height = frame_->height(); |
| 2352 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2353 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2354 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2355 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2356 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2360 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2361 | #ifdef DEBUG |
| 2362 | int original_height = frame_->height(); |
| 2363 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2364 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2365 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2366 | JumpTarget then; |
| 2367 | JumpTarget else_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2368 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2369 | if (has_valid_frame()) { |
| 2370 | Branch(false, &else_); |
| 2371 | } |
| 2372 | if (has_valid_frame() || then.is_linked()) { |
| 2373 | then.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2374 | LoadAndSpill(node->then_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2375 | } |
| 2376 | if (else_.is_linked()) { |
| 2377 | JumpTarget exit; |
| 2378 | if (has_valid_frame()) exit.Jump(); |
| 2379 | else_.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2380 | LoadAndSpill(node->else_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2381 | if (exit.is_linked()) exit.Bind(); |
| 2382 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2383 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2387 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2388 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2389 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2390 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2391 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2392 | JumpTarget slow; |
| 2393 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2394 | |
| 2395 | // Generate fast-case code for variables that might be shadowed by |
| 2396 | // eval-introduced variables. Eval is used a lot without |
| 2397 | // introducing variables. In those cases, we do not want to |
| 2398 | // perform a runtime call for all variables in the scope |
| 2399 | // containing the eval. |
| 2400 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2401 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2402 | // If there was no control flow to slow, we can exit early. |
| 2403 | if (!slow.is_linked()) { |
| 2404 | frame_->EmitPush(r0); |
| 2405 | return; |
| 2406 | } |
| 2407 | |
| 2408 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2409 | |
| 2410 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2411 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2412 | // Only generate the fast case for locals that rewrite to slots. |
| 2413 | // This rules out argument loads. |
| 2414 | if (potential_slot != NULL) { |
| 2415 | __ ldr(r0, |
| 2416 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2417 | r1, |
| 2418 | r2, |
| 2419 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2420 | if (potential_slot->var()->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2421 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2422 | __ cmp(r0, ip); |
| 2423 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2424 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2425 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2426 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2427 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2428 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2429 | } |
| 2430 | } |
| 2431 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2432 | slow.Bind(); |
| 2433 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2434 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2435 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2436 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2437 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2438 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2439 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2440 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2441 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2442 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2443 | done.Bind(); |
| 2444 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2445 | |
| 2446 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2447 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2448 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2449 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2450 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2451 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2452 | // initialized yet) which needs to be converted into the 'undefined' |
| 2453 | // value. |
| 2454 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2455 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2456 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2457 | __ cmp(r0, ip); |
| 2458 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2459 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2465 | void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) { |
| 2466 | ASSERT(slot != NULL); |
| 2467 | if (slot->type() == Slot::LOOKUP) { |
| 2468 | ASSERT(slot->var()->is_dynamic()); |
| 2469 | |
| 2470 | // For now, just do a runtime call. |
| 2471 | frame_->EmitPush(cp); |
| 2472 | __ mov(r0, Operand(slot->var()->name())); |
| 2473 | frame_->EmitPush(r0); |
| 2474 | |
| 2475 | if (init_state == CONST_INIT) { |
| 2476 | // Same as the case for a normal store, but ignores attribute |
| 2477 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 2478 | // const properties (introduced via eval("const foo = (some |
| 2479 | // expr);")). Also, uses the current function context instead of |
| 2480 | // the top context. |
| 2481 | // |
| 2482 | // Note that we must declare the foo upon entry of eval(), via a |
| 2483 | // context slot declaration, but we cannot initialize it at the |
| 2484 | // same time, because the const declaration may be at the end of |
| 2485 | // the eval code (sigh...) and the const variable may have been |
| 2486 | // used before (where its value is 'undefined'). Thus, we can only |
| 2487 | // do the initialization when we actually encounter the expression |
| 2488 | // and when the expression operands are defined and valid, and |
| 2489 | // thus we need the split into 2 operations: declaration of the |
| 2490 | // context slot followed by initialization. |
| 2491 | frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 2492 | } else { |
| 2493 | frame_->CallRuntime(Runtime::kStoreContextSlot, 3); |
| 2494 | } |
| 2495 | // Storing a variable must keep the (new) value on the expression |
| 2496 | // stack. This is necessary for compiling assignment expressions. |
| 2497 | frame_->EmitPush(r0); |
| 2498 | |
| 2499 | } else { |
| 2500 | ASSERT(!slot->var()->is_dynamic()); |
| 2501 | |
| 2502 | JumpTarget exit; |
| 2503 | if (init_state == CONST_INIT) { |
| 2504 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 2505 | // Only the first const initialization must be executed (the slot |
| 2506 | // still contains 'the hole' value). When the assignment is |
| 2507 | // executed, the code is identical to a normal store (see below). |
| 2508 | Comment cmnt(masm_, "[ Init const"); |
| 2509 | __ ldr(r2, SlotOperand(slot, r2)); |
| 2510 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2511 | __ cmp(r2, ip); |
| 2512 | exit.Branch(ne); |
| 2513 | } |
| 2514 | |
| 2515 | // We must execute the store. Storing a variable must keep the |
| 2516 | // (new) value on the stack. This is necessary for compiling |
| 2517 | // assignment expressions. |
| 2518 | // |
| 2519 | // Note: We will reach here even with slot->var()->mode() == |
| 2520 | // Variable::CONST because of const declarations which will |
| 2521 | // initialize consts to 'the hole' value and by doing so, end up |
| 2522 | // calling this code. r2 may be loaded with context; used below in |
| 2523 | // RecordWrite. |
| 2524 | frame_->EmitPop(r0); |
| 2525 | __ str(r0, SlotOperand(slot, r2)); |
| 2526 | frame_->EmitPush(r0); |
| 2527 | if (slot->type() == Slot::CONTEXT) { |
| 2528 | // Skip write barrier if the written value is a smi. |
| 2529 | __ tst(r0, Operand(kSmiTagMask)); |
| 2530 | exit.Branch(eq); |
| 2531 | // r2 is loaded with context when calling SlotOperand above. |
| 2532 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 2533 | __ mov(r3, Operand(offset)); |
| 2534 | __ RecordWrite(r2, r3, r1); |
| 2535 | } |
| 2536 | // If we definitely did not jump over the assignment, we do not need |
| 2537 | // to bind the exit label. Doing so can defeat peephole |
| 2538 | // optimization. |
| 2539 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
| 2540 | exit.Bind(); |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2546 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2547 | TypeofState typeof_state, |
| 2548 | Register tmp, |
| 2549 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2550 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2551 | // Check that no extension objects have been created by calls to |
| 2552 | // eval from the current scope to the global scope. |
| 2553 | Register context = cp; |
| 2554 | Scope* s = scope(); |
| 2555 | while (s != NULL) { |
| 2556 | if (s->num_heap_slots() > 0) { |
| 2557 | if (s->calls_eval()) { |
| 2558 | // Check that extension is NULL. |
| 2559 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2560 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2561 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2562 | } |
| 2563 | // Load next context in chain. |
| 2564 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2565 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2566 | context = tmp; |
| 2567 | } |
| 2568 | // If no outer scope calls eval, we do not need to check more |
| 2569 | // context extensions. |
| 2570 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2571 | s = s->outer_scope(); |
| 2572 | } |
| 2573 | |
| 2574 | if (s->is_eval_scope()) { |
| 2575 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2576 | if (!context.is(tmp)) { |
| 2577 | __ mov(tmp, Operand(context)); |
| 2578 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2579 | __ bind(&next); |
| 2580 | // Terminate at global context. |
| 2581 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2582 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2583 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2584 | __ b(eq, &fast); |
| 2585 | // Check that extension is NULL. |
| 2586 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2587 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2588 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2589 | // Load next context in chain. |
| 2590 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2591 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2592 | __ b(&next); |
| 2593 | __ bind(&fast); |
| 2594 | } |
| 2595 | |
| 2596 | // All extension objects were empty and it is safe to use a global |
| 2597 | // load IC call. |
| 2598 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2599 | // Load the global object. |
| 2600 | LoadGlobal(); |
| 2601 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2602 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2603 | // Call IC stub. |
| 2604 | if (typeof_state == INSIDE_TYPEOF) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2605 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2606 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2607 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2610 | // Drop the global object. The result is in r0. |
| 2611 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2615 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2616 | #ifdef DEBUG |
| 2617 | int original_height = frame_->height(); |
| 2618 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2619 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2620 | Comment cmnt(masm_, "[ Slot"); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2621 | LoadFromSlot(node, NOT_INSIDE_TYPEOF); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2622 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2623 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2624 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2625 | |
| 2626 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2627 | #ifdef DEBUG |
| 2628 | int original_height = frame_->height(); |
| 2629 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2630 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2631 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2632 | |
| 2633 | Variable* var = node->var(); |
| 2634 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2635 | if (expr != NULL) { |
| 2636 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2637 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2638 | ASSERT(var->is_global()); |
| 2639 | Reference ref(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2640 | ref.GetValueAndSpill(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2641 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2642 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2646 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2647 | #ifdef DEBUG |
| 2648 | int original_height = frame_->height(); |
| 2649 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2650 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2651 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2652 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2653 | frame_->EmitPush(r0); |
| 2654 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2658 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2659 | #ifdef DEBUG |
| 2660 | int original_height = frame_->height(); |
| 2661 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2662 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2663 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2664 | |
| 2665 | // Retrieve the literal array and check the allocated entry. |
| 2666 | |
| 2667 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2668 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2669 | |
| 2670 | // Load the literals array of the function. |
| 2671 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2672 | |
| 2673 | // Load the literal at the ast saved index. |
| 2674 | int literal_offset = |
| 2675 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2676 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2677 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2678 | JumpTarget done; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2679 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2680 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2681 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2682 | |
| 2683 | // If the entry is undefined we call the runtime system to computed |
| 2684 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2685 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2686 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2687 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2688 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2689 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2690 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2691 | frame_->EmitPush(r0); |
| 2692 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2693 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2694 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2695 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2696 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2697 | frame_->EmitPush(r2); |
| 2698 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2702 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2703 | #ifdef DEBUG |
| 2704 | int original_height = frame_->height(); |
| 2705 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2706 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2707 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2708 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2709 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2710 | __ ldr(r2, frame_->Function()); |
| 2711 | // Literal array. |
| 2712 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
| 2713 | // Literal index. |
| 2714 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
| 2715 | // Constant properties. |
| 2716 | __ mov(r0, Operand(node->constant_properties())); |
| 2717 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
| 2718 | if (node->depth() > 1) { |
| 2719 | frame_->CallRuntime(Runtime::kCreateObjectLiteral, 3); |
| 2720 | } else { |
| 2721 | frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2722 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2723 | frame_->EmitPush(r0); // save the result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2724 | for (int i = 0; i < node->properties()->length(); i++) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2725 | // At the start of each iteration, the top of stack contains |
| 2726 | // the newly created object literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2727 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2728 | Literal* key = property->key(); |
| 2729 | Expression* value = property->value(); |
| 2730 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2731 | case ObjectLiteral::Property::CONSTANT: |
| 2732 | break; |
| 2733 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2734 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2735 | // else fall through |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2736 | case ObjectLiteral::Property::COMPUTED: |
| 2737 | if (key->handle()->IsSymbol()) { |
| 2738 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 2739 | LoadAndSpill(value); |
| 2740 | frame_->EmitPop(r0); |
| 2741 | __ mov(r2, Operand(key->handle())); |
| 2742 | __ ldr(r1, frame_->Top()); // Load the receiver. |
| 2743 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
| 2744 | break; |
| 2745 | } |
| 2746 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2747 | case ObjectLiteral::Property::PROTOTYPE: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2748 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2749 | frame_->EmitPush(r0); // dup the result |
| 2750 | LoadAndSpill(key); |
| 2751 | LoadAndSpill(value); |
| 2752 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2753 | break; |
| 2754 | } |
| 2755 | case ObjectLiteral::Property::SETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2756 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2757 | frame_->EmitPush(r0); |
| 2758 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2759 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2760 | frame_->EmitPush(r0); |
| 2761 | LoadAndSpill(value); |
| 2762 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2763 | break; |
| 2764 | } |
| 2765 | case ObjectLiteral::Property::GETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2766 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2767 | frame_->EmitPush(r0); |
| 2768 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2769 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2770 | frame_->EmitPush(r0); |
| 2771 | LoadAndSpill(value); |
| 2772 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2773 | break; |
| 2774 | } |
| 2775 | } |
| 2776 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2777 | ASSERT(frame_->height() == original_height + 1); |
| 2778 | } |
| 2779 | |
| 2780 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2781 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2782 | #ifdef DEBUG |
| 2783 | int original_height = frame_->height(); |
| 2784 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2785 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2786 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2787 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2788 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2789 | __ ldr(r2, frame_->Function()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2790 | // Load the literals array of the function. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2791 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2792 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2793 | __ mov(r0, Operand(node->constant_elements())); |
| 2794 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2795 | int length = node->values()->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2796 | if (node->depth() > 1) { |
| 2797 | frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2798 | } else if (length > FastCloneShallowArrayStub::kMaximumLength) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2799 | frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2800 | } else { |
| 2801 | FastCloneShallowArrayStub stub(length); |
| 2802 | frame_->CallStub(&stub, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2803 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2804 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2805 | // r0: created object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2806 | |
| 2807 | // Generate code to set the elements in the array that are not |
| 2808 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2809 | for (int i = 0; i < node->values()->length(); i++) { |
| 2810 | Expression* value = node->values()->at(i); |
| 2811 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2812 | // If value is a literal the property value is already set in the |
| 2813 | // boilerplate object. |
| 2814 | if (value->AsLiteral() != NULL) continue; |
| 2815 | // If value is a materialized literal the property value is already set |
| 2816 | // in the boilerplate object if it is simple. |
| 2817 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
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 | // The property must be set by generated code. |
| 2820 | LoadAndSpill(value); |
| 2821 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2822 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2823 | // Fetch the object literal. |
| 2824 | __ ldr(r1, frame_->Top()); |
| 2825 | // Get the elements array. |
| 2826 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2827 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2828 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2829 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2830 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2831 | |
| 2832 | // Update the write barrier for the array address. |
| 2833 | __ mov(r3, Operand(offset)); |
| 2834 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2835 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2836 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2840 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2841 | #ifdef DEBUG |
| 2842 | int original_height = frame_->height(); |
| 2843 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2844 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2845 | // Call runtime routine to allocate the catch extension object and |
| 2846 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2847 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2848 | LoadAndSpill(node->key()); |
| 2849 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2850 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2851 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2852 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2856 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2857 | #ifdef DEBUG |
| 2858 | int original_height = frame_->height(); |
| 2859 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2860 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2861 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2862 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2863 | { Reference target(this, node->target(), node->is_compound()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2864 | if (target.is_illegal()) { |
| 2865 | // Fool the virtual frame into thinking that we left the assignment's |
| 2866 | // value on the frame. |
| 2867 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2868 | frame_->EmitPush(r0); |
| 2869 | ASSERT(frame_->height() == original_height + 1); |
| 2870 | return; |
| 2871 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2872 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2873 | if (node->op() == Token::ASSIGN || |
| 2874 | node->op() == Token::INIT_VAR || |
| 2875 | node->op() == Token::INIT_CONST) { |
| 2876 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2877 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2878 | } else { // Assignment is a compound assignment. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2879 | // Get the old value of the lhs. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2880 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2881 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2882 | bool overwrite = |
| 2883 | (node->value()->AsBinaryOperation() != NULL && |
| 2884 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2885 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2886 | SmiOperation(node->binary_op(), |
| 2887 | literal->handle(), |
| 2888 | false, |
| 2889 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2890 | frame_->EmitPush(r0); |
| 2891 | |
| 2892 | } else { |
| 2893 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2894 | GenericBinaryOperation(node->binary_op(), |
| 2895 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2896 | frame_->EmitPush(r0); |
| 2897 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2898 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2899 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2900 | if (var != NULL && |
| 2901 | (var->mode() == Variable::CONST) && |
| 2902 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2903 | // Assignment ignored - leave the value on the stack. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2904 | UnloadReference(&target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2905 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2906 | CodeForSourcePosition(node->position()); |
| 2907 | if (node->op() == Token::INIT_CONST) { |
| 2908 | // Dynamic constant initializations must use the function context |
| 2909 | // and initialize the actual constant declared. Dynamic variable |
| 2910 | // initializations are simply assignments and use SetValue. |
| 2911 | target.SetValue(CONST_INIT); |
| 2912 | } else { |
| 2913 | target.SetValue(NOT_CONST_INIT); |
| 2914 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2915 | } |
| 2916 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2917 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
| 2920 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2921 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2922 | #ifdef DEBUG |
| 2923 | int original_height = frame_->height(); |
| 2924 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2925 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2926 | Comment cmnt(masm_, "[ Throw"); |
| 2927 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2928 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2929 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2930 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2931 | frame_->EmitPush(r0); |
| 2932 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2936 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2937 | #ifdef DEBUG |
| 2938 | int original_height = frame_->height(); |
| 2939 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2940 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2941 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2942 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2943 | { Reference property(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2944 | property.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2945 | } |
| 2946 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2950 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2951 | #ifdef DEBUG |
| 2952 | int original_height = frame_->height(); |
| 2953 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2954 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2955 | Comment cmnt(masm_, "[ Call"); |
| 2956 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2957 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2958 | ZoneList<Expression*>* args = node->arguments(); |
| 2959 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2960 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2961 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2962 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2963 | Property* property = function->AsProperty(); |
| 2964 | |
| 2965 | // ------------------------------------------------------------------------ |
| 2966 | // Fast-case: Use inline caching. |
| 2967 | // --- |
| 2968 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2969 | // must be resolved after the arguments have been evaluated. The IC code |
| 2970 | // automatically handles this by loading the arguments before the function |
| 2971 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2972 | // ------------------------------------------------------------------------ |
| 2973 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2974 | if (var != NULL && var->is_possibly_eval()) { |
| 2975 | // ---------------------------------- |
| 2976 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 2977 | // ---------------------------------- |
| 2978 | |
| 2979 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 2980 | // resolve the function we need to call and the receiver of the |
| 2981 | // call. Then we call the resolved function using the given |
| 2982 | // arguments. |
| 2983 | // Prepare stack for call to resolved function. |
| 2984 | LoadAndSpill(function); |
| 2985 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 2986 | frame_->EmitPush(r2); // Slot for receiver |
| 2987 | int arg_count = args->length(); |
| 2988 | for (int i = 0; i < arg_count; i++) { |
| 2989 | LoadAndSpill(args->at(i)); |
| 2990 | } |
| 2991 | |
| 2992 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 2993 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 2994 | frame_->EmitPush(r1); |
| 2995 | if (arg_count > 0) { |
| 2996 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 2997 | frame_->EmitPush(r1); |
| 2998 | } else { |
| 2999 | frame_->EmitPush(r2); |
| 3000 | } |
| 3001 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3002 | // Push the receiver. |
| 3003 | __ ldr(r1, frame_->Receiver()); |
| 3004 | frame_->EmitPush(r1); |
| 3005 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3006 | // Resolve the call. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3007 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3008 | |
| 3009 | // 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] | 3010 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3011 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3012 | |
| 3013 | // Call the function. |
| 3014 | CodeForSourcePosition(node->position()); |
| 3015 | |
| 3016 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3017 | CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3018 | frame_->CallStub(&call_function, arg_count + 1); |
| 3019 | |
| 3020 | __ ldr(cp, frame_->Context()); |
| 3021 | // Remove the function from the stack. |
| 3022 | frame_->Drop(); |
| 3023 | frame_->EmitPush(r0); |
| 3024 | |
| 3025 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3026 | // ---------------------------------- |
| 3027 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3028 | // ---------------------------------- |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3029 | // Pass the global object as the receiver and let the IC stub |
| 3030 | // patch the stack to use the global proxy as 'this' in the |
| 3031 | // invoked function. |
| 3032 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3033 | |
| 3034 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3035 | int arg_count = args->length(); |
| 3036 | for (int i = 0; i < arg_count; i++) { |
| 3037 | LoadAndSpill(args->at(i)); |
| 3038 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3039 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3040 | // Setup the name register and call the IC initialization code. |
| 3041 | __ mov(r2, Operand(var->name())); |
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()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3048 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3049 | |
| 3050 | } else if (var != NULL && var->slot() != NULL && |
| 3051 | var->slot()->type() == Slot::LOOKUP) { |
| 3052 | // ---------------------------------- |
| 3053 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3054 | // ---------------------------------- |
| 3055 | |
| 3056 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3057 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3058 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3059 | frame_->EmitPush(r0); |
| 3060 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3061 | // r0: slot value; r1: receiver |
| 3062 | |
| 3063 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3064 | frame_->EmitPush(r0); // function |
| 3065 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3066 | |
| 3067 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3068 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3069 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3070 | |
| 3071 | } else if (property != NULL) { |
| 3072 | // Check if the key is a literal string. |
| 3073 | Literal* literal = property->key()->AsLiteral(); |
| 3074 | |
| 3075 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3076 | // ------------------------------------------------------------------ |
| 3077 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3078 | // ------------------------------------------------------------------ |
| 3079 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3080 | LoadAndSpill(property->obj()); // Receiver. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3081 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3082 | int arg_count = args->length(); |
| 3083 | for (int i = 0; i < arg_count; i++) { |
| 3084 | LoadAndSpill(args->at(i)); |
| 3085 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3086 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3087 | // Set the name register and call the IC initialization code. |
| 3088 | __ mov(r2, Operand(literal->handle())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3089 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3090 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3091 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3092 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3093 | __ ldr(cp, frame_->Context()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3094 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3095 | |
| 3096 | } else { |
| 3097 | // ------------------------------------------- |
| 3098 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3099 | // ------------------------------------------- |
| 3100 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3101 | LoadAndSpill(property->obj()); |
| 3102 | LoadAndSpill(property->key()); |
| 3103 | EmitKeyedLoad(false); |
| 3104 | frame_->Drop(); // key |
| 3105 | // Put the function below the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3106 | if (property->is_synthetic()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3107 | // Use the global receiver. |
| 3108 | frame_->Drop(); |
| 3109 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3110 | LoadGlobalReceiver(r0); |
| 3111 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3112 | frame_->EmitPop(r1); // receiver |
| 3113 | frame_->EmitPush(r0); // function |
| 3114 | frame_->EmitPush(r1); // receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3117 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3118 | CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3119 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | } else { |
| 3123 | // ---------------------------------- |
| 3124 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3125 | // ---------------------------------- |
| 3126 | |
| 3127 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3128 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3129 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3130 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3131 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3132 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3133 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3134 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3135 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3136 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3137 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3141 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3142 | #ifdef DEBUG |
| 3143 | int original_height = frame_->height(); |
| 3144 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3145 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3146 | Comment cmnt(masm_, "[ CallNew"); |
| 3147 | |
| 3148 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3149 | // expression in new calls must be evaluated before the |
| 3150 | // arguments. This is different from ordinary calls, where the |
| 3151 | // actual function to call is resolved after the arguments have been |
| 3152 | // evaluated. |
| 3153 | |
| 3154 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3155 | // receiver. There is no need to use the global proxy here because |
| 3156 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3157 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3158 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3159 | |
| 3160 | // Push the arguments ("left-to-right") on the stack. |
| 3161 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3162 | int arg_count = args->length(); |
| 3163 | for (int i = 0; i < arg_count; i++) { |
| 3164 | LoadAndSpill(args->at(i)); |
| 3165 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3166 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3167 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3168 | __ mov(r0, Operand(arg_count)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3169 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3170 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3171 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3172 | // Call the construct call builtin that handles allocation and |
| 3173 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3174 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3175 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3176 | frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3177 | |
| 3178 | // 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] | 3179 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3180 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3184 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3185 | VirtualFrame::SpilledScope spilled_scope; |
| 3186 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3187 | JumpTarget leave, null, function, non_function_constructor; |
| 3188 | |
| 3189 | // Load the object into r0. |
| 3190 | LoadAndSpill(args->at(0)); |
| 3191 | frame_->EmitPop(r0); |
| 3192 | |
| 3193 | // If the object is a smi, we return null. |
| 3194 | __ tst(r0, Operand(kSmiTagMask)); |
| 3195 | null.Branch(eq); |
| 3196 | |
| 3197 | // Check that the object is a JS object but take special care of JS |
| 3198 | // functions to make sure they have 'Function' as their class. |
| 3199 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3200 | null.Branch(lt); |
| 3201 | |
| 3202 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3203 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3204 | // LAST_JS_OBJECT_TYPE. |
| 3205 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3206 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3207 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3208 | function.Branch(eq); |
| 3209 | |
| 3210 | // Check if the constructor in the map is a function. |
| 3211 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3212 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3213 | non_function_constructor.Branch(ne); |
| 3214 | |
| 3215 | // The r0 register now contains the constructor function. Grab the |
| 3216 | // instance class name from there. |
| 3217 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3218 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3219 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3220 | leave.Jump(); |
| 3221 | |
| 3222 | // Functions have class 'Function'. |
| 3223 | function.Bind(); |
| 3224 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3225 | frame_->EmitPush(r0); |
| 3226 | leave.Jump(); |
| 3227 | |
| 3228 | // Objects with a non-function constructor have class 'Object'. |
| 3229 | non_function_constructor.Bind(); |
| 3230 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3231 | frame_->EmitPush(r0); |
| 3232 | leave.Jump(); |
| 3233 | |
| 3234 | // Non-JS objects have class null. |
| 3235 | null.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3236 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3237 | frame_->EmitPush(r0); |
| 3238 | |
| 3239 | // All done. |
| 3240 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3244 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3245 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3246 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3247 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3248 | LoadAndSpill(args->at(0)); |
| 3249 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3250 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3251 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3252 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3253 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3254 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3255 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3256 | // Load the value. |
| 3257 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3258 | leave.Bind(); |
| 3259 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3263 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3264 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3265 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3266 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3267 | LoadAndSpill(args->at(0)); // Load the object. |
| 3268 | LoadAndSpill(args->at(1)); // Load the value. |
| 3269 | frame_->EmitPop(r0); // r0 contains value |
| 3270 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3271 | // if (object->IsSmi()) return object. |
| 3272 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3273 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3274 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3275 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3276 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3277 | // Store the value. |
| 3278 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3279 | // Update the write barrier. |
| 3280 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3281 | __ RecordWrite(r1, r2, r3); |
| 3282 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3283 | leave.Bind(); |
| 3284 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3288 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3289 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3290 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3291 | LoadAndSpill(args->at(0)); |
| 3292 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3293 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3294 | cc_reg_ = eq; |
| 3295 | } |
| 3296 | |
| 3297 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3298 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3299 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3300 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3301 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3302 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3303 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3304 | LoadAndSpill(args->at(1)); |
| 3305 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3306 | __ CallRuntime(Runtime::kLog, 2); |
| 3307 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3308 | #endif |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3309 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3310 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3314 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3315 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3316 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3317 | LoadAndSpill(args->at(0)); |
| 3318 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3319 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3320 | cc_reg_ = eq; |
| 3321 | } |
| 3322 | |
| 3323 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3324 | // This should generate code that performs a charCodeAt() call or returns |
| 3325 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3326 | // 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] | 3327 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3328 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3329 | ASSERT(args->length() == 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3330 | Comment(masm_, "[ GenerateFastCharCodeAt"); |
| 3331 | |
| 3332 | LoadAndSpill(args->at(0)); |
| 3333 | LoadAndSpill(args->at(1)); |
| 3334 | frame_->EmitPop(r0); // Index. |
| 3335 | frame_->EmitPop(r1); // String. |
| 3336 | |
| 3337 | Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string; |
| 3338 | |
| 3339 | __ tst(r1, Operand(kSmiTagMask)); |
| 3340 | __ b(eq, &slow); // The 'string' was a Smi. |
| 3341 | |
| 3342 | ASSERT(kSmiTag == 0); |
| 3343 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
| 3344 | __ b(ne, &slow); // The index was negative or not a Smi. |
| 3345 | |
| 3346 | __ bind(&try_again_with_new_string); |
| 3347 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 3348 | __ b(ge, &slow); |
| 3349 | |
| 3350 | // Now r2 has the string type. |
| 3351 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3352 | // Now r3 has the length of the string. Compare with the index. |
| 3353 | __ cmp(r3, Operand(r0, LSR, kSmiTagSize)); |
| 3354 | __ b(le, &slow); |
| 3355 | |
| 3356 | // Here we know the index is in range. Check that string is sequential. |
| 3357 | ASSERT_EQ(0, kSeqStringTag); |
| 3358 | __ tst(r2, Operand(kStringRepresentationMask)); |
| 3359 | __ b(ne, ¬_a_flat_string); |
| 3360 | |
| 3361 | // Check whether it is an ASCII string. |
| 3362 | ASSERT_EQ(0, kTwoByteStringTag); |
| 3363 | __ tst(r2, Operand(kStringEncodingMask)); |
| 3364 | __ b(ne, &ascii_string); |
| 3365 | |
| 3366 | // 2-byte string. We can add without shifting since the Smi tag size is the |
| 3367 | // log2 of the number of bytes in a two-byte character. |
| 3368 | ASSERT_EQ(1, kSmiTagSize); |
| 3369 | ASSERT_EQ(0, kSmiShiftSize); |
| 3370 | __ add(r1, r1, Operand(r0)); |
| 3371 | __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize)); |
| 3372 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3373 | __ jmp(&end); |
| 3374 | |
| 3375 | __ bind(&ascii_string); |
| 3376 | __ add(r1, r1, Operand(r0, LSR, kSmiTagSize)); |
| 3377 | __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 3378 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3379 | __ jmp(&end); |
| 3380 | |
| 3381 | __ bind(¬_a_flat_string); |
| 3382 | __ and_(r2, r2, Operand(kStringRepresentationMask)); |
| 3383 | __ cmp(r2, Operand(kConsStringTag)); |
| 3384 | __ b(ne, &slow); |
| 3385 | |
| 3386 | // ConsString. |
| 3387 | // Check that the right hand side is the empty string (ie if this is really a |
| 3388 | // flat string in a cons string). If that is not the case we would rather go |
| 3389 | // to the runtime system now, to flatten the string. |
| 3390 | __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset)); |
| 3391 | __ LoadRoot(r3, Heap::kEmptyStringRootIndex); |
| 3392 | __ cmp(r2, Operand(r3)); |
| 3393 | __ b(ne, &slow); |
| 3394 | |
| 3395 | // Get the first of the two strings. |
| 3396 | __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset)); |
| 3397 | __ jmp(&try_again_with_new_string); |
| 3398 | |
| 3399 | __ bind(&slow); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3400 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3401 | |
| 3402 | __ bind(&end); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3403 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3407 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3408 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3409 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3410 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3411 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3412 | // We need the CC bits to come out as not_equal in the case where the |
| 3413 | // object is a smi. This can't be done with the usual test opcode so |
| 3414 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3415 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3416 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3417 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3418 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3419 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3420 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3421 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3422 | cc_reg_ = eq; |
| 3423 | } |
| 3424 | |
| 3425 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3426 | void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) { |
| 3427 | VirtualFrame::SpilledScope spilled_scope; |
| 3428 | ASSERT(args->length() == 1); |
| 3429 | LoadAndSpill(args->at(0)); |
| 3430 | JumpTarget answer; |
| 3431 | // We need the CC bits to come out as not_equal in the case where the |
| 3432 | // object is a smi. This can't be done with the usual test opcode so |
| 3433 | // we use XOR to get the right CC bits. |
| 3434 | frame_->EmitPop(r0); |
| 3435 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3436 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
| 3437 | answer.Branch(ne); |
| 3438 | // It is a heap object - get the map. Check if the object is a regexp. |
| 3439 | __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE); |
| 3440 | answer.Bind(); |
| 3441 | cc_reg_ = eq; |
| 3442 | } |
| 3443 | |
| 3444 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3445 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 3446 | // This generates a fast version of: |
| 3447 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
| 3448 | VirtualFrame::SpilledScope spilled_scope; |
| 3449 | ASSERT(args->length() == 1); |
| 3450 | LoadAndSpill(args->at(0)); |
| 3451 | frame_->EmitPop(r1); |
| 3452 | __ tst(r1, Operand(kSmiTagMask)); |
| 3453 | false_target()->Branch(eq); |
| 3454 | |
| 3455 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 3456 | __ cmp(r1, ip); |
| 3457 | true_target()->Branch(eq); |
| 3458 | |
| 3459 | Register map_reg = r2; |
| 3460 | __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3461 | // Undetectable objects behave like undefined when tested with typeof. |
| 3462 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 3463 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3464 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 3465 | false_target()->Branch(eq); |
| 3466 | |
| 3467 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 3468 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3469 | false_target()->Branch(lt); |
| 3470 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
| 3471 | cc_reg_ = le; |
| 3472 | } |
| 3473 | |
| 3474 | |
| 3475 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 3476 | // This generates a fast version of: |
| 3477 | // (%_ClassOf(arg) === 'Function') |
| 3478 | VirtualFrame::SpilledScope spilled_scope; |
| 3479 | ASSERT(args->length() == 1); |
| 3480 | LoadAndSpill(args->at(0)); |
| 3481 | frame_->EmitPop(r0); |
| 3482 | __ tst(r0, Operand(kSmiTagMask)); |
| 3483 | false_target()->Branch(eq); |
| 3484 | Register map_reg = r2; |
| 3485 | __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE); |
| 3486 | cc_reg_ = eq; |
| 3487 | } |
| 3488 | |
| 3489 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3490 | void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) { |
| 3491 | VirtualFrame::SpilledScope spilled_scope; |
| 3492 | ASSERT(args->length() == 1); |
| 3493 | LoadAndSpill(args->at(0)); |
| 3494 | frame_->EmitPop(r0); |
| 3495 | __ tst(r0, Operand(kSmiTagMask)); |
| 3496 | false_target()->Branch(eq); |
| 3497 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3498 | __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3499 | __ tst(r1, Operand(1 << Map::kIsUndetectable)); |
| 3500 | cc_reg_ = ne; |
| 3501 | } |
| 3502 | |
| 3503 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3504 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3505 | VirtualFrame::SpilledScope spilled_scope; |
| 3506 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3507 | |
| 3508 | // Get the frame pointer for the calling frame. |
| 3509 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3510 | |
| 3511 | // Skip the arguments adaptor frame if it exists. |
| 3512 | Label check_frame_marker; |
| 3513 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3514 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3515 | __ b(ne, &check_frame_marker); |
| 3516 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3517 | |
| 3518 | // Check the marker in the calling frame. |
| 3519 | __ bind(&check_frame_marker); |
| 3520 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3521 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3522 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3526 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3527 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3528 | ASSERT(args->length() == 0); |
| 3529 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3530 | // Seed the result with the formal parameters count, which will be used |
| 3531 | // in case no arguments adaptor frame is found below the current frame. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3532 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3533 | |
| 3534 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3535 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3536 | frame_->CallStub(&stub, 0); |
| 3537 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3541 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3542 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3543 | ASSERT(args->length() == 1); |
| 3544 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3545 | // Satisfy contract with ArgumentsAccessStub: |
| 3546 | // 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] | 3547 | LoadAndSpill(args->at(0)); |
| 3548 | frame_->EmitPop(r1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3549 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3550 | |
| 3551 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3552 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3553 | frame_->CallStub(&stub, 0); |
| 3554 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3558 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3559 | VirtualFrame::SpilledScope spilled_scope; |
| 3560 | ASSERT(args->length() == 0); |
| 3561 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3562 | RelocInfo::RUNTIME_ENTRY); |
| 3563 | frame_->EmitPush(r0); |
| 3564 | } |
| 3565 | |
| 3566 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3567 | void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) { |
| 3568 | ASSERT_EQ(2, args->length()); |
| 3569 | |
| 3570 | Load(args->at(0)); |
| 3571 | Load(args->at(1)); |
| 3572 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3573 | StringAddStub stub(NO_STRING_ADD_FLAGS); |
| 3574 | frame_->CallStub(&stub, 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3575 | frame_->EmitPush(r0); |
| 3576 | } |
| 3577 | |
| 3578 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3579 | void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) { |
| 3580 | ASSERT_EQ(3, args->length()); |
| 3581 | |
| 3582 | Load(args->at(0)); |
| 3583 | Load(args->at(1)); |
| 3584 | Load(args->at(2)); |
| 3585 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3586 | SubStringStub stub; |
| 3587 | frame_->CallStub(&stub, 3); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3588 | frame_->EmitPush(r0); |
| 3589 | } |
| 3590 | |
| 3591 | |
| 3592 | void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) { |
| 3593 | ASSERT_EQ(2, args->length()); |
| 3594 | |
| 3595 | Load(args->at(0)); |
| 3596 | Load(args->at(1)); |
| 3597 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3598 | StringCompareStub stub; |
| 3599 | frame_->CallStub(&stub, 2); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3600 | frame_->EmitPush(r0); |
| 3601 | } |
| 3602 | |
| 3603 | |
| 3604 | void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) { |
| 3605 | ASSERT_EQ(4, args->length()); |
| 3606 | |
| 3607 | Load(args->at(0)); |
| 3608 | Load(args->at(1)); |
| 3609 | Load(args->at(2)); |
| 3610 | Load(args->at(3)); |
| 3611 | |
| 3612 | frame_->CallRuntime(Runtime::kRegExpExec, 4); |
| 3613 | frame_->EmitPush(r0); |
| 3614 | } |
| 3615 | |
| 3616 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3617 | void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) { |
| 3618 | ASSERT_EQ(args->length(), 1); |
| 3619 | |
| 3620 | // Load the argument on the stack and jump to the runtime. |
| 3621 | Load(args->at(0)); |
| 3622 | |
| 3623 | frame_->CallRuntime(Runtime::kNumberToString, 1); |
| 3624 | frame_->EmitPush(r0); |
| 3625 | } |
| 3626 | |
| 3627 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3628 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3629 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3630 | ASSERT(args->length() == 2); |
| 3631 | |
| 3632 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3633 | LoadAndSpill(args->at(0)); |
| 3634 | LoadAndSpill(args->at(1)); |
| 3635 | frame_->EmitPop(r0); |
| 3636 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3637 | __ cmp(r0, Operand(r1)); |
| 3638 | cc_reg_ = eq; |
| 3639 | } |
| 3640 | |
| 3641 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3642 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3643 | #ifdef DEBUG |
| 3644 | int original_height = frame_->height(); |
| 3645 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3646 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3647 | if (CheckForInlineRuntimeCall(node)) { |
| 3648 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3649 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3650 | return; |
| 3651 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3652 | |
| 3653 | ZoneList<Expression*>* args = node->arguments(); |
| 3654 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3655 | Runtime::Function* function = node->function(); |
| 3656 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3657 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3658 | // Prepare stack for calling JS runtime function. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3659 | // Push the builtins object found in the current global object. |
| 3660 | __ ldr(r1, GlobalObject()); |
| 3661 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3662 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3663 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3664 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3665 | // Push the arguments ("left-to-right"). |
| 3666 | int arg_count = args->length(); |
| 3667 | for (int i = 0; i < arg_count; i++) { |
| 3668 | LoadAndSpill(args->at(i)); |
| 3669 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3670 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3671 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3672 | // Call the JS runtime function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3673 | __ mov(r2, Operand(node->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3674 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3675 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3676 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3677 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3678 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3679 | } else { |
| 3680 | // Call the C runtime function. |
| 3681 | frame_->CallRuntime(function, arg_count); |
| 3682 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3683 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3684 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3688 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3689 | #ifdef DEBUG |
| 3690 | int original_height = frame_->height(); |
| 3691 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3692 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3693 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3694 | |
| 3695 | Token::Value op = node->op(); |
| 3696 | |
| 3697 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3698 | LoadConditionAndSpill(node->expression(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3699 | false_target(), |
| 3700 | true_target(), |
| 3701 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3702 | // LoadCondition may (and usually does) leave a test and branch to |
| 3703 | // be emitted by the caller. In that case, negate the condition. |
| 3704 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3705 | |
| 3706 | } else if (op == Token::DELETE) { |
| 3707 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3708 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3709 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3710 | LoadAndSpill(property->obj()); |
| 3711 | LoadAndSpill(property->key()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3712 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3713 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3714 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3715 | Slot* slot = variable->slot(); |
| 3716 | if (variable->is_global()) { |
| 3717 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3718 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3719 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3720 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3721 | |
| 3722 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3723 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3724 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3725 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3726 | frame_->EmitPush(r0); |
| 3727 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3728 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3729 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3730 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3731 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3732 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3733 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3734 | } else { |
| 3735 | // Default: Result of deleting non-global, not dynamically |
| 3736 | // introduced variables is false. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3737 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3738 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3739 | |
| 3740 | } else { |
| 3741 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3742 | LoadAndSpill(node->expression()); // may have side-effects |
| 3743 | frame_->Drop(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3744 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3745 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3746 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3747 | |
| 3748 | } else if (op == Token::TYPEOF) { |
| 3749 | // Special case for loading the typeof expression; see comment on |
| 3750 | // LoadTypeofExpression(). |
| 3751 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3752 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3753 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3754 | |
| 3755 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3756 | bool overwrite = |
| 3757 | (node->expression()->AsBinaryOperation() != NULL && |
| 3758 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3759 | LoadAndSpill(node->expression()); |
| 3760 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3761 | switch (op) { |
| 3762 | case Token::NOT: |
| 3763 | case Token::DELETE: |
| 3764 | case Token::TYPEOF: |
| 3765 | UNREACHABLE(); // handled above |
| 3766 | break; |
| 3767 | |
| 3768 | case Token::SUB: { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3769 | GenericUnaryOpStub stub(Token::SUB, overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3770 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3771 | break; |
| 3772 | } |
| 3773 | |
| 3774 | case Token::BIT_NOT: { |
| 3775 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3776 | JumpTarget smi_label; |
| 3777 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3778 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3779 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3780 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3781 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 3782 | frame_->CallStub(&stub, 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3783 | continue_label.Jump(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3784 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3785 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3786 | __ mvn(r0, Operand(r0)); |
| 3787 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3788 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3789 | break; |
| 3790 | } |
| 3791 | |
| 3792 | case Token::VOID: |
| 3793 | // since the stack top is cached in r0, popping and then |
| 3794 | // pushing a value can be done by just writing to r0. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3795 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3796 | break; |
| 3797 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3798 | case Token::ADD: { |
| 3799 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3800 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3801 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3802 | continue_label.Branch(eq); |
| 3803 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3804 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3805 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3806 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3807 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3808 | default: |
| 3809 | UNREACHABLE(); |
| 3810 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3811 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3812 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3813 | ASSERT(!has_valid_frame() || |
| 3814 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3815 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3819 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3820 | #ifdef DEBUG |
| 3821 | int original_height = frame_->height(); |
| 3822 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3823 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3824 | Comment cmnt(masm_, "[ CountOperation"); |
| 3825 | |
| 3826 | bool is_postfix = node->is_postfix(); |
| 3827 | bool is_increment = node->op() == Token::INC; |
| 3828 | |
| 3829 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3830 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3831 | |
| 3832 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3833 | if (is_postfix) { |
| 3834 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3835 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3836 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3837 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3838 | // A constant reference is not saved to, so a constant reference is not a |
| 3839 | // compound assignment reference. |
| 3840 | { Reference target(this, node->expression(), !is_const); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3841 | if (target.is_illegal()) { |
| 3842 | // Spoof the virtual frame to have the expected height (one higher |
| 3843 | // than on entry). |
| 3844 | if (!is_postfix) { |
| 3845 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3846 | frame_->EmitPush(r0); |
| 3847 | } |
| 3848 | ASSERT(frame_->height() == original_height + 1); |
| 3849 | return; |
| 3850 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3851 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3852 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3853 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3854 | JumpTarget slow; |
| 3855 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3856 | |
| 3857 | // Load the value (1) into register r1. |
| 3858 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3859 | |
| 3860 | // Check for smi operand. |
| 3861 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3862 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3863 | |
| 3864 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3865 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3866 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3867 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3868 | |
| 3869 | // Perform optimistic increment/decrement. |
| 3870 | if (is_increment) { |
| 3871 | __ add(r0, r0, Operand(r1), SetCC); |
| 3872 | } else { |
| 3873 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3874 | } |
| 3875 | |
| 3876 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3877 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3878 | |
| 3879 | // Revert optimistic increment/decrement. |
| 3880 | if (is_increment) { |
| 3881 | __ sub(r0, r0, Operand(r1)); |
| 3882 | } else { |
| 3883 | __ add(r0, r0, Operand(r1)); |
| 3884 | } |
| 3885 | |
| 3886 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3887 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3888 | { |
| 3889 | // Convert the operand to a number. |
| 3890 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3891 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3892 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3893 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3894 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3895 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3898 | // Compute the new value. |
| 3899 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3900 | frame_->EmitPush(r0); |
| 3901 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3902 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3903 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3904 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3905 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3909 | exit.Bind(); |
| 3910 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3911 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3915 | if (is_postfix) frame_->EmitPop(r0); |
| 3916 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3920 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3921 | #ifdef DEBUG |
| 3922 | int original_height = frame_->height(); |
| 3923 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3924 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3925 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3926 | Token::Value op = node->op(); |
| 3927 | |
| 3928 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3929 | // operators must yield the result of one of the two expressions |
| 3930 | // before any ToBoolean() conversions. This means that the value |
| 3931 | // produced by a && or || operator is not necessarily a boolean. |
| 3932 | |
| 3933 | // NOTE: If the left hand side produces a materialized value (not in |
| 3934 | // the CC register), we force the right hand side to do the |
| 3935 | // same. This is necessary because we may have to branch to the exit |
| 3936 | // after evaluating the left hand side (due to the shortcut |
| 3937 | // semantics), but the compiler must (statically) know if the result |
| 3938 | // of compiling the binary operation is materialized or not. |
| 3939 | |
| 3940 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3941 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3942 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3943 | &is_true, |
| 3944 | false_target(), |
| 3945 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3946 | if (has_valid_frame() && !has_cc()) { |
| 3947 | // 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] | 3948 | JumpTarget pop_and_continue; |
| 3949 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3950 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3951 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3952 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3953 | // Avoid popping the result if it converts to 'false' using the |
| 3954 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3955 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3956 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3957 | Branch(false, &exit); |
| 3958 | |
| 3959 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3960 | pop_and_continue.Bind(); |
| 3961 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3962 | |
| 3963 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3964 | is_true.Bind(); |
| 3965 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3966 | |
| 3967 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3968 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3969 | } else if (has_cc() || is_true.is_linked()) { |
| 3970 | // The left-hand side is either (a) partially compiled to |
| 3971 | // control flow with a final branch left to emit or (b) fully |
| 3972 | // compiled to control flow and possibly true. |
| 3973 | if (has_cc()) { |
| 3974 | Branch(false, false_target()); |
| 3975 | } |
| 3976 | is_true.Bind(); |
| 3977 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3978 | true_target(), |
| 3979 | false_target(), |
| 3980 | false); |
| 3981 | } else { |
| 3982 | // Nothing to do. |
| 3983 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3987 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3988 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3989 | true_target(), |
| 3990 | &is_false, |
| 3991 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3992 | if (has_valid_frame() && !has_cc()) { |
| 3993 | // 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] | 3994 | JumpTarget pop_and_continue; |
| 3995 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3996 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3997 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3998 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3999 | // Avoid popping the result if it converts to 'true' using the |
| 4000 | // standard ToBoolean() conversion as described in ECMA-262, |
| 4001 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4002 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4003 | Branch(true, &exit); |
| 4004 | |
| 4005 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4006 | pop_and_continue.Bind(); |
| 4007 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4008 | |
| 4009 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4010 | is_false.Bind(); |
| 4011 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4012 | |
| 4013 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4014 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4015 | } else if (has_cc() || is_false.is_linked()) { |
| 4016 | // The left-hand side is either (a) partially compiled to |
| 4017 | // control flow with a final branch left to emit or (b) fully |
| 4018 | // compiled to control flow and possibly false. |
| 4019 | if (has_cc()) { |
| 4020 | Branch(true, true_target()); |
| 4021 | } |
| 4022 | is_false.Bind(); |
| 4023 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4024 | true_target(), |
| 4025 | false_target(), |
| 4026 | false); |
| 4027 | } else { |
| 4028 | // Nothing to do. |
| 4029 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | } else { |
| 4033 | // Optimize for the case where (at least) one of the expressions |
| 4034 | // is a literal small integer. |
| 4035 | Literal* lliteral = node->left()->AsLiteral(); |
| 4036 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4037 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 4038 | // never return a constant/immutable object. |
| 4039 | bool overwrite_left = |
| 4040 | (node->left()->AsBinaryOperation() != NULL && |
| 4041 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 4042 | bool overwrite_right = |
| 4043 | (node->right()->AsBinaryOperation() != NULL && |
| 4044 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4045 | |
| 4046 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4047 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4048 | SmiOperation(node->op(), |
| 4049 | rliteral->handle(), |
| 4050 | false, |
| 4051 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4052 | |
| 4053 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4054 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4055 | SmiOperation(node->op(), |
| 4056 | lliteral->handle(), |
| 4057 | true, |
| 4058 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4059 | |
| 4060 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4061 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 4062 | if (overwrite_left) { |
| 4063 | overwrite_mode = OVERWRITE_LEFT; |
| 4064 | } else if (overwrite_right) { |
| 4065 | overwrite_mode = OVERWRITE_RIGHT; |
| 4066 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4067 | LoadAndSpill(node->left()); |
| 4068 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4069 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4070 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4071 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4072 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4073 | ASSERT(!has_valid_frame() || |
| 4074 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4075 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4079 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4080 | #ifdef DEBUG |
| 4081 | int original_height = frame_->height(); |
| 4082 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4083 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4084 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4085 | frame_->EmitPush(r0); |
| 4086 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4090 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4091 | #ifdef DEBUG |
| 4092 | int original_height = frame_->height(); |
| 4093 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4094 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4095 | Comment cmnt(masm_, "[ CompareOperation"); |
| 4096 | |
| 4097 | // Get the expressions from the node. |
| 4098 | Expression* left = node->left(); |
| 4099 | Expression* right = node->right(); |
| 4100 | Token::Value op = node->op(); |
| 4101 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4102 | // To make null checks efficient, we check if either left or right is the |
| 4103 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 4104 | // instead of calling the (very) general runtime routine for checking |
| 4105 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4106 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4107 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4108 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4109 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4110 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4111 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4112 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4113 | LoadAndSpill(left_is_null ? right : left); |
| 4114 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4115 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4116 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4117 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4118 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4119 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4120 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4121 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4122 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4123 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4124 | __ cmp(r0, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4125 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4126 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4127 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4128 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4129 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4130 | // It can be an undetectable object. |
| 4131 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4132 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 4133 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 4134 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4137 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4138 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4139 | return; |
| 4140 | } |
| 4141 | } |
| 4142 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4143 | // To make typeof testing for natives implemented in JavaScript really |
| 4144 | // efficient, we generate special code for expressions of the form: |
| 4145 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4146 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 4147 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4148 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4149 | (right->AsLiteral() != NULL && |
| 4150 | right->AsLiteral()->handle()->IsString())) { |
| 4151 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4152 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4153 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4154 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4155 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4156 | |
| 4157 | if (check->Equals(Heap::number_symbol())) { |
| 4158 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4159 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4160 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4161 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 4162 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4163 | cc_reg_ = eq; |
| 4164 | |
| 4165 | } else if (check->Equals(Heap::string_symbol())) { |
| 4166 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4167 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4168 | |
| 4169 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4170 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4171 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4172 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4173 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4174 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4175 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4176 | |
| 4177 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 4178 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 4179 | cc_reg_ = lt; |
| 4180 | |
| 4181 | } else if (check->Equals(Heap::boolean_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4182 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 4183 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4184 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4185 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 4186 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4187 | cc_reg_ = eq; |
| 4188 | |
| 4189 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4190 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4191 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4192 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4193 | |
| 4194 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4195 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4196 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4197 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4198 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4199 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4200 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4201 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4202 | |
| 4203 | cc_reg_ = eq; |
| 4204 | |
| 4205 | } else if (check->Equals(Heap::function_symbol())) { |
| 4206 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4207 | false_target()->Branch(eq); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4208 | Register map_reg = r2; |
| 4209 | __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE); |
| 4210 | true_target()->Branch(eq); |
| 4211 | // Regular expressions are callable so typeof == 'function'. |
| 4212 | __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4213 | cc_reg_ = eq; |
| 4214 | |
| 4215 | } else if (check->Equals(Heap::object_symbol())) { |
| 4216 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4217 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4218 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4219 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4220 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4221 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4222 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4223 | Register map_reg = r2; |
| 4224 | __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE); |
| 4225 | false_target()->Branch(eq); |
| 4226 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4227 | // It can be an undetectable object. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4228 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4229 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4230 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4231 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4232 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4233 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4234 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4235 | false_target()->Branch(lt); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4236 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4237 | cc_reg_ = le; |
| 4238 | |
| 4239 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4240 | // Uncommon case: typeof testing against a string literal that is |
| 4241 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4242 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4243 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4244 | ASSERT(!has_valid_frame() || |
| 4245 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4246 | return; |
| 4247 | } |
| 4248 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4249 | switch (op) { |
| 4250 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4251 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4252 | break; |
| 4253 | |
| 4254 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4255 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4256 | break; |
| 4257 | |
| 4258 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4259 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4260 | break; |
| 4261 | |
| 4262 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4263 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4264 | break; |
| 4265 | |
| 4266 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4267 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4268 | break; |
| 4269 | |
| 4270 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4271 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4272 | break; |
| 4273 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4274 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4275 | LoadAndSpill(left); |
| 4276 | LoadAndSpill(right); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4277 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4278 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4279 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4280 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4281 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4282 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4283 | LoadAndSpill(left); |
| 4284 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4285 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4286 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4287 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4288 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4289 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4290 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4291 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4292 | |
| 4293 | default: |
| 4294 | UNREACHABLE(); |
| 4295 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4296 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4297 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4301 | void CodeGenerator::EmitKeyedLoad(bool is_global) { |
| 4302 | Comment cmnt(masm_, "[ Load from keyed Property"); |
| 4303 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 4304 | RelocInfo::Mode rmode = is_global |
| 4305 | ? RelocInfo::CODE_TARGET_CONTEXT |
| 4306 | : RelocInfo::CODE_TARGET; |
| 4307 | frame_->CallCodeObject(ic, rmode, 0); |
| 4308 | } |
| 4309 | |
| 4310 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4311 | #ifdef DEBUG |
| 4312 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4313 | #endif |
| 4314 | |
| 4315 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4316 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4317 | #define __ ACCESS_MASM(masm) |
| 4318 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4319 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4320 | Handle<String> Reference::GetName() { |
| 4321 | ASSERT(type_ == NAMED); |
| 4322 | Property* property = expression_->AsProperty(); |
| 4323 | if (property == NULL) { |
| 4324 | // Global variable reference treated as a named property reference. |
| 4325 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4326 | ASSERT(proxy->AsVariable() != NULL); |
| 4327 | ASSERT(proxy->AsVariable()->is_global()); |
| 4328 | return proxy->name(); |
| 4329 | } else { |
| 4330 | Literal* raw_name = property->key()->AsLiteral(); |
| 4331 | ASSERT(raw_name != NULL); |
| 4332 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4333 | } |
| 4334 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4335 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4336 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4337 | void Reference::GetValue() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4338 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4339 | ASSERT(!is_illegal()); |
| 4340 | ASSERT(!cgen_->has_cc()); |
| 4341 | MacroAssembler* masm = cgen_->masm(); |
| 4342 | Property* property = expression_->AsProperty(); |
| 4343 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4344 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | switch (type_) { |
| 4348 | case SLOT: { |
| 4349 | Comment cmnt(masm, "[ Load from Slot"); |
| 4350 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4351 | ASSERT(slot != NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4352 | cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4353 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4356 | case NAMED: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4357 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4358 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4359 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4360 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4361 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4362 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4363 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4364 | ASSERT(var == NULL || var->is_global()); |
| 4365 | RelocInfo::Mode rmode = (var == NULL) |
| 4366 | ? RelocInfo::CODE_TARGET |
| 4367 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4368 | frame->CallCodeObject(ic, rmode, 0); |
| 4369 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4370 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4373 | case KEYED: { |
| 4374 | // TODO(181): Implement inlined version of array indexing once |
| 4375 | // loop nesting is properly tracked on ARM. |
| 4376 | ASSERT(property != NULL); |
| 4377 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 4378 | ASSERT(var == NULL || var->is_global()); |
| 4379 | cgen_->EmitKeyedLoad(var != NULL); |
| 4380 | cgen_->frame()->EmitPush(r0); |
| 4381 | break; |
| 4382 | } |
| 4383 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4384 | default: |
| 4385 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4386 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4387 | |
| 4388 | if (!persist_after_get_) { |
| 4389 | cgen_->UnloadReference(this); |
| 4390 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4394 | void Reference::SetValue(InitState init_state) { |
| 4395 | ASSERT(!is_illegal()); |
| 4396 | ASSERT(!cgen_->has_cc()); |
| 4397 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4398 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4399 | Property* property = expression_->AsProperty(); |
| 4400 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4401 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4402 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4403 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4404 | switch (type_) { |
| 4405 | case SLOT: { |
| 4406 | Comment cmnt(masm, "[ Store to Slot"); |
| 4407 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4408 | cgen_->StoreToSlot(slot, init_state); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4409 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4410 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4413 | case NAMED: { |
| 4414 | Comment cmnt(masm, "[ Store to named Property"); |
| 4415 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4416 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4417 | Handle<String> name(GetName()); |
| 4418 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4419 | frame->EmitPop(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4420 | frame->EmitPop(r1); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4421 | __ mov(r2, Operand(name)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4422 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4423 | frame->EmitPush(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4424 | set_unloaded(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4425 | break; |
| 4426 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4427 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4428 | case KEYED: { |
| 4429 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4430 | Property* property = expression_->AsProperty(); |
| 4431 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4432 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4433 | |
| 4434 | // Call IC code. |
| 4435 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4436 | frame->EmitPop(r0); // value |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4437 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4438 | frame->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4439 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4440 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4441 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4442 | |
| 4443 | default: |
| 4444 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4449 | void FastNewClosureStub::Generate(MacroAssembler* masm) { |
| 4450 | // Clone the boilerplate in new space. Set the context to the |
| 4451 | // current context in cp. |
| 4452 | Label gc; |
| 4453 | |
| 4454 | // Pop the boilerplate function from the stack. |
| 4455 | __ pop(r3); |
| 4456 | |
| 4457 | // Attempt to allocate new JSFunction in new space. |
| 4458 | __ AllocateInNewSpace(JSFunction::kSize / kPointerSize, |
| 4459 | r0, |
| 4460 | r1, |
| 4461 | r2, |
| 4462 | &gc, |
| 4463 | TAG_OBJECT); |
| 4464 | |
| 4465 | // Compute the function map in the current global context and set that |
| 4466 | // as the map of the allocated object. |
| 4467 | __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4468 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 4469 | __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX))); |
| 4470 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4471 | |
| 4472 | // Clone the rest of the boilerplate fields. We don't have to update |
| 4473 | // the write barrier because the allocated object is in new space. |
| 4474 | for (int offset = kPointerSize; |
| 4475 | offset < JSFunction::kSize; |
| 4476 | offset += kPointerSize) { |
| 4477 | if (offset == JSFunction::kContextOffset) { |
| 4478 | __ str(cp, FieldMemOperand(r0, offset)); |
| 4479 | } else { |
| 4480 | __ ldr(r1, FieldMemOperand(r3, offset)); |
| 4481 | __ str(r1, FieldMemOperand(r0, offset)); |
| 4482 | } |
| 4483 | } |
| 4484 | |
| 4485 | // Return result. The argument boilerplate has been popped already. |
| 4486 | __ Ret(); |
| 4487 | |
| 4488 | // Create a new closure through the slower runtime call. |
| 4489 | __ bind(&gc); |
| 4490 | __ push(cp); |
| 4491 | __ push(r3); |
| 4492 | __ TailCallRuntime(ExternalReference(Runtime::kNewClosure), 2, 1); |
| 4493 | } |
| 4494 | |
| 4495 | |
| 4496 | void FastNewContextStub::Generate(MacroAssembler* masm) { |
| 4497 | // Try to allocate the context in new space. |
| 4498 | Label gc; |
| 4499 | int length = slots_ + Context::MIN_CONTEXT_SLOTS; |
| 4500 | |
| 4501 | // Attempt to allocate the context in new space. |
| 4502 | __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize), |
| 4503 | r0, |
| 4504 | r1, |
| 4505 | r2, |
| 4506 | &gc, |
| 4507 | TAG_OBJECT); |
| 4508 | |
| 4509 | // Load the function from the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4510 | __ ldr(r3, MemOperand(sp, 0)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4511 | |
| 4512 | // Setup the object header. |
| 4513 | __ LoadRoot(r2, Heap::kContextMapRootIndex); |
| 4514 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4515 | __ mov(r2, Operand(length)); |
| 4516 | __ str(r2, FieldMemOperand(r0, Array::kLengthOffset)); |
| 4517 | |
| 4518 | // Setup the fixed slots. |
| 4519 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 4520 | __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 4521 | __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 4522 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
| 4523 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX))); |
| 4524 | |
| 4525 | // Copy the global object from the surrounding context. |
| 4526 | __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4527 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4528 | |
| 4529 | // Initialize the rest of the slots to undefined. |
| 4530 | __ LoadRoot(r1, Heap::kUndefinedValueRootIndex); |
| 4531 | for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) { |
| 4532 | __ str(r1, MemOperand(r0, Context::SlotOffset(i))); |
| 4533 | } |
| 4534 | |
| 4535 | // Remove the on-stack argument and return. |
| 4536 | __ mov(cp, r0); |
| 4537 | __ pop(); |
| 4538 | __ Ret(); |
| 4539 | |
| 4540 | // Need to collect. Call into runtime system. |
| 4541 | __ bind(&gc); |
| 4542 | __ TailCallRuntime(ExternalReference(Runtime::kNewContext), 1, 1); |
| 4543 | } |
| 4544 | |
| 4545 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4546 | void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) { |
| 4547 | // Stack layout on entry: |
| 4548 | // |
| 4549 | // [sp]: constant elements. |
| 4550 | // [sp + kPointerSize]: literal index. |
| 4551 | // [sp + (2 * kPointerSize)]: literals array. |
| 4552 | |
| 4553 | // All sizes here are multiples of kPointerSize. |
| 4554 | int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0; |
| 4555 | int size = JSArray::kSize + elements_size; |
| 4556 | |
| 4557 | // Load boilerplate object into r3 and check if we need to create a |
| 4558 | // boilerplate. |
| 4559 | Label slow_case; |
| 4560 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 4561 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 4562 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 4563 | __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4564 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4565 | __ cmp(r3, ip); |
| 4566 | __ b(eq, &slow_case); |
| 4567 | |
| 4568 | // Allocate both the JS array and the elements array in one big |
| 4569 | // allocation. This avoids multiple limit checks. |
| 4570 | __ AllocateInNewSpace(size / kPointerSize, |
| 4571 | r0, |
| 4572 | r1, |
| 4573 | r2, |
| 4574 | &slow_case, |
| 4575 | TAG_OBJECT); |
| 4576 | |
| 4577 | // Copy the JS array part. |
| 4578 | for (int i = 0; i < JSArray::kSize; i += kPointerSize) { |
| 4579 | if ((i != JSArray::kElementsOffset) || (length_ == 0)) { |
| 4580 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 4581 | __ str(r1, FieldMemOperand(r0, i)); |
| 4582 | } |
| 4583 | } |
| 4584 | |
| 4585 | if (length_ > 0) { |
| 4586 | // Get hold of the elements array of the boilerplate and setup the |
| 4587 | // elements pointer in the resulting object. |
| 4588 | __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset)); |
| 4589 | __ add(r2, r0, Operand(JSArray::kSize)); |
| 4590 | __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 4591 | |
| 4592 | // Copy the elements array. |
| 4593 | for (int i = 0; i < elements_size; i += kPointerSize) { |
| 4594 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 4595 | __ str(r1, FieldMemOperand(r2, i)); |
| 4596 | } |
| 4597 | } |
| 4598 | |
| 4599 | // Return and remove the on-stack parameters. |
| 4600 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 4601 | __ Ret(); |
| 4602 | |
| 4603 | __ bind(&slow_case); |
| 4604 | ExternalReference runtime(Runtime::kCreateArrayLiteralShallow); |
| 4605 | __ TailCallRuntime(runtime, 3, 1); |
| 4606 | } |
| 4607 | |
| 4608 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4609 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4610 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4611 | // (31 instead of 32). |
| 4612 | static void CountLeadingZeros( |
| 4613 | MacroAssembler* masm, |
| 4614 | Register source, |
| 4615 | Register scratch, |
| 4616 | Register zeros) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4617 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4618 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4619 | #else |
| 4620 | __ mov(zeros, Operand(0)); |
| 4621 | __ mov(scratch, source); |
| 4622 | // Top 16. |
| 4623 | __ tst(scratch, Operand(0xffff0000)); |
| 4624 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4625 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4626 | // Top 8. |
| 4627 | __ tst(scratch, Operand(0xff000000)); |
| 4628 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4629 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4630 | // Top 4. |
| 4631 | __ tst(scratch, Operand(0xf0000000)); |
| 4632 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4633 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4634 | // Top 2. |
| 4635 | __ tst(scratch, Operand(0xc0000000)); |
| 4636 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4637 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4638 | // Top bit. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4639 | __ tst(scratch, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4640 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4641 | #endif |
| 4642 | } |
| 4643 | |
| 4644 | |
| 4645 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4646 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4647 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4648 | // scratch register. Destroys the source register. No GC occurs during this |
| 4649 | // stub so you don't have to set up the frame. |
| 4650 | class ConvertToDoubleStub : public CodeStub { |
| 4651 | public: |
| 4652 | ConvertToDoubleStub(Register result_reg_1, |
| 4653 | Register result_reg_2, |
| 4654 | Register source_reg, |
| 4655 | Register scratch_reg) |
| 4656 | : result1_(result_reg_1), |
| 4657 | result2_(result_reg_2), |
| 4658 | source_(source_reg), |
| 4659 | zeros_(scratch_reg) { } |
| 4660 | |
| 4661 | private: |
| 4662 | Register result1_; |
| 4663 | Register result2_; |
| 4664 | Register source_; |
| 4665 | Register zeros_; |
| 4666 | |
| 4667 | // Minor key encoding in 16 bits. |
| 4668 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4669 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4670 | |
| 4671 | Major MajorKey() { return ConvertToDouble; } |
| 4672 | int MinorKey() { |
| 4673 | // Encode the parameters in a unique 16 bit value. |
| 4674 | return result1_.code() + |
| 4675 | (result2_.code() << 4) + |
| 4676 | (source_.code() << 8) + |
| 4677 | (zeros_.code() << 12); |
| 4678 | } |
| 4679 | |
| 4680 | void Generate(MacroAssembler* masm); |
| 4681 | |
| 4682 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4683 | |
| 4684 | #ifdef DEBUG |
| 4685 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4686 | #endif |
| 4687 | }; |
| 4688 | |
| 4689 | |
| 4690 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4691 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4692 | Register exponent = result1_; |
| 4693 | Register mantissa = result2_; |
| 4694 | #else |
| 4695 | Register exponent = result2_; |
| 4696 | Register mantissa = result1_; |
| 4697 | #endif |
| 4698 | Label not_special; |
| 4699 | // Convert from Smi to integer. |
| 4700 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4701 | // Move sign bit from source to destination. This works because the sign bit |
| 4702 | // in the exponent word of the double has the same position and polarity as |
| 4703 | // the 2's complement sign bit in a Smi. |
| 4704 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4705 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4706 | // Subtract from 0 if source was negative. |
| 4707 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4708 | __ cmp(source_, Operand(1)); |
| 4709 | __ b(gt, ¬_special); |
| 4710 | |
| 4711 | // We have -1, 0 or 1, which we treat specially. |
| 4712 | __ cmp(source_, Operand(0)); |
| 4713 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4714 | static const uint32_t exponent_word_for_1 = |
| 4715 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4716 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4717 | // 1, 0 and -1 all have 0 for the second word. |
| 4718 | __ mov(mantissa, Operand(0)); |
| 4719 | __ Ret(); |
| 4720 | |
| 4721 | __ bind(¬_special); |
| 4722 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4723 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4724 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4725 | // Compute exponent and or it into the exponent register. |
| 4726 | // We use result2 as a scratch register here. |
| 4727 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4728 | __ orr(exponent, |
| 4729 | exponent, |
| 4730 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4731 | // Shift up the source chopping the top bit off. |
| 4732 | __ add(zeros_, zeros_, Operand(1)); |
| 4733 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4734 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4735 | // Compute lower part of fraction (last 12 bits). |
| 4736 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4737 | // And the top (top 20 bits). |
| 4738 | __ orr(exponent, |
| 4739 | exponent, |
| 4740 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4741 | __ Ret(); |
| 4742 | } |
| 4743 | |
| 4744 | |
| 4745 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4746 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4747 | // so you don't have to set up the frame. |
| 4748 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4749 | public: |
| 4750 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4751 | Register the_heap_number, |
| 4752 | Register scratch) |
| 4753 | : the_int_(the_int), |
| 4754 | the_heap_number_(the_heap_number), |
| 4755 | scratch_(scratch) { } |
| 4756 | |
| 4757 | private: |
| 4758 | Register the_int_; |
| 4759 | Register the_heap_number_; |
| 4760 | Register scratch_; |
| 4761 | |
| 4762 | // Minor key encoding in 16 bits. |
| 4763 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4764 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4765 | |
| 4766 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4767 | int MinorKey() { |
| 4768 | // Encode the parameters in a unique 16 bit value. |
| 4769 | return the_int_.code() + |
| 4770 | (the_heap_number_.code() << 4) + |
| 4771 | (scratch_.code() << 8); |
| 4772 | } |
| 4773 | |
| 4774 | void Generate(MacroAssembler* masm); |
| 4775 | |
| 4776 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4777 | |
| 4778 | #ifdef DEBUG |
| 4779 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4780 | #endif |
| 4781 | }; |
| 4782 | |
| 4783 | |
| 4784 | // See comment for class. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4785 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4786 | Label max_negative_int; |
| 4787 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4788 | // We test for the special value that has a different exponent. This test |
| 4789 | // has the neat side effect of setting the flags according to the sign. |
| 4790 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4791 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4792 | __ b(eq, &max_negative_int); |
| 4793 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4794 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4795 | uint32_t non_smi_exponent = |
| 4796 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4797 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4798 | // Set the sign bit in scratch_ if the value was negative. |
| 4799 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4800 | // Subtract from 0 if the value was negative. |
| 4801 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4802 | // We should be masking the implict first digit of the mantissa away here, |
| 4803 | // but it just ends up combining harmlessly with the last digit of the |
| 4804 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4805 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4806 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4807 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4808 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4809 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4810 | HeapNumber::kExponentOffset)); |
| 4811 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4812 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4813 | HeapNumber::kMantissaOffset)); |
| 4814 | __ Ret(); |
| 4815 | |
| 4816 | __ bind(&max_negative_int); |
| 4817 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4818 | // a double because it uses a sign bit instead of using two's complement. |
| 4819 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4820 | // significant 1 bit is not stored. |
| 4821 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4822 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4823 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4824 | __ mov(ip, Operand(0)); |
| 4825 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4826 | __ Ret(); |
| 4827 | } |
| 4828 | |
| 4829 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4830 | // Handle the case where the lhs and rhs are the same object. |
| 4831 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4832 | // for "identity and not NaN". |
| 4833 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4834 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4835 | Condition cc, |
| 4836 | bool never_nan_nan) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4837 | Label not_identical; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4838 | Label heap_number, return_equal; |
| 4839 | Register exp_mask_reg = r5; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4840 | __ cmp(r0, Operand(r1)); |
| 4841 | __ b(ne, ¬_identical); |
| 4842 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4843 | // The two objects are identical. If we know that one of them isn't NaN then |
| 4844 | // we now know they test equal. |
| 4845 | if (cc != eq || !never_nan_nan) { |
| 4846 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4847 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4848 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4849 | // so we do the second best thing - test it ourselves. |
| 4850 | // They are both equal and they are not both Smis so both of them are not |
| 4851 | // Smis. If it's not a heap number, then return equal. |
| 4852 | if (cc == lt || cc == gt) { |
| 4853 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4854 | __ b(ge, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4855 | } else { |
| 4856 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4857 | __ b(eq, &heap_number); |
| 4858 | // Comparing JS objects with <=, >= is complicated. |
| 4859 | if (cc != eq) { |
| 4860 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4861 | __ b(ge, slow); |
| 4862 | // Normally here we fall through to return_equal, but undefined is |
| 4863 | // special: (undefined == undefined) == true, but |
| 4864 | // (undefined <= undefined) == false! See ECMAScript 11.8.5. |
| 4865 | if (cc == le || cc == ge) { |
| 4866 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 4867 | __ b(ne, &return_equal); |
| 4868 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 4869 | __ cmp(r0, Operand(r2)); |
| 4870 | __ b(ne, &return_equal); |
| 4871 | if (cc == le) { |
| 4872 | // undefined <= undefined should fail. |
| 4873 | __ mov(r0, Operand(GREATER)); |
| 4874 | } else { |
| 4875 | // undefined >= undefined should fail. |
| 4876 | __ mov(r0, Operand(LESS)); |
| 4877 | } |
| 4878 | __ mov(pc, Operand(lr)); // Return. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4879 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4880 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4881 | } |
| 4882 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4883 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4884 | __ bind(&return_equal); |
| 4885 | if (cc == lt) { |
| 4886 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4887 | } else if (cc == gt) { |
| 4888 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4889 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4890 | __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4891 | } |
| 4892 | __ mov(pc, Operand(lr)); // Return. |
| 4893 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4894 | if (cc != eq || !never_nan_nan) { |
| 4895 | // For less and greater we don't have to check for NaN since the result of |
| 4896 | // x < x is false regardless. For the others here is some code to check |
| 4897 | // for NaN. |
| 4898 | if (cc != lt && cc != gt) { |
| 4899 | __ bind(&heap_number); |
| 4900 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4901 | // not NaN. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4902 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4903 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4904 | // and not all mantissa bits (0..51) clear. |
| 4905 | // Read top bits of double representation (second word of value). |
| 4906 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4907 | // Test that exponent bits are all set. |
| 4908 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4909 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4910 | __ b(ne, &return_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4911 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4912 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4913 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4914 | // Or with all low-bits of mantissa. |
| 4915 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4916 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4917 | // 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] | 4918 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if |
| 4919 | // not (it's a NaN). For <= and >= we need to load r0 with the failing |
| 4920 | // value if it's a NaN. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4921 | if (cc != eq) { |
| 4922 | // All-zero means Infinity means equal. |
| 4923 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 4924 | if (cc == le) { |
| 4925 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 4926 | } else { |
| 4927 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 4928 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4929 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4930 | __ mov(pc, Operand(lr)); // Return. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4931 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4932 | // No fall through here. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4933 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4934 | |
| 4935 | __ bind(¬_identical); |
| 4936 | } |
| 4937 | |
| 4938 | |
| 4939 | // See comment at call site. |
| 4940 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4941 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4942 | Label* slow, |
| 4943 | bool strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4944 | Label rhs_is_smi; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4945 | __ tst(r0, Operand(kSmiTagMask)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4946 | __ b(eq, &rhs_is_smi); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4947 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4948 | // 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] | 4949 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4950 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4951 | // 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] | 4952 | // succeed. Return non-equal (r0 is already not zero) |
| 4953 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4954 | } else { |
| 4955 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4956 | // the runtime. |
| 4957 | __ b(ne, slow); |
| 4958 | } |
| 4959 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4960 | // Lhs (r1) is a smi, rhs (r0) is a number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4961 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4962 | // Convert lhs to a double in d7 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4963 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4964 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 4965 | __ vmov(s15, r7); |
| 4966 | __ vcvt(d7, s15); |
| 4967 | // Load the double from rhs, tagged HeapNumber r0, to d6. |
| 4968 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 4969 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4970 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4971 | __ push(lr); |
| 4972 | // Convert lhs to a double in r2, r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4973 | __ mov(r7, Operand(r1)); |
| 4974 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4975 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4976 | // Load rhs to a double in r0, r1. |
| 4977 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4978 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4979 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4982 | // 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] | 4983 | // since it's a smi. |
| 4984 | __ jmp(lhs_not_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4985 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4986 | __ bind(&rhs_is_smi); |
| 4987 | // 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] | 4988 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4989 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4990 | // 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] | 4991 | // succeed. Return non-equal. |
| 4992 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 4993 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4994 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4995 | // 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] | 4996 | // the runtime. |
| 4997 | __ b(ne, slow); |
| 4998 | } |
| 4999 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5000 | // Rhs (r0) is a smi, lhs (r1) is a heap number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5001 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5002 | // Convert rhs to a double in d6 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5003 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5004 | // Load the double from lhs, tagged HeapNumber r1, to d7. |
| 5005 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5006 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5007 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5008 | __ vmov(s13, r7); |
| 5009 | __ vcvt(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5010 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5011 | __ push(lr); |
| 5012 | // Load lhs to a double in r2, r3. |
| 5013 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5014 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5015 | // Convert rhs to a double in r0, r1. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5016 | __ mov(r7, Operand(r0)); |
| 5017 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5018 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5019 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5020 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5021 | // Fall through to both_loaded_as_doubles. |
| 5022 | } |
| 5023 | |
| 5024 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5025 | void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5026 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5027 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5028 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5029 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5030 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5031 | Label one_is_nan, neither_is_nan; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5032 | Label lhs_not_nan_exp_mask_is_loaded; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5033 | |
| 5034 | Register exp_mask_reg = r5; |
| 5035 | |
| 5036 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5037 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 5038 | __ cmp(r4, Operand(exp_mask_reg)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5039 | __ b(ne, &lhs_not_nan_exp_mask_is_loaded); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5040 | __ mov(r4, |
| 5041 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5042 | SetCC); |
| 5043 | __ b(ne, &one_is_nan); |
| 5044 | __ cmp(lhs_mantissa, Operand(0)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5045 | __ b(ne, &one_is_nan); |
| 5046 | |
| 5047 | __ bind(lhs_not_nan); |
| 5048 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 5049 | __ bind(&lhs_not_nan_exp_mask_is_loaded); |
| 5050 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 5051 | __ cmp(r4, Operand(exp_mask_reg)); |
| 5052 | __ b(ne, &neither_is_nan); |
| 5053 | __ mov(r4, |
| 5054 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5055 | SetCC); |
| 5056 | __ b(ne, &one_is_nan); |
| 5057 | __ cmp(rhs_mantissa, Operand(0)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5058 | __ b(eq, &neither_is_nan); |
| 5059 | |
| 5060 | __ bind(&one_is_nan); |
| 5061 | // NaN comparisons always fail. |
| 5062 | // Load whatever we need in r0 to make the comparison fail. |
| 5063 | if (cc == lt || cc == le) { |
| 5064 | __ mov(r0, Operand(GREATER)); |
| 5065 | } else { |
| 5066 | __ mov(r0, Operand(LESS)); |
| 5067 | } |
| 5068 | __ mov(pc, Operand(lr)); // Return. |
| 5069 | |
| 5070 | __ bind(&neither_is_nan); |
| 5071 | } |
| 5072 | |
| 5073 | |
| 5074 | // See comment at call site. |
| 5075 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 5076 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5077 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5078 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5079 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5080 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5081 | |
| 5082 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 5083 | if (cc == eq) { |
| 5084 | // Doubles are not equal unless they have the same bit pattern. |
| 5085 | // Exception: 0 and -0. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5086 | __ cmp(rhs_mantissa, Operand(lhs_mantissa)); |
| 5087 | __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5088 | // Return non-zero if the numbers are unequal. |
| 5089 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 5090 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5091 | __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5092 | // If exponents are equal then return 0. |
| 5093 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 5094 | |
| 5095 | // Exponents are unequal. The only way we can return that the numbers |
| 5096 | // are equal is if one is -0 and the other is 0. We already dealt |
| 5097 | // with the case where both are -0 or both are 0. |
| 5098 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 5099 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 5100 | // equal. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5101 | __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5102 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 5103 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 5104 | // Now they are equal if and only if the lhs exponent is zero in its |
| 5105 | // low 31 bits. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5106 | __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5107 | __ mov(pc, Operand(lr)); |
| 5108 | } else { |
| 5109 | // Call a native function to do a comparison between two non-NaNs. |
| 5110 | // Call C routine that may not cause GC or other trouble. |
| 5111 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 5112 | __ Jump(r5); // Tail call. |
| 5113 | } |
| 5114 | } |
| 5115 | |
| 5116 | |
| 5117 | // See comment at call site. |
| 5118 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 5119 | // If either operand is a JSObject or an oddball value, then they are |
| 5120 | // not equal since their pointers are different. |
| 5121 | // There is no test for undetectability in strict equality. |
| 5122 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 5123 | Label first_non_object; |
| 5124 | // Get the type of the first operand into r2 and compare it with |
| 5125 | // FIRST_JS_OBJECT_TYPE. |
| 5126 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 5127 | __ b(lt, &first_non_object); |
| 5128 | |
| 5129 | // Return non-zero (r0 is not zero) |
| 5130 | Label return_not_equal; |
| 5131 | __ bind(&return_not_equal); |
| 5132 | __ mov(pc, Operand(lr)); // Return. |
| 5133 | |
| 5134 | __ bind(&first_non_object); |
| 5135 | // Check for oddballs: true, false, null, undefined. |
| 5136 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 5137 | __ b(eq, &return_not_equal); |
| 5138 | |
| 5139 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 5140 | __ b(ge, &return_not_equal); |
| 5141 | |
| 5142 | // Check for oddballs: true, false, null, undefined. |
| 5143 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 5144 | __ b(eq, &return_not_equal); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5145 | |
| 5146 | // Now that we have the types we might as well check for symbol-symbol. |
| 5147 | // Ensure that no non-strings have the symbol bit set. |
| 5148 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5149 | ASSERT(kSymbolTag != 0); |
| 5150 | __ and_(r2, r2, Operand(r3)); |
| 5151 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5152 | __ b(ne, &return_not_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
| 5155 | |
| 5156 | // See comment at call site. |
| 5157 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 5158 | Label* both_loaded_as_doubles, |
| 5159 | Label* not_heap_numbers, |
| 5160 | Label* slow) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5161 | __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5162 | __ b(ne, not_heap_numbers); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5163 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5164 | __ cmp(r2, r3); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5165 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 5166 | |
| 5167 | // Both are heap numbers. Load them up then jump to the code we have |
| 5168 | // for that. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5169 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5170 | CpuFeatures::Scope scope(VFP3); |
| 5171 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5172 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5173 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5174 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5175 | } else { |
| 5176 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5177 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5178 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5179 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5180 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5181 | __ jmp(both_loaded_as_doubles); |
| 5182 | } |
| 5183 | |
| 5184 | |
| 5185 | // Fast negative check for symbol-to-symbol equality. |
| 5186 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 5187 | // r2 is object type of r0. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5188 | // Ensure that no non-strings have the symbol bit set. |
| 5189 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5190 | ASSERT(kSymbolTag != 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5191 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5192 | __ b(eq, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5193 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5194 | __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5195 | __ tst(r3, Operand(kIsSymbolMask)); |
| 5196 | __ b(eq, slow); |
| 5197 | |
| 5198 | // Both are symbols. We already checked they weren't the same pointer |
| 5199 | // so they are not equal. |
| 5200 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 5201 | __ mov(pc, Operand(lr)); // Return. |
| 5202 | } |
| 5203 | |
| 5204 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5205 | // On entry r0 (rhs) and r1 (lhs) are the values to be compared. |
| 5206 | // On exit r0 is 0, positive or negative to indicate the result of |
| 5207 | // the comparison. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5208 | void CompareStub::Generate(MacroAssembler* masm) { |
| 5209 | Label slow; // Call builtin. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5210 | Label not_smis, both_loaded_as_doubles, lhs_not_nan; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5211 | |
| 5212 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 5213 | // it is certain that at least one operand isn't a smi. |
| 5214 | |
| 5215 | // Handle the case where the objects are identical. Either returns the answer |
| 5216 | // 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] | 5217 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5218 | |
| 5219 | // If either is a Smi (we know that not both are), then they can only |
| 5220 | // be strictly equal if the other is a HeapNumber. |
| 5221 | ASSERT_EQ(0, kSmiTag); |
| 5222 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 5223 | __ and_(r2, r0, Operand(r1)); |
| 5224 | __ tst(r2, Operand(kSmiTagMask)); |
| 5225 | __ b(ne, ¬_smis); |
| 5226 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 5227 | // 1) Return the answer. |
| 5228 | // 2) Go to slow. |
| 5229 | // 3) Fall through to both_loaded_as_doubles. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5230 | // 4) Jump to lhs_not_nan. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5231 | // 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] | 5232 | // comparison. If VFP3 is supported the double values of the numbers have |
| 5233 | // been loaded into d7 and d6. Otherwise, the double values have been loaded |
| 5234 | // into r0, r1, r2, and r3. |
| 5235 | EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5236 | |
| 5237 | __ bind(&both_loaded_as_doubles); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5238 | // The arguments have been converted to doubles and stored in d6 and d7, if |
| 5239 | // VFP3 is supported, or in r0, r1, r2, and r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5240 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5241 | __ bind(&lhs_not_nan); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5242 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5243 | Label no_nan; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5244 | // ARMv7 VFP3 instructions to implement double precision comparison. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5245 | __ vcmp(d7, d6); |
| 5246 | __ vmrs(pc); // Move vector status bits to normal status bits. |
| 5247 | Label nan; |
| 5248 | __ b(vs, &nan); |
| 5249 | __ mov(r0, Operand(EQUAL), LeaveCC, eq); |
| 5250 | __ mov(r0, Operand(LESS), LeaveCC, lt); |
| 5251 | __ mov(r0, Operand(GREATER), LeaveCC, gt); |
| 5252 | __ mov(pc, Operand(lr)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5253 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5254 | __ bind(&nan); |
| 5255 | // If one of the sides was a NaN then the v flag is set. Load r0 with |
| 5256 | // whatever it takes to make the comparison fail, since comparisons with NaN |
| 5257 | // always fail. |
| 5258 | if (cc_ == lt || cc_ == le) { |
| 5259 | __ mov(r0, Operand(GREATER)); |
| 5260 | } else { |
| 5261 | __ mov(r0, Operand(LESS)); |
| 5262 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5263 | __ mov(pc, Operand(lr)); |
| 5264 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5265 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 5266 | // fall through if neither is a NaN. Also binds lhs_not_nan. |
| 5267 | EmitNanCheck(masm, &lhs_not_nan, cc_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5268 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 5269 | // answer. Never falls through. |
| 5270 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 5271 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5272 | |
| 5273 | __ bind(¬_smis); |
| 5274 | // At this point we know we are dealing with two different objects, |
| 5275 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 5276 | if (strict_) { |
| 5277 | // This returns non-equal for some object types, or falls through if it |
| 5278 | // was not lucky. |
| 5279 | EmitStrictTwoHeapObjectCompare(masm); |
| 5280 | } |
| 5281 | |
| 5282 | Label check_for_symbols; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5283 | Label flat_string_check; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5284 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 5285 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 5286 | // 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] | 5287 | // 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] | 5288 | EmitCheckForTwoHeapNumbers(masm, |
| 5289 | &both_loaded_as_doubles, |
| 5290 | &check_for_symbols, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5291 | &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5292 | |
| 5293 | __ bind(&check_for_symbols); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5294 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 5295 | // symbols. |
| 5296 | if (cc_ == eq && !strict_) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5297 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5298 | // of r0 on entry. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5299 | EmitCheckForSymbols(masm, &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5300 | } |
| 5301 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5302 | // Check for both being sequential ASCII strings, and inline if that is the |
| 5303 | // case. |
| 5304 | __ bind(&flat_string_check); |
| 5305 | |
| 5306 | __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow); |
| 5307 | |
| 5308 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 5309 | StringCompareStub::GenerateCompareFlatAsciiStrings(masm, |
| 5310 | r1, |
| 5311 | r0, |
| 5312 | r2, |
| 5313 | r3, |
| 5314 | r4, |
| 5315 | r5); |
| 5316 | // Never falls through to here. |
| 5317 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5318 | __ bind(&slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5319 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5320 | __ push(r1); |
| 5321 | __ push(r0); |
| 5322 | // Figure out which native to call and setup the arguments. |
| 5323 | Builtins::JavaScript native; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5324 | if (cc_ == eq) { |
| 5325 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 5326 | } else { |
| 5327 | native = Builtins::COMPARE; |
| 5328 | int ncr; // NaN compare result |
| 5329 | if (cc_ == lt || cc_ == le) { |
| 5330 | ncr = GREATER; |
| 5331 | } else { |
| 5332 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 5333 | ncr = LESS; |
| 5334 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5335 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 5336 | __ push(r0); |
| 5337 | } |
| 5338 | |
| 5339 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 5340 | // tagged as a small integer. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5341 | __ InvokeBuiltin(native, JUMP_JS); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
| 5344 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5345 | // Allocates a heap number or jumps to the label if the young space is full and |
| 5346 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5347 | static void AllocateHeapNumber( |
| 5348 | MacroAssembler* masm, |
| 5349 | Label* need_gc, // Jump here if young space is full. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5350 | Register result, // The tagged address of the new heap number. |
| 5351 | Register scratch1, // A scratch register. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5352 | Register scratch2) { // Another scratch register. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5353 | // Allocate an object in the heap for the heap number and tag it as a heap |
| 5354 | // object. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5355 | __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize, |
| 5356 | result, |
| 5357 | scratch1, |
| 5358 | scratch2, |
| 5359 | need_gc, |
| 5360 | TAG_OBJECT); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5361 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5362 | // Get heap number map and store it in the allocated object. |
| 5363 | __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex); |
| 5364 | __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | |
| 5368 | // We fall into this code if the operands were Smis, but the result was |
| 5369 | // 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] | 5370 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 5371 | // to call the C-implemented binary fp operation routines we need to end up |
| 5372 | // with the double precision floating point operands in r0 and r1 (for the |
| 5373 | // 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] | 5374 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 5375 | Label* not_smi, |
| 5376 | const Builtins::JavaScript& builtin, |
| 5377 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5378 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5379 | Label slow, slow_pop_2_first, do_the_call; |
| 5380 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 5381 | // Smi-smi case (overflow). |
| 5382 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 5383 | // The new heap number is in r5. r6 and r7 are scratch. |
| 5384 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5385 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5386 | // If we have floating point hardware, inline ADD, SUB, MUL, and DIV, |
| 5387 | // using registers d7 and d6 for the double values. |
| 5388 | bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && |
| 5389 | Token::MOD != operation; |
| 5390 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5391 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5392 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5393 | __ vmov(s15, r7); |
| 5394 | __ vcvt(d7, s15); |
| 5395 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5396 | __ vmov(s13, r7); |
| 5397 | __ vcvt(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5398 | } else { |
| 5399 | // Write Smi from r0 to r3 and r2 in double format. r6 is scratch. |
| 5400 | __ mov(r7, Operand(r0)); |
| 5401 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5402 | __ push(lr); |
| 5403 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 5404 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 5405 | __ mov(r7, Operand(r1)); |
| 5406 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5407 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 5408 | __ pop(lr); |
| 5409 | } |
| 5410 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5411 | __ jmp(&do_the_call); // Tail call. No return. |
| 5412 | |
| 5413 | // We jump to here if something goes wrong (one param is not a number of any |
| 5414 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5415 | __ bind(&slow); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5416 | |
| 5417 | // Push arguments to the stack |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5418 | __ push(r1); |
| 5419 | __ push(r0); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5420 | |
| 5421 | if (Token::ADD == operation) { |
| 5422 | // Test for string arguments before calling runtime. |
| 5423 | // r1 : first argument |
| 5424 | // r0 : second argument |
| 5425 | // sp[0] : second argument |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5426 | // sp[4] : first argument |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5427 | |
| 5428 | Label not_strings, not_string1, string1; |
| 5429 | __ tst(r1, Operand(kSmiTagMask)); |
| 5430 | __ b(eq, ¬_string1); |
| 5431 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 5432 | __ b(ge, ¬_string1); |
| 5433 | |
| 5434 | // First argument is a a string, test second. |
| 5435 | __ tst(r0, Operand(kSmiTagMask)); |
| 5436 | __ b(eq, &string1); |
| 5437 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5438 | __ b(ge, &string1); |
| 5439 | |
| 5440 | // First and second argument are strings. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5441 | StringAddStub stub(NO_STRING_CHECK_IN_STUB); |
| 5442 | __ TailCallStub(&stub); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5443 | |
| 5444 | // Only first argument is a string. |
| 5445 | __ bind(&string1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5446 | __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS); |
| 5447 | |
| 5448 | // First argument was not a string, test second. |
| 5449 | __ bind(¬_string1); |
| 5450 | __ tst(r0, Operand(kSmiTagMask)); |
| 5451 | __ b(eq, ¬_strings); |
| 5452 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5453 | __ b(ge, ¬_strings); |
| 5454 | |
| 5455 | // Only second argument is a string. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5456 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 5457 | |
| 5458 | __ bind(¬_strings); |
| 5459 | } |
| 5460 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5461 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5462 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5463 | // 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] | 5464 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5465 | if (mode == NO_OVERWRITE) { |
| 5466 | // In the case where there is no chance of an overwritable float we may as |
| 5467 | // well do the allocation immediately while r0 and r1 are untouched. |
| 5468 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5469 | } |
| 5470 | |
| 5471 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5472 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5473 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5474 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5475 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5476 | if (mode == OVERWRITE_RIGHT) { |
| 5477 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5478 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5479 | if (use_fp_registers) { |
| 5480 | CpuFeatures::Scope scope(VFP3); |
| 5481 | // Load the double from tagged HeapNumber r0 to d7. |
| 5482 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5483 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5484 | } else { |
| 5485 | // Calling convention says that second double is in r2 and r3. |
| 5486 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5487 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5488 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5489 | __ jmp(&finished_loading_r0); |
| 5490 | __ bind(&r0_is_smi); |
| 5491 | if (mode == OVERWRITE_RIGHT) { |
| 5492 | // 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] | 5493 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5494 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5495 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5496 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5497 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5498 | // Convert smi in r0 to double in d7. |
| 5499 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5500 | __ vmov(s15, r7); |
| 5501 | __ vcvt(d7, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5502 | } else { |
| 5503 | // Write Smi from r0 to r3 and r2 in double format. |
| 5504 | __ mov(r7, Operand(r0)); |
| 5505 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5506 | __ push(lr); |
| 5507 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5508 | __ pop(lr); |
| 5509 | } |
| 5510 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5511 | __ bind(&finished_loading_r0); |
| 5512 | |
| 5513 | // Move r1 to a double in r0-r1. |
| 5514 | __ tst(r1, Operand(kSmiTagMask)); |
| 5515 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5516 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5517 | __ b(ne, &slow); |
| 5518 | if (mode == OVERWRITE_LEFT) { |
| 5519 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5520 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5521 | if (use_fp_registers) { |
| 5522 | CpuFeatures::Scope scope(VFP3); |
| 5523 | // Load the double from tagged HeapNumber r1 to d6. |
| 5524 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5525 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5526 | } else { |
| 5527 | // Calling convention says that first double is in r0 and r1. |
| 5528 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5529 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5530 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5531 | __ jmp(&finished_loading_r1); |
| 5532 | __ bind(&r1_is_smi); |
| 5533 | if (mode == OVERWRITE_LEFT) { |
| 5534 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5535 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5536 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5537 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5538 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5539 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5540 | // Convert smi in r1 to double in d6. |
| 5541 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5542 | __ vmov(s13, r7); |
| 5543 | __ vcvt(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5544 | } else { |
| 5545 | // Write Smi from r1 to r1 and r0 in double format. |
| 5546 | __ mov(r7, Operand(r1)); |
| 5547 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5548 | __ push(lr); |
| 5549 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5550 | __ pop(lr); |
| 5551 | } |
| 5552 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5553 | __ bind(&finished_loading_r1); |
| 5554 | |
| 5555 | __ bind(&do_the_call); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5556 | // If we are inlining the operation using VFP3 instructions for |
| 5557 | // add, subtract, multiply, or divide, the arguments are in d6 and d7. |
| 5558 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5559 | CpuFeatures::Scope scope(VFP3); |
| 5560 | // ARMv7 VFP3 instructions to implement |
| 5561 | // double precision, add, subtract, multiply, divide. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5562 | |
| 5563 | if (Token::MUL == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5564 | __ vmul(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5565 | } else if (Token::DIV == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5566 | __ vdiv(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5567 | } else if (Token::ADD == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5568 | __ vadd(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5569 | } else if (Token::SUB == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5570 | __ vsub(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5571 | } else { |
| 5572 | UNREACHABLE(); |
| 5573 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5574 | __ sub(r0, r5, Operand(kHeapObjectTag)); |
| 5575 | __ vstr(d5, r0, HeapNumber::kValueOffset); |
| 5576 | __ add(r0, r0, Operand(kHeapObjectTag)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5577 | __ mov(pc, lr); |
| 5578 | return; |
| 5579 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5580 | |
| 5581 | // If we did not inline the operation, then the arguments are in: |
| 5582 | // r0: Left value (least significant part of mantissa). |
| 5583 | // r1: Left value (sign, exponent, top of mantissa). |
| 5584 | // r2: Right value (least significant part of mantissa). |
| 5585 | // r3: Right value (sign, exponent, top of mantissa). |
| 5586 | // r5: Address of heap number for result. |
| 5587 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5588 | __ push(lr); // For later. |
| 5589 | __ push(r5); // Address of heap number that is answer. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5590 | __ AlignStack(0); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5591 | // Call C routine that may not cause GC or other trouble. |
| 5592 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5593 | __ Call(r5); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5594 | __ pop(r4); // Address of heap number. |
| 5595 | __ cmp(r4, Operand(Smi::FromInt(0))); |
| 5596 | __ 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] | 5597 | // Store answer in the overwritable heap number. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5598 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5599 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5600 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5601 | // substract the tag from r4. |
| 5602 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5603 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5604 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5605 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5606 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5607 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5608 | #endif |
| 5609 | __ mov(r0, Operand(r4)); |
| 5610 | // And we are done. |
| 5611 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
| 5614 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5615 | // 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] | 5616 | // 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] | 5617 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5618 | // 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] | 5619 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5620 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5621 | static void GetInt32(MacroAssembler* masm, |
| 5622 | Register source, |
| 5623 | Register dest, |
| 5624 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5625 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5626 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5627 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5628 | // Get exponent word. |
| 5629 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5630 | // Get exponent alone in scratch2. |
| 5631 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5632 | // Load dest with zero. We use this either for the final shift or |
| 5633 | // for the answer. |
| 5634 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5635 | // 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] | 5636 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5637 | // the exponent that we are fastest at and also the highest exponent we can |
| 5638 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5639 | const uint32_t non_smi_exponent = |
| 5640 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5641 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5642 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5643 | __ b(eq, &right_exponent); |
| 5644 | // If the exponent is higher than that then go to slow case. This catches |
| 5645 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5646 | __ b(gt, slow); |
| 5647 | |
| 5648 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5649 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5650 | // it rounds to zero. |
| 5651 | const uint32_t zero_exponent = |
| 5652 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5653 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5654 | // Dest already has a Smi zero. |
| 5655 | __ b(lt, &done); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5656 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 5657 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5658 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5659 | // We now have the exponent in dest. Subtract from 30 to get |
| 5660 | // how much to shift down. |
| 5661 | __ rsb(dest, dest, Operand(30)); |
| 5662 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5663 | __ bind(&right_exponent); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5664 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5665 | CpuFeatures::Scope scope(VFP3); |
| 5666 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 5667 | // conversion using round to zero. |
| 5668 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5669 | __ vmov(d7, scratch2, scratch); |
| 5670 | __ vcvt(s15, d7); |
| 5671 | __ vmov(dest, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5672 | } else { |
| 5673 | // Get the top bits of the mantissa. |
| 5674 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5675 | // Put back the implicit 1. |
| 5676 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5677 | // Shift up the mantissa bits to take up the space the exponent used to |
| 5678 | // take. We just orred in the implicit bit so that took care of one and |
| 5679 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 5680 | // distance. |
| 5681 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5682 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5683 | // Put sign in zero flag. |
| 5684 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 5685 | // Get the second half of the double. For some exponents we don't |
| 5686 | // actually need this because the bits get shifted out again, but |
| 5687 | // it's probably slower to test than just to do it. |
| 5688 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5689 | // Shift down 22 bits to get the last 10 bits. |
| 5690 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5691 | // Move down according to the exponent. |
| 5692 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 5693 | // Fix sign if sign bit was set. |
| 5694 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 5695 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5696 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5697 | } |
| 5698 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5699 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5700 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5701 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5702 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5703 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5704 | // write the answer into (either by allocating or by overwriting). |
| 5705 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5706 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5707 | Label slow, result_not_a_smi; |
| 5708 | Label r0_is_smi, r1_is_smi; |
| 5709 | Label done_checking_r0, done_checking_r1; |
| 5710 | |
| 5711 | __ tst(r1, Operand(kSmiTagMask)); |
| 5712 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5713 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5714 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5715 | GetInt32(masm, r1, r3, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5716 | __ jmp(&done_checking_r1); |
| 5717 | __ bind(&r1_is_smi); |
| 5718 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5719 | __ bind(&done_checking_r1); |
| 5720 | |
| 5721 | __ tst(r0, Operand(kSmiTagMask)); |
| 5722 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5723 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5724 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5725 | GetInt32(masm, r0, r2, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5726 | __ jmp(&done_checking_r0); |
| 5727 | __ bind(&r0_is_smi); |
| 5728 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5729 | __ bind(&done_checking_r0); |
| 5730 | |
| 5731 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5732 | // r2 and r3: Signed int32 operands. |
| 5733 | switch (op_) { |
| 5734 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5735 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5736 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5737 | case Token::SAR: |
| 5738 | // Use only the 5 least significant bits of the shift count. |
| 5739 | __ and_(r2, r2, Operand(0x1f)); |
| 5740 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5741 | break; |
| 5742 | case Token::SHR: |
| 5743 | // Use only the 5 least significant bits of the shift count. |
| 5744 | __ and_(r2, r2, Operand(0x1f)); |
| 5745 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5746 | // SHR is special because it is required to produce a positive answer. |
| 5747 | // The code below for writing into heap numbers isn't capable of writing |
| 5748 | // the register as an unsigned int so we go to slow case if we hit this |
| 5749 | // case. |
| 5750 | __ b(mi, &slow); |
| 5751 | break; |
| 5752 | case Token::SHL: |
| 5753 | // Use only the 5 least significant bits of the shift count. |
| 5754 | __ and_(r2, r2, Operand(0x1f)); |
| 5755 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5756 | break; |
| 5757 | default: UNREACHABLE(); |
| 5758 | } |
| 5759 | // check that the *signed* result fits in a smi |
| 5760 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5761 | __ b(mi, &result_not_a_smi); |
| 5762 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5763 | __ Ret(); |
| 5764 | |
| 5765 | Label have_to_allocate, got_a_heap_number; |
| 5766 | __ bind(&result_not_a_smi); |
| 5767 | switch (mode_) { |
| 5768 | case OVERWRITE_RIGHT: { |
| 5769 | __ tst(r0, Operand(kSmiTagMask)); |
| 5770 | __ b(eq, &have_to_allocate); |
| 5771 | __ mov(r5, Operand(r0)); |
| 5772 | break; |
| 5773 | } |
| 5774 | case OVERWRITE_LEFT: { |
| 5775 | __ tst(r1, Operand(kSmiTagMask)); |
| 5776 | __ b(eq, &have_to_allocate); |
| 5777 | __ mov(r5, Operand(r1)); |
| 5778 | break; |
| 5779 | } |
| 5780 | case NO_OVERWRITE: { |
| 5781 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5782 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5783 | } |
| 5784 | default: break; |
| 5785 | } |
| 5786 | __ bind(&got_a_heap_number); |
| 5787 | // r2: Answer as signed int32. |
| 5788 | // r5: Heap number to write answer into. |
| 5789 | |
| 5790 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5791 | // result. |
| 5792 | __ mov(r0, Operand(r5)); |
| 5793 | |
| 5794 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5795 | // r3 as scratch. r0 is preserved and returned. |
| 5796 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5797 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5798 | |
| 5799 | if (mode_ != NO_OVERWRITE) { |
| 5800 | __ bind(&have_to_allocate); |
| 5801 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5802 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5803 | __ jmp(&got_a_heap_number); |
| 5804 | } |
| 5805 | |
| 5806 | // If all else failed then we go to the runtime system. |
| 5807 | __ bind(&slow); |
| 5808 | __ push(r1); // restore stack |
| 5809 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5810 | switch (op_) { |
| 5811 | case Token::BIT_OR: |
| 5812 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5813 | break; |
| 5814 | case Token::BIT_AND: |
| 5815 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5816 | break; |
| 5817 | case Token::BIT_XOR: |
| 5818 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5819 | break; |
| 5820 | case Token::SAR: |
| 5821 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5822 | break; |
| 5823 | case Token::SHR: |
| 5824 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5825 | break; |
| 5826 | case Token::SHL: |
| 5827 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5828 | break; |
| 5829 | default: |
| 5830 | UNREACHABLE(); |
| 5831 | } |
| 5832 | } |
| 5833 | |
| 5834 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5835 | // Can we multiply by x with max two shifts and an add. |
| 5836 | // This answers yes to all integers from 2 to 10. |
| 5837 | static bool IsEasyToMultiplyBy(int x) { |
| 5838 | if (x < 2) return false; // Avoid special cases. |
| 5839 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 5840 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 5841 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 5842 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 5843 | return false; |
| 5844 | } |
| 5845 | |
| 5846 | |
| 5847 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 5848 | // Source and destination may be the same register. This routine does |
| 5849 | // not set carry and overflow the way a mul instruction would. |
| 5850 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 5851 | Register source, |
| 5852 | Register destination, |
| 5853 | int known_int) { |
| 5854 | if (IsPowerOf2(known_int)) { |
| 5855 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 5856 | } else if (PopCountLessThanEqual2(known_int)) { |
| 5857 | int first_bit = BitPosition(known_int); |
| 5858 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 5859 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 5860 | if (first_bit != 0) { |
| 5861 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 5862 | } |
| 5863 | } else { |
| 5864 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 5865 | int the_bit = BitPosition(known_int + 1); |
| 5866 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 5867 | } |
| 5868 | } |
| 5869 | |
| 5870 | |
| 5871 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 5872 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 5873 | // a result that needs shifting. |
| 5874 | static void MultiplyByKnownInt2( |
| 5875 | MacroAssembler* masm, |
| 5876 | Register result, |
| 5877 | Register source, |
| 5878 | Register known_int_register, // Smi tagged. |
| 5879 | int known_int, |
| 5880 | int* required_shift) { // Including Smi tag shift |
| 5881 | switch (known_int) { |
| 5882 | case 3: |
| 5883 | __ add(result, source, Operand(source, LSL, 1)); |
| 5884 | *required_shift = 1; |
| 5885 | break; |
| 5886 | case 5: |
| 5887 | __ add(result, source, Operand(source, LSL, 2)); |
| 5888 | *required_shift = 1; |
| 5889 | break; |
| 5890 | case 6: |
| 5891 | __ add(result, source, Operand(source, LSL, 1)); |
| 5892 | *required_shift = 2; |
| 5893 | break; |
| 5894 | case 7: |
| 5895 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 5896 | *required_shift = 1; |
| 5897 | break; |
| 5898 | case 9: |
| 5899 | __ add(result, source, Operand(source, LSL, 3)); |
| 5900 | *required_shift = 1; |
| 5901 | break; |
| 5902 | case 10: |
| 5903 | __ add(result, source, Operand(source, LSL, 2)); |
| 5904 | *required_shift = 2; |
| 5905 | break; |
| 5906 | default: |
| 5907 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 5908 | __ mul(result, source, known_int_register); |
| 5909 | *required_shift = 0; |
| 5910 | } |
| 5911 | } |
| 5912 | |
| 5913 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5914 | const char* GenericBinaryOpStub::GetName() { |
| 5915 | if (name_ != NULL) return name_; |
| 5916 | const int len = 100; |
| 5917 | name_ = Bootstrapper::AllocateAutoDeletedArray(len); |
| 5918 | if (name_ == NULL) return "OOM"; |
| 5919 | const char* op_name = Token::Name(op_); |
| 5920 | const char* overwrite_name; |
| 5921 | switch (mode_) { |
| 5922 | case NO_OVERWRITE: overwrite_name = "Alloc"; break; |
| 5923 | case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; |
| 5924 | case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; |
| 5925 | default: overwrite_name = "UnknownOverwrite"; break; |
| 5926 | } |
| 5927 | |
| 5928 | OS::SNPrintF(Vector<char>(name_, len), |
| 5929 | "GenericBinaryOpStub_%s_%s%s", |
| 5930 | op_name, |
| 5931 | overwrite_name, |
| 5932 | specialized_on_rhs_ ? "_ConstantRhs" : 0); |
| 5933 | return name_; |
| 5934 | } |
| 5935 | |
| 5936 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5937 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5938 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5939 | // r1 : x |
| 5940 | // r0 : y |
| 5941 | // result : r0 |
| 5942 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5943 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 5944 | // tell us that. |
| 5945 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 5946 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5947 | switch (op_) { |
| 5948 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5949 | Label not_smi; |
| 5950 | // Fast path. |
| 5951 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5952 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5953 | __ b(ne, ¬_smi); |
| 5954 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 5955 | // Return if no overflow. |
| 5956 | __ Ret(vc); |
| 5957 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 5958 | |
| 5959 | HandleBinaryOpSlowCases(masm, |
| 5960 | ¬_smi, |
| 5961 | Builtins::ADD, |
| 5962 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5963 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5964 | break; |
| 5965 | } |
| 5966 | |
| 5967 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5968 | Label not_smi; |
| 5969 | // Fast path. |
| 5970 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5971 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5972 | __ b(ne, ¬_smi); |
| 5973 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 5974 | // Return if no overflow. |
| 5975 | __ Ret(vc); |
| 5976 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 5977 | |
| 5978 | HandleBinaryOpSlowCases(masm, |
| 5979 | ¬_smi, |
| 5980 | Builtins::SUB, |
| 5981 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5982 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5983 | break; |
| 5984 | } |
| 5985 | |
| 5986 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5987 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5988 | ASSERT(kSmiTag == 0); // adjust code below |
| 5989 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5990 | __ b(ne, ¬_smi); |
| 5991 | // 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] | 5992 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5993 | // Do multiplication |
| 5994 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 5995 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5996 | __ mov(ip, Operand(r3, ASR, 31)); |
| 5997 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 5998 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5999 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6000 | __ tst(r3, Operand(r3)); |
| 6001 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6002 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6003 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 6004 | // We know one of them was zero. |
| 6005 | __ add(r2, r0, Operand(r1), SetCC); |
| 6006 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 6007 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 6008 | // Slow case. We fall through here if we multiplied a negative number |
| 6009 | // with 0, because that would mean we should produce -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6010 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6011 | |
| 6012 | HandleBinaryOpSlowCases(masm, |
| 6013 | ¬_smi, |
| 6014 | Builtins::MUL, |
| 6015 | Token::MUL, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6016 | mode_); |
| 6017 | break; |
| 6018 | } |
| 6019 | |
| 6020 | case Token::DIV: |
| 6021 | case Token::MOD: { |
| 6022 | Label not_smi; |
| 6023 | if (specialized_on_rhs_) { |
| 6024 | Label smi_is_unsuitable; |
| 6025 | __ BranchOnNotSmi(r1, ¬_smi); |
| 6026 | if (IsPowerOf2(constant_rhs_)) { |
| 6027 | if (op_ == Token::MOD) { |
| 6028 | __ and_(r0, |
| 6029 | r1, |
| 6030 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 6031 | SetCC); |
| 6032 | // We now have the answer, but if the input was negative we also |
| 6033 | // have the sign bit. Our work is done if the result is |
| 6034 | // positive or zero: |
| 6035 | __ Ret(pl); |
| 6036 | // A mod of a negative left hand side must return a negative number. |
| 6037 | // Unfortunately if the answer is 0 then we must return -0. And we |
| 6038 | // already optimistically trashed r0 so we may need to restore it. |
| 6039 | __ eor(r0, r0, Operand(0x80000000u), SetCC); |
| 6040 | // Next two instructions are conditional on the answer being -0. |
| 6041 | __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
| 6042 | __ b(eq, &smi_is_unsuitable); |
| 6043 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
| 6044 | __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_))); |
| 6045 | } else { |
| 6046 | ASSERT(op_ == Token::DIV); |
| 6047 | __ tst(r1, |
| 6048 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 6049 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 6050 | int shift = 0; |
| 6051 | int d = constant_rhs_; |
| 6052 | while ((d & 1) == 0) { |
| 6053 | d >>= 1; |
| 6054 | shift++; |
| 6055 | } |
| 6056 | __ mov(r0, Operand(r1, LSR, shift)); |
| 6057 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 6058 | } |
| 6059 | } else { |
| 6060 | // Not a power of 2. |
| 6061 | __ tst(r1, Operand(0x80000000u)); |
| 6062 | __ b(ne, &smi_is_unsuitable); |
| 6063 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 6064 | // multiplying. |
| 6065 | double divisor = 1.0 / constant_rhs_; |
| 6066 | int shift = 32; |
| 6067 | double scale = 4294967296.0; // 1 << 32. |
| 6068 | uint32_t mul; |
| 6069 | // Maximise the precision of the fixed point reciprocal. |
| 6070 | while (true) { |
| 6071 | mul = static_cast<uint32_t>(scale * divisor); |
| 6072 | if (mul >= 0x7fffffff) break; |
| 6073 | scale *= 2.0; |
| 6074 | shift++; |
| 6075 | } |
| 6076 | mul++; |
| 6077 | __ mov(r2, Operand(mul)); |
| 6078 | __ umull(r3, r2, r2, r1); |
| 6079 | __ mov(r2, Operand(r2, LSR, shift - 31)); |
| 6080 | // r2 is r1 / rhs. r2 is not Smi tagged. |
| 6081 | // r0 is still the known rhs. r0 is Smi tagged. |
| 6082 | // r1 is still the unkown lhs. r1 is Smi tagged. |
| 6083 | int required_r4_shift = 0; // Including the Smi tag shift of 1. |
| 6084 | // r4 = r2 * r0. |
| 6085 | MultiplyByKnownInt2(masm, |
| 6086 | r4, |
| 6087 | r2, |
| 6088 | r0, |
| 6089 | constant_rhs_, |
| 6090 | &required_r4_shift); |
| 6091 | // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs). |
| 6092 | if (op_ == Token::DIV) { |
| 6093 | __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC); |
| 6094 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
| 6095 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 6096 | } else { |
| 6097 | ASSERT(op_ == Token::MOD); |
| 6098 | __ sub(r0, r1, Operand(r4, LSL, required_r4_shift)); |
| 6099 | } |
| 6100 | } |
| 6101 | __ Ret(); |
| 6102 | __ bind(&smi_is_unsuitable); |
| 6103 | } else { |
| 6104 | __ jmp(¬_smi); |
| 6105 | } |
| 6106 | HandleBinaryOpSlowCases(masm, |
| 6107 | ¬_smi, |
| 6108 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV, |
| 6109 | op_, |
| 6110 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6111 | break; |
| 6112 | } |
| 6113 | |
| 6114 | case Token::BIT_OR: |
| 6115 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6116 | case Token::BIT_XOR: |
| 6117 | case Token::SAR: |
| 6118 | case Token::SHR: |
| 6119 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6120 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6121 | ASSERT(kSmiTag == 0); // adjust code below |
| 6122 | __ tst(r2, Operand(kSmiTagMask)); |
| 6123 | __ b(ne, &slow); |
| 6124 | switch (op_) { |
| 6125 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 6126 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 6127 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6128 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6129 | // Remove tags from right operand. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6130 | __ GetLeastBitsFromSmi(r2, r0, 5); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6131 | __ mov(r0, Operand(r1, ASR, r2)); |
| 6132 | // Smi tag result. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6133 | __ bic(r0, r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6134 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6135 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6136 | // Remove tags from operands. We can't do this on a 31 bit number |
| 6137 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 6138 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6139 | __ GetLeastBitsFromSmi(r2, r0, 5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6140 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6141 | // Unsigned shift is not allowed to produce a negative number, so |
| 6142 | // check the sign bit and the sign bit after Smi tagging. |
| 6143 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6144 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6145 | // Smi tag result. |
| 6146 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6147 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6148 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6149 | // Remove tags from operands. |
| 6150 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6151 | __ GetLeastBitsFromSmi(r2, r0, 5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6152 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6153 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6154 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 6155 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6156 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6157 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6158 | default: UNREACHABLE(); |
| 6159 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6160 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6161 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6162 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6163 | break; |
| 6164 | } |
| 6165 | |
| 6166 | default: UNREACHABLE(); |
| 6167 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6168 | // This code should be unreachable. |
| 6169 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6170 | } |
| 6171 | |
| 6172 | |
| 6173 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 6174 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 6175 | // argument, so give it a Smi. |
| 6176 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6177 | __ push(r0); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6178 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6179 | |
| 6180 | __ StubReturn(1); |
| 6181 | } |
| 6182 | |
| 6183 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6184 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6185 | Label slow, done; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6186 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6187 | if (op_ == Token::SUB) { |
| 6188 | // Check whether the value is a smi. |
| 6189 | Label try_float; |
| 6190 | __ tst(r0, Operand(kSmiTagMask)); |
| 6191 | __ b(ne, &try_float); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6192 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6193 | // Go slow case if the value of the expression is zero |
| 6194 | // to make sure that we switch between 0 and -0. |
| 6195 | __ cmp(r0, Operand(0)); |
| 6196 | __ b(eq, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6197 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6198 | // The value of the expression is a smi that is not zero. Try |
| 6199 | // optimistic subtraction '0 - value'. |
| 6200 | __ rsb(r1, r0, Operand(0), SetCC); |
| 6201 | __ b(vs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6202 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6203 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 6204 | __ b(&done); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6205 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6206 | __ bind(&try_float); |
| 6207 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6208 | __ b(ne, &slow); |
| 6209 | // r0 is a heap number. Get a new heap number in r1. |
| 6210 | if (overwrite_) { |
| 6211 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6212 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6213 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6214 | } else { |
| 6215 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
| 6216 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 6217 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6218 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 6219 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6220 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 6221 | __ mov(r0, Operand(r1)); |
| 6222 | } |
| 6223 | } else if (op_ == Token::BIT_NOT) { |
| 6224 | // Check if the operand is a heap number. |
| 6225 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6226 | __ b(ne, &slow); |
| 6227 | |
| 6228 | // Convert the heap number is r0 to an untagged integer in r1. |
| 6229 | GetInt32(masm, r0, r1, r2, r3, &slow); |
| 6230 | |
| 6231 | // Do the bitwise operation (move negated) and check if the result |
| 6232 | // fits in a smi. |
| 6233 | Label try_float; |
| 6234 | __ mvn(r1, Operand(r1)); |
| 6235 | __ add(r2, r1, Operand(0x40000000), SetCC); |
| 6236 | __ b(mi, &try_float); |
| 6237 | __ mov(r0, Operand(r1, LSL, kSmiTagSize)); |
| 6238 | __ b(&done); |
| 6239 | |
| 6240 | __ bind(&try_float); |
| 6241 | if (!overwrite_) { |
| 6242 | // Allocate a fresh heap number, but don't overwrite r0 until |
| 6243 | // we're sure we can do it without going through the slow case |
| 6244 | // that needs the value in r0. |
| 6245 | AllocateHeapNumber(masm, &slow, r2, r3, r4); |
| 6246 | __ mov(r0, Operand(r2)); |
| 6247 | } |
| 6248 | |
| 6249 | // WriteInt32ToHeapNumberStub does not trigger GC, so we do not |
| 6250 | // have to set up a frame. |
| 6251 | WriteInt32ToHeapNumberStub stub(r1, r0, r2); |
| 6252 | __ push(lr); |
| 6253 | __ Call(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 6254 | __ pop(lr); |
| 6255 | } else { |
| 6256 | UNIMPLEMENTED(); |
| 6257 | } |
| 6258 | |
| 6259 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6260 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6261 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6262 | // Handle the slow case by jumping to the JavaScript builtin. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6263 | __ bind(&slow); |
| 6264 | __ push(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6265 | switch (op_) { |
| 6266 | case Token::SUB: |
| 6267 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 6268 | break; |
| 6269 | case Token::BIT_NOT: |
| 6270 | __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS); |
| 6271 | break; |
| 6272 | default: |
| 6273 | UNREACHABLE(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6274 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
| 6277 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6278 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6279 | // r0 holds the exception. |
| 6280 | |
| 6281 | // Adjust this code if not the case. |
| 6282 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6283 | |
| 6284 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6285 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 6286 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6287 | |
| 6288 | // Restore the next handler and frame pointer, discard handler state. |
| 6289 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 6290 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6291 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6292 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6293 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 6294 | |
| 6295 | // Before returning we restore the context from the frame pointer if |
| 6296 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6297 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6298 | __ cmp(fp, Operand(0)); |
| 6299 | // Set cp to NULL if fp is NULL. |
| 6300 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6301 | // Restore cp otherwise. |
| 6302 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6303 | #ifdef DEBUG |
| 6304 | if (FLAG_debug_code) { |
| 6305 | __ mov(lr, Operand(pc)); |
| 6306 | } |
| 6307 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6308 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6309 | __ pop(pc); |
| 6310 | } |
| 6311 | |
| 6312 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6313 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 6314 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6315 | // Adjust this code if not the case. |
| 6316 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6317 | |
| 6318 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6319 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6320 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6321 | |
| 6322 | // Unwind the handlers until the ENTRY handler is found. |
| 6323 | Label loop, done; |
| 6324 | __ bind(&loop); |
| 6325 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6326 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 6327 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6328 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 6329 | __ b(eq, &done); |
| 6330 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6331 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 6332 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6333 | __ jmp(&loop); |
| 6334 | __ bind(&done); |
| 6335 | |
| 6336 | // 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] | 6337 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6338 | __ pop(r2); |
| 6339 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6340 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6341 | if (type == OUT_OF_MEMORY) { |
| 6342 | // Set external caught exception to false. |
| 6343 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 6344 | __ mov(r0, Operand(false)); |
| 6345 | __ mov(r2, Operand(external_caught)); |
| 6346 | __ str(r0, MemOperand(r2)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6347 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6348 | // Set pending exception and r0 to out of memory exception. |
| 6349 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6350 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6351 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 6352 | __ str(r0, MemOperand(r2)); |
| 6353 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6354 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6355 | // Stack layout at this point. See also StackHandlerConstants. |
| 6356 | // sp -> state (ENTRY) |
| 6357 | // fp |
| 6358 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6359 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6360 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 6361 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6362 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 6363 | // Before returning we restore the context from the frame pointer if |
| 6364 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6365 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6366 | __ cmp(fp, Operand(0)); |
| 6367 | // Set cp to NULL if fp is NULL. |
| 6368 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6369 | // Restore cp otherwise. |
| 6370 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6371 | #ifdef DEBUG |
| 6372 | if (FLAG_debug_code) { |
| 6373 | __ mov(lr, Operand(pc)); |
| 6374 | } |
| 6375 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6376 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6377 | __ pop(pc); |
| 6378 | } |
| 6379 | |
| 6380 | |
| 6381 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 6382 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6383 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6384 | Label* throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6385 | bool do_gc, |
| 6386 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6387 | // r0: result parameter for PerformGC, if any |
| 6388 | // r4: number of arguments including receiver (C callee-saved) |
| 6389 | // r5: pointer to builtin function (C callee-saved) |
| 6390 | // r6: pointer to the first argument (C callee-saved) |
| 6391 | |
| 6392 | if (do_gc) { |
| 6393 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6394 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 6395 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6396 | } |
| 6397 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6398 | ExternalReference scope_depth = |
| 6399 | ExternalReference::heap_always_allocate_scope_depth(); |
| 6400 | if (always_allocate) { |
| 6401 | __ mov(r0, Operand(scope_depth)); |
| 6402 | __ ldr(r1, MemOperand(r0)); |
| 6403 | __ add(r1, r1, Operand(1)); |
| 6404 | __ str(r1, MemOperand(r0)); |
| 6405 | } |
| 6406 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6407 | // Call C built-in. |
| 6408 | // r0 = argc, r1 = argv |
| 6409 | __ mov(r0, Operand(r4)); |
| 6410 | __ mov(r1, Operand(r6)); |
| 6411 | |
| 6412 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 6413 | // frames, we need to know where the return address is. Right now, |
| 6414 | // we push it on the stack to be able to find it again, but we never |
| 6415 | // restore from it in case of changes, which makes it impossible to |
| 6416 | // support moving the C entry code stub. This should be fixed, but currently |
| 6417 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 6418 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6419 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 6420 | masm->push(lr); |
| 6421 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6422 | |
| 6423 | if (always_allocate) { |
| 6424 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 6425 | // though (contain the result). |
| 6426 | __ mov(r2, Operand(scope_depth)); |
| 6427 | __ ldr(r3, MemOperand(r2)); |
| 6428 | __ sub(r3, r3, Operand(1)); |
| 6429 | __ str(r3, MemOperand(r2)); |
| 6430 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6431 | |
| 6432 | // check for failure result |
| 6433 | Label failure_returned; |
| 6434 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 6435 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 6436 | __ add(r2, r0, Operand(1)); |
| 6437 | __ tst(r2, Operand(kFailureTagMask)); |
| 6438 | __ b(eq, &failure_returned); |
| 6439 | |
| 6440 | // Exit C frame and return. |
| 6441 | // r0:r1: result |
| 6442 | // sp: stack pointer |
| 6443 | // fp: frame pointer |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6444 | __ LeaveExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6445 | |
| 6446 | // check if we should retry or throw exception |
| 6447 | Label retry; |
| 6448 | __ bind(&failure_returned); |
| 6449 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 6450 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 6451 | __ b(eq, &retry); |
| 6452 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6453 | // Special handling of out of memory exceptions. |
| 6454 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6455 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6456 | __ b(eq, throw_out_of_memory_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6457 | |
| 6458 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6459 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6460 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6461 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6462 | __ ldr(r0, MemOperand(ip)); |
| 6463 | __ str(r3, MemOperand(ip)); |
| 6464 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6465 | // Special handling of termination exceptions which are uncatchable |
| 6466 | // by javascript code. |
| 6467 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 6468 | __ b(eq, throw_termination_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6469 | |
| 6470 | // Handle normal exception. |
| 6471 | __ jmp(throw_normal_exception); |
| 6472 | |
| 6473 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 6474 | } |
| 6475 | |
| 6476 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6477 | void CEntryStub::Generate(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6478 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 6479 | // r0: number of arguments including receiver |
| 6480 | // r1: pointer to builtin function |
| 6481 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6482 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6483 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6484 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6485 | // Result returned in r0 or r0+r1 by default. |
| 6486 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6487 | // NOTE: Invocations of builtins may return failure objects |
| 6488 | // instead of a proper result. The builtin entry handles |
| 6489 | // this by performing a garbage collection and retrying the |
| 6490 | // builtin once. |
| 6491 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6492 | // Enter the exit frame that transitions from JavaScript to C++. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6493 | __ EnterExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6494 | |
| 6495 | // r4: number of arguments (C callee-saved) |
| 6496 | // r5: pointer to builtin function (C callee-saved) |
| 6497 | // r6: pointer to first argument (C callee-saved) |
| 6498 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6499 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6500 | Label throw_termination_exception; |
| 6501 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6502 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6503 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6504 | GenerateCore(masm, |
| 6505 | &throw_normal_exception, |
| 6506 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6507 | &throw_out_of_memory_exception, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6508 | false, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6509 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6510 | |
| 6511 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6512 | GenerateCore(masm, |
| 6513 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6514 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6515 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6516 | true, |
| 6517 | false); |
| 6518 | |
| 6519 | // Do full GC and retry runtime call one final time. |
| 6520 | Failure* failure = Failure::InternalError(); |
| 6521 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 6522 | GenerateCore(masm, |
| 6523 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6524 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6525 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6526 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6527 | true); |
| 6528 | |
| 6529 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6530 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 6531 | |
| 6532 | __ bind(&throw_termination_exception); |
| 6533 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6534 | |
| 6535 | __ bind(&throw_normal_exception); |
| 6536 | GenerateThrowTOS(masm); |
| 6537 | } |
| 6538 | |
| 6539 | |
| 6540 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 6541 | // r0: code entry |
| 6542 | // r1: function |
| 6543 | // r2: receiver |
| 6544 | // r3: argc |
| 6545 | // [sp+0]: argv |
| 6546 | |
| 6547 | Label invoke, exit; |
| 6548 | |
| 6549 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 6550 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6551 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6552 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 6553 | |
| 6554 | // Get address of argv, see stm above. |
| 6555 | // r0: code entry |
| 6556 | // r1: function |
| 6557 | // r2: receiver |
| 6558 | // r3: argc |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6559 | __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6560 | |
| 6561 | // Push a frame with special values setup to mark it as an entry frame. |
| 6562 | // r0: code entry |
| 6563 | // r1: function |
| 6564 | // r2: receiver |
| 6565 | // r3: argc |
| 6566 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6567 | __ 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] | 6568 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 6569 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6570 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 6571 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6572 | __ ldr(r5, MemOperand(r5)); |
| 6573 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 6574 | |
| 6575 | // Setup frame pointer for the frame to be pushed. |
| 6576 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6577 | |
| 6578 | // Call a faked try-block that does the invoke. |
| 6579 | __ bl(&invoke); |
| 6580 | |
| 6581 | // Caught exception: Store result (exception) in the pending |
| 6582 | // exception field in the JSEnv and return a failure sentinel. |
| 6583 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 6584 | // 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] | 6585 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6586 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6587 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6588 | __ b(&exit); |
| 6589 | |
| 6590 | // Invoke: Link this frame into the handler chain. |
| 6591 | __ bind(&invoke); |
| 6592 | // Must preserve r0-r4, r5-r7 are available. |
| 6593 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6594 | // If an exception not caught by another handler occurs, this handler |
| 6595 | // returns control to the code after the bl(&invoke) above, which |
| 6596 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 6597 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6598 | |
| 6599 | // Clear any pending exceptions. |
| 6600 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6601 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6602 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6603 | __ str(r5, MemOperand(ip)); |
| 6604 | |
| 6605 | // Invoke the function by calling through JS entry trampoline builtin. |
| 6606 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6607 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6608 | |
| 6609 | // Expected registers by Builtins::JSEntryTrampoline |
| 6610 | // r0: code entry |
| 6611 | // r1: function |
| 6612 | // r2: receiver |
| 6613 | // r3: argc |
| 6614 | // r4: argv |
| 6615 | if (is_construct) { |
| 6616 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6617 | __ mov(ip, Operand(construct_entry)); |
| 6618 | } else { |
| 6619 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6620 | __ mov(ip, Operand(entry)); |
| 6621 | } |
| 6622 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6623 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6624 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6625 | // macro for the add instruction because we don't want the coverage tool |
| 6626 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6627 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6628 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6629 | |
| 6630 | // Unlink this frame from the handler chain. When reading the |
| 6631 | // address of the next handler, there is no need to use the address |
| 6632 | // displacement since the current stack pointer (sp) points directly |
| 6633 | // to the stack handler. |
| 6634 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6635 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6636 | __ str(r3, MemOperand(ip)); |
| 6637 | // No need to restore registers |
| 6638 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6639 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6640 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6641 | __ bind(&exit); // r0 holds result |
| 6642 | // Restore the top frame descriptors from the stack. |
| 6643 | __ pop(r3); |
| 6644 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6645 | __ str(r3, MemOperand(ip)); |
| 6646 | |
| 6647 | // Reset the stack to the callee saved registers. |
| 6648 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6649 | |
| 6650 | // Restore callee-saved registers and return. |
| 6651 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6652 | if (FLAG_debug_code) { |
| 6653 | __ mov(lr, Operand(pc)); |
| 6654 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6655 | #endif |
| 6656 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6657 | } |
| 6658 | |
| 6659 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6660 | // This stub performs an instanceof, calling the builtin function if |
| 6661 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6662 | // be an instance of (these are fetched from the stack). |
| 6663 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6664 | // Get the object - slow case for smis (we may need to throw an exception |
| 6665 | // depending on the rhs). |
| 6666 | Label slow, loop, is_instance, is_not_instance; |
| 6667 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6668 | __ BranchOnSmi(r0, &slow); |
| 6669 | |
| 6670 | // Check that the left hand is a JS object and put map in r3. |
| 6671 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6672 | __ b(lt, &slow); |
| 6673 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6674 | __ b(gt, &slow); |
| 6675 | |
| 6676 | // Get the prototype of the function (r4 is result, r2 is scratch). |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6677 | __ ldr(r1, MemOperand(sp, 0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6678 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6679 | |
| 6680 | // Check that the function prototype is a JS object. |
| 6681 | __ BranchOnSmi(r4, &slow); |
| 6682 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6683 | __ b(lt, &slow); |
| 6684 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6685 | __ b(gt, &slow); |
| 6686 | |
| 6687 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6688 | // Get prototype of object into r2. |
| 6689 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6690 | |
| 6691 | // Loop through the prototype chain looking for the function prototype. |
| 6692 | __ bind(&loop); |
| 6693 | __ cmp(r2, Operand(r4)); |
| 6694 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6695 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 6696 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6697 | __ b(eq, &is_not_instance); |
| 6698 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6699 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6700 | __ jmp(&loop); |
| 6701 | |
| 6702 | __ bind(&is_instance); |
| 6703 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6704 | __ pop(); |
| 6705 | __ pop(); |
| 6706 | __ mov(pc, Operand(lr)); // Return. |
| 6707 | |
| 6708 | __ bind(&is_not_instance); |
| 6709 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6710 | __ pop(); |
| 6711 | __ pop(); |
| 6712 | __ mov(pc, Operand(lr)); // Return. |
| 6713 | |
| 6714 | // Slow-case. Tail call builtin. |
| 6715 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6716 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6717 | } |
| 6718 | |
| 6719 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6720 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6721 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6722 | Label adaptor; |
| 6723 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6724 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6725 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6726 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6727 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6728 | // Nothing to do: The formal number of parameters has already been |
| 6729 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6730 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6731 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6732 | // Arguments adaptor case: Read the arguments length from the |
| 6733 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6734 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6735 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6736 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6737 | } |
| 6738 | |
| 6739 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6740 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6741 | // The displacement is the offset of the last parameter (if any) |
| 6742 | // relative to the frame pointer. |
| 6743 | static const int kDisplacement = |
| 6744 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6745 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6746 | // Check that the key is a smi. |
| 6747 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6748 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6749 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6750 | // Check if the calling frame is an arguments adaptor frame. |
| 6751 | Label adaptor; |
| 6752 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6753 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6754 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6755 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6756 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6757 | // Check index against formal parameters count limit passed in |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6758 | // through register r0. Use unsigned comparison to get negative |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6759 | // check for free. |
| 6760 | __ cmp(r1, r0); |
| 6761 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6762 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6763 | // Read the argument from the stack and return it. |
| 6764 | __ sub(r3, r0, r1); |
| 6765 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6766 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6767 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6768 | |
| 6769 | // Arguments adaptor case: Check index against actual arguments |
| 6770 | // limit found in the arguments adaptor frame. Use unsigned |
| 6771 | // comparison to get negative check for free. |
| 6772 | __ bind(&adaptor); |
| 6773 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6774 | __ cmp(r1, r0); |
| 6775 | __ b(cs, &slow); |
| 6776 | |
| 6777 | // Read the argument from the adaptor frame and return it. |
| 6778 | __ sub(r3, r0, r1); |
| 6779 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6780 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6781 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6782 | |
| 6783 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6784 | // by calling the runtime system. |
| 6785 | __ bind(&slow); |
| 6786 | __ push(r1); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6787 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6788 | } |
| 6789 | |
| 6790 | |
| 6791 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6792 | // sp[0] : number of parameters |
| 6793 | // sp[4] : receiver displacement |
| 6794 | // sp[8] : function |
| 6795 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6796 | // Check if the calling frame is an arguments adaptor frame. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6797 | Label adaptor_frame, try_allocate, runtime; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6798 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6799 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6800 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6801 | __ b(eq, &adaptor_frame); |
| 6802 | |
| 6803 | // Get the length from the frame. |
| 6804 | __ ldr(r1, MemOperand(sp, 0)); |
| 6805 | __ b(&try_allocate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6806 | |
| 6807 | // Patch the arguments.length and the parameters pointer. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6808 | __ bind(&adaptor_frame); |
| 6809 | __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6810 | __ str(r1, MemOperand(sp, 0)); |
| 6811 | __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6812 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6813 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6814 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6815 | // Try the new space allocation. Start out with computing the size |
| 6816 | // of the arguments object and the elements array (in words, not |
| 6817 | // bytes because AllocateInNewSpace expects words). |
| 6818 | Label add_arguments_object; |
| 6819 | __ bind(&try_allocate); |
| 6820 | __ cmp(r1, Operand(0)); |
| 6821 | __ b(eq, &add_arguments_object); |
| 6822 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 6823 | __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize)); |
| 6824 | __ bind(&add_arguments_object); |
| 6825 | __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize)); |
| 6826 | |
| 6827 | // Do the allocation of both objects in one go. |
| 6828 | __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT); |
| 6829 | |
| 6830 | // Get the arguments boilerplate from the current (global) context. |
| 6831 | int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX); |
| 6832 | __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 6833 | __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset)); |
| 6834 | __ ldr(r4, MemOperand(r4, offset)); |
| 6835 | |
| 6836 | // Copy the JS object part. |
| 6837 | for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) { |
| 6838 | __ ldr(r3, FieldMemOperand(r4, i)); |
| 6839 | __ str(r3, FieldMemOperand(r0, i)); |
| 6840 | } |
| 6841 | |
| 6842 | // Setup the callee in-object property. |
| 6843 | ASSERT(Heap::arguments_callee_index == 0); |
| 6844 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 6845 | __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize)); |
| 6846 | |
| 6847 | // Get the length (smi tagged) and set that as an in-object property too. |
| 6848 | ASSERT(Heap::arguments_length_index == 1); |
| 6849 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 6850 | __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize)); |
| 6851 | |
| 6852 | // If there are no actual arguments, we're done. |
| 6853 | Label done; |
| 6854 | __ cmp(r1, Operand(0)); |
| 6855 | __ b(eq, &done); |
| 6856 | |
| 6857 | // Get the parameters pointer from the stack and untag the length. |
| 6858 | __ ldr(r2, MemOperand(sp, 1 * kPointerSize)); |
| 6859 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 6860 | |
| 6861 | // Setup the elements pointer in the allocated arguments object and |
| 6862 | // initialize the header in the elements fixed array. |
| 6863 | __ add(r4, r0, Operand(Heap::kArgumentsObjectSize)); |
| 6864 | __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 6865 | __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex); |
| 6866 | __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset)); |
| 6867 | __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset)); |
| 6868 | |
| 6869 | // Copy the fixed array slots. |
| 6870 | Label loop; |
| 6871 | // Setup r4 to point to the first array slot. |
| 6872 | __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 6873 | __ bind(&loop); |
| 6874 | // Pre-decrement r2 with kPointerSize on each iteration. |
| 6875 | // Pre-decrement in order to skip receiver. |
| 6876 | __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex)); |
| 6877 | // Post-increment r4 with kPointerSize on each iteration. |
| 6878 | __ str(r3, MemOperand(r4, kPointerSize, PostIndex)); |
| 6879 | __ sub(r1, r1, Operand(1)); |
| 6880 | __ cmp(r1, Operand(0)); |
| 6881 | __ b(ne, &loop); |
| 6882 | |
| 6883 | // Return and remove the on-stack parameters. |
| 6884 | __ bind(&done); |
| 6885 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 6886 | __ Ret(); |
| 6887 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6888 | // Do the runtime call to allocate the arguments object. |
| 6889 | __ bind(&runtime); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6890 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6891 | } |
| 6892 | |
| 6893 | |
| 6894 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6895 | Label slow; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6896 | |
| 6897 | // If the receiver might be a value (string, number or boolean) check for this |
| 6898 | // and box it if it is. |
| 6899 | if (ReceiverMightBeValue()) { |
| 6900 | // Get the receiver from the stack. |
| 6901 | // function, receiver [, arguments] |
| 6902 | Label receiver_is_value, receiver_is_js_object; |
| 6903 | __ ldr(r1, MemOperand(sp, argc_ * kPointerSize)); |
| 6904 | |
| 6905 | // Check if receiver is a smi (which is a number value). |
| 6906 | __ BranchOnSmi(r1, &receiver_is_value); |
| 6907 | |
| 6908 | // Check if the receiver is a valid JS object. |
| 6909 | __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 6910 | __ b(ge, &receiver_is_js_object); |
| 6911 | |
| 6912 | // Call the runtime to box the value. |
| 6913 | __ bind(&receiver_is_value); |
| 6914 | __ EnterInternalFrame(); |
| 6915 | __ push(r1); |
| 6916 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
| 6917 | __ LeaveInternalFrame(); |
| 6918 | __ str(r0, MemOperand(sp, argc_ * kPointerSize)); |
| 6919 | |
| 6920 | __ bind(&receiver_is_js_object); |
| 6921 | } |
| 6922 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6923 | // Get the function to call from the stack. |
| 6924 | // function, receiver [, arguments] |
| 6925 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6926 | |
| 6927 | // Check that the function is really a JavaScript function. |
| 6928 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6929 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6930 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6931 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6932 | __ b(ne, &slow); |
| 6933 | |
| 6934 | // Fast-case: Invoke the function now. |
| 6935 | // r1: pushed function |
| 6936 | ParameterCount actual(argc_); |
| 6937 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6938 | |
| 6939 | // Slow-case: Non-function called. |
| 6940 | __ bind(&slow); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6941 | // CALL_NON_FUNCTION expects the non-function callee as receiver (instead |
| 6942 | // of the original receiver from the call site). |
| 6943 | __ str(r1, MemOperand(sp, argc_ * kPointerSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6944 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6945 | __ mov(r2, Operand(0)); |
| 6946 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6947 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6948 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6949 | } |
| 6950 | |
| 6951 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6952 | const char* CompareStub::GetName() { |
| 6953 | switch (cc_) { |
| 6954 | case lt: return "CompareStub_LT"; |
| 6955 | case gt: return "CompareStub_GT"; |
| 6956 | case le: return "CompareStub_LE"; |
| 6957 | case ge: return "CompareStub_GE"; |
| 6958 | case ne: { |
| 6959 | if (strict_) { |
| 6960 | if (never_nan_nan_) { |
| 6961 | return "CompareStub_NE_STRICT_NO_NAN"; |
| 6962 | } else { |
| 6963 | return "CompareStub_NE_STRICT"; |
| 6964 | } |
| 6965 | } else { |
| 6966 | if (never_nan_nan_) { |
| 6967 | return "CompareStub_NE_NO_NAN"; |
| 6968 | } else { |
| 6969 | return "CompareStub_NE"; |
| 6970 | } |
| 6971 | } |
| 6972 | } |
| 6973 | case eq: { |
| 6974 | if (strict_) { |
| 6975 | if (never_nan_nan_) { |
| 6976 | return "CompareStub_EQ_STRICT_NO_NAN"; |
| 6977 | } else { |
| 6978 | return "CompareStub_EQ_STRICT"; |
| 6979 | } |
| 6980 | } else { |
| 6981 | if (never_nan_nan_) { |
| 6982 | return "CompareStub_EQ_NO_NAN"; |
| 6983 | } else { |
| 6984 | return "CompareStub_EQ"; |
| 6985 | } |
| 6986 | } |
| 6987 | } |
| 6988 | default: return "CompareStub"; |
| 6989 | } |
| 6990 | } |
| 6991 | |
| 6992 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6993 | int CompareStub::MinorKey() { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6994 | // Encode the three parameters in a unique 16 bit value. |
| 6995 | ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16)); |
| 6996 | int nnn_value = (never_nan_nan_ ? 2 : 0); |
| 6997 | if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs. |
| 6998 | return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6999 | } |
| 7000 | |
| 7001 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7002 | void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm, |
| 7003 | Register dest, |
| 7004 | Register src, |
| 7005 | Register count, |
| 7006 | Register scratch, |
| 7007 | bool ascii) { |
| 7008 | Label loop; |
| 7009 | Label done; |
| 7010 | // This loop just copies one character at a time, as it is only used for very |
| 7011 | // short strings. |
| 7012 | if (!ascii) { |
| 7013 | __ add(count, count, Operand(count), SetCC); |
| 7014 | } else { |
| 7015 | __ cmp(count, Operand(0)); |
| 7016 | } |
| 7017 | __ b(eq, &done); |
| 7018 | |
| 7019 | __ bind(&loop); |
| 7020 | __ ldrb(scratch, MemOperand(src, 1, PostIndex)); |
| 7021 | // Perform sub between load and dependent store to get the load time to |
| 7022 | // complete. |
| 7023 | __ sub(count, count, Operand(1), SetCC); |
| 7024 | __ strb(scratch, MemOperand(dest, 1, PostIndex)); |
| 7025 | // last iteration. |
| 7026 | __ b(gt, &loop); |
| 7027 | |
| 7028 | __ bind(&done); |
| 7029 | } |
| 7030 | |
| 7031 | |
| 7032 | enum CopyCharactersFlags { |
| 7033 | COPY_ASCII = 1, |
| 7034 | DEST_ALWAYS_ALIGNED = 2 |
| 7035 | }; |
| 7036 | |
| 7037 | |
| 7038 | void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm, |
| 7039 | Register dest, |
| 7040 | Register src, |
| 7041 | Register count, |
| 7042 | Register scratch1, |
| 7043 | Register scratch2, |
| 7044 | Register scratch3, |
| 7045 | Register scratch4, |
| 7046 | Register scratch5, |
| 7047 | int flags) { |
| 7048 | bool ascii = (flags & COPY_ASCII) != 0; |
| 7049 | bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0; |
| 7050 | |
| 7051 | if (dest_always_aligned && FLAG_debug_code) { |
| 7052 | // Check that destination is actually word aligned if the flag says |
| 7053 | // that it is. |
| 7054 | __ tst(dest, Operand(kPointerAlignmentMask)); |
| 7055 | __ Check(eq, "Destination of copy not aligned."); |
| 7056 | } |
| 7057 | |
| 7058 | const int kReadAlignment = 4; |
| 7059 | const int kReadAlignmentMask = kReadAlignment - 1; |
| 7060 | // Ensure that reading an entire aligned word containing the last character |
| 7061 | // of a string will not read outside the allocated area (because we pad up |
| 7062 | // to kObjectAlignment). |
| 7063 | ASSERT(kObjectAlignment >= kReadAlignment); |
| 7064 | // Assumes word reads and writes are little endian. |
| 7065 | // Nothing to do for zero characters. |
| 7066 | Label done; |
| 7067 | if (!ascii) { |
| 7068 | __ add(count, count, Operand(count), SetCC); |
| 7069 | } else { |
| 7070 | __ cmp(count, Operand(0)); |
| 7071 | } |
| 7072 | __ b(eq, &done); |
| 7073 | |
| 7074 | // Assume that you cannot read (or write) unaligned. |
| 7075 | Label byte_loop; |
| 7076 | // Must copy at least eight bytes, otherwise just do it one byte at a time. |
| 7077 | __ cmp(count, Operand(8)); |
| 7078 | __ add(count, dest, Operand(count)); |
| 7079 | Register limit = count; // Read until src equals this. |
| 7080 | __ b(lt, &byte_loop); |
| 7081 | |
| 7082 | if (!dest_always_aligned) { |
| 7083 | // Align dest by byte copying. Copies between zero and three bytes. |
| 7084 | __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC); |
| 7085 | Label dest_aligned; |
| 7086 | __ b(eq, &dest_aligned); |
| 7087 | __ cmp(scratch4, Operand(2)); |
| 7088 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex)); |
| 7089 | __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le); |
| 7090 | __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt); |
| 7091 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7092 | __ strb(scratch2, MemOperand(dest, 1, PostIndex), le); |
| 7093 | __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt); |
| 7094 | __ bind(&dest_aligned); |
| 7095 | } |
| 7096 | |
| 7097 | Label simple_loop; |
| 7098 | |
| 7099 | __ sub(scratch4, dest, Operand(src)); |
| 7100 | __ and_(scratch4, scratch4, Operand(0x03), SetCC); |
| 7101 | __ b(eq, &simple_loop); |
| 7102 | // Shift register is number of bits in a source word that |
| 7103 | // must be combined with bits in the next source word in order |
| 7104 | // to create a destination word. |
| 7105 | |
| 7106 | // Complex loop for src/dst that are not aligned the same way. |
| 7107 | { |
| 7108 | Label loop; |
| 7109 | __ mov(scratch4, Operand(scratch4, LSL, 3)); |
| 7110 | Register left_shift = scratch4; |
| 7111 | __ and_(src, src, Operand(~3)); // Round down to load previous word. |
| 7112 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 7113 | // Store the "shift" most significant bits of scratch in the least |
| 7114 | // signficant bits (i.e., shift down by (32-shift)). |
| 7115 | __ rsb(scratch2, left_shift, Operand(32)); |
| 7116 | Register right_shift = scratch2; |
| 7117 | __ mov(scratch1, Operand(scratch1, LSR, right_shift)); |
| 7118 | |
| 7119 | __ bind(&loop); |
| 7120 | __ ldr(scratch3, MemOperand(src, 4, PostIndex)); |
| 7121 | __ sub(scratch5, limit, Operand(dest)); |
| 7122 | __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift)); |
| 7123 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 7124 | __ mov(scratch1, Operand(scratch3, LSR, right_shift)); |
| 7125 | // Loop if four or more bytes left to copy. |
| 7126 | // Compare to eight, because we did the subtract before increasing dst. |
| 7127 | __ sub(scratch5, scratch5, Operand(8), SetCC); |
| 7128 | __ b(ge, &loop); |
| 7129 | } |
| 7130 | // There is now between zero and three bytes left to copy (negative that |
| 7131 | // number is in scratch5), and between one and three bytes already read into |
| 7132 | // scratch1 (eight times that number in scratch4). We may have read past |
| 7133 | // the end of the string, but because objects are aligned, we have not read |
| 7134 | // past the end of the object. |
| 7135 | // Find the minimum of remaining characters to move and preloaded characters |
| 7136 | // and write those as bytes. |
| 7137 | __ add(scratch5, scratch5, Operand(4), SetCC); |
| 7138 | __ b(eq, &done); |
| 7139 | __ cmp(scratch4, Operand(scratch5, LSL, 3), ne); |
| 7140 | // Move minimum of bytes read and bytes left to copy to scratch4. |
| 7141 | __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt); |
| 7142 | // Between one and three (value in scratch5) characters already read into |
| 7143 | // scratch ready to write. |
| 7144 | __ cmp(scratch5, Operand(2)); |
| 7145 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7146 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge); |
| 7147 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge); |
| 7148 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt); |
| 7149 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt); |
| 7150 | // Copy any remaining bytes. |
| 7151 | __ b(&byte_loop); |
| 7152 | |
| 7153 | // Simple loop. |
| 7154 | // Copy words from src to dst, until less than four bytes left. |
| 7155 | // Both src and dest are word aligned. |
| 7156 | __ bind(&simple_loop); |
| 7157 | { |
| 7158 | Label loop; |
| 7159 | __ bind(&loop); |
| 7160 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 7161 | __ sub(scratch3, limit, Operand(dest)); |
| 7162 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 7163 | // Compare to 8, not 4, because we do the substraction before increasing |
| 7164 | // dest. |
| 7165 | __ cmp(scratch3, Operand(8)); |
| 7166 | __ b(ge, &loop); |
| 7167 | } |
| 7168 | |
| 7169 | // Copy bytes from src to dst until dst hits limit. |
| 7170 | __ bind(&byte_loop); |
| 7171 | __ cmp(dest, Operand(limit)); |
| 7172 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt); |
| 7173 | __ b(ge, &done); |
| 7174 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7175 | __ b(&byte_loop); |
| 7176 | |
| 7177 | __ bind(&done); |
| 7178 | } |
| 7179 | |
| 7180 | |
| 7181 | void SubStringStub::Generate(MacroAssembler* masm) { |
| 7182 | Label runtime; |
| 7183 | |
| 7184 | // Stack frame on entry. |
| 7185 | // lr: return address |
| 7186 | // sp[0]: to |
| 7187 | // sp[4]: from |
| 7188 | // sp[8]: string |
| 7189 | |
| 7190 | // This stub is called from the native-call %_SubString(...), so |
| 7191 | // nothing can be assumed about the arguments. It is tested that: |
| 7192 | // "string" is a sequential string, |
| 7193 | // both "from" and "to" are smis, and |
| 7194 | // 0 <= from <= to <= string.length. |
| 7195 | // If any of these assumptions fail, we call the runtime system. |
| 7196 | |
| 7197 | static const int kToOffset = 0 * kPointerSize; |
| 7198 | static const int kFromOffset = 1 * kPointerSize; |
| 7199 | static const int kStringOffset = 2 * kPointerSize; |
| 7200 | |
| 7201 | |
| 7202 | // Check bounds and smi-ness. |
| 7203 | __ ldr(r7, MemOperand(sp, kToOffset)); |
| 7204 | __ ldr(r6, MemOperand(sp, kFromOffset)); |
| 7205 | ASSERT_EQ(0, kSmiTag); |
| 7206 | ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize); |
| 7207 | // I.e., arithmetic shift right by one un-smi-tags. |
| 7208 | __ mov(r2, Operand(r7, ASR, 1), SetCC); |
| 7209 | __ mov(r3, Operand(r6, ASR, 1), SetCC, cc); |
| 7210 | // If either r2 or r6 had the smi tag bit set, then carry is set now. |
| 7211 | __ b(cs, &runtime); // Either "from" or "to" is not a smi. |
| 7212 | __ b(mi, &runtime); // From is negative. |
| 7213 | |
| 7214 | __ sub(r2, r2, Operand(r3), SetCC); |
| 7215 | __ b(mi, &runtime); // Fail if from > to. |
| 7216 | // Handle sub-strings of length 2 and less in the runtime system. |
| 7217 | __ cmp(r2, Operand(2)); |
| 7218 | __ b(le, &runtime); |
| 7219 | |
| 7220 | // r2: length |
| 7221 | // r6: from (smi) |
| 7222 | // r7: to (smi) |
| 7223 | |
| 7224 | // Make sure first argument is a sequential (or flat) string. |
| 7225 | __ ldr(r5, MemOperand(sp, kStringOffset)); |
| 7226 | ASSERT_EQ(0, kSmiTag); |
| 7227 | __ tst(r5, Operand(kSmiTagMask)); |
| 7228 | __ b(eq, &runtime); |
| 7229 | Condition is_string = masm->IsObjectStringType(r5, r1); |
| 7230 | __ b(NegateCondition(is_string), &runtime); |
| 7231 | |
| 7232 | // r1: instance type |
| 7233 | // r2: length |
| 7234 | // r5: string |
| 7235 | // r6: from (smi) |
| 7236 | // r7: to (smi) |
| 7237 | Label seq_string; |
| 7238 | __ and_(r4, r1, Operand(kStringRepresentationMask)); |
| 7239 | ASSERT(kSeqStringTag < kConsStringTag); |
| 7240 | ASSERT(kExternalStringTag > kConsStringTag); |
| 7241 | __ cmp(r4, Operand(kConsStringTag)); |
| 7242 | __ b(gt, &runtime); // External strings go to runtime. |
| 7243 | __ b(lt, &seq_string); // Sequential strings are handled directly. |
| 7244 | |
| 7245 | // Cons string. Try to recurse (once) on the first substring. |
| 7246 | // (This adds a little more generality than necessary to handle flattened |
| 7247 | // cons strings, but not much). |
| 7248 | __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset)); |
| 7249 | __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset)); |
| 7250 | __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7251 | __ tst(r1, Operand(kStringRepresentationMask)); |
| 7252 | ASSERT_EQ(0, kSeqStringTag); |
| 7253 | __ b(ne, &runtime); // Cons and External strings go to runtime. |
| 7254 | |
| 7255 | // Definitly a sequential string. |
| 7256 | __ bind(&seq_string); |
| 7257 | |
| 7258 | // r1: instance type. |
| 7259 | // r2: length |
| 7260 | // r5: string |
| 7261 | // r6: from (smi) |
| 7262 | // r7: to (smi) |
| 7263 | __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset)); |
| 7264 | __ cmp(r4, Operand(r7, ASR, 1)); |
| 7265 | __ b(lt, &runtime); // Fail if to > length. |
| 7266 | |
| 7267 | // r1: instance type. |
| 7268 | // r2: result string length. |
| 7269 | // r5: string. |
| 7270 | // r6: from offset (smi) |
| 7271 | // Check for flat ascii string. |
| 7272 | Label non_ascii_flat; |
| 7273 | __ tst(r1, Operand(kStringEncodingMask)); |
| 7274 | ASSERT_EQ(0, kTwoByteStringTag); |
| 7275 | __ b(eq, &non_ascii_flat); |
| 7276 | |
| 7277 | // Allocate the result. |
| 7278 | __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime); |
| 7279 | |
| 7280 | // r0: result string. |
| 7281 | // r2: result string length. |
| 7282 | // r5: string. |
| 7283 | // r6: from offset (smi) |
| 7284 | // Locate first character of result. |
| 7285 | __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7286 | // Locate 'from' character of string. |
| 7287 | __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7288 | __ add(r5, r5, Operand(r6, ASR, 1)); |
| 7289 | |
| 7290 | // r0: result string. |
| 7291 | // r1: first character of result string. |
| 7292 | // r2: result string length. |
| 7293 | // r5: first character of sub string to copy. |
| 7294 | ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask); |
| 7295 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 7296 | COPY_ASCII | DEST_ALWAYS_ALIGNED); |
| 7297 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 7298 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7299 | __ Ret(); |
| 7300 | |
| 7301 | __ bind(&non_ascii_flat); |
| 7302 | // r2: result string length. |
| 7303 | // r5: string. |
| 7304 | // r6: from offset (smi) |
| 7305 | // Check for flat two byte string. |
| 7306 | |
| 7307 | // Allocate the result. |
| 7308 | __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime); |
| 7309 | |
| 7310 | // r0: result string. |
| 7311 | // r2: result string length. |
| 7312 | // r5: string. |
| 7313 | // Locate first character of result. |
| 7314 | __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7315 | // Locate 'from' character of string. |
| 7316 | __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7317 | // As "from" is a smi it is 2 times the value which matches the size of a two |
| 7318 | // byte character. |
| 7319 | __ add(r5, r5, Operand(r6)); |
| 7320 | |
| 7321 | // r0: result string. |
| 7322 | // r1: first character of result. |
| 7323 | // r2: result length. |
| 7324 | // r5: first character of string to copy. |
| 7325 | ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask); |
| 7326 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 7327 | DEST_ALWAYS_ALIGNED); |
| 7328 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 7329 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7330 | __ Ret(); |
| 7331 | |
| 7332 | // Just jump to runtime to create the sub string. |
| 7333 | __ bind(&runtime); |
| 7334 | __ TailCallRuntime(ExternalReference(Runtime::kSubString), 3, 1); |
| 7335 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7336 | |
| 7337 | |
| 7338 | void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 7339 | Register left, |
| 7340 | Register right, |
| 7341 | Register scratch1, |
| 7342 | Register scratch2, |
| 7343 | Register scratch3, |
| 7344 | Register scratch4) { |
| 7345 | Label compare_lengths; |
| 7346 | // Find minimum length and length difference. |
| 7347 | __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset)); |
| 7348 | __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset)); |
| 7349 | __ sub(scratch3, scratch1, Operand(scratch2), SetCC); |
| 7350 | Register length_delta = scratch3; |
| 7351 | __ mov(scratch1, scratch2, LeaveCC, gt); |
| 7352 | Register min_length = scratch1; |
| 7353 | __ tst(min_length, Operand(min_length)); |
| 7354 | __ b(eq, &compare_lengths); |
| 7355 | |
| 7356 | // Setup registers so that we only need to increment one register |
| 7357 | // in the loop. |
| 7358 | __ add(scratch2, min_length, |
| 7359 | Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7360 | __ add(left, left, Operand(scratch2)); |
| 7361 | __ add(right, right, Operand(scratch2)); |
| 7362 | // Registers left and right points to the min_length character of strings. |
| 7363 | __ rsb(min_length, min_length, Operand(-1)); |
| 7364 | Register index = min_length; |
| 7365 | // Index starts at -min_length. |
| 7366 | |
| 7367 | { |
| 7368 | // Compare loop. |
| 7369 | Label loop; |
| 7370 | __ bind(&loop); |
| 7371 | // Compare characters. |
| 7372 | __ add(index, index, Operand(1), SetCC); |
| 7373 | __ ldrb(scratch2, MemOperand(left, index), ne); |
| 7374 | __ ldrb(scratch4, MemOperand(right, index), ne); |
| 7375 | // Skip to compare lengths with eq condition true. |
| 7376 | __ b(eq, &compare_lengths); |
| 7377 | __ cmp(scratch2, scratch4); |
| 7378 | __ b(eq, &loop); |
| 7379 | // Fallthrough with eq condition false. |
| 7380 | } |
| 7381 | // Compare lengths - strings up to min-length are equal. |
| 7382 | __ bind(&compare_lengths); |
| 7383 | ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0)); |
| 7384 | // Use zero length_delta as result. |
| 7385 | __ mov(r0, Operand(length_delta), SetCC, eq); |
| 7386 | // Fall through to here if characters compare not-equal. |
| 7387 | __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt); |
| 7388 | __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt); |
| 7389 | __ Ret(); |
| 7390 | } |
| 7391 | |
| 7392 | |
| 7393 | void StringCompareStub::Generate(MacroAssembler* masm) { |
| 7394 | Label runtime; |
| 7395 | |
| 7396 | // Stack frame on entry. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7397 | // sp[0]: right string |
| 7398 | // sp[4]: left string |
| 7399 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left |
| 7400 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7401 | |
| 7402 | Label not_same; |
| 7403 | __ cmp(r0, r1); |
| 7404 | __ b(ne, ¬_same); |
| 7405 | ASSERT_EQ(0, EQUAL); |
| 7406 | ASSERT_EQ(0, kSmiTag); |
| 7407 | __ mov(r0, Operand(Smi::FromInt(EQUAL))); |
| 7408 | __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2); |
| 7409 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7410 | __ Ret(); |
| 7411 | |
| 7412 | __ bind(¬_same); |
| 7413 | |
| 7414 | // Check that both objects are sequential ascii strings. |
| 7415 | __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime); |
| 7416 | |
| 7417 | // Compare flat ascii strings natively. Remove arguments from stack first. |
| 7418 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 7419 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7420 | GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5); |
| 7421 | |
| 7422 | // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater) |
| 7423 | // tagged as a small integer. |
| 7424 | __ bind(&runtime); |
| 7425 | __ TailCallRuntime(ExternalReference(Runtime::kStringCompare), 2, 1); |
| 7426 | } |
| 7427 | |
| 7428 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7429 | void StringAddStub::Generate(MacroAssembler* masm) { |
| 7430 | Label string_add_runtime; |
| 7431 | // Stack on entry: |
| 7432 | // sp[0]: second argument. |
| 7433 | // sp[4]: first argument. |
| 7434 | |
| 7435 | // Load the two arguments. |
| 7436 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument. |
| 7437 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument. |
| 7438 | |
| 7439 | // Make sure that both arguments are strings if not known in advance. |
| 7440 | if (string_check_) { |
| 7441 | ASSERT_EQ(0, kSmiTag); |
| 7442 | __ JumpIfEitherSmi(r0, r1, &string_add_runtime); |
| 7443 | // Load instance types. |
| 7444 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7445 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7446 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7447 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7448 | ASSERT_EQ(0, kStringTag); |
| 7449 | // If either is not a string, go to runtime. |
| 7450 | __ tst(r4, Operand(kIsNotStringMask)); |
| 7451 | __ tst(r5, Operand(kIsNotStringMask), eq); |
| 7452 | __ b(ne, &string_add_runtime); |
| 7453 | } |
| 7454 | |
| 7455 | // Both arguments are strings. |
| 7456 | // r0: first string |
| 7457 | // r1: second string |
| 7458 | // r4: first string instance type (if string_check_) |
| 7459 | // r5: second string instance type (if string_check_) |
| 7460 | { |
| 7461 | Label strings_not_empty; |
| 7462 | // Check if either of the strings are empty. In that case return the other. |
| 7463 | __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset)); |
| 7464 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
| 7465 | __ cmp(r2, Operand(0)); // Test if first string is empty. |
| 7466 | __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second. |
| 7467 | __ cmp(r3, Operand(0), ne); // Else test if second string is empty. |
| 7468 | __ b(ne, &strings_not_empty); // If either string was empty, return r0. |
| 7469 | |
| 7470 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7471 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7472 | __ Ret(); |
| 7473 | |
| 7474 | __ bind(&strings_not_empty); |
| 7475 | } |
| 7476 | |
| 7477 | // Both strings are non-empty. |
| 7478 | // r0: first string |
| 7479 | // r1: second string |
| 7480 | // r2: length of first string |
| 7481 | // r3: length of second string |
| 7482 | // r4: first string instance type (if string_check_) |
| 7483 | // r5: second string instance type (if string_check_) |
| 7484 | // Look at the length of the result of adding the two strings. |
| 7485 | Label string_add_flat_result; |
| 7486 | // Adding two lengths can't overflow. |
| 7487 | ASSERT(String::kMaxLength * 2 > String::kMaxLength); |
| 7488 | __ add(r6, r2, Operand(r3)); |
| 7489 | // Use the runtime system when adding two one character strings, as it |
| 7490 | // contains optimizations for this specific case using the symbol table. |
| 7491 | __ cmp(r6, Operand(2)); |
| 7492 | __ b(eq, &string_add_runtime); |
| 7493 | // Check if resulting string will be flat. |
| 7494 | __ cmp(r6, Operand(String::kMinNonFlatLength)); |
| 7495 | __ b(lt, &string_add_flat_result); |
| 7496 | // Handle exceptionally long strings in the runtime system. |
| 7497 | ASSERT((String::kMaxLength & 0x80000000) == 0); |
| 7498 | ASSERT(IsPowerOf2(String::kMaxLength + 1)); |
| 7499 | // kMaxLength + 1 is representable as shifted literal, kMaxLength is not. |
| 7500 | __ cmp(r6, Operand(String::kMaxLength + 1)); |
| 7501 | __ b(hs, &string_add_runtime); |
| 7502 | |
| 7503 | // If result is not supposed to be flat, allocate a cons string object. |
| 7504 | // If both strings are ascii the result is an ascii cons string. |
| 7505 | if (!string_check_) { |
| 7506 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7507 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7508 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7509 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7510 | } |
| 7511 | Label non_ascii, allocated; |
| 7512 | ASSERT_EQ(0, kTwoByteStringTag); |
| 7513 | __ tst(r4, Operand(kStringEncodingMask)); |
| 7514 | __ tst(r5, Operand(kStringEncodingMask), ne); |
| 7515 | __ b(eq, &non_ascii); |
| 7516 | |
| 7517 | // Allocate an ASCII cons string. |
| 7518 | __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime); |
| 7519 | __ bind(&allocated); |
| 7520 | // Fill the fields of the cons string. |
| 7521 | __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset)); |
| 7522 | __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset)); |
| 7523 | __ mov(r0, Operand(r7)); |
| 7524 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7525 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7526 | __ Ret(); |
| 7527 | |
| 7528 | __ bind(&non_ascii); |
| 7529 | // Allocate a two byte cons string. |
| 7530 | __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime); |
| 7531 | __ jmp(&allocated); |
| 7532 | |
| 7533 | // Handle creating a flat result. First check that both strings are |
| 7534 | // sequential and that they have the same encoding. |
| 7535 | // r0: first string |
| 7536 | // r1: second string |
| 7537 | // r2: length of first string |
| 7538 | // r3: length of second string |
| 7539 | // r4: first string instance type (if string_check_) |
| 7540 | // r5: second string instance type (if string_check_) |
| 7541 | // r6: sum of lengths. |
| 7542 | __ bind(&string_add_flat_result); |
| 7543 | if (!string_check_) { |
| 7544 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7545 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7546 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7547 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7548 | } |
| 7549 | // Check that both strings are sequential. |
| 7550 | ASSERT_EQ(0, kSeqStringTag); |
| 7551 | __ tst(r4, Operand(kStringRepresentationMask)); |
| 7552 | __ tst(r5, Operand(kStringRepresentationMask), eq); |
| 7553 | __ b(ne, &string_add_runtime); |
| 7554 | // Now check if both strings have the same encoding (ASCII/Two-byte). |
| 7555 | // r0: first string. |
| 7556 | // r1: second string. |
| 7557 | // r2: length of first string. |
| 7558 | // r3: length of second string. |
| 7559 | // r6: sum of lengths.. |
| 7560 | Label non_ascii_string_add_flat_result; |
| 7561 | ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test. |
| 7562 | __ eor(r7, r4, Operand(r5)); |
| 7563 | __ tst(r7, Operand(kStringEncodingMask)); |
| 7564 | __ b(ne, &string_add_runtime); |
| 7565 | // And see if it's ASCII or two-byte. |
| 7566 | __ tst(r4, Operand(kStringEncodingMask)); |
| 7567 | __ b(eq, &non_ascii_string_add_flat_result); |
| 7568 | |
| 7569 | // Both strings are sequential ASCII strings. We also know that they are |
| 7570 | // short (since the sum of the lengths is less than kMinNonFlatLength). |
| 7571 | __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 7572 | // Locate first character of result. |
| 7573 | __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7574 | // Locate first character of first argument. |
| 7575 | __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7576 | // r0: first character of first string. |
| 7577 | // r1: second string. |
| 7578 | // r2: length of first string. |
| 7579 | // r3: length of second string. |
| 7580 | // r6: first character of result. |
| 7581 | // r7: result string. |
| 7582 | GenerateCopyCharacters(masm, r6, r0, r2, r4, true); |
| 7583 | |
| 7584 | // Load second argument and locate first character. |
| 7585 | __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7586 | // r1: first character of second string. |
| 7587 | // r3: length of second string. |
| 7588 | // r6: next character of result. |
| 7589 | // r7: result string. |
| 7590 | GenerateCopyCharacters(masm, r6, r1, r3, r4, true); |
| 7591 | __ mov(r0, Operand(r7)); |
| 7592 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7593 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7594 | __ Ret(); |
| 7595 | |
| 7596 | __ bind(&non_ascii_string_add_flat_result); |
| 7597 | // Both strings are sequential two byte strings. |
| 7598 | // r0: first string. |
| 7599 | // r1: second string. |
| 7600 | // r2: length of first string. |
| 7601 | // r3: length of second string. |
| 7602 | // r6: sum of length of strings. |
| 7603 | __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 7604 | // r0: first string. |
| 7605 | // r1: second string. |
| 7606 | // r2: length of first string. |
| 7607 | // r3: length of second string. |
| 7608 | // r7: result string. |
| 7609 | |
| 7610 | // Locate first character of result. |
| 7611 | __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7612 | // Locate first character of first argument. |
| 7613 | __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7614 | |
| 7615 | // r0: first character of first string. |
| 7616 | // r1: second string. |
| 7617 | // r2: length of first string. |
| 7618 | // r3: length of second string. |
| 7619 | // r6: first character of result. |
| 7620 | // r7: result string. |
| 7621 | GenerateCopyCharacters(masm, r6, r0, r2, r4, false); |
| 7622 | |
| 7623 | // Locate first character of second argument. |
| 7624 | __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7625 | |
| 7626 | // r1: first character of second string. |
| 7627 | // r3: length of second string. |
| 7628 | // r6: next character of result (after copy of first string). |
| 7629 | // r7: result string. |
| 7630 | GenerateCopyCharacters(masm, r6, r1, r3, r4, false); |
| 7631 | |
| 7632 | __ mov(r0, Operand(r7)); |
| 7633 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7634 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7635 | __ Ret(); |
| 7636 | |
| 7637 | // Just jump to runtime to add the two strings. |
| 7638 | __ bind(&string_add_runtime); |
| 7639 | __ TailCallRuntime(ExternalReference(Runtime::kStringAdd), 2, 1); |
| 7640 | } |
| 7641 | |
| 7642 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7643 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7644 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7645 | } } // namespace v8::internal |