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 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 184 | VirtualFrame::SpilledScope spilled_scope(frame_); |
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); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 277 | |
| 278 | // Bind all the bailout labels to the beginning of the function. |
| 279 | List<CompilationInfo::Bailout*>* bailouts = info->bailouts(); |
| 280 | for (int i = 0; i < bailouts->length(); i++) { |
| 281 | __ bind(bailouts->at(i)->label()); |
| 282 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 283 | } |
| 284 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 285 | // Initialize the function return target after the locals are set |
| 286 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 287 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 288 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 289 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 290 | // Generate code to 'execute' declarations and initialize functions |
| 291 | // (source elements). In case of an illegal redeclaration we need to |
| 292 | // handle that instead of processing the declarations. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 293 | if (scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 294 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 295 | scope()->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | } else { |
| 297 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 298 | ProcessDeclarations(scope()->declarations()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 299 | // Bail out if a stack-overflow exception occurred when processing |
| 300 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 301 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | } |
| 303 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 304 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 305 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 306 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 307 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 308 | |
| 309 | // Compile the body of the function in a vanilla state. Don't |
| 310 | // bother compiling all the code if the scope has an illegal |
| 311 | // redeclaration. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 312 | if (!scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 313 | Comment cmnt(masm_, "[ function body"); |
| 314 | #ifdef DEBUG |
| 315 | bool is_builtin = Bootstrapper::IsActive(); |
| 316 | bool should_trace = |
| 317 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 318 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 319 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 320 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 321 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | #endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 323 | VisitStatementsAndSpill(info->function()->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 325 | } |
| 326 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 327 | // Generate the return sequence if necessary. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 328 | if (has_valid_frame() || function_return_.is_linked()) { |
| 329 | if (!function_return_.is_linked()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 330 | CodeForReturnPosition(info->function()); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 331 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 332 | // exit |
| 333 | // r0: result |
| 334 | // sp: stack pointer |
| 335 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 336 | // cp: callee's context |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 337 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 338 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 339 | function_return_.Bind(); |
| 340 | if (FLAG_trace) { |
| 341 | // Push the return value on the stack as the parameter. |
| 342 | // Runtime::TraceExit returns the parameter as it is. |
| 343 | frame_->EmitPush(r0); |
| 344 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 345 | } |
| 346 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 347 | // Add a label for checking the size of the code used for returning. |
| 348 | Label check_exit_codesize; |
| 349 | masm_->bind(&check_exit_codesize); |
| 350 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 351 | // Calculate the exact length of the return sequence and make sure that |
| 352 | // the constant pool is not emitted inside of the return sequence. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 353 | int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 354 | int return_sequence_length = Assembler::kJSReturnSequenceLength; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 355 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 356 | // Additional mov instruction generated. |
| 357 | return_sequence_length++; |
| 358 | } |
| 359 | masm_->BlockConstPoolFor(return_sequence_length); |
| 360 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 361 | // Tear down the frame which will restore the caller's frame pointer and |
| 362 | // the link register. |
| 363 | frame_->Exit(); |
| 364 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 365 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 366 | // tool from instrumenting as we rely on the code size here. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 367 | masm_->add(sp, sp, Operand(sp_delta)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 368 | masm_->Jump(lr); |
| 369 | |
| 370 | // Check that the size of the code used for returning matches what is |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 371 | // expected by the debugger. The add instruction above is an addressing |
| 372 | // mode 1 instruction where there are restrictions on which immediate values |
| 373 | // can be encoded in the instruction and which immediate values requires |
| 374 | // use of an additional instruction for moving the immediate to a temporary |
| 375 | // register. |
| 376 | ASSERT_EQ(return_sequence_length, |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 377 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 378 | } |
| 379 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 380 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | ASSERT(!has_cc()); |
| 382 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 383 | ASSERT(!function_return_is_shadowed_); |
| 384 | function_return_.Unuse(); |
| 385 | DeleteFrame(); |
| 386 | |
| 387 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 388 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 389 | ProcessDeferred(); |
| 390 | } |
| 391 | |
| 392 | allocator_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 396 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 397 | // Currently, this assertion will fail if we try to assign to |
| 398 | // a constant variable that is constant because it is read-only |
| 399 | // (such as the variable referring to a named function expression). |
| 400 | // We need to implement assignments to read-only variables. |
| 401 | // Ideally, we should do this during AST generation (by converting |
| 402 | // such assignments into expression statements); however, in general |
| 403 | // we may not be able to make the decision until past AST generation, |
| 404 | // that is when the entire program is known. |
| 405 | ASSERT(slot != NULL); |
| 406 | int index = slot->index(); |
| 407 | switch (slot->type()) { |
| 408 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 409 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 410 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 411 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 412 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 413 | |
| 414 | case Slot::CONTEXT: { |
| 415 | // Follow the context chain if necessary. |
| 416 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 417 | Register context = cp; |
| 418 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 419 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 420 | // Load the closure. |
| 421 | // (All contexts, even 'with' contexts, have a closure, |
| 422 | // and it is the same for all contexts inside a function. |
| 423 | // There is no need to go to the function context first.) |
| 424 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 425 | // Load the function context (which is the incoming, outer context). |
| 426 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 427 | context = tmp; |
| 428 | } |
| 429 | // We may have a 'with' context now. Get the function context. |
| 430 | // (In fact this mov may never be the needed, since the scope analysis |
| 431 | // may not permit a direct context access in this case and thus we are |
| 432 | // always at a function context. However it is safe to dereference be- |
| 433 | // cause the function context of a function context is itself. Before |
| 434 | // deleting this mov we should try to create a counter-example first, |
| 435 | // though...) |
| 436 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 437 | return ContextOperand(tmp, index); |
| 438 | } |
| 439 | |
| 440 | default: |
| 441 | UNREACHABLE(); |
| 442 | return MemOperand(r0, 0); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 447 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 448 | Slot* slot, |
| 449 | Register tmp, |
| 450 | Register tmp2, |
| 451 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 452 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 453 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 454 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 455 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 456 | if (s->num_heap_slots() > 0) { |
| 457 | if (s->calls_eval()) { |
| 458 | // Check that extension is NULL. |
| 459 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 460 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 461 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 462 | } |
| 463 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 464 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 465 | context = tmp; |
| 466 | } |
| 467 | } |
| 468 | // Check that last extension is NULL. |
| 469 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 470 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 471 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 472 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 473 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 477 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 478 | // (partially) translated into branches, or it may have set the condition |
| 479 | // code register. If force_cc is set, the value is forced to set the |
| 480 | // condition code register and no value is pushed. If the condition code |
| 481 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 482 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 483 | void CodeGenerator::LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 484 | JumpTarget* true_target, |
| 485 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 486 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 487 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 488 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 489 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 490 | { CodeGenState new_state(this, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 491 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 492 | |
| 493 | // If we hit a stack overflow, we may not have actually visited |
| 494 | // the expression. In that case, we ensure that we have a |
| 495 | // valid-looking frame state because we will continue to generate |
| 496 | // code as we unwind the C++ stack. |
| 497 | // |
| 498 | // It's possible to have both a stack overflow and a valid frame |
| 499 | // state (eg, a subexpression overflowed, visiting it returned |
| 500 | // with a dummied frame state, and visiting this expression |
| 501 | // returned with a normal-looking state). |
| 502 | if (HasStackOverflow() && |
| 503 | has_valid_frame() && |
| 504 | !has_cc() && |
| 505 | frame_->height() == original_height) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 506 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 507 | true_target->Jump(); |
| 508 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 509 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 510 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 511 | // Convert the TOS value to a boolean in the condition code register. |
| 512 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 513 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 514 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 515 | ASSERT(!has_valid_frame() || |
| 516 | (has_cc() && frame_->height() == original_height) || |
| 517 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 521 | void CodeGenerator::Load(Expression* expr) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 522 | #ifdef DEBUG |
| 523 | int original_height = frame_->height(); |
| 524 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 525 | JumpTarget true_target; |
| 526 | JumpTarget false_target; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 527 | LoadCondition(expr, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 528 | |
| 529 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 530 | // Convert cc_reg_ into a boolean value. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 531 | VirtualFrame::SpilledScope scope(frame_); |
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()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 546 | VirtualFrame::SpilledScope scope(frame_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 547 | // We have at least one condition value that has been "translated" |
| 548 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 549 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 550 | if (frame_ != NULL) { |
| 551 | loaded.Jump(); // Don't lose the current TOS. |
| 552 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 554 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 555 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 556 | true_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 557 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 558 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 560 | // If both "true" and "false" need to be loaded jump across the code for |
| 561 | // "false". |
| 562 | if (both) { |
| 563 | loaded.Jump(); |
| 564 | } |
| 565 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 566 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 567 | false_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 568 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 569 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 570 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 571 | // A value is loaded on all paths reaching this point. |
| 572 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 573 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 574 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 575 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 576 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 580 | void CodeGenerator::LoadGlobal() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 581 | VirtualFrame::SpilledScope spilled_scope(frame_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 582 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 583 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 587 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 588 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 589 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 590 | __ ldr(scratch, |
| 591 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 592 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 596 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 597 | // Special handling of identifiers as subexpressions of typeof. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 598 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 599 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 600 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 601 | // For a global variable we build the property reference |
| 602 | // <global>.<variable> and perform a (regular non-contextual) property |
| 603 | // load to make sure we do not get reference errors. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 604 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 605 | Literal key(variable->name()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 606 | Property property(&global, &key, RelocInfo::kNoPosition); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 607 | Reference ref(this, &property); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 608 | ref.GetValue(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 609 | } else if (variable != NULL && variable->slot() != NULL) { |
| 610 | // For a variable that rewrites to a slot, we signal it is the immediate |
| 611 | // subexpression of a typeof. |
| 612 | LoadFromSlot(variable->slot(), INSIDE_TYPEOF); |
| 613 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 614 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 615 | // Anything else can be handled normally. |
| 616 | LoadAndSpill(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
| 620 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 621 | Reference::Reference(CodeGenerator* cgen, |
| 622 | Expression* expression, |
| 623 | bool persist_after_get) |
| 624 | : cgen_(cgen), |
| 625 | expression_(expression), |
| 626 | type_(ILLEGAL), |
| 627 | persist_after_get_(persist_after_get) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | cgen->LoadReference(this); |
| 629 | } |
| 630 | |
| 631 | |
| 632 | Reference::~Reference() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 633 | ASSERT(is_unloaded() || is_illegal()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 637 | void CodeGenerator::LoadReference(Reference* ref) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 638 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 639 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | Expression* e = ref->expression(); |
| 641 | Property* property = e->AsProperty(); |
| 642 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 643 | |
| 644 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 645 | // The expression is either a property or a variable proxy that rewrites |
| 646 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 647 | LoadAndSpill(property->obj()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 648 | if (property->key()->IsPropertyName()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 649 | ref->set_type(Reference::NAMED); |
| 650 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 651 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 652 | ref->set_type(Reference::KEYED); |
| 653 | } |
| 654 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 655 | // The expression is a variable proxy that does not rewrite to a |
| 656 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 657 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 658 | LoadGlobal(); |
| 659 | ref->set_type(Reference::NAMED); |
| 660 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 661 | ASSERT(var->slot() != NULL); |
| 662 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 663 | } |
| 664 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 665 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 666 | LoadAndSpill(e); |
| 667 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
| 671 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 672 | void CodeGenerator::UnloadReference(Reference* ref) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 673 | int size = ref->size(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 674 | ref->set_unloaded(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 675 | if (size == 0) return; |
| 676 | |
| 677 | // Pop a reference from the stack while preserving TOS. |
| 678 | VirtualFrame::RegisterAllocationScope scope(this); |
| 679 | Comment cmnt(masm_, "[ UnloadReference"); |
| 680 | if (size > 0) { |
| 681 | Register tos = frame_->PopToRegister(); |
| 682 | frame_->Drop(size); |
| 683 | frame_->EmitPush(tos); |
| 684 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 688 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 689 | // register to a boolean in the condition code register. The code |
| 690 | // 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] | 691 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 692 | JumpTarget* false_target) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 693 | VirtualFrame::SpilledScope spilled_scope(frame_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 694 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 695 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 696 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 697 | |
| 698 | // Fast case checks |
| 699 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 700 | // Check if the value is 'false'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 701 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 702 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 703 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 704 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 705 | // Check if the value is 'true'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 706 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 707 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 708 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 709 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 710 | // Check if the value is 'undefined'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 711 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 712 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 713 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 714 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 715 | // Check if the value is a smi. |
| 716 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 717 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 718 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 719 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 720 | |
| 721 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 722 | frame_->EmitPush(r0); |
| 723 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 724 | // Convert the result (r0) to a condition code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 725 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 726 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 727 | |
| 728 | cc_reg_ = ne; |
| 729 | } |
| 730 | |
| 731 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 732 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 733 | OverwriteMode overwrite_mode, |
| 734 | int constant_rhs) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 735 | VirtualFrame::SpilledScope spilled_scope(frame_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 736 | // sp[0] : y |
| 737 | // sp[1] : x |
| 738 | // result : r0 |
| 739 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 740 | // Stub is entered with a call: 'return address' is in lr. |
| 741 | switch (op) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 742 | case Token::ADD: |
| 743 | case Token::SUB: |
| 744 | case Token::MUL: |
| 745 | case Token::DIV: |
| 746 | case Token::MOD: |
| 747 | case Token::BIT_OR: |
| 748 | case Token::BIT_AND: |
| 749 | case Token::BIT_XOR: |
| 750 | case Token::SHL: |
| 751 | case Token::SHR: |
| 752 | case Token::SAR: { |
| 753 | frame_->EmitPop(r0); // r0 : y |
| 754 | frame_->EmitPop(r1); // r1 : x |
| 755 | GenericBinaryOpStub stub(op, overwrite_mode, r1, r0, constant_rhs); |
| 756 | frame_->CallStub(&stub, 0); |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | case Token::COMMA: |
| 761 | frame_->EmitPop(r0); |
| 762 | // Simply discard left value. |
| 763 | frame_->Drop(); |
| 764 | break; |
| 765 | |
| 766 | default: |
| 767 | // Other cases should have been handled before this point. |
| 768 | UNREACHABLE(); |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | |
| 774 | void CodeGenerator::VirtualFrameBinaryOperation(Token::Value op, |
| 775 | OverwriteMode overwrite_mode, |
| 776 | int constant_rhs) { |
| 777 | // top of virtual frame: y |
| 778 | // 2nd elt. on virtual frame : x |
| 779 | // result : top of virtual frame |
| 780 | |
| 781 | // Stub is entered with a call: 'return address' is in lr. |
| 782 | switch (op) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 783 | case Token::ADD: // fall through. |
| 784 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 785 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 786 | case Token::DIV: |
| 787 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 788 | case Token::BIT_OR: |
| 789 | case Token::BIT_AND: |
| 790 | case Token::BIT_XOR: |
| 791 | case Token::SHL: |
| 792 | case Token::SHR: |
| 793 | case Token::SAR: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 794 | Register rhs = frame_->PopToRegister(); |
| 795 | Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register. |
| 796 | { |
| 797 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 798 | GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs); |
| 799 | frame_->CallStub(&stub, 0); |
| 800 | } |
| 801 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 802 | break; |
| 803 | } |
| 804 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 805 | case Token::COMMA: { |
| 806 | Register scratch = frame_->PopToRegister(); |
| 807 | // Simply discard left value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 808 | frame_->Drop(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 809 | frame_->EmitPush(scratch); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 810 | break; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 811 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 812 | |
| 813 | default: |
| 814 | // Other cases should have been handled before this point. |
| 815 | UNREACHABLE(); |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 821 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 822 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 823 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 824 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 825 | bool reversed, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 826 | OverwriteMode overwrite_mode, |
| 827 | Register tos) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 828 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 829 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 830 | reversed_(reversed), |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 831 | overwrite_mode_(overwrite_mode), |
| 832 | tos_register_(tos) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 833 | set_comment("[ DeferredInlinedSmiOperation"); |
| 834 | } |
| 835 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 836 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 837 | |
| 838 | private: |
| 839 | Token::Value op_; |
| 840 | int value_; |
| 841 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 842 | OverwriteMode overwrite_mode_; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 843 | Register tos_register_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 844 | }; |
| 845 | |
| 846 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 847 | void DeferredInlineSmiOperation::Generate() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 848 | Register lhs = r1; |
| 849 | Register rhs = r0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 850 | switch (op_) { |
| 851 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 852 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 853 | if (reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 854 | __ sub(r0, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 855 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 856 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 857 | __ sub(r1, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 858 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 859 | } |
| 860 | break; |
| 861 | } |
| 862 | |
| 863 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 864 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 865 | if (reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 866 | __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 867 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 868 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 869 | __ add(r1, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 870 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 871 | } |
| 872 | break; |
| 873 | } |
| 874 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 875 | // For these operations there is no optimistic operation that needs to be |
| 876 | // reverted. |
| 877 | case Token::MUL: |
| 878 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 879 | case Token::BIT_OR: |
| 880 | case Token::BIT_XOR: |
| 881 | case Token::BIT_AND: { |
| 882 | if (reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 883 | if (tos_register_.is(r0)) { |
| 884 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 885 | } else { |
| 886 | ASSERT(tos_register_.is(r1)); |
| 887 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 888 | lhs = r0; |
| 889 | rhs = r1; |
| 890 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 891 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 892 | if (tos_register_.is(r1)) { |
| 893 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 894 | } else { |
| 895 | ASSERT(tos_register_.is(r0)); |
| 896 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 897 | lhs = r0; |
| 898 | rhs = r1; |
| 899 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 900 | } |
| 901 | break; |
| 902 | } |
| 903 | |
| 904 | case Token::SHL: |
| 905 | case Token::SHR: |
| 906 | case Token::SAR: { |
| 907 | if (!reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 908 | if (tos_register_.is(r1)) { |
| 909 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 910 | } else { |
| 911 | ASSERT(tos_register_.is(r0)); |
| 912 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 913 | lhs = r0; |
| 914 | rhs = r1; |
| 915 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 916 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 917 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 918 | } |
| 919 | break; |
| 920 | } |
| 921 | |
| 922 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 923 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 924 | UNREACHABLE(); |
| 925 | break; |
| 926 | } |
| 927 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 928 | GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 929 | __ CallStub(&stub); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 930 | // The generic stub returns its value in r0, but that's not |
| 931 | // necessarily what we want. We want whatever the inlined code |
| 932 | // expected, which is that the answer is in the same register as |
| 933 | // the operand was. |
| 934 | __ Move(tos_register_, r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 938 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 939 | x &= x - 1; |
| 940 | return (x & (x - 1)) == 0; |
| 941 | } |
| 942 | |
| 943 | |
| 944 | // Returns the index of the lowest bit set. |
| 945 | static int BitPosition(unsigned x) { |
| 946 | int bit_posn = 0; |
| 947 | while ((x & 0xf) == 0) { |
| 948 | bit_posn += 4; |
| 949 | x >>= 4; |
| 950 | } |
| 951 | while ((x & 1) == 0) { |
| 952 | bit_posn++; |
| 953 | x >>= 1; |
| 954 | } |
| 955 | return bit_posn; |
| 956 | } |
| 957 | |
| 958 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 959 | void CodeGenerator::VirtualFrameSmiOperation(Token::Value op, |
| 960 | Handle<Object> value, |
| 961 | bool reversed, |
| 962 | OverwriteMode mode) { |
| 963 | int int_value = Smi::cast(*value)->value(); |
| 964 | |
| 965 | bool something_to_inline; |
| 966 | switch (op) { |
| 967 | case Token::ADD: |
| 968 | case Token::SUB: |
| 969 | case Token::BIT_AND: |
| 970 | case Token::BIT_OR: |
| 971 | case Token::BIT_XOR: { |
| 972 | something_to_inline = true; |
| 973 | break; |
| 974 | } |
| 975 | case Token::SHL: |
| 976 | case Token::SHR: |
| 977 | case Token::SAR: { |
| 978 | if (reversed) { |
| 979 | something_to_inline = false; |
| 980 | } else { |
| 981 | something_to_inline = true; |
| 982 | } |
| 983 | break; |
| 984 | } |
| 985 | case Token::MOD: { |
| 986 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 987 | something_to_inline = false; |
| 988 | } else { |
| 989 | something_to_inline = true; |
| 990 | } |
| 991 | break; |
| 992 | } |
| 993 | case Token::MUL: { |
| 994 | if (!IsEasyToMultiplyBy(int_value)) { |
| 995 | something_to_inline = false; |
| 996 | } else { |
| 997 | something_to_inline = true; |
| 998 | } |
| 999 | break; |
| 1000 | } |
| 1001 | default: { |
| 1002 | something_to_inline = false; |
| 1003 | break; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | if (!something_to_inline) { |
| 1008 | if (!reversed) { |
| 1009 | // Push the rhs onto the virtual frame by putting it in a TOS register. |
| 1010 | Register rhs = frame_->GetTOSRegister(); |
| 1011 | __ mov(rhs, Operand(value)); |
| 1012 | frame_->EmitPush(rhs); |
| 1013 | VirtualFrameBinaryOperation(op, mode, int_value); |
| 1014 | } else { |
| 1015 | // Pop the rhs, then push lhs and rhs in the right order. Only performs |
| 1016 | // at most one pop, the rest takes place in TOS registers. |
| 1017 | Register lhs = frame_->GetTOSRegister(); // Get reg for pushing. |
| 1018 | Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this. |
| 1019 | __ mov(lhs, Operand(value)); |
| 1020 | frame_->EmitPush(lhs); |
| 1021 | frame_->EmitPush(rhs); |
| 1022 | VirtualFrameBinaryOperation(op, mode, kUnknownIntValue); |
| 1023 | } |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | // We move the top of stack to a register (normally no move is invoved). |
| 1028 | Register tos = frame_->PopToRegister(); |
| 1029 | // All other registers are spilled. The deferred code expects one argument |
| 1030 | // in a register and all other values are flushed to the stack. The |
| 1031 | // answer is returned in the same register that the top of stack argument was |
| 1032 | // in. |
| 1033 | frame_->SpillAll(); |
| 1034 | |
| 1035 | switch (op) { |
| 1036 | case Token::ADD: { |
| 1037 | DeferredCode* deferred = |
| 1038 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1039 | |
| 1040 | __ add(tos, tos, Operand(value), SetCC); |
| 1041 | deferred->Branch(vs); |
| 1042 | __ tst(tos, Operand(kSmiTagMask)); |
| 1043 | deferred->Branch(ne); |
| 1044 | deferred->BindExit(); |
| 1045 | frame_->EmitPush(tos); |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | case Token::SUB: { |
| 1050 | DeferredCode* deferred = |
| 1051 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1052 | |
| 1053 | if (reversed) { |
| 1054 | __ rsb(tos, tos, Operand(value), SetCC); |
| 1055 | } else { |
| 1056 | __ sub(tos, tos, Operand(value), SetCC); |
| 1057 | } |
| 1058 | deferred->Branch(vs); |
| 1059 | __ tst(tos, Operand(kSmiTagMask)); |
| 1060 | deferred->Branch(ne); |
| 1061 | deferred->BindExit(); |
| 1062 | frame_->EmitPush(tos); |
| 1063 | break; |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | case Token::BIT_OR: |
| 1068 | case Token::BIT_XOR: |
| 1069 | case Token::BIT_AND: { |
| 1070 | DeferredCode* deferred = |
| 1071 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1072 | __ tst(tos, Operand(kSmiTagMask)); |
| 1073 | deferred->Branch(ne); |
| 1074 | switch (op) { |
| 1075 | case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break; |
| 1076 | case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break; |
| 1077 | case Token::BIT_AND: __ and_(tos, tos, Operand(value)); break; |
| 1078 | default: UNREACHABLE(); |
| 1079 | } |
| 1080 | deferred->BindExit(); |
| 1081 | frame_->EmitPush(tos); |
| 1082 | break; |
| 1083 | } |
| 1084 | |
| 1085 | case Token::SHL: |
| 1086 | case Token::SHR: |
| 1087 | case Token::SAR: { |
| 1088 | ASSERT(!reversed); |
| 1089 | Register scratch = VirtualFrame::scratch0(); |
| 1090 | Register scratch2 = VirtualFrame::scratch1(); |
| 1091 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 1092 | DeferredCode* deferred = |
| 1093 | new DeferredInlineSmiOperation(op, shift_value, false, mode, tos); |
| 1094 | __ tst(tos, Operand(kSmiTagMask)); |
| 1095 | deferred->Branch(ne); |
| 1096 | __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // remove tags |
| 1097 | switch (op) { |
| 1098 | case Token::SHL: { |
| 1099 | if (shift_value != 0) { |
| 1100 | __ mov(scratch, Operand(scratch, LSL, shift_value)); |
| 1101 | } |
| 1102 | // check that the *signed* result fits in a smi |
| 1103 | __ add(scratch2, scratch, Operand(0x40000000), SetCC); |
| 1104 | deferred->Branch(mi); |
| 1105 | break; |
| 1106 | } |
| 1107 | case Token::SHR: { |
| 1108 | // LSR by immediate 0 means shifting 32 bits. |
| 1109 | if (shift_value != 0) { |
| 1110 | __ mov(scratch, Operand(scratch, LSR, shift_value)); |
| 1111 | } |
| 1112 | // check that the *unsigned* result fits in a smi |
| 1113 | // neither of the two high-order bits can be set: |
| 1114 | // - 0x80000000: high bit would be lost when smi tagging |
| 1115 | // - 0x40000000: this number would convert to negative when |
| 1116 | // smi tagging these two cases can only happen with shifts |
| 1117 | // by 0 or 1 when handed a valid smi |
| 1118 | __ tst(scratch, Operand(0xc0000000)); |
| 1119 | deferred->Branch(ne); |
| 1120 | break; |
| 1121 | } |
| 1122 | case Token::SAR: { |
| 1123 | if (shift_value != 0) { |
| 1124 | // ASR by immediate 0 means shifting 32 bits. |
| 1125 | __ mov(scratch, Operand(scratch, ASR, shift_value)); |
| 1126 | } |
| 1127 | break; |
| 1128 | } |
| 1129 | default: UNREACHABLE(); |
| 1130 | } |
| 1131 | __ mov(tos, Operand(scratch, LSL, kSmiTagSize)); |
| 1132 | deferred->BindExit(); |
| 1133 | frame_->EmitPush(tos); |
| 1134 | break; |
| 1135 | } |
| 1136 | |
| 1137 | case Token::MOD: { |
| 1138 | ASSERT(!reversed); |
| 1139 | ASSERT(int_value >= 2); |
| 1140 | ASSERT(IsPowerOf2(int_value)); |
| 1141 | DeferredCode* deferred = |
| 1142 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1143 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1144 | __ tst(tos, Operand(mask)); |
| 1145 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1146 | mask = (int_value << kSmiTagSize) - 1; |
| 1147 | __ and_(tos, tos, Operand(mask)); |
| 1148 | deferred->BindExit(); |
| 1149 | frame_->EmitPush(tos); |
| 1150 | break; |
| 1151 | } |
| 1152 | |
| 1153 | case Token::MUL: { |
| 1154 | ASSERT(IsEasyToMultiplyBy(int_value)); |
| 1155 | DeferredCode* deferred = |
| 1156 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1157 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1158 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1159 | unsigned mask = 0x80000000u; |
| 1160 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1161 | mask |= mask >> 1; |
| 1162 | } |
| 1163 | mask |= kSmiTagMask; |
| 1164 | // This does a single mask that checks for a too high value in a |
| 1165 | // conservative way and for a non-Smi. It also filters out negative |
| 1166 | // numbers, unfortunately, but since this code is inline we prefer |
| 1167 | // brevity to comprehensiveness. |
| 1168 | __ tst(tos, Operand(mask)); |
| 1169 | deferred->Branch(ne); |
| 1170 | MultiplyByKnownInt(masm_, tos, tos, int_value); |
| 1171 | deferred->BindExit(); |
| 1172 | frame_->EmitPush(tos); |
| 1173 | break; |
| 1174 | } |
| 1175 | |
| 1176 | default: |
| 1177 | UNREACHABLE(); |
| 1178 | break; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1183 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1184 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1185 | bool reversed, |
| 1186 | OverwriteMode mode) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1187 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1188 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 1189 | // some possible smi operations (like + and -) when (at least) one |
| 1190 | // of the operands is a literal smi. With this optimization, the |
| 1191 | // performance of the system is increased by ~15%, and the generated |
| 1192 | // code size is increased by ~1% (measured on a combination of |
| 1193 | // different benchmarks). |
| 1194 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1195 | // sp[0] : operand |
| 1196 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1197 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1198 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1199 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1200 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1201 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1202 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1203 | switch (op) { |
| 1204 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1205 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1206 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1207 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1208 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1209 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1210 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1211 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1212 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1213 | break; |
| 1214 | } |
| 1215 | |
| 1216 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1217 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1218 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1219 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1220 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1221 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1222 | } else { |
| 1223 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1224 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1225 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1226 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1227 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1228 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1229 | break; |
| 1230 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1231 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1232 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1233 | case Token::BIT_OR: |
| 1234 | case Token::BIT_XOR: |
| 1235 | case Token::BIT_AND: { |
| 1236 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1237 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1238 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1239 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1240 | switch (op) { |
| 1241 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 1242 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 1243 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 1244 | default: UNREACHABLE(); |
| 1245 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1246 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1247 | break; |
| 1248 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1249 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1250 | case Token::SHL: |
| 1251 | case Token::SHR: |
| 1252 | case Token::SAR: { |
| 1253 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1254 | something_to_inline = false; |
| 1255 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1256 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1257 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 1258 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1259 | new DeferredInlineSmiOperation(op, shift_value, false, mode, r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1260 | __ tst(r0, Operand(kSmiTagMask)); |
| 1261 | deferred->Branch(ne); |
| 1262 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 1263 | switch (op) { |
| 1264 | case Token::SHL: { |
| 1265 | if (shift_value != 0) { |
| 1266 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 1267 | } |
| 1268 | // check that the *unsigned* result fits in a smi |
| 1269 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 1270 | deferred->Branch(mi); |
| 1271 | break; |
| 1272 | } |
| 1273 | case Token::SHR: { |
| 1274 | // LSR by immediate 0 means shifting 32 bits. |
| 1275 | if (shift_value != 0) { |
| 1276 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 1277 | } |
| 1278 | // check that the *unsigned* result fits in a smi |
| 1279 | // neither of the two high-order bits can be set: |
| 1280 | // - 0x80000000: high bit would be lost when smi tagging |
| 1281 | // - 0x40000000: this number would convert to negative when |
| 1282 | // smi tagging these two cases can only happen with shifts |
| 1283 | // by 0 or 1 when handed a valid smi |
| 1284 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 1285 | deferred->Branch(ne); |
| 1286 | break; |
| 1287 | } |
| 1288 | case Token::SAR: { |
| 1289 | if (shift_value != 0) { |
| 1290 | // ASR by immediate 0 means shifting 32 bits. |
| 1291 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 1292 | } |
| 1293 | break; |
| 1294 | } |
| 1295 | default: UNREACHABLE(); |
| 1296 | } |
| 1297 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 1298 | deferred->BindExit(); |
| 1299 | break; |
| 1300 | } |
| 1301 | |
| 1302 | case Token::MOD: { |
| 1303 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 1304 | something_to_inline = false; |
| 1305 | break; |
| 1306 | } |
| 1307 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1308 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1309 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1310 | __ tst(r0, Operand(mask)); |
| 1311 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1312 | mask = (int_value << kSmiTagSize) - 1; |
| 1313 | __ and_(r0, r0, Operand(mask)); |
| 1314 | deferred->BindExit(); |
| 1315 | break; |
| 1316 | } |
| 1317 | |
| 1318 | case Token::MUL: { |
| 1319 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1320 | something_to_inline = false; |
| 1321 | break; |
| 1322 | } |
| 1323 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1324 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1325 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1326 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1327 | unsigned mask = 0x80000000u; |
| 1328 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1329 | mask |= mask >> 1; |
| 1330 | } |
| 1331 | mask |= kSmiTagMask; |
| 1332 | // This does a single mask that checks for a too high value in a |
| 1333 | // conservative way and for a non-Smi. It also filters out negative |
| 1334 | // numbers, unfortunately, but since this code is inline we prefer |
| 1335 | // brevity to comprehensiveness. |
| 1336 | __ tst(r0, Operand(mask)); |
| 1337 | deferred->Branch(ne); |
| 1338 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 1339 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1340 | break; |
| 1341 | } |
| 1342 | |
| 1343 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1344 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1345 | break; |
| 1346 | } |
| 1347 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1348 | if (!something_to_inline) { |
| 1349 | if (!reversed) { |
| 1350 | frame_->EmitPush(r0); |
| 1351 | __ mov(r0, Operand(value)); |
| 1352 | frame_->EmitPush(r0); |
| 1353 | GenericBinaryOperation(op, mode, int_value); |
| 1354 | } else { |
| 1355 | __ mov(ip, Operand(value)); |
| 1356 | frame_->EmitPush(ip); |
| 1357 | frame_->EmitPush(r0); |
| 1358 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1359 | } |
| 1360 | } |
| 1361 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1362 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1366 | void CodeGenerator::Comparison(Condition cc, |
| 1367 | Expression* left, |
| 1368 | Expression* right, |
| 1369 | bool strict) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1370 | VirtualFrame::RegisterAllocationScope scope(this); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1371 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1372 | if (left != NULL) Load(left); |
| 1373 | if (right != NULL) Load(right); |
| 1374 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1375 | // sp[0] : y |
| 1376 | // sp[1] : x |
| 1377 | // result : cc register |
| 1378 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1379 | // Strict only makes sense for equality comparisons. |
| 1380 | ASSERT(!strict || cc == eq); |
| 1381 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1382 | Register lhs; |
| 1383 | Register rhs; |
| 1384 | |
| 1385 | // We load the top two stack positions into registers chosen by the virtual |
| 1386 | // frame. This should keep the register shuffling to a minimum. |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1387 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1388 | if (cc == gt || cc == le) { |
| 1389 | cc = ReverseCondition(cc); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1390 | lhs = frame_->PopToRegister(); |
| 1391 | rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again! |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1392 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1393 | rhs = frame_->PopToRegister(); |
| 1394 | lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again! |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1395 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1396 | |
| 1397 | ASSERT(rhs.is(r0) || rhs.is(r1)); |
| 1398 | ASSERT(lhs.is(r0) || lhs.is(r1)); |
| 1399 | |
| 1400 | // Now we have the two sides in r0 and r1. We flush any other registers |
| 1401 | // because the stub doesn't know about register allocation. |
| 1402 | frame_->SpillAll(); |
| 1403 | Register scratch = VirtualFrame::scratch0(); |
| 1404 | __ orr(scratch, lhs, Operand(rhs)); |
| 1405 | __ tst(scratch, Operand(kSmiTagMask)); |
| 1406 | JumpTarget smi; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1407 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1408 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1409 | // Perform non-smi comparison by stub. |
| 1410 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1411 | // We call with 0 args because there are 0 on the stack. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1412 | if (!rhs.is(r0)) { |
| 1413 | __ Swap(rhs, lhs, ip); |
| 1414 | } |
| 1415 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1416 | CompareStub stub(cc, strict); |
| 1417 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1418 | __ cmp(r0, Operand(0)); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1419 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1420 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1421 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1422 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1423 | smi.Bind(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1424 | __ cmp(lhs, Operand(rhs)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1425 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1426 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1427 | cc_reg_ = cc; |
| 1428 | } |
| 1429 | |
| 1430 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1431 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1432 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1433 | CallFunctionFlags flags, |
| 1434 | int position) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1435 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1436 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1437 | int arg_count = args->length(); |
| 1438 | for (int i = 0; i < arg_count; i++) { |
| 1439 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1440 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1441 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1442 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1443 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1444 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1445 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1446 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1447 | CallFunctionStub call_function(arg_count, in_loop, flags); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1448 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1451 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1452 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1456 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1457 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1458 | ASSERT(has_cc()); |
| 1459 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1460 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1461 | cc_reg_ = al; |
| 1462 | } |
| 1463 | |
| 1464 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1465 | void CodeGenerator::CheckStack() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1466 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1467 | Comment cmnt(masm_, "[ check stack"); |
| 1468 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1469 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1470 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1471 | // gives a return address 12 bytes down. |
| 1472 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1473 | masm_->cmp(sp, Operand(ip)); |
| 1474 | StackCheckStub stub; |
| 1475 | // Call the stub if lower. |
| 1476 | masm_->mov(pc, |
| 1477 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1478 | RelocInfo::CODE_TARGET), |
| 1479 | LeaveCC, |
| 1480 | lo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1484 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1485 | #ifdef DEBUG |
| 1486 | int original_height = frame_->height(); |
| 1487 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1488 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1489 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1490 | VisitAndSpill(statements->at(i)); |
| 1491 | } |
| 1492 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1493 | } |
| 1494 | |
| 1495 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1496 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1497 | #ifdef DEBUG |
| 1498 | int original_height = frame_->height(); |
| 1499 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1500 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1501 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1502 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1503 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1504 | VisitStatementsAndSpill(node->statements()); |
| 1505 | if (node->break_target()->is_linked()) { |
| 1506 | node->break_target()->Bind(); |
| 1507 | } |
| 1508 | node->break_target()->Unuse(); |
| 1509 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1513 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1514 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1515 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1516 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1517 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1518 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1519 | frame_->EmitPush(r0); |
| 1520 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1521 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1525 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1526 | #ifdef DEBUG |
| 1527 | int original_height = frame_->height(); |
| 1528 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1529 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1530 | Comment cmnt(masm_, "[ Declaration"); |
| 1531 | Variable* var = node->proxy()->var(); |
| 1532 | ASSERT(var != NULL); // must have been resolved |
| 1533 | Slot* slot = var->slot(); |
| 1534 | |
| 1535 | // If it was not possible to allocate the variable at compile time, |
| 1536 | // we need to "declare" it at runtime to make sure it actually |
| 1537 | // exists in the local context. |
| 1538 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1539 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1540 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1541 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1542 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1543 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1544 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1545 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1546 | // Declaration nodes are always declared in only two modes. |
| 1547 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1548 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1549 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1550 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1551 | // Push initial value, if any. |
| 1552 | // Note: For variables we must not push an initial value (such as |
| 1553 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1554 | // must not destroy the current value. |
| 1555 | if (node->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1556 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1557 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1558 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1559 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1560 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1561 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1562 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1563 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1564 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1565 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1566 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1567 | return; |
| 1568 | } |
| 1569 | |
| 1570 | ASSERT(!var->is_global()); |
| 1571 | |
| 1572 | // If we have a function or a constant, we need to initialize the variable. |
| 1573 | Expression* val = NULL; |
| 1574 | if (node->mode() == Variable::CONST) { |
| 1575 | val = new Literal(Factory::the_hole_value()); |
| 1576 | } else { |
| 1577 | val = node->fun(); // NULL if we don't have a function |
| 1578 | } |
| 1579 | |
| 1580 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1581 | { |
| 1582 | // Set initial value. |
| 1583 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1584 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1585 | target.SetValue(NOT_CONST_INIT); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1586 | } |
| 1587 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1588 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1589 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1590 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1594 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1595 | #ifdef DEBUG |
| 1596 | int original_height = frame_->height(); |
| 1597 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1598 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1599 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1600 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1601 | Expression* expression = node->expression(); |
| 1602 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1603 | LoadAndSpill(expression); |
| 1604 | frame_->Drop(); |
| 1605 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1609 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1610 | #ifdef DEBUG |
| 1611 | int original_height = frame_->height(); |
| 1612 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1613 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1614 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1615 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1616 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1617 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1621 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1622 | #ifdef DEBUG |
| 1623 | int original_height = frame_->height(); |
| 1624 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1625 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1626 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1627 | // Generate different code depending on which parts of the if statement |
| 1628 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1629 | bool has_then_stm = node->HasThenStatement(); |
| 1630 | bool has_else_stm = node->HasElseStatement(); |
| 1631 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1632 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1633 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1634 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1635 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1636 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1637 | JumpTarget then; |
| 1638 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1639 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1640 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1641 | if (frame_ != NULL) { |
| 1642 | Branch(false, &else_); |
| 1643 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1644 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1645 | if (frame_ != NULL || then.is_linked()) { |
| 1646 | then.Bind(); |
| 1647 | VisitAndSpill(node->then_statement()); |
| 1648 | } |
| 1649 | if (frame_ != NULL) { |
| 1650 | exit.Jump(); |
| 1651 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1652 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1653 | if (else_.is_linked()) { |
| 1654 | else_.Bind(); |
| 1655 | VisitAndSpill(node->else_statement()); |
| 1656 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1657 | |
| 1658 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1659 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1660 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1661 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1662 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1663 | LoadConditionAndSpill(node->condition(), &then, &exit, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1664 | if (frame_ != NULL) { |
| 1665 | Branch(false, &exit); |
| 1666 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1667 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1668 | if (frame_ != NULL || then.is_linked()) { |
| 1669 | then.Bind(); |
| 1670 | VisitAndSpill(node->then_statement()); |
| 1671 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1672 | |
| 1673 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1674 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1675 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1676 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1677 | // if (!cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1678 | LoadConditionAndSpill(node->condition(), &exit, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1679 | if (frame_ != NULL) { |
| 1680 | Branch(true, &exit); |
| 1681 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1682 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1683 | if (frame_ != NULL || else_.is_linked()) { |
| 1684 | else_.Bind(); |
| 1685 | VisitAndSpill(node->else_statement()); |
| 1686 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1687 | |
| 1688 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1689 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1690 | ASSERT(!has_then_stm && !has_else_stm); |
| 1691 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1692 | LoadConditionAndSpill(node->condition(), &exit, &exit, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1693 | if (frame_ != NULL) { |
| 1694 | if (has_cc()) { |
| 1695 | cc_reg_ = al; |
| 1696 | } else { |
| 1697 | frame_->Drop(); |
| 1698 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1703 | if (exit.is_linked()) { |
| 1704 | exit.Bind(); |
| 1705 | } |
| 1706 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1710 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1711 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1712 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1713 | CodeForStatementPosition(node); |
| 1714 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1718 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1719 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1720 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1721 | CodeForStatementPosition(node); |
| 1722 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1726 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1727 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1728 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1729 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1730 | CodeForStatementPosition(node); |
| 1731 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1732 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1733 | frame_->EmitPop(r0); |
| 1734 | function_return_.Jump(); |
| 1735 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1736 | // Pop the result from the frame and prepare the frame for |
| 1737 | // returning thus making it easier to merge. |
| 1738 | frame_->EmitPop(r0); |
| 1739 | frame_->PrepareForReturn(); |
| 1740 | |
| 1741 | function_return_.Jump(); |
| 1742 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1746 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1747 | #ifdef DEBUG |
| 1748 | int original_height = frame_->height(); |
| 1749 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1750 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1751 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1752 | CodeForStatementPosition(node); |
| 1753 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1754 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1755 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1756 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1757 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1758 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1759 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1760 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1761 | __ cmp(r0, Operand(cp)); |
| 1762 | verified_true.Branch(eq); |
| 1763 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1764 | verified_true.Bind(); |
| 1765 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1766 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1767 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1768 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1772 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1773 | #ifdef DEBUG |
| 1774 | int original_height = frame_->height(); |
| 1775 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1776 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1777 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1778 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1779 | // Pop context. |
| 1780 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1781 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1782 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1783 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1787 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1788 | #ifdef DEBUG |
| 1789 | int original_height = frame_->height(); |
| 1790 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1791 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1792 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1793 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1794 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1795 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1796 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1797 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1798 | JumpTarget next_test; |
| 1799 | JumpTarget fall_through; |
| 1800 | JumpTarget default_entry; |
| 1801 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1802 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1803 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1804 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1805 | |
| 1806 | for (int i = 0; i < length; i++) { |
| 1807 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1808 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1809 | // Remember the default clause and compile it at the end. |
| 1810 | default_clause = clause; |
| 1811 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1814 | Comment cmnt(masm_, "[ Case clause"); |
| 1815 | // Compile the test. |
| 1816 | next_test.Bind(); |
| 1817 | next_test.Unuse(); |
| 1818 | // Duplicate TOS. |
| 1819 | __ ldr(r0, frame_->Top()); |
| 1820 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1821 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1822 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1823 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1824 | // Before entering the body from the test, remove the switch value from |
| 1825 | // the stack. |
| 1826 | frame_->Drop(); |
| 1827 | |
| 1828 | // Label the body so that fall through is enabled. |
| 1829 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1830 | default_exit.Bind(); |
| 1831 | } else { |
| 1832 | fall_through.Bind(); |
| 1833 | fall_through.Unuse(); |
| 1834 | } |
| 1835 | VisitStatementsAndSpill(clause->statements()); |
| 1836 | |
| 1837 | // If control flow can fall through from the body, jump to the next body |
| 1838 | // or the end of the statement. |
| 1839 | if (frame_ != NULL) { |
| 1840 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1841 | default_entry.Jump(); |
| 1842 | } else { |
| 1843 | fall_through.Jump(); |
| 1844 | } |
| 1845 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1848 | // The final "test" removes the switch value. |
| 1849 | next_test.Bind(); |
| 1850 | frame_->Drop(); |
| 1851 | |
| 1852 | // If there is a default clause, compile it. |
| 1853 | if (default_clause != NULL) { |
| 1854 | Comment cmnt(masm_, "[ Default clause"); |
| 1855 | default_entry.Bind(); |
| 1856 | VisitStatementsAndSpill(default_clause->statements()); |
| 1857 | // If control flow can fall out of the default and there is a case after |
| 1858 | // it, jup to that case's body. |
| 1859 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1860 | default_exit.Jump(); |
| 1861 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1862 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1863 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1864 | if (fall_through.is_linked()) { |
| 1865 | fall_through.Bind(); |
| 1866 | } |
| 1867 | |
| 1868 | if (node->break_target()->is_linked()) { |
| 1869 | node->break_target()->Bind(); |
| 1870 | } |
| 1871 | node->break_target()->Unuse(); |
| 1872 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1876 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1877 | #ifdef DEBUG |
| 1878 | int original_height = frame_->height(); |
| 1879 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1880 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1881 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1882 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1883 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1884 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1885 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1886 | // Label the top of the loop for the backward CFG edge. If the test |
| 1887 | // is always true we can use the continue target, and if the test is |
| 1888 | // always false there is no need. |
| 1889 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1890 | switch (info) { |
| 1891 | case ALWAYS_TRUE: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1892 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1893 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1894 | break; |
| 1895 | case ALWAYS_FALSE: |
| 1896 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1897 | break; |
| 1898 | case DONT_KNOW: |
| 1899 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1900 | body.Bind(); |
| 1901 | break; |
| 1902 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1903 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1904 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1905 | VisitAndSpill(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1906 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1907 | // Compile the test. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1908 | switch (info) { |
| 1909 | case ALWAYS_TRUE: |
| 1910 | // If control can fall off the end of the body, jump back to the |
| 1911 | // top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1912 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1913 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1914 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1915 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1916 | case ALWAYS_FALSE: |
| 1917 | // If we have a continue in the body, we only have to bind its |
| 1918 | // jump target. |
| 1919 | if (node->continue_target()->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1920 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1921 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1922 | break; |
| 1923 | case DONT_KNOW: |
| 1924 | // We have to compile the test expression if it can be reached by |
| 1925 | // control flow falling out of the body or via continue. |
| 1926 | if (node->continue_target()->is_linked()) { |
| 1927 | node->continue_target()->Bind(); |
| 1928 | } |
| 1929 | if (has_valid_frame()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1930 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 1931 | CodeForDoWhileConditionPosition(node); |
| 1932 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1933 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1934 | // A invalid frame here indicates that control did not |
| 1935 | // fall out of the test expression. |
| 1936 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | } |
| 1941 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1942 | if (node->break_target()->is_linked()) { |
| 1943 | node->break_target()->Bind(); |
| 1944 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1945 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1946 | } |
| 1947 | |
| 1948 | |
| 1949 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 1950 | #ifdef DEBUG |
| 1951 | int original_height = frame_->height(); |
| 1952 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1953 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1954 | Comment cmnt(masm_, "[ WhileStatement"); |
| 1955 | CodeForStatementPosition(node); |
| 1956 | |
| 1957 | // If the test is never true and has no side effects there is no need |
| 1958 | // to compile the test or body. |
| 1959 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1960 | if (info == ALWAYS_FALSE) return; |
| 1961 | |
| 1962 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1963 | |
| 1964 | // Label the top of the loop with the continue target for the backward |
| 1965 | // CFG edge. |
| 1966 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1967 | node->continue_target()->Bind(); |
| 1968 | |
| 1969 | if (info == DONT_KNOW) { |
| 1970 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1971 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1972 | if (has_valid_frame()) { |
| 1973 | // A NULL frame indicates that control did not fall out of the |
| 1974 | // test expression. |
| 1975 | Branch(false, node->break_target()); |
| 1976 | } |
| 1977 | if (has_valid_frame() || body.is_linked()) { |
| 1978 | body.Bind(); |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | if (has_valid_frame()) { |
| 1983 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1984 | VisitAndSpill(node->body()); |
| 1985 | |
| 1986 | // If control flow can fall out of the body, jump back to the top. |
| 1987 | if (has_valid_frame()) { |
| 1988 | node->continue_target()->Jump(); |
| 1989 | } |
| 1990 | } |
| 1991 | if (node->break_target()->is_linked()) { |
| 1992 | node->break_target()->Bind(); |
| 1993 | } |
| 1994 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1995 | } |
| 1996 | |
| 1997 | |
| 1998 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 1999 | #ifdef DEBUG |
| 2000 | int original_height = frame_->height(); |
| 2001 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2002 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2003 | Comment cmnt(masm_, "[ ForStatement"); |
| 2004 | CodeForStatementPosition(node); |
| 2005 | if (node->init() != NULL) { |
| 2006 | VisitAndSpill(node->init()); |
| 2007 | } |
| 2008 | |
| 2009 | // If the test is never true there is no need to compile the test or |
| 2010 | // body. |
| 2011 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 2012 | if (info == ALWAYS_FALSE) return; |
| 2013 | |
| 2014 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 2015 | |
| 2016 | // If there is no update statement, label the top of the loop with the |
| 2017 | // continue target, otherwise with the loop target. |
| 2018 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 2019 | if (node->next() == NULL) { |
| 2020 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 2021 | node->continue_target()->Bind(); |
| 2022 | } else { |
| 2023 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 2024 | loop.Bind(); |
| 2025 | } |
| 2026 | |
| 2027 | // If the test is always true, there is no need to compile it. |
| 2028 | if (info == DONT_KNOW) { |
| 2029 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2030 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2031 | if (has_valid_frame()) { |
| 2032 | Branch(false, node->break_target()); |
| 2033 | } |
| 2034 | if (has_valid_frame() || body.is_linked()) { |
| 2035 | body.Bind(); |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | if (has_valid_frame()) { |
| 2040 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 2041 | VisitAndSpill(node->body()); |
| 2042 | |
| 2043 | if (node->next() == NULL) { |
| 2044 | // If there is no update statement and control flow can fall out |
| 2045 | // of the loop, jump directly to the continue label. |
| 2046 | if (has_valid_frame()) { |
| 2047 | node->continue_target()->Jump(); |
| 2048 | } |
| 2049 | } else { |
| 2050 | // If there is an update statement and control flow can reach it |
| 2051 | // via falling out of the body of the loop or continuing, we |
| 2052 | // compile the update statement. |
| 2053 | if (node->continue_target()->is_linked()) { |
| 2054 | node->continue_target()->Bind(); |
| 2055 | } |
| 2056 | if (has_valid_frame()) { |
| 2057 | // Record source position of the statement as this code which is |
| 2058 | // after the code for the body actually belongs to the loop |
| 2059 | // statement and not the body. |
| 2060 | CodeForStatementPosition(node); |
| 2061 | VisitAndSpill(node->next()); |
| 2062 | loop.Jump(); |
| 2063 | } |
| 2064 | } |
| 2065 | } |
| 2066 | if (node->break_target()->is_linked()) { |
| 2067 | node->break_target()->Bind(); |
| 2068 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2069 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2073 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2074 | #ifdef DEBUG |
| 2075 | int original_height = frame_->height(); |
| 2076 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2077 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2078 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2079 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2080 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2081 | JumpTarget primitive; |
| 2082 | JumpTarget jsobject; |
| 2083 | JumpTarget fixed_array; |
| 2084 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 2085 | JumpTarget end_del_check; |
| 2086 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2087 | |
| 2088 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2089 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2090 | |
| 2091 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 2092 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2093 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2094 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2095 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2096 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2097 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2098 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2099 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2100 | |
| 2101 | // Stack layout in body: |
| 2102 | // [iteration counter (Smi)] |
| 2103 | // [length of array] |
| 2104 | // [FixedArray] |
| 2105 | // [Map or 0] |
| 2106 | // [Object] |
| 2107 | |
| 2108 | // Check if enumerable is already a JSObject |
| 2109 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2110 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2111 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2112 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2113 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2114 | primitive.Bind(); |
| 2115 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2116 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2117 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2118 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2119 | // Get the set of properties (as a FixedArray or Map). |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2120 | // r0: value to be iterated over |
| 2121 | frame_->EmitPush(r0); // Push the object being iterated over. |
| 2122 | |
| 2123 | // Check cache validity in generated code. This is a fast case for |
| 2124 | // the JSObject::IsSimpleEnum cache validity checks. If we cannot |
| 2125 | // guarantee cache validity, call the runtime system to check cache |
| 2126 | // validity or get the property names in a fixed array. |
| 2127 | JumpTarget call_runtime; |
| 2128 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 2129 | JumpTarget check_prototype; |
| 2130 | JumpTarget use_cache; |
| 2131 | __ mov(r1, Operand(r0)); |
| 2132 | loop.Bind(); |
| 2133 | // Check that there are no elements. |
| 2134 | __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 2135 | __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex); |
| 2136 | __ cmp(r2, r4); |
| 2137 | call_runtime.Branch(ne); |
| 2138 | // Check that instance descriptors are not empty so that we can |
| 2139 | // check for an enum cache. Leave the map in r3 for the subsequent |
| 2140 | // prototype load. |
| 2141 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 2142 | __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset)); |
| 2143 | __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex); |
| 2144 | __ cmp(r2, ip); |
| 2145 | call_runtime.Branch(eq); |
| 2146 | // Check that there in an enum cache in the non-empty instance |
| 2147 | // descriptors. This is the case if the next enumeration index |
| 2148 | // field does not contain a smi. |
| 2149 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset)); |
| 2150 | __ tst(r2, Operand(kSmiTagMask)); |
| 2151 | call_runtime.Branch(eq); |
| 2152 | // For all objects but the receiver, check that the cache is empty. |
| 2153 | // r4: empty fixed array root. |
| 2154 | __ cmp(r1, r0); |
| 2155 | check_prototype.Branch(eq); |
| 2156 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 2157 | __ cmp(r2, r4); |
| 2158 | call_runtime.Branch(ne); |
| 2159 | check_prototype.Bind(); |
| 2160 | // Load the prototype from the map and loop if non-null. |
| 2161 | __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 2162 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2163 | __ cmp(r1, ip); |
| 2164 | loop.Branch(ne); |
| 2165 | // The enum cache is valid. Load the map of the object being |
| 2166 | // iterated over and use the cache for the iteration. |
| 2167 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 2168 | use_cache.Jump(); |
| 2169 | |
| 2170 | call_runtime.Bind(); |
| 2171 | // Call the runtime to get the property names for the object. |
| 2172 | 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] | 2173 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2174 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2175 | // If we got a map from the runtime call, we can do a fast |
| 2176 | // modification check. Otherwise, we got a fixed array, and we have |
| 2177 | // to do a slow check. |
| 2178 | // r0: map or fixed array (result from call to |
| 2179 | // Runtime::kGetPropertyNamesFast) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2180 | __ mov(r2, Operand(r0)); |
| 2181 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2182 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 2183 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2184 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2185 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2186 | use_cache.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2187 | // Get enum cache |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2188 | // r0: map (either the result from a call to |
| 2189 | // Runtime::kGetPropertyNamesFast or has been fetched directly from |
| 2190 | // the object) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2191 | __ mov(r1, Operand(r0)); |
| 2192 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 2193 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 2194 | __ ldr(r2, |
| 2195 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 2196 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2197 | frame_->EmitPush(r0); // map |
| 2198 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2199 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2200 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2201 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2202 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2203 | frame_->EmitPush(r0); |
| 2204 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2205 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2206 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2207 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2208 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 2209 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2210 | |
| 2211 | // 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] | 2212 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2213 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2214 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2215 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2216 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2217 | |
| 2218 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2219 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2220 | // sp[0] : index |
| 2221 | // sp[1] : array/enum cache length |
| 2222 | // sp[2] : array or enum cache |
| 2223 | // sp[3] : 0 or map |
| 2224 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2225 | // Grab the current frame's height for the break and continue |
| 2226 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2227 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 2228 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2229 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2230 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 2231 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 2232 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 2233 | node->break_target()->Branch(hs); |
| 2234 | |
| 2235 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2236 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2237 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2238 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2239 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 2240 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 2241 | |
| 2242 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2243 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2244 | // Check if this (still) matches the map of the enumerable. |
| 2245 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2246 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2247 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 2248 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2249 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2250 | |
| 2251 | // 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] | 2252 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 2253 | frame_->EmitPush(r0); |
| 2254 | frame_->EmitPush(r3); // push entry |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2255 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2256 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2257 | |
| 2258 | // 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] | 2259 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2260 | __ cmp(r3, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2261 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2262 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2263 | end_del_check.Bind(); |
| 2264 | // Store the entry in the 'each' expression and take another spin in the |
| 2265 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 2266 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2267 | { Reference each(this, node->each()); |
| 2268 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2269 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2270 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 2271 | frame_->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2272 | each.SetValue(NOT_CONST_INIT); |
| 2273 | frame_->Drop(2); |
| 2274 | } else { |
| 2275 | // If the reference was to a slot we rely on the convenient property |
| 2276 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 2277 | // right on top of or right underneath a zero-sized reference. |
| 2278 | each.SetValue(NOT_CONST_INIT); |
| 2279 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2280 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2281 | } |
| 2282 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2283 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2284 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2285 | VisitAndSpill(node->body()); |
| 2286 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2287 | // Next. Reestablish a spilled frame in case we are coming here via |
| 2288 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2289 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2290 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2291 | frame_->EmitPop(r0); |
| 2292 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 2293 | frame_->EmitPush(r0); |
| 2294 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2295 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2296 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 2297 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2298 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2299 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2300 | |
| 2301 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2302 | exit.Bind(); |
| 2303 | node->continue_target()->Unuse(); |
| 2304 | node->break_target()->Unuse(); |
| 2305 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2309 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2310 | #ifdef DEBUG |
| 2311 | int original_height = frame_->height(); |
| 2312 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2313 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2314 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2315 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2316 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2317 | JumpTarget try_block; |
| 2318 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2319 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2320 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2321 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2322 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2323 | |
| 2324 | // Store the caught exception in the catch variable. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2325 | Variable* catch_var = node->catch_var()->var(); |
| 2326 | ASSERT(catch_var != NULL && catch_var->slot() != NULL); |
| 2327 | StoreToSlot(catch_var->slot(), NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2328 | |
| 2329 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2330 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2331 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2332 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2333 | if (frame_ != NULL) { |
| 2334 | exit.Jump(); |
| 2335 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2336 | |
| 2337 | |
| 2338 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2339 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2340 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2341 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2342 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2343 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2344 | // Shadow the labels for all escapes from the try block, including |
| 2345 | // returns. During shadowing, the original label is hidden as the |
| 2346 | // LabelShadow and operations on the original actually affect the |
| 2347 | // shadowing label. |
| 2348 | // |
| 2349 | // We should probably try to unify the escaping labels and the return |
| 2350 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2351 | int nof_escapes = node->escaping_targets()->length(); |
| 2352 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2353 | |
| 2354 | // Add the shadow target for the function return. |
| 2355 | static const int kReturnShadowIndex = 0; |
| 2356 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2357 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2358 | function_return_is_shadowed_ = true; |
| 2359 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2360 | |
| 2361 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2362 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2363 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2367 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2368 | |
| 2369 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2370 | // After shadowing stops, the original labels are unshadowed and the |
| 2371 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2372 | bool has_unlinks = false; |
| 2373 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2374 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2375 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2376 | } |
| 2377 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2378 | |
| 2379 | // Get an external reference to the handler address. |
| 2380 | ExternalReference handler_address(Top::k_handler_address); |
| 2381 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2382 | // If we can fall off the end of the try block, unlink from try chain. |
| 2383 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2384 | // The next handler address is on top of the frame. Unlink from |
| 2385 | // the handler list and drop the rest of this handler from the |
| 2386 | // frame. |
| 2387 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2388 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2389 | __ mov(r3, Operand(handler_address)); |
| 2390 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2391 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2392 | if (has_unlinks) { |
| 2393 | exit.Jump(); |
| 2394 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2397 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2398 | // jumped to. Deallocate each shadow target. |
| 2399 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2400 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2401 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2402 | shadows[i]->Bind(); |
| 2403 | // Because we can be jumping here (to spilled code) from unspilled |
| 2404 | // code, we need to reestablish a spilled frame at this block. |
| 2405 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2406 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2407 | // Reload sp from the top handler, because some statements that we |
| 2408 | // 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] | 2409 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2410 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2411 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2412 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2413 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2414 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2415 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2416 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2417 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2418 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2419 | frame_->PrepareForReturn(); |
| 2420 | } |
| 2421 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2425 | exit.Bind(); |
| 2426 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2430 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2431 | #ifdef DEBUG |
| 2432 | int original_height = frame_->height(); |
| 2433 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2434 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2435 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2436 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2437 | |
| 2438 | // State: Used to keep track of reason for entering the finally |
| 2439 | // block. Should probably be extended to hold information for |
| 2440 | // break/continue from within the try block. |
| 2441 | enum { FALLING, THROWING, JUMPING }; |
| 2442 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2443 | JumpTarget try_block; |
| 2444 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2445 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2446 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2447 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2448 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2449 | // In case of thrown exceptions, this is where we continue. |
| 2450 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2451 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2452 | |
| 2453 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2454 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2455 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2456 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2457 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2458 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2459 | // Shadow the labels for all escapes from the try block, including |
| 2460 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2461 | // operations on the original actually affect the shadowing label. |
| 2462 | // |
| 2463 | // We should probably try to unify the escaping labels and the return |
| 2464 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2465 | int nof_escapes = node->escaping_targets()->length(); |
| 2466 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2467 | |
| 2468 | // Add the shadow target for the function return. |
| 2469 | static const int kReturnShadowIndex = 0; |
| 2470 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2471 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2472 | function_return_is_shadowed_ = true; |
| 2473 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2474 | |
| 2475 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2476 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2477 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2481 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2482 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2483 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2484 | // After shadowing stops, the original labels are unshadowed and the |
| 2485 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2486 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2487 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2488 | shadows[i]->StopShadowing(); |
| 2489 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2490 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2491 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2492 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2493 | // Get an external reference to the handler address. |
| 2494 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2495 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2496 | // If we can fall off the end of the try block, unlink from the try |
| 2497 | // chain and set the state on the frame to FALLING. |
| 2498 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2499 | // The next handler address is on top of the frame. |
| 2500 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2501 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2502 | __ mov(r3, Operand(handler_address)); |
| 2503 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2504 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2505 | |
| 2506 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2507 | // state in r2, then jump around the unlink blocks if any. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2508 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2509 | frame_->EmitPush(r0); |
| 2510 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2511 | if (nof_unlinks > 0) { |
| 2512 | finally_block.Jump(); |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | // Generate code to unlink and set the state for the (formerly) |
| 2517 | // shadowing targets that have been jumped to. |
| 2518 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2519 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2520 | // If we have come from the shadowed return, the return value is |
| 2521 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2522 | // until it is pushed. |
| 2523 | // |
| 2524 | // Because we can be jumping here (to spilled code) from |
| 2525 | // unspilled code, we need to reestablish a spilled frame at |
| 2526 | // this block. |
| 2527 | shadows[i]->Bind(); |
| 2528 | frame_->SpillAll(); |
| 2529 | |
| 2530 | // Reload sp from the top handler, because some statements that |
| 2531 | // we break from (eg, for...in) may have left stuff on the |
| 2532 | // stack. |
| 2533 | __ mov(r3, Operand(handler_address)); |
| 2534 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2535 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2536 | |
| 2537 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2538 | // handler address is currently on top of the frame. |
| 2539 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2540 | frame_->EmitPop(r1); |
| 2541 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2542 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2543 | |
| 2544 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2545 | // If this label shadowed the function return, materialize the |
| 2546 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2547 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2548 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2549 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2550 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2551 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2552 | } |
| 2553 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2554 | if (--nof_unlinks > 0) { |
| 2555 | // If this is not the last unlink block, jump around the next. |
| 2556 | finally_block.Jump(); |
| 2557 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2558 | } |
| 2559 | } |
| 2560 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2561 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2562 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2563 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2564 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2565 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2566 | |
| 2567 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2568 | // and the state - while evaluating the finally block. |
| 2569 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2570 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2571 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2572 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2573 | if (has_valid_frame()) { |
| 2574 | // Restore state and return value or faked TOS. |
| 2575 | frame_->EmitPop(r2); |
| 2576 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2579 | // Generate code to jump to the right destination for all used |
| 2580 | // formerly shadowing targets. Deallocate each shadow target. |
| 2581 | for (int i = 0; i < shadows.length(); i++) { |
| 2582 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2583 | JumpTarget* original = shadows[i]->other_target(); |
| 2584 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2585 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2586 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2587 | skip.Branch(ne); |
| 2588 | frame_->PrepareForReturn(); |
| 2589 | original->Jump(); |
| 2590 | skip.Bind(); |
| 2591 | } else { |
| 2592 | original->Branch(eq); |
| 2593 | } |
| 2594 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2595 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2596 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2597 | if (has_valid_frame()) { |
| 2598 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2599 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2600 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2601 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2602 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2603 | // Rethrow exception. |
| 2604 | frame_->EmitPush(r0); |
| 2605 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2606 | |
| 2607 | // Done. |
| 2608 | exit.Bind(); |
| 2609 | } |
| 2610 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2614 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2615 | #ifdef DEBUG |
| 2616 | int original_height = frame_->height(); |
| 2617 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2618 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2619 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2620 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2621 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2622 | frame_->DebugBreak(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2623 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2624 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2625 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2629 | void CodeGenerator::InstantiateFunction( |
| 2630 | Handle<SharedFunctionInfo> function_info) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2631 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2632 | __ mov(r0, Operand(function_info)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2633 | // Use the fast case closure allocation code that allocates in new |
| 2634 | // space for nested functions that don't need literals cloning. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2635 | if (scope()->is_function_scope() && function_info->num_literals() == 0) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2636 | FastNewClosureStub stub; |
| 2637 | frame_->EmitPush(r0); |
| 2638 | frame_->CallStub(&stub, 1); |
| 2639 | frame_->EmitPush(r0); |
| 2640 | } else { |
| 2641 | // Create a new closure. |
| 2642 | frame_->EmitPush(cp); |
| 2643 | frame_->EmitPush(r0); |
| 2644 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2645 | frame_->EmitPush(r0); |
| 2646 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2650 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2651 | #ifdef DEBUG |
| 2652 | int original_height = frame_->height(); |
| 2653 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2654 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2655 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2656 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2657 | // Build the function info and instantiate it. |
| 2658 | Handle<SharedFunctionInfo> function_info = |
| 2659 | Compiler::BuildFunctionInfo(node, script(), this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2660 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2661 | if (HasStackOverflow()) { |
| 2662 | ASSERT(frame_->height() == original_height); |
| 2663 | return; |
| 2664 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2665 | InstantiateFunction(function_info); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2666 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2670 | void CodeGenerator::VisitSharedFunctionInfoLiteral( |
| 2671 | SharedFunctionInfoLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2672 | #ifdef DEBUG |
| 2673 | int original_height = frame_->height(); |
| 2674 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2675 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2676 | Comment cmnt(masm_, "[ SharedFunctionInfoLiteral"); |
| 2677 | InstantiateFunction(node->shared_function_info()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2678 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2682 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2683 | #ifdef DEBUG |
| 2684 | int original_height = frame_->height(); |
| 2685 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2686 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2687 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2688 | JumpTarget then; |
| 2689 | JumpTarget else_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2690 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2691 | if (has_valid_frame()) { |
| 2692 | Branch(false, &else_); |
| 2693 | } |
| 2694 | if (has_valid_frame() || then.is_linked()) { |
| 2695 | then.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2696 | LoadAndSpill(node->then_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2697 | } |
| 2698 | if (else_.is_linked()) { |
| 2699 | JumpTarget exit; |
| 2700 | if (has_valid_frame()) exit.Jump(); |
| 2701 | else_.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2702 | LoadAndSpill(node->else_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2703 | if (exit.is_linked()) exit.Bind(); |
| 2704 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2705 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2709 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
| 2710 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2711 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2712 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2713 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2714 | JumpTarget slow; |
| 2715 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2716 | |
| 2717 | // Generate fast-case code for variables that might be shadowed by |
| 2718 | // eval-introduced variables. Eval is used a lot without |
| 2719 | // introducing variables. In those cases, we do not want to |
| 2720 | // perform a runtime call for all variables in the scope |
| 2721 | // containing the eval. |
| 2722 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2723 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2724 | // If there was no control flow to slow, we can exit early. |
| 2725 | if (!slow.is_linked()) { |
| 2726 | frame_->EmitPush(r0); |
| 2727 | return; |
| 2728 | } |
| 2729 | |
| 2730 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2731 | |
| 2732 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2733 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2734 | // Only generate the fast case for locals that rewrite to slots. |
| 2735 | // This rules out argument loads. |
| 2736 | if (potential_slot != NULL) { |
| 2737 | __ ldr(r0, |
| 2738 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2739 | r1, |
| 2740 | r2, |
| 2741 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2742 | if (potential_slot->var()->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2743 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2744 | __ cmp(r0, ip); |
| 2745 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2746 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2747 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2748 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2749 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2750 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2751 | } |
| 2752 | } |
| 2753 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2754 | slow.Bind(); |
| 2755 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2756 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2757 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2758 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2759 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2760 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2761 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2762 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2763 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2764 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2765 | done.Bind(); |
| 2766 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2767 | |
| 2768 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2769 | Register scratch = VirtualFrame::scratch0(); |
| 2770 | frame_->EmitPush(SlotOperand(slot, scratch)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2771 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2772 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2773 | // initialized yet) which needs to be converted into the 'undefined' |
| 2774 | // value. |
| 2775 | Comment cmnt(masm_, "[ Unhole const"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2776 | frame_->EmitPop(scratch); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2777 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2778 | __ cmp(scratch, ip); |
| 2779 | __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex, eq); |
| 2780 | frame_->EmitPush(scratch); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2781 | } |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2786 | void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) { |
| 2787 | ASSERT(slot != NULL); |
| 2788 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2789 | VirtualFrame::SpilledScope spilled_scope(frame_); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2790 | ASSERT(slot->var()->is_dynamic()); |
| 2791 | |
| 2792 | // For now, just do a runtime call. |
| 2793 | frame_->EmitPush(cp); |
| 2794 | __ mov(r0, Operand(slot->var()->name())); |
| 2795 | frame_->EmitPush(r0); |
| 2796 | |
| 2797 | if (init_state == CONST_INIT) { |
| 2798 | // Same as the case for a normal store, but ignores attribute |
| 2799 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 2800 | // const properties (introduced via eval("const foo = (some |
| 2801 | // expr);")). Also, uses the current function context instead of |
| 2802 | // the top context. |
| 2803 | // |
| 2804 | // Note that we must declare the foo upon entry of eval(), via a |
| 2805 | // context slot declaration, but we cannot initialize it at the |
| 2806 | // same time, because the const declaration may be at the end of |
| 2807 | // the eval code (sigh...) and the const variable may have been |
| 2808 | // used before (where its value is 'undefined'). Thus, we can only |
| 2809 | // do the initialization when we actually encounter the expression |
| 2810 | // and when the expression operands are defined and valid, and |
| 2811 | // thus we need the split into 2 operations: declaration of the |
| 2812 | // context slot followed by initialization. |
| 2813 | frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 2814 | } else { |
| 2815 | frame_->CallRuntime(Runtime::kStoreContextSlot, 3); |
| 2816 | } |
| 2817 | // Storing a variable must keep the (new) value on the expression |
| 2818 | // stack. This is necessary for compiling assignment expressions. |
| 2819 | frame_->EmitPush(r0); |
| 2820 | |
| 2821 | } else { |
| 2822 | ASSERT(!slot->var()->is_dynamic()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2823 | Register scratch = VirtualFrame::scratch0(); |
| 2824 | VirtualFrame::RegisterAllocationScope scope(this); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2825 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2826 | // The frame must be spilled when branching to this target. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2827 | JumpTarget exit; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2828 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2829 | if (init_state == CONST_INIT) { |
| 2830 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 2831 | // Only the first const initialization must be executed (the slot |
| 2832 | // still contains 'the hole' value). When the assignment is |
| 2833 | // executed, the code is identical to a normal store (see below). |
| 2834 | Comment cmnt(masm_, "[ Init const"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2835 | __ ldr(scratch, SlotOperand(slot, scratch)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2836 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2837 | __ cmp(scratch, ip); |
| 2838 | frame_->SpillAll(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2839 | exit.Branch(ne); |
| 2840 | } |
| 2841 | |
| 2842 | // We must execute the store. Storing a variable must keep the |
| 2843 | // (new) value on the stack. This is necessary for compiling |
| 2844 | // assignment expressions. |
| 2845 | // |
| 2846 | // Note: We will reach here even with slot->var()->mode() == |
| 2847 | // Variable::CONST because of const declarations which will |
| 2848 | // initialize consts to 'the hole' value and by doing so, end up |
| 2849 | // calling this code. r2 may be loaded with context; used below in |
| 2850 | // RecordWrite. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2851 | Register tos = frame_->Peek(); |
| 2852 | __ str(tos, SlotOperand(slot, scratch)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2853 | if (slot->type() == Slot::CONTEXT) { |
| 2854 | // Skip write barrier if the written value is a smi. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2855 | __ tst(tos, Operand(kSmiTagMask)); |
| 2856 | // We don't use tos any more after here. |
| 2857 | VirtualFrame::SpilledScope spilled_scope(frame_); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2858 | exit.Branch(eq); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2859 | // scratch is loaded with context when calling SlotOperand above. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2860 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 2861 | __ mov(r3, Operand(offset)); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2862 | // r1 could be identical with tos, but that doesn't matter. |
| 2863 | __ RecordWrite(scratch, r3, r1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2864 | } |
| 2865 | // If we definitely did not jump over the assignment, we do not need |
| 2866 | // to bind the exit label. Doing so can defeat peephole |
| 2867 | // optimization. |
| 2868 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2869 | frame_->SpillAll(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2870 | exit.Bind(); |
| 2871 | } |
| 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2876 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2877 | TypeofState typeof_state, |
| 2878 | Register tmp, |
| 2879 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2880 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2881 | // Check that no extension objects have been created by calls to |
| 2882 | // eval from the current scope to the global scope. |
| 2883 | Register context = cp; |
| 2884 | Scope* s = scope(); |
| 2885 | while (s != NULL) { |
| 2886 | if (s->num_heap_slots() > 0) { |
| 2887 | if (s->calls_eval()) { |
| 2888 | // Check that extension is NULL. |
| 2889 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2890 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2891 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2892 | } |
| 2893 | // Load next context in chain. |
| 2894 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2895 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2896 | context = tmp; |
| 2897 | } |
| 2898 | // If no outer scope calls eval, we do not need to check more |
| 2899 | // context extensions. |
| 2900 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2901 | s = s->outer_scope(); |
| 2902 | } |
| 2903 | |
| 2904 | if (s->is_eval_scope()) { |
| 2905 | Label next, fast; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2906 | __ Move(tmp, context); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2907 | __ bind(&next); |
| 2908 | // Terminate at global context. |
| 2909 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2910 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2911 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2912 | __ b(eq, &fast); |
| 2913 | // Check that extension is NULL. |
| 2914 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2915 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2916 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2917 | // Load next context in chain. |
| 2918 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2919 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2920 | __ b(&next); |
| 2921 | __ bind(&fast); |
| 2922 | } |
| 2923 | |
| 2924 | // All extension objects were empty and it is safe to use a global |
| 2925 | // load IC call. |
| 2926 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2927 | // Load the global object. |
| 2928 | LoadGlobal(); |
| 2929 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2930 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2931 | // Call IC stub. |
| 2932 | if (typeof_state == INSIDE_TYPEOF) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2933 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2934 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2935 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2938 | // Drop the global object. The result is in r0. |
| 2939 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2943 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2944 | #ifdef DEBUG |
| 2945 | int original_height = frame_->height(); |
| 2946 | #endif |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2947 | Comment cmnt(masm_, "[ Slot"); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2948 | LoadFromSlot(node, NOT_INSIDE_TYPEOF); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2949 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2950 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2951 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2952 | |
| 2953 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2954 | #ifdef DEBUG |
| 2955 | int original_height = frame_->height(); |
| 2956 | #endif |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2957 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2958 | |
| 2959 | Variable* var = node->var(); |
| 2960 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2961 | if (expr != NULL) { |
| 2962 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2963 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2964 | ASSERT(var->is_global()); |
| 2965 | Reference ref(this, node); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2966 | ref.GetValue(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2967 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2968 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2972 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2973 | #ifdef DEBUG |
| 2974 | int original_height = frame_->height(); |
| 2975 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2976 | Comment cmnt(masm_, "[ Literal"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2977 | Register reg = frame_->GetTOSRegister(); |
| 2978 | __ mov(reg, Operand(node->handle())); |
| 2979 | frame_->EmitPush(reg); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2980 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2984 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2985 | #ifdef DEBUG |
| 2986 | int original_height = frame_->height(); |
| 2987 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2988 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2989 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2990 | |
| 2991 | // Retrieve the literal array and check the allocated entry. |
| 2992 | |
| 2993 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2994 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2995 | |
| 2996 | // Load the literals array of the function. |
| 2997 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2998 | |
| 2999 | // Load the literal at the ast saved index. |
| 3000 | int literal_offset = |
| 3001 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 3002 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 3003 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3004 | JumpTarget done; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3005 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 3006 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3007 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3008 | |
| 3009 | // If the entry is undefined we call the runtime system to computed |
| 3010 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3011 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3012 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3013 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3014 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3015 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3016 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3017 | frame_->EmitPush(r0); |
| 3018 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3019 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3020 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3021 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3022 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3023 | frame_->EmitPush(r2); |
| 3024 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3028 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3029 | #ifdef DEBUG |
| 3030 | int original_height = frame_->height(); |
| 3031 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3032 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3033 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 3034 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3035 | // Load the function of this activation. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3036 | __ ldr(r3, frame_->Function()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3037 | // Literal array. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3038 | __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3039 | // Literal index. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3040 | __ mov(r2, Operand(Smi::FromInt(node->literal_index()))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3041 | // Constant properties. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3042 | __ mov(r1, Operand(node->constant_properties())); |
| 3043 | // Should the object literal have fast elements? |
| 3044 | __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0))); |
| 3045 | frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3046 | if (node->depth() > 1) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3047 | frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3048 | } else { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3049 | frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3050 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3051 | frame_->EmitPush(r0); // save the result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3052 | for (int i = 0; i < node->properties()->length(); i++) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3053 | // At the start of each iteration, the top of stack contains |
| 3054 | // the newly created object literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3055 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 3056 | Literal* key = property->key(); |
| 3057 | Expression* value = property->value(); |
| 3058 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3059 | case ObjectLiteral::Property::CONSTANT: |
| 3060 | break; |
| 3061 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 3062 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 3063 | // else fall through |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3064 | case ObjectLiteral::Property::COMPUTED: |
| 3065 | if (key->handle()->IsSymbol()) { |
| 3066 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 3067 | LoadAndSpill(value); |
| 3068 | frame_->EmitPop(r0); |
| 3069 | __ mov(r2, Operand(key->handle())); |
| 3070 | __ ldr(r1, frame_->Top()); // Load the receiver. |
| 3071 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
| 3072 | break; |
| 3073 | } |
| 3074 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3075 | case ObjectLiteral::Property::PROTOTYPE: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3076 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3077 | frame_->EmitPush(r0); // dup the result |
| 3078 | LoadAndSpill(key); |
| 3079 | LoadAndSpill(value); |
| 3080 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3081 | break; |
| 3082 | } |
| 3083 | case ObjectLiteral::Property::SETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3084 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3085 | frame_->EmitPush(r0); |
| 3086 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3087 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3088 | frame_->EmitPush(r0); |
| 3089 | LoadAndSpill(value); |
| 3090 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3091 | break; |
| 3092 | } |
| 3093 | case ObjectLiteral::Property::GETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3094 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3095 | frame_->EmitPush(r0); |
| 3096 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3097 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3098 | frame_->EmitPush(r0); |
| 3099 | LoadAndSpill(value); |
| 3100 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3101 | break; |
| 3102 | } |
| 3103 | } |
| 3104 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3105 | ASSERT(frame_->height() == original_height + 1); |
| 3106 | } |
| 3107 | |
| 3108 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3109 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3110 | #ifdef DEBUG |
| 3111 | int original_height = frame_->height(); |
| 3112 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3113 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3114 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3115 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3116 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3117 | __ ldr(r2, frame_->Function()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3118 | // Load the literals array of the function. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3119 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3120 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3121 | __ mov(r0, Operand(node->constant_elements())); |
| 3122 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3123 | int length = node->values()->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3124 | if (node->depth() > 1) { |
| 3125 | frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3126 | } else if (length > FastCloneShallowArrayStub::kMaximumLength) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3127 | frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3128 | } else { |
| 3129 | FastCloneShallowArrayStub stub(length); |
| 3130 | frame_->CallStub(&stub, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3131 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3132 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3133 | // r0: created object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3134 | |
| 3135 | // Generate code to set the elements in the array that are not |
| 3136 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3137 | for (int i = 0; i < node->values()->length(); i++) { |
| 3138 | Expression* value = node->values()->at(i); |
| 3139 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3140 | // If value is a literal the property value is already set in the |
| 3141 | // boilerplate object. |
| 3142 | if (value->AsLiteral() != NULL) continue; |
| 3143 | // If value is a materialized literal the property value is already set |
| 3144 | // in the boilerplate object if it is simple. |
| 3145 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3146 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3147 | // The property must be set by generated code. |
| 3148 | LoadAndSpill(value); |
| 3149 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3150 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3151 | // Fetch the object literal. |
| 3152 | __ ldr(r1, frame_->Top()); |
| 3153 | // Get the elements array. |
| 3154 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3155 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3156 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3157 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3158 | __ str(r0, FieldMemOperand(r1, offset)); |
| 3159 | |
| 3160 | // Update the write barrier for the array address. |
| 3161 | __ mov(r3, Operand(offset)); |
| 3162 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3163 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3164 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3168 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3169 | #ifdef DEBUG |
| 3170 | int original_height = frame_->height(); |
| 3171 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3172 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3173 | // Call runtime routine to allocate the catch extension object and |
| 3174 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3175 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 3176 | LoadAndSpill(node->key()); |
| 3177 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3178 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 3179 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3180 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3184 | void CodeGenerator::VisitAssignment(Assignment* node) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3185 | VirtualFrame::RegisterAllocationScope scope(this); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3186 | #ifdef DEBUG |
| 3187 | int original_height = frame_->height(); |
| 3188 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3189 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3190 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3191 | { Reference target(this, node->target(), node->is_compound()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3192 | if (target.is_illegal()) { |
| 3193 | // Fool the virtual frame into thinking that we left the assignment's |
| 3194 | // value on the frame. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3195 | Register tos = frame_->GetTOSRegister(); |
| 3196 | __ mov(tos, Operand(Smi::FromInt(0))); |
| 3197 | frame_->EmitPush(tos); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3198 | ASSERT(frame_->height() == original_height + 1); |
| 3199 | return; |
| 3200 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3201 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3202 | if (node->op() == Token::ASSIGN || |
| 3203 | node->op() == Token::INIT_VAR || |
| 3204 | node->op() == Token::INIT_CONST) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3205 | Load(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3206 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3207 | } else { // Assignment is a compound assignment. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3208 | // Get the old value of the lhs. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3209 | target.GetValue(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3210 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3211 | bool overwrite = |
| 3212 | (node->value()->AsBinaryOperation() != NULL && |
| 3213 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3214 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3215 | VirtualFrameSmiOperation(node->binary_op(), |
| 3216 | literal->handle(), |
| 3217 | false, |
| 3218 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3219 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3220 | Load(node->value()); |
| 3221 | VirtualFrameBinaryOperation(node->binary_op(), |
| 3222 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3223 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3224 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3225 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 3226 | if (var != NULL && |
| 3227 | (var->mode() == Variable::CONST) && |
| 3228 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 3229 | // Assignment ignored - leave the value on the stack. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3230 | UnloadReference(&target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3231 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3232 | CodeForSourcePosition(node->position()); |
| 3233 | if (node->op() == Token::INIT_CONST) { |
| 3234 | // Dynamic constant initializations must use the function context |
| 3235 | // and initialize the actual constant declared. Dynamic variable |
| 3236 | // initializations are simply assignments and use SetValue. |
| 3237 | target.SetValue(CONST_INIT); |
| 3238 | } else { |
| 3239 | target.SetValue(NOT_CONST_INIT); |
| 3240 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3241 | } |
| 3242 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3243 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3247 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3248 | #ifdef DEBUG |
| 3249 | int original_height = frame_->height(); |
| 3250 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3251 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3252 | Comment cmnt(masm_, "[ Throw"); |
| 3253 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3254 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3255 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3256 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 3257 | frame_->EmitPush(r0); |
| 3258 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3262 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3263 | #ifdef DEBUG |
| 3264 | int original_height = frame_->height(); |
| 3265 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3266 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3267 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3268 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3269 | { Reference property(this, node); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3270 | property.GetValue(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3271 | } |
| 3272 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3276 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3277 | #ifdef DEBUG |
| 3278 | int original_height = frame_->height(); |
| 3279 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3280 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3281 | Comment cmnt(masm_, "[ Call"); |
| 3282 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3283 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3284 | ZoneList<Expression*>* args = node->arguments(); |
| 3285 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3286 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3287 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3288 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 3289 | Property* property = function->AsProperty(); |
| 3290 | |
| 3291 | // ------------------------------------------------------------------------ |
| 3292 | // Fast-case: Use inline caching. |
| 3293 | // --- |
| 3294 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 3295 | // must be resolved after the arguments have been evaluated. The IC code |
| 3296 | // automatically handles this by loading the arguments before the function |
| 3297 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 3298 | // ------------------------------------------------------------------------ |
| 3299 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3300 | if (var != NULL && var->is_possibly_eval()) { |
| 3301 | // ---------------------------------- |
| 3302 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 3303 | // ---------------------------------- |
| 3304 | |
| 3305 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 3306 | // resolve the function we need to call and the receiver of the |
| 3307 | // call. Then we call the resolved function using the given |
| 3308 | // arguments. |
| 3309 | // Prepare stack for call to resolved function. |
| 3310 | LoadAndSpill(function); |
| 3311 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 3312 | frame_->EmitPush(r2); // Slot for receiver |
| 3313 | int arg_count = args->length(); |
| 3314 | for (int i = 0; i < arg_count; i++) { |
| 3315 | LoadAndSpill(args->at(i)); |
| 3316 | } |
| 3317 | |
| 3318 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 3319 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3320 | frame_->EmitPush(r1); |
| 3321 | if (arg_count > 0) { |
| 3322 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3323 | frame_->EmitPush(r1); |
| 3324 | } else { |
| 3325 | frame_->EmitPush(r2); |
| 3326 | } |
| 3327 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3328 | // Push the receiver. |
| 3329 | __ ldr(r1, frame_->Receiver()); |
| 3330 | frame_->EmitPush(r1); |
| 3331 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3332 | // Resolve the call. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3333 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3334 | |
| 3335 | // 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] | 3336 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3337 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3338 | |
| 3339 | // Call the function. |
| 3340 | CodeForSourcePosition(node->position()); |
| 3341 | |
| 3342 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3343 | CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3344 | frame_->CallStub(&call_function, arg_count + 1); |
| 3345 | |
| 3346 | __ ldr(cp, frame_->Context()); |
| 3347 | // Remove the function from the stack. |
| 3348 | frame_->Drop(); |
| 3349 | frame_->EmitPush(r0); |
| 3350 | |
| 3351 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3352 | // ---------------------------------- |
| 3353 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3354 | // ---------------------------------- |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3355 | // Pass the global object as the receiver and let the IC stub |
| 3356 | // patch the stack to use the global proxy as 'this' in the |
| 3357 | // invoked function. |
| 3358 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3359 | |
| 3360 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3361 | int arg_count = args->length(); |
| 3362 | for (int i = 0; i < arg_count; i++) { |
| 3363 | LoadAndSpill(args->at(i)); |
| 3364 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3365 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3366 | // Setup the name register and call the IC initialization code. |
| 3367 | __ mov(r2, Operand(var->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3368 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3369 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3370 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3371 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3372 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3373 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3374 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3375 | |
| 3376 | } else if (var != NULL && var->slot() != NULL && |
| 3377 | var->slot()->type() == Slot::LOOKUP) { |
| 3378 | // ---------------------------------- |
| 3379 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3380 | // ---------------------------------- |
| 3381 | |
| 3382 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3383 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3384 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3385 | frame_->EmitPush(r0); |
| 3386 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3387 | // r0: slot value; r1: receiver |
| 3388 | |
| 3389 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3390 | frame_->EmitPush(r0); // function |
| 3391 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3392 | |
| 3393 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3394 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3395 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3396 | |
| 3397 | } else if (property != NULL) { |
| 3398 | // Check if the key is a literal string. |
| 3399 | Literal* literal = property->key()->AsLiteral(); |
| 3400 | |
| 3401 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3402 | // ------------------------------------------------------------------ |
| 3403 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3404 | // ------------------------------------------------------------------ |
| 3405 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3406 | LoadAndSpill(property->obj()); // Receiver. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3407 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3408 | int arg_count = args->length(); |
| 3409 | for (int i = 0; i < arg_count; i++) { |
| 3410 | LoadAndSpill(args->at(i)); |
| 3411 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3412 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3413 | // Set the name register and call the IC initialization code. |
| 3414 | __ mov(r2, Operand(literal->handle())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3415 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3416 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3417 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3418 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3419 | __ ldr(cp, frame_->Context()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3420 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3421 | |
| 3422 | } else { |
| 3423 | // ------------------------------------------- |
| 3424 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3425 | // ------------------------------------------- |
| 3426 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3427 | LoadAndSpill(property->obj()); |
| 3428 | LoadAndSpill(property->key()); |
| 3429 | EmitKeyedLoad(false); |
| 3430 | frame_->Drop(); // key |
| 3431 | // Put the function below the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3432 | if (property->is_synthetic()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3433 | // Use the global receiver. |
| 3434 | frame_->Drop(); |
| 3435 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3436 | LoadGlobalReceiver(r0); |
| 3437 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3438 | frame_->EmitPop(r1); // receiver |
| 3439 | frame_->EmitPush(r0); // function |
| 3440 | frame_->EmitPush(r1); // receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3443 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3444 | CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3445 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
| 3448 | } else { |
| 3449 | // ---------------------------------- |
| 3450 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3451 | // ---------------------------------- |
| 3452 | |
| 3453 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3454 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3455 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3456 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3457 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3458 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3459 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3460 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3461 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3462 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3463 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3467 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3468 | #ifdef DEBUG |
| 3469 | int original_height = frame_->height(); |
| 3470 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3471 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3472 | Comment cmnt(masm_, "[ CallNew"); |
| 3473 | |
| 3474 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3475 | // expression in new calls must be evaluated before the |
| 3476 | // arguments. This is different from ordinary calls, where the |
| 3477 | // actual function to call is resolved after the arguments have been |
| 3478 | // evaluated. |
| 3479 | |
| 3480 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3481 | // receiver. There is no need to use the global proxy here because |
| 3482 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3483 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3484 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3485 | |
| 3486 | // Push the arguments ("left-to-right") on the stack. |
| 3487 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3488 | int arg_count = args->length(); |
| 3489 | for (int i = 0; i < arg_count; i++) { |
| 3490 | LoadAndSpill(args->at(i)); |
| 3491 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3492 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3493 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3494 | __ mov(r0, Operand(arg_count)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3495 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3496 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3497 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3498 | // Call the construct call builtin that handles allocation and |
| 3499 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3500 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3501 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3502 | frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3503 | |
| 3504 | // 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] | 3505 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3506 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3510 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3511 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3512 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3513 | JumpTarget leave, null, function, non_function_constructor; |
| 3514 | |
| 3515 | // Load the object into r0. |
| 3516 | LoadAndSpill(args->at(0)); |
| 3517 | frame_->EmitPop(r0); |
| 3518 | |
| 3519 | // If the object is a smi, we return null. |
| 3520 | __ tst(r0, Operand(kSmiTagMask)); |
| 3521 | null.Branch(eq); |
| 3522 | |
| 3523 | // Check that the object is a JS object but take special care of JS |
| 3524 | // functions to make sure they have 'Function' as their class. |
| 3525 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3526 | null.Branch(lt); |
| 3527 | |
| 3528 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3529 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3530 | // LAST_JS_OBJECT_TYPE. |
| 3531 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3532 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3533 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3534 | function.Branch(eq); |
| 3535 | |
| 3536 | // Check if the constructor in the map is a function. |
| 3537 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3538 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3539 | non_function_constructor.Branch(ne); |
| 3540 | |
| 3541 | // The r0 register now contains the constructor function. Grab the |
| 3542 | // instance class name from there. |
| 3543 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3544 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3545 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3546 | leave.Jump(); |
| 3547 | |
| 3548 | // Functions have class 'Function'. |
| 3549 | function.Bind(); |
| 3550 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3551 | frame_->EmitPush(r0); |
| 3552 | leave.Jump(); |
| 3553 | |
| 3554 | // Objects with a non-function constructor have class 'Object'. |
| 3555 | non_function_constructor.Bind(); |
| 3556 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3557 | frame_->EmitPush(r0); |
| 3558 | leave.Jump(); |
| 3559 | |
| 3560 | // Non-JS objects have class null. |
| 3561 | null.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3562 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3563 | frame_->EmitPush(r0); |
| 3564 | |
| 3565 | // All done. |
| 3566 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3570 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3571 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3572 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3573 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3574 | LoadAndSpill(args->at(0)); |
| 3575 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3576 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3577 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3578 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3579 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3580 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3581 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3582 | // Load the value. |
| 3583 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3584 | leave.Bind(); |
| 3585 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3589 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3590 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3591 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3592 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3593 | LoadAndSpill(args->at(0)); // Load the object. |
| 3594 | LoadAndSpill(args->at(1)); // Load the value. |
| 3595 | frame_->EmitPop(r0); // r0 contains value |
| 3596 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3597 | // if (object->IsSmi()) return object. |
| 3598 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3599 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3600 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3601 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3602 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3603 | // Store the value. |
| 3604 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3605 | // Update the write barrier. |
| 3606 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3607 | __ RecordWrite(r1, r2, r3); |
| 3608 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3609 | leave.Bind(); |
| 3610 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3614 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3615 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3616 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3617 | LoadAndSpill(args->at(0)); |
| 3618 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3619 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3620 | cc_reg_ = eq; |
| 3621 | } |
| 3622 | |
| 3623 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3624 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3625 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3626 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3627 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3628 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3629 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3630 | LoadAndSpill(args->at(1)); |
| 3631 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3632 | __ CallRuntime(Runtime::kLog, 2); |
| 3633 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3634 | #endif |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3635 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3636 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3640 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3641 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3642 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3643 | LoadAndSpill(args->at(0)); |
| 3644 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3645 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3646 | cc_reg_ = eq; |
| 3647 | } |
| 3648 | |
| 3649 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3650 | // Generates the Math.pow method - currently just calls runtime. |
| 3651 | void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) { |
| 3652 | ASSERT(args->length() == 2); |
| 3653 | Load(args->at(0)); |
| 3654 | Load(args->at(1)); |
| 3655 | frame_->CallRuntime(Runtime::kMath_pow, 2); |
| 3656 | frame_->EmitPush(r0); |
| 3657 | } |
| 3658 | |
| 3659 | |
| 3660 | // Generates the Math.sqrt method - currently just calls runtime. |
| 3661 | void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) { |
| 3662 | ASSERT(args->length() == 1); |
| 3663 | Load(args->at(0)); |
| 3664 | frame_->CallRuntime(Runtime::kMath_sqrt, 1); |
| 3665 | frame_->EmitPush(r0); |
| 3666 | } |
| 3667 | |
| 3668 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3669 | // This should generate code that performs a charCodeAt() call or returns |
| 3670 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3671 | // 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] | 3672 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3673 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3674 | ASSERT(args->length() == 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3675 | Comment(masm_, "[ GenerateFastCharCodeAt"); |
| 3676 | |
| 3677 | LoadAndSpill(args->at(0)); |
| 3678 | LoadAndSpill(args->at(1)); |
| 3679 | frame_->EmitPop(r0); // Index. |
| 3680 | frame_->EmitPop(r1); // String. |
| 3681 | |
| 3682 | Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string; |
| 3683 | |
| 3684 | __ tst(r1, Operand(kSmiTagMask)); |
| 3685 | __ b(eq, &slow); // The 'string' was a Smi. |
| 3686 | |
| 3687 | ASSERT(kSmiTag == 0); |
| 3688 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
| 3689 | __ b(ne, &slow); // The index was negative or not a Smi. |
| 3690 | |
| 3691 | __ bind(&try_again_with_new_string); |
| 3692 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 3693 | __ b(ge, &slow); |
| 3694 | |
| 3695 | // Now r2 has the string type. |
| 3696 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3697 | // Now r3 has the length of the string. Compare with the index. |
| 3698 | __ cmp(r3, Operand(r0, LSR, kSmiTagSize)); |
| 3699 | __ b(le, &slow); |
| 3700 | |
| 3701 | // Here we know the index is in range. Check that string is sequential. |
| 3702 | ASSERT_EQ(0, kSeqStringTag); |
| 3703 | __ tst(r2, Operand(kStringRepresentationMask)); |
| 3704 | __ b(ne, ¬_a_flat_string); |
| 3705 | |
| 3706 | // Check whether it is an ASCII string. |
| 3707 | ASSERT_EQ(0, kTwoByteStringTag); |
| 3708 | __ tst(r2, Operand(kStringEncodingMask)); |
| 3709 | __ b(ne, &ascii_string); |
| 3710 | |
| 3711 | // 2-byte string. We can add without shifting since the Smi tag size is the |
| 3712 | // log2 of the number of bytes in a two-byte character. |
| 3713 | ASSERT_EQ(1, kSmiTagSize); |
| 3714 | ASSERT_EQ(0, kSmiShiftSize); |
| 3715 | __ add(r1, r1, Operand(r0)); |
| 3716 | __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize)); |
| 3717 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3718 | __ jmp(&end); |
| 3719 | |
| 3720 | __ bind(&ascii_string); |
| 3721 | __ add(r1, r1, Operand(r0, LSR, kSmiTagSize)); |
| 3722 | __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 3723 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3724 | __ jmp(&end); |
| 3725 | |
| 3726 | __ bind(¬_a_flat_string); |
| 3727 | __ and_(r2, r2, Operand(kStringRepresentationMask)); |
| 3728 | __ cmp(r2, Operand(kConsStringTag)); |
| 3729 | __ b(ne, &slow); |
| 3730 | |
| 3731 | // ConsString. |
| 3732 | // Check that the right hand side is the empty string (ie if this is really a |
| 3733 | // flat string in a cons string). If that is not the case we would rather go |
| 3734 | // to the runtime system now, to flatten the string. |
| 3735 | __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset)); |
| 3736 | __ LoadRoot(r3, Heap::kEmptyStringRootIndex); |
| 3737 | __ cmp(r2, Operand(r3)); |
| 3738 | __ b(ne, &slow); |
| 3739 | |
| 3740 | // Get the first of the two strings. |
| 3741 | __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset)); |
| 3742 | __ jmp(&try_again_with_new_string); |
| 3743 | |
| 3744 | __ bind(&slow); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3745 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3746 | |
| 3747 | __ bind(&end); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3748 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
| 3751 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3752 | void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) { |
| 3753 | Comment(masm_, "[ GenerateCharFromCode"); |
| 3754 | ASSERT(args->length() == 1); |
| 3755 | |
| 3756 | LoadAndSpill(args->at(0)); |
| 3757 | frame_->EmitPop(r0); |
| 3758 | |
| 3759 | JumpTarget slow_case; |
| 3760 | JumpTarget exit; |
| 3761 | |
| 3762 | // Fast case of Heap::LookupSingleCharacterStringFromCode. |
| 3763 | ASSERT(kSmiTag == 0); |
| 3764 | ASSERT(kSmiShiftSize == 0); |
| 3765 | ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1)); |
| 3766 | __ tst(r0, Operand(kSmiTagMask | |
| 3767 | ((~String::kMaxAsciiCharCode) << kSmiTagSize))); |
| 3768 | slow_case.Branch(nz); |
| 3769 | |
| 3770 | ASSERT(kSmiTag == 0); |
| 3771 | __ mov(r1, Operand(Factory::single_character_string_cache())); |
| 3772 | __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 3773 | __ ldr(r1, MemOperand(r1, FixedArray::kHeaderSize - kHeapObjectTag)); |
| 3774 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 3775 | __ cmp(r1, ip); |
| 3776 | slow_case.Branch(eq); |
| 3777 | |
| 3778 | frame_->EmitPush(r1); |
| 3779 | exit.Jump(); |
| 3780 | |
| 3781 | slow_case.Bind(); |
| 3782 | frame_->EmitPush(r0); |
| 3783 | frame_->CallRuntime(Runtime::kCharFromCode, 1); |
| 3784 | frame_->EmitPush(r0); |
| 3785 | |
| 3786 | exit.Bind(); |
| 3787 | } |
| 3788 | |
| 3789 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3790 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3791 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3792 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3793 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3794 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3795 | // We need the CC bits to come out as not_equal in the case where the |
| 3796 | // object is a smi. This can't be done with the usual test opcode so |
| 3797 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3798 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3799 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3800 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3801 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3802 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3803 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3804 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3805 | cc_reg_ = eq; |
| 3806 | } |
| 3807 | |
| 3808 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3809 | void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3810 | VirtualFrame::SpilledScope spilled_scope(frame_); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3811 | ASSERT(args->length() == 1); |
| 3812 | LoadAndSpill(args->at(0)); |
| 3813 | JumpTarget answer; |
| 3814 | // We need the CC bits to come out as not_equal in the case where the |
| 3815 | // object is a smi. This can't be done with the usual test opcode so |
| 3816 | // we use XOR to get the right CC bits. |
| 3817 | frame_->EmitPop(r0); |
| 3818 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3819 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
| 3820 | answer.Branch(ne); |
| 3821 | // It is a heap object - get the map. Check if the object is a regexp. |
| 3822 | __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE); |
| 3823 | answer.Bind(); |
| 3824 | cc_reg_ = eq; |
| 3825 | } |
| 3826 | |
| 3827 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3828 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 3829 | // This generates a fast version of: |
| 3830 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3831 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3832 | ASSERT(args->length() == 1); |
| 3833 | LoadAndSpill(args->at(0)); |
| 3834 | frame_->EmitPop(r1); |
| 3835 | __ tst(r1, Operand(kSmiTagMask)); |
| 3836 | false_target()->Branch(eq); |
| 3837 | |
| 3838 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 3839 | __ cmp(r1, ip); |
| 3840 | true_target()->Branch(eq); |
| 3841 | |
| 3842 | Register map_reg = r2; |
| 3843 | __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3844 | // Undetectable objects behave like undefined when tested with typeof. |
| 3845 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 3846 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3847 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 3848 | false_target()->Branch(eq); |
| 3849 | |
| 3850 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 3851 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3852 | false_target()->Branch(lt); |
| 3853 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
| 3854 | cc_reg_ = le; |
| 3855 | } |
| 3856 | |
| 3857 | |
| 3858 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 3859 | // This generates a fast version of: |
| 3860 | // (%_ClassOf(arg) === 'Function') |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3861 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3862 | ASSERT(args->length() == 1); |
| 3863 | LoadAndSpill(args->at(0)); |
| 3864 | frame_->EmitPop(r0); |
| 3865 | __ tst(r0, Operand(kSmiTagMask)); |
| 3866 | false_target()->Branch(eq); |
| 3867 | Register map_reg = r2; |
| 3868 | __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE); |
| 3869 | cc_reg_ = eq; |
| 3870 | } |
| 3871 | |
| 3872 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3873 | void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3874 | VirtualFrame::SpilledScope spilled_scope(frame_); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3875 | ASSERT(args->length() == 1); |
| 3876 | LoadAndSpill(args->at(0)); |
| 3877 | frame_->EmitPop(r0); |
| 3878 | __ tst(r0, Operand(kSmiTagMask)); |
| 3879 | false_target()->Branch(eq); |
| 3880 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3881 | __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3882 | __ tst(r1, Operand(1 << Map::kIsUndetectable)); |
| 3883 | cc_reg_ = ne; |
| 3884 | } |
| 3885 | |
| 3886 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3887 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3888 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3889 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3890 | |
| 3891 | // Get the frame pointer for the calling frame. |
| 3892 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3893 | |
| 3894 | // Skip the arguments adaptor frame if it exists. |
| 3895 | Label check_frame_marker; |
| 3896 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3897 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3898 | __ b(ne, &check_frame_marker); |
| 3899 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3900 | |
| 3901 | // Check the marker in the calling frame. |
| 3902 | __ bind(&check_frame_marker); |
| 3903 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3904 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3905 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3909 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3910 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3911 | ASSERT(args->length() == 0); |
| 3912 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3913 | Label exit; |
| 3914 | |
| 3915 | // Get the number of formal parameters. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3916 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3917 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3918 | // Check if the calling frame is an arguments adaptor frame. |
| 3919 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3920 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 3921 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 3922 | __ b(ne, &exit); |
| 3923 | |
| 3924 | // Arguments adaptor case: Read the arguments length from the |
| 3925 | // adaptor frame. |
| 3926 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 3927 | |
| 3928 | __ bind(&exit); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3929 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3933 | void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3934 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3935 | ASSERT(args->length() == 1); |
| 3936 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3937 | // Satisfy contract with ArgumentsAccessStub: |
| 3938 | // 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] | 3939 | LoadAndSpill(args->at(0)); |
| 3940 | frame_->EmitPop(r1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3941 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3942 | |
| 3943 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3944 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3945 | frame_->CallStub(&stub, 0); |
| 3946 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3950 | void CodeGenerator::GenerateRandomHeapNumber( |
| 3951 | ZoneList<Expression*>* args) { |
| 3952 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3953 | ASSERT(args->length() == 0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3954 | |
| 3955 | Label slow_allocate_heapnumber; |
| 3956 | Label heapnumber_allocated; |
| 3957 | |
| 3958 | __ AllocateHeapNumber(r0, r1, r2, &slow_allocate_heapnumber); |
| 3959 | __ jmp(&heapnumber_allocated); |
| 3960 | |
| 3961 | __ bind(&slow_allocate_heapnumber); |
| 3962 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3963 | __ push(r0); |
| 3964 | __ CallRuntime(Runtime::kNumberUnaryMinus, 1); |
| 3965 | |
| 3966 | __ bind(&heapnumber_allocated); |
| 3967 | __ PrepareCallCFunction(1, r1); |
| 3968 | __ CallCFunction( |
| 3969 | ExternalReference::fill_heap_number_with_random_function(), 1); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3970 | frame_->EmitPush(r0); |
| 3971 | } |
| 3972 | |
| 3973 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3974 | void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) { |
| 3975 | ASSERT_EQ(2, args->length()); |
| 3976 | |
| 3977 | Load(args->at(0)); |
| 3978 | Load(args->at(1)); |
| 3979 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3980 | StringAddStub stub(NO_STRING_ADD_FLAGS); |
| 3981 | frame_->CallStub(&stub, 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3982 | frame_->EmitPush(r0); |
| 3983 | } |
| 3984 | |
| 3985 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3986 | void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) { |
| 3987 | ASSERT_EQ(3, args->length()); |
| 3988 | |
| 3989 | Load(args->at(0)); |
| 3990 | Load(args->at(1)); |
| 3991 | Load(args->at(2)); |
| 3992 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3993 | SubStringStub stub; |
| 3994 | frame_->CallStub(&stub, 3); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3995 | frame_->EmitPush(r0); |
| 3996 | } |
| 3997 | |
| 3998 | |
| 3999 | void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) { |
| 4000 | ASSERT_EQ(2, args->length()); |
| 4001 | |
| 4002 | Load(args->at(0)); |
| 4003 | Load(args->at(1)); |
| 4004 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4005 | StringCompareStub stub; |
| 4006 | frame_->CallStub(&stub, 2); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4007 | frame_->EmitPush(r0); |
| 4008 | } |
| 4009 | |
| 4010 | |
| 4011 | void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) { |
| 4012 | ASSERT_EQ(4, args->length()); |
| 4013 | |
| 4014 | Load(args->at(0)); |
| 4015 | Load(args->at(1)); |
| 4016 | Load(args->at(2)); |
| 4017 | Load(args->at(3)); |
| 4018 | |
| 4019 | frame_->CallRuntime(Runtime::kRegExpExec, 4); |
| 4020 | frame_->EmitPush(r0); |
| 4021 | } |
| 4022 | |
| 4023 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame^] | 4024 | void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) { |
| 4025 | // No stub. This code only occurs a few times in regexp.js. |
| 4026 | const int kMaxInlineLength = 100; |
| 4027 | ASSERT_EQ(3, args->length()); |
| 4028 | Load(args->at(0)); // Size of array, smi. |
| 4029 | Load(args->at(1)); // "index" property value. |
| 4030 | Load(args->at(2)); // "input" property value. |
| 4031 | { |
| 4032 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 4033 | Label slowcase; |
| 4034 | Label done; |
| 4035 | __ ldr(r1, MemOperand(sp, kPointerSize * 2)); |
| 4036 | STATIC_ASSERT(kSmiTag == 0); |
| 4037 | STATIC_ASSERT(kSmiTagSize == 1); |
| 4038 | __ tst(r1, Operand(kSmiTagMask)); |
| 4039 | __ b(ne, &slowcase); |
| 4040 | __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength))); |
| 4041 | __ b(hi, &slowcase); |
| 4042 | // Smi-tagging is equivalent to multiplying by 2. |
| 4043 | // Allocate RegExpResult followed by FixedArray with size in ebx. |
| 4044 | // JSArray: [Map][empty properties][Elements][Length-smi][index][input] |
| 4045 | // Elements: [Map][Length][..elements..] |
| 4046 | // Size of JSArray with two in-object properties and the header of a |
| 4047 | // FixedArray. |
| 4048 | int objects_size = |
| 4049 | (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize; |
| 4050 | __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize)); |
| 4051 | __ add(r2, r5, Operand(objects_size)); |
| 4052 | __ AllocateInNewSpace(r2, // In: Size, in words. |
| 4053 | r0, // Out: Start of allocation (tagged). |
| 4054 | r3, // Scratch register. |
| 4055 | r4, // Scratch register. |
| 4056 | &slowcase, |
| 4057 | TAG_OBJECT); |
| 4058 | // r0: Start of allocated area, object-tagged. |
| 4059 | // r1: Number of elements in array, as smi. |
| 4060 | // r5: Number of elements, untagged. |
| 4061 | |
| 4062 | // Set JSArray map to global.regexp_result_map(). |
| 4063 | // Set empty properties FixedArray. |
| 4064 | // Set elements to point to FixedArray allocated right after the JSArray. |
| 4065 | // Interleave operations for better latency. |
| 4066 | __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 4067 | __ add(r3, r0, Operand(JSRegExpResult::kSize)); |
| 4068 | __ mov(r4, Operand(Factory::empty_fixed_array())); |
| 4069 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 4070 | __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 4071 | __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX)); |
| 4072 | __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset)); |
| 4073 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4074 | |
| 4075 | // Set input, index and length fields from arguments. |
| 4076 | __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit())); |
| 4077 | __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset)); |
| 4078 | __ add(sp, sp, Operand(kPointerSize)); |
| 4079 | __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset)); |
| 4080 | __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset)); |
| 4081 | |
| 4082 | // Fill out the elements FixedArray. |
| 4083 | // r0: JSArray, tagged. |
| 4084 | // r3: FixedArray, tagged. |
| 4085 | // r5: Number of elements in array, untagged. |
| 4086 | |
| 4087 | // Set map. |
| 4088 | __ mov(r2, Operand(Factory::fixed_array_map())); |
| 4089 | __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 4090 | // Set FixedArray length. |
| 4091 | __ str(r5, FieldMemOperand(r3, FixedArray::kLengthOffset)); |
| 4092 | // Fill contents of fixed-array with the-hole. |
| 4093 | __ mov(r2, Operand(Factory::the_hole_value())); |
| 4094 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 4095 | // Fill fixed array elements with hole. |
| 4096 | // r0: JSArray, tagged. |
| 4097 | // r2: the hole. |
| 4098 | // r3: Start of elements in FixedArray. |
| 4099 | // r5: Number of elements to fill. |
| 4100 | Label loop; |
| 4101 | __ tst(r5, Operand(r5)); |
| 4102 | __ bind(&loop); |
| 4103 | __ b(le, &done); // Jump if r1 is negative or zero. |
| 4104 | __ sub(r5, r5, Operand(1), SetCC); |
| 4105 | __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2)); |
| 4106 | __ jmp(&loop); |
| 4107 | |
| 4108 | __ bind(&slowcase); |
| 4109 | __ CallRuntime(Runtime::kRegExpConstructResult, 3); |
| 4110 | |
| 4111 | __ bind(&done); |
| 4112 | } |
| 4113 | frame_->Forget(3); |
| 4114 | frame_->EmitPush(r0); |
| 4115 | } |
| 4116 | |
| 4117 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4118 | void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) { |
| 4119 | ASSERT_EQ(args->length(), 1); |
| 4120 | |
| 4121 | // Load the argument on the stack and jump to the runtime. |
| 4122 | Load(args->at(0)); |
| 4123 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 4124 | NumberToStringStub stub; |
| 4125 | frame_->CallStub(&stub, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4126 | frame_->EmitPush(r0); |
| 4127 | } |
| 4128 | |
| 4129 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4130 | void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) { |
| 4131 | Comment cmnt(masm_, "[ GenerateCallFunction"); |
| 4132 | |
| 4133 | ASSERT(args->length() >= 2); |
| 4134 | |
| 4135 | int n_args = args->length() - 2; // for receiver and function. |
| 4136 | Load(args->at(0)); // receiver |
| 4137 | for (int i = 0; i < n_args; i++) { |
| 4138 | Load(args->at(i + 1)); |
| 4139 | } |
| 4140 | Load(args->at(n_args + 1)); // function |
| 4141 | frame_->CallJSFunction(n_args); |
| 4142 | frame_->EmitPush(r0); |
| 4143 | } |
| 4144 | |
| 4145 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4146 | void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) { |
| 4147 | ASSERT_EQ(args->length(), 1); |
| 4148 | // Load the argument on the stack and jump to the runtime. |
| 4149 | Load(args->at(0)); |
| 4150 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 4151 | frame_->EmitPush(r0); |
| 4152 | } |
| 4153 | |
| 4154 | |
| 4155 | void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) { |
| 4156 | ASSERT_EQ(args->length(), 1); |
| 4157 | // Load the argument on the stack and jump to the runtime. |
| 4158 | Load(args->at(0)); |
| 4159 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 4160 | frame_->EmitPush(r0); |
| 4161 | } |
| 4162 | |
| 4163 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4164 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4165 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4166 | ASSERT(args->length() == 2); |
| 4167 | |
| 4168 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4169 | LoadAndSpill(args->at(0)); |
| 4170 | LoadAndSpill(args->at(1)); |
| 4171 | frame_->EmitPop(r0); |
| 4172 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4173 | __ cmp(r0, Operand(r1)); |
| 4174 | cc_reg_ = eq; |
| 4175 | } |
| 4176 | |
| 4177 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4178 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4179 | #ifdef DEBUG |
| 4180 | int original_height = frame_->height(); |
| 4181 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4182 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4183 | if (CheckForInlineRuntimeCall(node)) { |
| 4184 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4185 | (!has_cc() && frame_->height() == original_height + 1)); |
| 4186 | return; |
| 4187 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4188 | |
| 4189 | ZoneList<Expression*>* args = node->arguments(); |
| 4190 | Comment cmnt(masm_, "[ CallRuntime"); |
| 4191 | Runtime::Function* function = node->function(); |
| 4192 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4193 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4194 | // Prepare stack for calling JS runtime function. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4195 | // Push the builtins object found in the current global object. |
| 4196 | __ ldr(r1, GlobalObject()); |
| 4197 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4198 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4199 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4200 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4201 | // Push the arguments ("left-to-right"). |
| 4202 | int arg_count = args->length(); |
| 4203 | for (int i = 0; i < arg_count; i++) { |
| 4204 | LoadAndSpill(args->at(i)); |
| 4205 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4206 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4207 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4208 | // Call the JS runtime function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4209 | __ mov(r2, Operand(node->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4210 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 4211 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4212 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4213 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4214 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4215 | } else { |
| 4216 | // Call the C runtime function. |
| 4217 | frame_->CallRuntime(function, arg_count); |
| 4218 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4219 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4220 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4224 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4225 | #ifdef DEBUG |
| 4226 | int original_height = frame_->height(); |
| 4227 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4228 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4229 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 4230 | |
| 4231 | Token::Value op = node->op(); |
| 4232 | |
| 4233 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4234 | LoadConditionAndSpill(node->expression(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4235 | false_target(), |
| 4236 | true_target(), |
| 4237 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4238 | // LoadCondition may (and usually does) leave a test and branch to |
| 4239 | // be emitted by the caller. In that case, negate the condition. |
| 4240 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4241 | |
| 4242 | } else if (op == Token::DELETE) { |
| 4243 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4244 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4245 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4246 | LoadAndSpill(property->obj()); |
| 4247 | LoadAndSpill(property->key()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4248 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4249 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4250 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4251 | Slot* slot = variable->slot(); |
| 4252 | if (variable->is_global()) { |
| 4253 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4254 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4255 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4256 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4257 | |
| 4258 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 4259 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4260 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4261 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4262 | frame_->EmitPush(r0); |
| 4263 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4264 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4265 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4266 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4267 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4268 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4269 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4270 | } else { |
| 4271 | // Default: Result of deleting non-global, not dynamically |
| 4272 | // introduced variables is false. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4273 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4274 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4275 | |
| 4276 | } else { |
| 4277 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4278 | LoadAndSpill(node->expression()); // may have side-effects |
| 4279 | frame_->Drop(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4280 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4281 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4282 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4283 | |
| 4284 | } else if (op == Token::TYPEOF) { |
| 4285 | // Special case for loading the typeof expression; see comment on |
| 4286 | // LoadTypeofExpression(). |
| 4287 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4288 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 4289 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4290 | |
| 4291 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4292 | bool overwrite = |
| 4293 | (node->expression()->AsBinaryOperation() != NULL && |
| 4294 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4295 | LoadAndSpill(node->expression()); |
| 4296 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4297 | switch (op) { |
| 4298 | case Token::NOT: |
| 4299 | case Token::DELETE: |
| 4300 | case Token::TYPEOF: |
| 4301 | UNREACHABLE(); // handled above |
| 4302 | break; |
| 4303 | |
| 4304 | case Token::SUB: { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4305 | GenericUnaryOpStub stub(Token::SUB, overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4306 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4307 | break; |
| 4308 | } |
| 4309 | |
| 4310 | case Token::BIT_NOT: { |
| 4311 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4312 | JumpTarget smi_label; |
| 4313 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4314 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4315 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4316 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4317 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 4318 | frame_->CallStub(&stub, 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4319 | continue_label.Jump(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4320 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4321 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4322 | __ mvn(r0, Operand(r0)); |
| 4323 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4324 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4325 | break; |
| 4326 | } |
| 4327 | |
| 4328 | case Token::VOID: |
| 4329 | // since the stack top is cached in r0, popping and then |
| 4330 | // pushing a value can be done by just writing to r0. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4331 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4332 | break; |
| 4333 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4334 | case Token::ADD: { |
| 4335 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4336 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4337 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4338 | continue_label.Branch(eq); |
| 4339 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4340 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4341 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4342 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4343 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4344 | default: |
| 4345 | UNREACHABLE(); |
| 4346 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4347 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4348 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4349 | ASSERT(!has_valid_frame() || |
| 4350 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4351 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4355 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4356 | #ifdef DEBUG |
| 4357 | int original_height = frame_->height(); |
| 4358 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4359 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4360 | Comment cmnt(masm_, "[ CountOperation"); |
| 4361 | |
| 4362 | bool is_postfix = node->is_postfix(); |
| 4363 | bool is_increment = node->op() == Token::INC; |
| 4364 | |
| 4365 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 4366 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 4367 | |
| 4368 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4369 | if (is_postfix) { |
| 4370 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4371 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4372 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4373 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4374 | // A constant reference is not saved to, so a constant reference is not a |
| 4375 | // compound assignment reference. |
| 4376 | { Reference target(this, node->expression(), !is_const); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4377 | if (target.is_illegal()) { |
| 4378 | // Spoof the virtual frame to have the expected height (one higher |
| 4379 | // than on entry). |
| 4380 | if (!is_postfix) { |
| 4381 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 4382 | frame_->EmitPush(r0); |
| 4383 | } |
| 4384 | ASSERT(frame_->height() == original_height + 1); |
| 4385 | return; |
| 4386 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4387 | target.GetValue(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4388 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4389 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4390 | JumpTarget slow; |
| 4391 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4392 | |
| 4393 | // Load the value (1) into register r1. |
| 4394 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 4395 | |
| 4396 | // Check for smi operand. |
| 4397 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4398 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4399 | |
| 4400 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4401 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4402 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4403 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4404 | |
| 4405 | // Perform optimistic increment/decrement. |
| 4406 | if (is_increment) { |
| 4407 | __ add(r0, r0, Operand(r1), SetCC); |
| 4408 | } else { |
| 4409 | __ sub(r0, r0, Operand(r1), SetCC); |
| 4410 | } |
| 4411 | |
| 4412 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4413 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4414 | |
| 4415 | // Revert optimistic increment/decrement. |
| 4416 | if (is_increment) { |
| 4417 | __ sub(r0, r0, Operand(r1)); |
| 4418 | } else { |
| 4419 | __ add(r0, r0, Operand(r1)); |
| 4420 | } |
| 4421 | |
| 4422 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4423 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4424 | { |
| 4425 | // Convert the operand to a number. |
| 4426 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4427 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4428 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4429 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4430 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4431 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4434 | // Compute the new value. |
| 4435 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 4436 | frame_->EmitPush(r0); |
| 4437 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4438 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4439 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4440 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4441 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4445 | exit.Bind(); |
| 4446 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4447 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4451 | if (is_postfix) frame_->EmitPop(r0); |
| 4452 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4456 | void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4457 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 4458 | // operators must yield the result of one of the two expressions |
| 4459 | // before any ToBoolean() conversions. This means that the value |
| 4460 | // produced by a && or || operator is not necessarily a boolean. |
| 4461 | |
| 4462 | // NOTE: If the left hand side produces a materialized value (not in |
| 4463 | // the CC register), we force the right hand side to do the |
| 4464 | // same. This is necessary because we may have to branch to the exit |
| 4465 | // after evaluating the left hand side (due to the shortcut |
| 4466 | // semantics), but the compiler must (statically) know if the result |
| 4467 | // of compiling the binary operation is materialized or not. |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4468 | if (node->op() == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4469 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4470 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4471 | &is_true, |
| 4472 | false_target(), |
| 4473 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4474 | if (has_valid_frame() && !has_cc()) { |
| 4475 | // 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] | 4476 | JumpTarget pop_and_continue; |
| 4477 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4478 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4479 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4480 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4481 | // Avoid popping the result if it converts to 'false' using the |
| 4482 | // standard ToBoolean() conversion as described in ECMA-262, |
| 4483 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4484 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4485 | Branch(false, &exit); |
| 4486 | |
| 4487 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4488 | pop_and_continue.Bind(); |
| 4489 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4490 | |
| 4491 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4492 | is_true.Bind(); |
| 4493 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4494 | |
| 4495 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4496 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4497 | } else if (has_cc() || is_true.is_linked()) { |
| 4498 | // The left-hand side is either (a) partially compiled to |
| 4499 | // control flow with a final branch left to emit or (b) fully |
| 4500 | // compiled to control flow and possibly true. |
| 4501 | if (has_cc()) { |
| 4502 | Branch(false, false_target()); |
| 4503 | } |
| 4504 | is_true.Bind(); |
| 4505 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4506 | true_target(), |
| 4507 | false_target(), |
| 4508 | false); |
| 4509 | } else { |
| 4510 | // Nothing to do. |
| 4511 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4514 | } else { |
| 4515 | ASSERT(node->op() == Token::OR); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4516 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4517 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4518 | true_target(), |
| 4519 | &is_false, |
| 4520 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4521 | if (has_valid_frame() && !has_cc()) { |
| 4522 | // 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] | 4523 | JumpTarget pop_and_continue; |
| 4524 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4525 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4526 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4527 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4528 | // Avoid popping the result if it converts to 'true' using the |
| 4529 | // standard ToBoolean() conversion as described in ECMA-262, |
| 4530 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4531 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4532 | Branch(true, &exit); |
| 4533 | |
| 4534 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4535 | pop_and_continue.Bind(); |
| 4536 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4537 | |
| 4538 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4539 | is_false.Bind(); |
| 4540 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4541 | |
| 4542 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4543 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4544 | } else if (has_cc() || is_false.is_linked()) { |
| 4545 | // The left-hand side is either (a) partially compiled to |
| 4546 | // control flow with a final branch left to emit or (b) fully |
| 4547 | // compiled to control flow and possibly false. |
| 4548 | if (has_cc()) { |
| 4549 | Branch(true, true_target()); |
| 4550 | } |
| 4551 | is_false.Bind(); |
| 4552 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4553 | true_target(), |
| 4554 | false_target(), |
| 4555 | false); |
| 4556 | } else { |
| 4557 | // Nothing to do. |
| 4558 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4559 | } |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4560 | } |
| 4561 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4562 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4563 | |
| 4564 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
| 4565 | #ifdef DEBUG |
| 4566 | int original_height = frame_->height(); |
| 4567 | #endif |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4568 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 4569 | |
| 4570 | if (node->op() == Token::AND || node->op() == Token::OR) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4571 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4572 | GenerateLogicalBooleanOperation(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4573 | } else { |
| 4574 | // Optimize for the case where (at least) one of the expressions |
| 4575 | // is a literal small integer. |
| 4576 | Literal* lliteral = node->left()->AsLiteral(); |
| 4577 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4578 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 4579 | // never return a constant/immutable object. |
| 4580 | bool overwrite_left = |
| 4581 | (node->left()->AsBinaryOperation() != NULL && |
| 4582 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 4583 | bool overwrite_right = |
| 4584 | (node->right()->AsBinaryOperation() != NULL && |
| 4585 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4586 | |
| 4587 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4588 | VirtualFrame::RegisterAllocationScope scope(this); |
| 4589 | Load(node->left()); |
| 4590 | VirtualFrameSmiOperation( |
| 4591 | node->op(), |
| 4592 | rliteral->handle(), |
| 4593 | false, |
| 4594 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4595 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4596 | VirtualFrame::RegisterAllocationScope scope(this); |
| 4597 | Load(node->right()); |
| 4598 | VirtualFrameSmiOperation(node->op(), |
| 4599 | lliteral->handle(), |
| 4600 | true, |
| 4601 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4602 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4603 | VirtualFrame::RegisterAllocationScope scope(this); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4604 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 4605 | if (overwrite_left) { |
| 4606 | overwrite_mode = OVERWRITE_LEFT; |
| 4607 | } else if (overwrite_right) { |
| 4608 | overwrite_mode = OVERWRITE_RIGHT; |
| 4609 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4610 | Load(node->left()); |
| 4611 | Load(node->right()); |
| 4612 | VirtualFrameBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4613 | } |
| 4614 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4615 | ASSERT(!has_valid_frame() || |
| 4616 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4617 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4621 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4622 | #ifdef DEBUG |
| 4623 | int original_height = frame_->height(); |
| 4624 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4625 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4626 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4627 | frame_->EmitPush(r0); |
| 4628 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4632 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4633 | #ifdef DEBUG |
| 4634 | int original_height = frame_->height(); |
| 4635 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4636 | Comment cmnt(masm_, "[ CompareOperation"); |
| 4637 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4638 | VirtualFrame::RegisterAllocationScope nonspilled_scope(this); |
| 4639 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4640 | // Get the expressions from the node. |
| 4641 | Expression* left = node->left(); |
| 4642 | Expression* right = node->right(); |
| 4643 | Token::Value op = node->op(); |
| 4644 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4645 | // To make null checks efficient, we check if either left or right is the |
| 4646 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 4647 | // instead of calling the (very) general runtime routine for checking |
| 4648 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4649 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4650 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4651 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4652 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4653 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4654 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4655 | if (left_is_null || right_is_null) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4656 | Load(left_is_null ? right : left); |
| 4657 | Register tos = frame_->PopToRegister(); |
| 4658 | // JumpTargets can't cope with register allocation yet. |
| 4659 | frame_->SpillAll(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4660 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4661 | __ cmp(tos, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4662 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4663 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4664 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4665 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4666 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4667 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4668 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4669 | __ cmp(tos, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4670 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4671 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4672 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4673 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4674 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4675 | // It can be an undetectable object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4676 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
| 4677 | __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 4678 | __ and_(tos, tos, Operand(1 << Map::kIsUndetectable)); |
| 4679 | __ cmp(tos, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4682 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4683 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4684 | return; |
| 4685 | } |
| 4686 | } |
| 4687 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4688 | // To make typeof testing for natives implemented in JavaScript really |
| 4689 | // efficient, we generate special code for expressions of the form: |
| 4690 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4691 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 4692 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4693 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4694 | (right->AsLiteral() != NULL && |
| 4695 | right->AsLiteral()->handle()->IsString())) { |
| 4696 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4697 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4698 | // Load the operand, move it to a register. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4699 | LoadTypeofExpression(operation->expression()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4700 | Register tos = frame_->PopToRegister(); |
| 4701 | |
| 4702 | // JumpTargets can't cope with register allocation yet. |
| 4703 | frame_->SpillAll(); |
| 4704 | |
| 4705 | Register scratch = VirtualFrame::scratch0(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4706 | |
| 4707 | if (check->Equals(Heap::number_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4708 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4709 | true_target()->Branch(eq); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4710 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4711 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4712 | __ cmp(tos, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4713 | cc_reg_ = eq; |
| 4714 | |
| 4715 | } else if (check->Equals(Heap::string_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4716 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4717 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4718 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4719 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4720 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4721 | // It can be an undetectable string object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4722 | __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 4723 | __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable)); |
| 4724 | __ cmp(scratch, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4725 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4726 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4727 | __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset)); |
| 4728 | __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4729 | cc_reg_ = lt; |
| 4730 | |
| 4731 | } else if (check->Equals(Heap::boolean_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4732 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4733 | __ cmp(tos, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4734 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4735 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4736 | __ cmp(tos, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4737 | cc_reg_ = eq; |
| 4738 | |
| 4739 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4740 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4741 | __ cmp(tos, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4742 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4743 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4744 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4745 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4746 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4747 | // It can be an undetectable object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4748 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
| 4749 | __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 4750 | __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable)); |
| 4751 | __ cmp(scratch, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4752 | |
| 4753 | cc_reg_ = eq; |
| 4754 | |
| 4755 | } else if (check->Equals(Heap::function_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4756 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4757 | false_target()->Branch(eq); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4758 | Register map_reg = scratch; |
| 4759 | __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4760 | true_target()->Branch(eq); |
| 4761 | // Regular expressions are callable so typeof == 'function'. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4762 | __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4763 | cc_reg_ = eq; |
| 4764 | |
| 4765 | } else if (check->Equals(Heap::object_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4766 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4767 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4768 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4769 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4770 | __ cmp(tos, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4771 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4772 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4773 | Register map_reg = scratch; |
| 4774 | __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4775 | false_target()->Branch(eq); |
| 4776 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4777 | // It can be an undetectable object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4778 | __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 4779 | __ and_(tos, tos, Operand(1 << Map::kIsUndetectable)); |
| 4780 | __ cmp(tos, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4781 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4782 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4783 | __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4784 | __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4785 | false_target()->Branch(lt); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4786 | __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4787 | cc_reg_ = le; |
| 4788 | |
| 4789 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4790 | // Uncommon case: typeof testing against a string literal that is |
| 4791 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4792 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4793 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4794 | ASSERT(!has_valid_frame() || |
| 4795 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4796 | return; |
| 4797 | } |
| 4798 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4799 | switch (op) { |
| 4800 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4801 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4802 | break; |
| 4803 | |
| 4804 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4805 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4806 | break; |
| 4807 | |
| 4808 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4809 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4810 | break; |
| 4811 | |
| 4812 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4813 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4814 | break; |
| 4815 | |
| 4816 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4817 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4818 | break; |
| 4819 | |
| 4820 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4821 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4822 | break; |
| 4823 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4824 | case Token::IN: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4825 | VirtualFrame::SpilledScope scope(frame_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4826 | LoadAndSpill(left); |
| 4827 | LoadAndSpill(right); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4828 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4829 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4830 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4831 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4832 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4833 | case Token::INSTANCEOF: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4834 | VirtualFrame::SpilledScope scope(frame_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4835 | LoadAndSpill(left); |
| 4836 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4837 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4838 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4839 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4840 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4841 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4842 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4843 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4844 | |
| 4845 | default: |
| 4846 | UNREACHABLE(); |
| 4847 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4848 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4849 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4853 | void CodeGenerator::EmitKeyedLoad(bool is_global) { |
| 4854 | Comment cmnt(masm_, "[ Load from keyed Property"); |
| 4855 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 4856 | RelocInfo::Mode rmode = is_global |
| 4857 | ? RelocInfo::CODE_TARGET_CONTEXT |
| 4858 | : RelocInfo::CODE_TARGET; |
| 4859 | frame_->CallCodeObject(ic, rmode, 0); |
| 4860 | } |
| 4861 | |
| 4862 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4863 | #ifdef DEBUG |
| 4864 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4865 | #endif |
| 4866 | |
| 4867 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4868 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4869 | #define __ ACCESS_MASM(masm) |
| 4870 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4871 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4872 | Handle<String> Reference::GetName() { |
| 4873 | ASSERT(type_ == NAMED); |
| 4874 | Property* property = expression_->AsProperty(); |
| 4875 | if (property == NULL) { |
| 4876 | // Global variable reference treated as a named property reference. |
| 4877 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4878 | ASSERT(proxy->AsVariable() != NULL); |
| 4879 | ASSERT(proxy->AsVariable()->is_global()); |
| 4880 | return proxy->name(); |
| 4881 | } else { |
| 4882 | Literal* raw_name = property->key()->AsLiteral(); |
| 4883 | ASSERT(raw_name != NULL); |
| 4884 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4885 | } |
| 4886 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4887 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4888 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4889 | void Reference::GetValue() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4890 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4891 | ASSERT(!is_illegal()); |
| 4892 | ASSERT(!cgen_->has_cc()); |
| 4893 | MacroAssembler* masm = cgen_->masm(); |
| 4894 | Property* property = expression_->AsProperty(); |
| 4895 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4896 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | switch (type_) { |
| 4900 | case SLOT: { |
| 4901 | Comment cmnt(masm, "[ Load from Slot"); |
| 4902 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4903 | ASSERT(slot != NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4904 | cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4905 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4908 | case NAMED: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4909 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4910 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4911 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4912 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4913 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4914 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4915 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4916 | ASSERT(var == NULL || var->is_global()); |
| 4917 | RelocInfo::Mode rmode = (var == NULL) |
| 4918 | ? RelocInfo::CODE_TARGET |
| 4919 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4920 | frame->CallCodeObject(ic, rmode, 0); |
| 4921 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4922 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4925 | case KEYED: { |
| 4926 | // TODO(181): Implement inlined version of array indexing once |
| 4927 | // loop nesting is properly tracked on ARM. |
| 4928 | ASSERT(property != NULL); |
| 4929 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 4930 | ASSERT(var == NULL || var->is_global()); |
| 4931 | cgen_->EmitKeyedLoad(var != NULL); |
| 4932 | cgen_->frame()->EmitPush(r0); |
| 4933 | break; |
| 4934 | } |
| 4935 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4936 | default: |
| 4937 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4938 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4939 | |
| 4940 | if (!persist_after_get_) { |
| 4941 | cgen_->UnloadReference(this); |
| 4942 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4946 | void Reference::SetValue(InitState init_state) { |
| 4947 | ASSERT(!is_illegal()); |
| 4948 | ASSERT(!cgen_->has_cc()); |
| 4949 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4950 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4951 | Property* property = expression_->AsProperty(); |
| 4952 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4953 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4954 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4955 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4956 | switch (type_) { |
| 4957 | case SLOT: { |
| 4958 | Comment cmnt(masm, "[ Store to Slot"); |
| 4959 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4960 | cgen_->StoreToSlot(slot, init_state); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4961 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4962 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4965 | case NAMED: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4966 | VirtualFrame::SpilledScope scope(frame); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4967 | Comment cmnt(masm, "[ Store to named Property"); |
| 4968 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4969 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4970 | Handle<String> name(GetName()); |
| 4971 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4972 | frame->EmitPop(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4973 | frame->EmitPop(r1); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4974 | __ mov(r2, Operand(name)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4975 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4976 | frame->EmitPush(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4977 | set_unloaded(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4978 | break; |
| 4979 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4980 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4981 | case KEYED: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4982 | VirtualFrame::SpilledScope scope(frame); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4983 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4984 | Property* property = expression_->AsProperty(); |
| 4985 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4986 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4987 | |
| 4988 | // Call IC code. |
| 4989 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4990 | frame->EmitPop(r0); // value |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4991 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4992 | frame->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4993 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4994 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4995 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4996 | |
| 4997 | default: |
| 4998 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4999 | } |
| 5000 | } |
| 5001 | |
| 5002 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5003 | void FastNewClosureStub::Generate(MacroAssembler* masm) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5004 | // Create a new closure from the given function info in new |
| 5005 | // space. Set the context to the current context in cp. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5006 | Label gc; |
| 5007 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5008 | // Pop the function info from the stack. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5009 | __ pop(r3); |
| 5010 | |
| 5011 | // Attempt to allocate new JSFunction in new space. |
| 5012 | __ AllocateInNewSpace(JSFunction::kSize / kPointerSize, |
| 5013 | r0, |
| 5014 | r1, |
| 5015 | r2, |
| 5016 | &gc, |
| 5017 | TAG_OBJECT); |
| 5018 | |
| 5019 | // Compute the function map in the current global context and set that |
| 5020 | // as the map of the allocated object. |
| 5021 | __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 5022 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 5023 | __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX))); |
| 5024 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 5025 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5026 | // Initialize the rest of the function. We don't have to update the |
| 5027 | // write barrier because the allocated object is in new space. |
| 5028 | __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex); |
| 5029 | __ LoadRoot(r2, Heap::kTheHoleValueRootIndex); |
| 5030 | __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset)); |
| 5031 | __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 5032 | __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset)); |
| 5033 | __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 5034 | __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset)); |
| 5035 | __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5036 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5037 | // Return result. The argument function info has been popped already. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5038 | __ Ret(); |
| 5039 | |
| 5040 | // Create a new closure through the slower runtime call. |
| 5041 | __ bind(&gc); |
| 5042 | __ push(cp); |
| 5043 | __ push(r3); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5044 | __ TailCallRuntime(Runtime::kNewClosure, 2, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | |
| 5048 | void FastNewContextStub::Generate(MacroAssembler* masm) { |
| 5049 | // Try to allocate the context in new space. |
| 5050 | Label gc; |
| 5051 | int length = slots_ + Context::MIN_CONTEXT_SLOTS; |
| 5052 | |
| 5053 | // Attempt to allocate the context in new space. |
| 5054 | __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize), |
| 5055 | r0, |
| 5056 | r1, |
| 5057 | r2, |
| 5058 | &gc, |
| 5059 | TAG_OBJECT); |
| 5060 | |
| 5061 | // Load the function from the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5062 | __ ldr(r3, MemOperand(sp, 0)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5063 | |
| 5064 | // Setup the object header. |
| 5065 | __ LoadRoot(r2, Heap::kContextMapRootIndex); |
| 5066 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 5067 | __ mov(r2, Operand(length)); |
| 5068 | __ str(r2, FieldMemOperand(r0, Array::kLengthOffset)); |
| 5069 | |
| 5070 | // Setup the fixed slots. |
| 5071 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 5072 | __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 5073 | __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 5074 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
| 5075 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX))); |
| 5076 | |
| 5077 | // Copy the global object from the surrounding context. |
| 5078 | __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 5079 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 5080 | |
| 5081 | // Initialize the rest of the slots to undefined. |
| 5082 | __ LoadRoot(r1, Heap::kUndefinedValueRootIndex); |
| 5083 | for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) { |
| 5084 | __ str(r1, MemOperand(r0, Context::SlotOffset(i))); |
| 5085 | } |
| 5086 | |
| 5087 | // Remove the on-stack argument and return. |
| 5088 | __ mov(cp, r0); |
| 5089 | __ pop(); |
| 5090 | __ Ret(); |
| 5091 | |
| 5092 | // Need to collect. Call into runtime system. |
| 5093 | __ bind(&gc); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5094 | __ TailCallRuntime(Runtime::kNewContext, 1, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5098 | void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) { |
| 5099 | // Stack layout on entry: |
| 5100 | // |
| 5101 | // [sp]: constant elements. |
| 5102 | // [sp + kPointerSize]: literal index. |
| 5103 | // [sp + (2 * kPointerSize)]: literals array. |
| 5104 | |
| 5105 | // All sizes here are multiples of kPointerSize. |
| 5106 | int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0; |
| 5107 | int size = JSArray::kSize + elements_size; |
| 5108 | |
| 5109 | // Load boilerplate object into r3 and check if we need to create a |
| 5110 | // boilerplate. |
| 5111 | Label slow_case; |
| 5112 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 5113 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 5114 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 5115 | __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5116 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 5117 | __ cmp(r3, ip); |
| 5118 | __ b(eq, &slow_case); |
| 5119 | |
| 5120 | // Allocate both the JS array and the elements array in one big |
| 5121 | // allocation. This avoids multiple limit checks. |
| 5122 | __ AllocateInNewSpace(size / kPointerSize, |
| 5123 | r0, |
| 5124 | r1, |
| 5125 | r2, |
| 5126 | &slow_case, |
| 5127 | TAG_OBJECT); |
| 5128 | |
| 5129 | // Copy the JS array part. |
| 5130 | for (int i = 0; i < JSArray::kSize; i += kPointerSize) { |
| 5131 | if ((i != JSArray::kElementsOffset) || (length_ == 0)) { |
| 5132 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 5133 | __ str(r1, FieldMemOperand(r0, i)); |
| 5134 | } |
| 5135 | } |
| 5136 | |
| 5137 | if (length_ > 0) { |
| 5138 | // Get hold of the elements array of the boilerplate and setup the |
| 5139 | // elements pointer in the resulting object. |
| 5140 | __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset)); |
| 5141 | __ add(r2, r0, Operand(JSArray::kSize)); |
| 5142 | __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 5143 | |
| 5144 | // Copy the elements array. |
| 5145 | for (int i = 0; i < elements_size; i += kPointerSize) { |
| 5146 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 5147 | __ str(r1, FieldMemOperand(r2, i)); |
| 5148 | } |
| 5149 | } |
| 5150 | |
| 5151 | // Return and remove the on-stack parameters. |
| 5152 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 5153 | __ Ret(); |
| 5154 | |
| 5155 | __ bind(&slow_case); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5156 | __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5160 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 5161 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 5162 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 5163 | // scratch register. Destroys the source register. No GC occurs during this |
| 5164 | // stub so you don't have to set up the frame. |
| 5165 | class ConvertToDoubleStub : public CodeStub { |
| 5166 | public: |
| 5167 | ConvertToDoubleStub(Register result_reg_1, |
| 5168 | Register result_reg_2, |
| 5169 | Register source_reg, |
| 5170 | Register scratch_reg) |
| 5171 | : result1_(result_reg_1), |
| 5172 | result2_(result_reg_2), |
| 5173 | source_(source_reg), |
| 5174 | zeros_(scratch_reg) { } |
| 5175 | |
| 5176 | private: |
| 5177 | Register result1_; |
| 5178 | Register result2_; |
| 5179 | Register source_; |
| 5180 | Register zeros_; |
| 5181 | |
| 5182 | // Minor key encoding in 16 bits. |
| 5183 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 5184 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 5185 | |
| 5186 | Major MajorKey() { return ConvertToDouble; } |
| 5187 | int MinorKey() { |
| 5188 | // Encode the parameters in a unique 16 bit value. |
| 5189 | return result1_.code() + |
| 5190 | (result2_.code() << 4) + |
| 5191 | (source_.code() << 8) + |
| 5192 | (zeros_.code() << 12); |
| 5193 | } |
| 5194 | |
| 5195 | void Generate(MacroAssembler* masm); |
| 5196 | |
| 5197 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 5198 | |
| 5199 | #ifdef DEBUG |
| 5200 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 5201 | #endif |
| 5202 | }; |
| 5203 | |
| 5204 | |
| 5205 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 5206 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 5207 | Register exponent = result1_; |
| 5208 | Register mantissa = result2_; |
| 5209 | #else |
| 5210 | Register exponent = result2_; |
| 5211 | Register mantissa = result1_; |
| 5212 | #endif |
| 5213 | Label not_special; |
| 5214 | // Convert from Smi to integer. |
| 5215 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 5216 | // Move sign bit from source to destination. This works because the sign bit |
| 5217 | // in the exponent word of the double has the same position and polarity as |
| 5218 | // the 2's complement sign bit in a Smi. |
| 5219 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 5220 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 5221 | // Subtract from 0 if source was negative. |
| 5222 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5223 | |
| 5224 | // We have -1, 0 or 1, which we treat specially. Register source_ contains |
| 5225 | // absolute value: it is either equal to 1 (special case of -1 and 1), |
| 5226 | // greater than 1 (not a special case) or less than 1 (special case of 0). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5227 | __ cmp(source_, Operand(1)); |
| 5228 | __ b(gt, ¬_special); |
| 5229 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5230 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 5231 | static const uint32_t exponent_word_for_1 = |
| 5232 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5233 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5234 | // 1, 0 and -1 all have 0 for the second word. |
| 5235 | __ mov(mantissa, Operand(0)); |
| 5236 | __ Ret(); |
| 5237 | |
| 5238 | __ bind(¬_special); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5239 | // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5240 | // Gets the wrong answer for 0, but we already checked for that case above. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5241 | __ CountLeadingZeros(source_, mantissa, zeros_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5242 | // Compute exponent and or it into the exponent register. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5243 | // We use mantissa as a scratch register here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5244 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 5245 | __ orr(exponent, |
| 5246 | exponent, |
| 5247 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 5248 | // Shift up the source chopping the top bit off. |
| 5249 | __ add(zeros_, zeros_, Operand(1)); |
| 5250 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 5251 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 5252 | // Compute lower part of fraction (last 12 bits). |
| 5253 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 5254 | // And the top (top 20 bits). |
| 5255 | __ orr(exponent, |
| 5256 | exponent, |
| 5257 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 5258 | __ Ret(); |
| 5259 | } |
| 5260 | |
| 5261 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5262 | // See comment for class. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5263 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5264 | Label max_negative_int; |
| 5265 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 5266 | // We test for the special value that has a different exponent. This test |
| 5267 | // has the neat side effect of setting the flags according to the sign. |
| 5268 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5269 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5270 | __ b(eq, &max_negative_int); |
| 5271 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 5272 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 5273 | uint32_t non_smi_exponent = |
| 5274 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5275 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 5276 | // Set the sign bit in scratch_ if the value was negative. |
| 5277 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 5278 | // Subtract from 0 if the value was negative. |
| 5279 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 5280 | // We should be masking the implict first digit of the mantissa away here, |
| 5281 | // but it just ends up combining harmlessly with the last digit of the |
| 5282 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 5283 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 5284 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 5285 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5286 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 5287 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 5288 | HeapNumber::kExponentOffset)); |
| 5289 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 5290 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 5291 | HeapNumber::kMantissaOffset)); |
| 5292 | __ Ret(); |
| 5293 | |
| 5294 | __ bind(&max_negative_int); |
| 5295 | // The max negative int32 is stored as a positive number in the mantissa of |
| 5296 | // a double because it uses a sign bit instead of using two's complement. |
| 5297 | // The actual mantissa bits stored are all 0 because the implicit most |
| 5298 | // significant 1 bit is not stored. |
| 5299 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 5300 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 5301 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 5302 | __ mov(ip, Operand(0)); |
| 5303 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 5304 | __ Ret(); |
| 5305 | } |
| 5306 | |
| 5307 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5308 | // Handle the case where the lhs and rhs are the same object. |
| 5309 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 5310 | // for "identity and not NaN". |
| 5311 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 5312 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5313 | Condition cc, |
| 5314 | bool never_nan_nan) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5315 | Label not_identical; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5316 | Label heap_number, return_equal; |
| 5317 | Register exp_mask_reg = r5; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5318 | __ cmp(r0, Operand(r1)); |
| 5319 | __ b(ne, ¬_identical); |
| 5320 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5321 | // The two objects are identical. If we know that one of them isn't NaN then |
| 5322 | // we now know they test equal. |
| 5323 | if (cc != eq || !never_nan_nan) { |
| 5324 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5325 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5326 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 5327 | // so we do the second best thing - test it ourselves. |
| 5328 | // They are both equal and they are not both Smis so both of them are not |
| 5329 | // Smis. If it's not a heap number, then return equal. |
| 5330 | if (cc == lt || cc == gt) { |
| 5331 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5332 | __ b(ge, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5333 | } else { |
| 5334 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5335 | __ b(eq, &heap_number); |
| 5336 | // Comparing JS objects with <=, >= is complicated. |
| 5337 | if (cc != eq) { |
| 5338 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 5339 | __ b(ge, slow); |
| 5340 | // Normally here we fall through to return_equal, but undefined is |
| 5341 | // special: (undefined == undefined) == true, but |
| 5342 | // (undefined <= undefined) == false! See ECMAScript 11.8.5. |
| 5343 | if (cc == le || cc == ge) { |
| 5344 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 5345 | __ b(ne, &return_equal); |
| 5346 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 5347 | __ cmp(r0, Operand(r2)); |
| 5348 | __ b(ne, &return_equal); |
| 5349 | if (cc == le) { |
| 5350 | // undefined <= undefined should fail. |
| 5351 | __ mov(r0, Operand(GREATER)); |
| 5352 | } else { |
| 5353 | // undefined >= undefined should fail. |
| 5354 | __ mov(r0, Operand(LESS)); |
| 5355 | } |
| 5356 | __ mov(pc, Operand(lr)); // Return. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5357 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5358 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5359 | } |
| 5360 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5361 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5362 | __ bind(&return_equal); |
| 5363 | if (cc == lt) { |
| 5364 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 5365 | } else if (cc == gt) { |
| 5366 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 5367 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5368 | __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5369 | } |
| 5370 | __ mov(pc, Operand(lr)); // Return. |
| 5371 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5372 | if (cc != eq || !never_nan_nan) { |
| 5373 | // For less and greater we don't have to check for NaN since the result of |
| 5374 | // x < x is false regardless. For the others here is some code to check |
| 5375 | // for NaN. |
| 5376 | if (cc != lt && cc != gt) { |
| 5377 | __ bind(&heap_number); |
| 5378 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 5379 | // not NaN. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5380 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5381 | // The representation of NaN values has all exponent bits (52..62) set, |
| 5382 | // and not all mantissa bits (0..51) clear. |
| 5383 | // Read top bits of double representation (second word of value). |
| 5384 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5385 | // Test that exponent bits are all set. |
| 5386 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 5387 | __ cmp(r3, Operand(exp_mask_reg)); |
| 5388 | __ b(ne, &return_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5389 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5390 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 5391 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 5392 | // Or with all low-bits of mantissa. |
| 5393 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 5394 | __ orr(r0, r3, Operand(r2), SetCC); |
| 5395 | // 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] | 5396 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if |
| 5397 | // not (it's a NaN). For <= and >= we need to load r0 with the failing |
| 5398 | // value if it's a NaN. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5399 | if (cc != eq) { |
| 5400 | // All-zero means Infinity means equal. |
| 5401 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 5402 | if (cc == le) { |
| 5403 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 5404 | } else { |
| 5405 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 5406 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5407 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5408 | __ mov(pc, Operand(lr)); // Return. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5409 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5410 | // No fall through here. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5411 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5412 | |
| 5413 | __ bind(¬_identical); |
| 5414 | } |
| 5415 | |
| 5416 | |
| 5417 | // See comment at call site. |
| 5418 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5419 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5420 | Label* slow, |
| 5421 | bool strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5422 | Label rhs_is_smi; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5423 | __ tst(r0, Operand(kSmiTagMask)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5424 | __ b(eq, &rhs_is_smi); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5425 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5426 | // 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] | 5427 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5428 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5429 | // 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] | 5430 | // succeed. Return non-equal (r0 is already not zero) |
| 5431 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 5432 | } else { |
| 5433 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 5434 | // the runtime. |
| 5435 | __ b(ne, slow); |
| 5436 | } |
| 5437 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5438 | // Lhs (r1) is a smi, rhs (r0) is a number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5439 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5440 | // Convert lhs to a double in d7 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5441 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5442 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5443 | __ vmov(s15, r7); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5444 | __ vcvt_f64_s32(d7, s15); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5445 | // Load the double from rhs, tagged HeapNumber r0, to d6. |
| 5446 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5447 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5448 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5449 | __ push(lr); |
| 5450 | // Convert lhs to a double in r2, r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5451 | __ mov(r7, Operand(r1)); |
| 5452 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5453 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5454 | // Load rhs to a double in r0, r1. |
| 5455 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5456 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5457 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5460 | // 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] | 5461 | // since it's a smi. |
| 5462 | __ jmp(lhs_not_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5463 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5464 | __ bind(&rhs_is_smi); |
| 5465 | // 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] | 5466 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5467 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5468 | // 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] | 5469 | // succeed. Return non-equal. |
| 5470 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 5471 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 5472 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5473 | // 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] | 5474 | // the runtime. |
| 5475 | __ b(ne, slow); |
| 5476 | } |
| 5477 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5478 | // Rhs (r0) is a smi, lhs (r1) is a heap number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5479 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5480 | // Convert rhs to a double in d6 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5481 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5482 | // Load the double from lhs, tagged HeapNumber r1, to d7. |
| 5483 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5484 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5485 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5486 | __ vmov(s13, r7); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5487 | __ vcvt_f64_s32(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5488 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5489 | __ push(lr); |
| 5490 | // Load lhs to a double in r2, r3. |
| 5491 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5492 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5493 | // Convert rhs to a double in r0, r1. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5494 | __ mov(r7, Operand(r0)); |
| 5495 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5496 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5497 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5498 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5499 | // Fall through to both_loaded_as_doubles. |
| 5500 | } |
| 5501 | |
| 5502 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5503 | void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5504 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5505 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5506 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5507 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5508 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5509 | Label one_is_nan, neither_is_nan; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5510 | Label lhs_not_nan_exp_mask_is_loaded; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5511 | |
| 5512 | Register exp_mask_reg = r5; |
| 5513 | |
| 5514 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5515 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 5516 | __ cmp(r4, Operand(exp_mask_reg)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5517 | __ b(ne, &lhs_not_nan_exp_mask_is_loaded); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5518 | __ mov(r4, |
| 5519 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5520 | SetCC); |
| 5521 | __ b(ne, &one_is_nan); |
| 5522 | __ cmp(lhs_mantissa, Operand(0)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5523 | __ b(ne, &one_is_nan); |
| 5524 | |
| 5525 | __ bind(lhs_not_nan); |
| 5526 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 5527 | __ bind(&lhs_not_nan_exp_mask_is_loaded); |
| 5528 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 5529 | __ cmp(r4, Operand(exp_mask_reg)); |
| 5530 | __ b(ne, &neither_is_nan); |
| 5531 | __ mov(r4, |
| 5532 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5533 | SetCC); |
| 5534 | __ b(ne, &one_is_nan); |
| 5535 | __ cmp(rhs_mantissa, Operand(0)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5536 | __ b(eq, &neither_is_nan); |
| 5537 | |
| 5538 | __ bind(&one_is_nan); |
| 5539 | // NaN comparisons always fail. |
| 5540 | // Load whatever we need in r0 to make the comparison fail. |
| 5541 | if (cc == lt || cc == le) { |
| 5542 | __ mov(r0, Operand(GREATER)); |
| 5543 | } else { |
| 5544 | __ mov(r0, Operand(LESS)); |
| 5545 | } |
| 5546 | __ mov(pc, Operand(lr)); // Return. |
| 5547 | |
| 5548 | __ bind(&neither_is_nan); |
| 5549 | } |
| 5550 | |
| 5551 | |
| 5552 | // See comment at call site. |
| 5553 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 5554 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5555 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5556 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5557 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5558 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5559 | |
| 5560 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 5561 | if (cc == eq) { |
| 5562 | // Doubles are not equal unless they have the same bit pattern. |
| 5563 | // Exception: 0 and -0. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5564 | __ cmp(rhs_mantissa, Operand(lhs_mantissa)); |
| 5565 | __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5566 | // Return non-zero if the numbers are unequal. |
| 5567 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 5568 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5569 | __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5570 | // If exponents are equal then return 0. |
| 5571 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 5572 | |
| 5573 | // Exponents are unequal. The only way we can return that the numbers |
| 5574 | // are equal is if one is -0 and the other is 0. We already dealt |
| 5575 | // with the case where both are -0 or both are 0. |
| 5576 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 5577 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 5578 | // equal. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5579 | __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5580 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 5581 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 5582 | // Now they are equal if and only if the lhs exponent is zero in its |
| 5583 | // low 31 bits. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5584 | __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5585 | __ mov(pc, Operand(lr)); |
| 5586 | } else { |
| 5587 | // Call a native function to do a comparison between two non-NaNs. |
| 5588 | // Call C routine that may not cause GC or other trouble. |
| 5589 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 5590 | __ Jump(r5); // Tail call. |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | |
| 5595 | // See comment at call site. |
| 5596 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 5597 | // If either operand is a JSObject or an oddball value, then they are |
| 5598 | // not equal since their pointers are different. |
| 5599 | // There is no test for undetectability in strict equality. |
| 5600 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 5601 | Label first_non_object; |
| 5602 | // Get the type of the first operand into r2 and compare it with |
| 5603 | // FIRST_JS_OBJECT_TYPE. |
| 5604 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 5605 | __ b(lt, &first_non_object); |
| 5606 | |
| 5607 | // Return non-zero (r0 is not zero) |
| 5608 | Label return_not_equal; |
| 5609 | __ bind(&return_not_equal); |
| 5610 | __ mov(pc, Operand(lr)); // Return. |
| 5611 | |
| 5612 | __ bind(&first_non_object); |
| 5613 | // Check for oddballs: true, false, null, undefined. |
| 5614 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 5615 | __ b(eq, &return_not_equal); |
| 5616 | |
| 5617 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 5618 | __ b(ge, &return_not_equal); |
| 5619 | |
| 5620 | // Check for oddballs: true, false, null, undefined. |
| 5621 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 5622 | __ b(eq, &return_not_equal); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5623 | |
| 5624 | // Now that we have the types we might as well check for symbol-symbol. |
| 5625 | // Ensure that no non-strings have the symbol bit set. |
| 5626 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5627 | ASSERT(kSymbolTag != 0); |
| 5628 | __ and_(r2, r2, Operand(r3)); |
| 5629 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5630 | __ b(ne, &return_not_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | |
| 5634 | // See comment at call site. |
| 5635 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 5636 | Label* both_loaded_as_doubles, |
| 5637 | Label* not_heap_numbers, |
| 5638 | Label* slow) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5639 | __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5640 | __ b(ne, not_heap_numbers); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5641 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5642 | __ cmp(r2, r3); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5643 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 5644 | |
| 5645 | // Both are heap numbers. Load them up then jump to the code we have |
| 5646 | // for that. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5647 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5648 | CpuFeatures::Scope scope(VFP3); |
| 5649 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5650 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5651 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5652 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5653 | } else { |
| 5654 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5655 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5656 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5657 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5658 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5659 | __ jmp(both_loaded_as_doubles); |
| 5660 | } |
| 5661 | |
| 5662 | |
| 5663 | // Fast negative check for symbol-to-symbol equality. |
| 5664 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 5665 | // r2 is object type of r0. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5666 | // Ensure that no non-strings have the symbol bit set. |
| 5667 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5668 | ASSERT(kSymbolTag != 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5669 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5670 | __ b(eq, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5671 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5672 | __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5673 | __ tst(r3, Operand(kIsSymbolMask)); |
| 5674 | __ b(eq, slow); |
| 5675 | |
| 5676 | // Both are symbols. We already checked they weren't the same pointer |
| 5677 | // so they are not equal. |
| 5678 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 5679 | __ mov(pc, Operand(lr)); // Return. |
| 5680 | } |
| 5681 | |
| 5682 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5683 | void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm, |
| 5684 | Register object, |
| 5685 | Register result, |
| 5686 | Register scratch1, |
| 5687 | Register scratch2, |
| 5688 | bool object_is_smi, |
| 5689 | Label* not_found) { |
| 5690 | // Currently only lookup for smis. Check for smi if object is not known to be |
| 5691 | // a smi. |
| 5692 | if (!object_is_smi) { |
| 5693 | ASSERT(kSmiTag == 0); |
| 5694 | __ tst(object, Operand(kSmiTagMask)); |
| 5695 | __ b(ne, not_found); |
| 5696 | } |
| 5697 | |
| 5698 | // Use of registers. Register result is used as a temporary. |
| 5699 | Register number_string_cache = result; |
| 5700 | Register mask = scratch1; |
| 5701 | Register scratch = scratch2; |
| 5702 | |
| 5703 | // Load the number string cache. |
| 5704 | __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex); |
| 5705 | |
| 5706 | // Make the hash mask from the length of the number string cache. It |
| 5707 | // contains two elements (number and string) for each cache entry. |
| 5708 | __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset)); |
| 5709 | // Divide length by two (length is not a smi). |
| 5710 | __ mov(mask, Operand(mask, ASR, 1)); |
| 5711 | __ sub(mask, mask, Operand(1)); // Make mask. |
| 5712 | |
| 5713 | // Calculate the entry in the number string cache. The hash value in the |
| 5714 | // number string cache for smis is just the smi value. |
| 5715 | __ and_(scratch, mask, Operand(object, ASR, 1)); |
| 5716 | |
| 5717 | // Calculate address of entry in string cache: each entry consists |
| 5718 | // of two pointer sized fields. |
| 5719 | __ add(scratch, |
| 5720 | number_string_cache, |
| 5721 | Operand(scratch, LSL, kPointerSizeLog2 + 1)); |
| 5722 | |
| 5723 | // Check if the entry is the smi we are looking for. |
| 5724 | Register object1 = scratch1; |
| 5725 | __ ldr(object1, FieldMemOperand(scratch, FixedArray::kHeaderSize)); |
| 5726 | __ cmp(object, object1); |
| 5727 | __ b(ne, not_found); |
| 5728 | |
| 5729 | // Get the result from the cache. |
| 5730 | __ ldr(result, |
| 5731 | FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize)); |
| 5732 | |
| 5733 | __ IncrementCounter(&Counters::number_to_string_native, |
| 5734 | 1, |
| 5735 | scratch1, |
| 5736 | scratch2); |
| 5737 | } |
| 5738 | |
| 5739 | |
| 5740 | void NumberToStringStub::Generate(MacroAssembler* masm) { |
| 5741 | Label runtime; |
| 5742 | |
| 5743 | __ ldr(r1, MemOperand(sp, 0)); |
| 5744 | |
| 5745 | // Generate code to lookup number in the number string cache. |
| 5746 | GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, false, &runtime); |
| 5747 | __ add(sp, sp, Operand(1 * kPointerSize)); |
| 5748 | __ Ret(); |
| 5749 | |
| 5750 | __ bind(&runtime); |
| 5751 | // Handle number to string in the runtime system if not found in the cache. |
| 5752 | __ TailCallRuntime(Runtime::kNumberToString, 1, 1); |
| 5753 | } |
| 5754 | |
| 5755 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5756 | // On entry r0 (rhs) and r1 (lhs) are the values to be compared. |
| 5757 | // On exit r0 is 0, positive or negative to indicate the result of |
| 5758 | // the comparison. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5759 | void CompareStub::Generate(MacroAssembler* masm) { |
| 5760 | Label slow; // Call builtin. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5761 | Label not_smis, both_loaded_as_doubles, lhs_not_nan; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5762 | |
| 5763 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 5764 | // it is certain that at least one operand isn't a smi. |
| 5765 | |
| 5766 | // Handle the case where the objects are identical. Either returns the answer |
| 5767 | // 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] | 5768 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5769 | |
| 5770 | // If either is a Smi (we know that not both are), then they can only |
| 5771 | // be strictly equal if the other is a HeapNumber. |
| 5772 | ASSERT_EQ(0, kSmiTag); |
| 5773 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 5774 | __ and_(r2, r0, Operand(r1)); |
| 5775 | __ tst(r2, Operand(kSmiTagMask)); |
| 5776 | __ b(ne, ¬_smis); |
| 5777 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 5778 | // 1) Return the answer. |
| 5779 | // 2) Go to slow. |
| 5780 | // 3) Fall through to both_loaded_as_doubles. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5781 | // 4) Jump to lhs_not_nan. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5782 | // 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] | 5783 | // comparison. If VFP3 is supported the double values of the numbers have |
| 5784 | // been loaded into d7 and d6. Otherwise, the double values have been loaded |
| 5785 | // into r0, r1, r2, and r3. |
| 5786 | EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5787 | |
| 5788 | __ bind(&both_loaded_as_doubles); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5789 | // The arguments have been converted to doubles and stored in d6 and d7, if |
| 5790 | // VFP3 is supported, or in r0, r1, r2, and r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5791 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5792 | __ bind(&lhs_not_nan); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5793 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5794 | Label no_nan; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5795 | // ARMv7 VFP3 instructions to implement double precision comparison. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5796 | __ vcmp(d7, d6); |
| 5797 | __ vmrs(pc); // Move vector status bits to normal status bits. |
| 5798 | Label nan; |
| 5799 | __ b(vs, &nan); |
| 5800 | __ mov(r0, Operand(EQUAL), LeaveCC, eq); |
| 5801 | __ mov(r0, Operand(LESS), LeaveCC, lt); |
| 5802 | __ mov(r0, Operand(GREATER), LeaveCC, gt); |
| 5803 | __ mov(pc, Operand(lr)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5804 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5805 | __ bind(&nan); |
| 5806 | // If one of the sides was a NaN then the v flag is set. Load r0 with |
| 5807 | // whatever it takes to make the comparison fail, since comparisons with NaN |
| 5808 | // always fail. |
| 5809 | if (cc_ == lt || cc_ == le) { |
| 5810 | __ mov(r0, Operand(GREATER)); |
| 5811 | } else { |
| 5812 | __ mov(r0, Operand(LESS)); |
| 5813 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5814 | __ mov(pc, Operand(lr)); |
| 5815 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5816 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 5817 | // fall through if neither is a NaN. Also binds lhs_not_nan. |
| 5818 | EmitNanCheck(masm, &lhs_not_nan, cc_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5819 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 5820 | // answer. Never falls through. |
| 5821 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 5822 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5823 | |
| 5824 | __ bind(¬_smis); |
| 5825 | // At this point we know we are dealing with two different objects, |
| 5826 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 5827 | if (strict_) { |
| 5828 | // This returns non-equal for some object types, or falls through if it |
| 5829 | // was not lucky. |
| 5830 | EmitStrictTwoHeapObjectCompare(masm); |
| 5831 | } |
| 5832 | |
| 5833 | Label check_for_symbols; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5834 | Label flat_string_check; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5835 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 5836 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 5837 | // 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] | 5838 | // 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] | 5839 | EmitCheckForTwoHeapNumbers(masm, |
| 5840 | &both_loaded_as_doubles, |
| 5841 | &check_for_symbols, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5842 | &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5843 | |
| 5844 | __ bind(&check_for_symbols); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5845 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 5846 | // symbols. |
| 5847 | if (cc_ == eq && !strict_) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5848 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5849 | // of r0 on entry. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5850 | EmitCheckForSymbols(masm, &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5851 | } |
| 5852 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5853 | // Check for both being sequential ASCII strings, and inline if that is the |
| 5854 | // case. |
| 5855 | __ bind(&flat_string_check); |
| 5856 | |
| 5857 | __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow); |
| 5858 | |
| 5859 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 5860 | StringCompareStub::GenerateCompareFlatAsciiStrings(masm, |
| 5861 | r1, |
| 5862 | r0, |
| 5863 | r2, |
| 5864 | r3, |
| 5865 | r4, |
| 5866 | r5); |
| 5867 | // Never falls through to here. |
| 5868 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5869 | __ bind(&slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5870 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5871 | __ push(r1); |
| 5872 | __ push(r0); |
| 5873 | // Figure out which native to call and setup the arguments. |
| 5874 | Builtins::JavaScript native; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5875 | if (cc_ == eq) { |
| 5876 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 5877 | } else { |
| 5878 | native = Builtins::COMPARE; |
| 5879 | int ncr; // NaN compare result |
| 5880 | if (cc_ == lt || cc_ == le) { |
| 5881 | ncr = GREATER; |
| 5882 | } else { |
| 5883 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 5884 | ncr = LESS; |
| 5885 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5886 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 5887 | __ push(r0); |
| 5888 | } |
| 5889 | |
| 5890 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 5891 | // tagged as a small integer. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5892 | __ InvokeBuiltin(native, JUMP_JS); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5896 | // We fall into this code if the operands were Smis, but the result was |
| 5897 | // 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] | 5898 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 5899 | // to call the C-implemented binary fp operation routines we need to end up |
| 5900 | // with the double precision floating point operands in r0 and r1 (for the |
| 5901 | // value in r1) and r2 and r3 (for the value in r0). |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5902 | void GenericBinaryOpStub::HandleBinaryOpSlowCases( |
| 5903 | MacroAssembler* masm, |
| 5904 | Label* not_smi, |
| 5905 | Register lhs, |
| 5906 | Register rhs, |
| 5907 | const Builtins::JavaScript& builtin) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5908 | Label slow, slow_pop_2_first, do_the_call; |
| 5909 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5910 | bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5911 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5912 | ASSERT((lhs.is(r0) && rhs.is(r1)) || lhs.is(r1) && rhs.is(r0)); |
| 5913 | |
| 5914 | if (ShouldGenerateSmiCode()) { |
| 5915 | // Smi-smi case (overflow). |
| 5916 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 5917 | // The new heap number is in r5. r6 and r7 are scratch. |
| 5918 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
| 5919 | |
| 5920 | // If we have floating point hardware, inline ADD, SUB, MUL, and DIV, |
| 5921 | // using registers d7 and d6 for the double values. |
| 5922 | if (use_fp_registers) { |
| 5923 | CpuFeatures::Scope scope(VFP3); |
| 5924 | __ mov(r7, Operand(rhs, ASR, kSmiTagSize)); |
| 5925 | __ vmov(s15, r7); |
| 5926 | __ vcvt_f64_s32(d7, s15); |
| 5927 | __ mov(r7, Operand(lhs, ASR, kSmiTagSize)); |
| 5928 | __ vmov(s13, r7); |
| 5929 | __ vcvt_f64_s32(d6, s13); |
| 5930 | } else { |
| 5931 | // Write Smi from rhs to r3 and r2 in double format. r6 is scratch. |
| 5932 | __ mov(r7, Operand(rhs)); |
| 5933 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5934 | __ push(lr); |
| 5935 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 5936 | // Write Smi from lhs to r1 and r0 in double format. r6 is scratch. |
| 5937 | __ mov(r7, Operand(lhs)); |
| 5938 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5939 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 5940 | __ pop(lr); |
| 5941 | } |
| 5942 | __ jmp(&do_the_call); // Tail call. No return. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5943 | } |
| 5944 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5945 | // We branch here if at least one of r0 and r1 is not a Smi. |
| 5946 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5947 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5948 | if (lhs.is(r0)) { |
| 5949 | __ Swap(r0, r1, ip); |
| 5950 | } |
| 5951 | |
| 5952 | if (ShouldGenerateFPCode()) { |
| 5953 | if (runtime_operands_type_ == BinaryOpIC::DEFAULT) { |
| 5954 | switch (op_) { |
| 5955 | case Token::ADD: |
| 5956 | case Token::SUB: |
| 5957 | case Token::MUL: |
| 5958 | case Token::DIV: |
| 5959 | GenerateTypeTransition(masm); |
| 5960 | break; |
| 5961 | |
| 5962 | default: |
| 5963 | break; |
| 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | if (mode_ == NO_OVERWRITE) { |
| 5968 | // In the case where there is no chance of an overwritable float we may as |
| 5969 | // well do the allocation immediately while r0 and r1 are untouched. |
| 5970 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
| 5971 | } |
| 5972 | |
| 5973 | // Move r0 to a double in r2-r3. |
| 5974 | __ tst(r0, Operand(kSmiTagMask)); |
| 5975 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5976 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5977 | __ b(ne, &slow); |
| 5978 | if (mode_ == OVERWRITE_RIGHT) { |
| 5979 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5980 | } |
| 5981 | if (use_fp_registers) { |
| 5982 | CpuFeatures::Scope scope(VFP3); |
| 5983 | // Load the double from tagged HeapNumber r0 to d7. |
| 5984 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5985 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5986 | } else { |
| 5987 | // Calling convention says that second double is in r2 and r3. |
| 5988 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5989 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5990 | } |
| 5991 | __ jmp(&finished_loading_r0); |
| 5992 | __ bind(&r0_is_smi); |
| 5993 | if (mode_ == OVERWRITE_RIGHT) { |
| 5994 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5995 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
| 5996 | } |
| 5997 | |
| 5998 | if (use_fp_registers) { |
| 5999 | CpuFeatures::Scope scope(VFP3); |
| 6000 | // Convert smi in r0 to double in d7. |
| 6001 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 6002 | __ vmov(s15, r7); |
| 6003 | __ vcvt_f64_s32(d7, s15); |
| 6004 | } else { |
| 6005 | // Write Smi from r0 to r3 and r2 in double format. |
| 6006 | __ mov(r7, Operand(r0)); |
| 6007 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 6008 | __ push(lr); |
| 6009 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 6010 | __ pop(lr); |
| 6011 | } |
| 6012 | |
| 6013 | __ bind(&finished_loading_r0); |
| 6014 | |
| 6015 | // Move r1 to a double in r0-r1. |
| 6016 | __ tst(r1, Operand(kSmiTagMask)); |
| 6017 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 6018 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 6019 | __ b(ne, &slow); |
| 6020 | if (mode_ == OVERWRITE_LEFT) { |
| 6021 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
| 6022 | } |
| 6023 | if (use_fp_registers) { |
| 6024 | CpuFeatures::Scope scope(VFP3); |
| 6025 | // Load the double from tagged HeapNumber r1 to d6. |
| 6026 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 6027 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 6028 | } else { |
| 6029 | // Calling convention says that first double is in r0 and r1. |
| 6030 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 6031 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 6032 | } |
| 6033 | __ jmp(&finished_loading_r1); |
| 6034 | __ bind(&r1_is_smi); |
| 6035 | if (mode_ == OVERWRITE_LEFT) { |
| 6036 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 6037 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
| 6038 | } |
| 6039 | |
| 6040 | if (use_fp_registers) { |
| 6041 | CpuFeatures::Scope scope(VFP3); |
| 6042 | // Convert smi in r1 to double in d6. |
| 6043 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 6044 | __ vmov(s13, r7); |
| 6045 | __ vcvt_f64_s32(d6, s13); |
| 6046 | } else { |
| 6047 | // Write Smi from r1 to r1 and r0 in double format. |
| 6048 | __ mov(r7, Operand(r1)); |
| 6049 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 6050 | __ push(lr); |
| 6051 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 6052 | __ pop(lr); |
| 6053 | } |
| 6054 | |
| 6055 | __ bind(&finished_loading_r1); |
| 6056 | |
| 6057 | __ bind(&do_the_call); |
| 6058 | // If we are inlining the operation using VFP3 instructions for |
| 6059 | // add, subtract, multiply, or divide, the arguments are in d6 and d7. |
| 6060 | if (use_fp_registers) { |
| 6061 | CpuFeatures::Scope scope(VFP3); |
| 6062 | // ARMv7 VFP3 instructions to implement |
| 6063 | // double precision, add, subtract, multiply, divide. |
| 6064 | |
| 6065 | if (Token::MUL == op_) { |
| 6066 | __ vmul(d5, d6, d7); |
| 6067 | } else if (Token::DIV == op_) { |
| 6068 | __ vdiv(d5, d6, d7); |
| 6069 | } else if (Token::ADD == op_) { |
| 6070 | __ vadd(d5, d6, d7); |
| 6071 | } else if (Token::SUB == op_) { |
| 6072 | __ vsub(d5, d6, d7); |
| 6073 | } else { |
| 6074 | UNREACHABLE(); |
| 6075 | } |
| 6076 | __ sub(r0, r5, Operand(kHeapObjectTag)); |
| 6077 | __ vstr(d5, r0, HeapNumber::kValueOffset); |
| 6078 | __ add(r0, r0, Operand(kHeapObjectTag)); |
| 6079 | __ mov(pc, lr); |
| 6080 | } else { |
| 6081 | // If we did not inline the operation, then the arguments are in: |
| 6082 | // r0: Left value (least significant part of mantissa). |
| 6083 | // r1: Left value (sign, exponent, top of mantissa). |
| 6084 | // r2: Right value (least significant part of mantissa). |
| 6085 | // r3: Right value (sign, exponent, top of mantissa). |
| 6086 | // r5: Address of heap number for result. |
| 6087 | |
| 6088 | __ push(lr); // For later. |
| 6089 | __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments. |
| 6090 | // Call C routine that may not cause GC or other trouble. r5 is callee |
| 6091 | // save. |
| 6092 | __ CallCFunction(ExternalReference::double_fp_operation(op_), 4); |
| 6093 | // Store answer in the overwritable heap number. |
| 6094 | #if !defined(USE_ARM_EABI) |
| 6095 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 6096 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 6097 | // substract the tag from r5. |
| 6098 | __ sub(r4, r5, Operand(kHeapObjectTag)); |
| 6099 | __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset)); |
| 6100 | #else |
| 6101 | // Double returned in registers 0 and 1. |
| 6102 | __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset)); |
| 6103 | __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4)); |
| 6104 | #endif |
| 6105 | __ mov(r0, Operand(r5)); |
| 6106 | // And we are done. |
| 6107 | __ pop(pc); |
| 6108 | } |
| 6109 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6110 | // We jump to here if something goes wrong (one param is not a number of any |
| 6111 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6112 | __ bind(&slow); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6113 | |
| 6114 | // Push arguments to the stack |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6115 | __ push(r1); |
| 6116 | __ push(r0); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6117 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6118 | if (Token::ADD == op_) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6119 | // Test for string arguments before calling runtime. |
| 6120 | // r1 : first argument |
| 6121 | // r0 : second argument |
| 6122 | // sp[0] : second argument |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6123 | // sp[4] : first argument |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6124 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6125 | Label not_strings, not_string1, string1, string1_smi2; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6126 | __ tst(r1, Operand(kSmiTagMask)); |
| 6127 | __ b(eq, ¬_string1); |
| 6128 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 6129 | __ b(ge, ¬_string1); |
| 6130 | |
| 6131 | // First argument is a a string, test second. |
| 6132 | __ tst(r0, Operand(kSmiTagMask)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6133 | __ b(eq, &string1_smi2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6134 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 6135 | __ b(ge, &string1); |
| 6136 | |
| 6137 | // First and second argument are strings. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6138 | StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB); |
| 6139 | __ TailCallStub(&string_add_stub); |
| 6140 | |
| 6141 | __ bind(&string1_smi2); |
| 6142 | // First argument is a string, second is a smi. Try to lookup the number |
| 6143 | // string for the smi in the number string cache. |
| 6144 | NumberToStringStub::GenerateLookupNumberStringCache( |
| 6145 | masm, r0, r2, r4, r5, true, &string1); |
| 6146 | |
| 6147 | // Replace second argument on stack and tailcall string add stub to make |
| 6148 | // the result. |
| 6149 | __ str(r2, MemOperand(sp, 0)); |
| 6150 | __ TailCallStub(&string_add_stub); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6151 | |
| 6152 | // Only first argument is a string. |
| 6153 | __ bind(&string1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6154 | __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS); |
| 6155 | |
| 6156 | // First argument was not a string, test second. |
| 6157 | __ bind(¬_string1); |
| 6158 | __ tst(r0, Operand(kSmiTagMask)); |
| 6159 | __ b(eq, ¬_strings); |
| 6160 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 6161 | __ b(ge, ¬_strings); |
| 6162 | |
| 6163 | // Only second argument is a string. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6164 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 6165 | |
| 6166 | __ bind(¬_strings); |
| 6167 | } |
| 6168 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6169 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6170 | } |
| 6171 | |
| 6172 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6173 | // 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] | 6174 | // 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] | 6175 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 6176 | // 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] | 6177 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 6178 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6179 | static void GetInt32(MacroAssembler* masm, |
| 6180 | Register source, |
| 6181 | Register dest, |
| 6182 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6183 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6184 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6185 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6186 | // Get exponent word. |
| 6187 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 6188 | // Get exponent alone in scratch2. |
| 6189 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6190 | // Load dest with zero. We use this either for the final shift or |
| 6191 | // for the answer. |
| 6192 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6193 | // 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] | 6194 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 6195 | // the exponent that we are fastest at and also the highest exponent we can |
| 6196 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6197 | const uint32_t non_smi_exponent = |
| 6198 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 6199 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6200 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 6201 | __ b(eq, &right_exponent); |
| 6202 | // If the exponent is higher than that then go to slow case. This catches |
| 6203 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 6204 | __ b(gt, slow); |
| 6205 | |
| 6206 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 6207 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 6208 | // it rounds to zero. |
| 6209 | const uint32_t zero_exponent = |
| 6210 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 6211 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 6212 | // Dest already has a Smi zero. |
| 6213 | __ b(lt, &done); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6214 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 6215 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 6216 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 6217 | // We now have the exponent in dest. Subtract from 30 to get |
| 6218 | // how much to shift down. |
| 6219 | __ rsb(dest, dest, Operand(30)); |
| 6220 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6221 | __ bind(&right_exponent); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6222 | if (CpuFeatures::IsSupported(VFP3)) { |
| 6223 | CpuFeatures::Scope scope(VFP3); |
| 6224 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 6225 | // conversion using round to zero. |
| 6226 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 6227 | __ vmov(d7, scratch2, scratch); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6228 | __ vcvt_s32_f64(s15, d7); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 6229 | __ vmov(dest, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6230 | } else { |
| 6231 | // Get the top bits of the mantissa. |
| 6232 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 6233 | // Put back the implicit 1. |
| 6234 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 6235 | // Shift up the mantissa bits to take up the space the exponent used to |
| 6236 | // take. We just orred in the implicit bit so that took care of one and |
| 6237 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 6238 | // distance. |
| 6239 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 6240 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 6241 | // Put sign in zero flag. |
| 6242 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 6243 | // Get the second half of the double. For some exponents we don't |
| 6244 | // actually need this because the bits get shifted out again, but |
| 6245 | // it's probably slower to test than just to do it. |
| 6246 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 6247 | // Shift down 22 bits to get the last 10 bits. |
| 6248 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 6249 | // Move down according to the exponent. |
| 6250 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 6251 | // Fix sign if sign bit was set. |
| 6252 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 6253 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6254 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6255 | } |
| 6256 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6257 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 6258 | // whether both inputs are either Smis or at least heap numbers that can be |
| 6259 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 6260 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 6261 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 6262 | // write the answer into (either by allocating or by overwriting). |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6263 | // On entry the operands are in lhs and rhs. On exit the answer is in r0. |
| 6264 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm, |
| 6265 | Register lhs, |
| 6266 | Register rhs) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6267 | Label slow, result_not_a_smi; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6268 | Label rhs_is_smi, lhs_is_smi; |
| 6269 | Label done_checking_rhs, done_checking_lhs; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6270 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6271 | __ tst(lhs, Operand(kSmiTagMask)); |
| 6272 | __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number. |
| 6273 | __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6274 | __ b(ne, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6275 | GetInt32(masm, lhs, r3, r5, r4, &slow); |
| 6276 | __ jmp(&done_checking_lhs); |
| 6277 | __ bind(&lhs_is_smi); |
| 6278 | __ mov(r3, Operand(lhs, ASR, 1)); |
| 6279 | __ bind(&done_checking_lhs); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6280 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6281 | __ tst(rhs, Operand(kSmiTagMask)); |
| 6282 | __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number. |
| 6283 | __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6284 | __ b(ne, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6285 | GetInt32(masm, rhs, r2, r5, r4, &slow); |
| 6286 | __ jmp(&done_checking_rhs); |
| 6287 | __ bind(&rhs_is_smi); |
| 6288 | __ mov(r2, Operand(rhs, ASR, 1)); |
| 6289 | __ bind(&done_checking_rhs); |
| 6290 | |
| 6291 | ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6292 | |
| 6293 | // r0 and r1: Original operands (Smi or heap numbers). |
| 6294 | // r2 and r3: Signed int32 operands. |
| 6295 | switch (op_) { |
| 6296 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 6297 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 6298 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 6299 | case Token::SAR: |
| 6300 | // Use only the 5 least significant bits of the shift count. |
| 6301 | __ and_(r2, r2, Operand(0x1f)); |
| 6302 | __ mov(r2, Operand(r3, ASR, r2)); |
| 6303 | break; |
| 6304 | case Token::SHR: |
| 6305 | // Use only the 5 least significant bits of the shift count. |
| 6306 | __ and_(r2, r2, Operand(0x1f)); |
| 6307 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 6308 | // SHR is special because it is required to produce a positive answer. |
| 6309 | // The code below for writing into heap numbers isn't capable of writing |
| 6310 | // the register as an unsigned int so we go to slow case if we hit this |
| 6311 | // case. |
| 6312 | __ b(mi, &slow); |
| 6313 | break; |
| 6314 | case Token::SHL: |
| 6315 | // Use only the 5 least significant bits of the shift count. |
| 6316 | __ and_(r2, r2, Operand(0x1f)); |
| 6317 | __ mov(r2, Operand(r3, LSL, r2)); |
| 6318 | break; |
| 6319 | default: UNREACHABLE(); |
| 6320 | } |
| 6321 | // check that the *signed* result fits in a smi |
| 6322 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 6323 | __ b(mi, &result_not_a_smi); |
| 6324 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 6325 | __ Ret(); |
| 6326 | |
| 6327 | Label have_to_allocate, got_a_heap_number; |
| 6328 | __ bind(&result_not_a_smi); |
| 6329 | switch (mode_) { |
| 6330 | case OVERWRITE_RIGHT: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6331 | __ tst(rhs, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6332 | __ b(eq, &have_to_allocate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6333 | __ mov(r5, Operand(rhs)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6334 | break; |
| 6335 | } |
| 6336 | case OVERWRITE_LEFT: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6337 | __ tst(lhs, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6338 | __ b(eq, &have_to_allocate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6339 | __ mov(r5, Operand(lhs)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6340 | break; |
| 6341 | } |
| 6342 | case NO_OVERWRITE: { |
| 6343 | // Get a new heap number in r5. r6 and r7 are scratch. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6344 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6345 | } |
| 6346 | default: break; |
| 6347 | } |
| 6348 | __ bind(&got_a_heap_number); |
| 6349 | // r2: Answer as signed int32. |
| 6350 | // r5: Heap number to write answer into. |
| 6351 | |
| 6352 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 6353 | // result. |
| 6354 | __ mov(r0, Operand(r5)); |
| 6355 | |
| 6356 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 6357 | // r3 as scratch. r0 is preserved and returned. |
| 6358 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 6359 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 6360 | |
| 6361 | if (mode_ != NO_OVERWRITE) { |
| 6362 | __ bind(&have_to_allocate); |
| 6363 | // Get a new heap number in r5. r6 and r7 are scratch. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6364 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6365 | __ jmp(&got_a_heap_number); |
| 6366 | } |
| 6367 | |
| 6368 | // If all else failed then we go to the runtime system. |
| 6369 | __ bind(&slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6370 | __ push(lhs); // restore stack |
| 6371 | __ push(rhs); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6372 | switch (op_) { |
| 6373 | case Token::BIT_OR: |
| 6374 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 6375 | break; |
| 6376 | case Token::BIT_AND: |
| 6377 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 6378 | break; |
| 6379 | case Token::BIT_XOR: |
| 6380 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 6381 | break; |
| 6382 | case Token::SAR: |
| 6383 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 6384 | break; |
| 6385 | case Token::SHR: |
| 6386 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 6387 | break; |
| 6388 | case Token::SHL: |
| 6389 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 6390 | break; |
| 6391 | default: |
| 6392 | UNREACHABLE(); |
| 6393 | } |
| 6394 | } |
| 6395 | |
| 6396 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6397 | // Can we multiply by x with max two shifts and an add. |
| 6398 | // This answers yes to all integers from 2 to 10. |
| 6399 | static bool IsEasyToMultiplyBy(int x) { |
| 6400 | if (x < 2) return false; // Avoid special cases. |
| 6401 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 6402 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 6403 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 6404 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 6405 | return false; |
| 6406 | } |
| 6407 | |
| 6408 | |
| 6409 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 6410 | // Source and destination may be the same register. This routine does |
| 6411 | // not set carry and overflow the way a mul instruction would. |
| 6412 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 6413 | Register source, |
| 6414 | Register destination, |
| 6415 | int known_int) { |
| 6416 | if (IsPowerOf2(known_int)) { |
| 6417 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 6418 | } else if (PopCountLessThanEqual2(known_int)) { |
| 6419 | int first_bit = BitPosition(known_int); |
| 6420 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 6421 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 6422 | if (first_bit != 0) { |
| 6423 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 6424 | } |
| 6425 | } else { |
| 6426 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 6427 | int the_bit = BitPosition(known_int + 1); |
| 6428 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 6429 | } |
| 6430 | } |
| 6431 | |
| 6432 | |
| 6433 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 6434 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 6435 | // a result that needs shifting. |
| 6436 | static void MultiplyByKnownInt2( |
| 6437 | MacroAssembler* masm, |
| 6438 | Register result, |
| 6439 | Register source, |
| 6440 | Register known_int_register, // Smi tagged. |
| 6441 | int known_int, |
| 6442 | int* required_shift) { // Including Smi tag shift |
| 6443 | switch (known_int) { |
| 6444 | case 3: |
| 6445 | __ add(result, source, Operand(source, LSL, 1)); |
| 6446 | *required_shift = 1; |
| 6447 | break; |
| 6448 | case 5: |
| 6449 | __ add(result, source, Operand(source, LSL, 2)); |
| 6450 | *required_shift = 1; |
| 6451 | break; |
| 6452 | case 6: |
| 6453 | __ add(result, source, Operand(source, LSL, 1)); |
| 6454 | *required_shift = 2; |
| 6455 | break; |
| 6456 | case 7: |
| 6457 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 6458 | *required_shift = 1; |
| 6459 | break; |
| 6460 | case 9: |
| 6461 | __ add(result, source, Operand(source, LSL, 3)); |
| 6462 | *required_shift = 1; |
| 6463 | break; |
| 6464 | case 10: |
| 6465 | __ add(result, source, Operand(source, LSL, 2)); |
| 6466 | *required_shift = 2; |
| 6467 | break; |
| 6468 | default: |
| 6469 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 6470 | __ mul(result, source, known_int_register); |
| 6471 | *required_shift = 0; |
| 6472 | } |
| 6473 | } |
| 6474 | |
| 6475 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 6476 | const char* GenericBinaryOpStub::GetName() { |
| 6477 | if (name_ != NULL) return name_; |
| 6478 | const int len = 100; |
| 6479 | name_ = Bootstrapper::AllocateAutoDeletedArray(len); |
| 6480 | if (name_ == NULL) return "OOM"; |
| 6481 | const char* op_name = Token::Name(op_); |
| 6482 | const char* overwrite_name; |
| 6483 | switch (mode_) { |
| 6484 | case NO_OVERWRITE: overwrite_name = "Alloc"; break; |
| 6485 | case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; |
| 6486 | case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; |
| 6487 | default: overwrite_name = "UnknownOverwrite"; break; |
| 6488 | } |
| 6489 | |
| 6490 | OS::SNPrintF(Vector<char>(name_, len), |
| 6491 | "GenericBinaryOpStub_%s_%s%s", |
| 6492 | op_name, |
| 6493 | overwrite_name, |
| 6494 | specialized_on_rhs_ ? "_ConstantRhs" : 0); |
| 6495 | return name_; |
| 6496 | } |
| 6497 | |
| 6498 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6499 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6500 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6501 | // lhs_ : x |
| 6502 | // rhs_ : y |
| 6503 | // r0 : result |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6504 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6505 | Register result = r0; |
| 6506 | Register lhs = lhs_; |
| 6507 | Register rhs = rhs_; |
| 6508 | |
| 6509 | // This code can't cope with other register allocations yet. |
| 6510 | ASSERT(result.is(r0) && |
| 6511 | ((lhs.is(r0) && rhs.is(r1)) || |
| 6512 | (lhs.is(r1) && rhs.is(r0)))); |
| 6513 | |
| 6514 | Register smi_test_reg = VirtualFrame::scratch0(); |
| 6515 | Register scratch = VirtualFrame::scratch1(); |
| 6516 | |
| 6517 | // All ops need to know whether we are dealing with two Smis. Set up |
| 6518 | // smi_test_reg to tell us that. |
| 6519 | if (ShouldGenerateSmiCode()) { |
| 6520 | __ orr(smi_test_reg, lhs, Operand(rhs)); |
| 6521 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6522 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6523 | switch (op_) { |
| 6524 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6525 | Label not_smi; |
| 6526 | // Fast path. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6527 | if (ShouldGenerateSmiCode()) { |
| 6528 | ASSERT(kSmiTag == 0); // Adjust code below. |
| 6529 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 6530 | __ b(ne, ¬_smi); |
| 6531 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 6532 | // Return if no overflow. |
| 6533 | __ Ret(vc); |
| 6534 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 6535 | } |
| 6536 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::ADD); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6537 | break; |
| 6538 | } |
| 6539 | |
| 6540 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6541 | Label not_smi; |
| 6542 | // Fast path. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6543 | if (ShouldGenerateSmiCode()) { |
| 6544 | ASSERT(kSmiTag == 0); // Adjust code below. |
| 6545 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 6546 | __ b(ne, ¬_smi); |
| 6547 | if (lhs.is(r1)) { |
| 6548 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 6549 | // Return if no overflow. |
| 6550 | __ Ret(vc); |
| 6551 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 6552 | } else { |
| 6553 | __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically. |
| 6554 | // Return if no overflow. |
| 6555 | __ Ret(vc); |
| 6556 | __ add(r0, r0, Operand(r1)); // Revert optimistic subtract. |
| 6557 | } |
| 6558 | } |
| 6559 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::SUB); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6560 | break; |
| 6561 | } |
| 6562 | |
| 6563 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6564 | Label not_smi, slow; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6565 | if (ShouldGenerateSmiCode()) { |
| 6566 | ASSERT(kSmiTag == 0); // adjust code below |
| 6567 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 6568 | Register scratch2 = smi_test_reg; |
| 6569 | smi_test_reg = no_reg; |
| 6570 | __ b(ne, ¬_smi); |
| 6571 | // Remove tag from one operand (but keep sign), so that result is Smi. |
| 6572 | __ mov(ip, Operand(rhs, ASR, kSmiTagSize)); |
| 6573 | // Do multiplication |
| 6574 | // scratch = lower 32 bits of ip * lhs. |
| 6575 | __ smull(scratch, scratch2, lhs, ip); |
| 6576 | // Go slow on overflows (overflow bit is not set). |
| 6577 | __ mov(ip, Operand(scratch, ASR, 31)); |
| 6578 | // No overflow if higher 33 bits are identical. |
| 6579 | __ cmp(ip, Operand(scratch2)); |
| 6580 | __ b(ne, &slow); |
| 6581 | // Go slow on zero result to handle -0. |
| 6582 | __ tst(scratch, Operand(scratch)); |
| 6583 | __ mov(result, Operand(scratch), LeaveCC, ne); |
| 6584 | __ Ret(ne); |
| 6585 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 6586 | // We know one of them was zero. |
| 6587 | __ add(scratch2, rhs, Operand(lhs), SetCC); |
| 6588 | __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 6589 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 6590 | // Slow case. We fall through here if we multiplied a negative number |
| 6591 | // with 0, because that would mean we should produce -0. |
| 6592 | __ bind(&slow); |
| 6593 | } |
| 6594 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::MUL); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6595 | break; |
| 6596 | } |
| 6597 | |
| 6598 | case Token::DIV: |
| 6599 | case Token::MOD: { |
| 6600 | Label not_smi; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6601 | if (ShouldGenerateSmiCode() && specialized_on_rhs_) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6602 | Label smi_is_unsuitable; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6603 | __ BranchOnNotSmi(lhs, ¬_smi); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6604 | if (IsPowerOf2(constant_rhs_)) { |
| 6605 | if (op_ == Token::MOD) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6606 | __ and_(rhs, |
| 6607 | lhs, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6608 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 6609 | SetCC); |
| 6610 | // We now have the answer, but if the input was negative we also |
| 6611 | // have the sign bit. Our work is done if the result is |
| 6612 | // positive or zero: |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6613 | if (!rhs.is(r0)) { |
| 6614 | __ mov(r0, rhs, LeaveCC, pl); |
| 6615 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6616 | __ Ret(pl); |
| 6617 | // A mod of a negative left hand side must return a negative number. |
| 6618 | // Unfortunately if the answer is 0 then we must return -0. And we |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6619 | // already optimistically trashed rhs so we may need to restore it. |
| 6620 | __ eor(rhs, rhs, Operand(0x80000000u), SetCC); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6621 | // Next two instructions are conditional on the answer being -0. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6622 | __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6623 | __ b(eq, &smi_is_unsuitable); |
| 6624 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6625 | __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_))); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6626 | } else { |
| 6627 | ASSERT(op_ == Token::DIV); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6628 | __ tst(lhs, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6629 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 6630 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 6631 | int shift = 0; |
| 6632 | int d = constant_rhs_; |
| 6633 | while ((d & 1) == 0) { |
| 6634 | d >>= 1; |
| 6635 | shift++; |
| 6636 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6637 | __ mov(r0, Operand(lhs, LSR, shift)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6638 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 6639 | } |
| 6640 | } else { |
| 6641 | // Not a power of 2. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6642 | __ tst(lhs, Operand(0x80000000u)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6643 | __ b(ne, &smi_is_unsuitable); |
| 6644 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 6645 | // multiplying. |
| 6646 | double divisor = 1.0 / constant_rhs_; |
| 6647 | int shift = 32; |
| 6648 | double scale = 4294967296.0; // 1 << 32. |
| 6649 | uint32_t mul; |
| 6650 | // Maximise the precision of the fixed point reciprocal. |
| 6651 | while (true) { |
| 6652 | mul = static_cast<uint32_t>(scale * divisor); |
| 6653 | if (mul >= 0x7fffffff) break; |
| 6654 | scale *= 2.0; |
| 6655 | shift++; |
| 6656 | } |
| 6657 | mul++; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6658 | Register scratch2 = smi_test_reg; |
| 6659 | smi_test_reg = no_reg; |
| 6660 | __ mov(scratch2, Operand(mul)); |
| 6661 | __ umull(scratch, scratch2, scratch2, lhs); |
| 6662 | __ mov(scratch2, Operand(scratch2, LSR, shift - 31)); |
| 6663 | // scratch2 is lhs / rhs. scratch2 is not Smi tagged. |
| 6664 | // rhs is still the known rhs. rhs is Smi tagged. |
| 6665 | // lhs is still the unkown lhs. lhs is Smi tagged. |
| 6666 | int required_scratch_shift = 0; // Including the Smi tag shift of 1. |
| 6667 | // scratch = scratch2 * rhs. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6668 | MultiplyByKnownInt2(masm, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6669 | scratch, |
| 6670 | scratch2, |
| 6671 | rhs, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6672 | constant_rhs_, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6673 | &required_scratch_shift); |
| 6674 | // scratch << required_scratch_shift is now the Smi tagged rhs * |
| 6675 | // (lhs / rhs) where / indicates integer division. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6676 | if (op_ == Token::DIV) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6677 | __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6678 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6679 | __ mov(result, Operand(scratch2, LSL, kSmiTagSize)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6680 | } else { |
| 6681 | ASSERT(op_ == Token::MOD); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6682 | __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6683 | } |
| 6684 | } |
| 6685 | __ Ret(); |
| 6686 | __ bind(&smi_is_unsuitable); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6687 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6688 | HandleBinaryOpSlowCases( |
| 6689 | masm, |
| 6690 | ¬_smi, |
| 6691 | lhs, |
| 6692 | rhs, |
| 6693 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6694 | break; |
| 6695 | } |
| 6696 | |
| 6697 | case Token::BIT_OR: |
| 6698 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6699 | case Token::BIT_XOR: |
| 6700 | case Token::SAR: |
| 6701 | case Token::SHR: |
| 6702 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6703 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6704 | ASSERT(kSmiTag == 0); // adjust code below |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6705 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6706 | __ b(ne, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6707 | Register scratch2 = smi_test_reg; |
| 6708 | smi_test_reg = no_reg; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6709 | switch (op_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6710 | case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break; |
| 6711 | case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break; |
| 6712 | case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6713 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6714 | // Remove tags from right operand. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6715 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 6716 | __ mov(result, Operand(lhs, ASR, scratch2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6717 | // Smi tag result. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6718 | __ bic(result, result, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6719 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6720 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6721 | // Remove tags from operands. We can't do this on a 31 bit number |
| 6722 | // because then the 0s get shifted into bit 30 instead of bit 31. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6723 | __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x |
| 6724 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 6725 | __ mov(scratch, Operand(scratch, LSR, scratch2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6726 | // Unsigned shift is not allowed to produce a negative number, so |
| 6727 | // check the sign bit and the sign bit after Smi tagging. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6728 | __ tst(scratch, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6729 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6730 | // Smi tag result. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6731 | __ mov(result, Operand(scratch, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6732 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6733 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6734 | // Remove tags from operands. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6735 | __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x |
| 6736 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 6737 | __ mov(scratch, Operand(scratch, LSL, scratch2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6738 | // Check that the signed result fits in a Smi. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6739 | __ add(scratch2, scratch, Operand(0x40000000), SetCC); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6740 | __ b(mi, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6741 | __ mov(result, Operand(scratch, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6742 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6743 | default: UNREACHABLE(); |
| 6744 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6745 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6746 | __ bind(&slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6747 | HandleNonSmiBitwiseOp(masm, lhs, rhs); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6748 | break; |
| 6749 | } |
| 6750 | |
| 6751 | default: UNREACHABLE(); |
| 6752 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6753 | // This code should be unreachable. |
| 6754 | __ stop("Unreachable"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6755 | |
| 6756 | // Generate an unreachable reference to the DEFAULT stub so that it can be |
| 6757 | // found at the end of this stub when clearing ICs at GC. |
| 6758 | // TODO(kaznacheev): Check performance impact and get rid of this. |
| 6759 | if (runtime_operands_type_ != BinaryOpIC::DEFAULT) { |
| 6760 | GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT); |
| 6761 | __ CallStub(&uninit); |
| 6762 | } |
| 6763 | } |
| 6764 | |
| 6765 | |
| 6766 | void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) { |
| 6767 | Label get_result; |
| 6768 | |
| 6769 | __ push(r1); |
| 6770 | __ push(r0); |
| 6771 | |
| 6772 | // Internal frame is necessary to handle exceptions properly. |
| 6773 | __ EnterInternalFrame(); |
| 6774 | // Call the stub proper to get the result in r0. |
| 6775 | __ Call(&get_result); |
| 6776 | __ LeaveInternalFrame(); |
| 6777 | |
| 6778 | __ push(r0); |
| 6779 | |
| 6780 | __ mov(r0, Operand(Smi::FromInt(MinorKey()))); |
| 6781 | __ push(r0); |
| 6782 | __ mov(r0, Operand(Smi::FromInt(op_))); |
| 6783 | __ push(r0); |
| 6784 | __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_))); |
| 6785 | __ push(r0); |
| 6786 | |
| 6787 | __ TailCallExternalReference( |
| 6788 | ExternalReference(IC_Utility(IC::kBinaryOp_Patch)), |
| 6789 | 6, |
| 6790 | 1); |
| 6791 | |
| 6792 | // The entry point for the result calculation is assumed to be immediately |
| 6793 | // after this sequence. |
| 6794 | __ bind(&get_result); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
| 6797 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6798 | Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6799 | GenericBinaryOpStub stub(key, type_info); |
| 6800 | return stub.GetCode(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6804 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 6805 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 6806 | // argument, so give it a Smi. |
| 6807 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6808 | __ push(r0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6809 | __ TailCallRuntime(Runtime::kStackGuard, 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6810 | |
| 6811 | __ StubReturn(1); |
| 6812 | } |
| 6813 | |
| 6814 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6815 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6816 | Label slow, done; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6817 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6818 | if (op_ == Token::SUB) { |
| 6819 | // Check whether the value is a smi. |
| 6820 | Label try_float; |
| 6821 | __ tst(r0, Operand(kSmiTagMask)); |
| 6822 | __ b(ne, &try_float); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6823 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6824 | // Go slow case if the value of the expression is zero |
| 6825 | // to make sure that we switch between 0 and -0. |
| 6826 | __ cmp(r0, Operand(0)); |
| 6827 | __ b(eq, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6828 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6829 | // The value of the expression is a smi that is not zero. Try |
| 6830 | // optimistic subtraction '0 - value'. |
| 6831 | __ rsb(r1, r0, Operand(0), SetCC); |
| 6832 | __ b(vs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6833 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6834 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 6835 | __ b(&done); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6836 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6837 | __ bind(&try_float); |
| 6838 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6839 | __ b(ne, &slow); |
| 6840 | // r0 is a heap number. Get a new heap number in r1. |
| 6841 | if (overwrite_) { |
| 6842 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6843 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6844 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6845 | } else { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6846 | __ AllocateHeapNumber(r1, r2, r3, &slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6847 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 6848 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6849 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 6850 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6851 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 6852 | __ mov(r0, Operand(r1)); |
| 6853 | } |
| 6854 | } else if (op_ == Token::BIT_NOT) { |
| 6855 | // Check if the operand is a heap number. |
| 6856 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6857 | __ b(ne, &slow); |
| 6858 | |
| 6859 | // Convert the heap number is r0 to an untagged integer in r1. |
| 6860 | GetInt32(masm, r0, r1, r2, r3, &slow); |
| 6861 | |
| 6862 | // Do the bitwise operation (move negated) and check if the result |
| 6863 | // fits in a smi. |
| 6864 | Label try_float; |
| 6865 | __ mvn(r1, Operand(r1)); |
| 6866 | __ add(r2, r1, Operand(0x40000000), SetCC); |
| 6867 | __ b(mi, &try_float); |
| 6868 | __ mov(r0, Operand(r1, LSL, kSmiTagSize)); |
| 6869 | __ b(&done); |
| 6870 | |
| 6871 | __ bind(&try_float); |
| 6872 | if (!overwrite_) { |
| 6873 | // Allocate a fresh heap number, but don't overwrite r0 until |
| 6874 | // we're sure we can do it without going through the slow case |
| 6875 | // that needs the value in r0. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6876 | __ AllocateHeapNumber(r2, r3, r4, &slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6877 | __ mov(r0, Operand(r2)); |
| 6878 | } |
| 6879 | |
| 6880 | // WriteInt32ToHeapNumberStub does not trigger GC, so we do not |
| 6881 | // have to set up a frame. |
| 6882 | WriteInt32ToHeapNumberStub stub(r1, r0, r2); |
| 6883 | __ push(lr); |
| 6884 | __ Call(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 6885 | __ pop(lr); |
| 6886 | } else { |
| 6887 | UNIMPLEMENTED(); |
| 6888 | } |
| 6889 | |
| 6890 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6891 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6892 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6893 | // Handle the slow case by jumping to the JavaScript builtin. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6894 | __ bind(&slow); |
| 6895 | __ push(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6896 | switch (op_) { |
| 6897 | case Token::SUB: |
| 6898 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 6899 | break; |
| 6900 | case Token::BIT_NOT: |
| 6901 | __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS); |
| 6902 | break; |
| 6903 | default: |
| 6904 | UNREACHABLE(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6905 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6906 | } |
| 6907 | |
| 6908 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6909 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6910 | // r0 holds the exception. |
| 6911 | |
| 6912 | // Adjust this code if not the case. |
| 6913 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6914 | |
| 6915 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6916 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 6917 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6918 | |
| 6919 | // Restore the next handler and frame pointer, discard handler state. |
| 6920 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 6921 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6922 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6923 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6924 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 6925 | |
| 6926 | // Before returning we restore the context from the frame pointer if |
| 6927 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6928 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6929 | __ cmp(fp, Operand(0)); |
| 6930 | // Set cp to NULL if fp is NULL. |
| 6931 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6932 | // Restore cp otherwise. |
| 6933 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6934 | #ifdef DEBUG |
| 6935 | if (FLAG_debug_code) { |
| 6936 | __ mov(lr, Operand(pc)); |
| 6937 | } |
| 6938 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6939 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6940 | __ pop(pc); |
| 6941 | } |
| 6942 | |
| 6943 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6944 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 6945 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6946 | // Adjust this code if not the case. |
| 6947 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6948 | |
| 6949 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6950 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6951 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6952 | |
| 6953 | // Unwind the handlers until the ENTRY handler is found. |
| 6954 | Label loop, done; |
| 6955 | __ bind(&loop); |
| 6956 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6957 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 6958 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6959 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 6960 | __ b(eq, &done); |
| 6961 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6962 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 6963 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6964 | __ jmp(&loop); |
| 6965 | __ bind(&done); |
| 6966 | |
| 6967 | // 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] | 6968 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6969 | __ pop(r2); |
| 6970 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6971 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6972 | if (type == OUT_OF_MEMORY) { |
| 6973 | // Set external caught exception to false. |
| 6974 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 6975 | __ mov(r0, Operand(false)); |
| 6976 | __ mov(r2, Operand(external_caught)); |
| 6977 | __ str(r0, MemOperand(r2)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6978 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6979 | // Set pending exception and r0 to out of memory exception. |
| 6980 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6981 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6982 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 6983 | __ str(r0, MemOperand(r2)); |
| 6984 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6985 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6986 | // Stack layout at this point. See also StackHandlerConstants. |
| 6987 | // sp -> state (ENTRY) |
| 6988 | // fp |
| 6989 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6990 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6991 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 6992 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6993 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 6994 | // Before returning we restore the context from the frame pointer if |
| 6995 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6996 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6997 | __ cmp(fp, Operand(0)); |
| 6998 | // Set cp to NULL if fp is NULL. |
| 6999 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 7000 | // Restore cp otherwise. |
| 7001 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7002 | #ifdef DEBUG |
| 7003 | if (FLAG_debug_code) { |
| 7004 | __ mov(lr, Operand(pc)); |
| 7005 | } |
| 7006 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7007 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7008 | __ pop(pc); |
| 7009 | } |
| 7010 | |
| 7011 | |
| 7012 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 7013 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7014 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7015 | Label* throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7016 | bool do_gc, |
| 7017 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7018 | // r0: result parameter for PerformGC, if any |
| 7019 | // r4: number of arguments including receiver (C callee-saved) |
| 7020 | // r5: pointer to builtin function (C callee-saved) |
| 7021 | // r6: pointer to the first argument (C callee-saved) |
| 7022 | |
| 7023 | if (do_gc) { |
| 7024 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7025 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 7026 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7027 | } |
| 7028 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7029 | ExternalReference scope_depth = |
| 7030 | ExternalReference::heap_always_allocate_scope_depth(); |
| 7031 | if (always_allocate) { |
| 7032 | __ mov(r0, Operand(scope_depth)); |
| 7033 | __ ldr(r1, MemOperand(r0)); |
| 7034 | __ add(r1, r1, Operand(1)); |
| 7035 | __ str(r1, MemOperand(r0)); |
| 7036 | } |
| 7037 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7038 | // Call C built-in. |
| 7039 | // r0 = argc, r1 = argv |
| 7040 | __ mov(r0, Operand(r4)); |
| 7041 | __ mov(r1, Operand(r6)); |
| 7042 | |
| 7043 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 7044 | // frames, we need to know where the return address is. Right now, |
| 7045 | // we push it on the stack to be able to find it again, but we never |
| 7046 | // restore from it in case of changes, which makes it impossible to |
| 7047 | // support moving the C entry code stub. This should be fixed, but currently |
| 7048 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 7049 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7050 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 7051 | masm->push(lr); |
| 7052 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7053 | |
| 7054 | if (always_allocate) { |
| 7055 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 7056 | // though (contain the result). |
| 7057 | __ mov(r2, Operand(scope_depth)); |
| 7058 | __ ldr(r3, MemOperand(r2)); |
| 7059 | __ sub(r3, r3, Operand(1)); |
| 7060 | __ str(r3, MemOperand(r2)); |
| 7061 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7062 | |
| 7063 | // check for failure result |
| 7064 | Label failure_returned; |
| 7065 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 7066 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 7067 | __ add(r2, r0, Operand(1)); |
| 7068 | __ tst(r2, Operand(kFailureTagMask)); |
| 7069 | __ b(eq, &failure_returned); |
| 7070 | |
| 7071 | // Exit C frame and return. |
| 7072 | // r0:r1: result |
| 7073 | // sp: stack pointer |
| 7074 | // fp: frame pointer |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7075 | __ LeaveExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7076 | |
| 7077 | // check if we should retry or throw exception |
| 7078 | Label retry; |
| 7079 | __ bind(&failure_returned); |
| 7080 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 7081 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 7082 | __ b(eq, &retry); |
| 7083 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7084 | // Special handling of out of memory exceptions. |
| 7085 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 7086 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 7087 | __ b(eq, throw_out_of_memory_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7088 | |
| 7089 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 7090 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7091 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 7092 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7093 | __ ldr(r0, MemOperand(ip)); |
| 7094 | __ str(r3, MemOperand(ip)); |
| 7095 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7096 | // Special handling of termination exceptions which are uncatchable |
| 7097 | // by javascript code. |
| 7098 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 7099 | __ b(eq, throw_termination_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7100 | |
| 7101 | // Handle normal exception. |
| 7102 | __ jmp(throw_normal_exception); |
| 7103 | |
| 7104 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 7105 | } |
| 7106 | |
| 7107 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7108 | void CEntryStub::Generate(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7109 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 7110 | // r0: number of arguments including receiver |
| 7111 | // r1: pointer to builtin function |
| 7112 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7113 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7114 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7115 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7116 | // Result returned in r0 or r0+r1 by default. |
| 7117 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7118 | // NOTE: Invocations of builtins may return failure objects |
| 7119 | // instead of a proper result. The builtin entry handles |
| 7120 | // this by performing a garbage collection and retrying the |
| 7121 | // builtin once. |
| 7122 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7123 | // Enter the exit frame that transitions from JavaScript to C++. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7124 | __ EnterExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7125 | |
| 7126 | // r4: number of arguments (C callee-saved) |
| 7127 | // r5: pointer to builtin function (C callee-saved) |
| 7128 | // r6: pointer to first argument (C callee-saved) |
| 7129 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7130 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7131 | Label throw_termination_exception; |
| 7132 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7133 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 7134 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7135 | GenerateCore(masm, |
| 7136 | &throw_normal_exception, |
| 7137 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7138 | &throw_out_of_memory_exception, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 7139 | false, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7140 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7141 | |
| 7142 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7143 | GenerateCore(masm, |
| 7144 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7145 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7146 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7147 | true, |
| 7148 | false); |
| 7149 | |
| 7150 | // Do full GC and retry runtime call one final time. |
| 7151 | Failure* failure = Failure::InternalError(); |
| 7152 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 7153 | GenerateCore(masm, |
| 7154 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7155 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7156 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7157 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7158 | true); |
| 7159 | |
| 7160 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7161 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 7162 | |
| 7163 | __ bind(&throw_termination_exception); |
| 7164 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7165 | |
| 7166 | __ bind(&throw_normal_exception); |
| 7167 | GenerateThrowTOS(masm); |
| 7168 | } |
| 7169 | |
| 7170 | |
| 7171 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 7172 | // r0: code entry |
| 7173 | // r1: function |
| 7174 | // r2: receiver |
| 7175 | // r3: argc |
| 7176 | // [sp+0]: argv |
| 7177 | |
| 7178 | Label invoke, exit; |
| 7179 | |
| 7180 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 7181 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7182 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7183 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 7184 | |
| 7185 | // Get address of argv, see stm above. |
| 7186 | // r0: code entry |
| 7187 | // r1: function |
| 7188 | // r2: receiver |
| 7189 | // r3: argc |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7190 | __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7191 | |
| 7192 | // Push a frame with special values setup to mark it as an entry frame. |
| 7193 | // r0: code entry |
| 7194 | // r1: function |
| 7195 | // r2: receiver |
| 7196 | // r3: argc |
| 7197 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7198 | __ 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] | 7199 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 7200 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7201 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 7202 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 7203 | __ ldr(r5, MemOperand(r5)); |
| 7204 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 7205 | |
| 7206 | // Setup frame pointer for the frame to be pushed. |
| 7207 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 7208 | |
| 7209 | // Call a faked try-block that does the invoke. |
| 7210 | __ bl(&invoke); |
| 7211 | |
| 7212 | // Caught exception: Store result (exception) in the pending |
| 7213 | // exception field in the JSEnv and return a failure sentinel. |
| 7214 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 7215 | // 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] | 7216 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7217 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 7218 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7219 | __ b(&exit); |
| 7220 | |
| 7221 | // Invoke: Link this frame into the handler chain. |
| 7222 | __ bind(&invoke); |
| 7223 | // Must preserve r0-r4, r5-r7 are available. |
| 7224 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7225 | // If an exception not caught by another handler occurs, this handler |
| 7226 | // returns control to the code after the bl(&invoke) above, which |
| 7227 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 7228 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7229 | |
| 7230 | // Clear any pending exceptions. |
| 7231 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 7232 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 7233 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7234 | __ str(r5, MemOperand(ip)); |
| 7235 | |
| 7236 | // Invoke the function by calling through JS entry trampoline builtin. |
| 7237 | // Notice that we cannot store a reference to the trampoline code directly in |
| 7238 | // this stub, because runtime stubs are not traversed when doing GC. |
| 7239 | |
| 7240 | // Expected registers by Builtins::JSEntryTrampoline |
| 7241 | // r0: code entry |
| 7242 | // r1: function |
| 7243 | // r2: receiver |
| 7244 | // r3: argc |
| 7245 | // r4: argv |
| 7246 | if (is_construct) { |
| 7247 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 7248 | __ mov(ip, Operand(construct_entry)); |
| 7249 | } else { |
| 7250 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 7251 | __ mov(ip, Operand(entry)); |
| 7252 | } |
| 7253 | __ ldr(ip, MemOperand(ip)); // deref address |
| 7254 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7255 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 7256 | // macro for the add instruction because we don't want the coverage tool |
| 7257 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7258 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7259 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7260 | |
| 7261 | // Unlink this frame from the handler chain. When reading the |
| 7262 | // address of the next handler, there is no need to use the address |
| 7263 | // displacement since the current stack pointer (sp) points directly |
| 7264 | // to the stack handler. |
| 7265 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 7266 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 7267 | __ str(r3, MemOperand(ip)); |
| 7268 | // No need to restore registers |
| 7269 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 7270 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7271 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7272 | __ bind(&exit); // r0 holds result |
| 7273 | // Restore the top frame descriptors from the stack. |
| 7274 | __ pop(r3); |
| 7275 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 7276 | __ str(r3, MemOperand(ip)); |
| 7277 | |
| 7278 | // Reset the stack to the callee saved registers. |
| 7279 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 7280 | |
| 7281 | // Restore callee-saved registers and return. |
| 7282 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7283 | if (FLAG_debug_code) { |
| 7284 | __ mov(lr, Operand(pc)); |
| 7285 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7286 | #endif |
| 7287 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 7288 | } |
| 7289 | |
| 7290 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7291 | // This stub performs an instanceof, calling the builtin function if |
| 7292 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 7293 | // be an instance of (these are fetched from the stack). |
| 7294 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 7295 | // Get the object - slow case for smis (we may need to throw an exception |
| 7296 | // depending on the rhs). |
| 7297 | Label slow, loop, is_instance, is_not_instance; |
| 7298 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 7299 | __ BranchOnSmi(r0, &slow); |
| 7300 | |
| 7301 | // Check that the left hand is a JS object and put map in r3. |
| 7302 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 7303 | __ b(lt, &slow); |
| 7304 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 7305 | __ b(gt, &slow); |
| 7306 | |
| 7307 | // 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] | 7308 | __ ldr(r1, MemOperand(sp, 0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7309 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 7310 | |
| 7311 | // Check that the function prototype is a JS object. |
| 7312 | __ BranchOnSmi(r4, &slow); |
| 7313 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 7314 | __ b(lt, &slow); |
| 7315 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 7316 | __ b(gt, &slow); |
| 7317 | |
| 7318 | // Register mapping: r3 is object map and r4 is function prototype. |
| 7319 | // Get prototype of object into r2. |
| 7320 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 7321 | |
| 7322 | // Loop through the prototype chain looking for the function prototype. |
| 7323 | __ bind(&loop); |
| 7324 | __ cmp(r2, Operand(r4)); |
| 7325 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 7326 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 7327 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7328 | __ b(eq, &is_not_instance); |
| 7329 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 7330 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 7331 | __ jmp(&loop); |
| 7332 | |
| 7333 | __ bind(&is_instance); |
| 7334 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 7335 | __ pop(); |
| 7336 | __ pop(); |
| 7337 | __ mov(pc, Operand(lr)); // Return. |
| 7338 | |
| 7339 | __ bind(&is_not_instance); |
| 7340 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 7341 | __ pop(); |
| 7342 | __ pop(); |
| 7343 | __ mov(pc, Operand(lr)); // Return. |
| 7344 | |
| 7345 | // Slow-case. Tail call builtin. |
| 7346 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7347 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 7348 | } |
| 7349 | |
| 7350 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7351 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 7352 | // The displacement is the offset of the last parameter (if any) |
| 7353 | // relative to the frame pointer. |
| 7354 | static const int kDisplacement = |
| 7355 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7356 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7357 | // Check that the key is a smi. |
| 7358 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7359 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7360 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7361 | // Check if the calling frame is an arguments adaptor frame. |
| 7362 | Label adaptor; |
| 7363 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 7364 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7365 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7366 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7367 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7368 | // Check index against formal parameters count limit passed in |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 7369 | // through register r0. Use unsigned comparison to get negative |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7370 | // check for free. |
| 7371 | __ cmp(r1, r0); |
| 7372 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7373 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7374 | // Read the argument from the stack and return it. |
| 7375 | __ sub(r3, r0, r1); |
| 7376 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 7377 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 7378 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7379 | |
| 7380 | // Arguments adaptor case: Check index against actual arguments |
| 7381 | // limit found in the arguments adaptor frame. Use unsigned |
| 7382 | // comparison to get negative check for free. |
| 7383 | __ bind(&adaptor); |
| 7384 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 7385 | __ cmp(r1, r0); |
| 7386 | __ b(cs, &slow); |
| 7387 | |
| 7388 | // Read the argument from the adaptor frame and return it. |
| 7389 | __ sub(r3, r0, r1); |
| 7390 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 7391 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 7392 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7393 | |
| 7394 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 7395 | // by calling the runtime system. |
| 7396 | __ bind(&slow); |
| 7397 | __ push(r1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7398 | __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | |
| 7402 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7403 | // sp[0] : number of parameters |
| 7404 | // sp[4] : receiver displacement |
| 7405 | // sp[8] : function |
| 7406 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7407 | // Check if the calling frame is an arguments adaptor frame. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7408 | Label adaptor_frame, try_allocate, runtime; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7409 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 7410 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7411 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7412 | __ b(eq, &adaptor_frame); |
| 7413 | |
| 7414 | // Get the length from the frame. |
| 7415 | __ ldr(r1, MemOperand(sp, 0)); |
| 7416 | __ b(&try_allocate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7417 | |
| 7418 | // Patch the arguments.length and the parameters pointer. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7419 | __ bind(&adaptor_frame); |
| 7420 | __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 7421 | __ str(r1, MemOperand(sp, 0)); |
| 7422 | __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7423 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 7424 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 7425 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7426 | // Try the new space allocation. Start out with computing the size |
| 7427 | // of the arguments object and the elements array (in words, not |
| 7428 | // bytes because AllocateInNewSpace expects words). |
| 7429 | Label add_arguments_object; |
| 7430 | __ bind(&try_allocate); |
| 7431 | __ cmp(r1, Operand(0)); |
| 7432 | __ b(eq, &add_arguments_object); |
| 7433 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 7434 | __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize)); |
| 7435 | __ bind(&add_arguments_object); |
| 7436 | __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize)); |
| 7437 | |
| 7438 | // Do the allocation of both objects in one go. |
| 7439 | __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT); |
| 7440 | |
| 7441 | // Get the arguments boilerplate from the current (global) context. |
| 7442 | int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX); |
| 7443 | __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 7444 | __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset)); |
| 7445 | __ ldr(r4, MemOperand(r4, offset)); |
| 7446 | |
| 7447 | // Copy the JS object part. |
| 7448 | for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) { |
| 7449 | __ ldr(r3, FieldMemOperand(r4, i)); |
| 7450 | __ str(r3, FieldMemOperand(r0, i)); |
| 7451 | } |
| 7452 | |
| 7453 | // Setup the callee in-object property. |
| 7454 | ASSERT(Heap::arguments_callee_index == 0); |
| 7455 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 7456 | __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize)); |
| 7457 | |
| 7458 | // Get the length (smi tagged) and set that as an in-object property too. |
| 7459 | ASSERT(Heap::arguments_length_index == 1); |
| 7460 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 7461 | __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize)); |
| 7462 | |
| 7463 | // If there are no actual arguments, we're done. |
| 7464 | Label done; |
| 7465 | __ cmp(r1, Operand(0)); |
| 7466 | __ b(eq, &done); |
| 7467 | |
| 7468 | // Get the parameters pointer from the stack and untag the length. |
| 7469 | __ ldr(r2, MemOperand(sp, 1 * kPointerSize)); |
| 7470 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 7471 | |
| 7472 | // Setup the elements pointer in the allocated arguments object and |
| 7473 | // initialize the header in the elements fixed array. |
| 7474 | __ add(r4, r0, Operand(Heap::kArgumentsObjectSize)); |
| 7475 | __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 7476 | __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex); |
| 7477 | __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset)); |
| 7478 | __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset)); |
| 7479 | |
| 7480 | // Copy the fixed array slots. |
| 7481 | Label loop; |
| 7482 | // Setup r4 to point to the first array slot. |
| 7483 | __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 7484 | __ bind(&loop); |
| 7485 | // Pre-decrement r2 with kPointerSize on each iteration. |
| 7486 | // Pre-decrement in order to skip receiver. |
| 7487 | __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex)); |
| 7488 | // Post-increment r4 with kPointerSize on each iteration. |
| 7489 | __ str(r3, MemOperand(r4, kPointerSize, PostIndex)); |
| 7490 | __ sub(r1, r1, Operand(1)); |
| 7491 | __ cmp(r1, Operand(0)); |
| 7492 | __ b(ne, &loop); |
| 7493 | |
| 7494 | // Return and remove the on-stack parameters. |
| 7495 | __ bind(&done); |
| 7496 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7497 | __ Ret(); |
| 7498 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7499 | // Do the runtime call to allocate the arguments object. |
| 7500 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7501 | __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7502 | } |
| 7503 | |
| 7504 | |
| 7505 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 7506 | Label slow; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7507 | |
| 7508 | // If the receiver might be a value (string, number or boolean) check for this |
| 7509 | // and box it if it is. |
| 7510 | if (ReceiverMightBeValue()) { |
| 7511 | // Get the receiver from the stack. |
| 7512 | // function, receiver [, arguments] |
| 7513 | Label receiver_is_value, receiver_is_js_object; |
| 7514 | __ ldr(r1, MemOperand(sp, argc_ * kPointerSize)); |
| 7515 | |
| 7516 | // Check if receiver is a smi (which is a number value). |
| 7517 | __ BranchOnSmi(r1, &receiver_is_value); |
| 7518 | |
| 7519 | // Check if the receiver is a valid JS object. |
| 7520 | __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 7521 | __ b(ge, &receiver_is_js_object); |
| 7522 | |
| 7523 | // Call the runtime to box the value. |
| 7524 | __ bind(&receiver_is_value); |
| 7525 | __ EnterInternalFrame(); |
| 7526 | __ push(r1); |
| 7527 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
| 7528 | __ LeaveInternalFrame(); |
| 7529 | __ str(r0, MemOperand(sp, argc_ * kPointerSize)); |
| 7530 | |
| 7531 | __ bind(&receiver_is_js_object); |
| 7532 | } |
| 7533 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7534 | // Get the function to call from the stack. |
| 7535 | // function, receiver [, arguments] |
| 7536 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 7537 | |
| 7538 | // Check that the function is really a JavaScript function. |
| 7539 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7540 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7541 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7542 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7543 | __ b(ne, &slow); |
| 7544 | |
| 7545 | // Fast-case: Invoke the function now. |
| 7546 | // r1: pushed function |
| 7547 | ParameterCount actual(argc_); |
| 7548 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 7549 | |
| 7550 | // Slow-case: Non-function called. |
| 7551 | __ bind(&slow); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7552 | // CALL_NON_FUNCTION expects the non-function callee as receiver (instead |
| 7553 | // of the original receiver from the call site). |
| 7554 | __ str(r1, MemOperand(sp, argc_ * kPointerSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7555 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 7556 | __ mov(r2, Operand(0)); |
| 7557 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 7558 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 7559 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7560 | } |
| 7561 | |
| 7562 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 7563 | // Unfortunately you have to run without snapshots to see most of these |
| 7564 | // names in the profile since most compare stubs end up in the snapshot. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7565 | const char* CompareStub::GetName() { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 7566 | if (name_ != NULL) return name_; |
| 7567 | const int kMaxNameLength = 100; |
| 7568 | name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength); |
| 7569 | if (name_ == NULL) return "OOM"; |
| 7570 | |
| 7571 | const char* cc_name; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7572 | switch (cc_) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 7573 | case lt: cc_name = "LT"; break; |
| 7574 | case gt: cc_name = "GT"; break; |
| 7575 | case le: cc_name = "LE"; break; |
| 7576 | case ge: cc_name = "GE"; break; |
| 7577 | case eq: cc_name = "EQ"; break; |
| 7578 | case ne: cc_name = "NE"; break; |
| 7579 | default: cc_name = "UnknownCondition"; break; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7580 | } |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 7581 | |
| 7582 | const char* strict_name = ""; |
| 7583 | if (strict_ && (cc_ == eq || cc_ == ne)) { |
| 7584 | strict_name = "_STRICT"; |
| 7585 | } |
| 7586 | |
| 7587 | const char* never_nan_nan_name = ""; |
| 7588 | if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) { |
| 7589 | never_nan_nan_name = "_NO_NAN"; |
| 7590 | } |
| 7591 | |
| 7592 | const char* include_number_compare_name = ""; |
| 7593 | if (!include_number_compare_) { |
| 7594 | include_number_compare_name = "_NO_NUMBER"; |
| 7595 | } |
| 7596 | |
| 7597 | OS::SNPrintF(Vector<char>(name_, kMaxNameLength), |
| 7598 | "CompareStub_%s%s%s%s", |
| 7599 | cc_name, |
| 7600 | strict_name, |
| 7601 | never_nan_nan_name, |
| 7602 | include_number_compare_name); |
| 7603 | return name_; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7604 | } |
| 7605 | |
| 7606 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7607 | int CompareStub::MinorKey() { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7608 | // Encode the three parameters in a unique 16 bit value. To avoid duplicate |
| 7609 | // stubs the never NaN NaN condition is only taken into account if the |
| 7610 | // condition is equals. |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 7611 | ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7612 | return ConditionField::encode(static_cast<unsigned>(cc_) >> 28) |
| 7613 | | StrictField::encode(strict_) |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 7614 | | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false) |
| 7615 | | IncludeNumberCompareField::encode(include_number_compare_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7616 | } |
| 7617 | |
| 7618 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7619 | void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm, |
| 7620 | Register dest, |
| 7621 | Register src, |
| 7622 | Register count, |
| 7623 | Register scratch, |
| 7624 | bool ascii) { |
| 7625 | Label loop; |
| 7626 | Label done; |
| 7627 | // This loop just copies one character at a time, as it is only used for very |
| 7628 | // short strings. |
| 7629 | if (!ascii) { |
| 7630 | __ add(count, count, Operand(count), SetCC); |
| 7631 | } else { |
| 7632 | __ cmp(count, Operand(0)); |
| 7633 | } |
| 7634 | __ b(eq, &done); |
| 7635 | |
| 7636 | __ bind(&loop); |
| 7637 | __ ldrb(scratch, MemOperand(src, 1, PostIndex)); |
| 7638 | // Perform sub between load and dependent store to get the load time to |
| 7639 | // complete. |
| 7640 | __ sub(count, count, Operand(1), SetCC); |
| 7641 | __ strb(scratch, MemOperand(dest, 1, PostIndex)); |
| 7642 | // last iteration. |
| 7643 | __ b(gt, &loop); |
| 7644 | |
| 7645 | __ bind(&done); |
| 7646 | } |
| 7647 | |
| 7648 | |
| 7649 | enum CopyCharactersFlags { |
| 7650 | COPY_ASCII = 1, |
| 7651 | DEST_ALWAYS_ALIGNED = 2 |
| 7652 | }; |
| 7653 | |
| 7654 | |
| 7655 | void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm, |
| 7656 | Register dest, |
| 7657 | Register src, |
| 7658 | Register count, |
| 7659 | Register scratch1, |
| 7660 | Register scratch2, |
| 7661 | Register scratch3, |
| 7662 | Register scratch4, |
| 7663 | Register scratch5, |
| 7664 | int flags) { |
| 7665 | bool ascii = (flags & COPY_ASCII) != 0; |
| 7666 | bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0; |
| 7667 | |
| 7668 | if (dest_always_aligned && FLAG_debug_code) { |
| 7669 | // Check that destination is actually word aligned if the flag says |
| 7670 | // that it is. |
| 7671 | __ tst(dest, Operand(kPointerAlignmentMask)); |
| 7672 | __ Check(eq, "Destination of copy not aligned."); |
| 7673 | } |
| 7674 | |
| 7675 | const int kReadAlignment = 4; |
| 7676 | const int kReadAlignmentMask = kReadAlignment - 1; |
| 7677 | // Ensure that reading an entire aligned word containing the last character |
| 7678 | // of a string will not read outside the allocated area (because we pad up |
| 7679 | // to kObjectAlignment). |
| 7680 | ASSERT(kObjectAlignment >= kReadAlignment); |
| 7681 | // Assumes word reads and writes are little endian. |
| 7682 | // Nothing to do for zero characters. |
| 7683 | Label done; |
| 7684 | if (!ascii) { |
| 7685 | __ add(count, count, Operand(count), SetCC); |
| 7686 | } else { |
| 7687 | __ cmp(count, Operand(0)); |
| 7688 | } |
| 7689 | __ b(eq, &done); |
| 7690 | |
| 7691 | // Assume that you cannot read (or write) unaligned. |
| 7692 | Label byte_loop; |
| 7693 | // Must copy at least eight bytes, otherwise just do it one byte at a time. |
| 7694 | __ cmp(count, Operand(8)); |
| 7695 | __ add(count, dest, Operand(count)); |
| 7696 | Register limit = count; // Read until src equals this. |
| 7697 | __ b(lt, &byte_loop); |
| 7698 | |
| 7699 | if (!dest_always_aligned) { |
| 7700 | // Align dest by byte copying. Copies between zero and three bytes. |
| 7701 | __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC); |
| 7702 | Label dest_aligned; |
| 7703 | __ b(eq, &dest_aligned); |
| 7704 | __ cmp(scratch4, Operand(2)); |
| 7705 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex)); |
| 7706 | __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le); |
| 7707 | __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt); |
| 7708 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7709 | __ strb(scratch2, MemOperand(dest, 1, PostIndex), le); |
| 7710 | __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt); |
| 7711 | __ bind(&dest_aligned); |
| 7712 | } |
| 7713 | |
| 7714 | Label simple_loop; |
| 7715 | |
| 7716 | __ sub(scratch4, dest, Operand(src)); |
| 7717 | __ and_(scratch4, scratch4, Operand(0x03), SetCC); |
| 7718 | __ b(eq, &simple_loop); |
| 7719 | // Shift register is number of bits in a source word that |
| 7720 | // must be combined with bits in the next source word in order |
| 7721 | // to create a destination word. |
| 7722 | |
| 7723 | // Complex loop for src/dst that are not aligned the same way. |
| 7724 | { |
| 7725 | Label loop; |
| 7726 | __ mov(scratch4, Operand(scratch4, LSL, 3)); |
| 7727 | Register left_shift = scratch4; |
| 7728 | __ and_(src, src, Operand(~3)); // Round down to load previous word. |
| 7729 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 7730 | // Store the "shift" most significant bits of scratch in the least |
| 7731 | // signficant bits (i.e., shift down by (32-shift)). |
| 7732 | __ rsb(scratch2, left_shift, Operand(32)); |
| 7733 | Register right_shift = scratch2; |
| 7734 | __ mov(scratch1, Operand(scratch1, LSR, right_shift)); |
| 7735 | |
| 7736 | __ bind(&loop); |
| 7737 | __ ldr(scratch3, MemOperand(src, 4, PostIndex)); |
| 7738 | __ sub(scratch5, limit, Operand(dest)); |
| 7739 | __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift)); |
| 7740 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 7741 | __ mov(scratch1, Operand(scratch3, LSR, right_shift)); |
| 7742 | // Loop if four or more bytes left to copy. |
| 7743 | // Compare to eight, because we did the subtract before increasing dst. |
| 7744 | __ sub(scratch5, scratch5, Operand(8), SetCC); |
| 7745 | __ b(ge, &loop); |
| 7746 | } |
| 7747 | // There is now between zero and three bytes left to copy (negative that |
| 7748 | // number is in scratch5), and between one and three bytes already read into |
| 7749 | // scratch1 (eight times that number in scratch4). We may have read past |
| 7750 | // the end of the string, but because objects are aligned, we have not read |
| 7751 | // past the end of the object. |
| 7752 | // Find the minimum of remaining characters to move and preloaded characters |
| 7753 | // and write those as bytes. |
| 7754 | __ add(scratch5, scratch5, Operand(4), SetCC); |
| 7755 | __ b(eq, &done); |
| 7756 | __ cmp(scratch4, Operand(scratch5, LSL, 3), ne); |
| 7757 | // Move minimum of bytes read and bytes left to copy to scratch4. |
| 7758 | __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt); |
| 7759 | // Between one and three (value in scratch5) characters already read into |
| 7760 | // scratch ready to write. |
| 7761 | __ cmp(scratch5, Operand(2)); |
| 7762 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7763 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge); |
| 7764 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge); |
| 7765 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt); |
| 7766 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt); |
| 7767 | // Copy any remaining bytes. |
| 7768 | __ b(&byte_loop); |
| 7769 | |
| 7770 | // Simple loop. |
| 7771 | // Copy words from src to dst, until less than four bytes left. |
| 7772 | // Both src and dest are word aligned. |
| 7773 | __ bind(&simple_loop); |
| 7774 | { |
| 7775 | Label loop; |
| 7776 | __ bind(&loop); |
| 7777 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 7778 | __ sub(scratch3, limit, Operand(dest)); |
| 7779 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 7780 | // Compare to 8, not 4, because we do the substraction before increasing |
| 7781 | // dest. |
| 7782 | __ cmp(scratch3, Operand(8)); |
| 7783 | __ b(ge, &loop); |
| 7784 | } |
| 7785 | |
| 7786 | // Copy bytes from src to dst until dst hits limit. |
| 7787 | __ bind(&byte_loop); |
| 7788 | __ cmp(dest, Operand(limit)); |
| 7789 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt); |
| 7790 | __ b(ge, &done); |
| 7791 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7792 | __ b(&byte_loop); |
| 7793 | |
| 7794 | __ bind(&done); |
| 7795 | } |
| 7796 | |
| 7797 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7798 | void StringStubBase::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm, |
| 7799 | Register c1, |
| 7800 | Register c2, |
| 7801 | Register scratch1, |
| 7802 | Register scratch2, |
| 7803 | Register scratch3, |
| 7804 | Register scratch4, |
| 7805 | Register scratch5, |
| 7806 | Label* not_found) { |
| 7807 | // Register scratch3 is the general scratch register in this function. |
| 7808 | Register scratch = scratch3; |
| 7809 | |
| 7810 | // Make sure that both characters are not digits as such strings has a |
| 7811 | // different hash algorithm. Don't try to look for these in the symbol table. |
| 7812 | Label not_array_index; |
| 7813 | __ sub(scratch, c1, Operand(static_cast<int>('0'))); |
| 7814 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 7815 | __ b(hi, ¬_array_index); |
| 7816 | __ sub(scratch, c2, Operand(static_cast<int>('0'))); |
| 7817 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 7818 | |
| 7819 | // If check failed combine both characters into single halfword. |
| 7820 | // This is required by the contract of the method: code at the |
| 7821 | // not_found branch expects this combination in c1 register |
| 7822 | __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls); |
| 7823 | __ b(ls, not_found); |
| 7824 | |
| 7825 | __ bind(¬_array_index); |
| 7826 | // Calculate the two character string hash. |
| 7827 | Register hash = scratch1; |
| 7828 | GenerateHashInit(masm, hash, c1); |
| 7829 | GenerateHashAddCharacter(masm, hash, c2); |
| 7830 | GenerateHashGetHash(masm, hash); |
| 7831 | |
| 7832 | // Collect the two characters in a register. |
| 7833 | Register chars = c1; |
| 7834 | __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte)); |
| 7835 | |
| 7836 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 7837 | // hash: hash of two character string. |
| 7838 | |
| 7839 | // Load symbol table |
| 7840 | // Load address of first element of the symbol table. |
| 7841 | Register symbol_table = c2; |
| 7842 | __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex); |
| 7843 | |
| 7844 | // Load undefined value |
| 7845 | Register undefined = scratch4; |
| 7846 | __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex); |
| 7847 | |
| 7848 | // Calculate capacity mask from the symbol table capacity. |
| 7849 | Register mask = scratch2; |
| 7850 | __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset)); |
| 7851 | __ mov(mask, Operand(mask, ASR, 1)); |
| 7852 | __ sub(mask, mask, Operand(1)); |
| 7853 | |
| 7854 | // Calculate untagged address of the first element of the symbol table. |
| 7855 | Register first_symbol_table_element = symbol_table; |
| 7856 | __ add(first_symbol_table_element, symbol_table, |
| 7857 | Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag)); |
| 7858 | |
| 7859 | // Registers |
| 7860 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 7861 | // hash: hash of two character string |
| 7862 | // mask: capacity mask |
| 7863 | // first_symbol_table_element: address of the first element of |
| 7864 | // the symbol table |
| 7865 | // scratch: - |
| 7866 | |
| 7867 | // Perform a number of probes in the symbol table. |
| 7868 | static const int kProbes = 4; |
| 7869 | Label found_in_symbol_table; |
| 7870 | Label next_probe[kProbes]; |
| 7871 | for (int i = 0; i < kProbes; i++) { |
| 7872 | Register candidate = scratch5; // Scratch register contains candidate. |
| 7873 | |
| 7874 | // Calculate entry in symbol table. |
| 7875 | if (i > 0) { |
| 7876 | __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i))); |
| 7877 | } else { |
| 7878 | __ mov(candidate, hash); |
| 7879 | } |
| 7880 | |
| 7881 | __ and_(candidate, candidate, Operand(mask)); |
| 7882 | |
| 7883 | // Load the entry from the symble table. |
| 7884 | ASSERT_EQ(1, SymbolTable::kEntrySize); |
| 7885 | __ ldr(candidate, |
| 7886 | MemOperand(first_symbol_table_element, |
| 7887 | candidate, |
| 7888 | LSL, |
| 7889 | kPointerSizeLog2)); |
| 7890 | |
| 7891 | // If entry is undefined no string with this hash can be found. |
| 7892 | __ cmp(candidate, undefined); |
| 7893 | __ b(eq, not_found); |
| 7894 | |
| 7895 | // If length is not 2 the string is not a candidate. |
| 7896 | __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset)); |
| 7897 | __ cmp(scratch, Operand(2)); |
| 7898 | __ b(ne, &next_probe[i]); |
| 7899 | |
| 7900 | // Check that the candidate is a non-external ascii string. |
| 7901 | __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset)); |
| 7902 | __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset)); |
| 7903 | __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, |
| 7904 | &next_probe[i]); |
| 7905 | |
| 7906 | // Check if the two characters match. |
| 7907 | // Assumes that word load is little endian. |
| 7908 | __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize)); |
| 7909 | __ cmp(chars, scratch); |
| 7910 | __ b(eq, &found_in_symbol_table); |
| 7911 | __ bind(&next_probe[i]); |
| 7912 | } |
| 7913 | |
| 7914 | // No matching 2 character string found by probing. |
| 7915 | __ jmp(not_found); |
| 7916 | |
| 7917 | // Scratch register contains result when we fall through to here. |
| 7918 | Register result = scratch; |
| 7919 | __ bind(&found_in_symbol_table); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 7920 | __ Move(r0, result); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7921 | } |
| 7922 | |
| 7923 | |
| 7924 | void StringStubBase::GenerateHashInit(MacroAssembler* masm, |
| 7925 | Register hash, |
| 7926 | Register character) { |
| 7927 | // hash = character + (character << 10); |
| 7928 | __ add(hash, character, Operand(character, LSL, 10)); |
| 7929 | // hash ^= hash >> 6; |
| 7930 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 7931 | } |
| 7932 | |
| 7933 | |
| 7934 | void StringStubBase::GenerateHashAddCharacter(MacroAssembler* masm, |
| 7935 | Register hash, |
| 7936 | Register character) { |
| 7937 | // hash += character; |
| 7938 | __ add(hash, hash, Operand(character)); |
| 7939 | // hash += hash << 10; |
| 7940 | __ add(hash, hash, Operand(hash, LSL, 10)); |
| 7941 | // hash ^= hash >> 6; |
| 7942 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 7943 | } |
| 7944 | |
| 7945 | |
| 7946 | void StringStubBase::GenerateHashGetHash(MacroAssembler* masm, |
| 7947 | Register hash) { |
| 7948 | // hash += hash << 3; |
| 7949 | __ add(hash, hash, Operand(hash, LSL, 3)); |
| 7950 | // hash ^= hash >> 11; |
| 7951 | __ eor(hash, hash, Operand(hash, ASR, 11)); |
| 7952 | // hash += hash << 15; |
| 7953 | __ add(hash, hash, Operand(hash, LSL, 15), SetCC); |
| 7954 | |
| 7955 | // if (hash == 0) hash = 27; |
| 7956 | __ mov(hash, Operand(27), LeaveCC, nz); |
| 7957 | } |
| 7958 | |
| 7959 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7960 | void SubStringStub::Generate(MacroAssembler* masm) { |
| 7961 | Label runtime; |
| 7962 | |
| 7963 | // Stack frame on entry. |
| 7964 | // lr: return address |
| 7965 | // sp[0]: to |
| 7966 | // sp[4]: from |
| 7967 | // sp[8]: string |
| 7968 | |
| 7969 | // This stub is called from the native-call %_SubString(...), so |
| 7970 | // nothing can be assumed about the arguments. It is tested that: |
| 7971 | // "string" is a sequential string, |
| 7972 | // both "from" and "to" are smis, and |
| 7973 | // 0 <= from <= to <= string.length. |
| 7974 | // If any of these assumptions fail, we call the runtime system. |
| 7975 | |
| 7976 | static const int kToOffset = 0 * kPointerSize; |
| 7977 | static const int kFromOffset = 1 * kPointerSize; |
| 7978 | static const int kStringOffset = 2 * kPointerSize; |
| 7979 | |
| 7980 | |
| 7981 | // Check bounds and smi-ness. |
| 7982 | __ ldr(r7, MemOperand(sp, kToOffset)); |
| 7983 | __ ldr(r6, MemOperand(sp, kFromOffset)); |
| 7984 | ASSERT_EQ(0, kSmiTag); |
| 7985 | ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize); |
| 7986 | // I.e., arithmetic shift right by one un-smi-tags. |
| 7987 | __ mov(r2, Operand(r7, ASR, 1), SetCC); |
| 7988 | __ mov(r3, Operand(r6, ASR, 1), SetCC, cc); |
| 7989 | // If either r2 or r6 had the smi tag bit set, then carry is set now. |
| 7990 | __ b(cs, &runtime); // Either "from" or "to" is not a smi. |
| 7991 | __ b(mi, &runtime); // From is negative. |
| 7992 | |
| 7993 | __ sub(r2, r2, Operand(r3), SetCC); |
| 7994 | __ b(mi, &runtime); // Fail if from > to. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7995 | // Special handling of sub-strings of length 1 and 2. One character strings |
| 7996 | // are handled in the runtime system (looked up in the single character |
| 7997 | // cache). Two character strings are looked for in the symbol cache. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7998 | __ cmp(r2, Operand(2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7999 | __ b(lt, &runtime); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8000 | |
| 8001 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8002 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8003 | // r6: from (smi) |
| 8004 | // r7: to (smi) |
| 8005 | |
| 8006 | // Make sure first argument is a sequential (or flat) string. |
| 8007 | __ ldr(r5, MemOperand(sp, kStringOffset)); |
| 8008 | ASSERT_EQ(0, kSmiTag); |
| 8009 | __ tst(r5, Operand(kSmiTagMask)); |
| 8010 | __ b(eq, &runtime); |
| 8011 | Condition is_string = masm->IsObjectStringType(r5, r1); |
| 8012 | __ b(NegateCondition(is_string), &runtime); |
| 8013 | |
| 8014 | // r1: instance type |
| 8015 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8016 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8017 | // r5: string |
| 8018 | // r6: from (smi) |
| 8019 | // r7: to (smi) |
| 8020 | Label seq_string; |
| 8021 | __ and_(r4, r1, Operand(kStringRepresentationMask)); |
| 8022 | ASSERT(kSeqStringTag < kConsStringTag); |
| 8023 | ASSERT(kExternalStringTag > kConsStringTag); |
| 8024 | __ cmp(r4, Operand(kConsStringTag)); |
| 8025 | __ b(gt, &runtime); // External strings go to runtime. |
| 8026 | __ b(lt, &seq_string); // Sequential strings are handled directly. |
| 8027 | |
| 8028 | // Cons string. Try to recurse (once) on the first substring. |
| 8029 | // (This adds a little more generality than necessary to handle flattened |
| 8030 | // cons strings, but not much). |
| 8031 | __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset)); |
| 8032 | __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset)); |
| 8033 | __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8034 | __ tst(r1, Operand(kStringRepresentationMask)); |
| 8035 | ASSERT_EQ(0, kSeqStringTag); |
| 8036 | __ b(ne, &runtime); // Cons and External strings go to runtime. |
| 8037 | |
| 8038 | // Definitly a sequential string. |
| 8039 | __ bind(&seq_string); |
| 8040 | |
| 8041 | // r1: instance type. |
| 8042 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8043 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8044 | // r5: string |
| 8045 | // r6: from (smi) |
| 8046 | // r7: to (smi) |
| 8047 | __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset)); |
| 8048 | __ cmp(r4, Operand(r7, ASR, 1)); |
| 8049 | __ b(lt, &runtime); // Fail if to > length. |
| 8050 | |
| 8051 | // r1: instance type. |
| 8052 | // r2: result string length. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8053 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8054 | // r5: string. |
| 8055 | // r6: from offset (smi) |
| 8056 | // Check for flat ascii string. |
| 8057 | Label non_ascii_flat; |
| 8058 | __ tst(r1, Operand(kStringEncodingMask)); |
| 8059 | ASSERT_EQ(0, kTwoByteStringTag); |
| 8060 | __ b(eq, &non_ascii_flat); |
| 8061 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8062 | Label result_longer_than_two; |
| 8063 | __ cmp(r2, Operand(2)); |
| 8064 | __ b(gt, &result_longer_than_two); |
| 8065 | |
| 8066 | // Sub string of length 2 requested. |
| 8067 | // Get the two characters forming the sub string. |
| 8068 | __ add(r5, r5, Operand(r3)); |
| 8069 | __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize)); |
| 8070 | __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1)); |
| 8071 | |
| 8072 | // Try to lookup two character string in symbol table. |
| 8073 | Label make_two_character_string; |
| 8074 | GenerateTwoCharacterSymbolTableProbe(masm, r3, r4, r1, r5, r6, r7, r9, |
| 8075 | &make_two_character_string); |
| 8076 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 8077 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 8078 | __ Ret(); |
| 8079 | |
| 8080 | // r2: result string length. |
| 8081 | // r3: two characters combined into halfword in little endian byte order. |
| 8082 | __ bind(&make_two_character_string); |
| 8083 | __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime); |
| 8084 | __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 8085 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 8086 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 8087 | __ Ret(); |
| 8088 | |
| 8089 | __ bind(&result_longer_than_two); |
| 8090 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8091 | // Allocate the result. |
| 8092 | __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime); |
| 8093 | |
| 8094 | // r0: result string. |
| 8095 | // r2: result string length. |
| 8096 | // r5: string. |
| 8097 | // r6: from offset (smi) |
| 8098 | // Locate first character of result. |
| 8099 | __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8100 | // Locate 'from' character of string. |
| 8101 | __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8102 | __ add(r5, r5, Operand(r6, ASR, 1)); |
| 8103 | |
| 8104 | // r0: result string. |
| 8105 | // r1: first character of result string. |
| 8106 | // r2: result string length. |
| 8107 | // r5: first character of sub string to copy. |
| 8108 | ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask); |
| 8109 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 8110 | COPY_ASCII | DEST_ALWAYS_ALIGNED); |
| 8111 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 8112 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 8113 | __ Ret(); |
| 8114 | |
| 8115 | __ bind(&non_ascii_flat); |
| 8116 | // r2: result string length. |
| 8117 | // r5: string. |
| 8118 | // r6: from offset (smi) |
| 8119 | // Check for flat two byte string. |
| 8120 | |
| 8121 | // Allocate the result. |
| 8122 | __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime); |
| 8123 | |
| 8124 | // r0: result string. |
| 8125 | // r2: result string length. |
| 8126 | // r5: string. |
| 8127 | // Locate first character of result. |
| 8128 | __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8129 | // Locate 'from' character of string. |
| 8130 | __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8131 | // As "from" is a smi it is 2 times the value which matches the size of a two |
| 8132 | // byte character. |
| 8133 | __ add(r5, r5, Operand(r6)); |
| 8134 | |
| 8135 | // r0: result string. |
| 8136 | // r1: first character of result. |
| 8137 | // r2: result length. |
| 8138 | // r5: first character of string to copy. |
| 8139 | ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask); |
| 8140 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 8141 | DEST_ALWAYS_ALIGNED); |
| 8142 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 8143 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 8144 | __ Ret(); |
| 8145 | |
| 8146 | // Just jump to runtime to create the sub string. |
| 8147 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8148 | __ TailCallRuntime(Runtime::kSubString, 3, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8149 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8150 | |
| 8151 | |
| 8152 | void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 8153 | Register left, |
| 8154 | Register right, |
| 8155 | Register scratch1, |
| 8156 | Register scratch2, |
| 8157 | Register scratch3, |
| 8158 | Register scratch4) { |
| 8159 | Label compare_lengths; |
| 8160 | // Find minimum length and length difference. |
| 8161 | __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset)); |
| 8162 | __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset)); |
| 8163 | __ sub(scratch3, scratch1, Operand(scratch2), SetCC); |
| 8164 | Register length_delta = scratch3; |
| 8165 | __ mov(scratch1, scratch2, LeaveCC, gt); |
| 8166 | Register min_length = scratch1; |
| 8167 | __ tst(min_length, Operand(min_length)); |
| 8168 | __ b(eq, &compare_lengths); |
| 8169 | |
| 8170 | // Setup registers so that we only need to increment one register |
| 8171 | // in the loop. |
| 8172 | __ add(scratch2, min_length, |
| 8173 | Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8174 | __ add(left, left, Operand(scratch2)); |
| 8175 | __ add(right, right, Operand(scratch2)); |
| 8176 | // Registers left and right points to the min_length character of strings. |
| 8177 | __ rsb(min_length, min_length, Operand(-1)); |
| 8178 | Register index = min_length; |
| 8179 | // Index starts at -min_length. |
| 8180 | |
| 8181 | { |
| 8182 | // Compare loop. |
| 8183 | Label loop; |
| 8184 | __ bind(&loop); |
| 8185 | // Compare characters. |
| 8186 | __ add(index, index, Operand(1), SetCC); |
| 8187 | __ ldrb(scratch2, MemOperand(left, index), ne); |
| 8188 | __ ldrb(scratch4, MemOperand(right, index), ne); |
| 8189 | // Skip to compare lengths with eq condition true. |
| 8190 | __ b(eq, &compare_lengths); |
| 8191 | __ cmp(scratch2, scratch4); |
| 8192 | __ b(eq, &loop); |
| 8193 | // Fallthrough with eq condition false. |
| 8194 | } |
| 8195 | // Compare lengths - strings up to min-length are equal. |
| 8196 | __ bind(&compare_lengths); |
| 8197 | ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0)); |
| 8198 | // Use zero length_delta as result. |
| 8199 | __ mov(r0, Operand(length_delta), SetCC, eq); |
| 8200 | // Fall through to here if characters compare not-equal. |
| 8201 | __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt); |
| 8202 | __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt); |
| 8203 | __ Ret(); |
| 8204 | } |
| 8205 | |
| 8206 | |
| 8207 | void StringCompareStub::Generate(MacroAssembler* masm) { |
| 8208 | Label runtime; |
| 8209 | |
| 8210 | // Stack frame on entry. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8211 | // sp[0]: right string |
| 8212 | // sp[4]: left string |
| 8213 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left |
| 8214 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8215 | |
| 8216 | Label not_same; |
| 8217 | __ cmp(r0, r1); |
| 8218 | __ b(ne, ¬_same); |
| 8219 | ASSERT_EQ(0, EQUAL); |
| 8220 | ASSERT_EQ(0, kSmiTag); |
| 8221 | __ mov(r0, Operand(Smi::FromInt(EQUAL))); |
| 8222 | __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2); |
| 8223 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8224 | __ Ret(); |
| 8225 | |
| 8226 | __ bind(¬_same); |
| 8227 | |
| 8228 | // Check that both objects are sequential ascii strings. |
| 8229 | __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime); |
| 8230 | |
| 8231 | // Compare flat ascii strings natively. Remove arguments from stack first. |
| 8232 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 8233 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8234 | GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5); |
| 8235 | |
| 8236 | // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater) |
| 8237 | // tagged as a small integer. |
| 8238 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8239 | __ TailCallRuntime(Runtime::kStringCompare, 2, 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8240 | } |
| 8241 | |
| 8242 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8243 | void StringAddStub::Generate(MacroAssembler* masm) { |
| 8244 | Label string_add_runtime; |
| 8245 | // Stack on entry: |
| 8246 | // sp[0]: second argument. |
| 8247 | // sp[4]: first argument. |
| 8248 | |
| 8249 | // Load the two arguments. |
| 8250 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument. |
| 8251 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument. |
| 8252 | |
| 8253 | // Make sure that both arguments are strings if not known in advance. |
| 8254 | if (string_check_) { |
| 8255 | ASSERT_EQ(0, kSmiTag); |
| 8256 | __ JumpIfEitherSmi(r0, r1, &string_add_runtime); |
| 8257 | // Load instance types. |
| 8258 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8259 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 8260 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8261 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 8262 | ASSERT_EQ(0, kStringTag); |
| 8263 | // If either is not a string, go to runtime. |
| 8264 | __ tst(r4, Operand(kIsNotStringMask)); |
| 8265 | __ tst(r5, Operand(kIsNotStringMask), eq); |
| 8266 | __ b(ne, &string_add_runtime); |
| 8267 | } |
| 8268 | |
| 8269 | // Both arguments are strings. |
| 8270 | // r0: first string |
| 8271 | // r1: second string |
| 8272 | // r4: first string instance type (if string_check_) |
| 8273 | // r5: second string instance type (if string_check_) |
| 8274 | { |
| 8275 | Label strings_not_empty; |
| 8276 | // Check if either of the strings are empty. In that case return the other. |
| 8277 | __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset)); |
| 8278 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
| 8279 | __ cmp(r2, Operand(0)); // Test if first string is empty. |
| 8280 | __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second. |
| 8281 | __ cmp(r3, Operand(0), ne); // Else test if second string is empty. |
| 8282 | __ b(ne, &strings_not_empty); // If either string was empty, return r0. |
| 8283 | |
| 8284 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8285 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8286 | __ Ret(); |
| 8287 | |
| 8288 | __ bind(&strings_not_empty); |
| 8289 | } |
| 8290 | |
| 8291 | // Both strings are non-empty. |
| 8292 | // r0: first string |
| 8293 | // r1: second string |
| 8294 | // r2: length of first string |
| 8295 | // r3: length of second string |
| 8296 | // r4: first string instance type (if string_check_) |
| 8297 | // r5: second string instance type (if string_check_) |
| 8298 | // 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] | 8299 | Label string_add_flat_result, longer_than_two; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8300 | // Adding two lengths can't overflow. |
| 8301 | ASSERT(String::kMaxLength * 2 > String::kMaxLength); |
| 8302 | __ add(r6, r2, Operand(r3)); |
| 8303 | // Use the runtime system when adding two one character strings, as it |
| 8304 | // contains optimizations for this specific case using the symbol table. |
| 8305 | __ cmp(r6, Operand(2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8306 | __ b(ne, &longer_than_two); |
| 8307 | |
| 8308 | // Check that both strings are non-external ascii strings. |
| 8309 | if (!string_check_) { |
| 8310 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8311 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 8312 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8313 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 8314 | } |
| 8315 | __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7, |
| 8316 | &string_add_runtime); |
| 8317 | |
| 8318 | // Get the two characters forming the sub string. |
| 8319 | __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 8320 | __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 8321 | |
| 8322 | // Try to lookup two character string in symbol table. If it is not found |
| 8323 | // just allocate a new one. |
| 8324 | Label make_two_character_string; |
| 8325 | GenerateTwoCharacterSymbolTableProbe(masm, r2, r3, r6, r7, r4, r5, r9, |
| 8326 | &make_two_character_string); |
| 8327 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8328 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8329 | __ Ret(); |
| 8330 | |
| 8331 | __ bind(&make_two_character_string); |
| 8332 | // Resulting string has length 2 and first chars of two strings |
| 8333 | // are combined into single halfword in r2 register. |
| 8334 | // So we can fill resulting string without two loops by a single |
| 8335 | // halfword store instruction (which assumes that processor is |
| 8336 | // in a little endian mode) |
| 8337 | __ mov(r6, Operand(2)); |
| 8338 | __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime); |
| 8339 | __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 8340 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8341 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8342 | __ Ret(); |
| 8343 | |
| 8344 | __ bind(&longer_than_two); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8345 | // Check if resulting string will be flat. |
| 8346 | __ cmp(r6, Operand(String::kMinNonFlatLength)); |
| 8347 | __ b(lt, &string_add_flat_result); |
| 8348 | // Handle exceptionally long strings in the runtime system. |
| 8349 | ASSERT((String::kMaxLength & 0x80000000) == 0); |
| 8350 | ASSERT(IsPowerOf2(String::kMaxLength + 1)); |
| 8351 | // kMaxLength + 1 is representable as shifted literal, kMaxLength is not. |
| 8352 | __ cmp(r6, Operand(String::kMaxLength + 1)); |
| 8353 | __ b(hs, &string_add_runtime); |
| 8354 | |
| 8355 | // If result is not supposed to be flat, allocate a cons string object. |
| 8356 | // If both strings are ascii the result is an ascii cons string. |
| 8357 | if (!string_check_) { |
| 8358 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8359 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 8360 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8361 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 8362 | } |
| 8363 | Label non_ascii, allocated; |
| 8364 | ASSERT_EQ(0, kTwoByteStringTag); |
| 8365 | __ tst(r4, Operand(kStringEncodingMask)); |
| 8366 | __ tst(r5, Operand(kStringEncodingMask), ne); |
| 8367 | __ b(eq, &non_ascii); |
| 8368 | |
| 8369 | // Allocate an ASCII cons string. |
| 8370 | __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime); |
| 8371 | __ bind(&allocated); |
| 8372 | // Fill the fields of the cons string. |
| 8373 | __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset)); |
| 8374 | __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset)); |
| 8375 | __ mov(r0, Operand(r7)); |
| 8376 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8377 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8378 | __ Ret(); |
| 8379 | |
| 8380 | __ bind(&non_ascii); |
| 8381 | // Allocate a two byte cons string. |
| 8382 | __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime); |
| 8383 | __ jmp(&allocated); |
| 8384 | |
| 8385 | // Handle creating a flat result. First check that both strings are |
| 8386 | // sequential and that they have the same encoding. |
| 8387 | // r0: first string |
| 8388 | // r1: second string |
| 8389 | // r2: length of first string |
| 8390 | // r3: length of second string |
| 8391 | // r4: first string instance type (if string_check_) |
| 8392 | // r5: second string instance type (if string_check_) |
| 8393 | // r6: sum of lengths. |
| 8394 | __ bind(&string_add_flat_result); |
| 8395 | if (!string_check_) { |
| 8396 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8397 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 8398 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8399 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 8400 | } |
| 8401 | // Check that both strings are sequential. |
| 8402 | ASSERT_EQ(0, kSeqStringTag); |
| 8403 | __ tst(r4, Operand(kStringRepresentationMask)); |
| 8404 | __ tst(r5, Operand(kStringRepresentationMask), eq); |
| 8405 | __ b(ne, &string_add_runtime); |
| 8406 | // Now check if both strings have the same encoding (ASCII/Two-byte). |
| 8407 | // r0: first string. |
| 8408 | // r1: second string. |
| 8409 | // r2: length of first string. |
| 8410 | // r3: length of second string. |
| 8411 | // r6: sum of lengths.. |
| 8412 | Label non_ascii_string_add_flat_result; |
| 8413 | ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test. |
| 8414 | __ eor(r7, r4, Operand(r5)); |
| 8415 | __ tst(r7, Operand(kStringEncodingMask)); |
| 8416 | __ b(ne, &string_add_runtime); |
| 8417 | // And see if it's ASCII or two-byte. |
| 8418 | __ tst(r4, Operand(kStringEncodingMask)); |
| 8419 | __ b(eq, &non_ascii_string_add_flat_result); |
| 8420 | |
| 8421 | // Both strings are sequential ASCII strings. We also know that they are |
| 8422 | // short (since the sum of the lengths is less than kMinNonFlatLength). |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8423 | // r6: length of resulting flat string |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8424 | __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 8425 | // Locate first character of result. |
| 8426 | __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8427 | // Locate first character of first argument. |
| 8428 | __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8429 | // r0: first character of first string. |
| 8430 | // r1: second string. |
| 8431 | // r2: length of first string. |
| 8432 | // r3: length of second string. |
| 8433 | // r6: first character of result. |
| 8434 | // r7: result string. |
| 8435 | GenerateCopyCharacters(masm, r6, r0, r2, r4, true); |
| 8436 | |
| 8437 | // Load second argument and locate first character. |
| 8438 | __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8439 | // r1: first character of second string. |
| 8440 | // r3: length of second string. |
| 8441 | // r6: next character of result. |
| 8442 | // r7: result string. |
| 8443 | GenerateCopyCharacters(masm, r6, r1, r3, r4, true); |
| 8444 | __ mov(r0, Operand(r7)); |
| 8445 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8446 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8447 | __ Ret(); |
| 8448 | |
| 8449 | __ bind(&non_ascii_string_add_flat_result); |
| 8450 | // Both strings are sequential two byte strings. |
| 8451 | // r0: first string. |
| 8452 | // r1: second string. |
| 8453 | // r2: length of first string. |
| 8454 | // r3: length of second string. |
| 8455 | // r6: sum of length of strings. |
| 8456 | __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 8457 | // r0: first string. |
| 8458 | // r1: second string. |
| 8459 | // r2: length of first string. |
| 8460 | // r3: length of second string. |
| 8461 | // r7: result string. |
| 8462 | |
| 8463 | // Locate first character of result. |
| 8464 | __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8465 | // Locate first character of first argument. |
| 8466 | __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8467 | |
| 8468 | // r0: first character of first string. |
| 8469 | // r1: second string. |
| 8470 | // r2: length of first string. |
| 8471 | // r3: length of second string. |
| 8472 | // r6: first character of result. |
| 8473 | // r7: result string. |
| 8474 | GenerateCopyCharacters(masm, r6, r0, r2, r4, false); |
| 8475 | |
| 8476 | // Locate first character of second argument. |
| 8477 | __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8478 | |
| 8479 | // r1: first character of second string. |
| 8480 | // r3: length of second string. |
| 8481 | // r6: next character of result (after copy of first string). |
| 8482 | // r7: result string. |
| 8483 | GenerateCopyCharacters(masm, r6, r1, r3, r4, false); |
| 8484 | |
| 8485 | __ mov(r0, Operand(r7)); |
| 8486 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8487 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8488 | __ Ret(); |
| 8489 | |
| 8490 | // Just jump to runtime to add the two strings. |
| 8491 | __ bind(&string_add_runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8492 | __ TailCallRuntime(Runtime::kStringAdd, 2, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8493 | } |
| 8494 | |
| 8495 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8496 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8497 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8498 | } } // namespace v8::internal |