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 | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 34 | #include "ic-inl.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 35 | #include "parser.h" |
| 36 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 38 | #include "scopes.h" |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 39 | #include "virtual-frame-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 41 | namespace v8 { |
| 42 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 44 | #define __ ACCESS_MASM(masm_) |
| 45 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 46 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 47 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 48 | Condition cc, |
| 49 | bool never_nan_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 50 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 51 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 52 | Label* slow, |
| 53 | bool strict); |
| 54 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
| 55 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 56 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 57 | Register source, |
| 58 | Register destination, |
| 59 | int known_int); |
| 60 | static bool IsEasyToMultiplyBy(int x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 64 | // ------------------------------------------------------------------------- |
| 65 | // Platform-specific DeferredCode functions. |
| 66 | |
| 67 | void DeferredCode::SaveRegisters() { |
| 68 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 69 | int action = registers_[i]; |
| 70 | if (action == kPush) { |
| 71 | __ push(RegisterAllocator::ToRegister(i)); |
| 72 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 73 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void DeferredCode::RestoreRegisters() { |
| 80 | // Restore registers in reverse order due to the stack. |
| 81 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 82 | int action = registers_[i]; |
| 83 | if (action == kPush) { |
| 84 | __ pop(RegisterAllocator::ToRegister(i)); |
| 85 | } else if (action != kIgnore) { |
| 86 | action &= ~kSyncedFlag; |
| 87 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 92 | |
| 93 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 94 | // CodeGenState implementation. |
| 95 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 96 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 97 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 98 | true_target_(NULL), |
| 99 | false_target_(NULL), |
| 100 | previous_(NULL) { |
| 101 | owner_->set_state(this); |
| 102 | } |
| 103 | |
| 104 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 105 | CodeGenState::CodeGenState(CodeGenerator* owner, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 106 | JumpTarget* true_target, |
| 107 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 108 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 109 | true_target_(true_target), |
| 110 | false_target_(false_target), |
| 111 | previous_(owner->state()) { |
| 112 | owner_->set_state(this); |
| 113 | } |
| 114 | |
| 115 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 116 | CodeGenState::~CodeGenState() { |
| 117 | ASSERT(owner_->state() == this); |
| 118 | owner_->set_state(previous_); |
| 119 | } |
| 120 | |
| 121 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 122 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 123 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 125 | CodeGenerator::CodeGenerator(MacroAssembler* masm) |
| 126 | : deferred_(8), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 127 | masm_(masm), |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 128 | info_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 129 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 130 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | cc_reg_(al), |
| 132 | state_(NULL), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 133 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
| 137 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 138 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 139 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 140 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | // cp: callee's context |
| 142 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 143 | void CodeGenerator::Generate(CompilationInfo* info) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 144 | // Record the position for debugging purposes. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 145 | CodeForFunctionPosition(info->function()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 146 | Comment cmnt(masm_, "[ function compiled by virtual frame code generator"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 147 | |
| 148 | // Initialize state. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 149 | info_ = info; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 150 | ASSERT(allocator_ == NULL); |
| 151 | RegisterAllocator register_allocator(this); |
| 152 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 153 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 154 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 155 | cc_reg_ = al; |
| 156 | { |
| 157 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 159 | // Entry: |
| 160 | // Stack: receiver, arguments |
| 161 | // lr: return address |
| 162 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 163 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 164 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 166 | allocator_->Initialize(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 167 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | #ifdef DEBUG |
| 169 | if (strlen(FLAG_stop_at) > 0 && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 170 | info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 171 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 172 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | } |
| 174 | #endif |
| 175 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 176 | if (info->mode() == CompilationInfo::PRIMARY) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 177 | frame_->Enter(); |
| 178 | // tos: code slot |
| 179 | |
| 180 | // Allocate space for locals and initialize them. This also checks |
| 181 | // for stack overflow. |
| 182 | frame_->AllocateStackSlots(); |
| 183 | |
| 184 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 185 | int heap_slots = scope()->num_heap_slots(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 186 | if (heap_slots > 0) { |
| 187 | // Allocate local context. |
| 188 | // Get outer context and create a new context based on it. |
| 189 | __ ldr(r0, frame_->Function()); |
| 190 | frame_->EmitPush(r0); |
| 191 | if (heap_slots <= FastNewContextStub::kMaximumSlots) { |
| 192 | FastNewContextStub stub(heap_slots); |
| 193 | frame_->CallStub(&stub, 1); |
| 194 | } else { |
| 195 | frame_->CallRuntime(Runtime::kNewContext, 1); |
| 196 | } |
| 197 | |
| 198 | #ifdef DEBUG |
| 199 | JumpTarget verified_true; |
| 200 | __ cmp(r0, Operand(cp)); |
| 201 | verified_true.Branch(eq); |
| 202 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 203 | verified_true.Bind(); |
| 204 | #endif |
| 205 | // Update context local. |
| 206 | __ str(cp, frame_->Context()); |
| 207 | } |
| 208 | |
| 209 | // TODO(1241774): Improve this code: |
| 210 | // 1) only needed if we have a context |
| 211 | // 2) no need to recompute context ptr every single time |
| 212 | // 3) don't copy parameter operand code from SlotOperand! |
| 213 | { |
| 214 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 215 | // Note that iteration order is relevant here! If we have the same |
| 216 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 217 | // needs to be copied into the context, it must be the last argument |
| 218 | // passed to the parameter that needs to be copied. This is a rare |
| 219 | // case so we don't check for it, instead we rely on the copying |
| 220 | // order: such a parameter is copied repeatedly into the same |
| 221 | // context location and thus the last value is what is seen inside |
| 222 | // the function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 223 | for (int i = 0; i < scope()->num_parameters(); i++) { |
| 224 | Variable* par = scope()->parameter(i); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 225 | Slot* slot = par->slot(); |
| 226 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 227 | ASSERT(!scope()->is_global_scope()); // No params in global scope. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 228 | __ ldr(r1, frame_->ParameterAt(i)); |
| 229 | // Loads r2 with context; used below in RecordWrite. |
| 230 | __ str(r1, SlotOperand(slot, r2)); |
| 231 | // Load the offset into r3. |
| 232 | int slot_offset = |
| 233 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 234 | __ mov(r3, Operand(slot_offset)); |
| 235 | __ RecordWrite(r2, r3, r1); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Store the arguments object. This must happen after context |
| 241 | // initialization because the arguments object may be stored in the |
| 242 | // context. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 243 | if (scope()->arguments() != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 244 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 245 | ASSERT(scope()->arguments_shadow() != NULL); |
| 246 | Variable* arguments = scope()->arguments()->var(); |
| 247 | Variable* shadow = scope()->arguments_shadow()->var(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 248 | ASSERT(arguments != NULL && arguments->slot() != NULL); |
| 249 | ASSERT(shadow != NULL && shadow->slot() != NULL); |
| 250 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
| 251 | __ ldr(r2, frame_->Function()); |
| 252 | // The receiver is below the arguments, the return address, and the |
| 253 | // frame pointer on the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 254 | const int kReceiverDisplacement = 2 + scope()->num_parameters(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 255 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 256 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 257 | frame_->Adjust(3); |
| 258 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
| 259 | frame_->CallStub(&stub, 3); |
| 260 | frame_->EmitPush(r0); |
| 261 | StoreToSlot(arguments->slot(), NOT_CONST_INIT); |
| 262 | StoreToSlot(shadow->slot(), NOT_CONST_INIT); |
| 263 | frame_->Drop(); // Value is no longer needed. |
| 264 | } |
| 265 | |
| 266 | // Initialize ThisFunction reference if present. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 267 | if (scope()->is_function_scope() && scope()->function() != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 268 | __ mov(ip, Operand(Factory::the_hole_value())); |
| 269 | frame_->EmitPush(ip); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 270 | StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 271 | } |
| 272 | } else { |
| 273 | // When used as the secondary compiler for splitting, r1, cp, |
| 274 | // fp, and lr have been pushed on the stack. Adjust the virtual |
| 275 | // frame to match this state. |
| 276 | frame_->Adjust(4); |
| 277 | allocator_->Unuse(r1); |
| 278 | allocator_->Unuse(lr); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 279 | |
| 280 | // Bind all the bailout labels to the beginning of the function. |
| 281 | List<CompilationInfo::Bailout*>* bailouts = info->bailouts(); |
| 282 | for (int i = 0; i < bailouts->length(); i++) { |
| 283 | __ bind(bailouts->at(i)->label()); |
| 284 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 285 | } |
| 286 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 287 | // Initialize the function return target after the locals are set |
| 288 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 289 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 290 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 291 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 292 | // Generate code to 'execute' declarations and initialize functions |
| 293 | // (source elements). In case of an illegal redeclaration we need to |
| 294 | // handle that instead of processing the declarations. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 295 | if (scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 297 | scope()->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | } else { |
| 299 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 300 | ProcessDeclarations(scope()->declarations()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 301 | // Bail out if a stack-overflow exception occurred when processing |
| 302 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 303 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | } |
| 305 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 306 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 307 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 308 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 309 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 310 | |
| 311 | // Compile the body of the function in a vanilla state. Don't |
| 312 | // bother compiling all the code if the scope has an illegal |
| 313 | // redeclaration. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 314 | if (!scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 315 | Comment cmnt(masm_, "[ function body"); |
| 316 | #ifdef DEBUG |
| 317 | bool is_builtin = Bootstrapper::IsActive(); |
| 318 | bool should_trace = |
| 319 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 320 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 321 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 322 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 323 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | #endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 325 | VisitStatementsAndSpill(info->function()->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | } |
| 328 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 329 | // Generate the return sequence if necessary. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 330 | if (has_valid_frame() || function_return_.is_linked()) { |
| 331 | if (!function_return_.is_linked()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 332 | CodeForReturnPosition(info->function()); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 333 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 334 | // exit |
| 335 | // r0: result |
| 336 | // sp: stack pointer |
| 337 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 338 | // cp: callee's context |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 339 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 340 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 341 | function_return_.Bind(); |
| 342 | if (FLAG_trace) { |
| 343 | // Push the return value on the stack as the parameter. |
| 344 | // Runtime::TraceExit returns the parameter as it is. |
| 345 | frame_->EmitPush(r0); |
| 346 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 347 | } |
| 348 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 349 | // Add a label for checking the size of the code used for returning. |
| 350 | Label check_exit_codesize; |
| 351 | masm_->bind(&check_exit_codesize); |
| 352 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 353 | // Calculate the exact length of the return sequence and make sure that |
| 354 | // the constant pool is not emitted inside of the return sequence. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 355 | int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 356 | int return_sequence_length = Assembler::kJSReturnSequenceLength; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 357 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 358 | // Additional mov instruction generated. |
| 359 | return_sequence_length++; |
| 360 | } |
| 361 | masm_->BlockConstPoolFor(return_sequence_length); |
| 362 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 363 | // Tear down the frame which will restore the caller's frame pointer and |
| 364 | // the link register. |
| 365 | frame_->Exit(); |
| 366 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 367 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 368 | // tool from instrumenting as we rely on the code size here. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 369 | masm_->add(sp, sp, Operand(sp_delta)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 370 | masm_->Jump(lr); |
| 371 | |
| 372 | // 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] | 373 | // expected by the debugger. The add instruction above is an addressing |
| 374 | // mode 1 instruction where there are restrictions on which immediate values |
| 375 | // can be encoded in the instruction and which immediate values requires |
| 376 | // use of an additional instruction for moving the immediate to a temporary |
| 377 | // register. |
| 378 | ASSERT_EQ(return_sequence_length, |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 379 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 380 | } |
| 381 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 382 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 383 | ASSERT(!has_cc()); |
| 384 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 385 | ASSERT(!function_return_is_shadowed_); |
| 386 | function_return_.Unuse(); |
| 387 | DeleteFrame(); |
| 388 | |
| 389 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 390 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 391 | ProcessDeferred(); |
| 392 | } |
| 393 | |
| 394 | allocator_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 398 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 399 | // Currently, this assertion will fail if we try to assign to |
| 400 | // a constant variable that is constant because it is read-only |
| 401 | // (such as the variable referring to a named function expression). |
| 402 | // We need to implement assignments to read-only variables. |
| 403 | // Ideally, we should do this during AST generation (by converting |
| 404 | // such assignments into expression statements); however, in general |
| 405 | // we may not be able to make the decision until past AST generation, |
| 406 | // that is when the entire program is known. |
| 407 | ASSERT(slot != NULL); |
| 408 | int index = slot->index(); |
| 409 | switch (slot->type()) { |
| 410 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 411 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 412 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 413 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 414 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 415 | |
| 416 | case Slot::CONTEXT: { |
| 417 | // Follow the context chain if necessary. |
| 418 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 419 | Register context = cp; |
| 420 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 421 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 422 | // Load the closure. |
| 423 | // (All contexts, even 'with' contexts, have a closure, |
| 424 | // and it is the same for all contexts inside a function. |
| 425 | // There is no need to go to the function context first.) |
| 426 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 427 | // Load the function context (which is the incoming, outer context). |
| 428 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 429 | context = tmp; |
| 430 | } |
| 431 | // We may have a 'with' context now. Get the function context. |
| 432 | // (In fact this mov may never be the needed, since the scope analysis |
| 433 | // may not permit a direct context access in this case and thus we are |
| 434 | // always at a function context. However it is safe to dereference be- |
| 435 | // cause the function context of a function context is itself. Before |
| 436 | // deleting this mov we should try to create a counter-example first, |
| 437 | // though...) |
| 438 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 439 | return ContextOperand(tmp, index); |
| 440 | } |
| 441 | |
| 442 | default: |
| 443 | UNREACHABLE(); |
| 444 | return MemOperand(r0, 0); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 449 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 450 | Slot* slot, |
| 451 | Register tmp, |
| 452 | Register tmp2, |
| 453 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 454 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 455 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 456 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 457 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 458 | if (s->num_heap_slots() > 0) { |
| 459 | if (s->calls_eval()) { |
| 460 | // Check that extension is NULL. |
| 461 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 462 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 463 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 464 | } |
| 465 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 466 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 467 | context = tmp; |
| 468 | } |
| 469 | } |
| 470 | // Check that last extension is NULL. |
| 471 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 472 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 473 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 474 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 475 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 479 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 480 | // (partially) translated into branches, or it may have set the condition |
| 481 | // code register. If force_cc is set, the value is forced to set the |
| 482 | // condition code register and no value is pushed. If the condition code |
| 483 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 484 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 485 | void CodeGenerator::LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 486 | JumpTarget* true_target, |
| 487 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 488 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 489 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 490 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 491 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 492 | { CodeGenState new_state(this, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 493 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 494 | |
| 495 | // If we hit a stack overflow, we may not have actually visited |
| 496 | // the expression. In that case, we ensure that we have a |
| 497 | // valid-looking frame state because we will continue to generate |
| 498 | // code as we unwind the C++ stack. |
| 499 | // |
| 500 | // It's possible to have both a stack overflow and a valid frame |
| 501 | // state (eg, a subexpression overflowed, visiting it returned |
| 502 | // with a dummied frame state, and visiting this expression |
| 503 | // returned with a normal-looking state). |
| 504 | if (HasStackOverflow() && |
| 505 | has_valid_frame() && |
| 506 | !has_cc() && |
| 507 | frame_->height() == original_height) { |
| 508 | true_target->Jump(); |
| 509 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 510 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 511 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 512 | // Convert the TOS value to a boolean in the condition code register. |
| 513 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 515 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 516 | ASSERT(!has_valid_frame() || |
| 517 | (has_cc() && frame_->height() == original_height) || |
| 518 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 522 | void CodeGenerator::Load(Expression* expr) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 523 | #ifdef DEBUG |
| 524 | int original_height = frame_->height(); |
| 525 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 526 | JumpTarget true_target; |
| 527 | JumpTarget false_target; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 528 | LoadCondition(expr, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 529 | |
| 530 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 531 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 532 | JumpTarget loaded; |
| 533 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 534 | materialize_true.Branch(cc_reg_); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 535 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 536 | frame_->EmitPush(r0); |
| 537 | loaded.Jump(); |
| 538 | materialize_true.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 539 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 540 | frame_->EmitPush(r0); |
| 541 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 542 | cc_reg_ = al; |
| 543 | } |
| 544 | |
| 545 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 546 | // We have at least one condition value that has been "translated" |
| 547 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 548 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 549 | if (frame_ != NULL) { |
| 550 | loaded.Jump(); // Don't lose the current TOS. |
| 551 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 552 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 553 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 554 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 555 | true_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 556 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 557 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 558 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 559 | // If both "true" and "false" need to be loaded jump across the code for |
| 560 | // "false". |
| 561 | if (both) { |
| 562 | loaded.Jump(); |
| 563 | } |
| 564 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 565 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 566 | false_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 567 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 568 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 569 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 570 | // A value is loaded on all paths reaching this point. |
| 571 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 572 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 573 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 574 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 575 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 579 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 580 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 581 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 582 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 586 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 587 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 588 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 589 | __ ldr(scratch, |
| 590 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 591 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 595 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 596 | // Special handling of identifiers as subexpressions of typeof. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 597 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 598 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 599 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 600 | // For a global variable we build the property reference |
| 601 | // <global>.<variable> and perform a (regular non-contextual) property |
| 602 | // load to make sure we do not get reference errors. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 604 | Literal key(variable->name()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 605 | Property property(&global, &key, RelocInfo::kNoPosition); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 606 | Reference ref(this, &property); |
| 607 | ref.GetValueAndSpill(); |
| 608 | } else if (variable != NULL && variable->slot() != NULL) { |
| 609 | // For a variable that rewrites to a slot, we signal it is the immediate |
| 610 | // subexpression of a typeof. |
| 611 | LoadFromSlot(variable->slot(), INSIDE_TYPEOF); |
| 612 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 613 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 614 | // Anything else can be handled normally. |
| 615 | LoadAndSpill(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 620 | Reference::Reference(CodeGenerator* cgen, |
| 621 | Expression* expression, |
| 622 | bool persist_after_get) |
| 623 | : cgen_(cgen), |
| 624 | expression_(expression), |
| 625 | type_(ILLEGAL), |
| 626 | persist_after_get_(persist_after_get) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | cgen->LoadReference(this); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | Reference::~Reference() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 632 | ASSERT(is_unloaded() || is_illegal()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 636 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 637 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 638 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 639 | Expression* e = ref->expression(); |
| 640 | Property* property = e->AsProperty(); |
| 641 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 642 | |
| 643 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 644 | // The expression is either a property or a variable proxy that rewrites |
| 645 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 646 | LoadAndSpill(property->obj()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 647 | if (property->key()->IsPropertyName()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 648 | ref->set_type(Reference::NAMED); |
| 649 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 650 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 651 | ref->set_type(Reference::KEYED); |
| 652 | } |
| 653 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 654 | // The expression is a variable proxy that does not rewrite to a |
| 655 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 656 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 657 | LoadGlobal(); |
| 658 | ref->set_type(Reference::NAMED); |
| 659 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 660 | ASSERT(var->slot() != NULL); |
| 661 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 662 | } |
| 663 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 664 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 665 | LoadAndSpill(e); |
| 666 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
| 670 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 671 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 672 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 673 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 674 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 675 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 676 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 677 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 678 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 679 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 680 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 681 | ref->set_unloaded(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 685 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 686 | // register to a boolean in the condition code register. The code |
| 687 | // 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] | 688 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 689 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 690 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 691 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 692 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 693 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 694 | |
| 695 | // Fast case checks |
| 696 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 697 | // Check if the value is 'false'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 698 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 699 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 700 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 701 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 702 | // Check if the value is 'true'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 703 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 704 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 705 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 706 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 707 | // Check if the value is 'undefined'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 708 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 709 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 710 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 711 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 712 | // Check if the value is a smi. |
| 713 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 714 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 715 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 716 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 717 | |
| 718 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 719 | frame_->EmitPush(r0); |
| 720 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 721 | // Convert the result (r0) to a condition code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 722 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 723 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 724 | |
| 725 | cc_reg_ = ne; |
| 726 | } |
| 727 | |
| 728 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 729 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 730 | OverwriteMode overwrite_mode, |
| 731 | int constant_rhs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 732 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 733 | // sp[0] : y |
| 734 | // sp[1] : x |
| 735 | // result : r0 |
| 736 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 737 | // Stub is entered with a call: 'return address' is in lr. |
| 738 | switch (op) { |
| 739 | case Token::ADD: // fall through. |
| 740 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 741 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 742 | case Token::DIV: |
| 743 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 744 | case Token::BIT_OR: |
| 745 | case Token::BIT_AND: |
| 746 | case Token::BIT_XOR: |
| 747 | case Token::SHL: |
| 748 | case Token::SHR: |
| 749 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 750 | frame_->EmitPop(r0); // r0 : y |
| 751 | frame_->EmitPop(r1); // r1 : x |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 752 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 753 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 754 | break; |
| 755 | } |
| 756 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 757 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 758 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 759 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 760 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 761 | break; |
| 762 | |
| 763 | default: |
| 764 | // Other cases should have been handled before this point. |
| 765 | UNREACHABLE(); |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 771 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 772 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 773 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 774 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 775 | bool reversed, |
| 776 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 777 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 778 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 779 | reversed_(reversed), |
| 780 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 781 | set_comment("[ DeferredInlinedSmiOperation"); |
| 782 | } |
| 783 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 784 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 785 | |
| 786 | private: |
| 787 | Token::Value op_; |
| 788 | int value_; |
| 789 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 790 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 791 | }; |
| 792 | |
| 793 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 794 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 795 | switch (op_) { |
| 796 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 797 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 798 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 799 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 800 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 801 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 802 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 803 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 804 | } |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 809 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 810 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 811 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 812 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 813 | } else { |
| 814 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 815 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 816 | } |
| 817 | break; |
| 818 | } |
| 819 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 820 | // For these operations there is no optimistic operation that needs to be |
| 821 | // reverted. |
| 822 | case Token::MUL: |
| 823 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 824 | case Token::BIT_OR: |
| 825 | case Token::BIT_XOR: |
| 826 | case Token::BIT_AND: { |
| 827 | if (reversed_) { |
| 828 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 829 | } else { |
| 830 | __ mov(r1, Operand(r0)); |
| 831 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 832 | } |
| 833 | break; |
| 834 | } |
| 835 | |
| 836 | case Token::SHL: |
| 837 | case Token::SHR: |
| 838 | case Token::SAR: { |
| 839 | if (!reversed_) { |
| 840 | __ mov(r1, Operand(r0)); |
| 841 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 842 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 843 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 844 | } |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 849 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 850 | UNREACHABLE(); |
| 851 | break; |
| 852 | } |
| 853 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 854 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 855 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 859 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 860 | x &= x - 1; |
| 861 | return (x & (x - 1)) == 0; |
| 862 | } |
| 863 | |
| 864 | |
| 865 | // Returns the index of the lowest bit set. |
| 866 | static int BitPosition(unsigned x) { |
| 867 | int bit_posn = 0; |
| 868 | while ((x & 0xf) == 0) { |
| 869 | bit_posn += 4; |
| 870 | x >>= 4; |
| 871 | } |
| 872 | while ((x & 1) == 0) { |
| 873 | bit_posn++; |
| 874 | x >>= 1; |
| 875 | } |
| 876 | return bit_posn; |
| 877 | } |
| 878 | |
| 879 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 880 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 881 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 882 | bool reversed, |
| 883 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 884 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 885 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 886 | // some possible smi operations (like + and -) when (at least) one |
| 887 | // of the operands is a literal smi. With this optimization, the |
| 888 | // performance of the system is increased by ~15%, and the generated |
| 889 | // code size is increased by ~1% (measured on a combination of |
| 890 | // different benchmarks). |
| 891 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 892 | // sp[0] : operand |
| 893 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 894 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 895 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 896 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 897 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 898 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 899 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 900 | switch (op) { |
| 901 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 902 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 903 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 904 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 905 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 906 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 907 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 908 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 909 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 910 | break; |
| 911 | } |
| 912 | |
| 913 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 914 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 915 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 916 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 917 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 918 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 919 | } else { |
| 920 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 921 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 922 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 923 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 924 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 925 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 926 | break; |
| 927 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 928 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 929 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 930 | case Token::BIT_OR: |
| 931 | case Token::BIT_XOR: |
| 932 | case Token::BIT_AND: { |
| 933 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 934 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 935 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 936 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 937 | switch (op) { |
| 938 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 939 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 940 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 941 | default: UNREACHABLE(); |
| 942 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 943 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 944 | break; |
| 945 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 946 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 947 | case Token::SHL: |
| 948 | case Token::SHR: |
| 949 | case Token::SAR: { |
| 950 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 951 | something_to_inline = false; |
| 952 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 953 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 954 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 955 | DeferredCode* deferred = |
| 956 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
| 957 | __ tst(r0, Operand(kSmiTagMask)); |
| 958 | deferred->Branch(ne); |
| 959 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 960 | switch (op) { |
| 961 | case Token::SHL: { |
| 962 | if (shift_value != 0) { |
| 963 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 964 | } |
| 965 | // check that the *unsigned* result fits in a smi |
| 966 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 967 | deferred->Branch(mi); |
| 968 | break; |
| 969 | } |
| 970 | case Token::SHR: { |
| 971 | // LSR by immediate 0 means shifting 32 bits. |
| 972 | if (shift_value != 0) { |
| 973 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 974 | } |
| 975 | // check that the *unsigned* result fits in a smi |
| 976 | // neither of the two high-order bits can be set: |
| 977 | // - 0x80000000: high bit would be lost when smi tagging |
| 978 | // - 0x40000000: this number would convert to negative when |
| 979 | // smi tagging these two cases can only happen with shifts |
| 980 | // by 0 or 1 when handed a valid smi |
| 981 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 982 | deferred->Branch(ne); |
| 983 | break; |
| 984 | } |
| 985 | case Token::SAR: { |
| 986 | if (shift_value != 0) { |
| 987 | // ASR by immediate 0 means shifting 32 bits. |
| 988 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 989 | } |
| 990 | break; |
| 991 | } |
| 992 | default: UNREACHABLE(); |
| 993 | } |
| 994 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 995 | deferred->BindExit(); |
| 996 | break; |
| 997 | } |
| 998 | |
| 999 | case Token::MOD: { |
| 1000 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 1001 | something_to_inline = false; |
| 1002 | break; |
| 1003 | } |
| 1004 | DeferredCode* deferred = |
| 1005 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1006 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1007 | __ tst(r0, Operand(mask)); |
| 1008 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1009 | mask = (int_value << kSmiTagSize) - 1; |
| 1010 | __ and_(r0, r0, Operand(mask)); |
| 1011 | deferred->BindExit(); |
| 1012 | break; |
| 1013 | } |
| 1014 | |
| 1015 | case Token::MUL: { |
| 1016 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1017 | something_to_inline = false; |
| 1018 | break; |
| 1019 | } |
| 1020 | DeferredCode* deferred = |
| 1021 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1022 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1023 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1024 | unsigned mask = 0x80000000u; |
| 1025 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1026 | mask |= mask >> 1; |
| 1027 | } |
| 1028 | mask |= kSmiTagMask; |
| 1029 | // This does a single mask that checks for a too high value in a |
| 1030 | // conservative way and for a non-Smi. It also filters out negative |
| 1031 | // numbers, unfortunately, but since this code is inline we prefer |
| 1032 | // brevity to comprehensiveness. |
| 1033 | __ tst(r0, Operand(mask)); |
| 1034 | deferred->Branch(ne); |
| 1035 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 1036 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1037 | break; |
| 1038 | } |
| 1039 | |
| 1040 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1041 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1042 | break; |
| 1043 | } |
| 1044 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1045 | if (!something_to_inline) { |
| 1046 | if (!reversed) { |
| 1047 | frame_->EmitPush(r0); |
| 1048 | __ mov(r0, Operand(value)); |
| 1049 | frame_->EmitPush(r0); |
| 1050 | GenericBinaryOperation(op, mode, int_value); |
| 1051 | } else { |
| 1052 | __ mov(ip, Operand(value)); |
| 1053 | frame_->EmitPush(ip); |
| 1054 | frame_->EmitPush(r0); |
| 1055 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1056 | } |
| 1057 | } |
| 1058 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1059 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1063 | void CodeGenerator::Comparison(Condition cc, |
| 1064 | Expression* left, |
| 1065 | Expression* right, |
| 1066 | bool strict) { |
| 1067 | if (left != NULL) LoadAndSpill(left); |
| 1068 | if (right != NULL) LoadAndSpill(right); |
| 1069 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1070 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1071 | // sp[0] : y |
| 1072 | // sp[1] : x |
| 1073 | // result : cc register |
| 1074 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1075 | // Strict only makes sense for equality comparisons. |
| 1076 | ASSERT(!strict || cc == eq); |
| 1077 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1078 | JumpTarget exit; |
| 1079 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1080 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1081 | if (cc == gt || cc == le) { |
| 1082 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1083 | frame_->EmitPop(r1); |
| 1084 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1085 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1086 | frame_->EmitPop(r0); |
| 1087 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1088 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1089 | __ orr(r2, r0, Operand(r1)); |
| 1090 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1091 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1092 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1093 | // Perform non-smi comparison by stub. |
| 1094 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1095 | // We call with 0 args because there are 0 on the stack. |
| 1096 | CompareStub stub(cc, strict); |
| 1097 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1098 | __ cmp(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1099 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1100 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1101 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1102 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1103 | __ cmp(r1, Operand(r0)); |
| 1104 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1105 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1106 | cc_reg_ = cc; |
| 1107 | } |
| 1108 | |
| 1109 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1110 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1111 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1112 | CallFunctionFlags flags, |
| 1113 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1114 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1115 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1116 | int arg_count = args->length(); |
| 1117 | for (int i = 0; i < arg_count; i++) { |
| 1118 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1119 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1120 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1121 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1122 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1123 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1124 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1125 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1126 | CallFunctionStub call_function(arg_count, in_loop, flags); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1127 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1128 | |
| 1129 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1130 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1131 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1135 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1136 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1137 | ASSERT(has_cc()); |
| 1138 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1139 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1140 | cc_reg_ = al; |
| 1141 | } |
| 1142 | |
| 1143 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1144 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1145 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1146 | Comment cmnt(masm_, "[ check stack"); |
| 1147 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1148 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1149 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1150 | // gives a return address 12 bytes down. |
| 1151 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1152 | masm_->cmp(sp, Operand(ip)); |
| 1153 | StackCheckStub stub; |
| 1154 | // Call the stub if lower. |
| 1155 | masm_->mov(pc, |
| 1156 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1157 | RelocInfo::CODE_TARGET), |
| 1158 | LeaveCC, |
| 1159 | lo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1163 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1164 | #ifdef DEBUG |
| 1165 | int original_height = frame_->height(); |
| 1166 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1167 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1168 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1169 | VisitAndSpill(statements->at(i)); |
| 1170 | } |
| 1171 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1172 | } |
| 1173 | |
| 1174 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1175 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1176 | #ifdef DEBUG |
| 1177 | int original_height = frame_->height(); |
| 1178 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1179 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1180 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1181 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1182 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1183 | VisitStatementsAndSpill(node->statements()); |
| 1184 | if (node->break_target()->is_linked()) { |
| 1185 | node->break_target()->Bind(); |
| 1186 | } |
| 1187 | node->break_target()->Unuse(); |
| 1188 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1192 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1193 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1194 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1195 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1196 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1197 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1198 | frame_->EmitPush(r0); |
| 1199 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1200 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1204 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1205 | #ifdef DEBUG |
| 1206 | int original_height = frame_->height(); |
| 1207 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1208 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1209 | Comment cmnt(masm_, "[ Declaration"); |
| 1210 | Variable* var = node->proxy()->var(); |
| 1211 | ASSERT(var != NULL); // must have been resolved |
| 1212 | Slot* slot = var->slot(); |
| 1213 | |
| 1214 | // If it was not possible to allocate the variable at compile time, |
| 1215 | // we need to "declare" it at runtime to make sure it actually |
| 1216 | // exists in the local context. |
| 1217 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1218 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1219 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1220 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1222 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1223 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1224 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1225 | // Declaration nodes are always declared in only two modes. |
| 1226 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1227 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1228 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1229 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1230 | // Push initial value, if any. |
| 1231 | // Note: For variables we must not push an initial value (such as |
| 1232 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1233 | // must not destroy the current value. |
| 1234 | if (node->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1235 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1236 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1237 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1238 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1239 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1240 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1241 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1242 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1243 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1244 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1245 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1246 | return; |
| 1247 | } |
| 1248 | |
| 1249 | ASSERT(!var->is_global()); |
| 1250 | |
| 1251 | // If we have a function or a constant, we need to initialize the variable. |
| 1252 | Expression* val = NULL; |
| 1253 | if (node->mode() == Variable::CONST) { |
| 1254 | val = new Literal(Factory::the_hole_value()); |
| 1255 | } else { |
| 1256 | val = node->fun(); // NULL if we don't have a function |
| 1257 | } |
| 1258 | |
| 1259 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1260 | { |
| 1261 | // Set initial value. |
| 1262 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1263 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1264 | target.SetValue(NOT_CONST_INIT); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1265 | } |
| 1266 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1267 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1268 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1269 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1273 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1274 | #ifdef DEBUG |
| 1275 | int original_height = frame_->height(); |
| 1276 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1277 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1278 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1279 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1280 | Expression* expression = node->expression(); |
| 1281 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1282 | LoadAndSpill(expression); |
| 1283 | frame_->Drop(); |
| 1284 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1288 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1289 | #ifdef DEBUG |
| 1290 | int original_height = frame_->height(); |
| 1291 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1292 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1294 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1296 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1300 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1301 | #ifdef DEBUG |
| 1302 | int original_height = frame_->height(); |
| 1303 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1304 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1305 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1306 | // Generate different code depending on which parts of the if statement |
| 1307 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | bool has_then_stm = node->HasThenStatement(); |
| 1309 | bool has_else_stm = node->HasElseStatement(); |
| 1310 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1311 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1312 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1313 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1314 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1315 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1316 | JumpTarget then; |
| 1317 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1318 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1319 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1320 | if (frame_ != NULL) { |
| 1321 | Branch(false, &else_); |
| 1322 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1323 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1324 | if (frame_ != NULL || then.is_linked()) { |
| 1325 | then.Bind(); |
| 1326 | VisitAndSpill(node->then_statement()); |
| 1327 | } |
| 1328 | if (frame_ != NULL) { |
| 1329 | exit.Jump(); |
| 1330 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1331 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1332 | if (else_.is_linked()) { |
| 1333 | else_.Bind(); |
| 1334 | VisitAndSpill(node->else_statement()); |
| 1335 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1336 | |
| 1337 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1338 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1339 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1340 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1341 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1342 | LoadConditionAndSpill(node->condition(), &then, &exit, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1343 | if (frame_ != NULL) { |
| 1344 | Branch(false, &exit); |
| 1345 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1346 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1347 | if (frame_ != NULL || then.is_linked()) { |
| 1348 | then.Bind(); |
| 1349 | VisitAndSpill(node->then_statement()); |
| 1350 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1351 | |
| 1352 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1353 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1354 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1355 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1356 | // if (!cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1357 | LoadConditionAndSpill(node->condition(), &exit, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1358 | if (frame_ != NULL) { |
| 1359 | Branch(true, &exit); |
| 1360 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1361 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1362 | if (frame_ != NULL || else_.is_linked()) { |
| 1363 | else_.Bind(); |
| 1364 | VisitAndSpill(node->else_statement()); |
| 1365 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1366 | |
| 1367 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1368 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1369 | ASSERT(!has_then_stm && !has_else_stm); |
| 1370 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1371 | LoadConditionAndSpill(node->condition(), &exit, &exit, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1372 | if (frame_ != NULL) { |
| 1373 | if (has_cc()) { |
| 1374 | cc_reg_ = al; |
| 1375 | } else { |
| 1376 | frame_->Drop(); |
| 1377 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1382 | if (exit.is_linked()) { |
| 1383 | exit.Bind(); |
| 1384 | } |
| 1385 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1389 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1390 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1391 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1392 | CodeForStatementPosition(node); |
| 1393 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1397 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1398 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1399 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1400 | CodeForStatementPosition(node); |
| 1401 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1405 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1406 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1407 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1408 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1409 | CodeForStatementPosition(node); |
| 1410 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1411 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1412 | frame_->EmitPop(r0); |
| 1413 | function_return_.Jump(); |
| 1414 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1415 | // Pop the result from the frame and prepare the frame for |
| 1416 | // returning thus making it easier to merge. |
| 1417 | frame_->EmitPop(r0); |
| 1418 | frame_->PrepareForReturn(); |
| 1419 | |
| 1420 | function_return_.Jump(); |
| 1421 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1425 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1426 | #ifdef DEBUG |
| 1427 | int original_height = frame_->height(); |
| 1428 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1429 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1430 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1431 | CodeForStatementPosition(node); |
| 1432 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1433 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1434 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1435 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1436 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1437 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1438 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1439 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1440 | __ cmp(r0, Operand(cp)); |
| 1441 | verified_true.Branch(eq); |
| 1442 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1443 | verified_true.Bind(); |
| 1444 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1445 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1446 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1447 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1451 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1452 | #ifdef DEBUG |
| 1453 | int original_height = frame_->height(); |
| 1454 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1455 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1456 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1457 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1458 | // Pop context. |
| 1459 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1460 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1461 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1462 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1466 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1467 | #ifdef DEBUG |
| 1468 | int original_height = frame_->height(); |
| 1469 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1470 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1471 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1472 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1473 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1474 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1475 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1476 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1477 | JumpTarget next_test; |
| 1478 | JumpTarget fall_through; |
| 1479 | JumpTarget default_entry; |
| 1480 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1481 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1482 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1483 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1484 | |
| 1485 | for (int i = 0; i < length; i++) { |
| 1486 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1487 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1488 | // Remember the default clause and compile it at the end. |
| 1489 | default_clause = clause; |
| 1490 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1493 | Comment cmnt(masm_, "[ Case clause"); |
| 1494 | // Compile the test. |
| 1495 | next_test.Bind(); |
| 1496 | next_test.Unuse(); |
| 1497 | // Duplicate TOS. |
| 1498 | __ ldr(r0, frame_->Top()); |
| 1499 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1500 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1501 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1502 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1503 | // Before entering the body from the test, remove the switch value from |
| 1504 | // the stack. |
| 1505 | frame_->Drop(); |
| 1506 | |
| 1507 | // Label the body so that fall through is enabled. |
| 1508 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1509 | default_exit.Bind(); |
| 1510 | } else { |
| 1511 | fall_through.Bind(); |
| 1512 | fall_through.Unuse(); |
| 1513 | } |
| 1514 | VisitStatementsAndSpill(clause->statements()); |
| 1515 | |
| 1516 | // If control flow can fall through from the body, jump to the next body |
| 1517 | // or the end of the statement. |
| 1518 | if (frame_ != NULL) { |
| 1519 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1520 | default_entry.Jump(); |
| 1521 | } else { |
| 1522 | fall_through.Jump(); |
| 1523 | } |
| 1524 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1527 | // The final "test" removes the switch value. |
| 1528 | next_test.Bind(); |
| 1529 | frame_->Drop(); |
| 1530 | |
| 1531 | // If there is a default clause, compile it. |
| 1532 | if (default_clause != NULL) { |
| 1533 | Comment cmnt(masm_, "[ Default clause"); |
| 1534 | default_entry.Bind(); |
| 1535 | VisitStatementsAndSpill(default_clause->statements()); |
| 1536 | // If control flow can fall out of the default and there is a case after |
| 1537 | // it, jup to that case's body. |
| 1538 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1539 | default_exit.Jump(); |
| 1540 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1541 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1542 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1543 | if (fall_through.is_linked()) { |
| 1544 | fall_through.Bind(); |
| 1545 | } |
| 1546 | |
| 1547 | if (node->break_target()->is_linked()) { |
| 1548 | node->break_target()->Bind(); |
| 1549 | } |
| 1550 | node->break_target()->Unuse(); |
| 1551 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1555 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1556 | #ifdef DEBUG |
| 1557 | int original_height = frame_->height(); |
| 1558 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1559 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1560 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1561 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1562 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1563 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1564 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1565 | // Label the top of the loop for the backward CFG edge. If the test |
| 1566 | // is always true we can use the continue target, and if the test is |
| 1567 | // always false there is no need. |
| 1568 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1569 | switch (info) { |
| 1570 | case ALWAYS_TRUE: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1571 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1572 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1573 | break; |
| 1574 | case ALWAYS_FALSE: |
| 1575 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1576 | break; |
| 1577 | case DONT_KNOW: |
| 1578 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1579 | body.Bind(); |
| 1580 | break; |
| 1581 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1582 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1583 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1584 | VisitAndSpill(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1585 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1586 | // Compile the test. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1587 | switch (info) { |
| 1588 | case ALWAYS_TRUE: |
| 1589 | // If control can fall off the end of the body, jump back to the |
| 1590 | // top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1591 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1592 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1593 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1594 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1595 | case ALWAYS_FALSE: |
| 1596 | // If we have a continue in the body, we only have to bind its |
| 1597 | // jump target. |
| 1598 | if (node->continue_target()->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1599 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1600 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1601 | break; |
| 1602 | case DONT_KNOW: |
| 1603 | // We have to compile the test expression if it can be reached by |
| 1604 | // control flow falling out of the body or via continue. |
| 1605 | if (node->continue_target()->is_linked()) { |
| 1606 | node->continue_target()->Bind(); |
| 1607 | } |
| 1608 | if (has_valid_frame()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1609 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 1610 | CodeForDoWhileConditionPosition(node); |
| 1611 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1612 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1613 | // A invalid frame here indicates that control did not |
| 1614 | // fall out of the test expression. |
| 1615 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1616 | } |
| 1617 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1618 | break; |
| 1619 | } |
| 1620 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1621 | if (node->break_target()->is_linked()) { |
| 1622 | node->break_target()->Bind(); |
| 1623 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1624 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 1629 | #ifdef DEBUG |
| 1630 | int original_height = frame_->height(); |
| 1631 | #endif |
| 1632 | VirtualFrame::SpilledScope spilled_scope; |
| 1633 | Comment cmnt(masm_, "[ WhileStatement"); |
| 1634 | CodeForStatementPosition(node); |
| 1635 | |
| 1636 | // If the test is never true and has no side effects there is no need |
| 1637 | // to compile the test or body. |
| 1638 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1639 | if (info == ALWAYS_FALSE) return; |
| 1640 | |
| 1641 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1642 | |
| 1643 | // Label the top of the loop with the continue target for the backward |
| 1644 | // CFG edge. |
| 1645 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1646 | node->continue_target()->Bind(); |
| 1647 | |
| 1648 | if (info == DONT_KNOW) { |
| 1649 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1650 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1651 | if (has_valid_frame()) { |
| 1652 | // A NULL frame indicates that control did not fall out of the |
| 1653 | // test expression. |
| 1654 | Branch(false, node->break_target()); |
| 1655 | } |
| 1656 | if (has_valid_frame() || body.is_linked()) { |
| 1657 | body.Bind(); |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | if (has_valid_frame()) { |
| 1662 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1663 | VisitAndSpill(node->body()); |
| 1664 | |
| 1665 | // If control flow can fall out of the body, jump back to the top. |
| 1666 | if (has_valid_frame()) { |
| 1667 | node->continue_target()->Jump(); |
| 1668 | } |
| 1669 | } |
| 1670 | if (node->break_target()->is_linked()) { |
| 1671 | node->break_target()->Bind(); |
| 1672 | } |
| 1673 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1674 | } |
| 1675 | |
| 1676 | |
| 1677 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 1678 | #ifdef DEBUG |
| 1679 | int original_height = frame_->height(); |
| 1680 | #endif |
| 1681 | VirtualFrame::SpilledScope spilled_scope; |
| 1682 | Comment cmnt(masm_, "[ ForStatement"); |
| 1683 | CodeForStatementPosition(node); |
| 1684 | if (node->init() != NULL) { |
| 1685 | VisitAndSpill(node->init()); |
| 1686 | } |
| 1687 | |
| 1688 | // If the test is never true there is no need to compile the test or |
| 1689 | // body. |
| 1690 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1691 | if (info == ALWAYS_FALSE) return; |
| 1692 | |
| 1693 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1694 | |
| 1695 | // If there is no update statement, label the top of the loop with the |
| 1696 | // continue target, otherwise with the loop target. |
| 1697 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1698 | if (node->next() == NULL) { |
| 1699 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1700 | node->continue_target()->Bind(); |
| 1701 | } else { |
| 1702 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1703 | loop.Bind(); |
| 1704 | } |
| 1705 | |
| 1706 | // If the test is always true, there is no need to compile it. |
| 1707 | if (info == DONT_KNOW) { |
| 1708 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1709 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1710 | if (has_valid_frame()) { |
| 1711 | Branch(false, node->break_target()); |
| 1712 | } |
| 1713 | if (has_valid_frame() || body.is_linked()) { |
| 1714 | body.Bind(); |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | if (has_valid_frame()) { |
| 1719 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1720 | VisitAndSpill(node->body()); |
| 1721 | |
| 1722 | if (node->next() == NULL) { |
| 1723 | // If there is no update statement and control flow can fall out |
| 1724 | // of the loop, jump directly to the continue label. |
| 1725 | if (has_valid_frame()) { |
| 1726 | node->continue_target()->Jump(); |
| 1727 | } |
| 1728 | } else { |
| 1729 | // If there is an update statement and control flow can reach it |
| 1730 | // via falling out of the body of the loop or continuing, we |
| 1731 | // compile the update statement. |
| 1732 | if (node->continue_target()->is_linked()) { |
| 1733 | node->continue_target()->Bind(); |
| 1734 | } |
| 1735 | if (has_valid_frame()) { |
| 1736 | // Record source position of the statement as this code which is |
| 1737 | // after the code for the body actually belongs to the loop |
| 1738 | // statement and not the body. |
| 1739 | CodeForStatementPosition(node); |
| 1740 | VisitAndSpill(node->next()); |
| 1741 | loop.Jump(); |
| 1742 | } |
| 1743 | } |
| 1744 | } |
| 1745 | if (node->break_target()->is_linked()) { |
| 1746 | node->break_target()->Bind(); |
| 1747 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1748 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1752 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1753 | #ifdef DEBUG |
| 1754 | int original_height = frame_->height(); |
| 1755 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1756 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1757 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1758 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1759 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1760 | JumpTarget primitive; |
| 1761 | JumpTarget jsobject; |
| 1762 | JumpTarget fixed_array; |
| 1763 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1764 | JumpTarget end_del_check; |
| 1765 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1766 | |
| 1767 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1768 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1769 | |
| 1770 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1771 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1772 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1773 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 1774 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1775 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1776 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1777 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1778 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1779 | |
| 1780 | // Stack layout in body: |
| 1781 | // [iteration counter (Smi)] |
| 1782 | // [length of array] |
| 1783 | // [FixedArray] |
| 1784 | // [Map or 0] |
| 1785 | // [Object] |
| 1786 | |
| 1787 | // Check if enumerable is already a JSObject |
| 1788 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1789 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1790 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1791 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1792 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1793 | primitive.Bind(); |
| 1794 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1795 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1796 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1797 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1798 | // Get the set of properties (as a FixedArray or Map). |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1799 | // r0: value to be iterated over |
| 1800 | frame_->EmitPush(r0); // Push the object being iterated over. |
| 1801 | |
| 1802 | // Check cache validity in generated code. This is a fast case for |
| 1803 | // the JSObject::IsSimpleEnum cache validity checks. If we cannot |
| 1804 | // guarantee cache validity, call the runtime system to check cache |
| 1805 | // validity or get the property names in a fixed array. |
| 1806 | JumpTarget call_runtime; |
| 1807 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1808 | JumpTarget check_prototype; |
| 1809 | JumpTarget use_cache; |
| 1810 | __ mov(r1, Operand(r0)); |
| 1811 | loop.Bind(); |
| 1812 | // Check that there are no elements. |
| 1813 | __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 1814 | __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex); |
| 1815 | __ cmp(r2, r4); |
| 1816 | call_runtime.Branch(ne); |
| 1817 | // Check that instance descriptors are not empty so that we can |
| 1818 | // check for an enum cache. Leave the map in r3 for the subsequent |
| 1819 | // prototype load. |
| 1820 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1821 | __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset)); |
| 1822 | __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex); |
| 1823 | __ cmp(r2, ip); |
| 1824 | call_runtime.Branch(eq); |
| 1825 | // Check that there in an enum cache in the non-empty instance |
| 1826 | // descriptors. This is the case if the next enumeration index |
| 1827 | // field does not contain a smi. |
| 1828 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset)); |
| 1829 | __ tst(r2, Operand(kSmiTagMask)); |
| 1830 | call_runtime.Branch(eq); |
| 1831 | // For all objects but the receiver, check that the cache is empty. |
| 1832 | // r4: empty fixed array root. |
| 1833 | __ cmp(r1, r0); |
| 1834 | check_prototype.Branch(eq); |
| 1835 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1836 | __ cmp(r2, r4); |
| 1837 | call_runtime.Branch(ne); |
| 1838 | check_prototype.Bind(); |
| 1839 | // Load the prototype from the map and loop if non-null. |
| 1840 | __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 1841 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1842 | __ cmp(r1, ip); |
| 1843 | loop.Branch(ne); |
| 1844 | // The enum cache is valid. Load the map of the object being |
| 1845 | // iterated over and use the cache for the iteration. |
| 1846 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1847 | use_cache.Jump(); |
| 1848 | |
| 1849 | call_runtime.Bind(); |
| 1850 | // Call the runtime to get the property names for the object. |
| 1851 | 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] | 1852 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1853 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1854 | // If we got a map from the runtime call, we can do a fast |
| 1855 | // modification check. Otherwise, we got a fixed array, and we have |
| 1856 | // to do a slow check. |
| 1857 | // r0: map or fixed array (result from call to |
| 1858 | // Runtime::kGetPropertyNamesFast) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | __ mov(r2, Operand(r0)); |
| 1860 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1861 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 1862 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1863 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1864 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1865 | use_cache.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1866 | // Get enum cache |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1867 | // r0: map (either the result from a call to |
| 1868 | // Runtime::kGetPropertyNamesFast or has been fetched directly from |
| 1869 | // the object) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1870 | __ mov(r1, Operand(r0)); |
| 1871 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1872 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1873 | __ ldr(r2, |
| 1874 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1875 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1876 | frame_->EmitPush(r0); // map |
| 1877 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1878 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1879 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1880 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1881 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1882 | frame_->EmitPush(r0); |
| 1883 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1885 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1886 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1887 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1888 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1889 | |
| 1890 | // 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] | 1891 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1892 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1893 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1894 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1895 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1896 | |
| 1897 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1898 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1899 | // sp[0] : index |
| 1900 | // sp[1] : array/enum cache length |
| 1901 | // sp[2] : array or enum cache |
| 1902 | // sp[3] : 0 or map |
| 1903 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1904 | // Grab the current frame's height for the break and continue |
| 1905 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1906 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1907 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1908 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1909 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1910 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1911 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1912 | node->break_target()->Branch(hs); |
| 1913 | |
| 1914 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1915 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1916 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1917 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1918 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1919 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1920 | |
| 1921 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1922 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1923 | // Check if this (still) matches the map of the enumerable. |
| 1924 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1925 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1926 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1927 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1928 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1929 | |
| 1930 | // 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] | 1931 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1932 | frame_->EmitPush(r0); |
| 1933 | frame_->EmitPush(r3); // push entry |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1934 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1935 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1936 | |
| 1937 | // 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] | 1938 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1939 | __ cmp(r3, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1940 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1941 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1942 | end_del_check.Bind(); |
| 1943 | // Store the entry in the 'each' expression and take another spin in the |
| 1944 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1945 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1946 | { Reference each(this, node->each()); |
| 1947 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1948 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1949 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1950 | frame_->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1951 | each.SetValue(NOT_CONST_INIT); |
| 1952 | frame_->Drop(2); |
| 1953 | } else { |
| 1954 | // If the reference was to a slot we rely on the convenient property |
| 1955 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1956 | // right on top of or right underneath a zero-sized reference. |
| 1957 | each.SetValue(NOT_CONST_INIT); |
| 1958 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1959 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1960 | } |
| 1961 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1962 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1963 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1964 | VisitAndSpill(node->body()); |
| 1965 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1966 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1967 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1968 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1969 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1970 | frame_->EmitPop(r0); |
| 1971 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1972 | frame_->EmitPush(r0); |
| 1973 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1974 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1975 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1976 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1977 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1978 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1979 | |
| 1980 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1981 | exit.Bind(); |
| 1982 | node->continue_target()->Unuse(); |
| 1983 | node->break_target()->Unuse(); |
| 1984 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1988 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1989 | #ifdef DEBUG |
| 1990 | int original_height = frame_->height(); |
| 1991 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1992 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1993 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1994 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1995 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1996 | JumpTarget try_block; |
| 1997 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1998 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1999 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2000 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2001 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2002 | |
| 2003 | // Store the caught exception in the catch variable. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2004 | Variable* catch_var = node->catch_var()->var(); |
| 2005 | ASSERT(catch_var != NULL && catch_var->slot() != NULL); |
| 2006 | StoreToSlot(catch_var->slot(), NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2007 | |
| 2008 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2009 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2010 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2011 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2012 | if (frame_ != NULL) { |
| 2013 | exit.Jump(); |
| 2014 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2015 | |
| 2016 | |
| 2017 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2018 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2019 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2020 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2021 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2022 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2023 | // Shadow the labels for all escapes from the try block, including |
| 2024 | // returns. During shadowing, the original label is hidden as the |
| 2025 | // LabelShadow and operations on the original actually affect the |
| 2026 | // shadowing label. |
| 2027 | // |
| 2028 | // We should probably try to unify the escaping labels and the return |
| 2029 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2030 | int nof_escapes = node->escaping_targets()->length(); |
| 2031 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2032 | |
| 2033 | // Add the shadow target for the function return. |
| 2034 | static const int kReturnShadowIndex = 0; |
| 2035 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2036 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2037 | function_return_is_shadowed_ = true; |
| 2038 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2039 | |
| 2040 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2041 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2042 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2046 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2047 | |
| 2048 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2049 | // After shadowing stops, the original labels are unshadowed and the |
| 2050 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2051 | bool has_unlinks = false; |
| 2052 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2053 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2054 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2055 | } |
| 2056 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2057 | |
| 2058 | // Get an external reference to the handler address. |
| 2059 | ExternalReference handler_address(Top::k_handler_address); |
| 2060 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2061 | // If we can fall off the end of the try block, unlink from try chain. |
| 2062 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2063 | // The next handler address is on top of the frame. Unlink from |
| 2064 | // the handler list and drop the rest of this handler from the |
| 2065 | // frame. |
| 2066 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2067 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2068 | __ mov(r3, Operand(handler_address)); |
| 2069 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2070 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2071 | if (has_unlinks) { |
| 2072 | exit.Jump(); |
| 2073 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2076 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2077 | // jumped to. Deallocate each shadow target. |
| 2078 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2079 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2080 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2081 | shadows[i]->Bind(); |
| 2082 | // Because we can be jumping here (to spilled code) from unspilled |
| 2083 | // code, we need to reestablish a spilled frame at this block. |
| 2084 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2085 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2086 | // Reload sp from the top handler, because some statements that we |
| 2087 | // 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] | 2088 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2089 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2090 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2091 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2092 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2093 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2094 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2095 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2096 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2097 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2098 | frame_->PrepareForReturn(); |
| 2099 | } |
| 2100 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2104 | exit.Bind(); |
| 2105 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2109 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2110 | #ifdef DEBUG |
| 2111 | int original_height = frame_->height(); |
| 2112 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2113 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2114 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2115 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2116 | |
| 2117 | // State: Used to keep track of reason for entering the finally |
| 2118 | // block. Should probably be extended to hold information for |
| 2119 | // break/continue from within the try block. |
| 2120 | enum { FALLING, THROWING, JUMPING }; |
| 2121 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2122 | JumpTarget try_block; |
| 2123 | JumpTarget finally_block; |
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 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2126 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2127 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2128 | // In case of thrown exceptions, this is where we continue. |
| 2129 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2130 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2131 | |
| 2132 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2133 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2134 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2135 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2136 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2137 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2138 | // Shadow the labels for all escapes from the try block, including |
| 2139 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2140 | // operations on the original actually affect the shadowing label. |
| 2141 | // |
| 2142 | // We should probably try to unify the escaping labels and the return |
| 2143 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2144 | int nof_escapes = node->escaping_targets()->length(); |
| 2145 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2146 | |
| 2147 | // Add the shadow target for the function return. |
| 2148 | static const int kReturnShadowIndex = 0; |
| 2149 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2150 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2151 | function_return_is_shadowed_ = true; |
| 2152 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2153 | |
| 2154 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2155 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2156 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2160 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2161 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2162 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2163 | // After shadowing stops, the original labels are unshadowed and the |
| 2164 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2165 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2166 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2167 | shadows[i]->StopShadowing(); |
| 2168 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2169 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2170 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2171 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2172 | // Get an external reference to the handler address. |
| 2173 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2174 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2175 | // If we can fall off the end of the try block, unlink from the try |
| 2176 | // chain and set the state on the frame to FALLING. |
| 2177 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2178 | // The next handler address is on top of the frame. |
| 2179 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2180 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2181 | __ mov(r3, Operand(handler_address)); |
| 2182 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2183 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2184 | |
| 2185 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2186 | // state in r2, then jump around the unlink blocks if any. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2187 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2188 | frame_->EmitPush(r0); |
| 2189 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2190 | if (nof_unlinks > 0) { |
| 2191 | finally_block.Jump(); |
| 2192 | } |
| 2193 | } |
| 2194 | |
| 2195 | // Generate code to unlink and set the state for the (formerly) |
| 2196 | // shadowing targets that have been jumped to. |
| 2197 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2198 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2199 | // If we have come from the shadowed return, the return value is |
| 2200 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2201 | // until it is pushed. |
| 2202 | // |
| 2203 | // Because we can be jumping here (to spilled code) from |
| 2204 | // unspilled code, we need to reestablish a spilled frame at |
| 2205 | // this block. |
| 2206 | shadows[i]->Bind(); |
| 2207 | frame_->SpillAll(); |
| 2208 | |
| 2209 | // Reload sp from the top handler, because some statements that |
| 2210 | // we break from (eg, for...in) may have left stuff on the |
| 2211 | // stack. |
| 2212 | __ mov(r3, Operand(handler_address)); |
| 2213 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2214 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2215 | |
| 2216 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2217 | // handler address is currently on top of the frame. |
| 2218 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2219 | frame_->EmitPop(r1); |
| 2220 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2221 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2222 | |
| 2223 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2224 | // If this label shadowed the function return, materialize the |
| 2225 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2226 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2227 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2228 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2229 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2230 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2231 | } |
| 2232 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2233 | if (--nof_unlinks > 0) { |
| 2234 | // If this is not the last unlink block, jump around the next. |
| 2235 | finally_block.Jump(); |
| 2236 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2237 | } |
| 2238 | } |
| 2239 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2240 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2241 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2242 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2243 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2244 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2245 | |
| 2246 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2247 | // and the state - while evaluating the finally block. |
| 2248 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2249 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2250 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2251 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2252 | if (has_valid_frame()) { |
| 2253 | // Restore state and return value or faked TOS. |
| 2254 | frame_->EmitPop(r2); |
| 2255 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2258 | // Generate code to jump to the right destination for all used |
| 2259 | // formerly shadowing targets. Deallocate each shadow target. |
| 2260 | for (int i = 0; i < shadows.length(); i++) { |
| 2261 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2262 | JumpTarget* original = shadows[i]->other_target(); |
| 2263 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2264 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2265 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2266 | skip.Branch(ne); |
| 2267 | frame_->PrepareForReturn(); |
| 2268 | original->Jump(); |
| 2269 | skip.Bind(); |
| 2270 | } else { |
| 2271 | original->Branch(eq); |
| 2272 | } |
| 2273 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2274 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2275 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2276 | if (has_valid_frame()) { |
| 2277 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2278 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2279 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2280 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2281 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2282 | // Rethrow exception. |
| 2283 | frame_->EmitPush(r0); |
| 2284 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2285 | |
| 2286 | // Done. |
| 2287 | exit.Bind(); |
| 2288 | } |
| 2289 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2293 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2294 | #ifdef DEBUG |
| 2295 | int original_height = frame_->height(); |
| 2296 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2297 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2298 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2299 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2300 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2301 | frame_->DebugBreak(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2302 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2303 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2304 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2308 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2309 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2310 | ASSERT(boilerplate->IsBoilerplate()); |
| 2311 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2312 | __ mov(r0, Operand(boilerplate)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2313 | // Use the fast case closure allocation code that allocates in new |
| 2314 | // space for nested functions that don't need literals cloning. |
| 2315 | if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) { |
| 2316 | FastNewClosureStub stub; |
| 2317 | frame_->EmitPush(r0); |
| 2318 | frame_->CallStub(&stub, 1); |
| 2319 | frame_->EmitPush(r0); |
| 2320 | } else { |
| 2321 | // Create a new closure. |
| 2322 | frame_->EmitPush(cp); |
| 2323 | frame_->EmitPush(r0); |
| 2324 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2325 | frame_->EmitPush(r0); |
| 2326 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2330 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2331 | #ifdef DEBUG |
| 2332 | int original_height = frame_->height(); |
| 2333 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2334 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2335 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2336 | |
| 2337 | // Build the function boilerplate and instantiate it. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2338 | Handle<JSFunction> boilerplate = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2339 | Compiler::BuildBoilerplate(node, script(), this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2340 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2341 | if (HasStackOverflow()) { |
| 2342 | ASSERT(frame_->height() == original_height); |
| 2343 | return; |
| 2344 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2345 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2346 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2350 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2351 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2352 | #ifdef DEBUG |
| 2353 | int original_height = frame_->height(); |
| 2354 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2355 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2356 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2357 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2358 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2362 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2363 | #ifdef DEBUG |
| 2364 | int original_height = frame_->height(); |
| 2365 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2366 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2367 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2368 | JumpTarget then; |
| 2369 | JumpTarget else_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2370 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2371 | if (has_valid_frame()) { |
| 2372 | Branch(false, &else_); |
| 2373 | } |
| 2374 | if (has_valid_frame() || then.is_linked()) { |
| 2375 | then.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2376 | LoadAndSpill(node->then_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2377 | } |
| 2378 | if (else_.is_linked()) { |
| 2379 | JumpTarget exit; |
| 2380 | if (has_valid_frame()) exit.Jump(); |
| 2381 | else_.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2382 | LoadAndSpill(node->else_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2383 | if (exit.is_linked()) exit.Bind(); |
| 2384 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2385 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2389 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2390 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2391 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2392 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2393 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2394 | JumpTarget slow; |
| 2395 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2396 | |
| 2397 | // Generate fast-case code for variables that might be shadowed by |
| 2398 | // eval-introduced variables. Eval is used a lot without |
| 2399 | // introducing variables. In those cases, we do not want to |
| 2400 | // perform a runtime call for all variables in the scope |
| 2401 | // containing the eval. |
| 2402 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2403 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2404 | // If there was no control flow to slow, we can exit early. |
| 2405 | if (!slow.is_linked()) { |
| 2406 | frame_->EmitPush(r0); |
| 2407 | return; |
| 2408 | } |
| 2409 | |
| 2410 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2411 | |
| 2412 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2413 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2414 | // Only generate the fast case for locals that rewrite to slots. |
| 2415 | // This rules out argument loads. |
| 2416 | if (potential_slot != NULL) { |
| 2417 | __ ldr(r0, |
| 2418 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2419 | r1, |
| 2420 | r2, |
| 2421 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2422 | if (potential_slot->var()->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2423 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2424 | __ cmp(r0, ip); |
| 2425 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2426 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2427 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2428 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2429 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2430 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2431 | } |
| 2432 | } |
| 2433 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2434 | slow.Bind(); |
| 2435 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2436 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2437 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2438 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2439 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2440 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2441 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2442 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2443 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2444 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2445 | done.Bind(); |
| 2446 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2447 | |
| 2448 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2449 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2450 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2451 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2452 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2453 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2454 | // initialized yet) which needs to be converted into the 'undefined' |
| 2455 | // value. |
| 2456 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2457 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2458 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2459 | __ cmp(r0, ip); |
| 2460 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2461 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2467 | void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) { |
| 2468 | ASSERT(slot != NULL); |
| 2469 | if (slot->type() == Slot::LOOKUP) { |
| 2470 | ASSERT(slot->var()->is_dynamic()); |
| 2471 | |
| 2472 | // For now, just do a runtime call. |
| 2473 | frame_->EmitPush(cp); |
| 2474 | __ mov(r0, Operand(slot->var()->name())); |
| 2475 | frame_->EmitPush(r0); |
| 2476 | |
| 2477 | if (init_state == CONST_INIT) { |
| 2478 | // Same as the case for a normal store, but ignores attribute |
| 2479 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 2480 | // const properties (introduced via eval("const foo = (some |
| 2481 | // expr);")). Also, uses the current function context instead of |
| 2482 | // the top context. |
| 2483 | // |
| 2484 | // Note that we must declare the foo upon entry of eval(), via a |
| 2485 | // context slot declaration, but we cannot initialize it at the |
| 2486 | // same time, because the const declaration may be at the end of |
| 2487 | // the eval code (sigh...) and the const variable may have been |
| 2488 | // used before (where its value is 'undefined'). Thus, we can only |
| 2489 | // do the initialization when we actually encounter the expression |
| 2490 | // and when the expression operands are defined and valid, and |
| 2491 | // thus we need the split into 2 operations: declaration of the |
| 2492 | // context slot followed by initialization. |
| 2493 | frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 2494 | } else { |
| 2495 | frame_->CallRuntime(Runtime::kStoreContextSlot, 3); |
| 2496 | } |
| 2497 | // Storing a variable must keep the (new) value on the expression |
| 2498 | // stack. This is necessary for compiling assignment expressions. |
| 2499 | frame_->EmitPush(r0); |
| 2500 | |
| 2501 | } else { |
| 2502 | ASSERT(!slot->var()->is_dynamic()); |
| 2503 | |
| 2504 | JumpTarget exit; |
| 2505 | if (init_state == CONST_INIT) { |
| 2506 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 2507 | // Only the first const initialization must be executed (the slot |
| 2508 | // still contains 'the hole' value). When the assignment is |
| 2509 | // executed, the code is identical to a normal store (see below). |
| 2510 | Comment cmnt(masm_, "[ Init const"); |
| 2511 | __ ldr(r2, SlotOperand(slot, r2)); |
| 2512 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2513 | __ cmp(r2, ip); |
| 2514 | exit.Branch(ne); |
| 2515 | } |
| 2516 | |
| 2517 | // We must execute the store. Storing a variable must keep the |
| 2518 | // (new) value on the stack. This is necessary for compiling |
| 2519 | // assignment expressions. |
| 2520 | // |
| 2521 | // Note: We will reach here even with slot->var()->mode() == |
| 2522 | // Variable::CONST because of const declarations which will |
| 2523 | // initialize consts to 'the hole' value and by doing so, end up |
| 2524 | // calling this code. r2 may be loaded with context; used below in |
| 2525 | // RecordWrite. |
| 2526 | frame_->EmitPop(r0); |
| 2527 | __ str(r0, SlotOperand(slot, r2)); |
| 2528 | frame_->EmitPush(r0); |
| 2529 | if (slot->type() == Slot::CONTEXT) { |
| 2530 | // Skip write barrier if the written value is a smi. |
| 2531 | __ tst(r0, Operand(kSmiTagMask)); |
| 2532 | exit.Branch(eq); |
| 2533 | // r2 is loaded with context when calling SlotOperand above. |
| 2534 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 2535 | __ mov(r3, Operand(offset)); |
| 2536 | __ RecordWrite(r2, r3, r1); |
| 2537 | } |
| 2538 | // If we definitely did not jump over the assignment, we do not need |
| 2539 | // to bind the exit label. Doing so can defeat peephole |
| 2540 | // optimization. |
| 2541 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
| 2542 | exit.Bind(); |
| 2543 | } |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2548 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2549 | TypeofState typeof_state, |
| 2550 | Register tmp, |
| 2551 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2552 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2553 | // Check that no extension objects have been created by calls to |
| 2554 | // eval from the current scope to the global scope. |
| 2555 | Register context = cp; |
| 2556 | Scope* s = scope(); |
| 2557 | while (s != NULL) { |
| 2558 | if (s->num_heap_slots() > 0) { |
| 2559 | if (s->calls_eval()) { |
| 2560 | // Check that extension is NULL. |
| 2561 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2562 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2563 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2564 | } |
| 2565 | // Load next context in chain. |
| 2566 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2567 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2568 | context = tmp; |
| 2569 | } |
| 2570 | // If no outer scope calls eval, we do not need to check more |
| 2571 | // context extensions. |
| 2572 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2573 | s = s->outer_scope(); |
| 2574 | } |
| 2575 | |
| 2576 | if (s->is_eval_scope()) { |
| 2577 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2578 | if (!context.is(tmp)) { |
| 2579 | __ mov(tmp, Operand(context)); |
| 2580 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2581 | __ bind(&next); |
| 2582 | // Terminate at global context. |
| 2583 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2584 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2585 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2586 | __ b(eq, &fast); |
| 2587 | // Check that extension is NULL. |
| 2588 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2589 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2590 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2591 | // Load next context in chain. |
| 2592 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2593 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2594 | __ b(&next); |
| 2595 | __ bind(&fast); |
| 2596 | } |
| 2597 | |
| 2598 | // All extension objects were empty and it is safe to use a global |
| 2599 | // load IC call. |
| 2600 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2601 | // Load the global object. |
| 2602 | LoadGlobal(); |
| 2603 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2604 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2605 | // Call IC stub. |
| 2606 | if (typeof_state == INSIDE_TYPEOF) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2607 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2608 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2609 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2612 | // Drop the global object. The result is in r0. |
| 2613 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2617 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2618 | #ifdef DEBUG |
| 2619 | int original_height = frame_->height(); |
| 2620 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2621 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2622 | Comment cmnt(masm_, "[ Slot"); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2623 | LoadFromSlot(node, NOT_INSIDE_TYPEOF); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2624 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2625 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2626 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2627 | |
| 2628 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2629 | #ifdef DEBUG |
| 2630 | int original_height = frame_->height(); |
| 2631 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2632 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2633 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2634 | |
| 2635 | Variable* var = node->var(); |
| 2636 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2637 | if (expr != NULL) { |
| 2638 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2639 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2640 | ASSERT(var->is_global()); |
| 2641 | Reference ref(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2642 | ref.GetValueAndSpill(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2643 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2644 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2648 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2649 | #ifdef DEBUG |
| 2650 | int original_height = frame_->height(); |
| 2651 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2652 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2653 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2654 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2655 | frame_->EmitPush(r0); |
| 2656 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2660 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2661 | #ifdef DEBUG |
| 2662 | int original_height = frame_->height(); |
| 2663 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2664 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2665 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2666 | |
| 2667 | // Retrieve the literal array and check the allocated entry. |
| 2668 | |
| 2669 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2670 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2671 | |
| 2672 | // Load the literals array of the function. |
| 2673 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2674 | |
| 2675 | // Load the literal at the ast saved index. |
| 2676 | int literal_offset = |
| 2677 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2678 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2679 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2680 | JumpTarget done; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2681 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2682 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2683 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2684 | |
| 2685 | // If the entry is undefined we call the runtime system to computed |
| 2686 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2687 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2688 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2689 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2690 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2691 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2692 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2693 | frame_->EmitPush(r0); |
| 2694 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2695 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2696 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2697 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2698 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2699 | frame_->EmitPush(r2); |
| 2700 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2704 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2705 | #ifdef DEBUG |
| 2706 | int original_height = frame_->height(); |
| 2707 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2708 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2709 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2710 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2711 | // Load the function of this activation. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 2712 | __ ldr(r3, frame_->Function()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2713 | // Literal array. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 2714 | __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2715 | // Literal index. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 2716 | __ mov(r2, Operand(Smi::FromInt(node->literal_index()))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2717 | // Constant properties. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 2718 | __ mov(r1, Operand(node->constant_properties())); |
| 2719 | // Should the object literal have fast elements? |
| 2720 | __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0))); |
| 2721 | frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2722 | if (node->depth() > 1) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 2723 | frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2724 | } else { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 2725 | frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2726 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2727 | frame_->EmitPush(r0); // save the result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2728 | for (int i = 0; i < node->properties()->length(); i++) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2729 | // At the start of each iteration, the top of stack contains |
| 2730 | // the newly created object literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2731 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2732 | Literal* key = property->key(); |
| 2733 | Expression* value = property->value(); |
| 2734 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2735 | case ObjectLiteral::Property::CONSTANT: |
| 2736 | break; |
| 2737 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2738 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2739 | // else fall through |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2740 | case ObjectLiteral::Property::COMPUTED: |
| 2741 | if (key->handle()->IsSymbol()) { |
| 2742 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 2743 | LoadAndSpill(value); |
| 2744 | frame_->EmitPop(r0); |
| 2745 | __ mov(r2, Operand(key->handle())); |
| 2746 | __ ldr(r1, frame_->Top()); // Load the receiver. |
| 2747 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
| 2748 | break; |
| 2749 | } |
| 2750 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2751 | case ObjectLiteral::Property::PROTOTYPE: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2752 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2753 | frame_->EmitPush(r0); // dup the result |
| 2754 | LoadAndSpill(key); |
| 2755 | LoadAndSpill(value); |
| 2756 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2757 | break; |
| 2758 | } |
| 2759 | case ObjectLiteral::Property::SETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2760 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2761 | frame_->EmitPush(r0); |
| 2762 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2763 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2764 | frame_->EmitPush(r0); |
| 2765 | LoadAndSpill(value); |
| 2766 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2767 | break; |
| 2768 | } |
| 2769 | case ObjectLiteral::Property::GETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2770 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2771 | frame_->EmitPush(r0); |
| 2772 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2773 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2774 | frame_->EmitPush(r0); |
| 2775 | LoadAndSpill(value); |
| 2776 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2777 | break; |
| 2778 | } |
| 2779 | } |
| 2780 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2781 | ASSERT(frame_->height() == original_height + 1); |
| 2782 | } |
| 2783 | |
| 2784 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2785 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2786 | #ifdef DEBUG |
| 2787 | int original_height = frame_->height(); |
| 2788 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2789 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2790 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2791 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2792 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2793 | __ ldr(r2, frame_->Function()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2794 | // Load the literals array of the function. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2795 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2796 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2797 | __ mov(r0, Operand(node->constant_elements())); |
| 2798 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2799 | int length = node->values()->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2800 | if (node->depth() > 1) { |
| 2801 | frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2802 | } else if (length > FastCloneShallowArrayStub::kMaximumLength) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2803 | frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2804 | } else { |
| 2805 | FastCloneShallowArrayStub stub(length); |
| 2806 | frame_->CallStub(&stub, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2807 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2808 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2809 | // r0: created object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2810 | |
| 2811 | // Generate code to set the elements in the array that are not |
| 2812 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2813 | for (int i = 0; i < node->values()->length(); i++) { |
| 2814 | Expression* value = node->values()->at(i); |
| 2815 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2816 | // If value is a literal the property value is already set in the |
| 2817 | // boilerplate object. |
| 2818 | if (value->AsLiteral() != NULL) continue; |
| 2819 | // If value is a materialized literal the property value is already set |
| 2820 | // in the boilerplate object if it is simple. |
| 2821 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
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 | // The property must be set by generated code. |
| 2824 | LoadAndSpill(value); |
| 2825 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2826 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2827 | // Fetch the object literal. |
| 2828 | __ ldr(r1, frame_->Top()); |
| 2829 | // Get the elements array. |
| 2830 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2831 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2832 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2833 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2834 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2835 | |
| 2836 | // Update the write barrier for the array address. |
| 2837 | __ mov(r3, Operand(offset)); |
| 2838 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2839 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2840 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2844 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2845 | #ifdef DEBUG |
| 2846 | int original_height = frame_->height(); |
| 2847 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2848 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2849 | // Call runtime routine to allocate the catch extension object and |
| 2850 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2851 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2852 | LoadAndSpill(node->key()); |
| 2853 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2854 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2855 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2856 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2860 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2861 | #ifdef DEBUG |
| 2862 | int original_height = frame_->height(); |
| 2863 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2864 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2865 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2866 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2867 | { Reference target(this, node->target(), node->is_compound()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2868 | if (target.is_illegal()) { |
| 2869 | // Fool the virtual frame into thinking that we left the assignment's |
| 2870 | // value on the frame. |
| 2871 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2872 | frame_->EmitPush(r0); |
| 2873 | ASSERT(frame_->height() == original_height + 1); |
| 2874 | return; |
| 2875 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2876 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2877 | if (node->op() == Token::ASSIGN || |
| 2878 | node->op() == Token::INIT_VAR || |
| 2879 | node->op() == Token::INIT_CONST) { |
| 2880 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2881 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2882 | } else { // Assignment is a compound assignment. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2883 | // Get the old value of the lhs. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2884 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2885 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2886 | bool overwrite = |
| 2887 | (node->value()->AsBinaryOperation() != NULL && |
| 2888 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2889 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2890 | SmiOperation(node->binary_op(), |
| 2891 | literal->handle(), |
| 2892 | false, |
| 2893 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2894 | frame_->EmitPush(r0); |
| 2895 | |
| 2896 | } else { |
| 2897 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2898 | GenericBinaryOperation(node->binary_op(), |
| 2899 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2900 | frame_->EmitPush(r0); |
| 2901 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2902 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2903 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2904 | if (var != NULL && |
| 2905 | (var->mode() == Variable::CONST) && |
| 2906 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2907 | // Assignment ignored - leave the value on the stack. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2908 | UnloadReference(&target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2909 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2910 | CodeForSourcePosition(node->position()); |
| 2911 | if (node->op() == Token::INIT_CONST) { |
| 2912 | // Dynamic constant initializations must use the function context |
| 2913 | // and initialize the actual constant declared. Dynamic variable |
| 2914 | // initializations are simply assignments and use SetValue. |
| 2915 | target.SetValue(CONST_INIT); |
| 2916 | } else { |
| 2917 | target.SetValue(NOT_CONST_INIT); |
| 2918 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2919 | } |
| 2920 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2921 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2925 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2926 | #ifdef DEBUG |
| 2927 | int original_height = frame_->height(); |
| 2928 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2929 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2930 | Comment cmnt(masm_, "[ Throw"); |
| 2931 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2932 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2933 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2934 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2935 | frame_->EmitPush(r0); |
| 2936 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2940 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2941 | #ifdef DEBUG |
| 2942 | int original_height = frame_->height(); |
| 2943 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2944 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2945 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2946 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2947 | { Reference property(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2948 | property.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2949 | } |
| 2950 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2954 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2955 | #ifdef DEBUG |
| 2956 | int original_height = frame_->height(); |
| 2957 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2958 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2959 | Comment cmnt(masm_, "[ Call"); |
| 2960 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2961 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2962 | ZoneList<Expression*>* args = node->arguments(); |
| 2963 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2964 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2965 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2966 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2967 | Property* property = function->AsProperty(); |
| 2968 | |
| 2969 | // ------------------------------------------------------------------------ |
| 2970 | // Fast-case: Use inline caching. |
| 2971 | // --- |
| 2972 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2973 | // must be resolved after the arguments have been evaluated. The IC code |
| 2974 | // automatically handles this by loading the arguments before the function |
| 2975 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2976 | // ------------------------------------------------------------------------ |
| 2977 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2978 | if (var != NULL && var->is_possibly_eval()) { |
| 2979 | // ---------------------------------- |
| 2980 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 2981 | // ---------------------------------- |
| 2982 | |
| 2983 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 2984 | // resolve the function we need to call and the receiver of the |
| 2985 | // call. Then we call the resolved function using the given |
| 2986 | // arguments. |
| 2987 | // Prepare stack for call to resolved function. |
| 2988 | LoadAndSpill(function); |
| 2989 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 2990 | frame_->EmitPush(r2); // Slot for receiver |
| 2991 | int arg_count = args->length(); |
| 2992 | for (int i = 0; i < arg_count; i++) { |
| 2993 | LoadAndSpill(args->at(i)); |
| 2994 | } |
| 2995 | |
| 2996 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 2997 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 2998 | frame_->EmitPush(r1); |
| 2999 | if (arg_count > 0) { |
| 3000 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3001 | frame_->EmitPush(r1); |
| 3002 | } else { |
| 3003 | frame_->EmitPush(r2); |
| 3004 | } |
| 3005 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3006 | // Push the receiver. |
| 3007 | __ ldr(r1, frame_->Receiver()); |
| 3008 | frame_->EmitPush(r1); |
| 3009 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3010 | // Resolve the call. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3011 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3012 | |
| 3013 | // 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] | 3014 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3015 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3016 | |
| 3017 | // Call the function. |
| 3018 | CodeForSourcePosition(node->position()); |
| 3019 | |
| 3020 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3021 | CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3022 | frame_->CallStub(&call_function, arg_count + 1); |
| 3023 | |
| 3024 | __ ldr(cp, frame_->Context()); |
| 3025 | // Remove the function from the stack. |
| 3026 | frame_->Drop(); |
| 3027 | frame_->EmitPush(r0); |
| 3028 | |
| 3029 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3030 | // ---------------------------------- |
| 3031 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3032 | // ---------------------------------- |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3033 | // Pass the global object as the receiver and let the IC stub |
| 3034 | // patch the stack to use the global proxy as 'this' in the |
| 3035 | // invoked function. |
| 3036 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3037 | |
| 3038 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3039 | int arg_count = args->length(); |
| 3040 | for (int i = 0; i < arg_count; i++) { |
| 3041 | LoadAndSpill(args->at(i)); |
| 3042 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3043 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3044 | // Setup the name register and call the IC initialization code. |
| 3045 | __ mov(r2, Operand(var->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3046 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3047 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3048 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3049 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3050 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3051 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3052 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3053 | |
| 3054 | } else if (var != NULL && var->slot() != NULL && |
| 3055 | var->slot()->type() == Slot::LOOKUP) { |
| 3056 | // ---------------------------------- |
| 3057 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3058 | // ---------------------------------- |
| 3059 | |
| 3060 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3061 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3062 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3063 | frame_->EmitPush(r0); |
| 3064 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3065 | // r0: slot value; r1: receiver |
| 3066 | |
| 3067 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3068 | frame_->EmitPush(r0); // function |
| 3069 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3070 | |
| 3071 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3072 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3073 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3074 | |
| 3075 | } else if (property != NULL) { |
| 3076 | // Check if the key is a literal string. |
| 3077 | Literal* literal = property->key()->AsLiteral(); |
| 3078 | |
| 3079 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3080 | // ------------------------------------------------------------------ |
| 3081 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3082 | // ------------------------------------------------------------------ |
| 3083 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3084 | LoadAndSpill(property->obj()); // Receiver. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3085 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3086 | int arg_count = args->length(); |
| 3087 | for (int i = 0; i < arg_count; i++) { |
| 3088 | LoadAndSpill(args->at(i)); |
| 3089 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3090 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3091 | // Set the name register and call the IC initialization code. |
| 3092 | __ mov(r2, Operand(literal->handle())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3093 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3094 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3095 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3096 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3097 | __ ldr(cp, frame_->Context()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3098 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3099 | |
| 3100 | } else { |
| 3101 | // ------------------------------------------- |
| 3102 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3103 | // ------------------------------------------- |
| 3104 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3105 | LoadAndSpill(property->obj()); |
| 3106 | LoadAndSpill(property->key()); |
| 3107 | EmitKeyedLoad(false); |
| 3108 | frame_->Drop(); // key |
| 3109 | // Put the function below the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3110 | if (property->is_synthetic()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3111 | // Use the global receiver. |
| 3112 | frame_->Drop(); |
| 3113 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3114 | LoadGlobalReceiver(r0); |
| 3115 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3116 | frame_->EmitPop(r1); // receiver |
| 3117 | frame_->EmitPush(r0); // function |
| 3118 | frame_->EmitPush(r1); // receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3121 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3122 | CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3123 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | } else { |
| 3127 | // ---------------------------------- |
| 3128 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3129 | // ---------------------------------- |
| 3130 | |
| 3131 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3132 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3133 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3134 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3135 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3136 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3137 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3138 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3139 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3140 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3141 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3145 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3146 | #ifdef DEBUG |
| 3147 | int original_height = frame_->height(); |
| 3148 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3149 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3150 | Comment cmnt(masm_, "[ CallNew"); |
| 3151 | |
| 3152 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3153 | // expression in new calls must be evaluated before the |
| 3154 | // arguments. This is different from ordinary calls, where the |
| 3155 | // actual function to call is resolved after the arguments have been |
| 3156 | // evaluated. |
| 3157 | |
| 3158 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3159 | // receiver. There is no need to use the global proxy here because |
| 3160 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3161 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3162 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3163 | |
| 3164 | // Push the arguments ("left-to-right") on the stack. |
| 3165 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3166 | int arg_count = args->length(); |
| 3167 | for (int i = 0; i < arg_count; i++) { |
| 3168 | LoadAndSpill(args->at(i)); |
| 3169 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3170 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3171 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3172 | __ mov(r0, Operand(arg_count)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3173 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3174 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3175 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3176 | // Call the construct call builtin that handles allocation and |
| 3177 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3178 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3179 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3180 | frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3181 | |
| 3182 | // 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] | 3183 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3184 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3188 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3189 | VirtualFrame::SpilledScope spilled_scope; |
| 3190 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3191 | JumpTarget leave, null, function, non_function_constructor; |
| 3192 | |
| 3193 | // Load the object into r0. |
| 3194 | LoadAndSpill(args->at(0)); |
| 3195 | frame_->EmitPop(r0); |
| 3196 | |
| 3197 | // If the object is a smi, we return null. |
| 3198 | __ tst(r0, Operand(kSmiTagMask)); |
| 3199 | null.Branch(eq); |
| 3200 | |
| 3201 | // Check that the object is a JS object but take special care of JS |
| 3202 | // functions to make sure they have 'Function' as their class. |
| 3203 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3204 | null.Branch(lt); |
| 3205 | |
| 3206 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3207 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3208 | // LAST_JS_OBJECT_TYPE. |
| 3209 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3210 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3211 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3212 | function.Branch(eq); |
| 3213 | |
| 3214 | // Check if the constructor in the map is a function. |
| 3215 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3216 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3217 | non_function_constructor.Branch(ne); |
| 3218 | |
| 3219 | // The r0 register now contains the constructor function. Grab the |
| 3220 | // instance class name from there. |
| 3221 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3222 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3223 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3224 | leave.Jump(); |
| 3225 | |
| 3226 | // Functions have class 'Function'. |
| 3227 | function.Bind(); |
| 3228 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3229 | frame_->EmitPush(r0); |
| 3230 | leave.Jump(); |
| 3231 | |
| 3232 | // Objects with a non-function constructor have class 'Object'. |
| 3233 | non_function_constructor.Bind(); |
| 3234 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3235 | frame_->EmitPush(r0); |
| 3236 | leave.Jump(); |
| 3237 | |
| 3238 | // Non-JS objects have class null. |
| 3239 | null.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3240 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3241 | frame_->EmitPush(r0); |
| 3242 | |
| 3243 | // All done. |
| 3244 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3248 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3249 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3250 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3251 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3252 | LoadAndSpill(args->at(0)); |
| 3253 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3254 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3255 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3256 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3257 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3258 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3259 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3260 | // Load the value. |
| 3261 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3262 | leave.Bind(); |
| 3263 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3267 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3268 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3269 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3270 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3271 | LoadAndSpill(args->at(0)); // Load the object. |
| 3272 | LoadAndSpill(args->at(1)); // Load the value. |
| 3273 | frame_->EmitPop(r0); // r0 contains value |
| 3274 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3275 | // if (object->IsSmi()) return object. |
| 3276 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3277 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3278 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3279 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3280 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3281 | // Store the value. |
| 3282 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3283 | // Update the write barrier. |
| 3284 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3285 | __ RecordWrite(r1, r2, r3); |
| 3286 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3287 | leave.Bind(); |
| 3288 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3292 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3293 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3294 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3295 | LoadAndSpill(args->at(0)); |
| 3296 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3297 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3298 | cc_reg_ = eq; |
| 3299 | } |
| 3300 | |
| 3301 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3302 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3303 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3304 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3305 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3306 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3307 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3308 | LoadAndSpill(args->at(1)); |
| 3309 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3310 | __ CallRuntime(Runtime::kLog, 2); |
| 3311 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3312 | #endif |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3313 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3314 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3318 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3319 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3320 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3321 | LoadAndSpill(args->at(0)); |
| 3322 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3323 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3324 | cc_reg_ = eq; |
| 3325 | } |
| 3326 | |
| 3327 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3328 | // Generates the Math.pow method - currently just calls runtime. |
| 3329 | void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) { |
| 3330 | ASSERT(args->length() == 2); |
| 3331 | Load(args->at(0)); |
| 3332 | Load(args->at(1)); |
| 3333 | frame_->CallRuntime(Runtime::kMath_pow, 2); |
| 3334 | frame_->EmitPush(r0); |
| 3335 | } |
| 3336 | |
| 3337 | |
| 3338 | // Generates the Math.sqrt method - currently just calls runtime. |
| 3339 | void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) { |
| 3340 | ASSERT(args->length() == 1); |
| 3341 | Load(args->at(0)); |
| 3342 | frame_->CallRuntime(Runtime::kMath_sqrt, 1); |
| 3343 | frame_->EmitPush(r0); |
| 3344 | } |
| 3345 | |
| 3346 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3347 | // This should generate code that performs a charCodeAt() call or returns |
| 3348 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3349 | // 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] | 3350 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3351 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3352 | ASSERT(args->length() == 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3353 | Comment(masm_, "[ GenerateFastCharCodeAt"); |
| 3354 | |
| 3355 | LoadAndSpill(args->at(0)); |
| 3356 | LoadAndSpill(args->at(1)); |
| 3357 | frame_->EmitPop(r0); // Index. |
| 3358 | frame_->EmitPop(r1); // String. |
| 3359 | |
| 3360 | Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string; |
| 3361 | |
| 3362 | __ tst(r1, Operand(kSmiTagMask)); |
| 3363 | __ b(eq, &slow); // The 'string' was a Smi. |
| 3364 | |
| 3365 | ASSERT(kSmiTag == 0); |
| 3366 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
| 3367 | __ b(ne, &slow); // The index was negative or not a Smi. |
| 3368 | |
| 3369 | __ bind(&try_again_with_new_string); |
| 3370 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 3371 | __ b(ge, &slow); |
| 3372 | |
| 3373 | // Now r2 has the string type. |
| 3374 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3375 | // Now r3 has the length of the string. Compare with the index. |
| 3376 | __ cmp(r3, Operand(r0, LSR, kSmiTagSize)); |
| 3377 | __ b(le, &slow); |
| 3378 | |
| 3379 | // Here we know the index is in range. Check that string is sequential. |
| 3380 | ASSERT_EQ(0, kSeqStringTag); |
| 3381 | __ tst(r2, Operand(kStringRepresentationMask)); |
| 3382 | __ b(ne, ¬_a_flat_string); |
| 3383 | |
| 3384 | // Check whether it is an ASCII string. |
| 3385 | ASSERT_EQ(0, kTwoByteStringTag); |
| 3386 | __ tst(r2, Operand(kStringEncodingMask)); |
| 3387 | __ b(ne, &ascii_string); |
| 3388 | |
| 3389 | // 2-byte string. We can add without shifting since the Smi tag size is the |
| 3390 | // log2 of the number of bytes in a two-byte character. |
| 3391 | ASSERT_EQ(1, kSmiTagSize); |
| 3392 | ASSERT_EQ(0, kSmiShiftSize); |
| 3393 | __ add(r1, r1, Operand(r0)); |
| 3394 | __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize)); |
| 3395 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3396 | __ jmp(&end); |
| 3397 | |
| 3398 | __ bind(&ascii_string); |
| 3399 | __ add(r1, r1, Operand(r0, LSR, kSmiTagSize)); |
| 3400 | __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 3401 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3402 | __ jmp(&end); |
| 3403 | |
| 3404 | __ bind(¬_a_flat_string); |
| 3405 | __ and_(r2, r2, Operand(kStringRepresentationMask)); |
| 3406 | __ cmp(r2, Operand(kConsStringTag)); |
| 3407 | __ b(ne, &slow); |
| 3408 | |
| 3409 | // ConsString. |
| 3410 | // Check that the right hand side is the empty string (ie if this is really a |
| 3411 | // flat string in a cons string). If that is not the case we would rather go |
| 3412 | // to the runtime system now, to flatten the string. |
| 3413 | __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset)); |
| 3414 | __ LoadRoot(r3, Heap::kEmptyStringRootIndex); |
| 3415 | __ cmp(r2, Operand(r3)); |
| 3416 | __ b(ne, &slow); |
| 3417 | |
| 3418 | // Get the first of the two strings. |
| 3419 | __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset)); |
| 3420 | __ jmp(&try_again_with_new_string); |
| 3421 | |
| 3422 | __ bind(&slow); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3423 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3424 | |
| 3425 | __ bind(&end); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3426 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3430 | void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) { |
| 3431 | Comment(masm_, "[ GenerateCharFromCode"); |
| 3432 | ASSERT(args->length() == 1); |
| 3433 | |
| 3434 | LoadAndSpill(args->at(0)); |
| 3435 | frame_->EmitPop(r0); |
| 3436 | |
| 3437 | JumpTarget slow_case; |
| 3438 | JumpTarget exit; |
| 3439 | |
| 3440 | // Fast case of Heap::LookupSingleCharacterStringFromCode. |
| 3441 | ASSERT(kSmiTag == 0); |
| 3442 | ASSERT(kSmiShiftSize == 0); |
| 3443 | ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1)); |
| 3444 | __ tst(r0, Operand(kSmiTagMask | |
| 3445 | ((~String::kMaxAsciiCharCode) << kSmiTagSize))); |
| 3446 | slow_case.Branch(nz); |
| 3447 | |
| 3448 | ASSERT(kSmiTag == 0); |
| 3449 | __ mov(r1, Operand(Factory::single_character_string_cache())); |
| 3450 | __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 3451 | __ ldr(r1, MemOperand(r1, FixedArray::kHeaderSize - kHeapObjectTag)); |
| 3452 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 3453 | __ cmp(r1, ip); |
| 3454 | slow_case.Branch(eq); |
| 3455 | |
| 3456 | frame_->EmitPush(r1); |
| 3457 | exit.Jump(); |
| 3458 | |
| 3459 | slow_case.Bind(); |
| 3460 | frame_->EmitPush(r0); |
| 3461 | frame_->CallRuntime(Runtime::kCharFromCode, 1); |
| 3462 | frame_->EmitPush(r0); |
| 3463 | |
| 3464 | exit.Bind(); |
| 3465 | } |
| 3466 | |
| 3467 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3468 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3469 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3470 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3471 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3472 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3473 | // We need the CC bits to come out as not_equal in the case where the |
| 3474 | // object is a smi. This can't be done with the usual test opcode so |
| 3475 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3476 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3477 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3478 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3479 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3480 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3481 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3482 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3483 | cc_reg_ = eq; |
| 3484 | } |
| 3485 | |
| 3486 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3487 | void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) { |
| 3488 | VirtualFrame::SpilledScope spilled_scope; |
| 3489 | ASSERT(args->length() == 1); |
| 3490 | LoadAndSpill(args->at(0)); |
| 3491 | JumpTarget answer; |
| 3492 | // We need the CC bits to come out as not_equal in the case where the |
| 3493 | // object is a smi. This can't be done with the usual test opcode so |
| 3494 | // we use XOR to get the right CC bits. |
| 3495 | frame_->EmitPop(r0); |
| 3496 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3497 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
| 3498 | answer.Branch(ne); |
| 3499 | // It is a heap object - get the map. Check if the object is a regexp. |
| 3500 | __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE); |
| 3501 | answer.Bind(); |
| 3502 | cc_reg_ = eq; |
| 3503 | } |
| 3504 | |
| 3505 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3506 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 3507 | // This generates a fast version of: |
| 3508 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
| 3509 | VirtualFrame::SpilledScope spilled_scope; |
| 3510 | ASSERT(args->length() == 1); |
| 3511 | LoadAndSpill(args->at(0)); |
| 3512 | frame_->EmitPop(r1); |
| 3513 | __ tst(r1, Operand(kSmiTagMask)); |
| 3514 | false_target()->Branch(eq); |
| 3515 | |
| 3516 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 3517 | __ cmp(r1, ip); |
| 3518 | true_target()->Branch(eq); |
| 3519 | |
| 3520 | Register map_reg = r2; |
| 3521 | __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3522 | // Undetectable objects behave like undefined when tested with typeof. |
| 3523 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 3524 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3525 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 3526 | false_target()->Branch(eq); |
| 3527 | |
| 3528 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 3529 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3530 | false_target()->Branch(lt); |
| 3531 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
| 3532 | cc_reg_ = le; |
| 3533 | } |
| 3534 | |
| 3535 | |
| 3536 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 3537 | // This generates a fast version of: |
| 3538 | // (%_ClassOf(arg) === 'Function') |
| 3539 | VirtualFrame::SpilledScope spilled_scope; |
| 3540 | ASSERT(args->length() == 1); |
| 3541 | LoadAndSpill(args->at(0)); |
| 3542 | frame_->EmitPop(r0); |
| 3543 | __ tst(r0, Operand(kSmiTagMask)); |
| 3544 | false_target()->Branch(eq); |
| 3545 | Register map_reg = r2; |
| 3546 | __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE); |
| 3547 | cc_reg_ = eq; |
| 3548 | } |
| 3549 | |
| 3550 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3551 | void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) { |
| 3552 | VirtualFrame::SpilledScope spilled_scope; |
| 3553 | ASSERT(args->length() == 1); |
| 3554 | LoadAndSpill(args->at(0)); |
| 3555 | frame_->EmitPop(r0); |
| 3556 | __ tst(r0, Operand(kSmiTagMask)); |
| 3557 | false_target()->Branch(eq); |
| 3558 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3559 | __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3560 | __ tst(r1, Operand(1 << Map::kIsUndetectable)); |
| 3561 | cc_reg_ = ne; |
| 3562 | } |
| 3563 | |
| 3564 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3565 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3566 | VirtualFrame::SpilledScope spilled_scope; |
| 3567 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3568 | |
| 3569 | // Get the frame pointer for the calling frame. |
| 3570 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3571 | |
| 3572 | // Skip the arguments adaptor frame if it exists. |
| 3573 | Label check_frame_marker; |
| 3574 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3575 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3576 | __ b(ne, &check_frame_marker); |
| 3577 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3578 | |
| 3579 | // Check the marker in the calling frame. |
| 3580 | __ bind(&check_frame_marker); |
| 3581 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3582 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3583 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3587 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3588 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3589 | ASSERT(args->length() == 0); |
| 3590 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3591 | // Seed the result with the formal parameters count, which will be used |
| 3592 | // 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] | 3593 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3594 | |
| 3595 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3596 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3597 | frame_->CallStub(&stub, 0); |
| 3598 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3602 | void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3603 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3604 | ASSERT(args->length() == 1); |
| 3605 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3606 | // Satisfy contract with ArgumentsAccessStub: |
| 3607 | // 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] | 3608 | LoadAndSpill(args->at(0)); |
| 3609 | frame_->EmitPop(r1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3610 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3611 | |
| 3612 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3613 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3614 | frame_->CallStub(&stub, 0); |
| 3615 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
| 3618 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3619 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3620 | VirtualFrame::SpilledScope spilled_scope; |
| 3621 | ASSERT(args->length() == 0); |
| 3622 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3623 | RelocInfo::RUNTIME_ENTRY); |
| 3624 | frame_->EmitPush(r0); |
| 3625 | } |
| 3626 | |
| 3627 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3628 | void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) { |
| 3629 | ASSERT_EQ(2, args->length()); |
| 3630 | |
| 3631 | Load(args->at(0)); |
| 3632 | Load(args->at(1)); |
| 3633 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3634 | StringAddStub stub(NO_STRING_ADD_FLAGS); |
| 3635 | frame_->CallStub(&stub, 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3636 | frame_->EmitPush(r0); |
| 3637 | } |
| 3638 | |
| 3639 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3640 | void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) { |
| 3641 | ASSERT_EQ(3, args->length()); |
| 3642 | |
| 3643 | Load(args->at(0)); |
| 3644 | Load(args->at(1)); |
| 3645 | Load(args->at(2)); |
| 3646 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3647 | SubStringStub stub; |
| 3648 | frame_->CallStub(&stub, 3); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3649 | frame_->EmitPush(r0); |
| 3650 | } |
| 3651 | |
| 3652 | |
| 3653 | void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) { |
| 3654 | ASSERT_EQ(2, args->length()); |
| 3655 | |
| 3656 | Load(args->at(0)); |
| 3657 | Load(args->at(1)); |
| 3658 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3659 | StringCompareStub stub; |
| 3660 | frame_->CallStub(&stub, 2); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3661 | frame_->EmitPush(r0); |
| 3662 | } |
| 3663 | |
| 3664 | |
| 3665 | void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) { |
| 3666 | ASSERT_EQ(4, args->length()); |
| 3667 | |
| 3668 | Load(args->at(0)); |
| 3669 | Load(args->at(1)); |
| 3670 | Load(args->at(2)); |
| 3671 | Load(args->at(3)); |
| 3672 | |
| 3673 | frame_->CallRuntime(Runtime::kRegExpExec, 4); |
| 3674 | frame_->EmitPush(r0); |
| 3675 | } |
| 3676 | |
| 3677 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3678 | void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) { |
| 3679 | ASSERT_EQ(args->length(), 1); |
| 3680 | |
| 3681 | // Load the argument on the stack and jump to the runtime. |
| 3682 | Load(args->at(0)); |
| 3683 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame^] | 3684 | NumberToStringStub stub; |
| 3685 | frame_->CallStub(&stub, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3686 | frame_->EmitPush(r0); |
| 3687 | } |
| 3688 | |
| 3689 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3690 | void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) { |
| 3691 | ASSERT_EQ(args->length(), 1); |
| 3692 | // Load the argument on the stack and jump to the runtime. |
| 3693 | Load(args->at(0)); |
| 3694 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 3695 | frame_->EmitPush(r0); |
| 3696 | } |
| 3697 | |
| 3698 | |
| 3699 | void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) { |
| 3700 | ASSERT_EQ(args->length(), 1); |
| 3701 | // Load the argument on the stack and jump to the runtime. |
| 3702 | Load(args->at(0)); |
| 3703 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 3704 | frame_->EmitPush(r0); |
| 3705 | } |
| 3706 | |
| 3707 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3708 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3709 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3710 | ASSERT(args->length() == 2); |
| 3711 | |
| 3712 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3713 | LoadAndSpill(args->at(0)); |
| 3714 | LoadAndSpill(args->at(1)); |
| 3715 | frame_->EmitPop(r0); |
| 3716 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3717 | __ cmp(r0, Operand(r1)); |
| 3718 | cc_reg_ = eq; |
| 3719 | } |
| 3720 | |
| 3721 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3722 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3723 | #ifdef DEBUG |
| 3724 | int original_height = frame_->height(); |
| 3725 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3726 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3727 | if (CheckForInlineRuntimeCall(node)) { |
| 3728 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3729 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3730 | return; |
| 3731 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3732 | |
| 3733 | ZoneList<Expression*>* args = node->arguments(); |
| 3734 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3735 | Runtime::Function* function = node->function(); |
| 3736 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3737 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3738 | // Prepare stack for calling JS runtime function. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3739 | // Push the builtins object found in the current global object. |
| 3740 | __ ldr(r1, GlobalObject()); |
| 3741 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3742 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3743 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3744 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3745 | // Push the arguments ("left-to-right"). |
| 3746 | int arg_count = args->length(); |
| 3747 | for (int i = 0; i < arg_count; i++) { |
| 3748 | LoadAndSpill(args->at(i)); |
| 3749 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3750 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3751 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3752 | // Call the JS runtime function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3753 | __ mov(r2, Operand(node->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3754 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3755 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3756 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3757 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3758 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3759 | } else { |
| 3760 | // Call the C runtime function. |
| 3761 | frame_->CallRuntime(function, arg_count); |
| 3762 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3763 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3764 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3768 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3769 | #ifdef DEBUG |
| 3770 | int original_height = frame_->height(); |
| 3771 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3772 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3773 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3774 | |
| 3775 | Token::Value op = node->op(); |
| 3776 | |
| 3777 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3778 | LoadConditionAndSpill(node->expression(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3779 | false_target(), |
| 3780 | true_target(), |
| 3781 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3782 | // LoadCondition may (and usually does) leave a test and branch to |
| 3783 | // be emitted by the caller. In that case, negate the condition. |
| 3784 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3785 | |
| 3786 | } else if (op == Token::DELETE) { |
| 3787 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3788 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3789 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3790 | LoadAndSpill(property->obj()); |
| 3791 | LoadAndSpill(property->key()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3792 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3793 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3794 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3795 | Slot* slot = variable->slot(); |
| 3796 | if (variable->is_global()) { |
| 3797 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3798 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3799 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3800 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3801 | |
| 3802 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3803 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3804 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3805 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3806 | frame_->EmitPush(r0); |
| 3807 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3808 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3809 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3810 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3811 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3812 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3813 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3814 | } else { |
| 3815 | // Default: Result of deleting non-global, not dynamically |
| 3816 | // introduced variables is false. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3817 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3818 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3819 | |
| 3820 | } else { |
| 3821 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3822 | LoadAndSpill(node->expression()); // may have side-effects |
| 3823 | frame_->Drop(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3824 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3825 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3826 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3827 | |
| 3828 | } else if (op == Token::TYPEOF) { |
| 3829 | // Special case for loading the typeof expression; see comment on |
| 3830 | // LoadTypeofExpression(). |
| 3831 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3832 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3833 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3834 | |
| 3835 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3836 | bool overwrite = |
| 3837 | (node->expression()->AsBinaryOperation() != NULL && |
| 3838 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3839 | LoadAndSpill(node->expression()); |
| 3840 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3841 | switch (op) { |
| 3842 | case Token::NOT: |
| 3843 | case Token::DELETE: |
| 3844 | case Token::TYPEOF: |
| 3845 | UNREACHABLE(); // handled above |
| 3846 | break; |
| 3847 | |
| 3848 | case Token::SUB: { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3849 | GenericUnaryOpStub stub(Token::SUB, overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3850 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3851 | break; |
| 3852 | } |
| 3853 | |
| 3854 | case Token::BIT_NOT: { |
| 3855 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3856 | JumpTarget smi_label; |
| 3857 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3858 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3859 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3860 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3861 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 3862 | frame_->CallStub(&stub, 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3863 | continue_label.Jump(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3864 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3865 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3866 | __ mvn(r0, Operand(r0)); |
| 3867 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3868 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3869 | break; |
| 3870 | } |
| 3871 | |
| 3872 | case Token::VOID: |
| 3873 | // since the stack top is cached in r0, popping and then |
| 3874 | // pushing a value can be done by just writing to r0. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3875 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3876 | break; |
| 3877 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3878 | case Token::ADD: { |
| 3879 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3880 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3881 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3882 | continue_label.Branch(eq); |
| 3883 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3884 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3885 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3886 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3887 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3888 | default: |
| 3889 | UNREACHABLE(); |
| 3890 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3891 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3892 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3893 | ASSERT(!has_valid_frame() || |
| 3894 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3895 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
| 3898 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3899 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3900 | #ifdef DEBUG |
| 3901 | int original_height = frame_->height(); |
| 3902 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3903 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3904 | Comment cmnt(masm_, "[ CountOperation"); |
| 3905 | |
| 3906 | bool is_postfix = node->is_postfix(); |
| 3907 | bool is_increment = node->op() == Token::INC; |
| 3908 | |
| 3909 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3910 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3911 | |
| 3912 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3913 | if (is_postfix) { |
| 3914 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3915 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3916 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3917 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3918 | // A constant reference is not saved to, so a constant reference is not a |
| 3919 | // compound assignment reference. |
| 3920 | { Reference target(this, node->expression(), !is_const); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3921 | if (target.is_illegal()) { |
| 3922 | // Spoof the virtual frame to have the expected height (one higher |
| 3923 | // than on entry). |
| 3924 | if (!is_postfix) { |
| 3925 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3926 | frame_->EmitPush(r0); |
| 3927 | } |
| 3928 | ASSERT(frame_->height() == original_height + 1); |
| 3929 | return; |
| 3930 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3931 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3932 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3933 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3934 | JumpTarget slow; |
| 3935 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3936 | |
| 3937 | // Load the value (1) into register r1. |
| 3938 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3939 | |
| 3940 | // Check for smi operand. |
| 3941 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3942 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3943 | |
| 3944 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3945 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3946 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3947 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3948 | |
| 3949 | // Perform optimistic increment/decrement. |
| 3950 | if (is_increment) { |
| 3951 | __ add(r0, r0, Operand(r1), SetCC); |
| 3952 | } else { |
| 3953 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3954 | } |
| 3955 | |
| 3956 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3957 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3958 | |
| 3959 | // Revert optimistic increment/decrement. |
| 3960 | if (is_increment) { |
| 3961 | __ sub(r0, r0, Operand(r1)); |
| 3962 | } else { |
| 3963 | __ add(r0, r0, Operand(r1)); |
| 3964 | } |
| 3965 | |
| 3966 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3967 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3968 | { |
| 3969 | // Convert the operand to a number. |
| 3970 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3971 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3972 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3973 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3974 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3975 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3978 | // Compute the new value. |
| 3979 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3980 | frame_->EmitPush(r0); |
| 3981 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3982 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3983 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3984 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3985 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3989 | exit.Bind(); |
| 3990 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3991 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3995 | if (is_postfix) frame_->EmitPop(r0); |
| 3996 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4000 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4001 | #ifdef DEBUG |
| 4002 | int original_height = frame_->height(); |
| 4003 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4004 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4005 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 4006 | Token::Value op = node->op(); |
| 4007 | |
| 4008 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 4009 | // operators must yield the result of one of the two expressions |
| 4010 | // before any ToBoolean() conversions. This means that the value |
| 4011 | // produced by a && or || operator is not necessarily a boolean. |
| 4012 | |
| 4013 | // NOTE: If the left hand side produces a materialized value (not in |
| 4014 | // the CC register), we force the right hand side to do the |
| 4015 | // same. This is necessary because we may have to branch to the exit |
| 4016 | // after evaluating the left hand side (due to the shortcut |
| 4017 | // semantics), but the compiler must (statically) know if the result |
| 4018 | // of compiling the binary operation is materialized or not. |
| 4019 | |
| 4020 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4021 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4022 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4023 | &is_true, |
| 4024 | false_target(), |
| 4025 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4026 | if (has_valid_frame() && !has_cc()) { |
| 4027 | // 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] | 4028 | JumpTarget pop_and_continue; |
| 4029 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4030 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4031 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4032 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4033 | // Avoid popping the result if it converts to 'false' using the |
| 4034 | // standard ToBoolean() conversion as described in ECMA-262, |
| 4035 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4036 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4037 | Branch(false, &exit); |
| 4038 | |
| 4039 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4040 | pop_and_continue.Bind(); |
| 4041 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4042 | |
| 4043 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4044 | is_true.Bind(); |
| 4045 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4046 | |
| 4047 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4048 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4049 | } else if (has_cc() || is_true.is_linked()) { |
| 4050 | // The left-hand side is either (a) partially compiled to |
| 4051 | // control flow with a final branch left to emit or (b) fully |
| 4052 | // compiled to control flow and possibly true. |
| 4053 | if (has_cc()) { |
| 4054 | Branch(false, false_target()); |
| 4055 | } |
| 4056 | is_true.Bind(); |
| 4057 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4058 | true_target(), |
| 4059 | false_target(), |
| 4060 | false); |
| 4061 | } else { |
| 4062 | // Nothing to do. |
| 4063 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4067 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4068 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4069 | true_target(), |
| 4070 | &is_false, |
| 4071 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4072 | if (has_valid_frame() && !has_cc()) { |
| 4073 | // 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] | 4074 | JumpTarget pop_and_continue; |
| 4075 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4076 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4077 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4078 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4079 | // Avoid popping the result if it converts to 'true' using the |
| 4080 | // standard ToBoolean() conversion as described in ECMA-262, |
| 4081 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4082 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4083 | Branch(true, &exit); |
| 4084 | |
| 4085 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4086 | pop_and_continue.Bind(); |
| 4087 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4088 | |
| 4089 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4090 | is_false.Bind(); |
| 4091 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4092 | |
| 4093 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4094 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4095 | } else if (has_cc() || is_false.is_linked()) { |
| 4096 | // The left-hand side is either (a) partially compiled to |
| 4097 | // control flow with a final branch left to emit or (b) fully |
| 4098 | // compiled to control flow and possibly false. |
| 4099 | if (has_cc()) { |
| 4100 | Branch(true, true_target()); |
| 4101 | } |
| 4102 | is_false.Bind(); |
| 4103 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4104 | true_target(), |
| 4105 | false_target(), |
| 4106 | false); |
| 4107 | } else { |
| 4108 | // Nothing to do. |
| 4109 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | } else { |
| 4113 | // Optimize for the case where (at least) one of the expressions |
| 4114 | // is a literal small integer. |
| 4115 | Literal* lliteral = node->left()->AsLiteral(); |
| 4116 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4117 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 4118 | // never return a constant/immutable object. |
| 4119 | bool overwrite_left = |
| 4120 | (node->left()->AsBinaryOperation() != NULL && |
| 4121 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 4122 | bool overwrite_right = |
| 4123 | (node->right()->AsBinaryOperation() != NULL && |
| 4124 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4125 | |
| 4126 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4127 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4128 | SmiOperation(node->op(), |
| 4129 | rliteral->handle(), |
| 4130 | false, |
| 4131 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4132 | |
| 4133 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4134 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4135 | SmiOperation(node->op(), |
| 4136 | lliteral->handle(), |
| 4137 | true, |
| 4138 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4139 | |
| 4140 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4141 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 4142 | if (overwrite_left) { |
| 4143 | overwrite_mode = OVERWRITE_LEFT; |
| 4144 | } else if (overwrite_right) { |
| 4145 | overwrite_mode = OVERWRITE_RIGHT; |
| 4146 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4147 | LoadAndSpill(node->left()); |
| 4148 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4149 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4150 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4151 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4152 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4153 | ASSERT(!has_valid_frame() || |
| 4154 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4155 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4159 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4160 | #ifdef DEBUG |
| 4161 | int original_height = frame_->height(); |
| 4162 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4163 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4164 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4165 | frame_->EmitPush(r0); |
| 4166 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4170 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4171 | #ifdef DEBUG |
| 4172 | int original_height = frame_->height(); |
| 4173 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4174 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4175 | Comment cmnt(masm_, "[ CompareOperation"); |
| 4176 | |
| 4177 | // Get the expressions from the node. |
| 4178 | Expression* left = node->left(); |
| 4179 | Expression* right = node->right(); |
| 4180 | Token::Value op = node->op(); |
| 4181 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4182 | // To make null checks efficient, we check if either left or right is the |
| 4183 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 4184 | // instead of calling the (very) general runtime routine for checking |
| 4185 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4186 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4187 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4188 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4189 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4190 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4191 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4192 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4193 | LoadAndSpill(left_is_null ? right : left); |
| 4194 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4195 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4196 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4197 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4198 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4199 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4200 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4201 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4202 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4203 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4204 | __ cmp(r0, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4205 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4206 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4207 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4208 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4209 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4210 | // It can be an undetectable object. |
| 4211 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4212 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 4213 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 4214 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4217 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4218 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4219 | return; |
| 4220 | } |
| 4221 | } |
| 4222 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4223 | // To make typeof testing for natives implemented in JavaScript really |
| 4224 | // efficient, we generate special code for expressions of the form: |
| 4225 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4226 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 4227 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4228 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4229 | (right->AsLiteral() != NULL && |
| 4230 | right->AsLiteral()->handle()->IsString())) { |
| 4231 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4232 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4233 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4234 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4235 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4236 | |
| 4237 | if (check->Equals(Heap::number_symbol())) { |
| 4238 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4239 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4240 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4241 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 4242 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4243 | cc_reg_ = eq; |
| 4244 | |
| 4245 | } else if (check->Equals(Heap::string_symbol())) { |
| 4246 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4247 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4248 | |
| 4249 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4250 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4251 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4252 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4253 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4254 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4255 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4256 | |
| 4257 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 4258 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 4259 | cc_reg_ = lt; |
| 4260 | |
| 4261 | } else if (check->Equals(Heap::boolean_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4262 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 4263 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4264 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4265 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 4266 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4267 | cc_reg_ = eq; |
| 4268 | |
| 4269 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4270 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4271 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4272 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4273 | |
| 4274 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4275 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4276 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4277 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4278 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4279 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4280 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4281 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4282 | |
| 4283 | cc_reg_ = eq; |
| 4284 | |
| 4285 | } else if (check->Equals(Heap::function_symbol())) { |
| 4286 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4287 | false_target()->Branch(eq); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4288 | Register map_reg = r2; |
| 4289 | __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE); |
| 4290 | true_target()->Branch(eq); |
| 4291 | // Regular expressions are callable so typeof == 'function'. |
| 4292 | __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4293 | cc_reg_ = eq; |
| 4294 | |
| 4295 | } else if (check->Equals(Heap::object_symbol())) { |
| 4296 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4297 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4298 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4299 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4300 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4301 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4302 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4303 | Register map_reg = r2; |
| 4304 | __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE); |
| 4305 | false_target()->Branch(eq); |
| 4306 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4307 | // It can be an undetectable object. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4308 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4309 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4310 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4311 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4312 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4313 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4314 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4315 | false_target()->Branch(lt); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4316 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4317 | cc_reg_ = le; |
| 4318 | |
| 4319 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4320 | // Uncommon case: typeof testing against a string literal that is |
| 4321 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4322 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4323 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4324 | ASSERT(!has_valid_frame() || |
| 4325 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4326 | return; |
| 4327 | } |
| 4328 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4329 | switch (op) { |
| 4330 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4331 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4332 | break; |
| 4333 | |
| 4334 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4335 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4336 | break; |
| 4337 | |
| 4338 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4339 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4340 | break; |
| 4341 | |
| 4342 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4343 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4344 | break; |
| 4345 | |
| 4346 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4347 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4348 | break; |
| 4349 | |
| 4350 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4351 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4352 | break; |
| 4353 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4354 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4355 | LoadAndSpill(left); |
| 4356 | LoadAndSpill(right); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4357 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4358 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4359 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4360 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4361 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4362 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4363 | LoadAndSpill(left); |
| 4364 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4365 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4366 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4367 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4368 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4369 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4370 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4371 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4372 | |
| 4373 | default: |
| 4374 | UNREACHABLE(); |
| 4375 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4376 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4377 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4381 | void CodeGenerator::EmitKeyedLoad(bool is_global) { |
| 4382 | Comment cmnt(masm_, "[ Load from keyed Property"); |
| 4383 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 4384 | RelocInfo::Mode rmode = is_global |
| 4385 | ? RelocInfo::CODE_TARGET_CONTEXT |
| 4386 | : RelocInfo::CODE_TARGET; |
| 4387 | frame_->CallCodeObject(ic, rmode, 0); |
| 4388 | } |
| 4389 | |
| 4390 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4391 | #ifdef DEBUG |
| 4392 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4393 | #endif |
| 4394 | |
| 4395 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4396 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4397 | #define __ ACCESS_MASM(masm) |
| 4398 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4399 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4400 | Handle<String> Reference::GetName() { |
| 4401 | ASSERT(type_ == NAMED); |
| 4402 | Property* property = expression_->AsProperty(); |
| 4403 | if (property == NULL) { |
| 4404 | // Global variable reference treated as a named property reference. |
| 4405 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4406 | ASSERT(proxy->AsVariable() != NULL); |
| 4407 | ASSERT(proxy->AsVariable()->is_global()); |
| 4408 | return proxy->name(); |
| 4409 | } else { |
| 4410 | Literal* raw_name = property->key()->AsLiteral(); |
| 4411 | ASSERT(raw_name != NULL); |
| 4412 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4413 | } |
| 4414 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4415 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4416 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4417 | void Reference::GetValue() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4418 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4419 | ASSERT(!is_illegal()); |
| 4420 | ASSERT(!cgen_->has_cc()); |
| 4421 | MacroAssembler* masm = cgen_->masm(); |
| 4422 | Property* property = expression_->AsProperty(); |
| 4423 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4424 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | switch (type_) { |
| 4428 | case SLOT: { |
| 4429 | Comment cmnt(masm, "[ Load from Slot"); |
| 4430 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4431 | ASSERT(slot != NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4432 | cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4433 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4436 | case NAMED: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4437 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4438 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4439 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4440 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4441 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4442 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4443 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4444 | ASSERT(var == NULL || var->is_global()); |
| 4445 | RelocInfo::Mode rmode = (var == NULL) |
| 4446 | ? RelocInfo::CODE_TARGET |
| 4447 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4448 | frame->CallCodeObject(ic, rmode, 0); |
| 4449 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4450 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4453 | case KEYED: { |
| 4454 | // TODO(181): Implement inlined version of array indexing once |
| 4455 | // loop nesting is properly tracked on ARM. |
| 4456 | ASSERT(property != NULL); |
| 4457 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 4458 | ASSERT(var == NULL || var->is_global()); |
| 4459 | cgen_->EmitKeyedLoad(var != NULL); |
| 4460 | cgen_->frame()->EmitPush(r0); |
| 4461 | break; |
| 4462 | } |
| 4463 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4464 | default: |
| 4465 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4466 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4467 | |
| 4468 | if (!persist_after_get_) { |
| 4469 | cgen_->UnloadReference(this); |
| 4470 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4474 | void Reference::SetValue(InitState init_state) { |
| 4475 | ASSERT(!is_illegal()); |
| 4476 | ASSERT(!cgen_->has_cc()); |
| 4477 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4478 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4479 | Property* property = expression_->AsProperty(); |
| 4480 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4481 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4482 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4483 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4484 | switch (type_) { |
| 4485 | case SLOT: { |
| 4486 | Comment cmnt(masm, "[ Store to Slot"); |
| 4487 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4488 | cgen_->StoreToSlot(slot, init_state); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4489 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4490 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4493 | case NAMED: { |
| 4494 | Comment cmnt(masm, "[ Store to named Property"); |
| 4495 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4496 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4497 | Handle<String> name(GetName()); |
| 4498 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4499 | frame->EmitPop(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4500 | frame->EmitPop(r1); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4501 | __ mov(r2, Operand(name)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4502 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4503 | frame->EmitPush(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4504 | set_unloaded(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4505 | break; |
| 4506 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4507 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4508 | case KEYED: { |
| 4509 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4510 | Property* property = expression_->AsProperty(); |
| 4511 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4512 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4513 | |
| 4514 | // Call IC code. |
| 4515 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4516 | frame->EmitPop(r0); // value |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4517 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4518 | frame->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4519 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4520 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4521 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4522 | |
| 4523 | default: |
| 4524 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4525 | } |
| 4526 | } |
| 4527 | |
| 4528 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4529 | void FastNewClosureStub::Generate(MacroAssembler* masm) { |
| 4530 | // Clone the boilerplate in new space. Set the context to the |
| 4531 | // current context in cp. |
| 4532 | Label gc; |
| 4533 | |
| 4534 | // Pop the boilerplate function from the stack. |
| 4535 | __ pop(r3); |
| 4536 | |
| 4537 | // Attempt to allocate new JSFunction in new space. |
| 4538 | __ AllocateInNewSpace(JSFunction::kSize / kPointerSize, |
| 4539 | r0, |
| 4540 | r1, |
| 4541 | r2, |
| 4542 | &gc, |
| 4543 | TAG_OBJECT); |
| 4544 | |
| 4545 | // Compute the function map in the current global context and set that |
| 4546 | // as the map of the allocated object. |
| 4547 | __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4548 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 4549 | __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX))); |
| 4550 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4551 | |
| 4552 | // Clone the rest of the boilerplate fields. We don't have to update |
| 4553 | // the write barrier because the allocated object is in new space. |
| 4554 | for (int offset = kPointerSize; |
| 4555 | offset < JSFunction::kSize; |
| 4556 | offset += kPointerSize) { |
| 4557 | if (offset == JSFunction::kContextOffset) { |
| 4558 | __ str(cp, FieldMemOperand(r0, offset)); |
| 4559 | } else { |
| 4560 | __ ldr(r1, FieldMemOperand(r3, offset)); |
| 4561 | __ str(r1, FieldMemOperand(r0, offset)); |
| 4562 | } |
| 4563 | } |
| 4564 | |
| 4565 | // Return result. The argument boilerplate has been popped already. |
| 4566 | __ Ret(); |
| 4567 | |
| 4568 | // Create a new closure through the slower runtime call. |
| 4569 | __ bind(&gc); |
| 4570 | __ push(cp); |
| 4571 | __ push(r3); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4572 | __ TailCallRuntime(Runtime::kNewClosure, 2, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | |
| 4576 | void FastNewContextStub::Generate(MacroAssembler* masm) { |
| 4577 | // Try to allocate the context in new space. |
| 4578 | Label gc; |
| 4579 | int length = slots_ + Context::MIN_CONTEXT_SLOTS; |
| 4580 | |
| 4581 | // Attempt to allocate the context in new space. |
| 4582 | __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize), |
| 4583 | r0, |
| 4584 | r1, |
| 4585 | r2, |
| 4586 | &gc, |
| 4587 | TAG_OBJECT); |
| 4588 | |
| 4589 | // Load the function from the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4590 | __ ldr(r3, MemOperand(sp, 0)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4591 | |
| 4592 | // Setup the object header. |
| 4593 | __ LoadRoot(r2, Heap::kContextMapRootIndex); |
| 4594 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4595 | __ mov(r2, Operand(length)); |
| 4596 | __ str(r2, FieldMemOperand(r0, Array::kLengthOffset)); |
| 4597 | |
| 4598 | // Setup the fixed slots. |
| 4599 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 4600 | __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 4601 | __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 4602 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
| 4603 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX))); |
| 4604 | |
| 4605 | // Copy the global object from the surrounding context. |
| 4606 | __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4607 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4608 | |
| 4609 | // Initialize the rest of the slots to undefined. |
| 4610 | __ LoadRoot(r1, Heap::kUndefinedValueRootIndex); |
| 4611 | for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) { |
| 4612 | __ str(r1, MemOperand(r0, Context::SlotOffset(i))); |
| 4613 | } |
| 4614 | |
| 4615 | // Remove the on-stack argument and return. |
| 4616 | __ mov(cp, r0); |
| 4617 | __ pop(); |
| 4618 | __ Ret(); |
| 4619 | |
| 4620 | // Need to collect. Call into runtime system. |
| 4621 | __ bind(&gc); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4622 | __ TailCallRuntime(Runtime::kNewContext, 1, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4626 | void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) { |
| 4627 | // Stack layout on entry: |
| 4628 | // |
| 4629 | // [sp]: constant elements. |
| 4630 | // [sp + kPointerSize]: literal index. |
| 4631 | // [sp + (2 * kPointerSize)]: literals array. |
| 4632 | |
| 4633 | // All sizes here are multiples of kPointerSize. |
| 4634 | int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0; |
| 4635 | int size = JSArray::kSize + elements_size; |
| 4636 | |
| 4637 | // Load boilerplate object into r3 and check if we need to create a |
| 4638 | // boilerplate. |
| 4639 | Label slow_case; |
| 4640 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 4641 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 4642 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 4643 | __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4644 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4645 | __ cmp(r3, ip); |
| 4646 | __ b(eq, &slow_case); |
| 4647 | |
| 4648 | // Allocate both the JS array and the elements array in one big |
| 4649 | // allocation. This avoids multiple limit checks. |
| 4650 | __ AllocateInNewSpace(size / kPointerSize, |
| 4651 | r0, |
| 4652 | r1, |
| 4653 | r2, |
| 4654 | &slow_case, |
| 4655 | TAG_OBJECT); |
| 4656 | |
| 4657 | // Copy the JS array part. |
| 4658 | for (int i = 0; i < JSArray::kSize; i += kPointerSize) { |
| 4659 | if ((i != JSArray::kElementsOffset) || (length_ == 0)) { |
| 4660 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 4661 | __ str(r1, FieldMemOperand(r0, i)); |
| 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | if (length_ > 0) { |
| 4666 | // Get hold of the elements array of the boilerplate and setup the |
| 4667 | // elements pointer in the resulting object. |
| 4668 | __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset)); |
| 4669 | __ add(r2, r0, Operand(JSArray::kSize)); |
| 4670 | __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 4671 | |
| 4672 | // Copy the elements array. |
| 4673 | for (int i = 0; i < elements_size; i += kPointerSize) { |
| 4674 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 4675 | __ str(r1, FieldMemOperand(r2, i)); |
| 4676 | } |
| 4677 | } |
| 4678 | |
| 4679 | // Return and remove the on-stack parameters. |
| 4680 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 4681 | __ Ret(); |
| 4682 | |
| 4683 | __ bind(&slow_case); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4684 | __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4688 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4689 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4690 | // (31 instead of 32). |
| 4691 | static void CountLeadingZeros( |
| 4692 | MacroAssembler* masm, |
| 4693 | Register source, |
| 4694 | Register scratch, |
| 4695 | Register zeros) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4696 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4697 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4698 | #else |
| 4699 | __ mov(zeros, Operand(0)); |
| 4700 | __ mov(scratch, source); |
| 4701 | // Top 16. |
| 4702 | __ tst(scratch, Operand(0xffff0000)); |
| 4703 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4704 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4705 | // Top 8. |
| 4706 | __ tst(scratch, Operand(0xff000000)); |
| 4707 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4708 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4709 | // Top 4. |
| 4710 | __ tst(scratch, Operand(0xf0000000)); |
| 4711 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4712 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4713 | // Top 2. |
| 4714 | __ tst(scratch, Operand(0xc0000000)); |
| 4715 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4716 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4717 | // Top bit. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4718 | __ tst(scratch, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4719 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4720 | #endif |
| 4721 | } |
| 4722 | |
| 4723 | |
| 4724 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4725 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4726 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4727 | // scratch register. Destroys the source register. No GC occurs during this |
| 4728 | // stub so you don't have to set up the frame. |
| 4729 | class ConvertToDoubleStub : public CodeStub { |
| 4730 | public: |
| 4731 | ConvertToDoubleStub(Register result_reg_1, |
| 4732 | Register result_reg_2, |
| 4733 | Register source_reg, |
| 4734 | Register scratch_reg) |
| 4735 | : result1_(result_reg_1), |
| 4736 | result2_(result_reg_2), |
| 4737 | source_(source_reg), |
| 4738 | zeros_(scratch_reg) { } |
| 4739 | |
| 4740 | private: |
| 4741 | Register result1_; |
| 4742 | Register result2_; |
| 4743 | Register source_; |
| 4744 | Register zeros_; |
| 4745 | |
| 4746 | // Minor key encoding in 16 bits. |
| 4747 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4748 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4749 | |
| 4750 | Major MajorKey() { return ConvertToDouble; } |
| 4751 | int MinorKey() { |
| 4752 | // Encode the parameters in a unique 16 bit value. |
| 4753 | return result1_.code() + |
| 4754 | (result2_.code() << 4) + |
| 4755 | (source_.code() << 8) + |
| 4756 | (zeros_.code() << 12); |
| 4757 | } |
| 4758 | |
| 4759 | void Generate(MacroAssembler* masm); |
| 4760 | |
| 4761 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4762 | |
| 4763 | #ifdef DEBUG |
| 4764 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4765 | #endif |
| 4766 | }; |
| 4767 | |
| 4768 | |
| 4769 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4770 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4771 | Register exponent = result1_; |
| 4772 | Register mantissa = result2_; |
| 4773 | #else |
| 4774 | Register exponent = result2_; |
| 4775 | Register mantissa = result1_; |
| 4776 | #endif |
| 4777 | Label not_special; |
| 4778 | // Convert from Smi to integer. |
| 4779 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4780 | // Move sign bit from source to destination. This works because the sign bit |
| 4781 | // in the exponent word of the double has the same position and polarity as |
| 4782 | // the 2's complement sign bit in a Smi. |
| 4783 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4784 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4785 | // Subtract from 0 if source was negative. |
| 4786 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4787 | __ cmp(source_, Operand(1)); |
| 4788 | __ b(gt, ¬_special); |
| 4789 | |
| 4790 | // We have -1, 0 or 1, which we treat specially. |
| 4791 | __ cmp(source_, Operand(0)); |
| 4792 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4793 | static const uint32_t exponent_word_for_1 = |
| 4794 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4795 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4796 | // 1, 0 and -1 all have 0 for the second word. |
| 4797 | __ mov(mantissa, Operand(0)); |
| 4798 | __ Ret(); |
| 4799 | |
| 4800 | __ bind(¬_special); |
| 4801 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4802 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4803 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4804 | // Compute exponent and or it into the exponent register. |
| 4805 | // We use result2 as a scratch register here. |
| 4806 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4807 | __ orr(exponent, |
| 4808 | exponent, |
| 4809 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4810 | // Shift up the source chopping the top bit off. |
| 4811 | __ add(zeros_, zeros_, Operand(1)); |
| 4812 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4813 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4814 | // Compute lower part of fraction (last 12 bits). |
| 4815 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4816 | // And the top (top 20 bits). |
| 4817 | __ orr(exponent, |
| 4818 | exponent, |
| 4819 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4820 | __ Ret(); |
| 4821 | } |
| 4822 | |
| 4823 | |
| 4824 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4825 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4826 | // so you don't have to set up the frame. |
| 4827 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4828 | public: |
| 4829 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4830 | Register the_heap_number, |
| 4831 | Register scratch) |
| 4832 | : the_int_(the_int), |
| 4833 | the_heap_number_(the_heap_number), |
| 4834 | scratch_(scratch) { } |
| 4835 | |
| 4836 | private: |
| 4837 | Register the_int_; |
| 4838 | Register the_heap_number_; |
| 4839 | Register scratch_; |
| 4840 | |
| 4841 | // Minor key encoding in 16 bits. |
| 4842 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4843 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4844 | |
| 4845 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4846 | int MinorKey() { |
| 4847 | // Encode the parameters in a unique 16 bit value. |
| 4848 | return the_int_.code() + |
| 4849 | (the_heap_number_.code() << 4) + |
| 4850 | (scratch_.code() << 8); |
| 4851 | } |
| 4852 | |
| 4853 | void Generate(MacroAssembler* masm); |
| 4854 | |
| 4855 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4856 | |
| 4857 | #ifdef DEBUG |
| 4858 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4859 | #endif |
| 4860 | }; |
| 4861 | |
| 4862 | |
| 4863 | // See comment for class. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4864 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4865 | Label max_negative_int; |
| 4866 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4867 | // We test for the special value that has a different exponent. This test |
| 4868 | // has the neat side effect of setting the flags according to the sign. |
| 4869 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4870 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4871 | __ b(eq, &max_negative_int); |
| 4872 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4873 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4874 | uint32_t non_smi_exponent = |
| 4875 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4876 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4877 | // Set the sign bit in scratch_ if the value was negative. |
| 4878 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4879 | // Subtract from 0 if the value was negative. |
| 4880 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4881 | // We should be masking the implict first digit of the mantissa away here, |
| 4882 | // but it just ends up combining harmlessly with the last digit of the |
| 4883 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4884 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4885 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4886 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4887 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4888 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4889 | HeapNumber::kExponentOffset)); |
| 4890 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4891 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4892 | HeapNumber::kMantissaOffset)); |
| 4893 | __ Ret(); |
| 4894 | |
| 4895 | __ bind(&max_negative_int); |
| 4896 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4897 | // a double because it uses a sign bit instead of using two's complement. |
| 4898 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4899 | // significant 1 bit is not stored. |
| 4900 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4901 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4902 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4903 | __ mov(ip, Operand(0)); |
| 4904 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4905 | __ Ret(); |
| 4906 | } |
| 4907 | |
| 4908 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4909 | // Handle the case where the lhs and rhs are the same object. |
| 4910 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4911 | // for "identity and not NaN". |
| 4912 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4913 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4914 | Condition cc, |
| 4915 | bool never_nan_nan) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4916 | Label not_identical; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4917 | Label heap_number, return_equal; |
| 4918 | Register exp_mask_reg = r5; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4919 | __ cmp(r0, Operand(r1)); |
| 4920 | __ b(ne, ¬_identical); |
| 4921 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4922 | // The two objects are identical. If we know that one of them isn't NaN then |
| 4923 | // we now know they test equal. |
| 4924 | if (cc != eq || !never_nan_nan) { |
| 4925 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4926 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4927 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4928 | // so we do the second best thing - test it ourselves. |
| 4929 | // They are both equal and they are not both Smis so both of them are not |
| 4930 | // Smis. If it's not a heap number, then return equal. |
| 4931 | if (cc == lt || cc == gt) { |
| 4932 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4933 | __ b(ge, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4934 | } else { |
| 4935 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4936 | __ b(eq, &heap_number); |
| 4937 | // Comparing JS objects with <=, >= is complicated. |
| 4938 | if (cc != eq) { |
| 4939 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4940 | __ b(ge, slow); |
| 4941 | // Normally here we fall through to return_equal, but undefined is |
| 4942 | // special: (undefined == undefined) == true, but |
| 4943 | // (undefined <= undefined) == false! See ECMAScript 11.8.5. |
| 4944 | if (cc == le || cc == ge) { |
| 4945 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 4946 | __ b(ne, &return_equal); |
| 4947 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 4948 | __ cmp(r0, Operand(r2)); |
| 4949 | __ b(ne, &return_equal); |
| 4950 | if (cc == le) { |
| 4951 | // undefined <= undefined should fail. |
| 4952 | __ mov(r0, Operand(GREATER)); |
| 4953 | } else { |
| 4954 | // undefined >= undefined should fail. |
| 4955 | __ mov(r0, Operand(LESS)); |
| 4956 | } |
| 4957 | __ mov(pc, Operand(lr)); // Return. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4958 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4959 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4960 | } |
| 4961 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4962 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4963 | __ bind(&return_equal); |
| 4964 | if (cc == lt) { |
| 4965 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4966 | } else if (cc == gt) { |
| 4967 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4968 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4969 | __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4970 | } |
| 4971 | __ mov(pc, Operand(lr)); // Return. |
| 4972 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4973 | if (cc != eq || !never_nan_nan) { |
| 4974 | // For less and greater we don't have to check for NaN since the result of |
| 4975 | // x < x is false regardless. For the others here is some code to check |
| 4976 | // for NaN. |
| 4977 | if (cc != lt && cc != gt) { |
| 4978 | __ bind(&heap_number); |
| 4979 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4980 | // not NaN. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4981 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4982 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4983 | // and not all mantissa bits (0..51) clear. |
| 4984 | // Read top bits of double representation (second word of value). |
| 4985 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4986 | // Test that exponent bits are all set. |
| 4987 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4988 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4989 | __ b(ne, &return_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4990 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4991 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4992 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4993 | // Or with all low-bits of mantissa. |
| 4994 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4995 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4996 | // 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] | 4997 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if |
| 4998 | // not (it's a NaN). For <= and >= we need to load r0 with the failing |
| 4999 | // value if it's a NaN. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5000 | if (cc != eq) { |
| 5001 | // All-zero means Infinity means equal. |
| 5002 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 5003 | if (cc == le) { |
| 5004 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 5005 | } else { |
| 5006 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 5007 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5008 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5009 | __ mov(pc, Operand(lr)); // Return. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5010 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5011 | // No fall through here. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5012 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5013 | |
| 5014 | __ bind(¬_identical); |
| 5015 | } |
| 5016 | |
| 5017 | |
| 5018 | // See comment at call site. |
| 5019 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5020 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5021 | Label* slow, |
| 5022 | bool strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5023 | Label rhs_is_smi; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5024 | __ tst(r0, Operand(kSmiTagMask)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5025 | __ b(eq, &rhs_is_smi); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5026 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5027 | // 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] | 5028 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5029 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5030 | // 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] | 5031 | // succeed. Return non-equal (r0 is already not zero) |
| 5032 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 5033 | } else { |
| 5034 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 5035 | // the runtime. |
| 5036 | __ b(ne, slow); |
| 5037 | } |
| 5038 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5039 | // Lhs (r1) is a smi, rhs (r0) is a number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5040 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5041 | // Convert lhs to a double in d7 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5042 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5043 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5044 | __ vmov(s15, r7); |
| 5045 | __ vcvt(d7, s15); |
| 5046 | // Load the double from rhs, tagged HeapNumber r0, to d6. |
| 5047 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5048 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5049 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5050 | __ push(lr); |
| 5051 | // Convert lhs to a double in r2, r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5052 | __ mov(r7, Operand(r1)); |
| 5053 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5054 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5055 | // Load rhs to a double in r0, r1. |
| 5056 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5057 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5058 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5061 | // 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] | 5062 | // since it's a smi. |
| 5063 | __ jmp(lhs_not_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5064 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5065 | __ bind(&rhs_is_smi); |
| 5066 | // 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] | 5067 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5068 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5069 | // 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] | 5070 | // succeed. Return non-equal. |
| 5071 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 5072 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 5073 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5074 | // 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] | 5075 | // the runtime. |
| 5076 | __ b(ne, slow); |
| 5077 | } |
| 5078 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5079 | // Rhs (r0) is a smi, lhs (r1) is a heap number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5080 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5081 | // Convert rhs to a double in d6 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5082 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5083 | // Load the double from lhs, tagged HeapNumber r1, to d7. |
| 5084 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5085 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5086 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5087 | __ vmov(s13, r7); |
| 5088 | __ vcvt(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5089 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5090 | __ push(lr); |
| 5091 | // Load lhs to a double in r2, r3. |
| 5092 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5093 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5094 | // Convert rhs to a double in r0, r1. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5095 | __ mov(r7, Operand(r0)); |
| 5096 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5097 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5098 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5099 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5100 | // Fall through to both_loaded_as_doubles. |
| 5101 | } |
| 5102 | |
| 5103 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5104 | void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5105 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5106 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5107 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5108 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5109 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5110 | Label one_is_nan, neither_is_nan; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5111 | Label lhs_not_nan_exp_mask_is_loaded; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5112 | |
| 5113 | Register exp_mask_reg = r5; |
| 5114 | |
| 5115 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5116 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 5117 | __ cmp(r4, Operand(exp_mask_reg)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5118 | __ b(ne, &lhs_not_nan_exp_mask_is_loaded); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5119 | __ mov(r4, |
| 5120 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5121 | SetCC); |
| 5122 | __ b(ne, &one_is_nan); |
| 5123 | __ cmp(lhs_mantissa, Operand(0)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5124 | __ b(ne, &one_is_nan); |
| 5125 | |
| 5126 | __ bind(lhs_not_nan); |
| 5127 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 5128 | __ bind(&lhs_not_nan_exp_mask_is_loaded); |
| 5129 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 5130 | __ cmp(r4, Operand(exp_mask_reg)); |
| 5131 | __ b(ne, &neither_is_nan); |
| 5132 | __ mov(r4, |
| 5133 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5134 | SetCC); |
| 5135 | __ b(ne, &one_is_nan); |
| 5136 | __ cmp(rhs_mantissa, Operand(0)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5137 | __ b(eq, &neither_is_nan); |
| 5138 | |
| 5139 | __ bind(&one_is_nan); |
| 5140 | // NaN comparisons always fail. |
| 5141 | // Load whatever we need in r0 to make the comparison fail. |
| 5142 | if (cc == lt || cc == le) { |
| 5143 | __ mov(r0, Operand(GREATER)); |
| 5144 | } else { |
| 5145 | __ mov(r0, Operand(LESS)); |
| 5146 | } |
| 5147 | __ mov(pc, Operand(lr)); // Return. |
| 5148 | |
| 5149 | __ bind(&neither_is_nan); |
| 5150 | } |
| 5151 | |
| 5152 | |
| 5153 | // See comment at call site. |
| 5154 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 5155 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5156 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5157 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5158 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5159 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5160 | |
| 5161 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 5162 | if (cc == eq) { |
| 5163 | // Doubles are not equal unless they have the same bit pattern. |
| 5164 | // Exception: 0 and -0. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5165 | __ cmp(rhs_mantissa, Operand(lhs_mantissa)); |
| 5166 | __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5167 | // Return non-zero if the numbers are unequal. |
| 5168 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 5169 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5170 | __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5171 | // If exponents are equal then return 0. |
| 5172 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 5173 | |
| 5174 | // Exponents are unequal. The only way we can return that the numbers |
| 5175 | // are equal is if one is -0 and the other is 0. We already dealt |
| 5176 | // with the case where both are -0 or both are 0. |
| 5177 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 5178 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 5179 | // equal. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5180 | __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5181 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 5182 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 5183 | // Now they are equal if and only if the lhs exponent is zero in its |
| 5184 | // low 31 bits. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5185 | __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5186 | __ mov(pc, Operand(lr)); |
| 5187 | } else { |
| 5188 | // Call a native function to do a comparison between two non-NaNs. |
| 5189 | // Call C routine that may not cause GC or other trouble. |
| 5190 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 5191 | __ Jump(r5); // Tail call. |
| 5192 | } |
| 5193 | } |
| 5194 | |
| 5195 | |
| 5196 | // See comment at call site. |
| 5197 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 5198 | // If either operand is a JSObject or an oddball value, then they are |
| 5199 | // not equal since their pointers are different. |
| 5200 | // There is no test for undetectability in strict equality. |
| 5201 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 5202 | Label first_non_object; |
| 5203 | // Get the type of the first operand into r2 and compare it with |
| 5204 | // FIRST_JS_OBJECT_TYPE. |
| 5205 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 5206 | __ b(lt, &first_non_object); |
| 5207 | |
| 5208 | // Return non-zero (r0 is not zero) |
| 5209 | Label return_not_equal; |
| 5210 | __ bind(&return_not_equal); |
| 5211 | __ mov(pc, Operand(lr)); // Return. |
| 5212 | |
| 5213 | __ bind(&first_non_object); |
| 5214 | // Check for oddballs: true, false, null, undefined. |
| 5215 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 5216 | __ b(eq, &return_not_equal); |
| 5217 | |
| 5218 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 5219 | __ b(ge, &return_not_equal); |
| 5220 | |
| 5221 | // Check for oddballs: true, false, null, undefined. |
| 5222 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 5223 | __ b(eq, &return_not_equal); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5224 | |
| 5225 | // Now that we have the types we might as well check for symbol-symbol. |
| 5226 | // Ensure that no non-strings have the symbol bit set. |
| 5227 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5228 | ASSERT(kSymbolTag != 0); |
| 5229 | __ and_(r2, r2, Operand(r3)); |
| 5230 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5231 | __ b(ne, &return_not_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
| 5234 | |
| 5235 | // See comment at call site. |
| 5236 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 5237 | Label* both_loaded_as_doubles, |
| 5238 | Label* not_heap_numbers, |
| 5239 | Label* slow) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5240 | __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5241 | __ b(ne, not_heap_numbers); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5242 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5243 | __ cmp(r2, r3); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5244 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 5245 | |
| 5246 | // Both are heap numbers. Load them up then jump to the code we have |
| 5247 | // for that. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5248 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5249 | CpuFeatures::Scope scope(VFP3); |
| 5250 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5251 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5252 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5253 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5254 | } else { |
| 5255 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5256 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5257 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5258 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5259 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5260 | __ jmp(both_loaded_as_doubles); |
| 5261 | } |
| 5262 | |
| 5263 | |
| 5264 | // Fast negative check for symbol-to-symbol equality. |
| 5265 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 5266 | // r2 is object type of r0. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5267 | // Ensure that no non-strings have the symbol bit set. |
| 5268 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5269 | ASSERT(kSymbolTag != 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5270 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5271 | __ b(eq, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5272 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5273 | __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5274 | __ tst(r3, Operand(kIsSymbolMask)); |
| 5275 | __ b(eq, slow); |
| 5276 | |
| 5277 | // Both are symbols. We already checked they weren't the same pointer |
| 5278 | // so they are not equal. |
| 5279 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 5280 | __ mov(pc, Operand(lr)); // Return. |
| 5281 | } |
| 5282 | |
| 5283 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame^] | 5284 | void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm, |
| 5285 | Register object, |
| 5286 | Register result, |
| 5287 | Register scratch1, |
| 5288 | Register scratch2, |
| 5289 | bool object_is_smi, |
| 5290 | Label* not_found) { |
| 5291 | // Currently only lookup for smis. Check for smi if object is not known to be |
| 5292 | // a smi. |
| 5293 | if (!object_is_smi) { |
| 5294 | ASSERT(kSmiTag == 0); |
| 5295 | __ tst(object, Operand(kSmiTagMask)); |
| 5296 | __ b(ne, not_found); |
| 5297 | } |
| 5298 | |
| 5299 | // Use of registers. Register result is used as a temporary. |
| 5300 | Register number_string_cache = result; |
| 5301 | Register mask = scratch1; |
| 5302 | Register scratch = scratch2; |
| 5303 | |
| 5304 | // Load the number string cache. |
| 5305 | __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex); |
| 5306 | |
| 5307 | // Make the hash mask from the length of the number string cache. It |
| 5308 | // contains two elements (number and string) for each cache entry. |
| 5309 | __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset)); |
| 5310 | // Divide length by two (length is not a smi). |
| 5311 | __ mov(mask, Operand(mask, ASR, 1)); |
| 5312 | __ sub(mask, mask, Operand(1)); // Make mask. |
| 5313 | |
| 5314 | // Calculate the entry in the number string cache. The hash value in the |
| 5315 | // number string cache for smis is just the smi value. |
| 5316 | __ and_(scratch, mask, Operand(object, ASR, 1)); |
| 5317 | |
| 5318 | // Calculate address of entry in string cache: each entry consists |
| 5319 | // of two pointer sized fields. |
| 5320 | __ add(scratch, |
| 5321 | number_string_cache, |
| 5322 | Operand(scratch, LSL, kPointerSizeLog2 + 1)); |
| 5323 | |
| 5324 | // Check if the entry is the smi we are looking for. |
| 5325 | Register object1 = scratch1; |
| 5326 | __ ldr(object1, FieldMemOperand(scratch, FixedArray::kHeaderSize)); |
| 5327 | __ cmp(object, object1); |
| 5328 | __ b(ne, not_found); |
| 5329 | |
| 5330 | // Get the result from the cache. |
| 5331 | __ ldr(result, |
| 5332 | FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize)); |
| 5333 | |
| 5334 | __ IncrementCounter(&Counters::number_to_string_native, |
| 5335 | 1, |
| 5336 | scratch1, |
| 5337 | scratch2); |
| 5338 | } |
| 5339 | |
| 5340 | |
| 5341 | void NumberToStringStub::Generate(MacroAssembler* masm) { |
| 5342 | Label runtime; |
| 5343 | |
| 5344 | __ ldr(r1, MemOperand(sp, 0)); |
| 5345 | |
| 5346 | // Generate code to lookup number in the number string cache. |
| 5347 | GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, false, &runtime); |
| 5348 | __ add(sp, sp, Operand(1 * kPointerSize)); |
| 5349 | __ Ret(); |
| 5350 | |
| 5351 | __ bind(&runtime); |
| 5352 | // Handle number to string in the runtime system if not found in the cache. |
| 5353 | __ TailCallRuntime(Runtime::kNumberToString, 1, 1); |
| 5354 | } |
| 5355 | |
| 5356 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5357 | // On entry r0 (rhs) and r1 (lhs) are the values to be compared. |
| 5358 | // On exit r0 is 0, positive or negative to indicate the result of |
| 5359 | // the comparison. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5360 | void CompareStub::Generate(MacroAssembler* masm) { |
| 5361 | Label slow; // Call builtin. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5362 | Label not_smis, both_loaded_as_doubles, lhs_not_nan; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5363 | |
| 5364 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 5365 | // it is certain that at least one operand isn't a smi. |
| 5366 | |
| 5367 | // Handle the case where the objects are identical. Either returns the answer |
| 5368 | // 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] | 5369 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5370 | |
| 5371 | // If either is a Smi (we know that not both are), then they can only |
| 5372 | // be strictly equal if the other is a HeapNumber. |
| 5373 | ASSERT_EQ(0, kSmiTag); |
| 5374 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 5375 | __ and_(r2, r0, Operand(r1)); |
| 5376 | __ tst(r2, Operand(kSmiTagMask)); |
| 5377 | __ b(ne, ¬_smis); |
| 5378 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 5379 | // 1) Return the answer. |
| 5380 | // 2) Go to slow. |
| 5381 | // 3) Fall through to both_loaded_as_doubles. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5382 | // 4) Jump to lhs_not_nan. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5383 | // 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] | 5384 | // comparison. If VFP3 is supported the double values of the numbers have |
| 5385 | // been loaded into d7 and d6. Otherwise, the double values have been loaded |
| 5386 | // into r0, r1, r2, and r3. |
| 5387 | EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5388 | |
| 5389 | __ bind(&both_loaded_as_doubles); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5390 | // The arguments have been converted to doubles and stored in d6 and d7, if |
| 5391 | // VFP3 is supported, or in r0, r1, r2, and r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5392 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5393 | __ bind(&lhs_not_nan); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5394 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5395 | Label no_nan; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5396 | // ARMv7 VFP3 instructions to implement double precision comparison. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5397 | __ vcmp(d7, d6); |
| 5398 | __ vmrs(pc); // Move vector status bits to normal status bits. |
| 5399 | Label nan; |
| 5400 | __ b(vs, &nan); |
| 5401 | __ mov(r0, Operand(EQUAL), LeaveCC, eq); |
| 5402 | __ mov(r0, Operand(LESS), LeaveCC, lt); |
| 5403 | __ mov(r0, Operand(GREATER), LeaveCC, gt); |
| 5404 | __ mov(pc, Operand(lr)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5405 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5406 | __ bind(&nan); |
| 5407 | // If one of the sides was a NaN then the v flag is set. Load r0 with |
| 5408 | // whatever it takes to make the comparison fail, since comparisons with NaN |
| 5409 | // always fail. |
| 5410 | if (cc_ == lt || cc_ == le) { |
| 5411 | __ mov(r0, Operand(GREATER)); |
| 5412 | } else { |
| 5413 | __ mov(r0, Operand(LESS)); |
| 5414 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5415 | __ mov(pc, Operand(lr)); |
| 5416 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5417 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 5418 | // fall through if neither is a NaN. Also binds lhs_not_nan. |
| 5419 | EmitNanCheck(masm, &lhs_not_nan, cc_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5420 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 5421 | // answer. Never falls through. |
| 5422 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 5423 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5424 | |
| 5425 | __ bind(¬_smis); |
| 5426 | // At this point we know we are dealing with two different objects, |
| 5427 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 5428 | if (strict_) { |
| 5429 | // This returns non-equal for some object types, or falls through if it |
| 5430 | // was not lucky. |
| 5431 | EmitStrictTwoHeapObjectCompare(masm); |
| 5432 | } |
| 5433 | |
| 5434 | Label check_for_symbols; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5435 | Label flat_string_check; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5436 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 5437 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 5438 | // 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] | 5439 | // 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] | 5440 | EmitCheckForTwoHeapNumbers(masm, |
| 5441 | &both_loaded_as_doubles, |
| 5442 | &check_for_symbols, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5443 | &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5444 | |
| 5445 | __ bind(&check_for_symbols); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5446 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 5447 | // symbols. |
| 5448 | if (cc_ == eq && !strict_) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5449 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5450 | // of r0 on entry. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5451 | EmitCheckForSymbols(masm, &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5454 | // Check for both being sequential ASCII strings, and inline if that is the |
| 5455 | // case. |
| 5456 | __ bind(&flat_string_check); |
| 5457 | |
| 5458 | __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow); |
| 5459 | |
| 5460 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 5461 | StringCompareStub::GenerateCompareFlatAsciiStrings(masm, |
| 5462 | r1, |
| 5463 | r0, |
| 5464 | r2, |
| 5465 | r3, |
| 5466 | r4, |
| 5467 | r5); |
| 5468 | // Never falls through to here. |
| 5469 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5470 | __ bind(&slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5471 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5472 | __ push(r1); |
| 5473 | __ push(r0); |
| 5474 | // Figure out which native to call and setup the arguments. |
| 5475 | Builtins::JavaScript native; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5476 | if (cc_ == eq) { |
| 5477 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 5478 | } else { |
| 5479 | native = Builtins::COMPARE; |
| 5480 | int ncr; // NaN compare result |
| 5481 | if (cc_ == lt || cc_ == le) { |
| 5482 | ncr = GREATER; |
| 5483 | } else { |
| 5484 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 5485 | ncr = LESS; |
| 5486 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5487 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 5488 | __ push(r0); |
| 5489 | } |
| 5490 | |
| 5491 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 5492 | // tagged as a small integer. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5493 | __ InvokeBuiltin(native, JUMP_JS); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5497 | // Allocates a heap number or jumps to the label if the young space is full and |
| 5498 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5499 | static void AllocateHeapNumber( |
| 5500 | MacroAssembler* masm, |
| 5501 | Label* need_gc, // Jump here if young space is full. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5502 | Register result, // The tagged address of the new heap number. |
| 5503 | Register scratch1, // A scratch register. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5504 | Register scratch2) { // Another scratch register. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5505 | // Allocate an object in the heap for the heap number and tag it as a heap |
| 5506 | // object. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5507 | __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize, |
| 5508 | result, |
| 5509 | scratch1, |
| 5510 | scratch2, |
| 5511 | need_gc, |
| 5512 | TAG_OBJECT); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5513 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5514 | // Get heap number map and store it in the allocated object. |
| 5515 | __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex); |
| 5516 | __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | |
| 5520 | // We fall into this code if the operands were Smis, but the result was |
| 5521 | // 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] | 5522 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 5523 | // to call the C-implemented binary fp operation routines we need to end up |
| 5524 | // with the double precision floating point operands in r0 and r1 (for the |
| 5525 | // 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] | 5526 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 5527 | Label* not_smi, |
| 5528 | const Builtins::JavaScript& builtin, |
| 5529 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5530 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5531 | Label slow, slow_pop_2_first, do_the_call; |
| 5532 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 5533 | // Smi-smi case (overflow). |
| 5534 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 5535 | // The new heap number is in r5. r6 and r7 are scratch. |
| 5536 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
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 we have floating point hardware, inline ADD, SUB, MUL, and DIV, |
| 5539 | // using registers d7 and d6 for the double values. |
| 5540 | bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && |
| 5541 | Token::MOD != operation; |
| 5542 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5543 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5544 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5545 | __ vmov(s15, r7); |
| 5546 | __ vcvt(d7, s15); |
| 5547 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5548 | __ vmov(s13, r7); |
| 5549 | __ vcvt(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5550 | } else { |
| 5551 | // Write Smi from r0 to r3 and r2 in double format. r6 is scratch. |
| 5552 | __ mov(r7, Operand(r0)); |
| 5553 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5554 | __ push(lr); |
| 5555 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 5556 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 5557 | __ mov(r7, Operand(r1)); |
| 5558 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5559 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 5560 | __ pop(lr); |
| 5561 | } |
| 5562 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5563 | __ jmp(&do_the_call); // Tail call. No return. |
| 5564 | |
| 5565 | // We jump to here if something goes wrong (one param is not a number of any |
| 5566 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5567 | __ bind(&slow); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5568 | |
| 5569 | // Push arguments to the stack |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5570 | __ push(r1); |
| 5571 | __ push(r0); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5572 | |
| 5573 | if (Token::ADD == operation) { |
| 5574 | // Test for string arguments before calling runtime. |
| 5575 | // r1 : first argument |
| 5576 | // r0 : second argument |
| 5577 | // sp[0] : second argument |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5578 | // sp[4] : first argument |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5579 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame^] | 5580 | Label not_strings, not_string1, string1, string1_smi2; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5581 | __ tst(r1, Operand(kSmiTagMask)); |
| 5582 | __ b(eq, ¬_string1); |
| 5583 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 5584 | __ b(ge, ¬_string1); |
| 5585 | |
| 5586 | // First argument is a a string, test second. |
| 5587 | __ tst(r0, Operand(kSmiTagMask)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame^] | 5588 | __ b(eq, &string1_smi2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5589 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5590 | __ b(ge, &string1); |
| 5591 | |
| 5592 | // First and second argument are strings. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame^] | 5593 | StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB); |
| 5594 | __ TailCallStub(&string_add_stub); |
| 5595 | |
| 5596 | __ bind(&string1_smi2); |
| 5597 | // First argument is a string, second is a smi. Try to lookup the number |
| 5598 | // string for the smi in the number string cache. |
| 5599 | NumberToStringStub::GenerateLookupNumberStringCache( |
| 5600 | masm, r0, r2, r4, r5, true, &string1); |
| 5601 | |
| 5602 | // Replace second argument on stack and tailcall string add stub to make |
| 5603 | // the result. |
| 5604 | __ str(r2, MemOperand(sp, 0)); |
| 5605 | __ TailCallStub(&string_add_stub); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5606 | |
| 5607 | // Only first argument is a string. |
| 5608 | __ bind(&string1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5609 | __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS); |
| 5610 | |
| 5611 | // First argument was not a string, test second. |
| 5612 | __ bind(¬_string1); |
| 5613 | __ tst(r0, Operand(kSmiTagMask)); |
| 5614 | __ b(eq, ¬_strings); |
| 5615 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5616 | __ b(ge, ¬_strings); |
| 5617 | |
| 5618 | // Only second argument is a string. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5619 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 5620 | |
| 5621 | __ bind(¬_strings); |
| 5622 | } |
| 5623 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5624 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5625 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5626 | // 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] | 5627 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5628 | if (mode == NO_OVERWRITE) { |
| 5629 | // In the case where there is no chance of an overwritable float we may as |
| 5630 | // well do the allocation immediately while r0 and r1 are untouched. |
| 5631 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5632 | } |
| 5633 | |
| 5634 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5635 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5636 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5637 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5638 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5639 | if (mode == OVERWRITE_RIGHT) { |
| 5640 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5641 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5642 | if (use_fp_registers) { |
| 5643 | CpuFeatures::Scope scope(VFP3); |
| 5644 | // Load the double from tagged HeapNumber r0 to d7. |
| 5645 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5646 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5647 | } else { |
| 5648 | // Calling convention says that second double is in r2 and r3. |
| 5649 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5650 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5651 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5652 | __ jmp(&finished_loading_r0); |
| 5653 | __ bind(&r0_is_smi); |
| 5654 | if (mode == OVERWRITE_RIGHT) { |
| 5655 | // 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] | 5656 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5657 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5658 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5659 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5660 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5661 | // Convert smi in r0 to double in d7. |
| 5662 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5663 | __ vmov(s15, r7); |
| 5664 | __ vcvt(d7, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5665 | } else { |
| 5666 | // Write Smi from r0 to r3 and r2 in double format. |
| 5667 | __ mov(r7, Operand(r0)); |
| 5668 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5669 | __ push(lr); |
| 5670 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5671 | __ pop(lr); |
| 5672 | } |
| 5673 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5674 | __ bind(&finished_loading_r0); |
| 5675 | |
| 5676 | // Move r1 to a double in r0-r1. |
| 5677 | __ tst(r1, Operand(kSmiTagMask)); |
| 5678 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5679 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5680 | __ b(ne, &slow); |
| 5681 | if (mode == OVERWRITE_LEFT) { |
| 5682 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5683 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5684 | if (use_fp_registers) { |
| 5685 | CpuFeatures::Scope scope(VFP3); |
| 5686 | // Load the double from tagged HeapNumber r1 to d6. |
| 5687 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5688 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5689 | } else { |
| 5690 | // Calling convention says that first double is in r0 and r1. |
| 5691 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5692 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5693 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5694 | __ jmp(&finished_loading_r1); |
| 5695 | __ bind(&r1_is_smi); |
| 5696 | if (mode == OVERWRITE_LEFT) { |
| 5697 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5698 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5699 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5700 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5701 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5702 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5703 | // Convert smi in r1 to double in d6. |
| 5704 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5705 | __ vmov(s13, r7); |
| 5706 | __ vcvt(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5707 | } else { |
| 5708 | // Write Smi from r1 to r1 and r0 in double format. |
| 5709 | __ mov(r7, Operand(r1)); |
| 5710 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5711 | __ push(lr); |
| 5712 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5713 | __ pop(lr); |
| 5714 | } |
| 5715 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5716 | __ bind(&finished_loading_r1); |
| 5717 | |
| 5718 | __ bind(&do_the_call); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5719 | // If we are inlining the operation using VFP3 instructions for |
| 5720 | // add, subtract, multiply, or divide, the arguments are in d6 and d7. |
| 5721 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5722 | CpuFeatures::Scope scope(VFP3); |
| 5723 | // ARMv7 VFP3 instructions to implement |
| 5724 | // double precision, add, subtract, multiply, divide. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5725 | |
| 5726 | if (Token::MUL == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5727 | __ vmul(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5728 | } else if (Token::DIV == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5729 | __ vdiv(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5730 | } else if (Token::ADD == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5731 | __ vadd(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5732 | } else if (Token::SUB == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5733 | __ vsub(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5734 | } else { |
| 5735 | UNREACHABLE(); |
| 5736 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5737 | __ sub(r0, r5, Operand(kHeapObjectTag)); |
| 5738 | __ vstr(d5, r0, HeapNumber::kValueOffset); |
| 5739 | __ add(r0, r0, Operand(kHeapObjectTag)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5740 | __ mov(pc, lr); |
| 5741 | return; |
| 5742 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5743 | |
| 5744 | // If we did not inline the operation, then the arguments are in: |
| 5745 | // r0: Left value (least significant part of mantissa). |
| 5746 | // r1: Left value (sign, exponent, top of mantissa). |
| 5747 | // r2: Right value (least significant part of mantissa). |
| 5748 | // r3: Right value (sign, exponent, top of mantissa). |
| 5749 | // r5: Address of heap number for result. |
| 5750 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5751 | __ push(lr); // For later. |
| 5752 | __ push(r5); // Address of heap number that is answer. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5753 | __ AlignStack(0); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5754 | // Call C routine that may not cause GC or other trouble. |
| 5755 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5756 | __ Call(r5); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5757 | __ pop(r4); // Address of heap number. |
| 5758 | __ cmp(r4, Operand(Smi::FromInt(0))); |
| 5759 | __ 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] | 5760 | // Store answer in the overwritable heap number. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5761 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5762 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5763 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5764 | // substract the tag from r4. |
| 5765 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5766 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5767 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5768 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5769 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5770 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5771 | #endif |
| 5772 | __ mov(r0, Operand(r4)); |
| 5773 | // And we are done. |
| 5774 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5775 | } |
| 5776 | |
| 5777 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5778 | // 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] | 5779 | // 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] | 5780 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5781 | // 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] | 5782 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5783 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5784 | static void GetInt32(MacroAssembler* masm, |
| 5785 | Register source, |
| 5786 | Register dest, |
| 5787 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5788 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5789 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5790 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5791 | // Get exponent word. |
| 5792 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5793 | // Get exponent alone in scratch2. |
| 5794 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5795 | // Load dest with zero. We use this either for the final shift or |
| 5796 | // for the answer. |
| 5797 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5798 | // 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] | 5799 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5800 | // the exponent that we are fastest at and also the highest exponent we can |
| 5801 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5802 | const uint32_t non_smi_exponent = |
| 5803 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5804 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5805 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5806 | __ b(eq, &right_exponent); |
| 5807 | // If the exponent is higher than that then go to slow case. This catches |
| 5808 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5809 | __ b(gt, slow); |
| 5810 | |
| 5811 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5812 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5813 | // it rounds to zero. |
| 5814 | const uint32_t zero_exponent = |
| 5815 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5816 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5817 | // Dest already has a Smi zero. |
| 5818 | __ b(lt, &done); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5819 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 5820 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5821 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5822 | // We now have the exponent in dest. Subtract from 30 to get |
| 5823 | // how much to shift down. |
| 5824 | __ rsb(dest, dest, Operand(30)); |
| 5825 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5826 | __ bind(&right_exponent); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5827 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5828 | CpuFeatures::Scope scope(VFP3); |
| 5829 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 5830 | // conversion using round to zero. |
| 5831 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5832 | __ vmov(d7, scratch2, scratch); |
| 5833 | __ vcvt(s15, d7); |
| 5834 | __ vmov(dest, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5835 | } else { |
| 5836 | // Get the top bits of the mantissa. |
| 5837 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5838 | // Put back the implicit 1. |
| 5839 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5840 | // Shift up the mantissa bits to take up the space the exponent used to |
| 5841 | // take. We just orred in the implicit bit so that took care of one and |
| 5842 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 5843 | // distance. |
| 5844 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5845 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5846 | // Put sign in zero flag. |
| 5847 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 5848 | // Get the second half of the double. For some exponents we don't |
| 5849 | // actually need this because the bits get shifted out again, but |
| 5850 | // it's probably slower to test than just to do it. |
| 5851 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5852 | // Shift down 22 bits to get the last 10 bits. |
| 5853 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5854 | // Move down according to the exponent. |
| 5855 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 5856 | // Fix sign if sign bit was set. |
| 5857 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 5858 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5859 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5860 | } |
| 5861 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5862 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5863 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5864 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5865 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5866 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5867 | // write the answer into (either by allocating or by overwriting). |
| 5868 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5869 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5870 | Label slow, result_not_a_smi; |
| 5871 | Label r0_is_smi, r1_is_smi; |
| 5872 | Label done_checking_r0, done_checking_r1; |
| 5873 | |
| 5874 | __ tst(r1, Operand(kSmiTagMask)); |
| 5875 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5876 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5877 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5878 | GetInt32(masm, r1, r3, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5879 | __ jmp(&done_checking_r1); |
| 5880 | __ bind(&r1_is_smi); |
| 5881 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5882 | __ bind(&done_checking_r1); |
| 5883 | |
| 5884 | __ tst(r0, Operand(kSmiTagMask)); |
| 5885 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5886 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5887 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5888 | GetInt32(masm, r0, r2, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5889 | __ jmp(&done_checking_r0); |
| 5890 | __ bind(&r0_is_smi); |
| 5891 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5892 | __ bind(&done_checking_r0); |
| 5893 | |
| 5894 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5895 | // r2 and r3: Signed int32 operands. |
| 5896 | switch (op_) { |
| 5897 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5898 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5899 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5900 | case Token::SAR: |
| 5901 | // Use only the 5 least significant bits of the shift count. |
| 5902 | __ and_(r2, r2, Operand(0x1f)); |
| 5903 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5904 | break; |
| 5905 | case Token::SHR: |
| 5906 | // Use only the 5 least significant bits of the shift count. |
| 5907 | __ and_(r2, r2, Operand(0x1f)); |
| 5908 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5909 | // SHR is special because it is required to produce a positive answer. |
| 5910 | // The code below for writing into heap numbers isn't capable of writing |
| 5911 | // the register as an unsigned int so we go to slow case if we hit this |
| 5912 | // case. |
| 5913 | __ b(mi, &slow); |
| 5914 | break; |
| 5915 | case Token::SHL: |
| 5916 | // Use only the 5 least significant bits of the shift count. |
| 5917 | __ and_(r2, r2, Operand(0x1f)); |
| 5918 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5919 | break; |
| 5920 | default: UNREACHABLE(); |
| 5921 | } |
| 5922 | // check that the *signed* result fits in a smi |
| 5923 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5924 | __ b(mi, &result_not_a_smi); |
| 5925 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5926 | __ Ret(); |
| 5927 | |
| 5928 | Label have_to_allocate, got_a_heap_number; |
| 5929 | __ bind(&result_not_a_smi); |
| 5930 | switch (mode_) { |
| 5931 | case OVERWRITE_RIGHT: { |
| 5932 | __ tst(r0, Operand(kSmiTagMask)); |
| 5933 | __ b(eq, &have_to_allocate); |
| 5934 | __ mov(r5, Operand(r0)); |
| 5935 | break; |
| 5936 | } |
| 5937 | case OVERWRITE_LEFT: { |
| 5938 | __ tst(r1, Operand(kSmiTagMask)); |
| 5939 | __ b(eq, &have_to_allocate); |
| 5940 | __ mov(r5, Operand(r1)); |
| 5941 | break; |
| 5942 | } |
| 5943 | case NO_OVERWRITE: { |
| 5944 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5945 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5946 | } |
| 5947 | default: break; |
| 5948 | } |
| 5949 | __ bind(&got_a_heap_number); |
| 5950 | // r2: Answer as signed int32. |
| 5951 | // r5: Heap number to write answer into. |
| 5952 | |
| 5953 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5954 | // result. |
| 5955 | __ mov(r0, Operand(r5)); |
| 5956 | |
| 5957 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5958 | // r3 as scratch. r0 is preserved and returned. |
| 5959 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5960 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5961 | |
| 5962 | if (mode_ != NO_OVERWRITE) { |
| 5963 | __ bind(&have_to_allocate); |
| 5964 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5965 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5966 | __ jmp(&got_a_heap_number); |
| 5967 | } |
| 5968 | |
| 5969 | // If all else failed then we go to the runtime system. |
| 5970 | __ bind(&slow); |
| 5971 | __ push(r1); // restore stack |
| 5972 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5973 | switch (op_) { |
| 5974 | case Token::BIT_OR: |
| 5975 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5976 | break; |
| 5977 | case Token::BIT_AND: |
| 5978 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5979 | break; |
| 5980 | case Token::BIT_XOR: |
| 5981 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5982 | break; |
| 5983 | case Token::SAR: |
| 5984 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5985 | break; |
| 5986 | case Token::SHR: |
| 5987 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5988 | break; |
| 5989 | case Token::SHL: |
| 5990 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5991 | break; |
| 5992 | default: |
| 5993 | UNREACHABLE(); |
| 5994 | } |
| 5995 | } |
| 5996 | |
| 5997 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5998 | // Can we multiply by x with max two shifts and an add. |
| 5999 | // This answers yes to all integers from 2 to 10. |
| 6000 | static bool IsEasyToMultiplyBy(int x) { |
| 6001 | if (x < 2) return false; // Avoid special cases. |
| 6002 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 6003 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 6004 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 6005 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 6006 | return false; |
| 6007 | } |
| 6008 | |
| 6009 | |
| 6010 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 6011 | // Source and destination may be the same register. This routine does |
| 6012 | // not set carry and overflow the way a mul instruction would. |
| 6013 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 6014 | Register source, |
| 6015 | Register destination, |
| 6016 | int known_int) { |
| 6017 | if (IsPowerOf2(known_int)) { |
| 6018 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 6019 | } else if (PopCountLessThanEqual2(known_int)) { |
| 6020 | int first_bit = BitPosition(known_int); |
| 6021 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 6022 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 6023 | if (first_bit != 0) { |
| 6024 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 6025 | } |
| 6026 | } else { |
| 6027 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 6028 | int the_bit = BitPosition(known_int + 1); |
| 6029 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 6030 | } |
| 6031 | } |
| 6032 | |
| 6033 | |
| 6034 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 6035 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 6036 | // a result that needs shifting. |
| 6037 | static void MultiplyByKnownInt2( |
| 6038 | MacroAssembler* masm, |
| 6039 | Register result, |
| 6040 | Register source, |
| 6041 | Register known_int_register, // Smi tagged. |
| 6042 | int known_int, |
| 6043 | int* required_shift) { // Including Smi tag shift |
| 6044 | switch (known_int) { |
| 6045 | case 3: |
| 6046 | __ add(result, source, Operand(source, LSL, 1)); |
| 6047 | *required_shift = 1; |
| 6048 | break; |
| 6049 | case 5: |
| 6050 | __ add(result, source, Operand(source, LSL, 2)); |
| 6051 | *required_shift = 1; |
| 6052 | break; |
| 6053 | case 6: |
| 6054 | __ add(result, source, Operand(source, LSL, 1)); |
| 6055 | *required_shift = 2; |
| 6056 | break; |
| 6057 | case 7: |
| 6058 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 6059 | *required_shift = 1; |
| 6060 | break; |
| 6061 | case 9: |
| 6062 | __ add(result, source, Operand(source, LSL, 3)); |
| 6063 | *required_shift = 1; |
| 6064 | break; |
| 6065 | case 10: |
| 6066 | __ add(result, source, Operand(source, LSL, 2)); |
| 6067 | *required_shift = 2; |
| 6068 | break; |
| 6069 | default: |
| 6070 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 6071 | __ mul(result, source, known_int_register); |
| 6072 | *required_shift = 0; |
| 6073 | } |
| 6074 | } |
| 6075 | |
| 6076 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 6077 | const char* GenericBinaryOpStub::GetName() { |
| 6078 | if (name_ != NULL) return name_; |
| 6079 | const int len = 100; |
| 6080 | name_ = Bootstrapper::AllocateAutoDeletedArray(len); |
| 6081 | if (name_ == NULL) return "OOM"; |
| 6082 | const char* op_name = Token::Name(op_); |
| 6083 | const char* overwrite_name; |
| 6084 | switch (mode_) { |
| 6085 | case NO_OVERWRITE: overwrite_name = "Alloc"; break; |
| 6086 | case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; |
| 6087 | case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; |
| 6088 | default: overwrite_name = "UnknownOverwrite"; break; |
| 6089 | } |
| 6090 | |
| 6091 | OS::SNPrintF(Vector<char>(name_, len), |
| 6092 | "GenericBinaryOpStub_%s_%s%s", |
| 6093 | op_name, |
| 6094 | overwrite_name, |
| 6095 | specialized_on_rhs_ ? "_ConstantRhs" : 0); |
| 6096 | return name_; |
| 6097 | } |
| 6098 | |
| 6099 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6100 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6101 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 6102 | // r1 : x |
| 6103 | // r0 : y |
| 6104 | // result : r0 |
| 6105 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6106 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 6107 | // tell us that. |
| 6108 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 6109 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6110 | switch (op_) { |
| 6111 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6112 | Label not_smi; |
| 6113 | // Fast path. |
| 6114 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6115 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6116 | __ b(ne, ¬_smi); |
| 6117 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 6118 | // Return if no overflow. |
| 6119 | __ Ret(vc); |
| 6120 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 6121 | |
| 6122 | HandleBinaryOpSlowCases(masm, |
| 6123 | ¬_smi, |
| 6124 | Builtins::ADD, |
| 6125 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6126 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6127 | break; |
| 6128 | } |
| 6129 | |
| 6130 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6131 | Label not_smi; |
| 6132 | // Fast path. |
| 6133 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6134 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6135 | __ b(ne, ¬_smi); |
| 6136 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 6137 | // Return if no overflow. |
| 6138 | __ Ret(vc); |
| 6139 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 6140 | |
| 6141 | HandleBinaryOpSlowCases(masm, |
| 6142 | ¬_smi, |
| 6143 | Builtins::SUB, |
| 6144 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6145 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6146 | break; |
| 6147 | } |
| 6148 | |
| 6149 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6150 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6151 | ASSERT(kSmiTag == 0); // adjust code below |
| 6152 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6153 | __ b(ne, ¬_smi); |
| 6154 | // 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] | 6155 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6156 | // Do multiplication |
| 6157 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 6158 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6159 | __ mov(ip, Operand(r3, ASR, 31)); |
| 6160 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 6161 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6162 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6163 | __ tst(r3, Operand(r3)); |
| 6164 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6165 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6166 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 6167 | // We know one of them was zero. |
| 6168 | __ add(r2, r0, Operand(r1), SetCC); |
| 6169 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 6170 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 6171 | // Slow case. We fall through here if we multiplied a negative number |
| 6172 | // with 0, because that would mean we should produce -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6173 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6174 | |
| 6175 | HandleBinaryOpSlowCases(masm, |
| 6176 | ¬_smi, |
| 6177 | Builtins::MUL, |
| 6178 | Token::MUL, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6179 | mode_); |
| 6180 | break; |
| 6181 | } |
| 6182 | |
| 6183 | case Token::DIV: |
| 6184 | case Token::MOD: { |
| 6185 | Label not_smi; |
| 6186 | if (specialized_on_rhs_) { |
| 6187 | Label smi_is_unsuitable; |
| 6188 | __ BranchOnNotSmi(r1, ¬_smi); |
| 6189 | if (IsPowerOf2(constant_rhs_)) { |
| 6190 | if (op_ == Token::MOD) { |
| 6191 | __ and_(r0, |
| 6192 | r1, |
| 6193 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 6194 | SetCC); |
| 6195 | // We now have the answer, but if the input was negative we also |
| 6196 | // have the sign bit. Our work is done if the result is |
| 6197 | // positive or zero: |
| 6198 | __ Ret(pl); |
| 6199 | // A mod of a negative left hand side must return a negative number. |
| 6200 | // Unfortunately if the answer is 0 then we must return -0. And we |
| 6201 | // already optimistically trashed r0 so we may need to restore it. |
| 6202 | __ eor(r0, r0, Operand(0x80000000u), SetCC); |
| 6203 | // Next two instructions are conditional on the answer being -0. |
| 6204 | __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
| 6205 | __ b(eq, &smi_is_unsuitable); |
| 6206 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
| 6207 | __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_))); |
| 6208 | } else { |
| 6209 | ASSERT(op_ == Token::DIV); |
| 6210 | __ tst(r1, |
| 6211 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 6212 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 6213 | int shift = 0; |
| 6214 | int d = constant_rhs_; |
| 6215 | while ((d & 1) == 0) { |
| 6216 | d >>= 1; |
| 6217 | shift++; |
| 6218 | } |
| 6219 | __ mov(r0, Operand(r1, LSR, shift)); |
| 6220 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 6221 | } |
| 6222 | } else { |
| 6223 | // Not a power of 2. |
| 6224 | __ tst(r1, Operand(0x80000000u)); |
| 6225 | __ b(ne, &smi_is_unsuitable); |
| 6226 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 6227 | // multiplying. |
| 6228 | double divisor = 1.0 / constant_rhs_; |
| 6229 | int shift = 32; |
| 6230 | double scale = 4294967296.0; // 1 << 32. |
| 6231 | uint32_t mul; |
| 6232 | // Maximise the precision of the fixed point reciprocal. |
| 6233 | while (true) { |
| 6234 | mul = static_cast<uint32_t>(scale * divisor); |
| 6235 | if (mul >= 0x7fffffff) break; |
| 6236 | scale *= 2.0; |
| 6237 | shift++; |
| 6238 | } |
| 6239 | mul++; |
| 6240 | __ mov(r2, Operand(mul)); |
| 6241 | __ umull(r3, r2, r2, r1); |
| 6242 | __ mov(r2, Operand(r2, LSR, shift - 31)); |
| 6243 | // r2 is r1 / rhs. r2 is not Smi tagged. |
| 6244 | // r0 is still the known rhs. r0 is Smi tagged. |
| 6245 | // r1 is still the unkown lhs. r1 is Smi tagged. |
| 6246 | int required_r4_shift = 0; // Including the Smi tag shift of 1. |
| 6247 | // r4 = r2 * r0. |
| 6248 | MultiplyByKnownInt2(masm, |
| 6249 | r4, |
| 6250 | r2, |
| 6251 | r0, |
| 6252 | constant_rhs_, |
| 6253 | &required_r4_shift); |
| 6254 | // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs). |
| 6255 | if (op_ == Token::DIV) { |
| 6256 | __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC); |
| 6257 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
| 6258 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 6259 | } else { |
| 6260 | ASSERT(op_ == Token::MOD); |
| 6261 | __ sub(r0, r1, Operand(r4, LSL, required_r4_shift)); |
| 6262 | } |
| 6263 | } |
| 6264 | __ Ret(); |
| 6265 | __ bind(&smi_is_unsuitable); |
| 6266 | } else { |
| 6267 | __ jmp(¬_smi); |
| 6268 | } |
| 6269 | HandleBinaryOpSlowCases(masm, |
| 6270 | ¬_smi, |
| 6271 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV, |
| 6272 | op_, |
| 6273 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6274 | break; |
| 6275 | } |
| 6276 | |
| 6277 | case Token::BIT_OR: |
| 6278 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6279 | case Token::BIT_XOR: |
| 6280 | case Token::SAR: |
| 6281 | case Token::SHR: |
| 6282 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6283 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6284 | ASSERT(kSmiTag == 0); // adjust code below |
| 6285 | __ tst(r2, Operand(kSmiTagMask)); |
| 6286 | __ b(ne, &slow); |
| 6287 | switch (op_) { |
| 6288 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 6289 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 6290 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6291 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6292 | // Remove tags from right operand. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6293 | __ GetLeastBitsFromSmi(r2, r0, 5); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6294 | __ mov(r0, Operand(r1, ASR, r2)); |
| 6295 | // Smi tag result. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6296 | __ bic(r0, r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6297 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6298 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6299 | // Remove tags from operands. We can't do this on a 31 bit number |
| 6300 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 6301 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6302 | __ GetLeastBitsFromSmi(r2, r0, 5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6303 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6304 | // Unsigned shift is not allowed to produce a negative number, so |
| 6305 | // check the sign bit and the sign bit after Smi tagging. |
| 6306 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6307 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6308 | // Smi tag result. |
| 6309 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6310 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6311 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6312 | // Remove tags from operands. |
| 6313 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6314 | __ GetLeastBitsFromSmi(r2, r0, 5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6315 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6316 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6317 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 6318 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6319 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6320 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6321 | default: UNREACHABLE(); |
| 6322 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6323 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6324 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6325 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6326 | break; |
| 6327 | } |
| 6328 | |
| 6329 | default: UNREACHABLE(); |
| 6330 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6331 | // This code should be unreachable. |
| 6332 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6333 | } |
| 6334 | |
| 6335 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6336 | Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) { |
| 6337 | return Handle<Code>::null(); |
| 6338 | } |
| 6339 | |
| 6340 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6341 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 6342 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 6343 | // argument, so give it a Smi. |
| 6344 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6345 | __ push(r0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6346 | __ TailCallRuntime(Runtime::kStackGuard, 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6347 | |
| 6348 | __ StubReturn(1); |
| 6349 | } |
| 6350 | |
| 6351 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6352 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6353 | Label slow, done; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6354 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6355 | if (op_ == Token::SUB) { |
| 6356 | // Check whether the value is a smi. |
| 6357 | Label try_float; |
| 6358 | __ tst(r0, Operand(kSmiTagMask)); |
| 6359 | __ b(ne, &try_float); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6360 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6361 | // Go slow case if the value of the expression is zero |
| 6362 | // to make sure that we switch between 0 and -0. |
| 6363 | __ cmp(r0, Operand(0)); |
| 6364 | __ b(eq, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6365 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6366 | // The value of the expression is a smi that is not zero. Try |
| 6367 | // optimistic subtraction '0 - value'. |
| 6368 | __ rsb(r1, r0, Operand(0), SetCC); |
| 6369 | __ b(vs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6370 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6371 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 6372 | __ b(&done); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6373 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6374 | __ bind(&try_float); |
| 6375 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6376 | __ b(ne, &slow); |
| 6377 | // r0 is a heap number. Get a new heap number in r1. |
| 6378 | if (overwrite_) { |
| 6379 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6380 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6381 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6382 | } else { |
| 6383 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
| 6384 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 6385 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6386 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 6387 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6388 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 6389 | __ mov(r0, Operand(r1)); |
| 6390 | } |
| 6391 | } else if (op_ == Token::BIT_NOT) { |
| 6392 | // Check if the operand is a heap number. |
| 6393 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6394 | __ b(ne, &slow); |
| 6395 | |
| 6396 | // Convert the heap number is r0 to an untagged integer in r1. |
| 6397 | GetInt32(masm, r0, r1, r2, r3, &slow); |
| 6398 | |
| 6399 | // Do the bitwise operation (move negated) and check if the result |
| 6400 | // fits in a smi. |
| 6401 | Label try_float; |
| 6402 | __ mvn(r1, Operand(r1)); |
| 6403 | __ add(r2, r1, Operand(0x40000000), SetCC); |
| 6404 | __ b(mi, &try_float); |
| 6405 | __ mov(r0, Operand(r1, LSL, kSmiTagSize)); |
| 6406 | __ b(&done); |
| 6407 | |
| 6408 | __ bind(&try_float); |
| 6409 | if (!overwrite_) { |
| 6410 | // Allocate a fresh heap number, but don't overwrite r0 until |
| 6411 | // we're sure we can do it without going through the slow case |
| 6412 | // that needs the value in r0. |
| 6413 | AllocateHeapNumber(masm, &slow, r2, r3, r4); |
| 6414 | __ mov(r0, Operand(r2)); |
| 6415 | } |
| 6416 | |
| 6417 | // WriteInt32ToHeapNumberStub does not trigger GC, so we do not |
| 6418 | // have to set up a frame. |
| 6419 | WriteInt32ToHeapNumberStub stub(r1, r0, r2); |
| 6420 | __ push(lr); |
| 6421 | __ Call(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 6422 | __ pop(lr); |
| 6423 | } else { |
| 6424 | UNIMPLEMENTED(); |
| 6425 | } |
| 6426 | |
| 6427 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6428 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6429 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6430 | // Handle the slow case by jumping to the JavaScript builtin. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6431 | __ bind(&slow); |
| 6432 | __ push(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6433 | switch (op_) { |
| 6434 | case Token::SUB: |
| 6435 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 6436 | break; |
| 6437 | case Token::BIT_NOT: |
| 6438 | __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS); |
| 6439 | break; |
| 6440 | default: |
| 6441 | UNREACHABLE(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6442 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6443 | } |
| 6444 | |
| 6445 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6446 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6447 | // r0 holds the exception. |
| 6448 | |
| 6449 | // Adjust this code if not the case. |
| 6450 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6451 | |
| 6452 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6453 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 6454 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6455 | |
| 6456 | // Restore the next handler and frame pointer, discard handler state. |
| 6457 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 6458 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6459 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6460 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6461 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 6462 | |
| 6463 | // Before returning we restore the context from the frame pointer if |
| 6464 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6465 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6466 | __ cmp(fp, Operand(0)); |
| 6467 | // Set cp to NULL if fp is NULL. |
| 6468 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6469 | // Restore cp otherwise. |
| 6470 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6471 | #ifdef DEBUG |
| 6472 | if (FLAG_debug_code) { |
| 6473 | __ mov(lr, Operand(pc)); |
| 6474 | } |
| 6475 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6476 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6477 | __ pop(pc); |
| 6478 | } |
| 6479 | |
| 6480 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6481 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 6482 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6483 | // Adjust this code if not the case. |
| 6484 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6485 | |
| 6486 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6487 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6488 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6489 | |
| 6490 | // Unwind the handlers until the ENTRY handler is found. |
| 6491 | Label loop, done; |
| 6492 | __ bind(&loop); |
| 6493 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6494 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 6495 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6496 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 6497 | __ b(eq, &done); |
| 6498 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6499 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 6500 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6501 | __ jmp(&loop); |
| 6502 | __ bind(&done); |
| 6503 | |
| 6504 | // 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] | 6505 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6506 | __ pop(r2); |
| 6507 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6508 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6509 | if (type == OUT_OF_MEMORY) { |
| 6510 | // Set external caught exception to false. |
| 6511 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 6512 | __ mov(r0, Operand(false)); |
| 6513 | __ mov(r2, Operand(external_caught)); |
| 6514 | __ str(r0, MemOperand(r2)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6515 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6516 | // Set pending exception and r0 to out of memory exception. |
| 6517 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6518 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6519 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 6520 | __ str(r0, MemOperand(r2)); |
| 6521 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6522 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6523 | // Stack layout at this point. See also StackHandlerConstants. |
| 6524 | // sp -> state (ENTRY) |
| 6525 | // fp |
| 6526 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6527 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6528 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 6529 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6530 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 6531 | // Before returning we restore the context from the frame pointer if |
| 6532 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6533 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6534 | __ cmp(fp, Operand(0)); |
| 6535 | // Set cp to NULL if fp is NULL. |
| 6536 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6537 | // Restore cp otherwise. |
| 6538 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6539 | #ifdef DEBUG |
| 6540 | if (FLAG_debug_code) { |
| 6541 | __ mov(lr, Operand(pc)); |
| 6542 | } |
| 6543 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6544 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6545 | __ pop(pc); |
| 6546 | } |
| 6547 | |
| 6548 | |
| 6549 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 6550 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6551 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6552 | Label* throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6553 | bool do_gc, |
| 6554 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6555 | // r0: result parameter for PerformGC, if any |
| 6556 | // r4: number of arguments including receiver (C callee-saved) |
| 6557 | // r5: pointer to builtin function (C callee-saved) |
| 6558 | // r6: pointer to the first argument (C callee-saved) |
| 6559 | |
| 6560 | if (do_gc) { |
| 6561 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6562 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 6563 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6564 | } |
| 6565 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6566 | ExternalReference scope_depth = |
| 6567 | ExternalReference::heap_always_allocate_scope_depth(); |
| 6568 | if (always_allocate) { |
| 6569 | __ mov(r0, Operand(scope_depth)); |
| 6570 | __ ldr(r1, MemOperand(r0)); |
| 6571 | __ add(r1, r1, Operand(1)); |
| 6572 | __ str(r1, MemOperand(r0)); |
| 6573 | } |
| 6574 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6575 | // Call C built-in. |
| 6576 | // r0 = argc, r1 = argv |
| 6577 | __ mov(r0, Operand(r4)); |
| 6578 | __ mov(r1, Operand(r6)); |
| 6579 | |
| 6580 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 6581 | // frames, we need to know where the return address is. Right now, |
| 6582 | // we push it on the stack to be able to find it again, but we never |
| 6583 | // restore from it in case of changes, which makes it impossible to |
| 6584 | // support moving the C entry code stub. This should be fixed, but currently |
| 6585 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 6586 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6587 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 6588 | masm->push(lr); |
| 6589 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6590 | |
| 6591 | if (always_allocate) { |
| 6592 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 6593 | // though (contain the result). |
| 6594 | __ mov(r2, Operand(scope_depth)); |
| 6595 | __ ldr(r3, MemOperand(r2)); |
| 6596 | __ sub(r3, r3, Operand(1)); |
| 6597 | __ str(r3, MemOperand(r2)); |
| 6598 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6599 | |
| 6600 | // check for failure result |
| 6601 | Label failure_returned; |
| 6602 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 6603 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 6604 | __ add(r2, r0, Operand(1)); |
| 6605 | __ tst(r2, Operand(kFailureTagMask)); |
| 6606 | __ b(eq, &failure_returned); |
| 6607 | |
| 6608 | // Exit C frame and return. |
| 6609 | // r0:r1: result |
| 6610 | // sp: stack pointer |
| 6611 | // fp: frame pointer |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6612 | __ LeaveExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6613 | |
| 6614 | // check if we should retry or throw exception |
| 6615 | Label retry; |
| 6616 | __ bind(&failure_returned); |
| 6617 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 6618 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 6619 | __ b(eq, &retry); |
| 6620 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6621 | // Special handling of out of memory exceptions. |
| 6622 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6623 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6624 | __ b(eq, throw_out_of_memory_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6625 | |
| 6626 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6627 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6628 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6629 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6630 | __ ldr(r0, MemOperand(ip)); |
| 6631 | __ str(r3, MemOperand(ip)); |
| 6632 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6633 | // Special handling of termination exceptions which are uncatchable |
| 6634 | // by javascript code. |
| 6635 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 6636 | __ b(eq, throw_termination_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6637 | |
| 6638 | // Handle normal exception. |
| 6639 | __ jmp(throw_normal_exception); |
| 6640 | |
| 6641 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 6642 | } |
| 6643 | |
| 6644 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6645 | void CEntryStub::Generate(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6646 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 6647 | // r0: number of arguments including receiver |
| 6648 | // r1: pointer to builtin function |
| 6649 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6650 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6651 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6652 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6653 | // Result returned in r0 or r0+r1 by default. |
| 6654 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6655 | // NOTE: Invocations of builtins may return failure objects |
| 6656 | // instead of a proper result. The builtin entry handles |
| 6657 | // this by performing a garbage collection and retrying the |
| 6658 | // builtin once. |
| 6659 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6660 | // Enter the exit frame that transitions from JavaScript to C++. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6661 | __ EnterExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6662 | |
| 6663 | // r4: number of arguments (C callee-saved) |
| 6664 | // r5: pointer to builtin function (C callee-saved) |
| 6665 | // r6: pointer to first argument (C callee-saved) |
| 6666 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6667 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6668 | Label throw_termination_exception; |
| 6669 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6670 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6671 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6672 | GenerateCore(masm, |
| 6673 | &throw_normal_exception, |
| 6674 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6675 | &throw_out_of_memory_exception, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6676 | false, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6677 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6678 | |
| 6679 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6680 | GenerateCore(masm, |
| 6681 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6682 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6683 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6684 | true, |
| 6685 | false); |
| 6686 | |
| 6687 | // Do full GC and retry runtime call one final time. |
| 6688 | Failure* failure = Failure::InternalError(); |
| 6689 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 6690 | GenerateCore(masm, |
| 6691 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6692 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6693 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6694 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6695 | true); |
| 6696 | |
| 6697 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6698 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 6699 | |
| 6700 | __ bind(&throw_termination_exception); |
| 6701 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6702 | |
| 6703 | __ bind(&throw_normal_exception); |
| 6704 | GenerateThrowTOS(masm); |
| 6705 | } |
| 6706 | |
| 6707 | |
| 6708 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 6709 | // r0: code entry |
| 6710 | // r1: function |
| 6711 | // r2: receiver |
| 6712 | // r3: argc |
| 6713 | // [sp+0]: argv |
| 6714 | |
| 6715 | Label invoke, exit; |
| 6716 | |
| 6717 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 6718 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6719 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6720 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 6721 | |
| 6722 | // Get address of argv, see stm above. |
| 6723 | // r0: code entry |
| 6724 | // r1: function |
| 6725 | // r2: receiver |
| 6726 | // r3: argc |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6727 | __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6728 | |
| 6729 | // Push a frame with special values setup to mark it as an entry frame. |
| 6730 | // r0: code entry |
| 6731 | // r1: function |
| 6732 | // r2: receiver |
| 6733 | // r3: argc |
| 6734 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6735 | __ 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] | 6736 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 6737 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6738 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 6739 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6740 | __ ldr(r5, MemOperand(r5)); |
| 6741 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 6742 | |
| 6743 | // Setup frame pointer for the frame to be pushed. |
| 6744 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6745 | |
| 6746 | // Call a faked try-block that does the invoke. |
| 6747 | __ bl(&invoke); |
| 6748 | |
| 6749 | // Caught exception: Store result (exception) in the pending |
| 6750 | // exception field in the JSEnv and return a failure sentinel. |
| 6751 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 6752 | // 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] | 6753 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6754 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6755 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6756 | __ b(&exit); |
| 6757 | |
| 6758 | // Invoke: Link this frame into the handler chain. |
| 6759 | __ bind(&invoke); |
| 6760 | // Must preserve r0-r4, r5-r7 are available. |
| 6761 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6762 | // If an exception not caught by another handler occurs, this handler |
| 6763 | // returns control to the code after the bl(&invoke) above, which |
| 6764 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 6765 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6766 | |
| 6767 | // Clear any pending exceptions. |
| 6768 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6769 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6770 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6771 | __ str(r5, MemOperand(ip)); |
| 6772 | |
| 6773 | // Invoke the function by calling through JS entry trampoline builtin. |
| 6774 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6775 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6776 | |
| 6777 | // Expected registers by Builtins::JSEntryTrampoline |
| 6778 | // r0: code entry |
| 6779 | // r1: function |
| 6780 | // r2: receiver |
| 6781 | // r3: argc |
| 6782 | // r4: argv |
| 6783 | if (is_construct) { |
| 6784 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6785 | __ mov(ip, Operand(construct_entry)); |
| 6786 | } else { |
| 6787 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6788 | __ mov(ip, Operand(entry)); |
| 6789 | } |
| 6790 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6791 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6792 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6793 | // macro for the add instruction because we don't want the coverage tool |
| 6794 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6795 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6796 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6797 | |
| 6798 | // Unlink this frame from the handler chain. When reading the |
| 6799 | // address of the next handler, there is no need to use the address |
| 6800 | // displacement since the current stack pointer (sp) points directly |
| 6801 | // to the stack handler. |
| 6802 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6803 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6804 | __ str(r3, MemOperand(ip)); |
| 6805 | // No need to restore registers |
| 6806 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6807 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6808 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6809 | __ bind(&exit); // r0 holds result |
| 6810 | // Restore the top frame descriptors from the stack. |
| 6811 | __ pop(r3); |
| 6812 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6813 | __ str(r3, MemOperand(ip)); |
| 6814 | |
| 6815 | // Reset the stack to the callee saved registers. |
| 6816 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6817 | |
| 6818 | // Restore callee-saved registers and return. |
| 6819 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6820 | if (FLAG_debug_code) { |
| 6821 | __ mov(lr, Operand(pc)); |
| 6822 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6823 | #endif |
| 6824 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6825 | } |
| 6826 | |
| 6827 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6828 | // This stub performs an instanceof, calling the builtin function if |
| 6829 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6830 | // be an instance of (these are fetched from the stack). |
| 6831 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6832 | // Get the object - slow case for smis (we may need to throw an exception |
| 6833 | // depending on the rhs). |
| 6834 | Label slow, loop, is_instance, is_not_instance; |
| 6835 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6836 | __ BranchOnSmi(r0, &slow); |
| 6837 | |
| 6838 | // Check that the left hand is a JS object and put map in r3. |
| 6839 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6840 | __ b(lt, &slow); |
| 6841 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6842 | __ b(gt, &slow); |
| 6843 | |
| 6844 | // 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] | 6845 | __ ldr(r1, MemOperand(sp, 0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6846 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6847 | |
| 6848 | // Check that the function prototype is a JS object. |
| 6849 | __ BranchOnSmi(r4, &slow); |
| 6850 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6851 | __ b(lt, &slow); |
| 6852 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6853 | __ b(gt, &slow); |
| 6854 | |
| 6855 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6856 | // Get prototype of object into r2. |
| 6857 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6858 | |
| 6859 | // Loop through the prototype chain looking for the function prototype. |
| 6860 | __ bind(&loop); |
| 6861 | __ cmp(r2, Operand(r4)); |
| 6862 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6863 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 6864 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6865 | __ b(eq, &is_not_instance); |
| 6866 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6867 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6868 | __ jmp(&loop); |
| 6869 | |
| 6870 | __ bind(&is_instance); |
| 6871 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6872 | __ pop(); |
| 6873 | __ pop(); |
| 6874 | __ mov(pc, Operand(lr)); // Return. |
| 6875 | |
| 6876 | __ bind(&is_not_instance); |
| 6877 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6878 | __ pop(); |
| 6879 | __ pop(); |
| 6880 | __ mov(pc, Operand(lr)); // Return. |
| 6881 | |
| 6882 | // Slow-case. Tail call builtin. |
| 6883 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6884 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6885 | } |
| 6886 | |
| 6887 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6888 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6889 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6890 | Label adaptor; |
| 6891 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6892 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6893 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6894 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6895 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6896 | // Nothing to do: The formal number of parameters has already been |
| 6897 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6898 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6899 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6900 | // Arguments adaptor case: Read the arguments length from the |
| 6901 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6902 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6903 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6904 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6905 | } |
| 6906 | |
| 6907 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6908 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6909 | // The displacement is the offset of the last parameter (if any) |
| 6910 | // relative to the frame pointer. |
| 6911 | static const int kDisplacement = |
| 6912 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6913 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6914 | // Check that the key is a smi. |
| 6915 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6916 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6917 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6918 | // Check if the calling frame is an arguments adaptor frame. |
| 6919 | Label adaptor; |
| 6920 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6921 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6922 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6923 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6924 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6925 | // Check index against formal parameters count limit passed in |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6926 | // through register r0. Use unsigned comparison to get negative |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6927 | // check for free. |
| 6928 | __ cmp(r1, r0); |
| 6929 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6930 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6931 | // Read the argument from the stack and return it. |
| 6932 | __ sub(r3, r0, r1); |
| 6933 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6934 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6935 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6936 | |
| 6937 | // Arguments adaptor case: Check index against actual arguments |
| 6938 | // limit found in the arguments adaptor frame. Use unsigned |
| 6939 | // comparison to get negative check for free. |
| 6940 | __ bind(&adaptor); |
| 6941 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6942 | __ cmp(r1, r0); |
| 6943 | __ b(cs, &slow); |
| 6944 | |
| 6945 | // Read the argument from the adaptor frame and return it. |
| 6946 | __ sub(r3, r0, r1); |
| 6947 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6948 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6949 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6950 | |
| 6951 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6952 | // by calling the runtime system. |
| 6953 | __ bind(&slow); |
| 6954 | __ push(r1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6955 | __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6956 | } |
| 6957 | |
| 6958 | |
| 6959 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6960 | // sp[0] : number of parameters |
| 6961 | // sp[4] : receiver displacement |
| 6962 | // sp[8] : function |
| 6963 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6964 | // Check if the calling frame is an arguments adaptor frame. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6965 | Label adaptor_frame, try_allocate, runtime; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6966 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6967 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6968 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6969 | __ b(eq, &adaptor_frame); |
| 6970 | |
| 6971 | // Get the length from the frame. |
| 6972 | __ ldr(r1, MemOperand(sp, 0)); |
| 6973 | __ b(&try_allocate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6974 | |
| 6975 | // Patch the arguments.length and the parameters pointer. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6976 | __ bind(&adaptor_frame); |
| 6977 | __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6978 | __ str(r1, MemOperand(sp, 0)); |
| 6979 | __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6980 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6981 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6982 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6983 | // Try the new space allocation. Start out with computing the size |
| 6984 | // of the arguments object and the elements array (in words, not |
| 6985 | // bytes because AllocateInNewSpace expects words). |
| 6986 | Label add_arguments_object; |
| 6987 | __ bind(&try_allocate); |
| 6988 | __ cmp(r1, Operand(0)); |
| 6989 | __ b(eq, &add_arguments_object); |
| 6990 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 6991 | __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize)); |
| 6992 | __ bind(&add_arguments_object); |
| 6993 | __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize)); |
| 6994 | |
| 6995 | // Do the allocation of both objects in one go. |
| 6996 | __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT); |
| 6997 | |
| 6998 | // Get the arguments boilerplate from the current (global) context. |
| 6999 | int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX); |
| 7000 | __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 7001 | __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset)); |
| 7002 | __ ldr(r4, MemOperand(r4, offset)); |
| 7003 | |
| 7004 | // Copy the JS object part. |
| 7005 | for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) { |
| 7006 | __ ldr(r3, FieldMemOperand(r4, i)); |
| 7007 | __ str(r3, FieldMemOperand(r0, i)); |
| 7008 | } |
| 7009 | |
| 7010 | // Setup the callee in-object property. |
| 7011 | ASSERT(Heap::arguments_callee_index == 0); |
| 7012 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 7013 | __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize)); |
| 7014 | |
| 7015 | // Get the length (smi tagged) and set that as an in-object property too. |
| 7016 | ASSERT(Heap::arguments_length_index == 1); |
| 7017 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 7018 | __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize)); |
| 7019 | |
| 7020 | // If there are no actual arguments, we're done. |
| 7021 | Label done; |
| 7022 | __ cmp(r1, Operand(0)); |
| 7023 | __ b(eq, &done); |
| 7024 | |
| 7025 | // Get the parameters pointer from the stack and untag the length. |
| 7026 | __ ldr(r2, MemOperand(sp, 1 * kPointerSize)); |
| 7027 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 7028 | |
| 7029 | // Setup the elements pointer in the allocated arguments object and |
| 7030 | // initialize the header in the elements fixed array. |
| 7031 | __ add(r4, r0, Operand(Heap::kArgumentsObjectSize)); |
| 7032 | __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 7033 | __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex); |
| 7034 | __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset)); |
| 7035 | __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset)); |
| 7036 | |
| 7037 | // Copy the fixed array slots. |
| 7038 | Label loop; |
| 7039 | // Setup r4 to point to the first array slot. |
| 7040 | __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 7041 | __ bind(&loop); |
| 7042 | // Pre-decrement r2 with kPointerSize on each iteration. |
| 7043 | // Pre-decrement in order to skip receiver. |
| 7044 | __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex)); |
| 7045 | // Post-increment r4 with kPointerSize on each iteration. |
| 7046 | __ str(r3, MemOperand(r4, kPointerSize, PostIndex)); |
| 7047 | __ sub(r1, r1, Operand(1)); |
| 7048 | __ cmp(r1, Operand(0)); |
| 7049 | __ b(ne, &loop); |
| 7050 | |
| 7051 | // Return and remove the on-stack parameters. |
| 7052 | __ bind(&done); |
| 7053 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7054 | __ Ret(); |
| 7055 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7056 | // Do the runtime call to allocate the arguments object. |
| 7057 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7058 | __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7059 | } |
| 7060 | |
| 7061 | |
| 7062 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 7063 | Label slow; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7064 | |
| 7065 | // If the receiver might be a value (string, number or boolean) check for this |
| 7066 | // and box it if it is. |
| 7067 | if (ReceiverMightBeValue()) { |
| 7068 | // Get the receiver from the stack. |
| 7069 | // function, receiver [, arguments] |
| 7070 | Label receiver_is_value, receiver_is_js_object; |
| 7071 | __ ldr(r1, MemOperand(sp, argc_ * kPointerSize)); |
| 7072 | |
| 7073 | // Check if receiver is a smi (which is a number value). |
| 7074 | __ BranchOnSmi(r1, &receiver_is_value); |
| 7075 | |
| 7076 | // Check if the receiver is a valid JS object. |
| 7077 | __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 7078 | __ b(ge, &receiver_is_js_object); |
| 7079 | |
| 7080 | // Call the runtime to box the value. |
| 7081 | __ bind(&receiver_is_value); |
| 7082 | __ EnterInternalFrame(); |
| 7083 | __ push(r1); |
| 7084 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
| 7085 | __ LeaveInternalFrame(); |
| 7086 | __ str(r0, MemOperand(sp, argc_ * kPointerSize)); |
| 7087 | |
| 7088 | __ bind(&receiver_is_js_object); |
| 7089 | } |
| 7090 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7091 | // Get the function to call from the stack. |
| 7092 | // function, receiver [, arguments] |
| 7093 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 7094 | |
| 7095 | // Check that the function is really a JavaScript function. |
| 7096 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7097 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7098 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7099 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7100 | __ b(ne, &slow); |
| 7101 | |
| 7102 | // Fast-case: Invoke the function now. |
| 7103 | // r1: pushed function |
| 7104 | ParameterCount actual(argc_); |
| 7105 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 7106 | |
| 7107 | // Slow-case: Non-function called. |
| 7108 | __ bind(&slow); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7109 | // CALL_NON_FUNCTION expects the non-function callee as receiver (instead |
| 7110 | // of the original receiver from the call site). |
| 7111 | __ str(r1, MemOperand(sp, argc_ * kPointerSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7112 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 7113 | __ mov(r2, Operand(0)); |
| 7114 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 7115 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 7116 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7117 | } |
| 7118 | |
| 7119 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7120 | const char* CompareStub::GetName() { |
| 7121 | switch (cc_) { |
| 7122 | case lt: return "CompareStub_LT"; |
| 7123 | case gt: return "CompareStub_GT"; |
| 7124 | case le: return "CompareStub_LE"; |
| 7125 | case ge: return "CompareStub_GE"; |
| 7126 | case ne: { |
| 7127 | if (strict_) { |
| 7128 | if (never_nan_nan_) { |
| 7129 | return "CompareStub_NE_STRICT_NO_NAN"; |
| 7130 | } else { |
| 7131 | return "CompareStub_NE_STRICT"; |
| 7132 | } |
| 7133 | } else { |
| 7134 | if (never_nan_nan_) { |
| 7135 | return "CompareStub_NE_NO_NAN"; |
| 7136 | } else { |
| 7137 | return "CompareStub_NE"; |
| 7138 | } |
| 7139 | } |
| 7140 | } |
| 7141 | case eq: { |
| 7142 | if (strict_) { |
| 7143 | if (never_nan_nan_) { |
| 7144 | return "CompareStub_EQ_STRICT_NO_NAN"; |
| 7145 | } else { |
| 7146 | return "CompareStub_EQ_STRICT"; |
| 7147 | } |
| 7148 | } else { |
| 7149 | if (never_nan_nan_) { |
| 7150 | return "CompareStub_EQ_NO_NAN"; |
| 7151 | } else { |
| 7152 | return "CompareStub_EQ"; |
| 7153 | } |
| 7154 | } |
| 7155 | } |
| 7156 | default: return "CompareStub"; |
| 7157 | } |
| 7158 | } |
| 7159 | |
| 7160 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7161 | int CompareStub::MinorKey() { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7162 | // Encode the three parameters in a unique 16 bit value. |
| 7163 | ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16)); |
| 7164 | int nnn_value = (never_nan_nan_ ? 2 : 0); |
| 7165 | if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs. |
| 7166 | return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7167 | } |
| 7168 | |
| 7169 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7170 | void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm, |
| 7171 | Register dest, |
| 7172 | Register src, |
| 7173 | Register count, |
| 7174 | Register scratch, |
| 7175 | bool ascii) { |
| 7176 | Label loop; |
| 7177 | Label done; |
| 7178 | // This loop just copies one character at a time, as it is only used for very |
| 7179 | // short strings. |
| 7180 | if (!ascii) { |
| 7181 | __ add(count, count, Operand(count), SetCC); |
| 7182 | } else { |
| 7183 | __ cmp(count, Operand(0)); |
| 7184 | } |
| 7185 | __ b(eq, &done); |
| 7186 | |
| 7187 | __ bind(&loop); |
| 7188 | __ ldrb(scratch, MemOperand(src, 1, PostIndex)); |
| 7189 | // Perform sub between load and dependent store to get the load time to |
| 7190 | // complete. |
| 7191 | __ sub(count, count, Operand(1), SetCC); |
| 7192 | __ strb(scratch, MemOperand(dest, 1, PostIndex)); |
| 7193 | // last iteration. |
| 7194 | __ b(gt, &loop); |
| 7195 | |
| 7196 | __ bind(&done); |
| 7197 | } |
| 7198 | |
| 7199 | |
| 7200 | enum CopyCharactersFlags { |
| 7201 | COPY_ASCII = 1, |
| 7202 | DEST_ALWAYS_ALIGNED = 2 |
| 7203 | }; |
| 7204 | |
| 7205 | |
| 7206 | void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm, |
| 7207 | Register dest, |
| 7208 | Register src, |
| 7209 | Register count, |
| 7210 | Register scratch1, |
| 7211 | Register scratch2, |
| 7212 | Register scratch3, |
| 7213 | Register scratch4, |
| 7214 | Register scratch5, |
| 7215 | int flags) { |
| 7216 | bool ascii = (flags & COPY_ASCII) != 0; |
| 7217 | bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0; |
| 7218 | |
| 7219 | if (dest_always_aligned && FLAG_debug_code) { |
| 7220 | // Check that destination is actually word aligned if the flag says |
| 7221 | // that it is. |
| 7222 | __ tst(dest, Operand(kPointerAlignmentMask)); |
| 7223 | __ Check(eq, "Destination of copy not aligned."); |
| 7224 | } |
| 7225 | |
| 7226 | const int kReadAlignment = 4; |
| 7227 | const int kReadAlignmentMask = kReadAlignment - 1; |
| 7228 | // Ensure that reading an entire aligned word containing the last character |
| 7229 | // of a string will not read outside the allocated area (because we pad up |
| 7230 | // to kObjectAlignment). |
| 7231 | ASSERT(kObjectAlignment >= kReadAlignment); |
| 7232 | // Assumes word reads and writes are little endian. |
| 7233 | // Nothing to do for zero characters. |
| 7234 | Label done; |
| 7235 | if (!ascii) { |
| 7236 | __ add(count, count, Operand(count), SetCC); |
| 7237 | } else { |
| 7238 | __ cmp(count, Operand(0)); |
| 7239 | } |
| 7240 | __ b(eq, &done); |
| 7241 | |
| 7242 | // Assume that you cannot read (or write) unaligned. |
| 7243 | Label byte_loop; |
| 7244 | // Must copy at least eight bytes, otherwise just do it one byte at a time. |
| 7245 | __ cmp(count, Operand(8)); |
| 7246 | __ add(count, dest, Operand(count)); |
| 7247 | Register limit = count; // Read until src equals this. |
| 7248 | __ b(lt, &byte_loop); |
| 7249 | |
| 7250 | if (!dest_always_aligned) { |
| 7251 | // Align dest by byte copying. Copies between zero and three bytes. |
| 7252 | __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC); |
| 7253 | Label dest_aligned; |
| 7254 | __ b(eq, &dest_aligned); |
| 7255 | __ cmp(scratch4, Operand(2)); |
| 7256 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex)); |
| 7257 | __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le); |
| 7258 | __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt); |
| 7259 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7260 | __ strb(scratch2, MemOperand(dest, 1, PostIndex), le); |
| 7261 | __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt); |
| 7262 | __ bind(&dest_aligned); |
| 7263 | } |
| 7264 | |
| 7265 | Label simple_loop; |
| 7266 | |
| 7267 | __ sub(scratch4, dest, Operand(src)); |
| 7268 | __ and_(scratch4, scratch4, Operand(0x03), SetCC); |
| 7269 | __ b(eq, &simple_loop); |
| 7270 | // Shift register is number of bits in a source word that |
| 7271 | // must be combined with bits in the next source word in order |
| 7272 | // to create a destination word. |
| 7273 | |
| 7274 | // Complex loop for src/dst that are not aligned the same way. |
| 7275 | { |
| 7276 | Label loop; |
| 7277 | __ mov(scratch4, Operand(scratch4, LSL, 3)); |
| 7278 | Register left_shift = scratch4; |
| 7279 | __ and_(src, src, Operand(~3)); // Round down to load previous word. |
| 7280 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 7281 | // Store the "shift" most significant bits of scratch in the least |
| 7282 | // signficant bits (i.e., shift down by (32-shift)). |
| 7283 | __ rsb(scratch2, left_shift, Operand(32)); |
| 7284 | Register right_shift = scratch2; |
| 7285 | __ mov(scratch1, Operand(scratch1, LSR, right_shift)); |
| 7286 | |
| 7287 | __ bind(&loop); |
| 7288 | __ ldr(scratch3, MemOperand(src, 4, PostIndex)); |
| 7289 | __ sub(scratch5, limit, Operand(dest)); |
| 7290 | __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift)); |
| 7291 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 7292 | __ mov(scratch1, Operand(scratch3, LSR, right_shift)); |
| 7293 | // Loop if four or more bytes left to copy. |
| 7294 | // Compare to eight, because we did the subtract before increasing dst. |
| 7295 | __ sub(scratch5, scratch5, Operand(8), SetCC); |
| 7296 | __ b(ge, &loop); |
| 7297 | } |
| 7298 | // There is now between zero and three bytes left to copy (negative that |
| 7299 | // number is in scratch5), and between one and three bytes already read into |
| 7300 | // scratch1 (eight times that number in scratch4). We may have read past |
| 7301 | // the end of the string, but because objects are aligned, we have not read |
| 7302 | // past the end of the object. |
| 7303 | // Find the minimum of remaining characters to move and preloaded characters |
| 7304 | // and write those as bytes. |
| 7305 | __ add(scratch5, scratch5, Operand(4), SetCC); |
| 7306 | __ b(eq, &done); |
| 7307 | __ cmp(scratch4, Operand(scratch5, LSL, 3), ne); |
| 7308 | // Move minimum of bytes read and bytes left to copy to scratch4. |
| 7309 | __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt); |
| 7310 | // Between one and three (value in scratch5) characters already read into |
| 7311 | // scratch ready to write. |
| 7312 | __ cmp(scratch5, Operand(2)); |
| 7313 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7314 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge); |
| 7315 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge); |
| 7316 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt); |
| 7317 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt); |
| 7318 | // Copy any remaining bytes. |
| 7319 | __ b(&byte_loop); |
| 7320 | |
| 7321 | // Simple loop. |
| 7322 | // Copy words from src to dst, until less than four bytes left. |
| 7323 | // Both src and dest are word aligned. |
| 7324 | __ bind(&simple_loop); |
| 7325 | { |
| 7326 | Label loop; |
| 7327 | __ bind(&loop); |
| 7328 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 7329 | __ sub(scratch3, limit, Operand(dest)); |
| 7330 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 7331 | // Compare to 8, not 4, because we do the substraction before increasing |
| 7332 | // dest. |
| 7333 | __ cmp(scratch3, Operand(8)); |
| 7334 | __ b(ge, &loop); |
| 7335 | } |
| 7336 | |
| 7337 | // Copy bytes from src to dst until dst hits limit. |
| 7338 | __ bind(&byte_loop); |
| 7339 | __ cmp(dest, Operand(limit)); |
| 7340 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt); |
| 7341 | __ b(ge, &done); |
| 7342 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7343 | __ b(&byte_loop); |
| 7344 | |
| 7345 | __ bind(&done); |
| 7346 | } |
| 7347 | |
| 7348 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7349 | void StringStubBase::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm, |
| 7350 | Register c1, |
| 7351 | Register c2, |
| 7352 | Register scratch1, |
| 7353 | Register scratch2, |
| 7354 | Register scratch3, |
| 7355 | Register scratch4, |
| 7356 | Register scratch5, |
| 7357 | Label* not_found) { |
| 7358 | // Register scratch3 is the general scratch register in this function. |
| 7359 | Register scratch = scratch3; |
| 7360 | |
| 7361 | // Make sure that both characters are not digits as such strings has a |
| 7362 | // different hash algorithm. Don't try to look for these in the symbol table. |
| 7363 | Label not_array_index; |
| 7364 | __ sub(scratch, c1, Operand(static_cast<int>('0'))); |
| 7365 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 7366 | __ b(hi, ¬_array_index); |
| 7367 | __ sub(scratch, c2, Operand(static_cast<int>('0'))); |
| 7368 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 7369 | |
| 7370 | // If check failed combine both characters into single halfword. |
| 7371 | // This is required by the contract of the method: code at the |
| 7372 | // not_found branch expects this combination in c1 register |
| 7373 | __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls); |
| 7374 | __ b(ls, not_found); |
| 7375 | |
| 7376 | __ bind(¬_array_index); |
| 7377 | // Calculate the two character string hash. |
| 7378 | Register hash = scratch1; |
| 7379 | GenerateHashInit(masm, hash, c1); |
| 7380 | GenerateHashAddCharacter(masm, hash, c2); |
| 7381 | GenerateHashGetHash(masm, hash); |
| 7382 | |
| 7383 | // Collect the two characters in a register. |
| 7384 | Register chars = c1; |
| 7385 | __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte)); |
| 7386 | |
| 7387 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 7388 | // hash: hash of two character string. |
| 7389 | |
| 7390 | // Load symbol table |
| 7391 | // Load address of first element of the symbol table. |
| 7392 | Register symbol_table = c2; |
| 7393 | __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex); |
| 7394 | |
| 7395 | // Load undefined value |
| 7396 | Register undefined = scratch4; |
| 7397 | __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex); |
| 7398 | |
| 7399 | // Calculate capacity mask from the symbol table capacity. |
| 7400 | Register mask = scratch2; |
| 7401 | __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset)); |
| 7402 | __ mov(mask, Operand(mask, ASR, 1)); |
| 7403 | __ sub(mask, mask, Operand(1)); |
| 7404 | |
| 7405 | // Calculate untagged address of the first element of the symbol table. |
| 7406 | Register first_symbol_table_element = symbol_table; |
| 7407 | __ add(first_symbol_table_element, symbol_table, |
| 7408 | Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag)); |
| 7409 | |
| 7410 | // Registers |
| 7411 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 7412 | // hash: hash of two character string |
| 7413 | // mask: capacity mask |
| 7414 | // first_symbol_table_element: address of the first element of |
| 7415 | // the symbol table |
| 7416 | // scratch: - |
| 7417 | |
| 7418 | // Perform a number of probes in the symbol table. |
| 7419 | static const int kProbes = 4; |
| 7420 | Label found_in_symbol_table; |
| 7421 | Label next_probe[kProbes]; |
| 7422 | for (int i = 0; i < kProbes; i++) { |
| 7423 | Register candidate = scratch5; // Scratch register contains candidate. |
| 7424 | |
| 7425 | // Calculate entry in symbol table. |
| 7426 | if (i > 0) { |
| 7427 | __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i))); |
| 7428 | } else { |
| 7429 | __ mov(candidate, hash); |
| 7430 | } |
| 7431 | |
| 7432 | __ and_(candidate, candidate, Operand(mask)); |
| 7433 | |
| 7434 | // Load the entry from the symble table. |
| 7435 | ASSERT_EQ(1, SymbolTable::kEntrySize); |
| 7436 | __ ldr(candidate, |
| 7437 | MemOperand(first_symbol_table_element, |
| 7438 | candidate, |
| 7439 | LSL, |
| 7440 | kPointerSizeLog2)); |
| 7441 | |
| 7442 | // If entry is undefined no string with this hash can be found. |
| 7443 | __ cmp(candidate, undefined); |
| 7444 | __ b(eq, not_found); |
| 7445 | |
| 7446 | // If length is not 2 the string is not a candidate. |
| 7447 | __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset)); |
| 7448 | __ cmp(scratch, Operand(2)); |
| 7449 | __ b(ne, &next_probe[i]); |
| 7450 | |
| 7451 | // Check that the candidate is a non-external ascii string. |
| 7452 | __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset)); |
| 7453 | __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset)); |
| 7454 | __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, |
| 7455 | &next_probe[i]); |
| 7456 | |
| 7457 | // Check if the two characters match. |
| 7458 | // Assumes that word load is little endian. |
| 7459 | __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize)); |
| 7460 | __ cmp(chars, scratch); |
| 7461 | __ b(eq, &found_in_symbol_table); |
| 7462 | __ bind(&next_probe[i]); |
| 7463 | } |
| 7464 | |
| 7465 | // No matching 2 character string found by probing. |
| 7466 | __ jmp(not_found); |
| 7467 | |
| 7468 | // Scratch register contains result when we fall through to here. |
| 7469 | Register result = scratch; |
| 7470 | __ bind(&found_in_symbol_table); |
| 7471 | if (!result.is(r0)) { |
| 7472 | __ mov(r0, result); |
| 7473 | } |
| 7474 | } |
| 7475 | |
| 7476 | |
| 7477 | void StringStubBase::GenerateHashInit(MacroAssembler* masm, |
| 7478 | Register hash, |
| 7479 | Register character) { |
| 7480 | // hash = character + (character << 10); |
| 7481 | __ add(hash, character, Operand(character, LSL, 10)); |
| 7482 | // hash ^= hash >> 6; |
| 7483 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 7484 | } |
| 7485 | |
| 7486 | |
| 7487 | void StringStubBase::GenerateHashAddCharacter(MacroAssembler* masm, |
| 7488 | Register hash, |
| 7489 | Register character) { |
| 7490 | // hash += character; |
| 7491 | __ add(hash, hash, Operand(character)); |
| 7492 | // hash += hash << 10; |
| 7493 | __ add(hash, hash, Operand(hash, LSL, 10)); |
| 7494 | // hash ^= hash >> 6; |
| 7495 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 7496 | } |
| 7497 | |
| 7498 | |
| 7499 | void StringStubBase::GenerateHashGetHash(MacroAssembler* masm, |
| 7500 | Register hash) { |
| 7501 | // hash += hash << 3; |
| 7502 | __ add(hash, hash, Operand(hash, LSL, 3)); |
| 7503 | // hash ^= hash >> 11; |
| 7504 | __ eor(hash, hash, Operand(hash, ASR, 11)); |
| 7505 | // hash += hash << 15; |
| 7506 | __ add(hash, hash, Operand(hash, LSL, 15), SetCC); |
| 7507 | |
| 7508 | // if (hash == 0) hash = 27; |
| 7509 | __ mov(hash, Operand(27), LeaveCC, nz); |
| 7510 | } |
| 7511 | |
| 7512 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7513 | void SubStringStub::Generate(MacroAssembler* masm) { |
| 7514 | Label runtime; |
| 7515 | |
| 7516 | // Stack frame on entry. |
| 7517 | // lr: return address |
| 7518 | // sp[0]: to |
| 7519 | // sp[4]: from |
| 7520 | // sp[8]: string |
| 7521 | |
| 7522 | // This stub is called from the native-call %_SubString(...), so |
| 7523 | // nothing can be assumed about the arguments. It is tested that: |
| 7524 | // "string" is a sequential string, |
| 7525 | // both "from" and "to" are smis, and |
| 7526 | // 0 <= from <= to <= string.length. |
| 7527 | // If any of these assumptions fail, we call the runtime system. |
| 7528 | |
| 7529 | static const int kToOffset = 0 * kPointerSize; |
| 7530 | static const int kFromOffset = 1 * kPointerSize; |
| 7531 | static const int kStringOffset = 2 * kPointerSize; |
| 7532 | |
| 7533 | |
| 7534 | // Check bounds and smi-ness. |
| 7535 | __ ldr(r7, MemOperand(sp, kToOffset)); |
| 7536 | __ ldr(r6, MemOperand(sp, kFromOffset)); |
| 7537 | ASSERT_EQ(0, kSmiTag); |
| 7538 | ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize); |
| 7539 | // I.e., arithmetic shift right by one un-smi-tags. |
| 7540 | __ mov(r2, Operand(r7, ASR, 1), SetCC); |
| 7541 | __ mov(r3, Operand(r6, ASR, 1), SetCC, cc); |
| 7542 | // If either r2 or r6 had the smi tag bit set, then carry is set now. |
| 7543 | __ b(cs, &runtime); // Either "from" or "to" is not a smi. |
| 7544 | __ b(mi, &runtime); // From is negative. |
| 7545 | |
| 7546 | __ sub(r2, r2, Operand(r3), SetCC); |
| 7547 | __ b(mi, &runtime); // Fail if from > to. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7548 | // Special handling of sub-strings of length 1 and 2. One character strings |
| 7549 | // are handled in the runtime system (looked up in the single character |
| 7550 | // cache). Two character strings are looked for in the symbol cache. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7551 | __ cmp(r2, Operand(2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7552 | __ b(lt, &runtime); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7553 | |
| 7554 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7555 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7556 | // r6: from (smi) |
| 7557 | // r7: to (smi) |
| 7558 | |
| 7559 | // Make sure first argument is a sequential (or flat) string. |
| 7560 | __ ldr(r5, MemOperand(sp, kStringOffset)); |
| 7561 | ASSERT_EQ(0, kSmiTag); |
| 7562 | __ tst(r5, Operand(kSmiTagMask)); |
| 7563 | __ b(eq, &runtime); |
| 7564 | Condition is_string = masm->IsObjectStringType(r5, r1); |
| 7565 | __ b(NegateCondition(is_string), &runtime); |
| 7566 | |
| 7567 | // r1: instance type |
| 7568 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7569 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7570 | // r5: string |
| 7571 | // r6: from (smi) |
| 7572 | // r7: to (smi) |
| 7573 | Label seq_string; |
| 7574 | __ and_(r4, r1, Operand(kStringRepresentationMask)); |
| 7575 | ASSERT(kSeqStringTag < kConsStringTag); |
| 7576 | ASSERT(kExternalStringTag > kConsStringTag); |
| 7577 | __ cmp(r4, Operand(kConsStringTag)); |
| 7578 | __ b(gt, &runtime); // External strings go to runtime. |
| 7579 | __ b(lt, &seq_string); // Sequential strings are handled directly. |
| 7580 | |
| 7581 | // Cons string. Try to recurse (once) on the first substring. |
| 7582 | // (This adds a little more generality than necessary to handle flattened |
| 7583 | // cons strings, but not much). |
| 7584 | __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset)); |
| 7585 | __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset)); |
| 7586 | __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7587 | __ tst(r1, Operand(kStringRepresentationMask)); |
| 7588 | ASSERT_EQ(0, kSeqStringTag); |
| 7589 | __ b(ne, &runtime); // Cons and External strings go to runtime. |
| 7590 | |
| 7591 | // Definitly a sequential string. |
| 7592 | __ bind(&seq_string); |
| 7593 | |
| 7594 | // r1: instance type. |
| 7595 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7596 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7597 | // r5: string |
| 7598 | // r6: from (smi) |
| 7599 | // r7: to (smi) |
| 7600 | __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset)); |
| 7601 | __ cmp(r4, Operand(r7, ASR, 1)); |
| 7602 | __ b(lt, &runtime); // Fail if to > length. |
| 7603 | |
| 7604 | // r1: instance type. |
| 7605 | // r2: result string length. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7606 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7607 | // r5: string. |
| 7608 | // r6: from offset (smi) |
| 7609 | // Check for flat ascii string. |
| 7610 | Label non_ascii_flat; |
| 7611 | __ tst(r1, Operand(kStringEncodingMask)); |
| 7612 | ASSERT_EQ(0, kTwoByteStringTag); |
| 7613 | __ b(eq, &non_ascii_flat); |
| 7614 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7615 | Label result_longer_than_two; |
| 7616 | __ cmp(r2, Operand(2)); |
| 7617 | __ b(gt, &result_longer_than_two); |
| 7618 | |
| 7619 | // Sub string of length 2 requested. |
| 7620 | // Get the two characters forming the sub string. |
| 7621 | __ add(r5, r5, Operand(r3)); |
| 7622 | __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize)); |
| 7623 | __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1)); |
| 7624 | |
| 7625 | // Try to lookup two character string in symbol table. |
| 7626 | Label make_two_character_string; |
| 7627 | GenerateTwoCharacterSymbolTableProbe(masm, r3, r4, r1, r5, r6, r7, r9, |
| 7628 | &make_two_character_string); |
| 7629 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 7630 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7631 | __ Ret(); |
| 7632 | |
| 7633 | // r2: result string length. |
| 7634 | // r3: two characters combined into halfword in little endian byte order. |
| 7635 | __ bind(&make_two_character_string); |
| 7636 | __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime); |
| 7637 | __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 7638 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 7639 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7640 | __ Ret(); |
| 7641 | |
| 7642 | __ bind(&result_longer_than_two); |
| 7643 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7644 | // Allocate the result. |
| 7645 | __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime); |
| 7646 | |
| 7647 | // r0: result string. |
| 7648 | // r2: result string length. |
| 7649 | // r5: string. |
| 7650 | // r6: from offset (smi) |
| 7651 | // Locate first character of result. |
| 7652 | __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7653 | // Locate 'from' character of string. |
| 7654 | __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7655 | __ add(r5, r5, Operand(r6, ASR, 1)); |
| 7656 | |
| 7657 | // r0: result string. |
| 7658 | // r1: first character of result string. |
| 7659 | // r2: result string length. |
| 7660 | // r5: first character of sub string to copy. |
| 7661 | ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask); |
| 7662 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 7663 | COPY_ASCII | DEST_ALWAYS_ALIGNED); |
| 7664 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 7665 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7666 | __ Ret(); |
| 7667 | |
| 7668 | __ bind(&non_ascii_flat); |
| 7669 | // r2: result string length. |
| 7670 | // r5: string. |
| 7671 | // r6: from offset (smi) |
| 7672 | // Check for flat two byte string. |
| 7673 | |
| 7674 | // Allocate the result. |
| 7675 | __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime); |
| 7676 | |
| 7677 | // r0: result string. |
| 7678 | // r2: result string length. |
| 7679 | // r5: string. |
| 7680 | // Locate first character of result. |
| 7681 | __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7682 | // Locate 'from' character of string. |
| 7683 | __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7684 | // As "from" is a smi it is 2 times the value which matches the size of a two |
| 7685 | // byte character. |
| 7686 | __ add(r5, r5, Operand(r6)); |
| 7687 | |
| 7688 | // r0: result string. |
| 7689 | // r1: first character of result. |
| 7690 | // r2: result length. |
| 7691 | // r5: first character of string to copy. |
| 7692 | ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask); |
| 7693 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 7694 | DEST_ALWAYS_ALIGNED); |
| 7695 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 7696 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7697 | __ Ret(); |
| 7698 | |
| 7699 | // Just jump to runtime to create the sub string. |
| 7700 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7701 | __ TailCallRuntime(Runtime::kSubString, 3, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7702 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7703 | |
| 7704 | |
| 7705 | void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 7706 | Register left, |
| 7707 | Register right, |
| 7708 | Register scratch1, |
| 7709 | Register scratch2, |
| 7710 | Register scratch3, |
| 7711 | Register scratch4) { |
| 7712 | Label compare_lengths; |
| 7713 | // Find minimum length and length difference. |
| 7714 | __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset)); |
| 7715 | __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset)); |
| 7716 | __ sub(scratch3, scratch1, Operand(scratch2), SetCC); |
| 7717 | Register length_delta = scratch3; |
| 7718 | __ mov(scratch1, scratch2, LeaveCC, gt); |
| 7719 | Register min_length = scratch1; |
| 7720 | __ tst(min_length, Operand(min_length)); |
| 7721 | __ b(eq, &compare_lengths); |
| 7722 | |
| 7723 | // Setup registers so that we only need to increment one register |
| 7724 | // in the loop. |
| 7725 | __ add(scratch2, min_length, |
| 7726 | Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7727 | __ add(left, left, Operand(scratch2)); |
| 7728 | __ add(right, right, Operand(scratch2)); |
| 7729 | // Registers left and right points to the min_length character of strings. |
| 7730 | __ rsb(min_length, min_length, Operand(-1)); |
| 7731 | Register index = min_length; |
| 7732 | // Index starts at -min_length. |
| 7733 | |
| 7734 | { |
| 7735 | // Compare loop. |
| 7736 | Label loop; |
| 7737 | __ bind(&loop); |
| 7738 | // Compare characters. |
| 7739 | __ add(index, index, Operand(1), SetCC); |
| 7740 | __ ldrb(scratch2, MemOperand(left, index), ne); |
| 7741 | __ ldrb(scratch4, MemOperand(right, index), ne); |
| 7742 | // Skip to compare lengths with eq condition true. |
| 7743 | __ b(eq, &compare_lengths); |
| 7744 | __ cmp(scratch2, scratch4); |
| 7745 | __ b(eq, &loop); |
| 7746 | // Fallthrough with eq condition false. |
| 7747 | } |
| 7748 | // Compare lengths - strings up to min-length are equal. |
| 7749 | __ bind(&compare_lengths); |
| 7750 | ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0)); |
| 7751 | // Use zero length_delta as result. |
| 7752 | __ mov(r0, Operand(length_delta), SetCC, eq); |
| 7753 | // Fall through to here if characters compare not-equal. |
| 7754 | __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt); |
| 7755 | __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt); |
| 7756 | __ Ret(); |
| 7757 | } |
| 7758 | |
| 7759 | |
| 7760 | void StringCompareStub::Generate(MacroAssembler* masm) { |
| 7761 | Label runtime; |
| 7762 | |
| 7763 | // Stack frame on entry. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7764 | // sp[0]: right string |
| 7765 | // sp[4]: left string |
| 7766 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left |
| 7767 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7768 | |
| 7769 | Label not_same; |
| 7770 | __ cmp(r0, r1); |
| 7771 | __ b(ne, ¬_same); |
| 7772 | ASSERT_EQ(0, EQUAL); |
| 7773 | ASSERT_EQ(0, kSmiTag); |
| 7774 | __ mov(r0, Operand(Smi::FromInt(EQUAL))); |
| 7775 | __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2); |
| 7776 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7777 | __ Ret(); |
| 7778 | |
| 7779 | __ bind(¬_same); |
| 7780 | |
| 7781 | // Check that both objects are sequential ascii strings. |
| 7782 | __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime); |
| 7783 | |
| 7784 | // Compare flat ascii strings natively. Remove arguments from stack first. |
| 7785 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 7786 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7787 | GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5); |
| 7788 | |
| 7789 | // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater) |
| 7790 | // tagged as a small integer. |
| 7791 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7792 | __ TailCallRuntime(Runtime::kStringCompare, 2, 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7793 | } |
| 7794 | |
| 7795 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7796 | void StringAddStub::Generate(MacroAssembler* masm) { |
| 7797 | Label string_add_runtime; |
| 7798 | // Stack on entry: |
| 7799 | // sp[0]: second argument. |
| 7800 | // sp[4]: first argument. |
| 7801 | |
| 7802 | // Load the two arguments. |
| 7803 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument. |
| 7804 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument. |
| 7805 | |
| 7806 | // Make sure that both arguments are strings if not known in advance. |
| 7807 | if (string_check_) { |
| 7808 | ASSERT_EQ(0, kSmiTag); |
| 7809 | __ JumpIfEitherSmi(r0, r1, &string_add_runtime); |
| 7810 | // Load instance types. |
| 7811 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7812 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7813 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7814 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7815 | ASSERT_EQ(0, kStringTag); |
| 7816 | // If either is not a string, go to runtime. |
| 7817 | __ tst(r4, Operand(kIsNotStringMask)); |
| 7818 | __ tst(r5, Operand(kIsNotStringMask), eq); |
| 7819 | __ b(ne, &string_add_runtime); |
| 7820 | } |
| 7821 | |
| 7822 | // Both arguments are strings. |
| 7823 | // r0: first string |
| 7824 | // r1: second string |
| 7825 | // r4: first string instance type (if string_check_) |
| 7826 | // r5: second string instance type (if string_check_) |
| 7827 | { |
| 7828 | Label strings_not_empty; |
| 7829 | // Check if either of the strings are empty. In that case return the other. |
| 7830 | __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset)); |
| 7831 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
| 7832 | __ cmp(r2, Operand(0)); // Test if first string is empty. |
| 7833 | __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second. |
| 7834 | __ cmp(r3, Operand(0), ne); // Else test if second string is empty. |
| 7835 | __ b(ne, &strings_not_empty); // If either string was empty, return r0. |
| 7836 | |
| 7837 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7838 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7839 | __ Ret(); |
| 7840 | |
| 7841 | __ bind(&strings_not_empty); |
| 7842 | } |
| 7843 | |
| 7844 | // Both strings are non-empty. |
| 7845 | // r0: first string |
| 7846 | // r1: second string |
| 7847 | // r2: length of first string |
| 7848 | // r3: length of second string |
| 7849 | // r4: first string instance type (if string_check_) |
| 7850 | // r5: second string instance type (if string_check_) |
| 7851 | // Look at the length of the result of adding the two strings. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7852 | Label string_add_flat_result, longer_than_two; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7853 | // Adding two lengths can't overflow. |
| 7854 | ASSERT(String::kMaxLength * 2 > String::kMaxLength); |
| 7855 | __ add(r6, r2, Operand(r3)); |
| 7856 | // Use the runtime system when adding two one character strings, as it |
| 7857 | // contains optimizations for this specific case using the symbol table. |
| 7858 | __ cmp(r6, Operand(2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7859 | __ b(ne, &longer_than_two); |
| 7860 | |
| 7861 | // Check that both strings are non-external ascii strings. |
| 7862 | if (!string_check_) { |
| 7863 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7864 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7865 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7866 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7867 | } |
| 7868 | __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7, |
| 7869 | &string_add_runtime); |
| 7870 | |
| 7871 | // Get the two characters forming the sub string. |
| 7872 | __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 7873 | __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 7874 | |
| 7875 | // Try to lookup two character string in symbol table. If it is not found |
| 7876 | // just allocate a new one. |
| 7877 | Label make_two_character_string; |
| 7878 | GenerateTwoCharacterSymbolTableProbe(masm, r2, r3, r6, r7, r4, r5, r9, |
| 7879 | &make_two_character_string); |
| 7880 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7881 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7882 | __ Ret(); |
| 7883 | |
| 7884 | __ bind(&make_two_character_string); |
| 7885 | // Resulting string has length 2 and first chars of two strings |
| 7886 | // are combined into single halfword in r2 register. |
| 7887 | // So we can fill resulting string without two loops by a single |
| 7888 | // halfword store instruction (which assumes that processor is |
| 7889 | // in a little endian mode) |
| 7890 | __ mov(r6, Operand(2)); |
| 7891 | __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime); |
| 7892 | __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 7893 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7894 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7895 | __ Ret(); |
| 7896 | |
| 7897 | __ bind(&longer_than_two); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7898 | // Check if resulting string will be flat. |
| 7899 | __ cmp(r6, Operand(String::kMinNonFlatLength)); |
| 7900 | __ b(lt, &string_add_flat_result); |
| 7901 | // Handle exceptionally long strings in the runtime system. |
| 7902 | ASSERT((String::kMaxLength & 0x80000000) == 0); |
| 7903 | ASSERT(IsPowerOf2(String::kMaxLength + 1)); |
| 7904 | // kMaxLength + 1 is representable as shifted literal, kMaxLength is not. |
| 7905 | __ cmp(r6, Operand(String::kMaxLength + 1)); |
| 7906 | __ b(hs, &string_add_runtime); |
| 7907 | |
| 7908 | // If result is not supposed to be flat, allocate a cons string object. |
| 7909 | // If both strings are ascii the result is an ascii cons string. |
| 7910 | if (!string_check_) { |
| 7911 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7912 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7913 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7914 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7915 | } |
| 7916 | Label non_ascii, allocated; |
| 7917 | ASSERT_EQ(0, kTwoByteStringTag); |
| 7918 | __ tst(r4, Operand(kStringEncodingMask)); |
| 7919 | __ tst(r5, Operand(kStringEncodingMask), ne); |
| 7920 | __ b(eq, &non_ascii); |
| 7921 | |
| 7922 | // Allocate an ASCII cons string. |
| 7923 | __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime); |
| 7924 | __ bind(&allocated); |
| 7925 | // Fill the fields of the cons string. |
| 7926 | __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset)); |
| 7927 | __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset)); |
| 7928 | __ mov(r0, Operand(r7)); |
| 7929 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7930 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7931 | __ Ret(); |
| 7932 | |
| 7933 | __ bind(&non_ascii); |
| 7934 | // Allocate a two byte cons string. |
| 7935 | __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime); |
| 7936 | __ jmp(&allocated); |
| 7937 | |
| 7938 | // Handle creating a flat result. First check that both strings are |
| 7939 | // sequential and that they have the same encoding. |
| 7940 | // r0: first string |
| 7941 | // r1: second string |
| 7942 | // r2: length of first string |
| 7943 | // r3: length of second string |
| 7944 | // r4: first string instance type (if string_check_) |
| 7945 | // r5: second string instance type (if string_check_) |
| 7946 | // r6: sum of lengths. |
| 7947 | __ bind(&string_add_flat_result); |
| 7948 | if (!string_check_) { |
| 7949 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7950 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7951 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7952 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7953 | } |
| 7954 | // Check that both strings are sequential. |
| 7955 | ASSERT_EQ(0, kSeqStringTag); |
| 7956 | __ tst(r4, Operand(kStringRepresentationMask)); |
| 7957 | __ tst(r5, Operand(kStringRepresentationMask), eq); |
| 7958 | __ b(ne, &string_add_runtime); |
| 7959 | // Now check if both strings have the same encoding (ASCII/Two-byte). |
| 7960 | // r0: first string. |
| 7961 | // r1: second string. |
| 7962 | // r2: length of first string. |
| 7963 | // r3: length of second string. |
| 7964 | // r6: sum of lengths.. |
| 7965 | Label non_ascii_string_add_flat_result; |
| 7966 | ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test. |
| 7967 | __ eor(r7, r4, Operand(r5)); |
| 7968 | __ tst(r7, Operand(kStringEncodingMask)); |
| 7969 | __ b(ne, &string_add_runtime); |
| 7970 | // And see if it's ASCII or two-byte. |
| 7971 | __ tst(r4, Operand(kStringEncodingMask)); |
| 7972 | __ b(eq, &non_ascii_string_add_flat_result); |
| 7973 | |
| 7974 | // Both strings are sequential ASCII strings. We also know that they are |
| 7975 | // short (since the sum of the lengths is less than kMinNonFlatLength). |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7976 | // r6: length of resulting flat string |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7977 | __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 7978 | // Locate first character of result. |
| 7979 | __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7980 | // Locate first character of first argument. |
| 7981 | __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7982 | // r0: first character of first string. |
| 7983 | // r1: second string. |
| 7984 | // r2: length of first string. |
| 7985 | // r3: length of second string. |
| 7986 | // r6: first character of result. |
| 7987 | // r7: result string. |
| 7988 | GenerateCopyCharacters(masm, r6, r0, r2, r4, true); |
| 7989 | |
| 7990 | // Load second argument and locate first character. |
| 7991 | __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7992 | // r1: first character of second string. |
| 7993 | // r3: length of second string. |
| 7994 | // r6: next character of result. |
| 7995 | // r7: result string. |
| 7996 | GenerateCopyCharacters(masm, r6, r1, r3, r4, true); |
| 7997 | __ mov(r0, Operand(r7)); |
| 7998 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7999 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8000 | __ Ret(); |
| 8001 | |
| 8002 | __ bind(&non_ascii_string_add_flat_result); |
| 8003 | // Both strings are sequential two byte strings. |
| 8004 | // r0: first string. |
| 8005 | // r1: second string. |
| 8006 | // r2: length of first string. |
| 8007 | // r3: length of second string. |
| 8008 | // r6: sum of length of strings. |
| 8009 | __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 8010 | // r0: first string. |
| 8011 | // r1: second string. |
| 8012 | // r2: length of first string. |
| 8013 | // r3: length of second string. |
| 8014 | // r7: result string. |
| 8015 | |
| 8016 | // Locate first character of result. |
| 8017 | __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8018 | // Locate first character of first argument. |
| 8019 | __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8020 | |
| 8021 | // r0: first character of first string. |
| 8022 | // r1: second string. |
| 8023 | // r2: length of first string. |
| 8024 | // r3: length of second string. |
| 8025 | // r6: first character of result. |
| 8026 | // r7: result string. |
| 8027 | GenerateCopyCharacters(masm, r6, r0, r2, r4, false); |
| 8028 | |
| 8029 | // Locate first character of second argument. |
| 8030 | __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8031 | |
| 8032 | // r1: first character of second string. |
| 8033 | // r3: length of second string. |
| 8034 | // r6: next character of result (after copy of first string). |
| 8035 | // r7: result string. |
| 8036 | GenerateCopyCharacters(masm, r6, r1, r3, r4, false); |
| 8037 | |
| 8038 | __ mov(r0, Operand(r7)); |
| 8039 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8040 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8041 | __ Ret(); |
| 8042 | |
| 8043 | // Just jump to runtime to add the two strings. |
| 8044 | __ bind(&string_add_runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8045 | __ TailCallRuntime(Runtime::kStringAdd, 2, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8046 | } |
| 8047 | |
| 8048 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8049 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8050 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8051 | } } // namespace v8::internal |