sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "debug.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 34 | #include "parser.h" |
| 35 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 37 | #include "scopes.h" |
| 38 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 43 | #define __ ACCESS_MASM(masm_) |
| 44 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 45 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 46 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 47 | Condition cc, |
| 48 | bool never_nan_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 49 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 50 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 51 | Label* slow, |
| 52 | bool strict); |
| 53 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
| 54 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 55 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 56 | Register source, |
| 57 | Register destination, |
| 58 | int known_int); |
| 59 | static bool IsEasyToMultiplyBy(int x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 63 | // ------------------------------------------------------------------------- |
| 64 | // Platform-specific DeferredCode functions. |
| 65 | |
| 66 | void DeferredCode::SaveRegisters() { |
| 67 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 68 | int action = registers_[i]; |
| 69 | if (action == kPush) { |
| 70 | __ push(RegisterAllocator::ToRegister(i)); |
| 71 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 72 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void DeferredCode::RestoreRegisters() { |
| 79 | // Restore registers in reverse order due to the stack. |
| 80 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 81 | int action = registers_[i]; |
| 82 | if (action == kPush) { |
| 83 | __ pop(RegisterAllocator::ToRegister(i)); |
| 84 | } else if (action != kIgnore) { |
| 85 | action &= ~kSyncedFlag; |
| 86 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 91 | |
| 92 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 93 | // CodeGenState implementation. |
| 94 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 95 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 96 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 97 | true_target_(NULL), |
| 98 | false_target_(NULL), |
| 99 | previous_(NULL) { |
| 100 | owner_->set_state(this); |
| 101 | } |
| 102 | |
| 103 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 104 | CodeGenState::CodeGenState(CodeGenerator* owner, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 105 | JumpTarget* true_target, |
| 106 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 107 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 108 | true_target_(true_target), |
| 109 | false_target_(false_target), |
| 110 | previous_(owner->state()) { |
| 111 | owner_->set_state(this); |
| 112 | } |
| 113 | |
| 114 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 115 | CodeGenState::~CodeGenState() { |
| 116 | ASSERT(owner_->state() == this); |
| 117 | owner_->set_state(previous_); |
| 118 | } |
| 119 | |
| 120 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 121 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 122 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 124 | CodeGenerator::CodeGenerator(MacroAssembler* masm) |
| 125 | : deferred_(8), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 126 | masm_(masm), |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 127 | info_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 128 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 129 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | cc_reg_(al), |
| 131 | state_(NULL), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 132 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 136 | Scope* CodeGenerator::scope() { return info_->function()->scope(); } |
| 137 | |
| 138 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 139 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 140 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 142 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | // cp: callee's context |
| 144 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame^] | 145 | void CodeGenerator::Generate(CompilationInfo* info) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 146 | // Record the position for debugging purposes. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 147 | CodeForFunctionPosition(info->function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | |
| 149 | // Initialize state. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 150 | info_ = info; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 151 | ASSERT(allocator_ == NULL); |
| 152 | RegisterAllocator register_allocator(this); |
| 153 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 154 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 155 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 156 | cc_reg_ = al; |
| 157 | { |
| 158 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 159 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 160 | // Entry: |
| 161 | // Stack: receiver, arguments |
| 162 | // lr: return address |
| 163 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 165 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 166 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 167 | allocator_->Initialize(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 168 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 169 | #ifdef DEBUG |
| 170 | if (strlen(FLAG_stop_at) > 0 && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 171 | info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 172 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 173 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | } |
| 175 | #endif |
| 176 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame^] | 177 | if (info->mode() == CompilationInfo::PRIMARY) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 178 | frame_->Enter(); |
| 179 | // tos: code slot |
| 180 | |
| 181 | // Allocate space for locals and initialize them. This also checks |
| 182 | // for stack overflow. |
| 183 | frame_->AllocateStackSlots(); |
| 184 | |
| 185 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 186 | int heap_slots = scope()->num_heap_slots(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 187 | if (heap_slots > 0) { |
| 188 | // Allocate local context. |
| 189 | // Get outer context and create a new context based on it. |
| 190 | __ ldr(r0, frame_->Function()); |
| 191 | frame_->EmitPush(r0); |
| 192 | if (heap_slots <= FastNewContextStub::kMaximumSlots) { |
| 193 | FastNewContextStub stub(heap_slots); |
| 194 | frame_->CallStub(&stub, 1); |
| 195 | } else { |
| 196 | frame_->CallRuntime(Runtime::kNewContext, 1); |
| 197 | } |
| 198 | |
| 199 | #ifdef DEBUG |
| 200 | JumpTarget verified_true; |
| 201 | __ cmp(r0, Operand(cp)); |
| 202 | verified_true.Branch(eq); |
| 203 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 204 | verified_true.Bind(); |
| 205 | #endif |
| 206 | // Update context local. |
| 207 | __ str(cp, frame_->Context()); |
| 208 | } |
| 209 | |
| 210 | // TODO(1241774): Improve this code: |
| 211 | // 1) only needed if we have a context |
| 212 | // 2) no need to recompute context ptr every single time |
| 213 | // 3) don't copy parameter operand code from SlotOperand! |
| 214 | { |
| 215 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 216 | // Note that iteration order is relevant here! If we have the same |
| 217 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 218 | // needs to be copied into the context, it must be the last argument |
| 219 | // passed to the parameter that needs to be copied. This is a rare |
| 220 | // case so we don't check for it, instead we rely on the copying |
| 221 | // order: such a parameter is copied repeatedly into the same |
| 222 | // context location and thus the last value is what is seen inside |
| 223 | // the function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 224 | for (int i = 0; i < scope()->num_parameters(); i++) { |
| 225 | Variable* par = scope()->parameter(i); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 226 | Slot* slot = par->slot(); |
| 227 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 228 | ASSERT(!scope()->is_global_scope()); // No params in global scope. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 229 | __ ldr(r1, frame_->ParameterAt(i)); |
| 230 | // Loads r2 with context; used below in RecordWrite. |
| 231 | __ str(r1, SlotOperand(slot, r2)); |
| 232 | // Load the offset into r3. |
| 233 | int slot_offset = |
| 234 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 235 | __ mov(r3, Operand(slot_offset)); |
| 236 | __ RecordWrite(r2, r3, r1); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Store the arguments object. This must happen after context |
| 242 | // initialization because the arguments object may be stored in the |
| 243 | // context. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 244 | if (scope()->arguments() != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 245 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 246 | ASSERT(scope()->arguments_shadow() != NULL); |
| 247 | Variable* arguments = scope()->arguments()->var(); |
| 248 | Variable* shadow = scope()->arguments_shadow()->var(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 249 | ASSERT(arguments != NULL && arguments->slot() != NULL); |
| 250 | ASSERT(shadow != NULL && shadow->slot() != NULL); |
| 251 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
| 252 | __ ldr(r2, frame_->Function()); |
| 253 | // The receiver is below the arguments, the return address, and the |
| 254 | // frame pointer on the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 255 | const int kReceiverDisplacement = 2 + scope()->num_parameters(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 256 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 257 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 258 | frame_->Adjust(3); |
| 259 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
| 260 | frame_->CallStub(&stub, 3); |
| 261 | frame_->EmitPush(r0); |
| 262 | StoreToSlot(arguments->slot(), NOT_CONST_INIT); |
| 263 | StoreToSlot(shadow->slot(), NOT_CONST_INIT); |
| 264 | frame_->Drop(); // Value is no longer needed. |
| 265 | } |
| 266 | |
| 267 | // Initialize ThisFunction reference if present. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 268 | if (scope()->is_function_scope() && scope()->function() != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 269 | __ mov(ip, Operand(Factory::the_hole_value())); |
| 270 | frame_->EmitPush(ip); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 271 | StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 272 | } |
| 273 | } else { |
| 274 | // When used as the secondary compiler for splitting, r1, cp, |
| 275 | // fp, and lr have been pushed on the stack. Adjust the virtual |
| 276 | // frame to match this state. |
| 277 | frame_->Adjust(4); |
| 278 | allocator_->Unuse(r1); |
| 279 | allocator_->Unuse(lr); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame^] | 280 | |
| 281 | // Bind all the bailout labels to the beginning of the function. |
| 282 | List<CompilationInfo::Bailout*>* bailouts = info->bailouts(); |
| 283 | for (int i = 0; i < bailouts->length(); i++) { |
| 284 | __ bind(bailouts->at(i)->label()); |
| 285 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 286 | } |
| 287 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 288 | // Initialize the function return target after the locals are set |
| 289 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 290 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 291 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 292 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 293 | // Generate code to 'execute' declarations and initialize functions |
| 294 | // (source elements). In case of an illegal redeclaration we need to |
| 295 | // handle that instead of processing the declarations. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 296 | if (scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 298 | scope()->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | } else { |
| 300 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 301 | ProcessDeclarations(scope()->declarations()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 302 | // Bail out if a stack-overflow exception occurred when processing |
| 303 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 304 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 305 | } |
| 306 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 307 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 308 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 309 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 310 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | |
| 312 | // Compile the body of the function in a vanilla state. Don't |
| 313 | // bother compiling all the code if the scope has an illegal |
| 314 | // redeclaration. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 315 | if (!scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 316 | Comment cmnt(masm_, "[ function body"); |
| 317 | #ifdef DEBUG |
| 318 | bool is_builtin = Bootstrapper::IsActive(); |
| 319 | bool should_trace = |
| 320 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 321 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 322 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 323 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 324 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 325 | #endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 326 | VisitStatementsAndSpill(info->function()->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 328 | } |
| 329 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 330 | // Generate the return sequence if necessary. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 331 | if (has_valid_frame() || function_return_.is_linked()) { |
| 332 | if (!function_return_.is_linked()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 333 | CodeForReturnPosition(info->function()); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 334 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 335 | // exit |
| 336 | // r0: result |
| 337 | // sp: stack pointer |
| 338 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 339 | // cp: callee's context |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 340 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 341 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 342 | function_return_.Bind(); |
| 343 | if (FLAG_trace) { |
| 344 | // Push the return value on the stack as the parameter. |
| 345 | // Runtime::TraceExit returns the parameter as it is. |
| 346 | frame_->EmitPush(r0); |
| 347 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 348 | } |
| 349 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 350 | // Add a label for checking the size of the code used for returning. |
| 351 | Label check_exit_codesize; |
| 352 | masm_->bind(&check_exit_codesize); |
| 353 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 354 | // Calculate the exact length of the return sequence and make sure that |
| 355 | // the constant pool is not emitted inside of the return sequence. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 356 | int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 357 | int return_sequence_length = Assembler::kJSReturnSequenceLength; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 358 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 359 | // Additional mov instruction generated. |
| 360 | return_sequence_length++; |
| 361 | } |
| 362 | masm_->BlockConstPoolFor(return_sequence_length); |
| 363 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 364 | // Tear down the frame which will restore the caller's frame pointer and |
| 365 | // the link register. |
| 366 | frame_->Exit(); |
| 367 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 368 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 369 | // tool from instrumenting as we rely on the code size here. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 370 | masm_->add(sp, sp, Operand(sp_delta)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 371 | masm_->Jump(lr); |
| 372 | |
| 373 | // 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] | 374 | // expected by the debugger. The add instruction above is an addressing |
| 375 | // mode 1 instruction where there are restrictions on which immediate values |
| 376 | // can be encoded in the instruction and which immediate values requires |
| 377 | // use of an additional instruction for moving the immediate to a temporary |
| 378 | // register. |
| 379 | ASSERT_EQ(return_sequence_length, |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 380 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 381 | } |
| 382 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 383 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 384 | ASSERT(!has_cc()); |
| 385 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 386 | ASSERT(!function_return_is_shadowed_); |
| 387 | function_return_.Unuse(); |
| 388 | DeleteFrame(); |
| 389 | |
| 390 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 391 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 392 | ProcessDeferred(); |
| 393 | } |
| 394 | |
| 395 | allocator_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 399 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 400 | // Currently, this assertion will fail if we try to assign to |
| 401 | // a constant variable that is constant because it is read-only |
| 402 | // (such as the variable referring to a named function expression). |
| 403 | // We need to implement assignments to read-only variables. |
| 404 | // Ideally, we should do this during AST generation (by converting |
| 405 | // such assignments into expression statements); however, in general |
| 406 | // we may not be able to make the decision until past AST generation, |
| 407 | // that is when the entire program is known. |
| 408 | ASSERT(slot != NULL); |
| 409 | int index = slot->index(); |
| 410 | switch (slot->type()) { |
| 411 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 412 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 413 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 414 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 415 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 416 | |
| 417 | case Slot::CONTEXT: { |
| 418 | // Follow the context chain if necessary. |
| 419 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 420 | Register context = cp; |
| 421 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 422 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 423 | // Load the closure. |
| 424 | // (All contexts, even 'with' contexts, have a closure, |
| 425 | // and it is the same for all contexts inside a function. |
| 426 | // There is no need to go to the function context first.) |
| 427 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 428 | // Load the function context (which is the incoming, outer context). |
| 429 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 430 | context = tmp; |
| 431 | } |
| 432 | // We may have a 'with' context now. Get the function context. |
| 433 | // (In fact this mov may never be the needed, since the scope analysis |
| 434 | // may not permit a direct context access in this case and thus we are |
| 435 | // always at a function context. However it is safe to dereference be- |
| 436 | // cause the function context of a function context is itself. Before |
| 437 | // deleting this mov we should try to create a counter-example first, |
| 438 | // though...) |
| 439 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 440 | return ContextOperand(tmp, index); |
| 441 | } |
| 442 | |
| 443 | default: |
| 444 | UNREACHABLE(); |
| 445 | return MemOperand(r0, 0); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 450 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 451 | Slot* slot, |
| 452 | Register tmp, |
| 453 | Register tmp2, |
| 454 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 455 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 456 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 457 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 458 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 459 | if (s->num_heap_slots() > 0) { |
| 460 | if (s->calls_eval()) { |
| 461 | // Check that extension is NULL. |
| 462 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 463 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 464 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 465 | } |
| 466 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 467 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 468 | context = tmp; |
| 469 | } |
| 470 | } |
| 471 | // Check that last extension is NULL. |
| 472 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 473 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 474 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 475 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 476 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 480 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 481 | // (partially) translated into branches, or it may have set the condition |
| 482 | // code register. If force_cc is set, the value is forced to set the |
| 483 | // condition code register and no value is pushed. If the condition code |
| 484 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 485 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 486 | void CodeGenerator::LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 487 | JumpTarget* true_target, |
| 488 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 489 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 490 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 491 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 492 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 493 | { CodeGenState new_state(this, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 494 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 495 | |
| 496 | // If we hit a stack overflow, we may not have actually visited |
| 497 | // the expression. In that case, we ensure that we have a |
| 498 | // valid-looking frame state because we will continue to generate |
| 499 | // code as we unwind the C++ stack. |
| 500 | // |
| 501 | // It's possible to have both a stack overflow and a valid frame |
| 502 | // state (eg, a subexpression overflowed, visiting it returned |
| 503 | // with a dummied frame state, and visiting this expression |
| 504 | // returned with a normal-looking state). |
| 505 | if (HasStackOverflow() && |
| 506 | has_valid_frame() && |
| 507 | !has_cc() && |
| 508 | frame_->height() == original_height) { |
| 509 | true_target->Jump(); |
| 510 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 511 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 512 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 513 | // Convert the TOS value to a boolean in the condition code register. |
| 514 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 515 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 516 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 517 | ASSERT(!has_valid_frame() || |
| 518 | (has_cc() && frame_->height() == original_height) || |
| 519 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 523 | void CodeGenerator::Load(Expression* expr) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 524 | #ifdef DEBUG |
| 525 | int original_height = frame_->height(); |
| 526 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 527 | JumpTarget true_target; |
| 528 | JumpTarget false_target; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 529 | LoadCondition(expr, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 530 | |
| 531 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 532 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 533 | JumpTarget loaded; |
| 534 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 535 | materialize_true.Branch(cc_reg_); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 536 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 537 | frame_->EmitPush(r0); |
| 538 | loaded.Jump(); |
| 539 | materialize_true.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 540 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 541 | frame_->EmitPush(r0); |
| 542 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | cc_reg_ = al; |
| 544 | } |
| 545 | |
| 546 | if (true_target.is_linked() || false_target.is_linked()) { |
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() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 581 | VirtualFrame::SpilledScope spilled_scope; |
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) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 588 | VirtualFrame::SpilledScope spilled_scope; |
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. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 598 | VirtualFrame::SpilledScope spilled_scope; |
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); |
| 608 | ref.GetValueAndSpill(); |
| 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) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 638 | VirtualFrame::SpilledScope spilled_scope; |
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) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 673 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 674 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 675 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 676 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 677 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 678 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 679 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 680 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 681 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 682 | ref->set_unloaded(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 686 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 687 | // register to a boolean in the condition code register. The code |
| 688 | // 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] | 689 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 690 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 691 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 692 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 693 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 694 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 695 | |
| 696 | // Fast case checks |
| 697 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 698 | // Check if the value is 'false'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 699 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 700 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 701 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 703 | // Check if the value is 'true'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 704 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 705 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 706 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 707 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 708 | // Check if the value is 'undefined'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 709 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 710 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 711 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 712 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 713 | // Check if the value is a smi. |
| 714 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 715 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 716 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 717 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 718 | |
| 719 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 720 | frame_->EmitPush(r0); |
| 721 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 722 | // Convert the result (r0) to a condition code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 723 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 724 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | |
| 726 | cc_reg_ = ne; |
| 727 | } |
| 728 | |
| 729 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 730 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 731 | OverwriteMode overwrite_mode, |
| 732 | int constant_rhs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 733 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 734 | // sp[0] : y |
| 735 | // sp[1] : x |
| 736 | // result : r0 |
| 737 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 738 | // Stub is entered with a call: 'return address' is in lr. |
| 739 | switch (op) { |
| 740 | case Token::ADD: // fall through. |
| 741 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 742 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 743 | case Token::DIV: |
| 744 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 745 | case Token::BIT_OR: |
| 746 | case Token::BIT_AND: |
| 747 | case Token::BIT_XOR: |
| 748 | case Token::SHL: |
| 749 | case Token::SHR: |
| 750 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 751 | frame_->EmitPop(r0); // r0 : y |
| 752 | frame_->EmitPop(r1); // r1 : x |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 753 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 754 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 755 | break; |
| 756 | } |
| 757 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 758 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 759 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 760 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 761 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 762 | break; |
| 763 | |
| 764 | default: |
| 765 | // Other cases should have been handled before this point. |
| 766 | UNREACHABLE(); |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 772 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 773 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 774 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 775 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 776 | bool reversed, |
| 777 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 778 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 779 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 780 | reversed_(reversed), |
| 781 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 782 | set_comment("[ DeferredInlinedSmiOperation"); |
| 783 | } |
| 784 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 785 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 786 | |
| 787 | private: |
| 788 | Token::Value op_; |
| 789 | int value_; |
| 790 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 791 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 792 | }; |
| 793 | |
| 794 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 795 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 796 | switch (op_) { |
| 797 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 798 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 799 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 800 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 801 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 802 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 803 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 804 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 805 | } |
| 806 | break; |
| 807 | } |
| 808 | |
| 809 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 810 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 811 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 812 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 813 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 814 | } else { |
| 815 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 816 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 817 | } |
| 818 | break; |
| 819 | } |
| 820 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 821 | // For these operations there is no optimistic operation that needs to be |
| 822 | // reverted. |
| 823 | case Token::MUL: |
| 824 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 825 | case Token::BIT_OR: |
| 826 | case Token::BIT_XOR: |
| 827 | case Token::BIT_AND: { |
| 828 | if (reversed_) { |
| 829 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 830 | } else { |
| 831 | __ mov(r1, Operand(r0)); |
| 832 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 833 | } |
| 834 | break; |
| 835 | } |
| 836 | |
| 837 | case Token::SHL: |
| 838 | case Token::SHR: |
| 839 | case Token::SAR: { |
| 840 | if (!reversed_) { |
| 841 | __ mov(r1, Operand(r0)); |
| 842 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 843 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 844 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 845 | } |
| 846 | break; |
| 847 | } |
| 848 | |
| 849 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 850 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 851 | UNREACHABLE(); |
| 852 | break; |
| 853 | } |
| 854 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 855 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 856 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 860 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 861 | x &= x - 1; |
| 862 | return (x & (x - 1)) == 0; |
| 863 | } |
| 864 | |
| 865 | |
| 866 | // Returns the index of the lowest bit set. |
| 867 | static int BitPosition(unsigned x) { |
| 868 | int bit_posn = 0; |
| 869 | while ((x & 0xf) == 0) { |
| 870 | bit_posn += 4; |
| 871 | x >>= 4; |
| 872 | } |
| 873 | while ((x & 1) == 0) { |
| 874 | bit_posn++; |
| 875 | x >>= 1; |
| 876 | } |
| 877 | return bit_posn; |
| 878 | } |
| 879 | |
| 880 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 881 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 882 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 883 | bool reversed, |
| 884 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 885 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 886 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 887 | // some possible smi operations (like + and -) when (at least) one |
| 888 | // of the operands is a literal smi. With this optimization, the |
| 889 | // performance of the system is increased by ~15%, and the generated |
| 890 | // code size is increased by ~1% (measured on a combination of |
| 891 | // different benchmarks). |
| 892 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 893 | // sp[0] : operand |
| 894 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 895 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 896 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 897 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 898 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 899 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 900 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 901 | switch (op) { |
| 902 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 903 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 904 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 905 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 906 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 907 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 908 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 909 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 910 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 911 | break; |
| 912 | } |
| 913 | |
| 914 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 915 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 916 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 917 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 918 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 919 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 920 | } else { |
| 921 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 922 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 923 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 924 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 925 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 926 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 927 | break; |
| 928 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 929 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 930 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 931 | case Token::BIT_OR: |
| 932 | case Token::BIT_XOR: |
| 933 | case Token::BIT_AND: { |
| 934 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 935 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 936 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 937 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 938 | switch (op) { |
| 939 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 940 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 941 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 942 | default: UNREACHABLE(); |
| 943 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 944 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 945 | break; |
| 946 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 947 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 948 | case Token::SHL: |
| 949 | case Token::SHR: |
| 950 | case Token::SAR: { |
| 951 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 952 | something_to_inline = false; |
| 953 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 954 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 955 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 956 | DeferredCode* deferred = |
| 957 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
| 958 | __ tst(r0, Operand(kSmiTagMask)); |
| 959 | deferred->Branch(ne); |
| 960 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 961 | switch (op) { |
| 962 | case Token::SHL: { |
| 963 | if (shift_value != 0) { |
| 964 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 965 | } |
| 966 | // check that the *unsigned* result fits in a smi |
| 967 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 968 | deferred->Branch(mi); |
| 969 | break; |
| 970 | } |
| 971 | case Token::SHR: { |
| 972 | // LSR by immediate 0 means shifting 32 bits. |
| 973 | if (shift_value != 0) { |
| 974 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 975 | } |
| 976 | // check that the *unsigned* result fits in a smi |
| 977 | // neither of the two high-order bits can be set: |
| 978 | // - 0x80000000: high bit would be lost when smi tagging |
| 979 | // - 0x40000000: this number would convert to negative when |
| 980 | // smi tagging these two cases can only happen with shifts |
| 981 | // by 0 or 1 when handed a valid smi |
| 982 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 983 | deferred->Branch(ne); |
| 984 | break; |
| 985 | } |
| 986 | case Token::SAR: { |
| 987 | if (shift_value != 0) { |
| 988 | // ASR by immediate 0 means shifting 32 bits. |
| 989 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 990 | } |
| 991 | break; |
| 992 | } |
| 993 | default: UNREACHABLE(); |
| 994 | } |
| 995 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 996 | deferred->BindExit(); |
| 997 | break; |
| 998 | } |
| 999 | |
| 1000 | case Token::MOD: { |
| 1001 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 1002 | something_to_inline = false; |
| 1003 | break; |
| 1004 | } |
| 1005 | DeferredCode* deferred = |
| 1006 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1007 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1008 | __ tst(r0, Operand(mask)); |
| 1009 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1010 | mask = (int_value << kSmiTagSize) - 1; |
| 1011 | __ and_(r0, r0, Operand(mask)); |
| 1012 | deferred->BindExit(); |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | case Token::MUL: { |
| 1017 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1018 | something_to_inline = false; |
| 1019 | break; |
| 1020 | } |
| 1021 | DeferredCode* deferred = |
| 1022 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1023 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1024 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1025 | unsigned mask = 0x80000000u; |
| 1026 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1027 | mask |= mask >> 1; |
| 1028 | } |
| 1029 | mask |= kSmiTagMask; |
| 1030 | // This does a single mask that checks for a too high value in a |
| 1031 | // conservative way and for a non-Smi. It also filters out negative |
| 1032 | // numbers, unfortunately, but since this code is inline we prefer |
| 1033 | // brevity to comprehensiveness. |
| 1034 | __ tst(r0, Operand(mask)); |
| 1035 | deferred->Branch(ne); |
| 1036 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 1037 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1038 | break; |
| 1039 | } |
| 1040 | |
| 1041 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1042 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1043 | break; |
| 1044 | } |
| 1045 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1046 | if (!something_to_inline) { |
| 1047 | if (!reversed) { |
| 1048 | frame_->EmitPush(r0); |
| 1049 | __ mov(r0, Operand(value)); |
| 1050 | frame_->EmitPush(r0); |
| 1051 | GenericBinaryOperation(op, mode, int_value); |
| 1052 | } else { |
| 1053 | __ mov(ip, Operand(value)); |
| 1054 | frame_->EmitPush(ip); |
| 1055 | frame_->EmitPush(r0); |
| 1056 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1057 | } |
| 1058 | } |
| 1059 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1060 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1064 | void CodeGenerator::Comparison(Condition cc, |
| 1065 | Expression* left, |
| 1066 | Expression* right, |
| 1067 | bool strict) { |
| 1068 | if (left != NULL) LoadAndSpill(left); |
| 1069 | if (right != NULL) LoadAndSpill(right); |
| 1070 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1071 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1072 | // sp[0] : y |
| 1073 | // sp[1] : x |
| 1074 | // result : cc register |
| 1075 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1076 | // Strict only makes sense for equality comparisons. |
| 1077 | ASSERT(!strict || cc == eq); |
| 1078 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1079 | JumpTarget exit; |
| 1080 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1081 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1082 | if (cc == gt || cc == le) { |
| 1083 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1084 | frame_->EmitPop(r1); |
| 1085 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1086 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1087 | frame_->EmitPop(r0); |
| 1088 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1089 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1090 | __ orr(r2, r0, Operand(r1)); |
| 1091 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1092 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1093 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1094 | // Perform non-smi comparison by stub. |
| 1095 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1096 | // We call with 0 args because there are 0 on the stack. |
| 1097 | CompareStub stub(cc, strict); |
| 1098 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1099 | __ cmp(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1100 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1101 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1102 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1103 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1104 | __ cmp(r1, Operand(r0)); |
| 1105 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1106 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1107 | cc_reg_ = cc; |
| 1108 | } |
| 1109 | |
| 1110 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1111 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1112 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1113 | CallFunctionFlags flags, |
| 1114 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1115 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1116 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1117 | int arg_count = args->length(); |
| 1118 | for (int i = 0; i < arg_count; i++) { |
| 1119 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1120 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1121 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1122 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1123 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1124 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1125 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1126 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1127 | CallFunctionStub call_function(arg_count, in_loop, flags); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1128 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1129 | |
| 1130 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1131 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1132 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1136 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1137 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1138 | ASSERT(has_cc()); |
| 1139 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1140 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1141 | cc_reg_ = al; |
| 1142 | } |
| 1143 | |
| 1144 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1145 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1146 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1147 | Comment cmnt(masm_, "[ check stack"); |
| 1148 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1149 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1150 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1151 | // gives a return address 12 bytes down. |
| 1152 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1153 | masm_->cmp(sp, Operand(ip)); |
| 1154 | StackCheckStub stub; |
| 1155 | // Call the stub if lower. |
| 1156 | masm_->mov(pc, |
| 1157 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1158 | RelocInfo::CODE_TARGET), |
| 1159 | LeaveCC, |
| 1160 | lo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1164 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1165 | #ifdef DEBUG |
| 1166 | int original_height = frame_->height(); |
| 1167 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1168 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1169 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1170 | VisitAndSpill(statements->at(i)); |
| 1171 | } |
| 1172 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1173 | } |
| 1174 | |
| 1175 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1176 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1177 | #ifdef DEBUG |
| 1178 | int original_height = frame_->height(); |
| 1179 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1180 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1181 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1182 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1183 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1184 | VisitStatementsAndSpill(node->statements()); |
| 1185 | if (node->break_target()->is_linked()) { |
| 1186 | node->break_target()->Bind(); |
| 1187 | } |
| 1188 | node->break_target()->Unuse(); |
| 1189 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1193 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1194 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1195 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1196 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1197 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1198 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1199 | frame_->EmitPush(r0); |
| 1200 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1201 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1205 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1206 | #ifdef DEBUG |
| 1207 | int original_height = frame_->height(); |
| 1208 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1209 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1210 | Comment cmnt(masm_, "[ Declaration"); |
| 1211 | Variable* var = node->proxy()->var(); |
| 1212 | ASSERT(var != NULL); // must have been resolved |
| 1213 | Slot* slot = var->slot(); |
| 1214 | |
| 1215 | // If it was not possible to allocate the variable at compile time, |
| 1216 | // we need to "declare" it at runtime to make sure it actually |
| 1217 | // exists in the local context. |
| 1218 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1219 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1220 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1221 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1222 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1223 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1224 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1225 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1226 | // Declaration nodes are always declared in only two modes. |
| 1227 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1228 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1229 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1230 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1231 | // Push initial value, if any. |
| 1232 | // Note: For variables we must not push an initial value (such as |
| 1233 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1234 | // must not destroy the current value. |
| 1235 | if (node->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1236 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1237 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1238 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1239 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1240 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1241 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1242 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1243 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1244 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1245 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1246 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | ASSERT(!var->is_global()); |
| 1251 | |
| 1252 | // If we have a function or a constant, we need to initialize the variable. |
| 1253 | Expression* val = NULL; |
| 1254 | if (node->mode() == Variable::CONST) { |
| 1255 | val = new Literal(Factory::the_hole_value()); |
| 1256 | } else { |
| 1257 | val = node->fun(); // NULL if we don't have a function |
| 1258 | } |
| 1259 | |
| 1260 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1261 | { |
| 1262 | // Set initial value. |
| 1263 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1264 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1265 | target.SetValue(NOT_CONST_INIT); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1266 | } |
| 1267 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1268 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1269 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1270 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1274 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1275 | #ifdef DEBUG |
| 1276 | int original_height = frame_->height(); |
| 1277 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1278 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1279 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1280 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1281 | Expression* expression = node->expression(); |
| 1282 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1283 | LoadAndSpill(expression); |
| 1284 | frame_->Drop(); |
| 1285 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1289 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1290 | #ifdef DEBUG |
| 1291 | int original_height = frame_->height(); |
| 1292 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1293 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1294 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1295 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1296 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1297 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1301 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1302 | #ifdef DEBUG |
| 1303 | int original_height = frame_->height(); |
| 1304 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1305 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1306 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1307 | // Generate different code depending on which parts of the if statement |
| 1308 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1309 | bool has_then_stm = node->HasThenStatement(); |
| 1310 | bool has_else_stm = node->HasElseStatement(); |
| 1311 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1312 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1313 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1314 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1315 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1316 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1317 | JumpTarget then; |
| 1318 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1319 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1320 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1321 | if (frame_ != NULL) { |
| 1322 | Branch(false, &else_); |
| 1323 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1324 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1325 | if (frame_ != NULL || then.is_linked()) { |
| 1326 | then.Bind(); |
| 1327 | VisitAndSpill(node->then_statement()); |
| 1328 | } |
| 1329 | if (frame_ != NULL) { |
| 1330 | exit.Jump(); |
| 1331 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1332 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1333 | if (else_.is_linked()) { |
| 1334 | else_.Bind(); |
| 1335 | VisitAndSpill(node->else_statement()); |
| 1336 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1337 | |
| 1338 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1339 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1340 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1341 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1342 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1343 | LoadConditionAndSpill(node->condition(), &then, &exit, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1344 | if (frame_ != NULL) { |
| 1345 | Branch(false, &exit); |
| 1346 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1347 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1348 | if (frame_ != NULL || then.is_linked()) { |
| 1349 | then.Bind(); |
| 1350 | VisitAndSpill(node->then_statement()); |
| 1351 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1352 | |
| 1353 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1354 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1355 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1356 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1357 | // if (!cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1358 | LoadConditionAndSpill(node->condition(), &exit, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1359 | if (frame_ != NULL) { |
| 1360 | Branch(true, &exit); |
| 1361 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1362 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1363 | if (frame_ != NULL || else_.is_linked()) { |
| 1364 | else_.Bind(); |
| 1365 | VisitAndSpill(node->else_statement()); |
| 1366 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1367 | |
| 1368 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1369 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1370 | ASSERT(!has_then_stm && !has_else_stm); |
| 1371 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1372 | LoadConditionAndSpill(node->condition(), &exit, &exit, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1373 | if (frame_ != NULL) { |
| 1374 | if (has_cc()) { |
| 1375 | cc_reg_ = al; |
| 1376 | } else { |
| 1377 | frame_->Drop(); |
| 1378 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1383 | if (exit.is_linked()) { |
| 1384 | exit.Bind(); |
| 1385 | } |
| 1386 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1390 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1391 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1393 | CodeForStatementPosition(node); |
| 1394 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1398 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1399 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1400 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1401 | CodeForStatementPosition(node); |
| 1402 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1406 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1407 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1408 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1409 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1410 | CodeForStatementPosition(node); |
| 1411 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1412 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1413 | frame_->EmitPop(r0); |
| 1414 | function_return_.Jump(); |
| 1415 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1416 | // Pop the result from the frame and prepare the frame for |
| 1417 | // returning thus making it easier to merge. |
| 1418 | frame_->EmitPop(r0); |
| 1419 | frame_->PrepareForReturn(); |
| 1420 | |
| 1421 | function_return_.Jump(); |
| 1422 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1426 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1427 | #ifdef DEBUG |
| 1428 | int original_height = frame_->height(); |
| 1429 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1430 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1431 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1432 | CodeForStatementPosition(node); |
| 1433 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1434 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1435 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1436 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1437 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1438 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1439 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1440 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1441 | __ cmp(r0, Operand(cp)); |
| 1442 | verified_true.Branch(eq); |
| 1443 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1444 | verified_true.Bind(); |
| 1445 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1446 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1447 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1448 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1452 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1453 | #ifdef DEBUG |
| 1454 | int original_height = frame_->height(); |
| 1455 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1456 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1457 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1458 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1459 | // Pop context. |
| 1460 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1461 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1462 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1463 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1467 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1468 | #ifdef DEBUG |
| 1469 | int original_height = frame_->height(); |
| 1470 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1471 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1472 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1473 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1474 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1475 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1476 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1477 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1478 | JumpTarget next_test; |
| 1479 | JumpTarget fall_through; |
| 1480 | JumpTarget default_entry; |
| 1481 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1482 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1483 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1484 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1485 | |
| 1486 | for (int i = 0; i < length; i++) { |
| 1487 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1488 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1489 | // Remember the default clause and compile it at the end. |
| 1490 | default_clause = clause; |
| 1491 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1494 | Comment cmnt(masm_, "[ Case clause"); |
| 1495 | // Compile the test. |
| 1496 | next_test.Bind(); |
| 1497 | next_test.Unuse(); |
| 1498 | // Duplicate TOS. |
| 1499 | __ ldr(r0, frame_->Top()); |
| 1500 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1501 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1502 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1503 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1504 | // Before entering the body from the test, remove the switch value from |
| 1505 | // the stack. |
| 1506 | frame_->Drop(); |
| 1507 | |
| 1508 | // Label the body so that fall through is enabled. |
| 1509 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1510 | default_exit.Bind(); |
| 1511 | } else { |
| 1512 | fall_through.Bind(); |
| 1513 | fall_through.Unuse(); |
| 1514 | } |
| 1515 | VisitStatementsAndSpill(clause->statements()); |
| 1516 | |
| 1517 | // If control flow can fall through from the body, jump to the next body |
| 1518 | // or the end of the statement. |
| 1519 | if (frame_ != NULL) { |
| 1520 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1521 | default_entry.Jump(); |
| 1522 | } else { |
| 1523 | fall_through.Jump(); |
| 1524 | } |
| 1525 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1528 | // The final "test" removes the switch value. |
| 1529 | next_test.Bind(); |
| 1530 | frame_->Drop(); |
| 1531 | |
| 1532 | // If there is a default clause, compile it. |
| 1533 | if (default_clause != NULL) { |
| 1534 | Comment cmnt(masm_, "[ Default clause"); |
| 1535 | default_entry.Bind(); |
| 1536 | VisitStatementsAndSpill(default_clause->statements()); |
| 1537 | // If control flow can fall out of the default and there is a case after |
| 1538 | // it, jup to that case's body. |
| 1539 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1540 | default_exit.Jump(); |
| 1541 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1542 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1543 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1544 | if (fall_through.is_linked()) { |
| 1545 | fall_through.Bind(); |
| 1546 | } |
| 1547 | |
| 1548 | if (node->break_target()->is_linked()) { |
| 1549 | node->break_target()->Bind(); |
| 1550 | } |
| 1551 | node->break_target()->Unuse(); |
| 1552 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1556 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1557 | #ifdef DEBUG |
| 1558 | int original_height = frame_->height(); |
| 1559 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1560 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1561 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1562 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1563 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1564 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1565 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1566 | // Label the top of the loop for the backward CFG edge. If the test |
| 1567 | // is always true we can use the continue target, and if the test is |
| 1568 | // always false there is no need. |
| 1569 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1570 | switch (info) { |
| 1571 | case ALWAYS_TRUE: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1572 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1573 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1574 | break; |
| 1575 | case ALWAYS_FALSE: |
| 1576 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1577 | break; |
| 1578 | case DONT_KNOW: |
| 1579 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1580 | body.Bind(); |
| 1581 | break; |
| 1582 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1583 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1584 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1585 | VisitAndSpill(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1586 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1587 | // Compile the test. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1588 | switch (info) { |
| 1589 | case ALWAYS_TRUE: |
| 1590 | // If control can fall off the end of the body, jump back to the |
| 1591 | // top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1592 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1593 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1594 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1595 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1596 | case ALWAYS_FALSE: |
| 1597 | // If we have a continue in the body, we only have to bind its |
| 1598 | // jump target. |
| 1599 | if (node->continue_target()->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1600 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1601 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1602 | break; |
| 1603 | case DONT_KNOW: |
| 1604 | // We have to compile the test expression if it can be reached by |
| 1605 | // control flow falling out of the body or via continue. |
| 1606 | if (node->continue_target()->is_linked()) { |
| 1607 | node->continue_target()->Bind(); |
| 1608 | } |
| 1609 | if (has_valid_frame()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1610 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 1611 | CodeForDoWhileConditionPosition(node); |
| 1612 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1613 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1614 | // A invalid frame here indicates that control did not |
| 1615 | // fall out of the test expression. |
| 1616 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1617 | } |
| 1618 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1619 | break; |
| 1620 | } |
| 1621 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1622 | if (node->break_target()->is_linked()) { |
| 1623 | node->break_target()->Bind(); |
| 1624 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1625 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 1630 | #ifdef DEBUG |
| 1631 | int original_height = frame_->height(); |
| 1632 | #endif |
| 1633 | VirtualFrame::SpilledScope spilled_scope; |
| 1634 | Comment cmnt(masm_, "[ WhileStatement"); |
| 1635 | CodeForStatementPosition(node); |
| 1636 | |
| 1637 | // If the test is never true and has no side effects there is no need |
| 1638 | // to compile the test or body. |
| 1639 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1640 | if (info == ALWAYS_FALSE) return; |
| 1641 | |
| 1642 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1643 | |
| 1644 | // Label the top of the loop with the continue target for the backward |
| 1645 | // CFG edge. |
| 1646 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1647 | node->continue_target()->Bind(); |
| 1648 | |
| 1649 | if (info == DONT_KNOW) { |
| 1650 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1651 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1652 | if (has_valid_frame()) { |
| 1653 | // A NULL frame indicates that control did not fall out of the |
| 1654 | // test expression. |
| 1655 | Branch(false, node->break_target()); |
| 1656 | } |
| 1657 | if (has_valid_frame() || body.is_linked()) { |
| 1658 | body.Bind(); |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | if (has_valid_frame()) { |
| 1663 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1664 | VisitAndSpill(node->body()); |
| 1665 | |
| 1666 | // If control flow can fall out of the body, jump back to the top. |
| 1667 | if (has_valid_frame()) { |
| 1668 | node->continue_target()->Jump(); |
| 1669 | } |
| 1670 | } |
| 1671 | if (node->break_target()->is_linked()) { |
| 1672 | node->break_target()->Bind(); |
| 1673 | } |
| 1674 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1675 | } |
| 1676 | |
| 1677 | |
| 1678 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 1679 | #ifdef DEBUG |
| 1680 | int original_height = frame_->height(); |
| 1681 | #endif |
| 1682 | VirtualFrame::SpilledScope spilled_scope; |
| 1683 | Comment cmnt(masm_, "[ ForStatement"); |
| 1684 | CodeForStatementPosition(node); |
| 1685 | if (node->init() != NULL) { |
| 1686 | VisitAndSpill(node->init()); |
| 1687 | } |
| 1688 | |
| 1689 | // If the test is never true there is no need to compile the test or |
| 1690 | // body. |
| 1691 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1692 | if (info == ALWAYS_FALSE) return; |
| 1693 | |
| 1694 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1695 | |
| 1696 | // If there is no update statement, label the top of the loop with the |
| 1697 | // continue target, otherwise with the loop target. |
| 1698 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1699 | if (node->next() == NULL) { |
| 1700 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1701 | node->continue_target()->Bind(); |
| 1702 | } else { |
| 1703 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1704 | loop.Bind(); |
| 1705 | } |
| 1706 | |
| 1707 | // If the test is always true, there is no need to compile it. |
| 1708 | if (info == DONT_KNOW) { |
| 1709 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1710 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1711 | if (has_valid_frame()) { |
| 1712 | Branch(false, node->break_target()); |
| 1713 | } |
| 1714 | if (has_valid_frame() || body.is_linked()) { |
| 1715 | body.Bind(); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | if (has_valid_frame()) { |
| 1720 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1721 | VisitAndSpill(node->body()); |
| 1722 | |
| 1723 | if (node->next() == NULL) { |
| 1724 | // If there is no update statement and control flow can fall out |
| 1725 | // of the loop, jump directly to the continue label. |
| 1726 | if (has_valid_frame()) { |
| 1727 | node->continue_target()->Jump(); |
| 1728 | } |
| 1729 | } else { |
| 1730 | // If there is an update statement and control flow can reach it |
| 1731 | // via falling out of the body of the loop or continuing, we |
| 1732 | // compile the update statement. |
| 1733 | if (node->continue_target()->is_linked()) { |
| 1734 | node->continue_target()->Bind(); |
| 1735 | } |
| 1736 | if (has_valid_frame()) { |
| 1737 | // Record source position of the statement as this code which is |
| 1738 | // after the code for the body actually belongs to the loop |
| 1739 | // statement and not the body. |
| 1740 | CodeForStatementPosition(node); |
| 1741 | VisitAndSpill(node->next()); |
| 1742 | loop.Jump(); |
| 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | if (node->break_target()->is_linked()) { |
| 1747 | node->break_target()->Bind(); |
| 1748 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1749 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1753 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1754 | #ifdef DEBUG |
| 1755 | int original_height = frame_->height(); |
| 1756 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1757 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1758 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1759 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1760 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1761 | JumpTarget primitive; |
| 1762 | JumpTarget jsobject; |
| 1763 | JumpTarget fixed_array; |
| 1764 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1765 | JumpTarget end_del_check; |
| 1766 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1767 | |
| 1768 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1769 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1772 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1773 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1774 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 1775 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1776 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1777 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1778 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1779 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1780 | |
| 1781 | // Stack layout in body: |
| 1782 | // [iteration counter (Smi)] |
| 1783 | // [length of array] |
| 1784 | // [FixedArray] |
| 1785 | // [Map or 0] |
| 1786 | // [Object] |
| 1787 | |
| 1788 | // Check if enumerable is already a JSObject |
| 1789 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1790 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1791 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1792 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1793 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1794 | primitive.Bind(); |
| 1795 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1796 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1797 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1798 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1799 | // Get the set of properties (as a FixedArray or Map). |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1800 | // r0: value to be iterated over |
| 1801 | frame_->EmitPush(r0); // Push the object being iterated over. |
| 1802 | |
| 1803 | // Check cache validity in generated code. This is a fast case for |
| 1804 | // the JSObject::IsSimpleEnum cache validity checks. If we cannot |
| 1805 | // guarantee cache validity, call the runtime system to check cache |
| 1806 | // validity or get the property names in a fixed array. |
| 1807 | JumpTarget call_runtime; |
| 1808 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1809 | JumpTarget check_prototype; |
| 1810 | JumpTarget use_cache; |
| 1811 | __ mov(r1, Operand(r0)); |
| 1812 | loop.Bind(); |
| 1813 | // Check that there are no elements. |
| 1814 | __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 1815 | __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex); |
| 1816 | __ cmp(r2, r4); |
| 1817 | call_runtime.Branch(ne); |
| 1818 | // Check that instance descriptors are not empty so that we can |
| 1819 | // check for an enum cache. Leave the map in r3 for the subsequent |
| 1820 | // prototype load. |
| 1821 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1822 | __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset)); |
| 1823 | __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex); |
| 1824 | __ cmp(r2, ip); |
| 1825 | call_runtime.Branch(eq); |
| 1826 | // Check that there in an enum cache in the non-empty instance |
| 1827 | // descriptors. This is the case if the next enumeration index |
| 1828 | // field does not contain a smi. |
| 1829 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset)); |
| 1830 | __ tst(r2, Operand(kSmiTagMask)); |
| 1831 | call_runtime.Branch(eq); |
| 1832 | // For all objects but the receiver, check that the cache is empty. |
| 1833 | // r4: empty fixed array root. |
| 1834 | __ cmp(r1, r0); |
| 1835 | check_prototype.Branch(eq); |
| 1836 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1837 | __ cmp(r2, r4); |
| 1838 | call_runtime.Branch(ne); |
| 1839 | check_prototype.Bind(); |
| 1840 | // Load the prototype from the map and loop if non-null. |
| 1841 | __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 1842 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1843 | __ cmp(r1, ip); |
| 1844 | loop.Branch(ne); |
| 1845 | // The enum cache is valid. Load the map of the object being |
| 1846 | // iterated over and use the cache for the iteration. |
| 1847 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1848 | use_cache.Jump(); |
| 1849 | |
| 1850 | call_runtime.Bind(); |
| 1851 | // Call the runtime to get the property names for the object. |
| 1852 | 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] | 1853 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1854 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1855 | // If we got a map from the runtime call, we can do a fast |
| 1856 | // modification check. Otherwise, we got a fixed array, and we have |
| 1857 | // to do a slow check. |
| 1858 | // r0: map or fixed array (result from call to |
| 1859 | // Runtime::kGetPropertyNamesFast) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1860 | __ mov(r2, Operand(r0)); |
| 1861 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1862 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 1863 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1864 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1865 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1866 | use_cache.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1867 | // Get enum cache |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1868 | // r0: map (either the result from a call to |
| 1869 | // Runtime::kGetPropertyNamesFast or has been fetched directly from |
| 1870 | // the object) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1871 | __ mov(r1, Operand(r0)); |
| 1872 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1873 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1874 | __ ldr(r2, |
| 1875 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1876 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1877 | frame_->EmitPush(r0); // map |
| 1878 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1879 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1880 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1881 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1882 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1883 | frame_->EmitPush(r0); |
| 1884 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1885 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1886 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1887 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1888 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1889 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1890 | |
| 1891 | // 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] | 1892 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1893 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1894 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1895 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1896 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1897 | |
| 1898 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1899 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1900 | // sp[0] : index |
| 1901 | // sp[1] : array/enum cache length |
| 1902 | // sp[2] : array or enum cache |
| 1903 | // sp[3] : 0 or map |
| 1904 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1905 | // Grab the current frame's height for the break and continue |
| 1906 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1907 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1908 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1909 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1910 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1911 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1912 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1913 | node->break_target()->Branch(hs); |
| 1914 | |
| 1915 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1916 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1917 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1918 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1919 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1920 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1921 | |
| 1922 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1923 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1924 | // Check if this (still) matches the map of the enumerable. |
| 1925 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1926 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1927 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1928 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1929 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1930 | |
| 1931 | // 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] | 1932 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1933 | frame_->EmitPush(r0); |
| 1934 | frame_->EmitPush(r3); // push entry |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1935 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1936 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1937 | |
| 1938 | // 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] | 1939 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1940 | __ cmp(r3, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1941 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1942 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1943 | end_del_check.Bind(); |
| 1944 | // Store the entry in the 'each' expression and take another spin in the |
| 1945 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1946 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1947 | { Reference each(this, node->each()); |
| 1948 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1949 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1950 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1951 | frame_->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1952 | each.SetValue(NOT_CONST_INIT); |
| 1953 | frame_->Drop(2); |
| 1954 | } else { |
| 1955 | // If the reference was to a slot we rely on the convenient property |
| 1956 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1957 | // right on top of or right underneath a zero-sized reference. |
| 1958 | each.SetValue(NOT_CONST_INIT); |
| 1959 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1960 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1961 | } |
| 1962 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1963 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1964 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1965 | VisitAndSpill(node->body()); |
| 1966 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1967 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1968 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1969 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1970 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1971 | frame_->EmitPop(r0); |
| 1972 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1973 | frame_->EmitPush(r0); |
| 1974 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1975 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1976 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1977 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1978 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1979 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1980 | |
| 1981 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1982 | exit.Bind(); |
| 1983 | node->continue_target()->Unuse(); |
| 1984 | node->break_target()->Unuse(); |
| 1985 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1989 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1990 | #ifdef DEBUG |
| 1991 | int original_height = frame_->height(); |
| 1992 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1993 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1994 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1995 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1996 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1997 | JumpTarget try_block; |
| 1998 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1999 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2000 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2001 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2002 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2003 | |
| 2004 | // Store the caught exception in the catch variable. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2005 | Variable* catch_var = node->catch_var()->var(); |
| 2006 | ASSERT(catch_var != NULL && catch_var->slot() != NULL); |
| 2007 | StoreToSlot(catch_var->slot(), NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2008 | |
| 2009 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2010 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2011 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2012 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2013 | if (frame_ != NULL) { |
| 2014 | exit.Jump(); |
| 2015 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2016 | |
| 2017 | |
| 2018 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2019 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2020 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2021 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2022 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2023 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2024 | // Shadow the labels for all escapes from the try block, including |
| 2025 | // returns. During shadowing, the original label is hidden as the |
| 2026 | // LabelShadow and operations on the original actually affect the |
| 2027 | // shadowing label. |
| 2028 | // |
| 2029 | // We should probably try to unify the escaping labels and the return |
| 2030 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2031 | int nof_escapes = node->escaping_targets()->length(); |
| 2032 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2033 | |
| 2034 | // Add the shadow target for the function return. |
| 2035 | static const int kReturnShadowIndex = 0; |
| 2036 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2037 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2038 | function_return_is_shadowed_ = true; |
| 2039 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2040 | |
| 2041 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2042 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2043 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2047 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2048 | |
| 2049 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2050 | // After shadowing stops, the original labels are unshadowed and the |
| 2051 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2052 | bool has_unlinks = false; |
| 2053 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2054 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2055 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2056 | } |
| 2057 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2058 | |
| 2059 | // Get an external reference to the handler address. |
| 2060 | ExternalReference handler_address(Top::k_handler_address); |
| 2061 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2062 | // If we can fall off the end of the try block, unlink from try chain. |
| 2063 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2064 | // The next handler address is on top of the frame. Unlink from |
| 2065 | // the handler list and drop the rest of this handler from the |
| 2066 | // frame. |
| 2067 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2068 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2069 | __ mov(r3, Operand(handler_address)); |
| 2070 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2071 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2072 | if (has_unlinks) { |
| 2073 | exit.Jump(); |
| 2074 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2077 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2078 | // jumped to. Deallocate each shadow target. |
| 2079 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2080 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2081 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2082 | shadows[i]->Bind(); |
| 2083 | // Because we can be jumping here (to spilled code) from unspilled |
| 2084 | // code, we need to reestablish a spilled frame at this block. |
| 2085 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2086 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2087 | // Reload sp from the top handler, because some statements that we |
| 2088 | // 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] | 2089 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2090 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2091 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2093 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2094 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2095 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2096 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2097 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2098 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2099 | frame_->PrepareForReturn(); |
| 2100 | } |
| 2101 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2105 | exit.Bind(); |
| 2106 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2110 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2111 | #ifdef DEBUG |
| 2112 | int original_height = frame_->height(); |
| 2113 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2114 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2115 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2116 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2117 | |
| 2118 | // State: Used to keep track of reason for entering the finally |
| 2119 | // block. Should probably be extended to hold information for |
| 2120 | // break/continue from within the try block. |
| 2121 | enum { FALLING, THROWING, JUMPING }; |
| 2122 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2123 | JumpTarget try_block; |
| 2124 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2125 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2126 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2127 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2128 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2129 | // In case of thrown exceptions, this is where we continue. |
| 2130 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2131 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2132 | |
| 2133 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2134 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2135 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2136 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2137 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2138 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2139 | // Shadow the labels for all escapes from the try block, including |
| 2140 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2141 | // operations on the original actually affect the shadowing label. |
| 2142 | // |
| 2143 | // We should probably try to unify the escaping labels and the return |
| 2144 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2145 | int nof_escapes = node->escaping_targets()->length(); |
| 2146 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2147 | |
| 2148 | // Add the shadow target for the function return. |
| 2149 | static const int kReturnShadowIndex = 0; |
| 2150 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2151 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2152 | function_return_is_shadowed_ = true; |
| 2153 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2154 | |
| 2155 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2156 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2157 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2161 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2162 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2163 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2164 | // After shadowing stops, the original labels are unshadowed and the |
| 2165 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2166 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2167 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2168 | shadows[i]->StopShadowing(); |
| 2169 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2170 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2171 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2172 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2173 | // Get an external reference to the handler address. |
| 2174 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2175 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2176 | // If we can fall off the end of the try block, unlink from the try |
| 2177 | // chain and set the state on the frame to FALLING. |
| 2178 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2179 | // The next handler address is on top of the frame. |
| 2180 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2181 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2182 | __ mov(r3, Operand(handler_address)); |
| 2183 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2184 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2185 | |
| 2186 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2187 | // state in r2, then jump around the unlink blocks if any. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2188 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2189 | frame_->EmitPush(r0); |
| 2190 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2191 | if (nof_unlinks > 0) { |
| 2192 | finally_block.Jump(); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | // Generate code to unlink and set the state for the (formerly) |
| 2197 | // shadowing targets that have been jumped to. |
| 2198 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2199 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2200 | // If we have come from the shadowed return, the return value is |
| 2201 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2202 | // until it is pushed. |
| 2203 | // |
| 2204 | // Because we can be jumping here (to spilled code) from |
| 2205 | // unspilled code, we need to reestablish a spilled frame at |
| 2206 | // this block. |
| 2207 | shadows[i]->Bind(); |
| 2208 | frame_->SpillAll(); |
| 2209 | |
| 2210 | // Reload sp from the top handler, because some statements that |
| 2211 | // we break from (eg, for...in) may have left stuff on the |
| 2212 | // stack. |
| 2213 | __ mov(r3, Operand(handler_address)); |
| 2214 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2215 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2216 | |
| 2217 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2218 | // handler address is currently on top of the frame. |
| 2219 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2220 | frame_->EmitPop(r1); |
| 2221 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2222 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2223 | |
| 2224 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2225 | // If this label shadowed the function return, materialize the |
| 2226 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2227 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2228 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2229 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2230 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2231 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2232 | } |
| 2233 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2234 | if (--nof_unlinks > 0) { |
| 2235 | // If this is not the last unlink block, jump around the next. |
| 2236 | finally_block.Jump(); |
| 2237 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2241 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2242 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2243 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2244 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2245 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2246 | |
| 2247 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2248 | // and the state - while evaluating the finally block. |
| 2249 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2250 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2251 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2252 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2253 | if (has_valid_frame()) { |
| 2254 | // Restore state and return value or faked TOS. |
| 2255 | frame_->EmitPop(r2); |
| 2256 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2259 | // Generate code to jump to the right destination for all used |
| 2260 | // formerly shadowing targets. Deallocate each shadow target. |
| 2261 | for (int i = 0; i < shadows.length(); i++) { |
| 2262 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2263 | JumpTarget* original = shadows[i]->other_target(); |
| 2264 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2265 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2266 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2267 | skip.Branch(ne); |
| 2268 | frame_->PrepareForReturn(); |
| 2269 | original->Jump(); |
| 2270 | skip.Bind(); |
| 2271 | } else { |
| 2272 | original->Branch(eq); |
| 2273 | } |
| 2274 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2275 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2276 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2277 | if (has_valid_frame()) { |
| 2278 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2279 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2280 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2281 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2282 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2283 | // Rethrow exception. |
| 2284 | frame_->EmitPush(r0); |
| 2285 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2286 | |
| 2287 | // Done. |
| 2288 | exit.Bind(); |
| 2289 | } |
| 2290 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2294 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2295 | #ifdef DEBUG |
| 2296 | int original_height = frame_->height(); |
| 2297 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2298 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2299 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2300 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2301 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2302 | frame_->DebugBreak(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2303 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2304 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2305 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2309 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2310 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2311 | ASSERT(boilerplate->IsBoilerplate()); |
| 2312 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2313 | __ mov(r0, Operand(boilerplate)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2314 | // Use the fast case closure allocation code that allocates in new |
| 2315 | // space for nested functions that don't need literals cloning. |
| 2316 | if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) { |
| 2317 | FastNewClosureStub stub; |
| 2318 | frame_->EmitPush(r0); |
| 2319 | frame_->CallStub(&stub, 1); |
| 2320 | frame_->EmitPush(r0); |
| 2321 | } else { |
| 2322 | // Create a new closure. |
| 2323 | frame_->EmitPush(cp); |
| 2324 | frame_->EmitPush(r0); |
| 2325 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2326 | frame_->EmitPush(r0); |
| 2327 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2331 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2332 | #ifdef DEBUG |
| 2333 | int original_height = frame_->height(); |
| 2334 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2335 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2336 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2337 | |
| 2338 | // Build the function boilerplate and instantiate it. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2339 | Handle<JSFunction> boilerplate = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2340 | Compiler::BuildBoilerplate(node, script(), this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2341 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2342 | if (HasStackOverflow()) { |
| 2343 | ASSERT(frame_->height() == original_height); |
| 2344 | return; |
| 2345 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2346 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2347 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2351 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2352 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2353 | #ifdef DEBUG |
| 2354 | int original_height = frame_->height(); |
| 2355 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2356 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2357 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2358 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2359 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2363 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2364 | #ifdef DEBUG |
| 2365 | int original_height = frame_->height(); |
| 2366 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2367 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2368 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2369 | JumpTarget then; |
| 2370 | JumpTarget else_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2371 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2372 | if (has_valid_frame()) { |
| 2373 | Branch(false, &else_); |
| 2374 | } |
| 2375 | if (has_valid_frame() || then.is_linked()) { |
| 2376 | then.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2377 | LoadAndSpill(node->then_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2378 | } |
| 2379 | if (else_.is_linked()) { |
| 2380 | JumpTarget exit; |
| 2381 | if (has_valid_frame()) exit.Jump(); |
| 2382 | else_.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2383 | LoadAndSpill(node->else_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2384 | if (exit.is_linked()) exit.Bind(); |
| 2385 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2386 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2390 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2391 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2392 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2393 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2394 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2395 | JumpTarget slow; |
| 2396 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2397 | |
| 2398 | // Generate fast-case code for variables that might be shadowed by |
| 2399 | // eval-introduced variables. Eval is used a lot without |
| 2400 | // introducing variables. In those cases, we do not want to |
| 2401 | // perform a runtime call for all variables in the scope |
| 2402 | // containing the eval. |
| 2403 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2404 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2405 | // If there was no control flow to slow, we can exit early. |
| 2406 | if (!slow.is_linked()) { |
| 2407 | frame_->EmitPush(r0); |
| 2408 | return; |
| 2409 | } |
| 2410 | |
| 2411 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2412 | |
| 2413 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2414 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2415 | // Only generate the fast case for locals that rewrite to slots. |
| 2416 | // This rules out argument loads. |
| 2417 | if (potential_slot != NULL) { |
| 2418 | __ ldr(r0, |
| 2419 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2420 | r1, |
| 2421 | r2, |
| 2422 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2423 | if (potential_slot->var()->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2424 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2425 | __ cmp(r0, ip); |
| 2426 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2427 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2428 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2429 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2430 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2431 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2435 | slow.Bind(); |
| 2436 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2437 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2438 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2439 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2440 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2441 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2442 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2443 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2444 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2445 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2446 | done.Bind(); |
| 2447 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2448 | |
| 2449 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2450 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2451 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2452 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2453 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2454 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2455 | // initialized yet) which needs to be converted into the 'undefined' |
| 2456 | // value. |
| 2457 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2458 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2459 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2460 | __ cmp(r0, ip); |
| 2461 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2462 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2463 | } |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2468 | void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) { |
| 2469 | ASSERT(slot != NULL); |
| 2470 | if (slot->type() == Slot::LOOKUP) { |
| 2471 | ASSERT(slot->var()->is_dynamic()); |
| 2472 | |
| 2473 | // For now, just do a runtime call. |
| 2474 | frame_->EmitPush(cp); |
| 2475 | __ mov(r0, Operand(slot->var()->name())); |
| 2476 | frame_->EmitPush(r0); |
| 2477 | |
| 2478 | if (init_state == CONST_INIT) { |
| 2479 | // Same as the case for a normal store, but ignores attribute |
| 2480 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 2481 | // const properties (introduced via eval("const foo = (some |
| 2482 | // expr);")). Also, uses the current function context instead of |
| 2483 | // the top context. |
| 2484 | // |
| 2485 | // Note that we must declare the foo upon entry of eval(), via a |
| 2486 | // context slot declaration, but we cannot initialize it at the |
| 2487 | // same time, because the const declaration may be at the end of |
| 2488 | // the eval code (sigh...) and the const variable may have been |
| 2489 | // used before (where its value is 'undefined'). Thus, we can only |
| 2490 | // do the initialization when we actually encounter the expression |
| 2491 | // and when the expression operands are defined and valid, and |
| 2492 | // thus we need the split into 2 operations: declaration of the |
| 2493 | // context slot followed by initialization. |
| 2494 | frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 2495 | } else { |
| 2496 | frame_->CallRuntime(Runtime::kStoreContextSlot, 3); |
| 2497 | } |
| 2498 | // Storing a variable must keep the (new) value on the expression |
| 2499 | // stack. This is necessary for compiling assignment expressions. |
| 2500 | frame_->EmitPush(r0); |
| 2501 | |
| 2502 | } else { |
| 2503 | ASSERT(!slot->var()->is_dynamic()); |
| 2504 | |
| 2505 | JumpTarget exit; |
| 2506 | if (init_state == CONST_INIT) { |
| 2507 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 2508 | // Only the first const initialization must be executed (the slot |
| 2509 | // still contains 'the hole' value). When the assignment is |
| 2510 | // executed, the code is identical to a normal store (see below). |
| 2511 | Comment cmnt(masm_, "[ Init const"); |
| 2512 | __ ldr(r2, SlotOperand(slot, r2)); |
| 2513 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2514 | __ cmp(r2, ip); |
| 2515 | exit.Branch(ne); |
| 2516 | } |
| 2517 | |
| 2518 | // We must execute the store. Storing a variable must keep the |
| 2519 | // (new) value on the stack. This is necessary for compiling |
| 2520 | // assignment expressions. |
| 2521 | // |
| 2522 | // Note: We will reach here even with slot->var()->mode() == |
| 2523 | // Variable::CONST because of const declarations which will |
| 2524 | // initialize consts to 'the hole' value and by doing so, end up |
| 2525 | // calling this code. r2 may be loaded with context; used below in |
| 2526 | // RecordWrite. |
| 2527 | frame_->EmitPop(r0); |
| 2528 | __ str(r0, SlotOperand(slot, r2)); |
| 2529 | frame_->EmitPush(r0); |
| 2530 | if (slot->type() == Slot::CONTEXT) { |
| 2531 | // Skip write barrier if the written value is a smi. |
| 2532 | __ tst(r0, Operand(kSmiTagMask)); |
| 2533 | exit.Branch(eq); |
| 2534 | // r2 is loaded with context when calling SlotOperand above. |
| 2535 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 2536 | __ mov(r3, Operand(offset)); |
| 2537 | __ RecordWrite(r2, r3, r1); |
| 2538 | } |
| 2539 | // If we definitely did not jump over the assignment, we do not need |
| 2540 | // to bind the exit label. Doing so can defeat peephole |
| 2541 | // optimization. |
| 2542 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
| 2543 | exit.Bind(); |
| 2544 | } |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2549 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2550 | TypeofState typeof_state, |
| 2551 | Register tmp, |
| 2552 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2553 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2554 | // Check that no extension objects have been created by calls to |
| 2555 | // eval from the current scope to the global scope. |
| 2556 | Register context = cp; |
| 2557 | Scope* s = scope(); |
| 2558 | while (s != NULL) { |
| 2559 | if (s->num_heap_slots() > 0) { |
| 2560 | if (s->calls_eval()) { |
| 2561 | // Check that extension is NULL. |
| 2562 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2563 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2564 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2565 | } |
| 2566 | // Load next context in chain. |
| 2567 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2568 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2569 | context = tmp; |
| 2570 | } |
| 2571 | // If no outer scope calls eval, we do not need to check more |
| 2572 | // context extensions. |
| 2573 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2574 | s = s->outer_scope(); |
| 2575 | } |
| 2576 | |
| 2577 | if (s->is_eval_scope()) { |
| 2578 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2579 | if (!context.is(tmp)) { |
| 2580 | __ mov(tmp, Operand(context)); |
| 2581 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2582 | __ bind(&next); |
| 2583 | // Terminate at global context. |
| 2584 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2585 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2586 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2587 | __ b(eq, &fast); |
| 2588 | // Check that extension is NULL. |
| 2589 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2590 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2591 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2592 | // Load next context in chain. |
| 2593 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2594 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2595 | __ b(&next); |
| 2596 | __ bind(&fast); |
| 2597 | } |
| 2598 | |
| 2599 | // All extension objects were empty and it is safe to use a global |
| 2600 | // load IC call. |
| 2601 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2602 | // Load the global object. |
| 2603 | LoadGlobal(); |
| 2604 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2605 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2606 | // Call IC stub. |
| 2607 | if (typeof_state == INSIDE_TYPEOF) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2608 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2609 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2610 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2613 | // Drop the global object. The result is in r0. |
| 2614 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2618 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2619 | #ifdef DEBUG |
| 2620 | int original_height = frame_->height(); |
| 2621 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2622 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2623 | Comment cmnt(masm_, "[ Slot"); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2624 | LoadFromSlot(node, NOT_INSIDE_TYPEOF); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2625 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2626 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2627 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2628 | |
| 2629 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2630 | #ifdef DEBUG |
| 2631 | int original_height = frame_->height(); |
| 2632 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2633 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2634 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2635 | |
| 2636 | Variable* var = node->var(); |
| 2637 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2638 | if (expr != NULL) { |
| 2639 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2640 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2641 | ASSERT(var->is_global()); |
| 2642 | Reference ref(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2643 | ref.GetValueAndSpill(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2644 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2645 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2649 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2650 | #ifdef DEBUG |
| 2651 | int original_height = frame_->height(); |
| 2652 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2653 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2654 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2655 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2656 | frame_->EmitPush(r0); |
| 2657 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2661 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2662 | #ifdef DEBUG |
| 2663 | int original_height = frame_->height(); |
| 2664 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2665 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2666 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2667 | |
| 2668 | // Retrieve the literal array and check the allocated entry. |
| 2669 | |
| 2670 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2671 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2672 | |
| 2673 | // Load the literals array of the function. |
| 2674 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2675 | |
| 2676 | // Load the literal at the ast saved index. |
| 2677 | int literal_offset = |
| 2678 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2679 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2680 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2681 | JumpTarget done; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2682 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2683 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2684 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2685 | |
| 2686 | // If the entry is undefined we call the runtime system to computed |
| 2687 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2688 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2689 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2690 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2691 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2692 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2693 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2694 | frame_->EmitPush(r0); |
| 2695 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2696 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2697 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2698 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2699 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2700 | frame_->EmitPush(r2); |
| 2701 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2705 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2706 | #ifdef DEBUG |
| 2707 | int original_height = frame_->height(); |
| 2708 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2709 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2710 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2711 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2712 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2713 | __ ldr(r2, frame_->Function()); |
| 2714 | // Literal array. |
| 2715 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
| 2716 | // Literal index. |
| 2717 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
| 2718 | // Constant properties. |
| 2719 | __ mov(r0, Operand(node->constant_properties())); |
| 2720 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
| 2721 | if (node->depth() > 1) { |
| 2722 | frame_->CallRuntime(Runtime::kCreateObjectLiteral, 3); |
| 2723 | } else { |
| 2724 | frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2725 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2726 | frame_->EmitPush(r0); // save the result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2727 | for (int i = 0; i < node->properties()->length(); i++) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2728 | // At the start of each iteration, the top of stack contains |
| 2729 | // the newly created object literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2730 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2731 | Literal* key = property->key(); |
| 2732 | Expression* value = property->value(); |
| 2733 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2734 | case ObjectLiteral::Property::CONSTANT: |
| 2735 | break; |
| 2736 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2737 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2738 | // else fall through |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2739 | case ObjectLiteral::Property::COMPUTED: |
| 2740 | if (key->handle()->IsSymbol()) { |
| 2741 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 2742 | LoadAndSpill(value); |
| 2743 | frame_->EmitPop(r0); |
| 2744 | __ mov(r2, Operand(key->handle())); |
| 2745 | __ ldr(r1, frame_->Top()); // Load the receiver. |
| 2746 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
| 2747 | break; |
| 2748 | } |
| 2749 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2750 | case ObjectLiteral::Property::PROTOTYPE: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2751 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2752 | frame_->EmitPush(r0); // dup the result |
| 2753 | LoadAndSpill(key); |
| 2754 | LoadAndSpill(value); |
| 2755 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2756 | break; |
| 2757 | } |
| 2758 | case ObjectLiteral::Property::SETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2759 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2760 | frame_->EmitPush(r0); |
| 2761 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2762 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2763 | frame_->EmitPush(r0); |
| 2764 | LoadAndSpill(value); |
| 2765 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2766 | break; |
| 2767 | } |
| 2768 | case ObjectLiteral::Property::GETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2769 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2770 | frame_->EmitPush(r0); |
| 2771 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2772 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2773 | frame_->EmitPush(r0); |
| 2774 | LoadAndSpill(value); |
| 2775 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2776 | break; |
| 2777 | } |
| 2778 | } |
| 2779 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2780 | ASSERT(frame_->height() == original_height + 1); |
| 2781 | } |
| 2782 | |
| 2783 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2784 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2785 | #ifdef DEBUG |
| 2786 | int original_height = frame_->height(); |
| 2787 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2788 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2789 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2790 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2791 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2792 | __ ldr(r2, frame_->Function()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2793 | // Load the literals array of the function. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2794 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2795 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2796 | __ mov(r0, Operand(node->constant_elements())); |
| 2797 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2798 | int length = node->values()->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2799 | if (node->depth() > 1) { |
| 2800 | frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2801 | } else if (length > FastCloneShallowArrayStub::kMaximumLength) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2802 | frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2803 | } else { |
| 2804 | FastCloneShallowArrayStub stub(length); |
| 2805 | frame_->CallStub(&stub, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2806 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2807 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2808 | // r0: created object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2809 | |
| 2810 | // Generate code to set the elements in the array that are not |
| 2811 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2812 | for (int i = 0; i < node->values()->length(); i++) { |
| 2813 | Expression* value = node->values()->at(i); |
| 2814 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2815 | // If value is a literal the property value is already set in the |
| 2816 | // boilerplate object. |
| 2817 | if (value->AsLiteral() != NULL) continue; |
| 2818 | // If value is a materialized literal the property value is already set |
| 2819 | // in the boilerplate object if it is simple. |
| 2820 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2821 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2822 | // The property must be set by generated code. |
| 2823 | LoadAndSpill(value); |
| 2824 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2825 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2826 | // Fetch the object literal. |
| 2827 | __ ldr(r1, frame_->Top()); |
| 2828 | // Get the elements array. |
| 2829 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2830 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2831 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2832 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2833 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2834 | |
| 2835 | // Update the write barrier for the array address. |
| 2836 | __ mov(r3, Operand(offset)); |
| 2837 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2838 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2839 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2843 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2844 | #ifdef DEBUG |
| 2845 | int original_height = frame_->height(); |
| 2846 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2847 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2848 | // Call runtime routine to allocate the catch extension object and |
| 2849 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2850 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2851 | LoadAndSpill(node->key()); |
| 2852 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2853 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2854 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2855 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2859 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2860 | #ifdef DEBUG |
| 2861 | int original_height = frame_->height(); |
| 2862 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2863 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2864 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2865 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2866 | { Reference target(this, node->target(), node->is_compound()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2867 | if (target.is_illegal()) { |
| 2868 | // Fool the virtual frame into thinking that we left the assignment's |
| 2869 | // value on the frame. |
| 2870 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2871 | frame_->EmitPush(r0); |
| 2872 | ASSERT(frame_->height() == original_height + 1); |
| 2873 | return; |
| 2874 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2875 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2876 | if (node->op() == Token::ASSIGN || |
| 2877 | node->op() == Token::INIT_VAR || |
| 2878 | node->op() == Token::INIT_CONST) { |
| 2879 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2880 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2881 | } else { // Assignment is a compound assignment. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2882 | // Get the old value of the lhs. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2883 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2884 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2885 | bool overwrite = |
| 2886 | (node->value()->AsBinaryOperation() != NULL && |
| 2887 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2888 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2889 | SmiOperation(node->binary_op(), |
| 2890 | literal->handle(), |
| 2891 | false, |
| 2892 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2893 | frame_->EmitPush(r0); |
| 2894 | |
| 2895 | } else { |
| 2896 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2897 | GenericBinaryOperation(node->binary_op(), |
| 2898 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2899 | frame_->EmitPush(r0); |
| 2900 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2901 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2902 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2903 | if (var != NULL && |
| 2904 | (var->mode() == Variable::CONST) && |
| 2905 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2906 | // Assignment ignored - leave the value on the stack. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2907 | UnloadReference(&target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2908 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2909 | CodeForSourcePosition(node->position()); |
| 2910 | if (node->op() == Token::INIT_CONST) { |
| 2911 | // Dynamic constant initializations must use the function context |
| 2912 | // and initialize the actual constant declared. Dynamic variable |
| 2913 | // initializations are simply assignments and use SetValue. |
| 2914 | target.SetValue(CONST_INIT); |
| 2915 | } else { |
| 2916 | target.SetValue(NOT_CONST_INIT); |
| 2917 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2918 | } |
| 2919 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2920 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2924 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2925 | #ifdef DEBUG |
| 2926 | int original_height = frame_->height(); |
| 2927 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2928 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2929 | Comment cmnt(masm_, "[ Throw"); |
| 2930 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2931 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2932 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2933 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2934 | frame_->EmitPush(r0); |
| 2935 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2939 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2940 | #ifdef DEBUG |
| 2941 | int original_height = frame_->height(); |
| 2942 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2943 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2944 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2945 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2946 | { Reference property(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2947 | property.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2948 | } |
| 2949 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2953 | void CodeGenerator::VisitCall(Call* 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 |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2957 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2958 | Comment cmnt(masm_, "[ Call"); |
| 2959 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2960 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2961 | ZoneList<Expression*>* args = node->arguments(); |
| 2962 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2963 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2964 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2965 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2966 | Property* property = function->AsProperty(); |
| 2967 | |
| 2968 | // ------------------------------------------------------------------------ |
| 2969 | // Fast-case: Use inline caching. |
| 2970 | // --- |
| 2971 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2972 | // must be resolved after the arguments have been evaluated. The IC code |
| 2973 | // automatically handles this by loading the arguments before the function |
| 2974 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2975 | // ------------------------------------------------------------------------ |
| 2976 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2977 | if (var != NULL && var->is_possibly_eval()) { |
| 2978 | // ---------------------------------- |
| 2979 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 2980 | // ---------------------------------- |
| 2981 | |
| 2982 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 2983 | // resolve the function we need to call and the receiver of the |
| 2984 | // call. Then we call the resolved function using the given |
| 2985 | // arguments. |
| 2986 | // Prepare stack for call to resolved function. |
| 2987 | LoadAndSpill(function); |
| 2988 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 2989 | frame_->EmitPush(r2); // Slot for receiver |
| 2990 | int arg_count = args->length(); |
| 2991 | for (int i = 0; i < arg_count; i++) { |
| 2992 | LoadAndSpill(args->at(i)); |
| 2993 | } |
| 2994 | |
| 2995 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 2996 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 2997 | frame_->EmitPush(r1); |
| 2998 | if (arg_count > 0) { |
| 2999 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3000 | frame_->EmitPush(r1); |
| 3001 | } else { |
| 3002 | frame_->EmitPush(r2); |
| 3003 | } |
| 3004 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3005 | // Push the receiver. |
| 3006 | __ ldr(r1, frame_->Receiver()); |
| 3007 | frame_->EmitPush(r1); |
| 3008 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3009 | // Resolve the call. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3010 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3011 | |
| 3012 | // 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] | 3013 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3014 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3015 | |
| 3016 | // Call the function. |
| 3017 | CodeForSourcePosition(node->position()); |
| 3018 | |
| 3019 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3020 | CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3021 | frame_->CallStub(&call_function, arg_count + 1); |
| 3022 | |
| 3023 | __ ldr(cp, frame_->Context()); |
| 3024 | // Remove the function from the stack. |
| 3025 | frame_->Drop(); |
| 3026 | frame_->EmitPush(r0); |
| 3027 | |
| 3028 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3029 | // ---------------------------------- |
| 3030 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3031 | // ---------------------------------- |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3032 | // Pass the global object as the receiver and let the IC stub |
| 3033 | // patch the stack to use the global proxy as 'this' in the |
| 3034 | // invoked function. |
| 3035 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3036 | |
| 3037 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3038 | int arg_count = args->length(); |
| 3039 | for (int i = 0; i < arg_count; i++) { |
| 3040 | LoadAndSpill(args->at(i)); |
| 3041 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3042 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3043 | // Setup the name register and call the IC initialization code. |
| 3044 | __ mov(r2, Operand(var->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3045 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3046 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3047 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3048 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3049 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3050 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3051 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3052 | |
| 3053 | } else if (var != NULL && var->slot() != NULL && |
| 3054 | var->slot()->type() == Slot::LOOKUP) { |
| 3055 | // ---------------------------------- |
| 3056 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3057 | // ---------------------------------- |
| 3058 | |
| 3059 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3060 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3061 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3062 | frame_->EmitPush(r0); |
| 3063 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3064 | // r0: slot value; r1: receiver |
| 3065 | |
| 3066 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3067 | frame_->EmitPush(r0); // function |
| 3068 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3069 | |
| 3070 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3071 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3072 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3073 | |
| 3074 | } else if (property != NULL) { |
| 3075 | // Check if the key is a literal string. |
| 3076 | Literal* literal = property->key()->AsLiteral(); |
| 3077 | |
| 3078 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3079 | // ------------------------------------------------------------------ |
| 3080 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3081 | // ------------------------------------------------------------------ |
| 3082 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3083 | LoadAndSpill(property->obj()); // Receiver. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3084 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3085 | int arg_count = args->length(); |
| 3086 | for (int i = 0; i < arg_count; i++) { |
| 3087 | LoadAndSpill(args->at(i)); |
| 3088 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3089 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3090 | // Set the name register and call the IC initialization code. |
| 3091 | __ mov(r2, Operand(literal->handle())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3092 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3093 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3094 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3095 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3096 | __ ldr(cp, frame_->Context()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3097 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3098 | |
| 3099 | } else { |
| 3100 | // ------------------------------------------- |
| 3101 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3102 | // ------------------------------------------- |
| 3103 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3104 | LoadAndSpill(property->obj()); |
| 3105 | LoadAndSpill(property->key()); |
| 3106 | EmitKeyedLoad(false); |
| 3107 | frame_->Drop(); // key |
| 3108 | // Put the function below the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3109 | if (property->is_synthetic()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3110 | // Use the global receiver. |
| 3111 | frame_->Drop(); |
| 3112 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3113 | LoadGlobalReceiver(r0); |
| 3114 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3115 | frame_->EmitPop(r1); // receiver |
| 3116 | frame_->EmitPush(r0); // function |
| 3117 | frame_->EmitPush(r1); // receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3120 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3121 | CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3122 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | } else { |
| 3126 | // ---------------------------------- |
| 3127 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3128 | // ---------------------------------- |
| 3129 | |
| 3130 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3131 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3132 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3133 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3134 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3135 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3136 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3137 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3138 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3139 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3140 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3144 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3145 | #ifdef DEBUG |
| 3146 | int original_height = frame_->height(); |
| 3147 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3148 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3149 | Comment cmnt(masm_, "[ CallNew"); |
| 3150 | |
| 3151 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3152 | // expression in new calls must be evaluated before the |
| 3153 | // arguments. This is different from ordinary calls, where the |
| 3154 | // actual function to call is resolved after the arguments have been |
| 3155 | // evaluated. |
| 3156 | |
| 3157 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3158 | // receiver. There is no need to use the global proxy here because |
| 3159 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3160 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3161 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Push the arguments ("left-to-right") on the stack. |
| 3164 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3165 | int arg_count = args->length(); |
| 3166 | for (int i = 0; i < arg_count; i++) { |
| 3167 | LoadAndSpill(args->at(i)); |
| 3168 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3169 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3170 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3171 | __ mov(r0, Operand(arg_count)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3172 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3173 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3174 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3175 | // Call the construct call builtin that handles allocation and |
| 3176 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3177 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3178 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3179 | frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3180 | |
| 3181 | // 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] | 3182 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3183 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3187 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3188 | VirtualFrame::SpilledScope spilled_scope; |
| 3189 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3190 | JumpTarget leave, null, function, non_function_constructor; |
| 3191 | |
| 3192 | // Load the object into r0. |
| 3193 | LoadAndSpill(args->at(0)); |
| 3194 | frame_->EmitPop(r0); |
| 3195 | |
| 3196 | // If the object is a smi, we return null. |
| 3197 | __ tst(r0, Operand(kSmiTagMask)); |
| 3198 | null.Branch(eq); |
| 3199 | |
| 3200 | // Check that the object is a JS object but take special care of JS |
| 3201 | // functions to make sure they have 'Function' as their class. |
| 3202 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3203 | null.Branch(lt); |
| 3204 | |
| 3205 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3206 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3207 | // LAST_JS_OBJECT_TYPE. |
| 3208 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3209 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3210 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3211 | function.Branch(eq); |
| 3212 | |
| 3213 | // Check if the constructor in the map is a function. |
| 3214 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3215 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3216 | non_function_constructor.Branch(ne); |
| 3217 | |
| 3218 | // The r0 register now contains the constructor function. Grab the |
| 3219 | // instance class name from there. |
| 3220 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3221 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3222 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3223 | leave.Jump(); |
| 3224 | |
| 3225 | // Functions have class 'Function'. |
| 3226 | function.Bind(); |
| 3227 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3228 | frame_->EmitPush(r0); |
| 3229 | leave.Jump(); |
| 3230 | |
| 3231 | // Objects with a non-function constructor have class 'Object'. |
| 3232 | non_function_constructor.Bind(); |
| 3233 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3234 | frame_->EmitPush(r0); |
| 3235 | leave.Jump(); |
| 3236 | |
| 3237 | // Non-JS objects have class null. |
| 3238 | null.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3239 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3240 | frame_->EmitPush(r0); |
| 3241 | |
| 3242 | // All done. |
| 3243 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3247 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3248 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3249 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3250 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3251 | LoadAndSpill(args->at(0)); |
| 3252 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3253 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3254 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3255 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3256 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3257 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3258 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3259 | // Load the value. |
| 3260 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3261 | leave.Bind(); |
| 3262 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3266 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3267 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3268 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3269 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3270 | LoadAndSpill(args->at(0)); // Load the object. |
| 3271 | LoadAndSpill(args->at(1)); // Load the value. |
| 3272 | frame_->EmitPop(r0); // r0 contains value |
| 3273 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3274 | // if (object->IsSmi()) return object. |
| 3275 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3276 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3277 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3278 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3279 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3280 | // Store the value. |
| 3281 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3282 | // Update the write barrier. |
| 3283 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3284 | __ RecordWrite(r1, r2, r3); |
| 3285 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3286 | leave.Bind(); |
| 3287 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3291 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3292 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3293 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3294 | LoadAndSpill(args->at(0)); |
| 3295 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3296 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3297 | cc_reg_ = eq; |
| 3298 | } |
| 3299 | |
| 3300 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3301 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3302 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3303 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3304 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3305 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3306 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3307 | LoadAndSpill(args->at(1)); |
| 3308 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3309 | __ CallRuntime(Runtime::kLog, 2); |
| 3310 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3311 | #endif |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3312 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3313 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3317 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3318 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3319 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3320 | LoadAndSpill(args->at(0)); |
| 3321 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3322 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3323 | cc_reg_ = eq; |
| 3324 | } |
| 3325 | |
| 3326 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3327 | // This should generate code that performs a charCodeAt() call or returns |
| 3328 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3329 | // 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] | 3330 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3331 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3332 | ASSERT(args->length() == 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3333 | Comment(masm_, "[ GenerateFastCharCodeAt"); |
| 3334 | |
| 3335 | LoadAndSpill(args->at(0)); |
| 3336 | LoadAndSpill(args->at(1)); |
| 3337 | frame_->EmitPop(r0); // Index. |
| 3338 | frame_->EmitPop(r1); // String. |
| 3339 | |
| 3340 | Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string; |
| 3341 | |
| 3342 | __ tst(r1, Operand(kSmiTagMask)); |
| 3343 | __ b(eq, &slow); // The 'string' was a Smi. |
| 3344 | |
| 3345 | ASSERT(kSmiTag == 0); |
| 3346 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
| 3347 | __ b(ne, &slow); // The index was negative or not a Smi. |
| 3348 | |
| 3349 | __ bind(&try_again_with_new_string); |
| 3350 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 3351 | __ b(ge, &slow); |
| 3352 | |
| 3353 | // Now r2 has the string type. |
| 3354 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3355 | // Now r3 has the length of the string. Compare with the index. |
| 3356 | __ cmp(r3, Operand(r0, LSR, kSmiTagSize)); |
| 3357 | __ b(le, &slow); |
| 3358 | |
| 3359 | // Here we know the index is in range. Check that string is sequential. |
| 3360 | ASSERT_EQ(0, kSeqStringTag); |
| 3361 | __ tst(r2, Operand(kStringRepresentationMask)); |
| 3362 | __ b(ne, ¬_a_flat_string); |
| 3363 | |
| 3364 | // Check whether it is an ASCII string. |
| 3365 | ASSERT_EQ(0, kTwoByteStringTag); |
| 3366 | __ tst(r2, Operand(kStringEncodingMask)); |
| 3367 | __ b(ne, &ascii_string); |
| 3368 | |
| 3369 | // 2-byte string. We can add without shifting since the Smi tag size is the |
| 3370 | // log2 of the number of bytes in a two-byte character. |
| 3371 | ASSERT_EQ(1, kSmiTagSize); |
| 3372 | ASSERT_EQ(0, kSmiShiftSize); |
| 3373 | __ add(r1, r1, Operand(r0)); |
| 3374 | __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize)); |
| 3375 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3376 | __ jmp(&end); |
| 3377 | |
| 3378 | __ bind(&ascii_string); |
| 3379 | __ add(r1, r1, Operand(r0, LSR, kSmiTagSize)); |
| 3380 | __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 3381 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3382 | __ jmp(&end); |
| 3383 | |
| 3384 | __ bind(¬_a_flat_string); |
| 3385 | __ and_(r2, r2, Operand(kStringRepresentationMask)); |
| 3386 | __ cmp(r2, Operand(kConsStringTag)); |
| 3387 | __ b(ne, &slow); |
| 3388 | |
| 3389 | // ConsString. |
| 3390 | // Check that the right hand side is the empty string (ie if this is really a |
| 3391 | // flat string in a cons string). If that is not the case we would rather go |
| 3392 | // to the runtime system now, to flatten the string. |
| 3393 | __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset)); |
| 3394 | __ LoadRoot(r3, Heap::kEmptyStringRootIndex); |
| 3395 | __ cmp(r2, Operand(r3)); |
| 3396 | __ b(ne, &slow); |
| 3397 | |
| 3398 | // Get the first of the two strings. |
| 3399 | __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset)); |
| 3400 | __ jmp(&try_again_with_new_string); |
| 3401 | |
| 3402 | __ bind(&slow); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3403 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3404 | |
| 3405 | __ bind(&end); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3406 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3410 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3411 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3412 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3413 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3414 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3415 | // We need the CC bits to come out as not_equal in the case where the |
| 3416 | // object is a smi. This can't be done with the usual test opcode so |
| 3417 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3418 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3419 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3420 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3421 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3422 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3423 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3424 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3425 | cc_reg_ = eq; |
| 3426 | } |
| 3427 | |
| 3428 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame^] | 3429 | void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) { |
| 3430 | VirtualFrame::SpilledScope spilled_scope; |
| 3431 | ASSERT(args->length() == 1); |
| 3432 | LoadAndSpill(args->at(0)); |
| 3433 | JumpTarget answer; |
| 3434 | // We need the CC bits to come out as not_equal in the case where the |
| 3435 | // object is a smi. This can't be done with the usual test opcode so |
| 3436 | // we use XOR to get the right CC bits. |
| 3437 | frame_->EmitPop(r0); |
| 3438 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3439 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
| 3440 | answer.Branch(ne); |
| 3441 | // It is a heap object - get the map. Check if the object is a regexp. |
| 3442 | __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE); |
| 3443 | answer.Bind(); |
| 3444 | cc_reg_ = eq; |
| 3445 | } |
| 3446 | |
| 3447 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3448 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 3449 | // This generates a fast version of: |
| 3450 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
| 3451 | VirtualFrame::SpilledScope spilled_scope; |
| 3452 | ASSERT(args->length() == 1); |
| 3453 | LoadAndSpill(args->at(0)); |
| 3454 | frame_->EmitPop(r1); |
| 3455 | __ tst(r1, Operand(kSmiTagMask)); |
| 3456 | false_target()->Branch(eq); |
| 3457 | |
| 3458 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 3459 | __ cmp(r1, ip); |
| 3460 | true_target()->Branch(eq); |
| 3461 | |
| 3462 | Register map_reg = r2; |
| 3463 | __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3464 | // Undetectable objects behave like undefined when tested with typeof. |
| 3465 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 3466 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3467 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 3468 | false_target()->Branch(eq); |
| 3469 | |
| 3470 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 3471 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3472 | false_target()->Branch(lt); |
| 3473 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
| 3474 | cc_reg_ = le; |
| 3475 | } |
| 3476 | |
| 3477 | |
| 3478 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 3479 | // This generates a fast version of: |
| 3480 | // (%_ClassOf(arg) === 'Function') |
| 3481 | VirtualFrame::SpilledScope spilled_scope; |
| 3482 | ASSERT(args->length() == 1); |
| 3483 | LoadAndSpill(args->at(0)); |
| 3484 | frame_->EmitPop(r0); |
| 3485 | __ tst(r0, Operand(kSmiTagMask)); |
| 3486 | false_target()->Branch(eq); |
| 3487 | Register map_reg = r2; |
| 3488 | __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE); |
| 3489 | cc_reg_ = eq; |
| 3490 | } |
| 3491 | |
| 3492 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3493 | void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) { |
| 3494 | VirtualFrame::SpilledScope spilled_scope; |
| 3495 | ASSERT(args->length() == 1); |
| 3496 | LoadAndSpill(args->at(0)); |
| 3497 | frame_->EmitPop(r0); |
| 3498 | __ tst(r0, Operand(kSmiTagMask)); |
| 3499 | false_target()->Branch(eq); |
| 3500 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3501 | __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3502 | __ tst(r1, Operand(1 << Map::kIsUndetectable)); |
| 3503 | cc_reg_ = ne; |
| 3504 | } |
| 3505 | |
| 3506 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3507 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3508 | VirtualFrame::SpilledScope spilled_scope; |
| 3509 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3510 | |
| 3511 | // Get the frame pointer for the calling frame. |
| 3512 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3513 | |
| 3514 | // Skip the arguments adaptor frame if it exists. |
| 3515 | Label check_frame_marker; |
| 3516 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3517 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3518 | __ b(ne, &check_frame_marker); |
| 3519 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3520 | |
| 3521 | // Check the marker in the calling frame. |
| 3522 | __ bind(&check_frame_marker); |
| 3523 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3524 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3525 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3529 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3530 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3531 | ASSERT(args->length() == 0); |
| 3532 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3533 | // Seed the result with the formal parameters count, which will be used |
| 3534 | // in case no arguments adaptor frame is found below the current frame. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3535 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3536 | |
| 3537 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3538 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3539 | frame_->CallStub(&stub, 0); |
| 3540 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3544 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3545 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3546 | ASSERT(args->length() == 1); |
| 3547 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3548 | // Satisfy contract with ArgumentsAccessStub: |
| 3549 | // 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] | 3550 | LoadAndSpill(args->at(0)); |
| 3551 | frame_->EmitPop(r1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3552 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3553 | |
| 3554 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3555 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3556 | frame_->CallStub(&stub, 0); |
| 3557 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3561 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3562 | VirtualFrame::SpilledScope spilled_scope; |
| 3563 | ASSERT(args->length() == 0); |
| 3564 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3565 | RelocInfo::RUNTIME_ENTRY); |
| 3566 | frame_->EmitPush(r0); |
| 3567 | } |
| 3568 | |
| 3569 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3570 | void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) { |
| 3571 | ASSERT_EQ(2, args->length()); |
| 3572 | |
| 3573 | Load(args->at(0)); |
| 3574 | Load(args->at(1)); |
| 3575 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3576 | StringAddStub stub(NO_STRING_ADD_FLAGS); |
| 3577 | frame_->CallStub(&stub, 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3578 | frame_->EmitPush(r0); |
| 3579 | } |
| 3580 | |
| 3581 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3582 | void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) { |
| 3583 | ASSERT_EQ(3, args->length()); |
| 3584 | |
| 3585 | Load(args->at(0)); |
| 3586 | Load(args->at(1)); |
| 3587 | Load(args->at(2)); |
| 3588 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3589 | SubStringStub stub; |
| 3590 | frame_->CallStub(&stub, 3); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3591 | frame_->EmitPush(r0); |
| 3592 | } |
| 3593 | |
| 3594 | |
| 3595 | void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) { |
| 3596 | ASSERT_EQ(2, args->length()); |
| 3597 | |
| 3598 | Load(args->at(0)); |
| 3599 | Load(args->at(1)); |
| 3600 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3601 | StringCompareStub stub; |
| 3602 | frame_->CallStub(&stub, 2); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3603 | frame_->EmitPush(r0); |
| 3604 | } |
| 3605 | |
| 3606 | |
| 3607 | void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) { |
| 3608 | ASSERT_EQ(4, args->length()); |
| 3609 | |
| 3610 | Load(args->at(0)); |
| 3611 | Load(args->at(1)); |
| 3612 | Load(args->at(2)); |
| 3613 | Load(args->at(3)); |
| 3614 | |
| 3615 | frame_->CallRuntime(Runtime::kRegExpExec, 4); |
| 3616 | frame_->EmitPush(r0); |
| 3617 | } |
| 3618 | |
| 3619 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3620 | void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) { |
| 3621 | ASSERT_EQ(args->length(), 1); |
| 3622 | |
| 3623 | // Load the argument on the stack and jump to the runtime. |
| 3624 | Load(args->at(0)); |
| 3625 | |
| 3626 | frame_->CallRuntime(Runtime::kNumberToString, 1); |
| 3627 | frame_->EmitPush(r0); |
| 3628 | } |
| 3629 | |
| 3630 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3631 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3632 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3633 | ASSERT(args->length() == 2); |
| 3634 | |
| 3635 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3636 | LoadAndSpill(args->at(0)); |
| 3637 | LoadAndSpill(args->at(1)); |
| 3638 | frame_->EmitPop(r0); |
| 3639 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3640 | __ cmp(r0, Operand(r1)); |
| 3641 | cc_reg_ = eq; |
| 3642 | } |
| 3643 | |
| 3644 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3645 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3646 | #ifdef DEBUG |
| 3647 | int original_height = frame_->height(); |
| 3648 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3649 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3650 | if (CheckForInlineRuntimeCall(node)) { |
| 3651 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3652 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3653 | return; |
| 3654 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3655 | |
| 3656 | ZoneList<Expression*>* args = node->arguments(); |
| 3657 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3658 | Runtime::Function* function = node->function(); |
| 3659 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3660 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3661 | // Prepare stack for calling JS runtime function. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3662 | // Push the builtins object found in the current global object. |
| 3663 | __ ldr(r1, GlobalObject()); |
| 3664 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3665 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3666 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3667 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3668 | // Push the arguments ("left-to-right"). |
| 3669 | int arg_count = args->length(); |
| 3670 | for (int i = 0; i < arg_count; i++) { |
| 3671 | LoadAndSpill(args->at(i)); |
| 3672 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3673 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3674 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3675 | // Call the JS runtime function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3676 | __ mov(r2, Operand(node->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3677 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3678 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3679 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3680 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3681 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3682 | } else { |
| 3683 | // Call the C runtime function. |
| 3684 | frame_->CallRuntime(function, arg_count); |
| 3685 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3686 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3687 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3691 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3692 | #ifdef DEBUG |
| 3693 | int original_height = frame_->height(); |
| 3694 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3695 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3696 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3697 | |
| 3698 | Token::Value op = node->op(); |
| 3699 | |
| 3700 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3701 | LoadConditionAndSpill(node->expression(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3702 | false_target(), |
| 3703 | true_target(), |
| 3704 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3705 | // LoadCondition may (and usually does) leave a test and branch to |
| 3706 | // be emitted by the caller. In that case, negate the condition. |
| 3707 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3708 | |
| 3709 | } else if (op == Token::DELETE) { |
| 3710 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3711 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3712 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3713 | LoadAndSpill(property->obj()); |
| 3714 | LoadAndSpill(property->key()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3715 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3716 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3717 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3718 | Slot* slot = variable->slot(); |
| 3719 | if (variable->is_global()) { |
| 3720 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3721 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3722 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3723 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3724 | |
| 3725 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3726 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3727 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3728 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3729 | frame_->EmitPush(r0); |
| 3730 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3731 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3732 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3733 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3734 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3735 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3736 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3737 | } else { |
| 3738 | // Default: Result of deleting non-global, not dynamically |
| 3739 | // introduced variables is false. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3740 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3741 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3742 | |
| 3743 | } else { |
| 3744 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3745 | LoadAndSpill(node->expression()); // may have side-effects |
| 3746 | frame_->Drop(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3747 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3748 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3749 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3750 | |
| 3751 | } else if (op == Token::TYPEOF) { |
| 3752 | // Special case for loading the typeof expression; see comment on |
| 3753 | // LoadTypeofExpression(). |
| 3754 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3755 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3756 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3757 | |
| 3758 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3759 | bool overwrite = |
| 3760 | (node->expression()->AsBinaryOperation() != NULL && |
| 3761 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3762 | LoadAndSpill(node->expression()); |
| 3763 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3764 | switch (op) { |
| 3765 | case Token::NOT: |
| 3766 | case Token::DELETE: |
| 3767 | case Token::TYPEOF: |
| 3768 | UNREACHABLE(); // handled above |
| 3769 | break; |
| 3770 | |
| 3771 | case Token::SUB: { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3772 | GenericUnaryOpStub stub(Token::SUB, overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3773 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3774 | break; |
| 3775 | } |
| 3776 | |
| 3777 | case Token::BIT_NOT: { |
| 3778 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3779 | JumpTarget smi_label; |
| 3780 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3781 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3782 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3783 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3784 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 3785 | frame_->CallStub(&stub, 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3786 | continue_label.Jump(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3787 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3788 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3789 | __ mvn(r0, Operand(r0)); |
| 3790 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3791 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3792 | break; |
| 3793 | } |
| 3794 | |
| 3795 | case Token::VOID: |
| 3796 | // since the stack top is cached in r0, popping and then |
| 3797 | // pushing a value can be done by just writing to r0. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3798 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3799 | break; |
| 3800 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3801 | case Token::ADD: { |
| 3802 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3803 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3804 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3805 | continue_label.Branch(eq); |
| 3806 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3807 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3808 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3809 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3810 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3811 | default: |
| 3812 | UNREACHABLE(); |
| 3813 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3814 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3815 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3816 | ASSERT(!has_valid_frame() || |
| 3817 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3818 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3822 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3823 | #ifdef DEBUG |
| 3824 | int original_height = frame_->height(); |
| 3825 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3826 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3827 | Comment cmnt(masm_, "[ CountOperation"); |
| 3828 | |
| 3829 | bool is_postfix = node->is_postfix(); |
| 3830 | bool is_increment = node->op() == Token::INC; |
| 3831 | |
| 3832 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3833 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3834 | |
| 3835 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3836 | if (is_postfix) { |
| 3837 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3838 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3839 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3840 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3841 | // A constant reference is not saved to, so a constant reference is not a |
| 3842 | // compound assignment reference. |
| 3843 | { Reference target(this, node->expression(), !is_const); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3844 | if (target.is_illegal()) { |
| 3845 | // Spoof the virtual frame to have the expected height (one higher |
| 3846 | // than on entry). |
| 3847 | if (!is_postfix) { |
| 3848 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3849 | frame_->EmitPush(r0); |
| 3850 | } |
| 3851 | ASSERT(frame_->height() == original_height + 1); |
| 3852 | return; |
| 3853 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3854 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3855 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3856 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3857 | JumpTarget slow; |
| 3858 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3859 | |
| 3860 | // Load the value (1) into register r1. |
| 3861 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3862 | |
| 3863 | // Check for smi operand. |
| 3864 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3865 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3866 | |
| 3867 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3868 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3869 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3870 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3871 | |
| 3872 | // Perform optimistic increment/decrement. |
| 3873 | if (is_increment) { |
| 3874 | __ add(r0, r0, Operand(r1), SetCC); |
| 3875 | } else { |
| 3876 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3877 | } |
| 3878 | |
| 3879 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3880 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3881 | |
| 3882 | // Revert optimistic increment/decrement. |
| 3883 | if (is_increment) { |
| 3884 | __ sub(r0, r0, Operand(r1)); |
| 3885 | } else { |
| 3886 | __ add(r0, r0, Operand(r1)); |
| 3887 | } |
| 3888 | |
| 3889 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3890 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3891 | { |
| 3892 | // Convert the operand to a number. |
| 3893 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3894 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3895 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3896 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3897 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3898 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3901 | // Compute the new value. |
| 3902 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3903 | frame_->EmitPush(r0); |
| 3904 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3905 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3906 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3907 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3908 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3912 | exit.Bind(); |
| 3913 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3914 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3918 | if (is_postfix) frame_->EmitPop(r0); |
| 3919 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3923 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3924 | #ifdef DEBUG |
| 3925 | int original_height = frame_->height(); |
| 3926 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3927 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3928 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3929 | Token::Value op = node->op(); |
| 3930 | |
| 3931 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3932 | // operators must yield the result of one of the two expressions |
| 3933 | // before any ToBoolean() conversions. This means that the value |
| 3934 | // produced by a && or || operator is not necessarily a boolean. |
| 3935 | |
| 3936 | // NOTE: If the left hand side produces a materialized value (not in |
| 3937 | // the CC register), we force the right hand side to do the |
| 3938 | // same. This is necessary because we may have to branch to the exit |
| 3939 | // after evaluating the left hand side (due to the shortcut |
| 3940 | // semantics), but the compiler must (statically) know if the result |
| 3941 | // of compiling the binary operation is materialized or not. |
| 3942 | |
| 3943 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3944 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3945 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3946 | &is_true, |
| 3947 | false_target(), |
| 3948 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3949 | if (has_valid_frame() && !has_cc()) { |
| 3950 | // 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] | 3951 | JumpTarget pop_and_continue; |
| 3952 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3953 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3954 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3955 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3956 | // Avoid popping the result if it converts to 'false' using the |
| 3957 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3958 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3959 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3960 | Branch(false, &exit); |
| 3961 | |
| 3962 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3963 | pop_and_continue.Bind(); |
| 3964 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3965 | |
| 3966 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3967 | is_true.Bind(); |
| 3968 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3969 | |
| 3970 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3971 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3972 | } else if (has_cc() || is_true.is_linked()) { |
| 3973 | // The left-hand side is either (a) partially compiled to |
| 3974 | // control flow with a final branch left to emit or (b) fully |
| 3975 | // compiled to control flow and possibly true. |
| 3976 | if (has_cc()) { |
| 3977 | Branch(false, false_target()); |
| 3978 | } |
| 3979 | is_true.Bind(); |
| 3980 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3981 | true_target(), |
| 3982 | false_target(), |
| 3983 | false); |
| 3984 | } else { |
| 3985 | // Nothing to do. |
| 3986 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3990 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3991 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3992 | true_target(), |
| 3993 | &is_false, |
| 3994 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3995 | if (has_valid_frame() && !has_cc()) { |
| 3996 | // 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] | 3997 | JumpTarget pop_and_continue; |
| 3998 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3999 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4000 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4001 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4002 | // Avoid popping the result if it converts to 'true' using the |
| 4003 | // standard ToBoolean() conversion as described in ECMA-262, |
| 4004 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4005 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4006 | Branch(true, &exit); |
| 4007 | |
| 4008 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4009 | pop_and_continue.Bind(); |
| 4010 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4011 | |
| 4012 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4013 | is_false.Bind(); |
| 4014 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4015 | |
| 4016 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4017 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4018 | } else if (has_cc() || is_false.is_linked()) { |
| 4019 | // The left-hand side is either (a) partially compiled to |
| 4020 | // control flow with a final branch left to emit or (b) fully |
| 4021 | // compiled to control flow and possibly false. |
| 4022 | if (has_cc()) { |
| 4023 | Branch(true, true_target()); |
| 4024 | } |
| 4025 | is_false.Bind(); |
| 4026 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4027 | true_target(), |
| 4028 | false_target(), |
| 4029 | false); |
| 4030 | } else { |
| 4031 | // Nothing to do. |
| 4032 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | } else { |
| 4036 | // Optimize for the case where (at least) one of the expressions |
| 4037 | // is a literal small integer. |
| 4038 | Literal* lliteral = node->left()->AsLiteral(); |
| 4039 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4040 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 4041 | // never return a constant/immutable object. |
| 4042 | bool overwrite_left = |
| 4043 | (node->left()->AsBinaryOperation() != NULL && |
| 4044 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 4045 | bool overwrite_right = |
| 4046 | (node->right()->AsBinaryOperation() != NULL && |
| 4047 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4048 | |
| 4049 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4050 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4051 | SmiOperation(node->op(), |
| 4052 | rliteral->handle(), |
| 4053 | false, |
| 4054 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4055 | |
| 4056 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4057 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4058 | SmiOperation(node->op(), |
| 4059 | lliteral->handle(), |
| 4060 | true, |
| 4061 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4062 | |
| 4063 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4064 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 4065 | if (overwrite_left) { |
| 4066 | overwrite_mode = OVERWRITE_LEFT; |
| 4067 | } else if (overwrite_right) { |
| 4068 | overwrite_mode = OVERWRITE_RIGHT; |
| 4069 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4070 | LoadAndSpill(node->left()); |
| 4071 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4072 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4073 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4074 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4075 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4076 | ASSERT(!has_valid_frame() || |
| 4077 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4078 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4082 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4083 | #ifdef DEBUG |
| 4084 | int original_height = frame_->height(); |
| 4085 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4086 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4087 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4088 | frame_->EmitPush(r0); |
| 4089 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4093 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4094 | #ifdef DEBUG |
| 4095 | int original_height = frame_->height(); |
| 4096 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4097 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4098 | Comment cmnt(masm_, "[ CompareOperation"); |
| 4099 | |
| 4100 | // Get the expressions from the node. |
| 4101 | Expression* left = node->left(); |
| 4102 | Expression* right = node->right(); |
| 4103 | Token::Value op = node->op(); |
| 4104 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4105 | // To make null checks efficient, we check if either left or right is the |
| 4106 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 4107 | // instead of calling the (very) general runtime routine for checking |
| 4108 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4109 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4110 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4111 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4112 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4113 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4114 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4115 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4116 | LoadAndSpill(left_is_null ? right : left); |
| 4117 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4118 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4119 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4120 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4121 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4122 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4123 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4124 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4125 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4126 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4127 | __ cmp(r0, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4128 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4129 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4130 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4131 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4132 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4133 | // It can be an undetectable object. |
| 4134 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4135 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 4136 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 4137 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4140 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4141 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4142 | return; |
| 4143 | } |
| 4144 | } |
| 4145 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4146 | // To make typeof testing for natives implemented in JavaScript really |
| 4147 | // efficient, we generate special code for expressions of the form: |
| 4148 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4149 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 4150 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4151 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4152 | (right->AsLiteral() != NULL && |
| 4153 | right->AsLiteral()->handle()->IsString())) { |
| 4154 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4155 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4156 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4157 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4158 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4159 | |
| 4160 | if (check->Equals(Heap::number_symbol())) { |
| 4161 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4162 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4163 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4164 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 4165 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4166 | cc_reg_ = eq; |
| 4167 | |
| 4168 | } else if (check->Equals(Heap::string_symbol())) { |
| 4169 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4170 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4171 | |
| 4172 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4173 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4174 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4175 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4176 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4177 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4178 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4179 | |
| 4180 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 4181 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 4182 | cc_reg_ = lt; |
| 4183 | |
| 4184 | } else if (check->Equals(Heap::boolean_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4185 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 4186 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4187 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4188 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 4189 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4190 | cc_reg_ = eq; |
| 4191 | |
| 4192 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4193 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4194 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4195 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4196 | |
| 4197 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4198 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4199 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4200 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4201 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4202 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4203 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4204 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4205 | |
| 4206 | cc_reg_ = eq; |
| 4207 | |
| 4208 | } else if (check->Equals(Heap::function_symbol())) { |
| 4209 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4210 | false_target()->Branch(eq); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4211 | Register map_reg = r2; |
| 4212 | __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE); |
| 4213 | true_target()->Branch(eq); |
| 4214 | // Regular expressions are callable so typeof == 'function'. |
| 4215 | __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4216 | cc_reg_ = eq; |
| 4217 | |
| 4218 | } else if (check->Equals(Heap::object_symbol())) { |
| 4219 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4220 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4221 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4222 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4223 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4224 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4225 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4226 | Register map_reg = r2; |
| 4227 | __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE); |
| 4228 | false_target()->Branch(eq); |
| 4229 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4230 | // It can be an undetectable object. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4231 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4232 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4233 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4234 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4235 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4236 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4237 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4238 | false_target()->Branch(lt); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4239 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4240 | cc_reg_ = le; |
| 4241 | |
| 4242 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4243 | // Uncommon case: typeof testing against a string literal that is |
| 4244 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4245 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4246 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4247 | ASSERT(!has_valid_frame() || |
| 4248 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4249 | return; |
| 4250 | } |
| 4251 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4252 | switch (op) { |
| 4253 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4254 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4255 | break; |
| 4256 | |
| 4257 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4258 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4259 | break; |
| 4260 | |
| 4261 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4262 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4263 | break; |
| 4264 | |
| 4265 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4266 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4267 | break; |
| 4268 | |
| 4269 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4270 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4271 | break; |
| 4272 | |
| 4273 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4274 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4275 | break; |
| 4276 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4277 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4278 | LoadAndSpill(left); |
| 4279 | LoadAndSpill(right); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4280 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4281 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4282 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4283 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4284 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4285 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4286 | LoadAndSpill(left); |
| 4287 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4288 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4289 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4290 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4291 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4292 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4293 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4294 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4295 | |
| 4296 | default: |
| 4297 | UNREACHABLE(); |
| 4298 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4299 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4300 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4304 | void CodeGenerator::EmitKeyedLoad(bool is_global) { |
| 4305 | Comment cmnt(masm_, "[ Load from keyed Property"); |
| 4306 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 4307 | RelocInfo::Mode rmode = is_global |
| 4308 | ? RelocInfo::CODE_TARGET_CONTEXT |
| 4309 | : RelocInfo::CODE_TARGET; |
| 4310 | frame_->CallCodeObject(ic, rmode, 0); |
| 4311 | } |
| 4312 | |
| 4313 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4314 | #ifdef DEBUG |
| 4315 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4316 | #endif |
| 4317 | |
| 4318 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4319 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4320 | #define __ ACCESS_MASM(masm) |
| 4321 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4322 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4323 | Handle<String> Reference::GetName() { |
| 4324 | ASSERT(type_ == NAMED); |
| 4325 | Property* property = expression_->AsProperty(); |
| 4326 | if (property == NULL) { |
| 4327 | // Global variable reference treated as a named property reference. |
| 4328 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4329 | ASSERT(proxy->AsVariable() != NULL); |
| 4330 | ASSERT(proxy->AsVariable()->is_global()); |
| 4331 | return proxy->name(); |
| 4332 | } else { |
| 4333 | Literal* raw_name = property->key()->AsLiteral(); |
| 4334 | ASSERT(raw_name != NULL); |
| 4335 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4336 | } |
| 4337 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4338 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4339 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4340 | void Reference::GetValue() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4341 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4342 | ASSERT(!is_illegal()); |
| 4343 | ASSERT(!cgen_->has_cc()); |
| 4344 | MacroAssembler* masm = cgen_->masm(); |
| 4345 | Property* property = expression_->AsProperty(); |
| 4346 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4347 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | switch (type_) { |
| 4351 | case SLOT: { |
| 4352 | Comment cmnt(masm, "[ Load from Slot"); |
| 4353 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4354 | ASSERT(slot != NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4355 | cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4356 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4359 | case NAMED: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4360 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4361 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4362 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4363 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4364 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4365 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4366 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4367 | ASSERT(var == NULL || var->is_global()); |
| 4368 | RelocInfo::Mode rmode = (var == NULL) |
| 4369 | ? RelocInfo::CODE_TARGET |
| 4370 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4371 | frame->CallCodeObject(ic, rmode, 0); |
| 4372 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4373 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4376 | case KEYED: { |
| 4377 | // TODO(181): Implement inlined version of array indexing once |
| 4378 | // loop nesting is properly tracked on ARM. |
| 4379 | ASSERT(property != NULL); |
| 4380 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 4381 | ASSERT(var == NULL || var->is_global()); |
| 4382 | cgen_->EmitKeyedLoad(var != NULL); |
| 4383 | cgen_->frame()->EmitPush(r0); |
| 4384 | break; |
| 4385 | } |
| 4386 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4387 | default: |
| 4388 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4389 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4390 | |
| 4391 | if (!persist_after_get_) { |
| 4392 | cgen_->UnloadReference(this); |
| 4393 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4397 | void Reference::SetValue(InitState init_state) { |
| 4398 | ASSERT(!is_illegal()); |
| 4399 | ASSERT(!cgen_->has_cc()); |
| 4400 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4401 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4402 | Property* property = expression_->AsProperty(); |
| 4403 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4404 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4405 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4406 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4407 | switch (type_) { |
| 4408 | case SLOT: { |
| 4409 | Comment cmnt(masm, "[ Store to Slot"); |
| 4410 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4411 | cgen_->StoreToSlot(slot, init_state); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4412 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4413 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4416 | case NAMED: { |
| 4417 | Comment cmnt(masm, "[ Store to named Property"); |
| 4418 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4419 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4420 | Handle<String> name(GetName()); |
| 4421 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4422 | frame->EmitPop(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4423 | frame->EmitPop(r1); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4424 | __ mov(r2, Operand(name)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4425 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4426 | frame->EmitPush(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4427 | set_unloaded(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4428 | break; |
| 4429 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4430 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4431 | case KEYED: { |
| 4432 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4433 | Property* property = expression_->AsProperty(); |
| 4434 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4435 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4436 | |
| 4437 | // Call IC code. |
| 4438 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4439 | frame->EmitPop(r0); // value |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4440 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4441 | frame->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4442 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4443 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4444 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4445 | |
| 4446 | default: |
| 4447 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4448 | } |
| 4449 | } |
| 4450 | |
| 4451 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4452 | void FastNewClosureStub::Generate(MacroAssembler* masm) { |
| 4453 | // Clone the boilerplate in new space. Set the context to the |
| 4454 | // current context in cp. |
| 4455 | Label gc; |
| 4456 | |
| 4457 | // Pop the boilerplate function from the stack. |
| 4458 | __ pop(r3); |
| 4459 | |
| 4460 | // Attempt to allocate new JSFunction in new space. |
| 4461 | __ AllocateInNewSpace(JSFunction::kSize / kPointerSize, |
| 4462 | r0, |
| 4463 | r1, |
| 4464 | r2, |
| 4465 | &gc, |
| 4466 | TAG_OBJECT); |
| 4467 | |
| 4468 | // Compute the function map in the current global context and set that |
| 4469 | // as the map of the allocated object. |
| 4470 | __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4471 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 4472 | __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX))); |
| 4473 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4474 | |
| 4475 | // Clone the rest of the boilerplate fields. We don't have to update |
| 4476 | // the write barrier because the allocated object is in new space. |
| 4477 | for (int offset = kPointerSize; |
| 4478 | offset < JSFunction::kSize; |
| 4479 | offset += kPointerSize) { |
| 4480 | if (offset == JSFunction::kContextOffset) { |
| 4481 | __ str(cp, FieldMemOperand(r0, offset)); |
| 4482 | } else { |
| 4483 | __ ldr(r1, FieldMemOperand(r3, offset)); |
| 4484 | __ str(r1, FieldMemOperand(r0, offset)); |
| 4485 | } |
| 4486 | } |
| 4487 | |
| 4488 | // Return result. The argument boilerplate has been popped already. |
| 4489 | __ Ret(); |
| 4490 | |
| 4491 | // Create a new closure through the slower runtime call. |
| 4492 | __ bind(&gc); |
| 4493 | __ push(cp); |
| 4494 | __ push(r3); |
| 4495 | __ TailCallRuntime(ExternalReference(Runtime::kNewClosure), 2, 1); |
| 4496 | } |
| 4497 | |
| 4498 | |
| 4499 | void FastNewContextStub::Generate(MacroAssembler* masm) { |
| 4500 | // Try to allocate the context in new space. |
| 4501 | Label gc; |
| 4502 | int length = slots_ + Context::MIN_CONTEXT_SLOTS; |
| 4503 | |
| 4504 | // Attempt to allocate the context in new space. |
| 4505 | __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize), |
| 4506 | r0, |
| 4507 | r1, |
| 4508 | r2, |
| 4509 | &gc, |
| 4510 | TAG_OBJECT); |
| 4511 | |
| 4512 | // Load the function from the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4513 | __ ldr(r3, MemOperand(sp, 0)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4514 | |
| 4515 | // Setup the object header. |
| 4516 | __ LoadRoot(r2, Heap::kContextMapRootIndex); |
| 4517 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4518 | __ mov(r2, Operand(length)); |
| 4519 | __ str(r2, FieldMemOperand(r0, Array::kLengthOffset)); |
| 4520 | |
| 4521 | // Setup the fixed slots. |
| 4522 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 4523 | __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 4524 | __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 4525 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
| 4526 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX))); |
| 4527 | |
| 4528 | // Copy the global object from the surrounding context. |
| 4529 | __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4530 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4531 | |
| 4532 | // Initialize the rest of the slots to undefined. |
| 4533 | __ LoadRoot(r1, Heap::kUndefinedValueRootIndex); |
| 4534 | for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) { |
| 4535 | __ str(r1, MemOperand(r0, Context::SlotOffset(i))); |
| 4536 | } |
| 4537 | |
| 4538 | // Remove the on-stack argument and return. |
| 4539 | __ mov(cp, r0); |
| 4540 | __ pop(); |
| 4541 | __ Ret(); |
| 4542 | |
| 4543 | // Need to collect. Call into runtime system. |
| 4544 | __ bind(&gc); |
| 4545 | __ TailCallRuntime(ExternalReference(Runtime::kNewContext), 1, 1); |
| 4546 | } |
| 4547 | |
| 4548 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4549 | void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) { |
| 4550 | // Stack layout on entry: |
| 4551 | // |
| 4552 | // [sp]: constant elements. |
| 4553 | // [sp + kPointerSize]: literal index. |
| 4554 | // [sp + (2 * kPointerSize)]: literals array. |
| 4555 | |
| 4556 | // All sizes here are multiples of kPointerSize. |
| 4557 | int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0; |
| 4558 | int size = JSArray::kSize + elements_size; |
| 4559 | |
| 4560 | // Load boilerplate object into r3 and check if we need to create a |
| 4561 | // boilerplate. |
| 4562 | Label slow_case; |
| 4563 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 4564 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 4565 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 4566 | __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4567 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4568 | __ cmp(r3, ip); |
| 4569 | __ b(eq, &slow_case); |
| 4570 | |
| 4571 | // Allocate both the JS array and the elements array in one big |
| 4572 | // allocation. This avoids multiple limit checks. |
| 4573 | __ AllocateInNewSpace(size / kPointerSize, |
| 4574 | r0, |
| 4575 | r1, |
| 4576 | r2, |
| 4577 | &slow_case, |
| 4578 | TAG_OBJECT); |
| 4579 | |
| 4580 | // Copy the JS array part. |
| 4581 | for (int i = 0; i < JSArray::kSize; i += kPointerSize) { |
| 4582 | if ((i != JSArray::kElementsOffset) || (length_ == 0)) { |
| 4583 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 4584 | __ str(r1, FieldMemOperand(r0, i)); |
| 4585 | } |
| 4586 | } |
| 4587 | |
| 4588 | if (length_ > 0) { |
| 4589 | // Get hold of the elements array of the boilerplate and setup the |
| 4590 | // elements pointer in the resulting object. |
| 4591 | __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset)); |
| 4592 | __ add(r2, r0, Operand(JSArray::kSize)); |
| 4593 | __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 4594 | |
| 4595 | // Copy the elements array. |
| 4596 | for (int i = 0; i < elements_size; i += kPointerSize) { |
| 4597 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 4598 | __ str(r1, FieldMemOperand(r2, i)); |
| 4599 | } |
| 4600 | } |
| 4601 | |
| 4602 | // Return and remove the on-stack parameters. |
| 4603 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 4604 | __ Ret(); |
| 4605 | |
| 4606 | __ bind(&slow_case); |
| 4607 | ExternalReference runtime(Runtime::kCreateArrayLiteralShallow); |
| 4608 | __ TailCallRuntime(runtime, 3, 1); |
| 4609 | } |
| 4610 | |
| 4611 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4612 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4613 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4614 | // (31 instead of 32). |
| 4615 | static void CountLeadingZeros( |
| 4616 | MacroAssembler* masm, |
| 4617 | Register source, |
| 4618 | Register scratch, |
| 4619 | Register zeros) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4620 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4621 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4622 | #else |
| 4623 | __ mov(zeros, Operand(0)); |
| 4624 | __ mov(scratch, source); |
| 4625 | // Top 16. |
| 4626 | __ tst(scratch, Operand(0xffff0000)); |
| 4627 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4628 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4629 | // Top 8. |
| 4630 | __ tst(scratch, Operand(0xff000000)); |
| 4631 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4632 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4633 | // Top 4. |
| 4634 | __ tst(scratch, Operand(0xf0000000)); |
| 4635 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4636 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4637 | // Top 2. |
| 4638 | __ tst(scratch, Operand(0xc0000000)); |
| 4639 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4640 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4641 | // Top bit. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4642 | __ tst(scratch, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4643 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4644 | #endif |
| 4645 | } |
| 4646 | |
| 4647 | |
| 4648 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4649 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4650 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4651 | // scratch register. Destroys the source register. No GC occurs during this |
| 4652 | // stub so you don't have to set up the frame. |
| 4653 | class ConvertToDoubleStub : public CodeStub { |
| 4654 | public: |
| 4655 | ConvertToDoubleStub(Register result_reg_1, |
| 4656 | Register result_reg_2, |
| 4657 | Register source_reg, |
| 4658 | Register scratch_reg) |
| 4659 | : result1_(result_reg_1), |
| 4660 | result2_(result_reg_2), |
| 4661 | source_(source_reg), |
| 4662 | zeros_(scratch_reg) { } |
| 4663 | |
| 4664 | private: |
| 4665 | Register result1_; |
| 4666 | Register result2_; |
| 4667 | Register source_; |
| 4668 | Register zeros_; |
| 4669 | |
| 4670 | // Minor key encoding in 16 bits. |
| 4671 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4672 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4673 | |
| 4674 | Major MajorKey() { return ConvertToDouble; } |
| 4675 | int MinorKey() { |
| 4676 | // Encode the parameters in a unique 16 bit value. |
| 4677 | return result1_.code() + |
| 4678 | (result2_.code() << 4) + |
| 4679 | (source_.code() << 8) + |
| 4680 | (zeros_.code() << 12); |
| 4681 | } |
| 4682 | |
| 4683 | void Generate(MacroAssembler* masm); |
| 4684 | |
| 4685 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4686 | |
| 4687 | #ifdef DEBUG |
| 4688 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4689 | #endif |
| 4690 | }; |
| 4691 | |
| 4692 | |
| 4693 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4694 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4695 | Register exponent = result1_; |
| 4696 | Register mantissa = result2_; |
| 4697 | #else |
| 4698 | Register exponent = result2_; |
| 4699 | Register mantissa = result1_; |
| 4700 | #endif |
| 4701 | Label not_special; |
| 4702 | // Convert from Smi to integer. |
| 4703 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4704 | // Move sign bit from source to destination. This works because the sign bit |
| 4705 | // in the exponent word of the double has the same position and polarity as |
| 4706 | // the 2's complement sign bit in a Smi. |
| 4707 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4708 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4709 | // Subtract from 0 if source was negative. |
| 4710 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4711 | __ cmp(source_, Operand(1)); |
| 4712 | __ b(gt, ¬_special); |
| 4713 | |
| 4714 | // We have -1, 0 or 1, which we treat specially. |
| 4715 | __ cmp(source_, Operand(0)); |
| 4716 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4717 | static const uint32_t exponent_word_for_1 = |
| 4718 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4719 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4720 | // 1, 0 and -1 all have 0 for the second word. |
| 4721 | __ mov(mantissa, Operand(0)); |
| 4722 | __ Ret(); |
| 4723 | |
| 4724 | __ bind(¬_special); |
| 4725 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4726 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4727 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4728 | // Compute exponent and or it into the exponent register. |
| 4729 | // We use result2 as a scratch register here. |
| 4730 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4731 | __ orr(exponent, |
| 4732 | exponent, |
| 4733 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4734 | // Shift up the source chopping the top bit off. |
| 4735 | __ add(zeros_, zeros_, Operand(1)); |
| 4736 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4737 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4738 | // Compute lower part of fraction (last 12 bits). |
| 4739 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4740 | // And the top (top 20 bits). |
| 4741 | __ orr(exponent, |
| 4742 | exponent, |
| 4743 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4744 | __ Ret(); |
| 4745 | } |
| 4746 | |
| 4747 | |
| 4748 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4749 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4750 | // so you don't have to set up the frame. |
| 4751 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4752 | public: |
| 4753 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4754 | Register the_heap_number, |
| 4755 | Register scratch) |
| 4756 | : the_int_(the_int), |
| 4757 | the_heap_number_(the_heap_number), |
| 4758 | scratch_(scratch) { } |
| 4759 | |
| 4760 | private: |
| 4761 | Register the_int_; |
| 4762 | Register the_heap_number_; |
| 4763 | Register scratch_; |
| 4764 | |
| 4765 | // Minor key encoding in 16 bits. |
| 4766 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4767 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4768 | |
| 4769 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4770 | int MinorKey() { |
| 4771 | // Encode the parameters in a unique 16 bit value. |
| 4772 | return the_int_.code() + |
| 4773 | (the_heap_number_.code() << 4) + |
| 4774 | (scratch_.code() << 8); |
| 4775 | } |
| 4776 | |
| 4777 | void Generate(MacroAssembler* masm); |
| 4778 | |
| 4779 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4780 | |
| 4781 | #ifdef DEBUG |
| 4782 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4783 | #endif |
| 4784 | }; |
| 4785 | |
| 4786 | |
| 4787 | // See comment for class. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4788 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4789 | Label max_negative_int; |
| 4790 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4791 | // We test for the special value that has a different exponent. This test |
| 4792 | // has the neat side effect of setting the flags according to the sign. |
| 4793 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4794 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4795 | __ b(eq, &max_negative_int); |
| 4796 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4797 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4798 | uint32_t non_smi_exponent = |
| 4799 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4800 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4801 | // Set the sign bit in scratch_ if the value was negative. |
| 4802 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4803 | // Subtract from 0 if the value was negative. |
| 4804 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4805 | // We should be masking the implict first digit of the mantissa away here, |
| 4806 | // but it just ends up combining harmlessly with the last digit of the |
| 4807 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4808 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4809 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4810 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4811 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4812 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4813 | HeapNumber::kExponentOffset)); |
| 4814 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4815 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4816 | HeapNumber::kMantissaOffset)); |
| 4817 | __ Ret(); |
| 4818 | |
| 4819 | __ bind(&max_negative_int); |
| 4820 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4821 | // a double because it uses a sign bit instead of using two's complement. |
| 4822 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4823 | // significant 1 bit is not stored. |
| 4824 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4825 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4826 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4827 | __ mov(ip, Operand(0)); |
| 4828 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4829 | __ Ret(); |
| 4830 | } |
| 4831 | |
| 4832 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4833 | // Handle the case where the lhs and rhs are the same object. |
| 4834 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4835 | // for "identity and not NaN". |
| 4836 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4837 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4838 | Condition cc, |
| 4839 | bool never_nan_nan) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4840 | Label not_identical; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4841 | Label heap_number, return_equal; |
| 4842 | Register exp_mask_reg = r5; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4843 | __ cmp(r0, Operand(r1)); |
| 4844 | __ b(ne, ¬_identical); |
| 4845 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4846 | // The two objects are identical. If we know that one of them isn't NaN then |
| 4847 | // we now know they test equal. |
| 4848 | if (cc != eq || !never_nan_nan) { |
| 4849 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4850 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4851 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4852 | // so we do the second best thing - test it ourselves. |
| 4853 | // They are both equal and they are not both Smis so both of them are not |
| 4854 | // Smis. If it's not a heap number, then return equal. |
| 4855 | if (cc == lt || cc == gt) { |
| 4856 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4857 | __ b(ge, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4858 | } else { |
| 4859 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4860 | __ b(eq, &heap_number); |
| 4861 | // Comparing JS objects with <=, >= is complicated. |
| 4862 | if (cc != eq) { |
| 4863 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4864 | __ b(ge, slow); |
| 4865 | // Normally here we fall through to return_equal, but undefined is |
| 4866 | // special: (undefined == undefined) == true, but |
| 4867 | // (undefined <= undefined) == false! See ECMAScript 11.8.5. |
| 4868 | if (cc == le || cc == ge) { |
| 4869 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 4870 | __ b(ne, &return_equal); |
| 4871 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 4872 | __ cmp(r0, Operand(r2)); |
| 4873 | __ b(ne, &return_equal); |
| 4874 | if (cc == le) { |
| 4875 | // undefined <= undefined should fail. |
| 4876 | __ mov(r0, Operand(GREATER)); |
| 4877 | } else { |
| 4878 | // undefined >= undefined should fail. |
| 4879 | __ mov(r0, Operand(LESS)); |
| 4880 | } |
| 4881 | __ mov(pc, Operand(lr)); // Return. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4882 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4883 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4884 | } |
| 4885 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4886 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4887 | __ bind(&return_equal); |
| 4888 | if (cc == lt) { |
| 4889 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4890 | } else if (cc == gt) { |
| 4891 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4892 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4893 | __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4894 | } |
| 4895 | __ mov(pc, Operand(lr)); // Return. |
| 4896 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4897 | if (cc != eq || !never_nan_nan) { |
| 4898 | // For less and greater we don't have to check for NaN since the result of |
| 4899 | // x < x is false regardless. For the others here is some code to check |
| 4900 | // for NaN. |
| 4901 | if (cc != lt && cc != gt) { |
| 4902 | __ bind(&heap_number); |
| 4903 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4904 | // not NaN. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4905 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4906 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4907 | // and not all mantissa bits (0..51) clear. |
| 4908 | // Read top bits of double representation (second word of value). |
| 4909 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4910 | // Test that exponent bits are all set. |
| 4911 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4912 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4913 | __ b(ne, &return_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4914 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4915 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4916 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4917 | // Or with all low-bits of mantissa. |
| 4918 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4919 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4920 | // 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] | 4921 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if |
| 4922 | // not (it's a NaN). For <= and >= we need to load r0 with the failing |
| 4923 | // value if it's a NaN. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4924 | if (cc != eq) { |
| 4925 | // All-zero means Infinity means equal. |
| 4926 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 4927 | if (cc == le) { |
| 4928 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 4929 | } else { |
| 4930 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 4931 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4932 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4933 | __ mov(pc, Operand(lr)); // Return. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4934 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4935 | // No fall through here. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4936 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4937 | |
| 4938 | __ bind(¬_identical); |
| 4939 | } |
| 4940 | |
| 4941 | |
| 4942 | // See comment at call site. |
| 4943 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4944 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4945 | Label* slow, |
| 4946 | bool strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4947 | Label rhs_is_smi; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4948 | __ tst(r0, Operand(kSmiTagMask)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4949 | __ b(eq, &rhs_is_smi); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4950 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4951 | // 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] | 4952 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4953 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4954 | // 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] | 4955 | // succeed. Return non-equal (r0 is already not zero) |
| 4956 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4957 | } else { |
| 4958 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4959 | // the runtime. |
| 4960 | __ b(ne, slow); |
| 4961 | } |
| 4962 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4963 | // Lhs (r1) is a smi, rhs (r0) is a number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4964 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4965 | // Convert lhs to a double in d7 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4966 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4967 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 4968 | __ vmov(s15, r7); |
| 4969 | __ vcvt(d7, s15); |
| 4970 | // Load the double from rhs, tagged HeapNumber r0, to d6. |
| 4971 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 4972 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4973 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4974 | __ push(lr); |
| 4975 | // Convert lhs to a double in r2, r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4976 | __ mov(r7, Operand(r1)); |
| 4977 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4978 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4979 | // Load rhs to a double in r0, r1. |
| 4980 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4981 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4982 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4985 | // 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] | 4986 | // since it's a smi. |
| 4987 | __ jmp(lhs_not_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4988 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4989 | __ bind(&rhs_is_smi); |
| 4990 | // 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] | 4991 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4992 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4993 | // 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] | 4994 | // succeed. Return non-equal. |
| 4995 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 4996 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4997 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4998 | // 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] | 4999 | // the runtime. |
| 5000 | __ b(ne, slow); |
| 5001 | } |
| 5002 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5003 | // Rhs (r0) is a smi, lhs (r1) is a heap number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5004 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5005 | // Convert rhs to a double in d6 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5006 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5007 | // Load the double from lhs, tagged HeapNumber r1, to d7. |
| 5008 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5009 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5010 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5011 | __ vmov(s13, r7); |
| 5012 | __ vcvt(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5013 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5014 | __ push(lr); |
| 5015 | // Load lhs to a double in r2, r3. |
| 5016 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5017 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5018 | // Convert rhs to a double in r0, r1. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5019 | __ mov(r7, Operand(r0)); |
| 5020 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5021 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5022 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5023 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5024 | // Fall through to both_loaded_as_doubles. |
| 5025 | } |
| 5026 | |
| 5027 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5028 | void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5029 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5030 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5031 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5032 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5033 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5034 | Label one_is_nan, neither_is_nan; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5035 | Label lhs_not_nan_exp_mask_is_loaded; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5036 | |
| 5037 | Register exp_mask_reg = r5; |
| 5038 | |
| 5039 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5040 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 5041 | __ cmp(r4, Operand(exp_mask_reg)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5042 | __ b(ne, &lhs_not_nan_exp_mask_is_loaded); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5043 | __ mov(r4, |
| 5044 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5045 | SetCC); |
| 5046 | __ b(ne, &one_is_nan); |
| 5047 | __ cmp(lhs_mantissa, Operand(0)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5048 | __ b(ne, &one_is_nan); |
| 5049 | |
| 5050 | __ bind(lhs_not_nan); |
| 5051 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 5052 | __ bind(&lhs_not_nan_exp_mask_is_loaded); |
| 5053 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 5054 | __ cmp(r4, Operand(exp_mask_reg)); |
| 5055 | __ b(ne, &neither_is_nan); |
| 5056 | __ mov(r4, |
| 5057 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5058 | SetCC); |
| 5059 | __ b(ne, &one_is_nan); |
| 5060 | __ cmp(rhs_mantissa, Operand(0)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5061 | __ b(eq, &neither_is_nan); |
| 5062 | |
| 5063 | __ bind(&one_is_nan); |
| 5064 | // NaN comparisons always fail. |
| 5065 | // Load whatever we need in r0 to make the comparison fail. |
| 5066 | if (cc == lt || cc == le) { |
| 5067 | __ mov(r0, Operand(GREATER)); |
| 5068 | } else { |
| 5069 | __ mov(r0, Operand(LESS)); |
| 5070 | } |
| 5071 | __ mov(pc, Operand(lr)); // Return. |
| 5072 | |
| 5073 | __ bind(&neither_is_nan); |
| 5074 | } |
| 5075 | |
| 5076 | |
| 5077 | // See comment at call site. |
| 5078 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 5079 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5080 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5081 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5082 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5083 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5084 | |
| 5085 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 5086 | if (cc == eq) { |
| 5087 | // Doubles are not equal unless they have the same bit pattern. |
| 5088 | // Exception: 0 and -0. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5089 | __ cmp(rhs_mantissa, Operand(lhs_mantissa)); |
| 5090 | __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5091 | // Return non-zero if the numbers are unequal. |
| 5092 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 5093 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5094 | __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5095 | // If exponents are equal then return 0. |
| 5096 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 5097 | |
| 5098 | // Exponents are unequal. The only way we can return that the numbers |
| 5099 | // are equal is if one is -0 and the other is 0. We already dealt |
| 5100 | // with the case where both are -0 or both are 0. |
| 5101 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 5102 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 5103 | // equal. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5104 | __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5105 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 5106 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 5107 | // Now they are equal if and only if the lhs exponent is zero in its |
| 5108 | // low 31 bits. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5109 | __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5110 | __ mov(pc, Operand(lr)); |
| 5111 | } else { |
| 5112 | // Call a native function to do a comparison between two non-NaNs. |
| 5113 | // Call C routine that may not cause GC or other trouble. |
| 5114 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 5115 | __ Jump(r5); // Tail call. |
| 5116 | } |
| 5117 | } |
| 5118 | |
| 5119 | |
| 5120 | // See comment at call site. |
| 5121 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 5122 | // If either operand is a JSObject or an oddball value, then they are |
| 5123 | // not equal since their pointers are different. |
| 5124 | // There is no test for undetectability in strict equality. |
| 5125 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 5126 | Label first_non_object; |
| 5127 | // Get the type of the first operand into r2 and compare it with |
| 5128 | // FIRST_JS_OBJECT_TYPE. |
| 5129 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 5130 | __ b(lt, &first_non_object); |
| 5131 | |
| 5132 | // Return non-zero (r0 is not zero) |
| 5133 | Label return_not_equal; |
| 5134 | __ bind(&return_not_equal); |
| 5135 | __ mov(pc, Operand(lr)); // Return. |
| 5136 | |
| 5137 | __ bind(&first_non_object); |
| 5138 | // Check for oddballs: true, false, null, undefined. |
| 5139 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 5140 | __ b(eq, &return_not_equal); |
| 5141 | |
| 5142 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 5143 | __ b(ge, &return_not_equal); |
| 5144 | |
| 5145 | // Check for oddballs: true, false, null, undefined. |
| 5146 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 5147 | __ b(eq, &return_not_equal); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5148 | |
| 5149 | // Now that we have the types we might as well check for symbol-symbol. |
| 5150 | // Ensure that no non-strings have the symbol bit set. |
| 5151 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5152 | ASSERT(kSymbolTag != 0); |
| 5153 | __ and_(r2, r2, Operand(r3)); |
| 5154 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5155 | __ b(ne, &return_not_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5156 | } |
| 5157 | |
| 5158 | |
| 5159 | // See comment at call site. |
| 5160 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 5161 | Label* both_loaded_as_doubles, |
| 5162 | Label* not_heap_numbers, |
| 5163 | Label* slow) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5164 | __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5165 | __ b(ne, not_heap_numbers); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5166 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5167 | __ cmp(r2, r3); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5168 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 5169 | |
| 5170 | // Both are heap numbers. Load them up then jump to the code we have |
| 5171 | // for that. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5172 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5173 | CpuFeatures::Scope scope(VFP3); |
| 5174 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5175 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5176 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5177 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5178 | } else { |
| 5179 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5180 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5181 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5182 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5183 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5184 | __ jmp(both_loaded_as_doubles); |
| 5185 | } |
| 5186 | |
| 5187 | |
| 5188 | // Fast negative check for symbol-to-symbol equality. |
| 5189 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 5190 | // r2 is object type of r0. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5191 | // Ensure that no non-strings have the symbol bit set. |
| 5192 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5193 | ASSERT(kSymbolTag != 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5194 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5195 | __ b(eq, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5196 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5197 | __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5198 | __ tst(r3, Operand(kIsSymbolMask)); |
| 5199 | __ b(eq, slow); |
| 5200 | |
| 5201 | // Both are symbols. We already checked they weren't the same pointer |
| 5202 | // so they are not equal. |
| 5203 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 5204 | __ mov(pc, Operand(lr)); // Return. |
| 5205 | } |
| 5206 | |
| 5207 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5208 | // On entry r0 (rhs) and r1 (lhs) are the values to be compared. |
| 5209 | // On exit r0 is 0, positive or negative to indicate the result of |
| 5210 | // the comparison. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5211 | void CompareStub::Generate(MacroAssembler* masm) { |
| 5212 | Label slow; // Call builtin. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5213 | Label not_smis, both_loaded_as_doubles, lhs_not_nan; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5214 | |
| 5215 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 5216 | // it is certain that at least one operand isn't a smi. |
| 5217 | |
| 5218 | // Handle the case where the objects are identical. Either returns the answer |
| 5219 | // 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] | 5220 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5221 | |
| 5222 | // If either is a Smi (we know that not both are), then they can only |
| 5223 | // be strictly equal if the other is a HeapNumber. |
| 5224 | ASSERT_EQ(0, kSmiTag); |
| 5225 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 5226 | __ and_(r2, r0, Operand(r1)); |
| 5227 | __ tst(r2, Operand(kSmiTagMask)); |
| 5228 | __ b(ne, ¬_smis); |
| 5229 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 5230 | // 1) Return the answer. |
| 5231 | // 2) Go to slow. |
| 5232 | // 3) Fall through to both_loaded_as_doubles. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5233 | // 4) Jump to lhs_not_nan. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5234 | // 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] | 5235 | // comparison. If VFP3 is supported the double values of the numbers have |
| 5236 | // been loaded into d7 and d6. Otherwise, the double values have been loaded |
| 5237 | // into r0, r1, r2, and r3. |
| 5238 | EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5239 | |
| 5240 | __ bind(&both_loaded_as_doubles); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5241 | // The arguments have been converted to doubles and stored in d6 and d7, if |
| 5242 | // VFP3 is supported, or in r0, r1, r2, and r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5243 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5244 | __ bind(&lhs_not_nan); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5245 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5246 | Label no_nan; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5247 | // ARMv7 VFP3 instructions to implement double precision comparison. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5248 | __ vcmp(d7, d6); |
| 5249 | __ vmrs(pc); // Move vector status bits to normal status bits. |
| 5250 | Label nan; |
| 5251 | __ b(vs, &nan); |
| 5252 | __ mov(r0, Operand(EQUAL), LeaveCC, eq); |
| 5253 | __ mov(r0, Operand(LESS), LeaveCC, lt); |
| 5254 | __ mov(r0, Operand(GREATER), LeaveCC, gt); |
| 5255 | __ mov(pc, Operand(lr)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5256 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5257 | __ bind(&nan); |
| 5258 | // If one of the sides was a NaN then the v flag is set. Load r0 with |
| 5259 | // whatever it takes to make the comparison fail, since comparisons with NaN |
| 5260 | // always fail. |
| 5261 | if (cc_ == lt || cc_ == le) { |
| 5262 | __ mov(r0, Operand(GREATER)); |
| 5263 | } else { |
| 5264 | __ mov(r0, Operand(LESS)); |
| 5265 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5266 | __ mov(pc, Operand(lr)); |
| 5267 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5268 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 5269 | // fall through if neither is a NaN. Also binds lhs_not_nan. |
| 5270 | EmitNanCheck(masm, &lhs_not_nan, cc_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5271 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 5272 | // answer. Never falls through. |
| 5273 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 5274 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5275 | |
| 5276 | __ bind(¬_smis); |
| 5277 | // At this point we know we are dealing with two different objects, |
| 5278 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 5279 | if (strict_) { |
| 5280 | // This returns non-equal for some object types, or falls through if it |
| 5281 | // was not lucky. |
| 5282 | EmitStrictTwoHeapObjectCompare(masm); |
| 5283 | } |
| 5284 | |
| 5285 | Label check_for_symbols; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5286 | Label flat_string_check; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5287 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 5288 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 5289 | // 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] | 5290 | // 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] | 5291 | EmitCheckForTwoHeapNumbers(masm, |
| 5292 | &both_loaded_as_doubles, |
| 5293 | &check_for_symbols, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5294 | &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5295 | |
| 5296 | __ bind(&check_for_symbols); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5297 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 5298 | // symbols. |
| 5299 | if (cc_ == eq && !strict_) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5300 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5301 | // of r0 on entry. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5302 | EmitCheckForSymbols(masm, &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5305 | // Check for both being sequential ASCII strings, and inline if that is the |
| 5306 | // case. |
| 5307 | __ bind(&flat_string_check); |
| 5308 | |
| 5309 | __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow); |
| 5310 | |
| 5311 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 5312 | StringCompareStub::GenerateCompareFlatAsciiStrings(masm, |
| 5313 | r1, |
| 5314 | r0, |
| 5315 | r2, |
| 5316 | r3, |
| 5317 | r4, |
| 5318 | r5); |
| 5319 | // Never falls through to here. |
| 5320 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5321 | __ bind(&slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5322 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5323 | __ push(r1); |
| 5324 | __ push(r0); |
| 5325 | // Figure out which native to call and setup the arguments. |
| 5326 | Builtins::JavaScript native; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5327 | if (cc_ == eq) { |
| 5328 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 5329 | } else { |
| 5330 | native = Builtins::COMPARE; |
| 5331 | int ncr; // NaN compare result |
| 5332 | if (cc_ == lt || cc_ == le) { |
| 5333 | ncr = GREATER; |
| 5334 | } else { |
| 5335 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 5336 | ncr = LESS; |
| 5337 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5338 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 5339 | __ push(r0); |
| 5340 | } |
| 5341 | |
| 5342 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 5343 | // tagged as a small integer. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5344 | __ InvokeBuiltin(native, JUMP_JS); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
| 5347 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5348 | // Allocates a heap number or jumps to the label if the young space is full and |
| 5349 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5350 | static void AllocateHeapNumber( |
| 5351 | MacroAssembler* masm, |
| 5352 | Label* need_gc, // Jump here if young space is full. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5353 | Register result, // The tagged address of the new heap number. |
| 5354 | Register scratch1, // A scratch register. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5355 | Register scratch2) { // Another scratch register. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5356 | // Allocate an object in the heap for the heap number and tag it as a heap |
| 5357 | // object. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5358 | __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize, |
| 5359 | result, |
| 5360 | scratch1, |
| 5361 | scratch2, |
| 5362 | need_gc, |
| 5363 | TAG_OBJECT); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5364 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5365 | // Get heap number map and store it in the allocated object. |
| 5366 | __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex); |
| 5367 | __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | |
| 5371 | // We fall into this code if the operands were Smis, but the result was |
| 5372 | // 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] | 5373 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 5374 | // to call the C-implemented binary fp operation routines we need to end up |
| 5375 | // with the double precision floating point operands in r0 and r1 (for the |
| 5376 | // value in r1) and r2 and r3 (for the value in r0). |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5377 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 5378 | Label* not_smi, |
| 5379 | const Builtins::JavaScript& builtin, |
| 5380 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5381 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5382 | Label slow, slow_pop_2_first, do_the_call; |
| 5383 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 5384 | // Smi-smi case (overflow). |
| 5385 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 5386 | // The new heap number is in r5. r6 and r7 are scratch. |
| 5387 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5388 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5389 | // If we have floating point hardware, inline ADD, SUB, MUL, and DIV, |
| 5390 | // using registers d7 and d6 for the double values. |
| 5391 | bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && |
| 5392 | Token::MOD != operation; |
| 5393 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5394 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5395 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5396 | __ vmov(s15, r7); |
| 5397 | __ vcvt(d7, s15); |
| 5398 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5399 | __ vmov(s13, r7); |
| 5400 | __ vcvt(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5401 | } else { |
| 5402 | // Write Smi from r0 to r3 and r2 in double format. r6 is scratch. |
| 5403 | __ mov(r7, Operand(r0)); |
| 5404 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5405 | __ push(lr); |
| 5406 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 5407 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 5408 | __ mov(r7, Operand(r1)); |
| 5409 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5410 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 5411 | __ pop(lr); |
| 5412 | } |
| 5413 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5414 | __ jmp(&do_the_call); // Tail call. No return. |
| 5415 | |
| 5416 | // We jump to here if something goes wrong (one param is not a number of any |
| 5417 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5418 | __ bind(&slow); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5419 | |
| 5420 | // Push arguments to the stack |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5421 | __ push(r1); |
| 5422 | __ push(r0); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5423 | |
| 5424 | if (Token::ADD == operation) { |
| 5425 | // Test for string arguments before calling runtime. |
| 5426 | // r1 : first argument |
| 5427 | // r0 : second argument |
| 5428 | // sp[0] : second argument |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5429 | // sp[4] : first argument |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5430 | |
| 5431 | Label not_strings, not_string1, string1; |
| 5432 | __ tst(r1, Operand(kSmiTagMask)); |
| 5433 | __ b(eq, ¬_string1); |
| 5434 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 5435 | __ b(ge, ¬_string1); |
| 5436 | |
| 5437 | // First argument is a a string, test second. |
| 5438 | __ tst(r0, Operand(kSmiTagMask)); |
| 5439 | __ b(eq, &string1); |
| 5440 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5441 | __ b(ge, &string1); |
| 5442 | |
| 5443 | // First and second argument are strings. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5444 | StringAddStub stub(NO_STRING_CHECK_IN_STUB); |
| 5445 | __ TailCallStub(&stub); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5446 | |
| 5447 | // Only first argument is a string. |
| 5448 | __ bind(&string1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5449 | __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS); |
| 5450 | |
| 5451 | // First argument was not a string, test second. |
| 5452 | __ bind(¬_string1); |
| 5453 | __ tst(r0, Operand(kSmiTagMask)); |
| 5454 | __ b(eq, ¬_strings); |
| 5455 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5456 | __ b(ge, ¬_strings); |
| 5457 | |
| 5458 | // Only second argument is a string. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5459 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 5460 | |
| 5461 | __ bind(¬_strings); |
| 5462 | } |
| 5463 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5464 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5465 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5466 | // We branch here if at least one of r0 and r1 is not a Smi. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5467 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5468 | if (mode == NO_OVERWRITE) { |
| 5469 | // In the case where there is no chance of an overwritable float we may as |
| 5470 | // well do the allocation immediately while r0 and r1 are untouched. |
| 5471 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5472 | } |
| 5473 | |
| 5474 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5475 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5476 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5477 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5478 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5479 | if (mode == OVERWRITE_RIGHT) { |
| 5480 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5481 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5482 | if (use_fp_registers) { |
| 5483 | CpuFeatures::Scope scope(VFP3); |
| 5484 | // Load the double from tagged HeapNumber r0 to d7. |
| 5485 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5486 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5487 | } else { |
| 5488 | // Calling convention says that second double is in r2 and r3. |
| 5489 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5490 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5491 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5492 | __ jmp(&finished_loading_r0); |
| 5493 | __ bind(&r0_is_smi); |
| 5494 | if (mode == OVERWRITE_RIGHT) { |
| 5495 | // We can't overwrite a Smi so get address of new heap number into r5. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5496 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5497 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5498 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5499 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5500 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5501 | // Convert smi in r0 to double in d7. |
| 5502 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5503 | __ vmov(s15, r7); |
| 5504 | __ vcvt(d7, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5505 | } else { |
| 5506 | // Write Smi from r0 to r3 and r2 in double format. |
| 5507 | __ mov(r7, Operand(r0)); |
| 5508 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5509 | __ push(lr); |
| 5510 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5511 | __ pop(lr); |
| 5512 | } |
| 5513 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5514 | __ bind(&finished_loading_r0); |
| 5515 | |
| 5516 | // Move r1 to a double in r0-r1. |
| 5517 | __ tst(r1, Operand(kSmiTagMask)); |
| 5518 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5519 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5520 | __ b(ne, &slow); |
| 5521 | if (mode == OVERWRITE_LEFT) { |
| 5522 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5523 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5524 | if (use_fp_registers) { |
| 5525 | CpuFeatures::Scope scope(VFP3); |
| 5526 | // Load the double from tagged HeapNumber r1 to d6. |
| 5527 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5528 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5529 | } else { |
| 5530 | // Calling convention says that first double is in r0 and r1. |
| 5531 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5532 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5533 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5534 | __ jmp(&finished_loading_r1); |
| 5535 | __ bind(&r1_is_smi); |
| 5536 | if (mode == OVERWRITE_LEFT) { |
| 5537 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5538 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5539 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5540 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5541 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5542 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5543 | // Convert smi in r1 to double in d6. |
| 5544 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5545 | __ vmov(s13, r7); |
| 5546 | __ vcvt(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5547 | } else { |
| 5548 | // Write Smi from r1 to r1 and r0 in double format. |
| 5549 | __ mov(r7, Operand(r1)); |
| 5550 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5551 | __ push(lr); |
| 5552 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5553 | __ pop(lr); |
| 5554 | } |
| 5555 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5556 | __ bind(&finished_loading_r1); |
| 5557 | |
| 5558 | __ bind(&do_the_call); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5559 | // If we are inlining the operation using VFP3 instructions for |
| 5560 | // add, subtract, multiply, or divide, the arguments are in d6 and d7. |
| 5561 | if (use_fp_registers) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5562 | CpuFeatures::Scope scope(VFP3); |
| 5563 | // ARMv7 VFP3 instructions to implement |
| 5564 | // double precision, add, subtract, multiply, divide. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5565 | |
| 5566 | if (Token::MUL == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5567 | __ vmul(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5568 | } else if (Token::DIV == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5569 | __ vdiv(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5570 | } else if (Token::ADD == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5571 | __ vadd(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5572 | } else if (Token::SUB == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5573 | __ vsub(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5574 | } else { |
| 5575 | UNREACHABLE(); |
| 5576 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5577 | __ sub(r0, r5, Operand(kHeapObjectTag)); |
| 5578 | __ vstr(d5, r0, HeapNumber::kValueOffset); |
| 5579 | __ add(r0, r0, Operand(kHeapObjectTag)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5580 | __ mov(pc, lr); |
| 5581 | return; |
| 5582 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5583 | |
| 5584 | // If we did not inline the operation, then the arguments are in: |
| 5585 | // r0: Left value (least significant part of mantissa). |
| 5586 | // r1: Left value (sign, exponent, top of mantissa). |
| 5587 | // r2: Right value (least significant part of mantissa). |
| 5588 | // r3: Right value (sign, exponent, top of mantissa). |
| 5589 | // r5: Address of heap number for result. |
| 5590 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5591 | __ push(lr); // For later. |
| 5592 | __ push(r5); // Address of heap number that is answer. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5593 | __ AlignStack(0); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5594 | // Call C routine that may not cause GC or other trouble. |
| 5595 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5596 | __ Call(r5); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5597 | __ pop(r4); // Address of heap number. |
| 5598 | __ cmp(r4, Operand(Smi::FromInt(0))); |
| 5599 | __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5600 | // Store answer in the overwritable heap number. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5601 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5602 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5603 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5604 | // substract the tag from r4. |
| 5605 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5606 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5607 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5608 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5609 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5610 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5611 | #endif |
| 5612 | __ mov(r0, Operand(r4)); |
| 5613 | // And we are done. |
| 5614 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5618 | // 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] | 5619 | // 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] | 5620 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5621 | // 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] | 5622 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5623 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5624 | static void GetInt32(MacroAssembler* masm, |
| 5625 | Register source, |
| 5626 | Register dest, |
| 5627 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5628 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5629 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5630 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5631 | // Get exponent word. |
| 5632 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5633 | // Get exponent alone in scratch2. |
| 5634 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5635 | // Load dest with zero. We use this either for the final shift or |
| 5636 | // for the answer. |
| 5637 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5638 | // 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] | 5639 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5640 | // the exponent that we are fastest at and also the highest exponent we can |
| 5641 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5642 | const uint32_t non_smi_exponent = |
| 5643 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5644 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5645 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5646 | __ b(eq, &right_exponent); |
| 5647 | // If the exponent is higher than that then go to slow case. This catches |
| 5648 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5649 | __ b(gt, slow); |
| 5650 | |
| 5651 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5652 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5653 | // it rounds to zero. |
| 5654 | const uint32_t zero_exponent = |
| 5655 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5656 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5657 | // Dest already has a Smi zero. |
| 5658 | __ b(lt, &done); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5659 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 5660 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5661 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5662 | // We now have the exponent in dest. Subtract from 30 to get |
| 5663 | // how much to shift down. |
| 5664 | __ rsb(dest, dest, Operand(30)); |
| 5665 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5666 | __ bind(&right_exponent); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5667 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5668 | CpuFeatures::Scope scope(VFP3); |
| 5669 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 5670 | // conversion using round to zero. |
| 5671 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5672 | __ vmov(d7, scratch2, scratch); |
| 5673 | __ vcvt(s15, d7); |
| 5674 | __ vmov(dest, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5675 | } else { |
| 5676 | // Get the top bits of the mantissa. |
| 5677 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5678 | // Put back the implicit 1. |
| 5679 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5680 | // Shift up the mantissa bits to take up the space the exponent used to |
| 5681 | // take. We just orred in the implicit bit so that took care of one and |
| 5682 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 5683 | // distance. |
| 5684 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5685 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5686 | // Put sign in zero flag. |
| 5687 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 5688 | // Get the second half of the double. For some exponents we don't |
| 5689 | // actually need this because the bits get shifted out again, but |
| 5690 | // it's probably slower to test than just to do it. |
| 5691 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5692 | // Shift down 22 bits to get the last 10 bits. |
| 5693 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5694 | // Move down according to the exponent. |
| 5695 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 5696 | // Fix sign if sign bit was set. |
| 5697 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 5698 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5699 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5702 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5703 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5704 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5705 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5706 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5707 | // write the answer into (either by allocating or by overwriting). |
| 5708 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5709 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5710 | Label slow, result_not_a_smi; |
| 5711 | Label r0_is_smi, r1_is_smi; |
| 5712 | Label done_checking_r0, done_checking_r1; |
| 5713 | |
| 5714 | __ tst(r1, Operand(kSmiTagMask)); |
| 5715 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5716 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5717 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5718 | GetInt32(masm, r1, r3, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5719 | __ jmp(&done_checking_r1); |
| 5720 | __ bind(&r1_is_smi); |
| 5721 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5722 | __ bind(&done_checking_r1); |
| 5723 | |
| 5724 | __ tst(r0, Operand(kSmiTagMask)); |
| 5725 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5726 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5727 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5728 | GetInt32(masm, r0, r2, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5729 | __ jmp(&done_checking_r0); |
| 5730 | __ bind(&r0_is_smi); |
| 5731 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5732 | __ bind(&done_checking_r0); |
| 5733 | |
| 5734 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5735 | // r2 and r3: Signed int32 operands. |
| 5736 | switch (op_) { |
| 5737 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5738 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5739 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5740 | case Token::SAR: |
| 5741 | // Use only the 5 least significant bits of the shift count. |
| 5742 | __ and_(r2, r2, Operand(0x1f)); |
| 5743 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5744 | break; |
| 5745 | case Token::SHR: |
| 5746 | // Use only the 5 least significant bits of the shift count. |
| 5747 | __ and_(r2, r2, Operand(0x1f)); |
| 5748 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5749 | // SHR is special because it is required to produce a positive answer. |
| 5750 | // The code below for writing into heap numbers isn't capable of writing |
| 5751 | // the register as an unsigned int so we go to slow case if we hit this |
| 5752 | // case. |
| 5753 | __ b(mi, &slow); |
| 5754 | break; |
| 5755 | case Token::SHL: |
| 5756 | // Use only the 5 least significant bits of the shift count. |
| 5757 | __ and_(r2, r2, Operand(0x1f)); |
| 5758 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5759 | break; |
| 5760 | default: UNREACHABLE(); |
| 5761 | } |
| 5762 | // check that the *signed* result fits in a smi |
| 5763 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5764 | __ b(mi, &result_not_a_smi); |
| 5765 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5766 | __ Ret(); |
| 5767 | |
| 5768 | Label have_to_allocate, got_a_heap_number; |
| 5769 | __ bind(&result_not_a_smi); |
| 5770 | switch (mode_) { |
| 5771 | case OVERWRITE_RIGHT: { |
| 5772 | __ tst(r0, Operand(kSmiTagMask)); |
| 5773 | __ b(eq, &have_to_allocate); |
| 5774 | __ mov(r5, Operand(r0)); |
| 5775 | break; |
| 5776 | } |
| 5777 | case OVERWRITE_LEFT: { |
| 5778 | __ tst(r1, Operand(kSmiTagMask)); |
| 5779 | __ b(eq, &have_to_allocate); |
| 5780 | __ mov(r5, Operand(r1)); |
| 5781 | break; |
| 5782 | } |
| 5783 | case NO_OVERWRITE: { |
| 5784 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5785 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5786 | } |
| 5787 | default: break; |
| 5788 | } |
| 5789 | __ bind(&got_a_heap_number); |
| 5790 | // r2: Answer as signed int32. |
| 5791 | // r5: Heap number to write answer into. |
| 5792 | |
| 5793 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5794 | // result. |
| 5795 | __ mov(r0, Operand(r5)); |
| 5796 | |
| 5797 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5798 | // r3 as scratch. r0 is preserved and returned. |
| 5799 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5800 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5801 | |
| 5802 | if (mode_ != NO_OVERWRITE) { |
| 5803 | __ bind(&have_to_allocate); |
| 5804 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5805 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5806 | __ jmp(&got_a_heap_number); |
| 5807 | } |
| 5808 | |
| 5809 | // If all else failed then we go to the runtime system. |
| 5810 | __ bind(&slow); |
| 5811 | __ push(r1); // restore stack |
| 5812 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5813 | switch (op_) { |
| 5814 | case Token::BIT_OR: |
| 5815 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5816 | break; |
| 5817 | case Token::BIT_AND: |
| 5818 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5819 | break; |
| 5820 | case Token::BIT_XOR: |
| 5821 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5822 | break; |
| 5823 | case Token::SAR: |
| 5824 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5825 | break; |
| 5826 | case Token::SHR: |
| 5827 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5828 | break; |
| 5829 | case Token::SHL: |
| 5830 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5831 | break; |
| 5832 | default: |
| 5833 | UNREACHABLE(); |
| 5834 | } |
| 5835 | } |
| 5836 | |
| 5837 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5838 | // Can we multiply by x with max two shifts and an add. |
| 5839 | // This answers yes to all integers from 2 to 10. |
| 5840 | static bool IsEasyToMultiplyBy(int x) { |
| 5841 | if (x < 2) return false; // Avoid special cases. |
| 5842 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 5843 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 5844 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 5845 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 5846 | return false; |
| 5847 | } |
| 5848 | |
| 5849 | |
| 5850 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 5851 | // Source and destination may be the same register. This routine does |
| 5852 | // not set carry and overflow the way a mul instruction would. |
| 5853 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 5854 | Register source, |
| 5855 | Register destination, |
| 5856 | int known_int) { |
| 5857 | if (IsPowerOf2(known_int)) { |
| 5858 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 5859 | } else if (PopCountLessThanEqual2(known_int)) { |
| 5860 | int first_bit = BitPosition(known_int); |
| 5861 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 5862 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 5863 | if (first_bit != 0) { |
| 5864 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 5865 | } |
| 5866 | } else { |
| 5867 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 5868 | int the_bit = BitPosition(known_int + 1); |
| 5869 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 5870 | } |
| 5871 | } |
| 5872 | |
| 5873 | |
| 5874 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 5875 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 5876 | // a result that needs shifting. |
| 5877 | static void MultiplyByKnownInt2( |
| 5878 | MacroAssembler* masm, |
| 5879 | Register result, |
| 5880 | Register source, |
| 5881 | Register known_int_register, // Smi tagged. |
| 5882 | int known_int, |
| 5883 | int* required_shift) { // Including Smi tag shift |
| 5884 | switch (known_int) { |
| 5885 | case 3: |
| 5886 | __ add(result, source, Operand(source, LSL, 1)); |
| 5887 | *required_shift = 1; |
| 5888 | break; |
| 5889 | case 5: |
| 5890 | __ add(result, source, Operand(source, LSL, 2)); |
| 5891 | *required_shift = 1; |
| 5892 | break; |
| 5893 | case 6: |
| 5894 | __ add(result, source, Operand(source, LSL, 1)); |
| 5895 | *required_shift = 2; |
| 5896 | break; |
| 5897 | case 7: |
| 5898 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 5899 | *required_shift = 1; |
| 5900 | break; |
| 5901 | case 9: |
| 5902 | __ add(result, source, Operand(source, LSL, 3)); |
| 5903 | *required_shift = 1; |
| 5904 | break; |
| 5905 | case 10: |
| 5906 | __ add(result, source, Operand(source, LSL, 2)); |
| 5907 | *required_shift = 2; |
| 5908 | break; |
| 5909 | default: |
| 5910 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 5911 | __ mul(result, source, known_int_register); |
| 5912 | *required_shift = 0; |
| 5913 | } |
| 5914 | } |
| 5915 | |
| 5916 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5917 | const char* GenericBinaryOpStub::GetName() { |
| 5918 | if (name_ != NULL) return name_; |
| 5919 | const int len = 100; |
| 5920 | name_ = Bootstrapper::AllocateAutoDeletedArray(len); |
| 5921 | if (name_ == NULL) return "OOM"; |
| 5922 | const char* op_name = Token::Name(op_); |
| 5923 | const char* overwrite_name; |
| 5924 | switch (mode_) { |
| 5925 | case NO_OVERWRITE: overwrite_name = "Alloc"; break; |
| 5926 | case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; |
| 5927 | case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; |
| 5928 | default: overwrite_name = "UnknownOverwrite"; break; |
| 5929 | } |
| 5930 | |
| 5931 | OS::SNPrintF(Vector<char>(name_, len), |
| 5932 | "GenericBinaryOpStub_%s_%s%s", |
| 5933 | op_name, |
| 5934 | overwrite_name, |
| 5935 | specialized_on_rhs_ ? "_ConstantRhs" : 0); |
| 5936 | return name_; |
| 5937 | } |
| 5938 | |
| 5939 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5940 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5941 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5942 | // r1 : x |
| 5943 | // r0 : y |
| 5944 | // result : r0 |
| 5945 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5946 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 5947 | // tell us that. |
| 5948 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 5949 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5950 | switch (op_) { |
| 5951 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5952 | Label not_smi; |
| 5953 | // Fast path. |
| 5954 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5955 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5956 | __ b(ne, ¬_smi); |
| 5957 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 5958 | // Return if no overflow. |
| 5959 | __ Ret(vc); |
| 5960 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 5961 | |
| 5962 | HandleBinaryOpSlowCases(masm, |
| 5963 | ¬_smi, |
| 5964 | Builtins::ADD, |
| 5965 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5966 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5967 | break; |
| 5968 | } |
| 5969 | |
| 5970 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5971 | Label not_smi; |
| 5972 | // Fast path. |
| 5973 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5974 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5975 | __ b(ne, ¬_smi); |
| 5976 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 5977 | // Return if no overflow. |
| 5978 | __ Ret(vc); |
| 5979 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 5980 | |
| 5981 | HandleBinaryOpSlowCases(masm, |
| 5982 | ¬_smi, |
| 5983 | Builtins::SUB, |
| 5984 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5985 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5986 | break; |
| 5987 | } |
| 5988 | |
| 5989 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5990 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5991 | ASSERT(kSmiTag == 0); // adjust code below |
| 5992 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5993 | __ b(ne, ¬_smi); |
| 5994 | // Remove tag from one operand (but keep sign), so that result is Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5995 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5996 | // Do multiplication |
| 5997 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 5998 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5999 | __ mov(ip, Operand(r3, ASR, 31)); |
| 6000 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 6001 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6002 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6003 | __ tst(r3, Operand(r3)); |
| 6004 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6005 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6006 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 6007 | // We know one of them was zero. |
| 6008 | __ add(r2, r0, Operand(r1), SetCC); |
| 6009 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 6010 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 6011 | // Slow case. We fall through here if we multiplied a negative number |
| 6012 | // with 0, because that would mean we should produce -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6013 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6014 | |
| 6015 | HandleBinaryOpSlowCases(masm, |
| 6016 | ¬_smi, |
| 6017 | Builtins::MUL, |
| 6018 | Token::MUL, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6019 | mode_); |
| 6020 | break; |
| 6021 | } |
| 6022 | |
| 6023 | case Token::DIV: |
| 6024 | case Token::MOD: { |
| 6025 | Label not_smi; |
| 6026 | if (specialized_on_rhs_) { |
| 6027 | Label smi_is_unsuitable; |
| 6028 | __ BranchOnNotSmi(r1, ¬_smi); |
| 6029 | if (IsPowerOf2(constant_rhs_)) { |
| 6030 | if (op_ == Token::MOD) { |
| 6031 | __ and_(r0, |
| 6032 | r1, |
| 6033 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 6034 | SetCC); |
| 6035 | // We now have the answer, but if the input was negative we also |
| 6036 | // have the sign bit. Our work is done if the result is |
| 6037 | // positive or zero: |
| 6038 | __ Ret(pl); |
| 6039 | // A mod of a negative left hand side must return a negative number. |
| 6040 | // Unfortunately if the answer is 0 then we must return -0. And we |
| 6041 | // already optimistically trashed r0 so we may need to restore it. |
| 6042 | __ eor(r0, r0, Operand(0x80000000u), SetCC); |
| 6043 | // Next two instructions are conditional on the answer being -0. |
| 6044 | __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
| 6045 | __ b(eq, &smi_is_unsuitable); |
| 6046 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
| 6047 | __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_))); |
| 6048 | } else { |
| 6049 | ASSERT(op_ == Token::DIV); |
| 6050 | __ tst(r1, |
| 6051 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 6052 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 6053 | int shift = 0; |
| 6054 | int d = constant_rhs_; |
| 6055 | while ((d & 1) == 0) { |
| 6056 | d >>= 1; |
| 6057 | shift++; |
| 6058 | } |
| 6059 | __ mov(r0, Operand(r1, LSR, shift)); |
| 6060 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 6061 | } |
| 6062 | } else { |
| 6063 | // Not a power of 2. |
| 6064 | __ tst(r1, Operand(0x80000000u)); |
| 6065 | __ b(ne, &smi_is_unsuitable); |
| 6066 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 6067 | // multiplying. |
| 6068 | double divisor = 1.0 / constant_rhs_; |
| 6069 | int shift = 32; |
| 6070 | double scale = 4294967296.0; // 1 << 32. |
| 6071 | uint32_t mul; |
| 6072 | // Maximise the precision of the fixed point reciprocal. |
| 6073 | while (true) { |
| 6074 | mul = static_cast<uint32_t>(scale * divisor); |
| 6075 | if (mul >= 0x7fffffff) break; |
| 6076 | scale *= 2.0; |
| 6077 | shift++; |
| 6078 | } |
| 6079 | mul++; |
| 6080 | __ mov(r2, Operand(mul)); |
| 6081 | __ umull(r3, r2, r2, r1); |
| 6082 | __ mov(r2, Operand(r2, LSR, shift - 31)); |
| 6083 | // r2 is r1 / rhs. r2 is not Smi tagged. |
| 6084 | // r0 is still the known rhs. r0 is Smi tagged. |
| 6085 | // r1 is still the unkown lhs. r1 is Smi tagged. |
| 6086 | int required_r4_shift = 0; // Including the Smi tag shift of 1. |
| 6087 | // r4 = r2 * r0. |
| 6088 | MultiplyByKnownInt2(masm, |
| 6089 | r4, |
| 6090 | r2, |
| 6091 | r0, |
| 6092 | constant_rhs_, |
| 6093 | &required_r4_shift); |
| 6094 | // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs). |
| 6095 | if (op_ == Token::DIV) { |
| 6096 | __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC); |
| 6097 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
| 6098 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 6099 | } else { |
| 6100 | ASSERT(op_ == Token::MOD); |
| 6101 | __ sub(r0, r1, Operand(r4, LSL, required_r4_shift)); |
| 6102 | } |
| 6103 | } |
| 6104 | __ Ret(); |
| 6105 | __ bind(&smi_is_unsuitable); |
| 6106 | } else { |
| 6107 | __ jmp(¬_smi); |
| 6108 | } |
| 6109 | HandleBinaryOpSlowCases(masm, |
| 6110 | ¬_smi, |
| 6111 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV, |
| 6112 | op_, |
| 6113 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6114 | break; |
| 6115 | } |
| 6116 | |
| 6117 | case Token::BIT_OR: |
| 6118 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6119 | case Token::BIT_XOR: |
| 6120 | case Token::SAR: |
| 6121 | case Token::SHR: |
| 6122 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6123 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6124 | ASSERT(kSmiTag == 0); // adjust code below |
| 6125 | __ tst(r2, Operand(kSmiTagMask)); |
| 6126 | __ b(ne, &slow); |
| 6127 | switch (op_) { |
| 6128 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 6129 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 6130 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6131 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6132 | // Remove tags from right operand. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6133 | __ GetLeastBitsFromSmi(r2, r0, 5); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6134 | __ mov(r0, Operand(r1, ASR, r2)); |
| 6135 | // Smi tag result. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6136 | __ bic(r0, r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6137 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6138 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6139 | // Remove tags from operands. We can't do this on a 31 bit number |
| 6140 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 6141 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6142 | __ GetLeastBitsFromSmi(r2, r0, 5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6143 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6144 | // Unsigned shift is not allowed to produce a negative number, so |
| 6145 | // check the sign bit and the sign bit after Smi tagging. |
| 6146 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6147 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6148 | // Smi tag result. |
| 6149 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6150 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6151 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6152 | // Remove tags from operands. |
| 6153 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6154 | __ GetLeastBitsFromSmi(r2, r0, 5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6155 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6156 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6157 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 6158 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6159 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6160 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6161 | default: UNREACHABLE(); |
| 6162 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6163 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6164 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6165 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6166 | break; |
| 6167 | } |
| 6168 | |
| 6169 | default: UNREACHABLE(); |
| 6170 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6171 | // This code should be unreachable. |
| 6172 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6173 | } |
| 6174 | |
| 6175 | |
| 6176 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 6177 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 6178 | // argument, so give it a Smi. |
| 6179 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6180 | __ push(r0); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6181 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6182 | |
| 6183 | __ StubReturn(1); |
| 6184 | } |
| 6185 | |
| 6186 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6187 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6188 | Label slow, done; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6189 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6190 | if (op_ == Token::SUB) { |
| 6191 | // Check whether the value is a smi. |
| 6192 | Label try_float; |
| 6193 | __ tst(r0, Operand(kSmiTagMask)); |
| 6194 | __ b(ne, &try_float); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6195 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6196 | // Go slow case if the value of the expression is zero |
| 6197 | // to make sure that we switch between 0 and -0. |
| 6198 | __ cmp(r0, Operand(0)); |
| 6199 | __ b(eq, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6200 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6201 | // The value of the expression is a smi that is not zero. Try |
| 6202 | // optimistic subtraction '0 - value'. |
| 6203 | __ rsb(r1, r0, Operand(0), SetCC); |
| 6204 | __ b(vs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6205 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6206 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 6207 | __ b(&done); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6208 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6209 | __ bind(&try_float); |
| 6210 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6211 | __ b(ne, &slow); |
| 6212 | // r0 is a heap number. Get a new heap number in r1. |
| 6213 | if (overwrite_) { |
| 6214 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6215 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6216 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6217 | } else { |
| 6218 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
| 6219 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 6220 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6221 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 6222 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6223 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 6224 | __ mov(r0, Operand(r1)); |
| 6225 | } |
| 6226 | } else if (op_ == Token::BIT_NOT) { |
| 6227 | // Check if the operand is a heap number. |
| 6228 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6229 | __ b(ne, &slow); |
| 6230 | |
| 6231 | // Convert the heap number is r0 to an untagged integer in r1. |
| 6232 | GetInt32(masm, r0, r1, r2, r3, &slow); |
| 6233 | |
| 6234 | // Do the bitwise operation (move negated) and check if the result |
| 6235 | // fits in a smi. |
| 6236 | Label try_float; |
| 6237 | __ mvn(r1, Operand(r1)); |
| 6238 | __ add(r2, r1, Operand(0x40000000), SetCC); |
| 6239 | __ b(mi, &try_float); |
| 6240 | __ mov(r0, Operand(r1, LSL, kSmiTagSize)); |
| 6241 | __ b(&done); |
| 6242 | |
| 6243 | __ bind(&try_float); |
| 6244 | if (!overwrite_) { |
| 6245 | // Allocate a fresh heap number, but don't overwrite r0 until |
| 6246 | // we're sure we can do it without going through the slow case |
| 6247 | // that needs the value in r0. |
| 6248 | AllocateHeapNumber(masm, &slow, r2, r3, r4); |
| 6249 | __ mov(r0, Operand(r2)); |
| 6250 | } |
| 6251 | |
| 6252 | // WriteInt32ToHeapNumberStub does not trigger GC, so we do not |
| 6253 | // have to set up a frame. |
| 6254 | WriteInt32ToHeapNumberStub stub(r1, r0, r2); |
| 6255 | __ push(lr); |
| 6256 | __ Call(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 6257 | __ pop(lr); |
| 6258 | } else { |
| 6259 | UNIMPLEMENTED(); |
| 6260 | } |
| 6261 | |
| 6262 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6263 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6264 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6265 | // Handle the slow case by jumping to the JavaScript builtin. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6266 | __ bind(&slow); |
| 6267 | __ push(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6268 | switch (op_) { |
| 6269 | case Token::SUB: |
| 6270 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 6271 | break; |
| 6272 | case Token::BIT_NOT: |
| 6273 | __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS); |
| 6274 | break; |
| 6275 | default: |
| 6276 | UNREACHABLE(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6277 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6278 | } |
| 6279 | |
| 6280 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6281 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6282 | // r0 holds the exception. |
| 6283 | |
| 6284 | // Adjust this code if not the case. |
| 6285 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6286 | |
| 6287 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6288 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 6289 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6290 | |
| 6291 | // Restore the next handler and frame pointer, discard handler state. |
| 6292 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 6293 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6294 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6295 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6296 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 6297 | |
| 6298 | // Before returning we restore the context from the frame pointer if |
| 6299 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6300 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6301 | __ cmp(fp, Operand(0)); |
| 6302 | // Set cp to NULL if fp is NULL. |
| 6303 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6304 | // Restore cp otherwise. |
| 6305 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6306 | #ifdef DEBUG |
| 6307 | if (FLAG_debug_code) { |
| 6308 | __ mov(lr, Operand(pc)); |
| 6309 | } |
| 6310 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6311 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6312 | __ pop(pc); |
| 6313 | } |
| 6314 | |
| 6315 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6316 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 6317 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6318 | // Adjust this code if not the case. |
| 6319 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6320 | |
| 6321 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6322 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6323 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6324 | |
| 6325 | // Unwind the handlers until the ENTRY handler is found. |
| 6326 | Label loop, done; |
| 6327 | __ bind(&loop); |
| 6328 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6329 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 6330 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6331 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 6332 | __ b(eq, &done); |
| 6333 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6334 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 6335 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6336 | __ jmp(&loop); |
| 6337 | __ bind(&done); |
| 6338 | |
| 6339 | // 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] | 6340 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6341 | __ pop(r2); |
| 6342 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6343 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6344 | if (type == OUT_OF_MEMORY) { |
| 6345 | // Set external caught exception to false. |
| 6346 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 6347 | __ mov(r0, Operand(false)); |
| 6348 | __ mov(r2, Operand(external_caught)); |
| 6349 | __ str(r0, MemOperand(r2)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6350 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6351 | // Set pending exception and r0 to out of memory exception. |
| 6352 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6353 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6354 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 6355 | __ str(r0, MemOperand(r2)); |
| 6356 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6357 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6358 | // Stack layout at this point. See also StackHandlerConstants. |
| 6359 | // sp -> state (ENTRY) |
| 6360 | // fp |
| 6361 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6362 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6363 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 6364 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6365 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 6366 | // Before returning we restore the context from the frame pointer if |
| 6367 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6368 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6369 | __ cmp(fp, Operand(0)); |
| 6370 | // Set cp to NULL if fp is NULL. |
| 6371 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6372 | // Restore cp otherwise. |
| 6373 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6374 | #ifdef DEBUG |
| 6375 | if (FLAG_debug_code) { |
| 6376 | __ mov(lr, Operand(pc)); |
| 6377 | } |
| 6378 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6379 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6380 | __ pop(pc); |
| 6381 | } |
| 6382 | |
| 6383 | |
| 6384 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 6385 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6386 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6387 | Label* throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6388 | bool do_gc, |
| 6389 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6390 | // r0: result parameter for PerformGC, if any |
| 6391 | // r4: number of arguments including receiver (C callee-saved) |
| 6392 | // r5: pointer to builtin function (C callee-saved) |
| 6393 | // r6: pointer to the first argument (C callee-saved) |
| 6394 | |
| 6395 | if (do_gc) { |
| 6396 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6397 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 6398 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6401 | ExternalReference scope_depth = |
| 6402 | ExternalReference::heap_always_allocate_scope_depth(); |
| 6403 | if (always_allocate) { |
| 6404 | __ mov(r0, Operand(scope_depth)); |
| 6405 | __ ldr(r1, MemOperand(r0)); |
| 6406 | __ add(r1, r1, Operand(1)); |
| 6407 | __ str(r1, MemOperand(r0)); |
| 6408 | } |
| 6409 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6410 | // Call C built-in. |
| 6411 | // r0 = argc, r1 = argv |
| 6412 | __ mov(r0, Operand(r4)); |
| 6413 | __ mov(r1, Operand(r6)); |
| 6414 | |
| 6415 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 6416 | // frames, we need to know where the return address is. Right now, |
| 6417 | // we push it on the stack to be able to find it again, but we never |
| 6418 | // restore from it in case of changes, which makes it impossible to |
| 6419 | // support moving the C entry code stub. This should be fixed, but currently |
| 6420 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 6421 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6422 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 6423 | masm->push(lr); |
| 6424 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6425 | |
| 6426 | if (always_allocate) { |
| 6427 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 6428 | // though (contain the result). |
| 6429 | __ mov(r2, Operand(scope_depth)); |
| 6430 | __ ldr(r3, MemOperand(r2)); |
| 6431 | __ sub(r3, r3, Operand(1)); |
| 6432 | __ str(r3, MemOperand(r2)); |
| 6433 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6434 | |
| 6435 | // check for failure result |
| 6436 | Label failure_returned; |
| 6437 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 6438 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 6439 | __ add(r2, r0, Operand(1)); |
| 6440 | __ tst(r2, Operand(kFailureTagMask)); |
| 6441 | __ b(eq, &failure_returned); |
| 6442 | |
| 6443 | // Exit C frame and return. |
| 6444 | // r0:r1: result |
| 6445 | // sp: stack pointer |
| 6446 | // fp: frame pointer |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6447 | __ LeaveExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6448 | |
| 6449 | // check if we should retry or throw exception |
| 6450 | Label retry; |
| 6451 | __ bind(&failure_returned); |
| 6452 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 6453 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 6454 | __ b(eq, &retry); |
| 6455 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6456 | // Special handling of out of memory exceptions. |
| 6457 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6458 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6459 | __ b(eq, throw_out_of_memory_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6460 | |
| 6461 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6462 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6463 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6464 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6465 | __ ldr(r0, MemOperand(ip)); |
| 6466 | __ str(r3, MemOperand(ip)); |
| 6467 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6468 | // Special handling of termination exceptions which are uncatchable |
| 6469 | // by javascript code. |
| 6470 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 6471 | __ b(eq, throw_termination_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6472 | |
| 6473 | // Handle normal exception. |
| 6474 | __ jmp(throw_normal_exception); |
| 6475 | |
| 6476 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 6477 | } |
| 6478 | |
| 6479 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6480 | void CEntryStub::Generate(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6481 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 6482 | // r0: number of arguments including receiver |
| 6483 | // r1: pointer to builtin function |
| 6484 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6485 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6486 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6487 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6488 | // Result returned in r0 or r0+r1 by default. |
| 6489 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6490 | // NOTE: Invocations of builtins may return failure objects |
| 6491 | // instead of a proper result. The builtin entry handles |
| 6492 | // this by performing a garbage collection and retrying the |
| 6493 | // builtin once. |
| 6494 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6495 | // Enter the exit frame that transitions from JavaScript to C++. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6496 | __ EnterExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6497 | |
| 6498 | // r4: number of arguments (C callee-saved) |
| 6499 | // r5: pointer to builtin function (C callee-saved) |
| 6500 | // r6: pointer to first argument (C callee-saved) |
| 6501 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6502 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6503 | Label throw_termination_exception; |
| 6504 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6505 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6506 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6507 | GenerateCore(masm, |
| 6508 | &throw_normal_exception, |
| 6509 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6510 | &throw_out_of_memory_exception, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6511 | false, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6512 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6513 | |
| 6514 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6515 | GenerateCore(masm, |
| 6516 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6517 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6518 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6519 | true, |
| 6520 | false); |
| 6521 | |
| 6522 | // Do full GC and retry runtime call one final time. |
| 6523 | Failure* failure = Failure::InternalError(); |
| 6524 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 6525 | GenerateCore(masm, |
| 6526 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6527 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6528 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6529 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6530 | true); |
| 6531 | |
| 6532 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6533 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 6534 | |
| 6535 | __ bind(&throw_termination_exception); |
| 6536 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6537 | |
| 6538 | __ bind(&throw_normal_exception); |
| 6539 | GenerateThrowTOS(masm); |
| 6540 | } |
| 6541 | |
| 6542 | |
| 6543 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 6544 | // r0: code entry |
| 6545 | // r1: function |
| 6546 | // r2: receiver |
| 6547 | // r3: argc |
| 6548 | // [sp+0]: argv |
| 6549 | |
| 6550 | Label invoke, exit; |
| 6551 | |
| 6552 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 6553 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6554 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6555 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 6556 | |
| 6557 | // Get address of argv, see stm above. |
| 6558 | // r0: code entry |
| 6559 | // r1: function |
| 6560 | // r2: receiver |
| 6561 | // r3: argc |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6562 | __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6563 | |
| 6564 | // Push a frame with special values setup to mark it as an entry frame. |
| 6565 | // r0: code entry |
| 6566 | // r1: function |
| 6567 | // r2: receiver |
| 6568 | // r3: argc |
| 6569 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6570 | __ 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] | 6571 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 6572 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6573 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 6574 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6575 | __ ldr(r5, MemOperand(r5)); |
| 6576 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 6577 | |
| 6578 | // Setup frame pointer for the frame to be pushed. |
| 6579 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6580 | |
| 6581 | // Call a faked try-block that does the invoke. |
| 6582 | __ bl(&invoke); |
| 6583 | |
| 6584 | // Caught exception: Store result (exception) in the pending |
| 6585 | // exception field in the JSEnv and return a failure sentinel. |
| 6586 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 6587 | // 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] | 6588 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6589 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6590 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6591 | __ b(&exit); |
| 6592 | |
| 6593 | // Invoke: Link this frame into the handler chain. |
| 6594 | __ bind(&invoke); |
| 6595 | // Must preserve r0-r4, r5-r7 are available. |
| 6596 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6597 | // If an exception not caught by another handler occurs, this handler |
| 6598 | // returns control to the code after the bl(&invoke) above, which |
| 6599 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 6600 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6601 | |
| 6602 | // Clear any pending exceptions. |
| 6603 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6604 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6605 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6606 | __ str(r5, MemOperand(ip)); |
| 6607 | |
| 6608 | // Invoke the function by calling through JS entry trampoline builtin. |
| 6609 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6610 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6611 | |
| 6612 | // Expected registers by Builtins::JSEntryTrampoline |
| 6613 | // r0: code entry |
| 6614 | // r1: function |
| 6615 | // r2: receiver |
| 6616 | // r3: argc |
| 6617 | // r4: argv |
| 6618 | if (is_construct) { |
| 6619 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6620 | __ mov(ip, Operand(construct_entry)); |
| 6621 | } else { |
| 6622 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6623 | __ mov(ip, Operand(entry)); |
| 6624 | } |
| 6625 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6626 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6627 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6628 | // macro for the add instruction because we don't want the coverage tool |
| 6629 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6630 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6631 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6632 | |
| 6633 | // Unlink this frame from the handler chain. When reading the |
| 6634 | // address of the next handler, there is no need to use the address |
| 6635 | // displacement since the current stack pointer (sp) points directly |
| 6636 | // to the stack handler. |
| 6637 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6638 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6639 | __ str(r3, MemOperand(ip)); |
| 6640 | // No need to restore registers |
| 6641 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6642 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6643 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6644 | __ bind(&exit); // r0 holds result |
| 6645 | // Restore the top frame descriptors from the stack. |
| 6646 | __ pop(r3); |
| 6647 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6648 | __ str(r3, MemOperand(ip)); |
| 6649 | |
| 6650 | // Reset the stack to the callee saved registers. |
| 6651 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6652 | |
| 6653 | // Restore callee-saved registers and return. |
| 6654 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6655 | if (FLAG_debug_code) { |
| 6656 | __ mov(lr, Operand(pc)); |
| 6657 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6658 | #endif |
| 6659 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6660 | } |
| 6661 | |
| 6662 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6663 | // This stub performs an instanceof, calling the builtin function if |
| 6664 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6665 | // be an instance of (these are fetched from the stack). |
| 6666 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6667 | // Get the object - slow case for smis (we may need to throw an exception |
| 6668 | // depending on the rhs). |
| 6669 | Label slow, loop, is_instance, is_not_instance; |
| 6670 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6671 | __ BranchOnSmi(r0, &slow); |
| 6672 | |
| 6673 | // Check that the left hand is a JS object and put map in r3. |
| 6674 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6675 | __ b(lt, &slow); |
| 6676 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6677 | __ b(gt, &slow); |
| 6678 | |
| 6679 | // 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] | 6680 | __ ldr(r1, MemOperand(sp, 0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6681 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6682 | |
| 6683 | // Check that the function prototype is a JS object. |
| 6684 | __ BranchOnSmi(r4, &slow); |
| 6685 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6686 | __ b(lt, &slow); |
| 6687 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6688 | __ b(gt, &slow); |
| 6689 | |
| 6690 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6691 | // Get prototype of object into r2. |
| 6692 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6693 | |
| 6694 | // Loop through the prototype chain looking for the function prototype. |
| 6695 | __ bind(&loop); |
| 6696 | __ cmp(r2, Operand(r4)); |
| 6697 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6698 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 6699 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6700 | __ b(eq, &is_not_instance); |
| 6701 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6702 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6703 | __ jmp(&loop); |
| 6704 | |
| 6705 | __ bind(&is_instance); |
| 6706 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6707 | __ pop(); |
| 6708 | __ pop(); |
| 6709 | __ mov(pc, Operand(lr)); // Return. |
| 6710 | |
| 6711 | __ bind(&is_not_instance); |
| 6712 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6713 | __ pop(); |
| 6714 | __ pop(); |
| 6715 | __ mov(pc, Operand(lr)); // Return. |
| 6716 | |
| 6717 | // Slow-case. Tail call builtin. |
| 6718 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6719 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6720 | } |
| 6721 | |
| 6722 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6723 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6724 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6725 | Label adaptor; |
| 6726 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6727 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6728 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6729 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6730 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6731 | // Nothing to do: The formal number of parameters has already been |
| 6732 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6733 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6734 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6735 | // Arguments adaptor case: Read the arguments length from the |
| 6736 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6737 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6738 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6739 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
| 6742 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6743 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6744 | // The displacement is the offset of the last parameter (if any) |
| 6745 | // relative to the frame pointer. |
| 6746 | static const int kDisplacement = |
| 6747 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6748 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6749 | // Check that the key is a smi. |
| 6750 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6751 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6752 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6753 | // Check if the calling frame is an arguments adaptor frame. |
| 6754 | Label adaptor; |
| 6755 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6756 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6757 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6758 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6759 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6760 | // Check index against formal parameters count limit passed in |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6761 | // through register r0. Use unsigned comparison to get negative |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6762 | // check for free. |
| 6763 | __ cmp(r1, r0); |
| 6764 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6765 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6766 | // Read the argument from the stack and return it. |
| 6767 | __ sub(r3, r0, r1); |
| 6768 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6769 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6770 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6771 | |
| 6772 | // Arguments adaptor case: Check index against actual arguments |
| 6773 | // limit found in the arguments adaptor frame. Use unsigned |
| 6774 | // comparison to get negative check for free. |
| 6775 | __ bind(&adaptor); |
| 6776 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6777 | __ cmp(r1, r0); |
| 6778 | __ b(cs, &slow); |
| 6779 | |
| 6780 | // Read the argument from the adaptor frame and return it. |
| 6781 | __ sub(r3, r0, r1); |
| 6782 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6783 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6784 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6785 | |
| 6786 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6787 | // by calling the runtime system. |
| 6788 | __ bind(&slow); |
| 6789 | __ push(r1); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6790 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6791 | } |
| 6792 | |
| 6793 | |
| 6794 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6795 | // sp[0] : number of parameters |
| 6796 | // sp[4] : receiver displacement |
| 6797 | // sp[8] : function |
| 6798 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6799 | // Check if the calling frame is an arguments adaptor frame. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6800 | Label adaptor_frame, try_allocate, runtime; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6801 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6802 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6803 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6804 | __ b(eq, &adaptor_frame); |
| 6805 | |
| 6806 | // Get the length from the frame. |
| 6807 | __ ldr(r1, MemOperand(sp, 0)); |
| 6808 | __ b(&try_allocate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6809 | |
| 6810 | // Patch the arguments.length and the parameters pointer. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6811 | __ bind(&adaptor_frame); |
| 6812 | __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6813 | __ str(r1, MemOperand(sp, 0)); |
| 6814 | __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6815 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6816 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6817 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6818 | // Try the new space allocation. Start out with computing the size |
| 6819 | // of the arguments object and the elements array (in words, not |
| 6820 | // bytes because AllocateInNewSpace expects words). |
| 6821 | Label add_arguments_object; |
| 6822 | __ bind(&try_allocate); |
| 6823 | __ cmp(r1, Operand(0)); |
| 6824 | __ b(eq, &add_arguments_object); |
| 6825 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 6826 | __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize)); |
| 6827 | __ bind(&add_arguments_object); |
| 6828 | __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize)); |
| 6829 | |
| 6830 | // Do the allocation of both objects in one go. |
| 6831 | __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT); |
| 6832 | |
| 6833 | // Get the arguments boilerplate from the current (global) context. |
| 6834 | int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX); |
| 6835 | __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 6836 | __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset)); |
| 6837 | __ ldr(r4, MemOperand(r4, offset)); |
| 6838 | |
| 6839 | // Copy the JS object part. |
| 6840 | for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) { |
| 6841 | __ ldr(r3, FieldMemOperand(r4, i)); |
| 6842 | __ str(r3, FieldMemOperand(r0, i)); |
| 6843 | } |
| 6844 | |
| 6845 | // Setup the callee in-object property. |
| 6846 | ASSERT(Heap::arguments_callee_index == 0); |
| 6847 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 6848 | __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize)); |
| 6849 | |
| 6850 | // Get the length (smi tagged) and set that as an in-object property too. |
| 6851 | ASSERT(Heap::arguments_length_index == 1); |
| 6852 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 6853 | __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize)); |
| 6854 | |
| 6855 | // If there are no actual arguments, we're done. |
| 6856 | Label done; |
| 6857 | __ cmp(r1, Operand(0)); |
| 6858 | __ b(eq, &done); |
| 6859 | |
| 6860 | // Get the parameters pointer from the stack and untag the length. |
| 6861 | __ ldr(r2, MemOperand(sp, 1 * kPointerSize)); |
| 6862 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 6863 | |
| 6864 | // Setup the elements pointer in the allocated arguments object and |
| 6865 | // initialize the header in the elements fixed array. |
| 6866 | __ add(r4, r0, Operand(Heap::kArgumentsObjectSize)); |
| 6867 | __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 6868 | __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex); |
| 6869 | __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset)); |
| 6870 | __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset)); |
| 6871 | |
| 6872 | // Copy the fixed array slots. |
| 6873 | Label loop; |
| 6874 | // Setup r4 to point to the first array slot. |
| 6875 | __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 6876 | __ bind(&loop); |
| 6877 | // Pre-decrement r2 with kPointerSize on each iteration. |
| 6878 | // Pre-decrement in order to skip receiver. |
| 6879 | __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex)); |
| 6880 | // Post-increment r4 with kPointerSize on each iteration. |
| 6881 | __ str(r3, MemOperand(r4, kPointerSize, PostIndex)); |
| 6882 | __ sub(r1, r1, Operand(1)); |
| 6883 | __ cmp(r1, Operand(0)); |
| 6884 | __ b(ne, &loop); |
| 6885 | |
| 6886 | // Return and remove the on-stack parameters. |
| 6887 | __ bind(&done); |
| 6888 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 6889 | __ Ret(); |
| 6890 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6891 | // Do the runtime call to allocate the arguments object. |
| 6892 | __ bind(&runtime); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6893 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6894 | } |
| 6895 | |
| 6896 | |
| 6897 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6898 | Label slow; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6899 | |
| 6900 | // If the receiver might be a value (string, number or boolean) check for this |
| 6901 | // and box it if it is. |
| 6902 | if (ReceiverMightBeValue()) { |
| 6903 | // Get the receiver from the stack. |
| 6904 | // function, receiver [, arguments] |
| 6905 | Label receiver_is_value, receiver_is_js_object; |
| 6906 | __ ldr(r1, MemOperand(sp, argc_ * kPointerSize)); |
| 6907 | |
| 6908 | // Check if receiver is a smi (which is a number value). |
| 6909 | __ BranchOnSmi(r1, &receiver_is_value); |
| 6910 | |
| 6911 | // Check if the receiver is a valid JS object. |
| 6912 | __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 6913 | __ b(ge, &receiver_is_js_object); |
| 6914 | |
| 6915 | // Call the runtime to box the value. |
| 6916 | __ bind(&receiver_is_value); |
| 6917 | __ EnterInternalFrame(); |
| 6918 | __ push(r1); |
| 6919 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
| 6920 | __ LeaveInternalFrame(); |
| 6921 | __ str(r0, MemOperand(sp, argc_ * kPointerSize)); |
| 6922 | |
| 6923 | __ bind(&receiver_is_js_object); |
| 6924 | } |
| 6925 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6926 | // Get the function to call from the stack. |
| 6927 | // function, receiver [, arguments] |
| 6928 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6929 | |
| 6930 | // Check that the function is really a JavaScript function. |
| 6931 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6932 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6933 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6934 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6935 | __ b(ne, &slow); |
| 6936 | |
| 6937 | // Fast-case: Invoke the function now. |
| 6938 | // r1: pushed function |
| 6939 | ParameterCount actual(argc_); |
| 6940 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6941 | |
| 6942 | // Slow-case: Non-function called. |
| 6943 | __ bind(&slow); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6944 | // CALL_NON_FUNCTION expects the non-function callee as receiver (instead |
| 6945 | // of the original receiver from the call site). |
| 6946 | __ str(r1, MemOperand(sp, argc_ * kPointerSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6947 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6948 | __ mov(r2, Operand(0)); |
| 6949 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6950 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6951 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6952 | } |
| 6953 | |
| 6954 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6955 | const char* CompareStub::GetName() { |
| 6956 | switch (cc_) { |
| 6957 | case lt: return "CompareStub_LT"; |
| 6958 | case gt: return "CompareStub_GT"; |
| 6959 | case le: return "CompareStub_LE"; |
| 6960 | case ge: return "CompareStub_GE"; |
| 6961 | case ne: { |
| 6962 | if (strict_) { |
| 6963 | if (never_nan_nan_) { |
| 6964 | return "CompareStub_NE_STRICT_NO_NAN"; |
| 6965 | } else { |
| 6966 | return "CompareStub_NE_STRICT"; |
| 6967 | } |
| 6968 | } else { |
| 6969 | if (never_nan_nan_) { |
| 6970 | return "CompareStub_NE_NO_NAN"; |
| 6971 | } else { |
| 6972 | return "CompareStub_NE"; |
| 6973 | } |
| 6974 | } |
| 6975 | } |
| 6976 | case eq: { |
| 6977 | if (strict_) { |
| 6978 | if (never_nan_nan_) { |
| 6979 | return "CompareStub_EQ_STRICT_NO_NAN"; |
| 6980 | } else { |
| 6981 | return "CompareStub_EQ_STRICT"; |
| 6982 | } |
| 6983 | } else { |
| 6984 | if (never_nan_nan_) { |
| 6985 | return "CompareStub_EQ_NO_NAN"; |
| 6986 | } else { |
| 6987 | return "CompareStub_EQ"; |
| 6988 | } |
| 6989 | } |
| 6990 | } |
| 6991 | default: return "CompareStub"; |
| 6992 | } |
| 6993 | } |
| 6994 | |
| 6995 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6996 | int CompareStub::MinorKey() { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6997 | // Encode the three parameters in a unique 16 bit value. |
| 6998 | ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16)); |
| 6999 | int nnn_value = (never_nan_nan_ ? 2 : 0); |
| 7000 | if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs. |
| 7001 | return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7002 | } |
| 7003 | |
| 7004 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7005 | void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm, |
| 7006 | Register dest, |
| 7007 | Register src, |
| 7008 | Register count, |
| 7009 | Register scratch, |
| 7010 | bool ascii) { |
| 7011 | Label loop; |
| 7012 | Label done; |
| 7013 | // This loop just copies one character at a time, as it is only used for very |
| 7014 | // short strings. |
| 7015 | if (!ascii) { |
| 7016 | __ add(count, count, Operand(count), SetCC); |
| 7017 | } else { |
| 7018 | __ cmp(count, Operand(0)); |
| 7019 | } |
| 7020 | __ b(eq, &done); |
| 7021 | |
| 7022 | __ bind(&loop); |
| 7023 | __ ldrb(scratch, MemOperand(src, 1, PostIndex)); |
| 7024 | // Perform sub between load and dependent store to get the load time to |
| 7025 | // complete. |
| 7026 | __ sub(count, count, Operand(1), SetCC); |
| 7027 | __ strb(scratch, MemOperand(dest, 1, PostIndex)); |
| 7028 | // last iteration. |
| 7029 | __ b(gt, &loop); |
| 7030 | |
| 7031 | __ bind(&done); |
| 7032 | } |
| 7033 | |
| 7034 | |
| 7035 | enum CopyCharactersFlags { |
| 7036 | COPY_ASCII = 1, |
| 7037 | DEST_ALWAYS_ALIGNED = 2 |
| 7038 | }; |
| 7039 | |
| 7040 | |
| 7041 | void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm, |
| 7042 | Register dest, |
| 7043 | Register src, |
| 7044 | Register count, |
| 7045 | Register scratch1, |
| 7046 | Register scratch2, |
| 7047 | Register scratch3, |
| 7048 | Register scratch4, |
| 7049 | Register scratch5, |
| 7050 | int flags) { |
| 7051 | bool ascii = (flags & COPY_ASCII) != 0; |
| 7052 | bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0; |
| 7053 | |
| 7054 | if (dest_always_aligned && FLAG_debug_code) { |
| 7055 | // Check that destination is actually word aligned if the flag says |
| 7056 | // that it is. |
| 7057 | __ tst(dest, Operand(kPointerAlignmentMask)); |
| 7058 | __ Check(eq, "Destination of copy not aligned."); |
| 7059 | } |
| 7060 | |
| 7061 | const int kReadAlignment = 4; |
| 7062 | const int kReadAlignmentMask = kReadAlignment - 1; |
| 7063 | // Ensure that reading an entire aligned word containing the last character |
| 7064 | // of a string will not read outside the allocated area (because we pad up |
| 7065 | // to kObjectAlignment). |
| 7066 | ASSERT(kObjectAlignment >= kReadAlignment); |
| 7067 | // Assumes word reads and writes are little endian. |
| 7068 | // Nothing to do for zero characters. |
| 7069 | Label done; |
| 7070 | if (!ascii) { |
| 7071 | __ add(count, count, Operand(count), SetCC); |
| 7072 | } else { |
| 7073 | __ cmp(count, Operand(0)); |
| 7074 | } |
| 7075 | __ b(eq, &done); |
| 7076 | |
| 7077 | // Assume that you cannot read (or write) unaligned. |
| 7078 | Label byte_loop; |
| 7079 | // Must copy at least eight bytes, otherwise just do it one byte at a time. |
| 7080 | __ cmp(count, Operand(8)); |
| 7081 | __ add(count, dest, Operand(count)); |
| 7082 | Register limit = count; // Read until src equals this. |
| 7083 | __ b(lt, &byte_loop); |
| 7084 | |
| 7085 | if (!dest_always_aligned) { |
| 7086 | // Align dest by byte copying. Copies between zero and three bytes. |
| 7087 | __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC); |
| 7088 | Label dest_aligned; |
| 7089 | __ b(eq, &dest_aligned); |
| 7090 | __ cmp(scratch4, Operand(2)); |
| 7091 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex)); |
| 7092 | __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le); |
| 7093 | __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt); |
| 7094 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7095 | __ strb(scratch2, MemOperand(dest, 1, PostIndex), le); |
| 7096 | __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt); |
| 7097 | __ bind(&dest_aligned); |
| 7098 | } |
| 7099 | |
| 7100 | Label simple_loop; |
| 7101 | |
| 7102 | __ sub(scratch4, dest, Operand(src)); |
| 7103 | __ and_(scratch4, scratch4, Operand(0x03), SetCC); |
| 7104 | __ b(eq, &simple_loop); |
| 7105 | // Shift register is number of bits in a source word that |
| 7106 | // must be combined with bits in the next source word in order |
| 7107 | // to create a destination word. |
| 7108 | |
| 7109 | // Complex loop for src/dst that are not aligned the same way. |
| 7110 | { |
| 7111 | Label loop; |
| 7112 | __ mov(scratch4, Operand(scratch4, LSL, 3)); |
| 7113 | Register left_shift = scratch4; |
| 7114 | __ and_(src, src, Operand(~3)); // Round down to load previous word. |
| 7115 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 7116 | // Store the "shift" most significant bits of scratch in the least |
| 7117 | // signficant bits (i.e., shift down by (32-shift)). |
| 7118 | __ rsb(scratch2, left_shift, Operand(32)); |
| 7119 | Register right_shift = scratch2; |
| 7120 | __ mov(scratch1, Operand(scratch1, LSR, right_shift)); |
| 7121 | |
| 7122 | __ bind(&loop); |
| 7123 | __ ldr(scratch3, MemOperand(src, 4, PostIndex)); |
| 7124 | __ sub(scratch5, limit, Operand(dest)); |
| 7125 | __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift)); |
| 7126 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 7127 | __ mov(scratch1, Operand(scratch3, LSR, right_shift)); |
| 7128 | // Loop if four or more bytes left to copy. |
| 7129 | // Compare to eight, because we did the subtract before increasing dst. |
| 7130 | __ sub(scratch5, scratch5, Operand(8), SetCC); |
| 7131 | __ b(ge, &loop); |
| 7132 | } |
| 7133 | // There is now between zero and three bytes left to copy (negative that |
| 7134 | // number is in scratch5), and between one and three bytes already read into |
| 7135 | // scratch1 (eight times that number in scratch4). We may have read past |
| 7136 | // the end of the string, but because objects are aligned, we have not read |
| 7137 | // past the end of the object. |
| 7138 | // Find the minimum of remaining characters to move and preloaded characters |
| 7139 | // and write those as bytes. |
| 7140 | __ add(scratch5, scratch5, Operand(4), SetCC); |
| 7141 | __ b(eq, &done); |
| 7142 | __ cmp(scratch4, Operand(scratch5, LSL, 3), ne); |
| 7143 | // Move minimum of bytes read and bytes left to copy to scratch4. |
| 7144 | __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt); |
| 7145 | // Between one and three (value in scratch5) characters already read into |
| 7146 | // scratch ready to write. |
| 7147 | __ cmp(scratch5, Operand(2)); |
| 7148 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7149 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge); |
| 7150 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge); |
| 7151 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt); |
| 7152 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt); |
| 7153 | // Copy any remaining bytes. |
| 7154 | __ b(&byte_loop); |
| 7155 | |
| 7156 | // Simple loop. |
| 7157 | // Copy words from src to dst, until less than four bytes left. |
| 7158 | // Both src and dest are word aligned. |
| 7159 | __ bind(&simple_loop); |
| 7160 | { |
| 7161 | Label loop; |
| 7162 | __ bind(&loop); |
| 7163 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 7164 | __ sub(scratch3, limit, Operand(dest)); |
| 7165 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 7166 | // Compare to 8, not 4, because we do the substraction before increasing |
| 7167 | // dest. |
| 7168 | __ cmp(scratch3, Operand(8)); |
| 7169 | __ b(ge, &loop); |
| 7170 | } |
| 7171 | |
| 7172 | // Copy bytes from src to dst until dst hits limit. |
| 7173 | __ bind(&byte_loop); |
| 7174 | __ cmp(dest, Operand(limit)); |
| 7175 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt); |
| 7176 | __ b(ge, &done); |
| 7177 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 7178 | __ b(&byte_loop); |
| 7179 | |
| 7180 | __ bind(&done); |
| 7181 | } |
| 7182 | |
| 7183 | |
| 7184 | void SubStringStub::Generate(MacroAssembler* masm) { |
| 7185 | Label runtime; |
| 7186 | |
| 7187 | // Stack frame on entry. |
| 7188 | // lr: return address |
| 7189 | // sp[0]: to |
| 7190 | // sp[4]: from |
| 7191 | // sp[8]: string |
| 7192 | |
| 7193 | // This stub is called from the native-call %_SubString(...), so |
| 7194 | // nothing can be assumed about the arguments. It is tested that: |
| 7195 | // "string" is a sequential string, |
| 7196 | // both "from" and "to" are smis, and |
| 7197 | // 0 <= from <= to <= string.length. |
| 7198 | // If any of these assumptions fail, we call the runtime system. |
| 7199 | |
| 7200 | static const int kToOffset = 0 * kPointerSize; |
| 7201 | static const int kFromOffset = 1 * kPointerSize; |
| 7202 | static const int kStringOffset = 2 * kPointerSize; |
| 7203 | |
| 7204 | |
| 7205 | // Check bounds and smi-ness. |
| 7206 | __ ldr(r7, MemOperand(sp, kToOffset)); |
| 7207 | __ ldr(r6, MemOperand(sp, kFromOffset)); |
| 7208 | ASSERT_EQ(0, kSmiTag); |
| 7209 | ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize); |
| 7210 | // I.e., arithmetic shift right by one un-smi-tags. |
| 7211 | __ mov(r2, Operand(r7, ASR, 1), SetCC); |
| 7212 | __ mov(r3, Operand(r6, ASR, 1), SetCC, cc); |
| 7213 | // If either r2 or r6 had the smi tag bit set, then carry is set now. |
| 7214 | __ b(cs, &runtime); // Either "from" or "to" is not a smi. |
| 7215 | __ b(mi, &runtime); // From is negative. |
| 7216 | |
| 7217 | __ sub(r2, r2, Operand(r3), SetCC); |
| 7218 | __ b(mi, &runtime); // Fail if from > to. |
| 7219 | // Handle sub-strings of length 2 and less in the runtime system. |
| 7220 | __ cmp(r2, Operand(2)); |
| 7221 | __ b(le, &runtime); |
| 7222 | |
| 7223 | // r2: length |
| 7224 | // r6: from (smi) |
| 7225 | // r7: to (smi) |
| 7226 | |
| 7227 | // Make sure first argument is a sequential (or flat) string. |
| 7228 | __ ldr(r5, MemOperand(sp, kStringOffset)); |
| 7229 | ASSERT_EQ(0, kSmiTag); |
| 7230 | __ tst(r5, Operand(kSmiTagMask)); |
| 7231 | __ b(eq, &runtime); |
| 7232 | Condition is_string = masm->IsObjectStringType(r5, r1); |
| 7233 | __ b(NegateCondition(is_string), &runtime); |
| 7234 | |
| 7235 | // r1: instance type |
| 7236 | // r2: length |
| 7237 | // r5: string |
| 7238 | // r6: from (smi) |
| 7239 | // r7: to (smi) |
| 7240 | Label seq_string; |
| 7241 | __ and_(r4, r1, Operand(kStringRepresentationMask)); |
| 7242 | ASSERT(kSeqStringTag < kConsStringTag); |
| 7243 | ASSERT(kExternalStringTag > kConsStringTag); |
| 7244 | __ cmp(r4, Operand(kConsStringTag)); |
| 7245 | __ b(gt, &runtime); // External strings go to runtime. |
| 7246 | __ b(lt, &seq_string); // Sequential strings are handled directly. |
| 7247 | |
| 7248 | // Cons string. Try to recurse (once) on the first substring. |
| 7249 | // (This adds a little more generality than necessary to handle flattened |
| 7250 | // cons strings, but not much). |
| 7251 | __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset)); |
| 7252 | __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset)); |
| 7253 | __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7254 | __ tst(r1, Operand(kStringRepresentationMask)); |
| 7255 | ASSERT_EQ(0, kSeqStringTag); |
| 7256 | __ b(ne, &runtime); // Cons and External strings go to runtime. |
| 7257 | |
| 7258 | // Definitly a sequential string. |
| 7259 | __ bind(&seq_string); |
| 7260 | |
| 7261 | // r1: instance type. |
| 7262 | // r2: length |
| 7263 | // r5: string |
| 7264 | // r6: from (smi) |
| 7265 | // r7: to (smi) |
| 7266 | __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset)); |
| 7267 | __ cmp(r4, Operand(r7, ASR, 1)); |
| 7268 | __ b(lt, &runtime); // Fail if to > length. |
| 7269 | |
| 7270 | // r1: instance type. |
| 7271 | // r2: result string length. |
| 7272 | // r5: string. |
| 7273 | // r6: from offset (smi) |
| 7274 | // Check for flat ascii string. |
| 7275 | Label non_ascii_flat; |
| 7276 | __ tst(r1, Operand(kStringEncodingMask)); |
| 7277 | ASSERT_EQ(0, kTwoByteStringTag); |
| 7278 | __ b(eq, &non_ascii_flat); |
| 7279 | |
| 7280 | // Allocate the result. |
| 7281 | __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime); |
| 7282 | |
| 7283 | // r0: result string. |
| 7284 | // r2: result string length. |
| 7285 | // r5: string. |
| 7286 | // r6: from offset (smi) |
| 7287 | // Locate first character of result. |
| 7288 | __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7289 | // Locate 'from' character of string. |
| 7290 | __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7291 | __ add(r5, r5, Operand(r6, ASR, 1)); |
| 7292 | |
| 7293 | // r0: result string. |
| 7294 | // r1: first character of result string. |
| 7295 | // r2: result string length. |
| 7296 | // r5: first character of sub string to copy. |
| 7297 | ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask); |
| 7298 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 7299 | COPY_ASCII | DEST_ALWAYS_ALIGNED); |
| 7300 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 7301 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7302 | __ Ret(); |
| 7303 | |
| 7304 | __ bind(&non_ascii_flat); |
| 7305 | // r2: result string length. |
| 7306 | // r5: string. |
| 7307 | // r6: from offset (smi) |
| 7308 | // Check for flat two byte string. |
| 7309 | |
| 7310 | // Allocate the result. |
| 7311 | __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime); |
| 7312 | |
| 7313 | // r0: result string. |
| 7314 | // r2: result string length. |
| 7315 | // r5: string. |
| 7316 | // Locate first character of result. |
| 7317 | __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7318 | // Locate 'from' character of string. |
| 7319 | __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7320 | // As "from" is a smi it is 2 times the value which matches the size of a two |
| 7321 | // byte character. |
| 7322 | __ add(r5, r5, Operand(r6)); |
| 7323 | |
| 7324 | // r0: result string. |
| 7325 | // r1: first character of result. |
| 7326 | // r2: result length. |
| 7327 | // r5: first character of string to copy. |
| 7328 | ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask); |
| 7329 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 7330 | DEST_ALWAYS_ALIGNED); |
| 7331 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 7332 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7333 | __ Ret(); |
| 7334 | |
| 7335 | // Just jump to runtime to create the sub string. |
| 7336 | __ bind(&runtime); |
| 7337 | __ TailCallRuntime(ExternalReference(Runtime::kSubString), 3, 1); |
| 7338 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7339 | |
| 7340 | |
| 7341 | void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 7342 | Register left, |
| 7343 | Register right, |
| 7344 | Register scratch1, |
| 7345 | Register scratch2, |
| 7346 | Register scratch3, |
| 7347 | Register scratch4) { |
| 7348 | Label compare_lengths; |
| 7349 | // Find minimum length and length difference. |
| 7350 | __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset)); |
| 7351 | __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset)); |
| 7352 | __ sub(scratch3, scratch1, Operand(scratch2), SetCC); |
| 7353 | Register length_delta = scratch3; |
| 7354 | __ mov(scratch1, scratch2, LeaveCC, gt); |
| 7355 | Register min_length = scratch1; |
| 7356 | __ tst(min_length, Operand(min_length)); |
| 7357 | __ b(eq, &compare_lengths); |
| 7358 | |
| 7359 | // Setup registers so that we only need to increment one register |
| 7360 | // in the loop. |
| 7361 | __ add(scratch2, min_length, |
| 7362 | Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7363 | __ add(left, left, Operand(scratch2)); |
| 7364 | __ add(right, right, Operand(scratch2)); |
| 7365 | // Registers left and right points to the min_length character of strings. |
| 7366 | __ rsb(min_length, min_length, Operand(-1)); |
| 7367 | Register index = min_length; |
| 7368 | // Index starts at -min_length. |
| 7369 | |
| 7370 | { |
| 7371 | // Compare loop. |
| 7372 | Label loop; |
| 7373 | __ bind(&loop); |
| 7374 | // Compare characters. |
| 7375 | __ add(index, index, Operand(1), SetCC); |
| 7376 | __ ldrb(scratch2, MemOperand(left, index), ne); |
| 7377 | __ ldrb(scratch4, MemOperand(right, index), ne); |
| 7378 | // Skip to compare lengths with eq condition true. |
| 7379 | __ b(eq, &compare_lengths); |
| 7380 | __ cmp(scratch2, scratch4); |
| 7381 | __ b(eq, &loop); |
| 7382 | // Fallthrough with eq condition false. |
| 7383 | } |
| 7384 | // Compare lengths - strings up to min-length are equal. |
| 7385 | __ bind(&compare_lengths); |
| 7386 | ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0)); |
| 7387 | // Use zero length_delta as result. |
| 7388 | __ mov(r0, Operand(length_delta), SetCC, eq); |
| 7389 | // Fall through to here if characters compare not-equal. |
| 7390 | __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt); |
| 7391 | __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt); |
| 7392 | __ Ret(); |
| 7393 | } |
| 7394 | |
| 7395 | |
| 7396 | void StringCompareStub::Generate(MacroAssembler* masm) { |
| 7397 | Label runtime; |
| 7398 | |
| 7399 | // Stack frame on entry. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7400 | // sp[0]: right string |
| 7401 | // sp[4]: left string |
| 7402 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left |
| 7403 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7404 | |
| 7405 | Label not_same; |
| 7406 | __ cmp(r0, r1); |
| 7407 | __ b(ne, ¬_same); |
| 7408 | ASSERT_EQ(0, EQUAL); |
| 7409 | ASSERT_EQ(0, kSmiTag); |
| 7410 | __ mov(r0, Operand(Smi::FromInt(EQUAL))); |
| 7411 | __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2); |
| 7412 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7413 | __ Ret(); |
| 7414 | |
| 7415 | __ bind(¬_same); |
| 7416 | |
| 7417 | // Check that both objects are sequential ascii strings. |
| 7418 | __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime); |
| 7419 | |
| 7420 | // Compare flat ascii strings natively. Remove arguments from stack first. |
| 7421 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 7422 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7423 | GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5); |
| 7424 | |
| 7425 | // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater) |
| 7426 | // tagged as a small integer. |
| 7427 | __ bind(&runtime); |
| 7428 | __ TailCallRuntime(ExternalReference(Runtime::kStringCompare), 2, 1); |
| 7429 | } |
| 7430 | |
| 7431 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7432 | void StringAddStub::Generate(MacroAssembler* masm) { |
| 7433 | Label string_add_runtime; |
| 7434 | // Stack on entry: |
| 7435 | // sp[0]: second argument. |
| 7436 | // sp[4]: first argument. |
| 7437 | |
| 7438 | // Load the two arguments. |
| 7439 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument. |
| 7440 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument. |
| 7441 | |
| 7442 | // Make sure that both arguments are strings if not known in advance. |
| 7443 | if (string_check_) { |
| 7444 | ASSERT_EQ(0, kSmiTag); |
| 7445 | __ JumpIfEitherSmi(r0, r1, &string_add_runtime); |
| 7446 | // Load instance types. |
| 7447 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7448 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7449 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7450 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7451 | ASSERT_EQ(0, kStringTag); |
| 7452 | // If either is not a string, go to runtime. |
| 7453 | __ tst(r4, Operand(kIsNotStringMask)); |
| 7454 | __ tst(r5, Operand(kIsNotStringMask), eq); |
| 7455 | __ b(ne, &string_add_runtime); |
| 7456 | } |
| 7457 | |
| 7458 | // Both arguments are strings. |
| 7459 | // r0: first string |
| 7460 | // r1: second string |
| 7461 | // r4: first string instance type (if string_check_) |
| 7462 | // r5: second string instance type (if string_check_) |
| 7463 | { |
| 7464 | Label strings_not_empty; |
| 7465 | // Check if either of the strings are empty. In that case return the other. |
| 7466 | __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset)); |
| 7467 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
| 7468 | __ cmp(r2, Operand(0)); // Test if first string is empty. |
| 7469 | __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second. |
| 7470 | __ cmp(r3, Operand(0), ne); // Else test if second string is empty. |
| 7471 | __ b(ne, &strings_not_empty); // If either string was empty, return r0. |
| 7472 | |
| 7473 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7474 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7475 | __ Ret(); |
| 7476 | |
| 7477 | __ bind(&strings_not_empty); |
| 7478 | } |
| 7479 | |
| 7480 | // Both strings are non-empty. |
| 7481 | // r0: first string |
| 7482 | // r1: second string |
| 7483 | // r2: length of first string |
| 7484 | // r3: length of second string |
| 7485 | // r4: first string instance type (if string_check_) |
| 7486 | // r5: second string instance type (if string_check_) |
| 7487 | // Look at the length of the result of adding the two strings. |
| 7488 | Label string_add_flat_result; |
| 7489 | // Adding two lengths can't overflow. |
| 7490 | ASSERT(String::kMaxLength * 2 > String::kMaxLength); |
| 7491 | __ add(r6, r2, Operand(r3)); |
| 7492 | // Use the runtime system when adding two one character strings, as it |
| 7493 | // contains optimizations for this specific case using the symbol table. |
| 7494 | __ cmp(r6, Operand(2)); |
| 7495 | __ b(eq, &string_add_runtime); |
| 7496 | // Check if resulting string will be flat. |
| 7497 | __ cmp(r6, Operand(String::kMinNonFlatLength)); |
| 7498 | __ b(lt, &string_add_flat_result); |
| 7499 | // Handle exceptionally long strings in the runtime system. |
| 7500 | ASSERT((String::kMaxLength & 0x80000000) == 0); |
| 7501 | ASSERT(IsPowerOf2(String::kMaxLength + 1)); |
| 7502 | // kMaxLength + 1 is representable as shifted literal, kMaxLength is not. |
| 7503 | __ cmp(r6, Operand(String::kMaxLength + 1)); |
| 7504 | __ b(hs, &string_add_runtime); |
| 7505 | |
| 7506 | // If result is not supposed to be flat, allocate a cons string object. |
| 7507 | // If both strings are ascii the result is an ascii cons string. |
| 7508 | if (!string_check_) { |
| 7509 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7510 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7511 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7512 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7513 | } |
| 7514 | Label non_ascii, allocated; |
| 7515 | ASSERT_EQ(0, kTwoByteStringTag); |
| 7516 | __ tst(r4, Operand(kStringEncodingMask)); |
| 7517 | __ tst(r5, Operand(kStringEncodingMask), ne); |
| 7518 | __ b(eq, &non_ascii); |
| 7519 | |
| 7520 | // Allocate an ASCII cons string. |
| 7521 | __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime); |
| 7522 | __ bind(&allocated); |
| 7523 | // Fill the fields of the cons string. |
| 7524 | __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset)); |
| 7525 | __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset)); |
| 7526 | __ mov(r0, Operand(r7)); |
| 7527 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7528 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7529 | __ Ret(); |
| 7530 | |
| 7531 | __ bind(&non_ascii); |
| 7532 | // Allocate a two byte cons string. |
| 7533 | __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime); |
| 7534 | __ jmp(&allocated); |
| 7535 | |
| 7536 | // Handle creating a flat result. First check that both strings are |
| 7537 | // sequential and that they have the same encoding. |
| 7538 | // r0: first string |
| 7539 | // r1: second string |
| 7540 | // r2: length of first string |
| 7541 | // r3: length of second string |
| 7542 | // r4: first string instance type (if string_check_) |
| 7543 | // r5: second string instance type (if string_check_) |
| 7544 | // r6: sum of lengths. |
| 7545 | __ bind(&string_add_flat_result); |
| 7546 | if (!string_check_) { |
| 7547 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7548 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 7549 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 7550 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 7551 | } |
| 7552 | // Check that both strings are sequential. |
| 7553 | ASSERT_EQ(0, kSeqStringTag); |
| 7554 | __ tst(r4, Operand(kStringRepresentationMask)); |
| 7555 | __ tst(r5, Operand(kStringRepresentationMask), eq); |
| 7556 | __ b(ne, &string_add_runtime); |
| 7557 | // Now check if both strings have the same encoding (ASCII/Two-byte). |
| 7558 | // r0: first string. |
| 7559 | // r1: second string. |
| 7560 | // r2: length of first string. |
| 7561 | // r3: length of second string. |
| 7562 | // r6: sum of lengths.. |
| 7563 | Label non_ascii_string_add_flat_result; |
| 7564 | ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test. |
| 7565 | __ eor(r7, r4, Operand(r5)); |
| 7566 | __ tst(r7, Operand(kStringEncodingMask)); |
| 7567 | __ b(ne, &string_add_runtime); |
| 7568 | // And see if it's ASCII or two-byte. |
| 7569 | __ tst(r4, Operand(kStringEncodingMask)); |
| 7570 | __ b(eq, &non_ascii_string_add_flat_result); |
| 7571 | |
| 7572 | // Both strings are sequential ASCII strings. We also know that they are |
| 7573 | // short (since the sum of the lengths is less than kMinNonFlatLength). |
| 7574 | __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 7575 | // Locate first character of result. |
| 7576 | __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7577 | // Locate first character of first argument. |
| 7578 | __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7579 | // r0: first character of first string. |
| 7580 | // r1: second string. |
| 7581 | // r2: length of first string. |
| 7582 | // r3: length of second string. |
| 7583 | // r6: first character of result. |
| 7584 | // r7: result string. |
| 7585 | GenerateCopyCharacters(masm, r6, r0, r2, r4, true); |
| 7586 | |
| 7587 | // Load second argument and locate first character. |
| 7588 | __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7589 | // r1: first character of second string. |
| 7590 | // r3: length of second string. |
| 7591 | // r6: next character of result. |
| 7592 | // r7: result string. |
| 7593 | GenerateCopyCharacters(masm, r6, r1, r3, r4, true); |
| 7594 | __ mov(r0, Operand(r7)); |
| 7595 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7596 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7597 | __ Ret(); |
| 7598 | |
| 7599 | __ bind(&non_ascii_string_add_flat_result); |
| 7600 | // Both strings are sequential two byte strings. |
| 7601 | // r0: first string. |
| 7602 | // r1: second string. |
| 7603 | // r2: length of first string. |
| 7604 | // r3: length of second string. |
| 7605 | // r6: sum of length of strings. |
| 7606 | __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 7607 | // r0: first string. |
| 7608 | // r1: second string. |
| 7609 | // r2: length of first string. |
| 7610 | // r3: length of second string. |
| 7611 | // r7: result string. |
| 7612 | |
| 7613 | // Locate first character of result. |
| 7614 | __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7615 | // Locate first character of first argument. |
| 7616 | __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7617 | |
| 7618 | // r0: first character of first string. |
| 7619 | // r1: second string. |
| 7620 | // r2: length of first string. |
| 7621 | // r3: length of second string. |
| 7622 | // r6: first character of result. |
| 7623 | // r7: result string. |
| 7624 | GenerateCopyCharacters(masm, r6, r0, r2, r4, false); |
| 7625 | |
| 7626 | // Locate first character of second argument. |
| 7627 | __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 7628 | |
| 7629 | // r1: first character of second string. |
| 7630 | // r3: length of second string. |
| 7631 | // r6: next character of result (after copy of first string). |
| 7632 | // r7: result string. |
| 7633 | GenerateCopyCharacters(masm, r6, r1, r3, r4, false); |
| 7634 | |
| 7635 | __ mov(r0, Operand(r7)); |
| 7636 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 7637 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 7638 | __ Ret(); |
| 7639 | |
| 7640 | // Just jump to runtime to add the two strings. |
| 7641 | __ bind(&string_add_runtime); |
| 7642 | __ TailCallRuntime(ExternalReference(Runtime::kStringAdd), 2, 1); |
| 7643 | } |
| 7644 | |
| 7645 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7646 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7647 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7648 | } } // namespace v8::internal |