sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "debug.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 34 | #include "ic-inl.h" |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 35 | #include "jsregexp.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 36 | #include "parser.h" |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 37 | #include "regexp-macro-assembler.h" |
| 38 | #include "regexp-stack.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 39 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 41 | #include "scopes.h" |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 42 | #include "virtual-frame-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 44 | namespace v8 { |
| 45 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 47 | #define __ ACCESS_MASM(masm_) |
| 48 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 49 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 50 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 51 | Condition cc, |
| 52 | bool never_nan_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 53 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 54 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 55 | Label* slow, |
| 56 | bool strict); |
| 57 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
| 58 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 59 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 60 | Register source, |
| 61 | Register destination, |
| 62 | int known_int); |
| 63 | static bool IsEasyToMultiplyBy(int x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 67 | // ------------------------------------------------------------------------- |
| 68 | // Platform-specific DeferredCode functions. |
| 69 | |
| 70 | void DeferredCode::SaveRegisters() { |
| 71 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 72 | int action = registers_[i]; |
| 73 | if (action == kPush) { |
| 74 | __ push(RegisterAllocator::ToRegister(i)); |
| 75 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 76 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void DeferredCode::RestoreRegisters() { |
| 83 | // Restore registers in reverse order due to the stack. |
| 84 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 85 | int action = registers_[i]; |
| 86 | if (action == kPush) { |
| 87 | __ pop(RegisterAllocator::ToRegister(i)); |
| 88 | } else if (action != kIgnore) { |
| 89 | action &= ~kSyncedFlag; |
| 90 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 95 | |
| 96 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 97 | // CodeGenState implementation. |
| 98 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 99 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 100 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 101 | true_target_(NULL), |
| 102 | false_target_(NULL), |
| 103 | previous_(NULL) { |
| 104 | owner_->set_state(this); |
| 105 | } |
| 106 | |
| 107 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 108 | CodeGenState::CodeGenState(CodeGenerator* owner, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 109 | JumpTarget* true_target, |
| 110 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 111 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 112 | true_target_(true_target), |
| 113 | false_target_(false_target), |
| 114 | previous_(owner->state()) { |
| 115 | owner_->set_state(this); |
| 116 | } |
| 117 | |
| 118 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 119 | CodeGenState::~CodeGenState() { |
| 120 | ASSERT(owner_->state() == this); |
| 121 | owner_->set_state(previous_); |
| 122 | } |
| 123 | |
| 124 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 125 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 126 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 127 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 128 | CodeGenerator::CodeGenerator(MacroAssembler* masm) |
| 129 | : deferred_(8), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 130 | masm_(masm), |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 131 | info_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 132 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 133 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | cc_reg_(al), |
| 135 | state_(NULL), |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 136 | loop_nesting_(0), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 137 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 142 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 144 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | // cp: callee's context |
| 146 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 147 | void CodeGenerator::Generate(CompilationInfo* info) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 148 | // Record the position for debugging purposes. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 149 | CodeForFunctionPosition(info->function()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 150 | Comment cmnt(masm_, "[ function compiled by virtual frame code generator"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | |
| 152 | // Initialize state. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 153 | info_ = info; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 154 | ASSERT(allocator_ == NULL); |
| 155 | RegisterAllocator register_allocator(this); |
| 156 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 157 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 158 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 159 | cc_reg_ = al; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 160 | |
| 161 | // Adjust for function-level loop nesting. |
| 162 | ASSERT_EQ(0, loop_nesting_); |
| 163 | loop_nesting_ = info->loop_nesting(); |
| 164 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 165 | { |
| 166 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 168 | // Entry: |
| 169 | // Stack: receiver, arguments |
| 170 | // lr: return address |
| 171 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 173 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 175 | allocator_->Initialize(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 176 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 177 | #ifdef DEBUG |
| 178 | if (strlen(FLAG_stop_at) > 0 && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 179 | info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 180 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 181 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 182 | } |
| 183 | #endif |
| 184 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 185 | if (info->mode() == CompilationInfo::PRIMARY) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 186 | frame_->Enter(); |
| 187 | // tos: code slot |
| 188 | |
| 189 | // Allocate space for locals and initialize them. This also checks |
| 190 | // for stack overflow. |
| 191 | frame_->AllocateStackSlots(); |
| 192 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 193 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 194 | int heap_slots = scope()->num_heap_slots(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 195 | if (heap_slots > 0) { |
| 196 | // Allocate local context. |
| 197 | // Get outer context and create a new context based on it. |
| 198 | __ ldr(r0, frame_->Function()); |
| 199 | frame_->EmitPush(r0); |
| 200 | if (heap_slots <= FastNewContextStub::kMaximumSlots) { |
| 201 | FastNewContextStub stub(heap_slots); |
| 202 | frame_->CallStub(&stub, 1); |
| 203 | } else { |
| 204 | frame_->CallRuntime(Runtime::kNewContext, 1); |
| 205 | } |
| 206 | |
| 207 | #ifdef DEBUG |
| 208 | JumpTarget verified_true; |
| 209 | __ cmp(r0, Operand(cp)); |
| 210 | verified_true.Branch(eq); |
| 211 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 212 | verified_true.Bind(); |
| 213 | #endif |
| 214 | // Update context local. |
| 215 | __ str(cp, frame_->Context()); |
| 216 | } |
| 217 | |
| 218 | // TODO(1241774): Improve this code: |
| 219 | // 1) only needed if we have a context |
| 220 | // 2) no need to recompute context ptr every single time |
| 221 | // 3) don't copy parameter operand code from SlotOperand! |
| 222 | { |
| 223 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 224 | // Note that iteration order is relevant here! If we have the same |
| 225 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 226 | // needs to be copied into the context, it must be the last argument |
| 227 | // passed to the parameter that needs to be copied. This is a rare |
| 228 | // case so we don't check for it, instead we rely on the copying |
| 229 | // order: such a parameter is copied repeatedly into the same |
| 230 | // context location and thus the last value is what is seen inside |
| 231 | // the function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 232 | for (int i = 0; i < scope()->num_parameters(); i++) { |
| 233 | Variable* par = scope()->parameter(i); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 234 | Slot* slot = par->slot(); |
| 235 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 236 | ASSERT(!scope()->is_global_scope()); // No params in global scope. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 237 | __ ldr(r1, frame_->ParameterAt(i)); |
| 238 | // Loads r2 with context; used below in RecordWrite. |
| 239 | __ str(r1, SlotOperand(slot, r2)); |
| 240 | // Load the offset into r3. |
| 241 | int slot_offset = |
| 242 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 243 | __ mov(r3, Operand(slot_offset)); |
| 244 | __ RecordWrite(r2, r3, r1); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Store the arguments object. This must happen after context |
| 250 | // initialization because the arguments object may be stored in the |
| 251 | // context. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 252 | if (scope()->arguments() != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 253 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 254 | ASSERT(scope()->arguments_shadow() != NULL); |
| 255 | Variable* arguments = scope()->arguments()->var(); |
| 256 | Variable* shadow = scope()->arguments_shadow()->var(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 257 | ASSERT(arguments != NULL && arguments->slot() != NULL); |
| 258 | ASSERT(shadow != NULL && shadow->slot() != NULL); |
| 259 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
| 260 | __ ldr(r2, frame_->Function()); |
| 261 | // The receiver is below the arguments, the return address, and the |
| 262 | // frame pointer on the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 263 | const int kReceiverDisplacement = 2 + scope()->num_parameters(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 264 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 265 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 266 | frame_->Adjust(3); |
| 267 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
| 268 | frame_->CallStub(&stub, 3); |
| 269 | frame_->EmitPush(r0); |
| 270 | StoreToSlot(arguments->slot(), NOT_CONST_INIT); |
| 271 | StoreToSlot(shadow->slot(), NOT_CONST_INIT); |
| 272 | frame_->Drop(); // Value is no longer needed. |
| 273 | } |
| 274 | |
| 275 | // Initialize ThisFunction reference if present. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 276 | if (scope()->is_function_scope() && scope()->function() != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 277 | __ mov(ip, Operand(Factory::the_hole_value())); |
| 278 | frame_->EmitPush(ip); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 279 | StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 280 | } |
| 281 | } else { |
| 282 | // When used as the secondary compiler for splitting, r1, cp, |
| 283 | // fp, and lr have been pushed on the stack. Adjust the virtual |
| 284 | // frame to match this state. |
| 285 | frame_->Adjust(4); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 286 | |
| 287 | // Bind all the bailout labels to the beginning of the function. |
| 288 | List<CompilationInfo::Bailout*>* bailouts = info->bailouts(); |
| 289 | for (int i = 0; i < bailouts->length(); i++) { |
| 290 | __ bind(bailouts->at(i)->label()); |
| 291 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 292 | } |
| 293 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 294 | // Initialize the function return target after the locals are set |
| 295 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 296 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 297 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 299 | // Generate code to 'execute' declarations and initialize functions |
| 300 | // (source elements). In case of an illegal redeclaration we need to |
| 301 | // handle that instead of processing the declarations. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 302 | if (scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 303 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 304 | scope()->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 305 | } else { |
| 306 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 307 | ProcessDeclarations(scope()->declarations()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 308 | // Bail out if a stack-overflow exception occurred when processing |
| 309 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 310 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | } |
| 312 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 313 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 314 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 315 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 316 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 317 | |
| 318 | // Compile the body of the function in a vanilla state. Don't |
| 319 | // bother compiling all the code if the scope has an illegal |
| 320 | // redeclaration. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 321 | if (!scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | Comment cmnt(masm_, "[ function body"); |
| 323 | #ifdef DEBUG |
| 324 | bool is_builtin = Bootstrapper::IsActive(); |
| 325 | bool should_trace = |
| 326 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 327 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 328 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 329 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 330 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 331 | #endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 332 | VisitStatementsAndSpill(info->function()->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 333 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 334 | } |
| 335 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 336 | // Generate the return sequence if necessary. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 337 | if (has_valid_frame() || function_return_.is_linked()) { |
| 338 | if (!function_return_.is_linked()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 339 | CodeForReturnPosition(info->function()); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 340 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 341 | // exit |
| 342 | // r0: result |
| 343 | // sp: stack pointer |
| 344 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 345 | // cp: callee's context |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 346 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 347 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 348 | function_return_.Bind(); |
| 349 | if (FLAG_trace) { |
| 350 | // Push the return value on the stack as the parameter. |
| 351 | // Runtime::TraceExit returns the parameter as it is. |
| 352 | frame_->EmitPush(r0); |
| 353 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 354 | } |
| 355 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 356 | // Add a label for checking the size of the code used for returning. |
| 357 | Label check_exit_codesize; |
| 358 | masm_->bind(&check_exit_codesize); |
| 359 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 360 | // Calculate the exact length of the return sequence and make sure that |
| 361 | // the constant pool is not emitted inside of the return sequence. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 362 | int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 363 | int return_sequence_length = Assembler::kJSReturnSequenceLength; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 364 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 365 | // Additional mov instruction generated. |
| 366 | return_sequence_length++; |
| 367 | } |
| 368 | masm_->BlockConstPoolFor(return_sequence_length); |
| 369 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 370 | // Tear down the frame which will restore the caller's frame pointer and |
| 371 | // the link register. |
| 372 | frame_->Exit(); |
| 373 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 374 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 375 | // tool from instrumenting as we rely on the code size here. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 376 | masm_->add(sp, sp, Operand(sp_delta)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 377 | masm_->Jump(lr); |
| 378 | |
| 379 | // 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] | 380 | // expected by the debugger. The add instruction above is an addressing |
| 381 | // mode 1 instruction where there are restrictions on which immediate values |
| 382 | // can be encoded in the instruction and which immediate values requires |
| 383 | // use of an additional instruction for moving the immediate to a temporary |
| 384 | // register. |
| 385 | ASSERT_EQ(return_sequence_length, |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 386 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 387 | } |
| 388 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 389 | // Adjust for function-level loop nesting. |
| 390 | ASSERT(loop_nesting_ == info->loop_nesting()); |
| 391 | loop_nesting_ = 0; |
| 392 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | ASSERT(!has_cc()); |
| 395 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 396 | ASSERT(!function_return_is_shadowed_); |
| 397 | function_return_.Unuse(); |
| 398 | DeleteFrame(); |
| 399 | |
| 400 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 401 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 402 | ProcessDeferred(); |
| 403 | } |
| 404 | |
| 405 | allocator_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 409 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 410 | // Currently, this assertion will fail if we try to assign to |
| 411 | // a constant variable that is constant because it is read-only |
| 412 | // (such as the variable referring to a named function expression). |
| 413 | // We need to implement assignments to read-only variables. |
| 414 | // Ideally, we should do this during AST generation (by converting |
| 415 | // such assignments into expression statements); however, in general |
| 416 | // we may not be able to make the decision until past AST generation, |
| 417 | // that is when the entire program is known. |
| 418 | ASSERT(slot != NULL); |
| 419 | int index = slot->index(); |
| 420 | switch (slot->type()) { |
| 421 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 422 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 423 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 424 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 425 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 426 | |
| 427 | case Slot::CONTEXT: { |
| 428 | // Follow the context chain if necessary. |
| 429 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 430 | Register context = cp; |
| 431 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 432 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 433 | // Load the closure. |
| 434 | // (All contexts, even 'with' contexts, have a closure, |
| 435 | // and it is the same for all contexts inside a function. |
| 436 | // There is no need to go to the function context first.) |
| 437 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 438 | // Load the function context (which is the incoming, outer context). |
| 439 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 440 | context = tmp; |
| 441 | } |
| 442 | // We may have a 'with' context now. Get the function context. |
| 443 | // (In fact this mov may never be the needed, since the scope analysis |
| 444 | // may not permit a direct context access in this case and thus we are |
| 445 | // always at a function context. However it is safe to dereference be- |
| 446 | // cause the function context of a function context is itself. Before |
| 447 | // deleting this mov we should try to create a counter-example first, |
| 448 | // though...) |
| 449 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 450 | return ContextOperand(tmp, index); |
| 451 | } |
| 452 | |
| 453 | default: |
| 454 | UNREACHABLE(); |
| 455 | return MemOperand(r0, 0); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 460 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 461 | Slot* slot, |
| 462 | Register tmp, |
| 463 | Register tmp2, |
| 464 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 465 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 466 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 467 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 468 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 469 | if (s->num_heap_slots() > 0) { |
| 470 | if (s->calls_eval()) { |
| 471 | // Check that 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 | } |
| 476 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 477 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 478 | context = tmp; |
| 479 | } |
| 480 | } |
| 481 | // Check that last extension is NULL. |
| 482 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 483 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 484 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 485 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 486 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 490 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 491 | // (partially) translated into branches, or it may have set the condition |
| 492 | // code register. If force_cc is set, the value is forced to set the |
| 493 | // condition code register and no value is pushed. If the condition code |
| 494 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 495 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 496 | void CodeGenerator::LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 497 | JumpTarget* true_target, |
| 498 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 499 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 500 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 501 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 502 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 503 | { CodeGenState new_state(this, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 504 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 505 | |
| 506 | // If we hit a stack overflow, we may not have actually visited |
| 507 | // the expression. In that case, we ensure that we have a |
| 508 | // valid-looking frame state because we will continue to generate |
| 509 | // code as we unwind the C++ stack. |
| 510 | // |
| 511 | // It's possible to have both a stack overflow and a valid frame |
| 512 | // state (eg, a subexpression overflowed, visiting it returned |
| 513 | // with a dummied frame state, and visiting this expression |
| 514 | // returned with a normal-looking state). |
| 515 | if (HasStackOverflow() && |
| 516 | has_valid_frame() && |
| 517 | !has_cc() && |
| 518 | frame_->height() == original_height) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 519 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 520 | true_target->Jump(); |
| 521 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 522 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 523 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 524 | // Convert the TOS value to a boolean in the condition code register. |
| 525 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 526 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 527 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 528 | ASSERT(!has_valid_frame() || |
| 529 | (has_cc() && frame_->height() == original_height) || |
| 530 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 534 | void CodeGenerator::Load(Expression* expr) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 535 | #ifdef DEBUG |
| 536 | int original_height = frame_->height(); |
| 537 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 538 | JumpTarget true_target; |
| 539 | JumpTarget false_target; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 540 | LoadCondition(expr, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 541 | |
| 542 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 543 | // Convert cc_reg_ into a boolean value. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 544 | VirtualFrame::SpilledScope scope(frame_); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 545 | JumpTarget loaded; |
| 546 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 547 | materialize_true.Branch(cc_reg_); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 548 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 549 | frame_->EmitPush(r0); |
| 550 | loaded.Jump(); |
| 551 | materialize_true.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 552 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 553 | frame_->EmitPush(r0); |
| 554 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 555 | cc_reg_ = al; |
| 556 | } |
| 557 | |
| 558 | if (true_target.is_linked() || false_target.is_linked()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 559 | VirtualFrame::SpilledScope scope(frame_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 560 | // We have at least one condition value that has been "translated" |
| 561 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 562 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 563 | if (frame_ != NULL) { |
| 564 | loaded.Jump(); // Don't lose the current TOS. |
| 565 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 566 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 567 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 568 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 569 | true_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 570 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 571 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 572 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 573 | // If both "true" and "false" need to be loaded jump across the code for |
| 574 | // "false". |
| 575 | if (both) { |
| 576 | loaded.Jump(); |
| 577 | } |
| 578 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 579 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 580 | false_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 581 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 582 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 584 | // A value is loaded on all paths reaching this point. |
| 585 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 586 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 587 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 589 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 593 | void CodeGenerator::LoadGlobal() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 594 | VirtualFrame::SpilledScope spilled_scope(frame_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 595 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 596 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 600 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 601 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 602 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 603 | __ ldr(scratch, |
| 604 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 605 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 609 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 610 | // Special handling of identifiers as subexpressions of typeof. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 611 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 612 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 613 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 614 | // For a global variable we build the property reference |
| 615 | // <global>.<variable> and perform a (regular non-contextual) property |
| 616 | // load to make sure we do not get reference errors. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 617 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 618 | Literal key(variable->name()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 619 | Property property(&global, &key, RelocInfo::kNoPosition); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 620 | Reference ref(this, &property); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 621 | ref.GetValue(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 622 | } else if (variable != NULL && variable->slot() != NULL) { |
| 623 | // For a variable that rewrites to a slot, we signal it is the immediate |
| 624 | // subexpression of a typeof. |
| 625 | LoadFromSlot(variable->slot(), INSIDE_TYPEOF); |
| 626 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 628 | // Anything else can be handled normally. |
| 629 | LoadAndSpill(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 634 | Reference::Reference(CodeGenerator* cgen, |
| 635 | Expression* expression, |
| 636 | bool persist_after_get) |
| 637 | : cgen_(cgen), |
| 638 | expression_(expression), |
| 639 | type_(ILLEGAL), |
| 640 | persist_after_get_(persist_after_get) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 641 | cgen->LoadReference(this); |
| 642 | } |
| 643 | |
| 644 | |
| 645 | Reference::~Reference() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 646 | ASSERT(is_unloaded() || is_illegal()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 650 | void CodeGenerator::LoadReference(Reference* ref) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 651 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 652 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 653 | Expression* e = ref->expression(); |
| 654 | Property* property = e->AsProperty(); |
| 655 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 656 | |
| 657 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 658 | // The expression is either a property or a variable proxy that rewrites |
| 659 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 660 | LoadAndSpill(property->obj()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 661 | if (property->key()->IsPropertyName()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 662 | ref->set_type(Reference::NAMED); |
| 663 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 664 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 665 | ref->set_type(Reference::KEYED); |
| 666 | } |
| 667 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 668 | // The expression is a variable proxy that does not rewrite to a |
| 669 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 671 | LoadGlobal(); |
| 672 | ref->set_type(Reference::NAMED); |
| 673 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 674 | ASSERT(var->slot() != NULL); |
| 675 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 676 | } |
| 677 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 678 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 679 | LoadAndSpill(e); |
| 680 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
| 684 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 685 | void CodeGenerator::UnloadReference(Reference* ref) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 686 | int size = ref->size(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 687 | ref->set_unloaded(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 688 | if (size == 0) return; |
| 689 | |
| 690 | // Pop a reference from the stack while preserving TOS. |
| 691 | VirtualFrame::RegisterAllocationScope scope(this); |
| 692 | Comment cmnt(masm_, "[ UnloadReference"); |
| 693 | if (size > 0) { |
| 694 | Register tos = frame_->PopToRegister(); |
| 695 | frame_->Drop(size); |
| 696 | frame_->EmitPush(tos); |
| 697 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 701 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 702 | // register to a boolean in the condition code register. The code |
| 703 | // 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] | 704 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 705 | JumpTarget* false_target) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 706 | VirtualFrame::SpilledScope spilled_scope(frame_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 707 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 708 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 709 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 710 | |
| 711 | // Fast case checks |
| 712 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 713 | // Check if the value is 'false'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 714 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 715 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 716 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 717 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 718 | // Check if the value is 'true'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 719 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 720 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 721 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 722 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 723 | // Check if the value is 'undefined'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 724 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 725 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 726 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 727 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 728 | // Check if the value is a smi. |
| 729 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 730 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 731 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 732 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 733 | |
| 734 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 735 | frame_->EmitPush(r0); |
| 736 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 737 | // Convert the result (r0) to a condition code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 738 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 739 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 740 | |
| 741 | cc_reg_ = ne; |
| 742 | } |
| 743 | |
| 744 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 745 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 746 | OverwriteMode overwrite_mode, |
| 747 | int constant_rhs) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 748 | VirtualFrame::SpilledScope spilled_scope(frame_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 749 | // sp[0] : y |
| 750 | // sp[1] : x |
| 751 | // result : r0 |
| 752 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 753 | // Stub is entered with a call: 'return address' is in lr. |
| 754 | switch (op) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 755 | case Token::ADD: |
| 756 | case Token::SUB: |
| 757 | case Token::MUL: |
| 758 | case Token::DIV: |
| 759 | case Token::MOD: |
| 760 | case Token::BIT_OR: |
| 761 | case Token::BIT_AND: |
| 762 | case Token::BIT_XOR: |
| 763 | case Token::SHL: |
| 764 | case Token::SHR: |
| 765 | case Token::SAR: { |
| 766 | frame_->EmitPop(r0); // r0 : y |
| 767 | frame_->EmitPop(r1); // r1 : x |
| 768 | GenericBinaryOpStub stub(op, overwrite_mode, r1, r0, constant_rhs); |
| 769 | frame_->CallStub(&stub, 0); |
| 770 | break; |
| 771 | } |
| 772 | |
| 773 | case Token::COMMA: |
| 774 | frame_->EmitPop(r0); |
| 775 | // Simply discard left value. |
| 776 | frame_->Drop(); |
| 777 | break; |
| 778 | |
| 779 | default: |
| 780 | // Other cases should have been handled before this point. |
| 781 | UNREACHABLE(); |
| 782 | break; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | |
| 787 | void CodeGenerator::VirtualFrameBinaryOperation(Token::Value op, |
| 788 | OverwriteMode overwrite_mode, |
| 789 | int constant_rhs) { |
| 790 | // top of virtual frame: y |
| 791 | // 2nd elt. on virtual frame : x |
| 792 | // result : top of virtual frame |
| 793 | |
| 794 | // Stub is entered with a call: 'return address' is in lr. |
| 795 | switch (op) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 796 | case Token::ADD: // fall through. |
| 797 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 798 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 799 | case Token::DIV: |
| 800 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 801 | case Token::BIT_OR: |
| 802 | case Token::BIT_AND: |
| 803 | case Token::BIT_XOR: |
| 804 | case Token::SHL: |
| 805 | case Token::SHR: |
| 806 | case Token::SAR: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 807 | Register rhs = frame_->PopToRegister(); |
| 808 | Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register. |
| 809 | { |
| 810 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 811 | GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs); |
| 812 | frame_->CallStub(&stub, 0); |
| 813 | } |
| 814 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 815 | break; |
| 816 | } |
| 817 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 818 | case Token::COMMA: { |
| 819 | Register scratch = frame_->PopToRegister(); |
| 820 | // Simply discard left value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 821 | frame_->Drop(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 822 | frame_->EmitPush(scratch); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 823 | break; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 824 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 825 | |
| 826 | default: |
| 827 | // Other cases should have been handled before this point. |
| 828 | UNREACHABLE(); |
| 829 | break; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 834 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 835 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 836 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 837 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 838 | bool reversed, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 839 | OverwriteMode overwrite_mode, |
| 840 | Register tos) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 841 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 842 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 843 | reversed_(reversed), |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 844 | overwrite_mode_(overwrite_mode), |
| 845 | tos_register_(tos) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 846 | set_comment("[ DeferredInlinedSmiOperation"); |
| 847 | } |
| 848 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 849 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 850 | |
| 851 | private: |
| 852 | Token::Value op_; |
| 853 | int value_; |
| 854 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 855 | OverwriteMode overwrite_mode_; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 856 | Register tos_register_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 857 | }; |
| 858 | |
| 859 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 860 | void DeferredInlineSmiOperation::Generate() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 861 | Register lhs = r1; |
| 862 | Register rhs = r0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 863 | switch (op_) { |
| 864 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 865 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 866 | if (reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 867 | __ sub(r0, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 868 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 869 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 870 | __ sub(r1, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 871 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 872 | } |
| 873 | break; |
| 874 | } |
| 875 | |
| 876 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 877 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 878 | if (reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 879 | __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 880 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 881 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 882 | __ add(r1, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 883 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 884 | } |
| 885 | break; |
| 886 | } |
| 887 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 888 | // For these operations there is no optimistic operation that needs to be |
| 889 | // reverted. |
| 890 | case Token::MUL: |
| 891 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 892 | case Token::BIT_OR: |
| 893 | case Token::BIT_XOR: |
| 894 | case Token::BIT_AND: { |
| 895 | if (reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 896 | if (tos_register_.is(r0)) { |
| 897 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 898 | } else { |
| 899 | ASSERT(tos_register_.is(r1)); |
| 900 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 901 | lhs = r0; |
| 902 | rhs = r1; |
| 903 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 904 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 905 | if (tos_register_.is(r1)) { |
| 906 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 907 | } else { |
| 908 | ASSERT(tos_register_.is(r0)); |
| 909 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 910 | lhs = r0; |
| 911 | rhs = r1; |
| 912 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 913 | } |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | case Token::SHL: |
| 918 | case Token::SHR: |
| 919 | case Token::SAR: { |
| 920 | if (!reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 921 | if (tos_register_.is(r1)) { |
| 922 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 923 | } else { |
| 924 | ASSERT(tos_register_.is(r0)); |
| 925 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 926 | lhs = r0; |
| 927 | rhs = r1; |
| 928 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 929 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 930 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 931 | } |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 936 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 937 | UNREACHABLE(); |
| 938 | break; |
| 939 | } |
| 940 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 941 | GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 942 | __ CallStub(&stub); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 943 | // The generic stub returns its value in r0, but that's not |
| 944 | // necessarily what we want. We want whatever the inlined code |
| 945 | // expected, which is that the answer is in the same register as |
| 946 | // the operand was. |
| 947 | __ Move(tos_register_, r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 951 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 952 | x &= x - 1; |
| 953 | return (x & (x - 1)) == 0; |
| 954 | } |
| 955 | |
| 956 | |
| 957 | // Returns the index of the lowest bit set. |
| 958 | static int BitPosition(unsigned x) { |
| 959 | int bit_posn = 0; |
| 960 | while ((x & 0xf) == 0) { |
| 961 | bit_posn += 4; |
| 962 | x >>= 4; |
| 963 | } |
| 964 | while ((x & 1) == 0) { |
| 965 | bit_posn++; |
| 966 | x >>= 1; |
| 967 | } |
| 968 | return bit_posn; |
| 969 | } |
| 970 | |
| 971 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 972 | void CodeGenerator::VirtualFrameSmiOperation(Token::Value op, |
| 973 | Handle<Object> value, |
| 974 | bool reversed, |
| 975 | OverwriteMode mode) { |
| 976 | int int_value = Smi::cast(*value)->value(); |
| 977 | |
| 978 | bool something_to_inline; |
| 979 | switch (op) { |
| 980 | case Token::ADD: |
| 981 | case Token::SUB: |
| 982 | case Token::BIT_AND: |
| 983 | case Token::BIT_OR: |
| 984 | case Token::BIT_XOR: { |
| 985 | something_to_inline = true; |
| 986 | break; |
| 987 | } |
| 988 | case Token::SHL: |
| 989 | case Token::SHR: |
| 990 | case Token::SAR: { |
| 991 | if (reversed) { |
| 992 | something_to_inline = false; |
| 993 | } else { |
| 994 | something_to_inline = true; |
| 995 | } |
| 996 | break; |
| 997 | } |
| 998 | case Token::MOD: { |
| 999 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 1000 | something_to_inline = false; |
| 1001 | } else { |
| 1002 | something_to_inline = true; |
| 1003 | } |
| 1004 | break; |
| 1005 | } |
| 1006 | case Token::MUL: { |
| 1007 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1008 | something_to_inline = false; |
| 1009 | } else { |
| 1010 | something_to_inline = true; |
| 1011 | } |
| 1012 | break; |
| 1013 | } |
| 1014 | default: { |
| 1015 | something_to_inline = false; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | if (!something_to_inline) { |
| 1021 | if (!reversed) { |
| 1022 | // Push the rhs onto the virtual frame by putting it in a TOS register. |
| 1023 | Register rhs = frame_->GetTOSRegister(); |
| 1024 | __ mov(rhs, Operand(value)); |
| 1025 | frame_->EmitPush(rhs); |
| 1026 | VirtualFrameBinaryOperation(op, mode, int_value); |
| 1027 | } else { |
| 1028 | // Pop the rhs, then push lhs and rhs in the right order. Only performs |
| 1029 | // at most one pop, the rest takes place in TOS registers. |
| 1030 | Register lhs = frame_->GetTOSRegister(); // Get reg for pushing. |
| 1031 | Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this. |
| 1032 | __ mov(lhs, Operand(value)); |
| 1033 | frame_->EmitPush(lhs); |
| 1034 | frame_->EmitPush(rhs); |
| 1035 | VirtualFrameBinaryOperation(op, mode, kUnknownIntValue); |
| 1036 | } |
| 1037 | return; |
| 1038 | } |
| 1039 | |
| 1040 | // We move the top of stack to a register (normally no move is invoved). |
| 1041 | Register tos = frame_->PopToRegister(); |
| 1042 | // All other registers are spilled. The deferred code expects one argument |
| 1043 | // in a register and all other values are flushed to the stack. The |
| 1044 | // answer is returned in the same register that the top of stack argument was |
| 1045 | // in. |
| 1046 | frame_->SpillAll(); |
| 1047 | |
| 1048 | switch (op) { |
| 1049 | case Token::ADD: { |
| 1050 | DeferredCode* deferred = |
| 1051 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1052 | |
| 1053 | __ add(tos, tos, Operand(value), SetCC); |
| 1054 | deferred->Branch(vs); |
| 1055 | __ tst(tos, Operand(kSmiTagMask)); |
| 1056 | deferred->Branch(ne); |
| 1057 | deferred->BindExit(); |
| 1058 | frame_->EmitPush(tos); |
| 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | case Token::SUB: { |
| 1063 | DeferredCode* deferred = |
| 1064 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1065 | |
| 1066 | if (reversed) { |
| 1067 | __ rsb(tos, tos, Operand(value), SetCC); |
| 1068 | } else { |
| 1069 | __ sub(tos, tos, Operand(value), SetCC); |
| 1070 | } |
| 1071 | deferred->Branch(vs); |
| 1072 | __ tst(tos, Operand(kSmiTagMask)); |
| 1073 | deferred->Branch(ne); |
| 1074 | deferred->BindExit(); |
| 1075 | frame_->EmitPush(tos); |
| 1076 | break; |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | case Token::BIT_OR: |
| 1081 | case Token::BIT_XOR: |
| 1082 | case Token::BIT_AND: { |
| 1083 | DeferredCode* deferred = |
| 1084 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1085 | __ tst(tos, Operand(kSmiTagMask)); |
| 1086 | deferred->Branch(ne); |
| 1087 | switch (op) { |
| 1088 | case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break; |
| 1089 | case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break; |
| 1090 | case Token::BIT_AND: __ and_(tos, tos, Operand(value)); break; |
| 1091 | default: UNREACHABLE(); |
| 1092 | } |
| 1093 | deferred->BindExit(); |
| 1094 | frame_->EmitPush(tos); |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | case Token::SHL: |
| 1099 | case Token::SHR: |
| 1100 | case Token::SAR: { |
| 1101 | ASSERT(!reversed); |
| 1102 | Register scratch = VirtualFrame::scratch0(); |
| 1103 | Register scratch2 = VirtualFrame::scratch1(); |
| 1104 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 1105 | DeferredCode* deferred = |
| 1106 | new DeferredInlineSmiOperation(op, shift_value, false, mode, tos); |
| 1107 | __ tst(tos, Operand(kSmiTagMask)); |
| 1108 | deferred->Branch(ne); |
| 1109 | __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // remove tags |
| 1110 | switch (op) { |
| 1111 | case Token::SHL: { |
| 1112 | if (shift_value != 0) { |
| 1113 | __ mov(scratch, Operand(scratch, LSL, shift_value)); |
| 1114 | } |
| 1115 | // check that the *signed* result fits in a smi |
| 1116 | __ add(scratch2, scratch, Operand(0x40000000), SetCC); |
| 1117 | deferred->Branch(mi); |
| 1118 | break; |
| 1119 | } |
| 1120 | case Token::SHR: { |
| 1121 | // LSR by immediate 0 means shifting 32 bits. |
| 1122 | if (shift_value != 0) { |
| 1123 | __ mov(scratch, Operand(scratch, LSR, shift_value)); |
| 1124 | } |
| 1125 | // check that the *unsigned* result fits in a smi |
| 1126 | // neither of the two high-order bits can be set: |
| 1127 | // - 0x80000000: high bit would be lost when smi tagging |
| 1128 | // - 0x40000000: this number would convert to negative when |
| 1129 | // smi tagging these two cases can only happen with shifts |
| 1130 | // by 0 or 1 when handed a valid smi |
| 1131 | __ tst(scratch, Operand(0xc0000000)); |
| 1132 | deferred->Branch(ne); |
| 1133 | break; |
| 1134 | } |
| 1135 | case Token::SAR: { |
| 1136 | if (shift_value != 0) { |
| 1137 | // ASR by immediate 0 means shifting 32 bits. |
| 1138 | __ mov(scratch, Operand(scratch, ASR, shift_value)); |
| 1139 | } |
| 1140 | break; |
| 1141 | } |
| 1142 | default: UNREACHABLE(); |
| 1143 | } |
| 1144 | __ mov(tos, Operand(scratch, LSL, kSmiTagSize)); |
| 1145 | deferred->BindExit(); |
| 1146 | frame_->EmitPush(tos); |
| 1147 | break; |
| 1148 | } |
| 1149 | |
| 1150 | case Token::MOD: { |
| 1151 | ASSERT(!reversed); |
| 1152 | ASSERT(int_value >= 2); |
| 1153 | ASSERT(IsPowerOf2(int_value)); |
| 1154 | DeferredCode* deferred = |
| 1155 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1156 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1157 | __ tst(tos, Operand(mask)); |
| 1158 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1159 | mask = (int_value << kSmiTagSize) - 1; |
| 1160 | __ and_(tos, tos, Operand(mask)); |
| 1161 | deferred->BindExit(); |
| 1162 | frame_->EmitPush(tos); |
| 1163 | break; |
| 1164 | } |
| 1165 | |
| 1166 | case Token::MUL: { |
| 1167 | ASSERT(IsEasyToMultiplyBy(int_value)); |
| 1168 | DeferredCode* deferred = |
| 1169 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1170 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1171 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1172 | unsigned mask = 0x80000000u; |
| 1173 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1174 | mask |= mask >> 1; |
| 1175 | } |
| 1176 | mask |= kSmiTagMask; |
| 1177 | // This does a single mask that checks for a too high value in a |
| 1178 | // conservative way and for a non-Smi. It also filters out negative |
| 1179 | // numbers, unfortunately, but since this code is inline we prefer |
| 1180 | // brevity to comprehensiveness. |
| 1181 | __ tst(tos, Operand(mask)); |
| 1182 | deferred->Branch(ne); |
| 1183 | MultiplyByKnownInt(masm_, tos, tos, int_value); |
| 1184 | deferred->BindExit(); |
| 1185 | frame_->EmitPush(tos); |
| 1186 | break; |
| 1187 | } |
| 1188 | |
| 1189 | default: |
| 1190 | UNREACHABLE(); |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1196 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1197 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1198 | bool reversed, |
| 1199 | OverwriteMode mode) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1200 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1201 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 1202 | // some possible smi operations (like + and -) when (at least) one |
| 1203 | // of the operands is a literal smi. With this optimization, the |
| 1204 | // performance of the system is increased by ~15%, and the generated |
| 1205 | // code size is increased by ~1% (measured on a combination of |
| 1206 | // different benchmarks). |
| 1207 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1208 | // sp[0] : operand |
| 1209 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1210 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1211 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1212 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1213 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1214 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1215 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1216 | switch (op) { |
| 1217 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1218 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1219 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1220 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1221 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1222 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1223 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1224 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1225 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1226 | break; |
| 1227 | } |
| 1228 | |
| 1229 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1230 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1231 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1232 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1233 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1234 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1235 | } else { |
| 1236 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1237 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1238 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1239 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1240 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1241 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1244 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1245 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1246 | case Token::BIT_OR: |
| 1247 | case Token::BIT_XOR: |
| 1248 | case Token::BIT_AND: { |
| 1249 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1250 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1251 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1252 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1253 | switch (op) { |
| 1254 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 1255 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 1256 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 1257 | default: UNREACHABLE(); |
| 1258 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1259 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1260 | break; |
| 1261 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1262 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1263 | case Token::SHL: |
| 1264 | case Token::SHR: |
| 1265 | case Token::SAR: { |
| 1266 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1267 | something_to_inline = false; |
| 1268 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1269 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1270 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 1271 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1272 | new DeferredInlineSmiOperation(op, shift_value, false, mode, r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1273 | __ tst(r0, Operand(kSmiTagMask)); |
| 1274 | deferred->Branch(ne); |
| 1275 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 1276 | switch (op) { |
| 1277 | case Token::SHL: { |
| 1278 | if (shift_value != 0) { |
| 1279 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 1280 | } |
| 1281 | // check that the *unsigned* result fits in a smi |
| 1282 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 1283 | deferred->Branch(mi); |
| 1284 | break; |
| 1285 | } |
| 1286 | case Token::SHR: { |
| 1287 | // LSR by immediate 0 means shifting 32 bits. |
| 1288 | if (shift_value != 0) { |
| 1289 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 1290 | } |
| 1291 | // check that the *unsigned* result fits in a smi |
| 1292 | // neither of the two high-order bits can be set: |
| 1293 | // - 0x80000000: high bit would be lost when smi tagging |
| 1294 | // - 0x40000000: this number would convert to negative when |
| 1295 | // smi tagging these two cases can only happen with shifts |
| 1296 | // by 0 or 1 when handed a valid smi |
| 1297 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 1298 | deferred->Branch(ne); |
| 1299 | break; |
| 1300 | } |
| 1301 | case Token::SAR: { |
| 1302 | if (shift_value != 0) { |
| 1303 | // ASR by immediate 0 means shifting 32 bits. |
| 1304 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 1305 | } |
| 1306 | break; |
| 1307 | } |
| 1308 | default: UNREACHABLE(); |
| 1309 | } |
| 1310 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 1311 | deferred->BindExit(); |
| 1312 | break; |
| 1313 | } |
| 1314 | |
| 1315 | case Token::MOD: { |
| 1316 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 1317 | something_to_inline = false; |
| 1318 | break; |
| 1319 | } |
| 1320 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1321 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1322 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1323 | __ tst(r0, Operand(mask)); |
| 1324 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1325 | mask = (int_value << kSmiTagSize) - 1; |
| 1326 | __ and_(r0, r0, Operand(mask)); |
| 1327 | deferred->BindExit(); |
| 1328 | break; |
| 1329 | } |
| 1330 | |
| 1331 | case Token::MUL: { |
| 1332 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1333 | something_to_inline = false; |
| 1334 | break; |
| 1335 | } |
| 1336 | DeferredCode* deferred = |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1337 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1338 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1339 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1340 | unsigned mask = 0x80000000u; |
| 1341 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1342 | mask |= mask >> 1; |
| 1343 | } |
| 1344 | mask |= kSmiTagMask; |
| 1345 | // This does a single mask that checks for a too high value in a |
| 1346 | // conservative way and for a non-Smi. It also filters out negative |
| 1347 | // numbers, unfortunately, but since this code is inline we prefer |
| 1348 | // brevity to comprehensiveness. |
| 1349 | __ tst(r0, Operand(mask)); |
| 1350 | deferred->Branch(ne); |
| 1351 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 1352 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1353 | break; |
| 1354 | } |
| 1355 | |
| 1356 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1357 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1358 | break; |
| 1359 | } |
| 1360 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1361 | if (!something_to_inline) { |
| 1362 | if (!reversed) { |
| 1363 | frame_->EmitPush(r0); |
| 1364 | __ mov(r0, Operand(value)); |
| 1365 | frame_->EmitPush(r0); |
| 1366 | GenericBinaryOperation(op, mode, int_value); |
| 1367 | } else { |
| 1368 | __ mov(ip, Operand(value)); |
| 1369 | frame_->EmitPush(ip); |
| 1370 | frame_->EmitPush(r0); |
| 1371 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1372 | } |
| 1373 | } |
| 1374 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1375 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1379 | void CodeGenerator::Comparison(Condition cc, |
| 1380 | Expression* left, |
| 1381 | Expression* right, |
| 1382 | bool strict) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1383 | VirtualFrame::RegisterAllocationScope scope(this); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1384 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1385 | if (left != NULL) Load(left); |
| 1386 | if (right != NULL) Load(right); |
| 1387 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1388 | // sp[0] : y |
| 1389 | // sp[1] : x |
| 1390 | // result : cc register |
| 1391 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | // Strict only makes sense for equality comparisons. |
| 1393 | ASSERT(!strict || cc == eq); |
| 1394 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1395 | Register lhs; |
| 1396 | Register rhs; |
| 1397 | |
| 1398 | // We load the top two stack positions into registers chosen by the virtual |
| 1399 | // frame. This should keep the register shuffling to a minimum. |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1400 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1401 | if (cc == gt || cc == le) { |
| 1402 | cc = ReverseCondition(cc); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1403 | lhs = frame_->PopToRegister(); |
| 1404 | rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again! |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1405 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1406 | rhs = frame_->PopToRegister(); |
| 1407 | lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again! |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1408 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1409 | |
| 1410 | ASSERT(rhs.is(r0) || rhs.is(r1)); |
| 1411 | ASSERT(lhs.is(r0) || lhs.is(r1)); |
| 1412 | |
| 1413 | // Now we have the two sides in r0 and r1. We flush any other registers |
| 1414 | // because the stub doesn't know about register allocation. |
| 1415 | frame_->SpillAll(); |
| 1416 | Register scratch = VirtualFrame::scratch0(); |
| 1417 | __ orr(scratch, lhs, Operand(rhs)); |
| 1418 | __ tst(scratch, Operand(kSmiTagMask)); |
| 1419 | JumpTarget smi; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1420 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1421 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1422 | // Perform non-smi comparison by stub. |
| 1423 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1424 | // We call with 0 args because there are 0 on the stack. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1425 | if (!rhs.is(r0)) { |
| 1426 | __ Swap(rhs, lhs, ip); |
| 1427 | } |
| 1428 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1429 | CompareStub stub(cc, strict); |
| 1430 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1431 | __ cmp(r0, Operand(0)); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1432 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1433 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1434 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1435 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1436 | smi.Bind(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1437 | __ cmp(lhs, Operand(rhs)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1438 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1439 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1440 | cc_reg_ = cc; |
| 1441 | } |
| 1442 | |
| 1443 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1444 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1445 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1446 | CallFunctionFlags flags, |
| 1447 | int position) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1448 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1450 | int arg_count = args->length(); |
| 1451 | for (int i = 0; i < arg_count; i++) { |
| 1452 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1453 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1454 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1455 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1456 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1457 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1458 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1459 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1460 | CallFunctionStub call_function(arg_count, in_loop, flags); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1461 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1462 | |
| 1463 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1464 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1465 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1469 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1470 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1471 | ASSERT(has_cc()); |
| 1472 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1473 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1474 | cc_reg_ = al; |
| 1475 | } |
| 1476 | |
| 1477 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1478 | void CodeGenerator::CheckStack() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1479 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1480 | Comment cmnt(masm_, "[ check stack"); |
| 1481 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1482 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1483 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1484 | // gives a return address 12 bytes down. |
| 1485 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1486 | masm_->cmp(sp, Operand(ip)); |
| 1487 | StackCheckStub stub; |
| 1488 | // Call the stub if lower. |
| 1489 | masm_->mov(pc, |
| 1490 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1491 | RelocInfo::CODE_TARGET), |
| 1492 | LeaveCC, |
| 1493 | lo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1497 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1498 | #ifdef DEBUG |
| 1499 | int original_height = frame_->height(); |
| 1500 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1501 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1502 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1503 | VisitAndSpill(statements->at(i)); |
| 1504 | } |
| 1505 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1506 | } |
| 1507 | |
| 1508 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1509 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1510 | #ifdef DEBUG |
| 1511 | int original_height = frame_->height(); |
| 1512 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1513 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1514 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1515 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1516 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1517 | VisitStatementsAndSpill(node->statements()); |
| 1518 | if (node->break_target()->is_linked()) { |
| 1519 | node->break_target()->Bind(); |
| 1520 | } |
| 1521 | node->break_target()->Unuse(); |
| 1522 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1526 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1527 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1528 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1529 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1530 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1531 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1532 | frame_->EmitPush(r0); |
| 1533 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1534 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1538 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1539 | #ifdef DEBUG |
| 1540 | int original_height = frame_->height(); |
| 1541 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1542 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1543 | Comment cmnt(masm_, "[ Declaration"); |
| 1544 | Variable* var = node->proxy()->var(); |
| 1545 | ASSERT(var != NULL); // must have been resolved |
| 1546 | Slot* slot = var->slot(); |
| 1547 | |
| 1548 | // If it was not possible to allocate the variable at compile time, |
| 1549 | // we need to "declare" it at runtime to make sure it actually |
| 1550 | // exists in the local context. |
| 1551 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1552 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1553 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1554 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1555 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1556 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1557 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1558 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1559 | // Declaration nodes are always declared in only two modes. |
| 1560 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1561 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1562 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1563 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1564 | // Push initial value, if any. |
| 1565 | // Note: For variables we must not push an initial value (such as |
| 1566 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1567 | // must not destroy the current value. |
| 1568 | if (node->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1569 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1570 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1571 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1572 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1573 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1574 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1575 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1576 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1577 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1578 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1579 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | ASSERT(!var->is_global()); |
| 1584 | |
| 1585 | // If we have a function or a constant, we need to initialize the variable. |
| 1586 | Expression* val = NULL; |
| 1587 | if (node->mode() == Variable::CONST) { |
| 1588 | val = new Literal(Factory::the_hole_value()); |
| 1589 | } else { |
| 1590 | val = node->fun(); // NULL if we don't have a function |
| 1591 | } |
| 1592 | |
| 1593 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1594 | { |
| 1595 | // Set initial value. |
| 1596 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1597 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1598 | target.SetValue(NOT_CONST_INIT); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1599 | } |
| 1600 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1601 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1602 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1603 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1607 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1608 | #ifdef DEBUG |
| 1609 | int original_height = frame_->height(); |
| 1610 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1611 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1612 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1613 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1614 | Expression* expression = node->expression(); |
| 1615 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1616 | LoadAndSpill(expression); |
| 1617 | frame_->Drop(); |
| 1618 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1622 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1623 | #ifdef DEBUG |
| 1624 | int original_height = frame_->height(); |
| 1625 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1626 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1627 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1628 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1629 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1630 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1634 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1635 | #ifdef DEBUG |
| 1636 | int original_height = frame_->height(); |
| 1637 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1638 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1639 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1640 | // Generate different code depending on which parts of the if statement |
| 1641 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1642 | bool has_then_stm = node->HasThenStatement(); |
| 1643 | bool has_else_stm = node->HasElseStatement(); |
| 1644 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1645 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1646 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1647 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1648 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1649 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1650 | JumpTarget then; |
| 1651 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1652 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1653 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1654 | if (frame_ != NULL) { |
| 1655 | Branch(false, &else_); |
| 1656 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1657 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1658 | if (frame_ != NULL || then.is_linked()) { |
| 1659 | then.Bind(); |
| 1660 | VisitAndSpill(node->then_statement()); |
| 1661 | } |
| 1662 | if (frame_ != NULL) { |
| 1663 | exit.Jump(); |
| 1664 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1665 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1666 | if (else_.is_linked()) { |
| 1667 | else_.Bind(); |
| 1668 | VisitAndSpill(node->else_statement()); |
| 1669 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1670 | |
| 1671 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1672 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1673 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1674 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1675 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1676 | LoadConditionAndSpill(node->condition(), &then, &exit, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1677 | if (frame_ != NULL) { |
| 1678 | Branch(false, &exit); |
| 1679 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1680 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1681 | if (frame_ != NULL || then.is_linked()) { |
| 1682 | then.Bind(); |
| 1683 | VisitAndSpill(node->then_statement()); |
| 1684 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1685 | |
| 1686 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1687 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1688 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1689 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1690 | // if (!cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1691 | LoadConditionAndSpill(node->condition(), &exit, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1692 | if (frame_ != NULL) { |
| 1693 | Branch(true, &exit); |
| 1694 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1695 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1696 | if (frame_ != NULL || else_.is_linked()) { |
| 1697 | else_.Bind(); |
| 1698 | VisitAndSpill(node->else_statement()); |
| 1699 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1700 | |
| 1701 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1702 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1703 | ASSERT(!has_then_stm && !has_else_stm); |
| 1704 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1705 | LoadConditionAndSpill(node->condition(), &exit, &exit, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1706 | if (frame_ != NULL) { |
| 1707 | if (has_cc()) { |
| 1708 | cc_reg_ = al; |
| 1709 | } else { |
| 1710 | frame_->Drop(); |
| 1711 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1716 | if (exit.is_linked()) { |
| 1717 | exit.Bind(); |
| 1718 | } |
| 1719 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1723 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1724 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1725 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1726 | CodeForStatementPosition(node); |
| 1727 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1731 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1732 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1733 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1734 | CodeForStatementPosition(node); |
| 1735 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1739 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1740 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1741 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1742 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1743 | CodeForStatementPosition(node); |
| 1744 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1745 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1746 | frame_->EmitPop(r0); |
| 1747 | function_return_.Jump(); |
| 1748 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1749 | // Pop the result from the frame and prepare the frame for |
| 1750 | // returning thus making it easier to merge. |
| 1751 | frame_->EmitPop(r0); |
| 1752 | frame_->PrepareForReturn(); |
| 1753 | |
| 1754 | function_return_.Jump(); |
| 1755 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1759 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1760 | #ifdef DEBUG |
| 1761 | int original_height = frame_->height(); |
| 1762 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1763 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1764 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1765 | CodeForStatementPosition(node); |
| 1766 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1767 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1768 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1769 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1770 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1771 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1772 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1773 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1774 | __ cmp(r0, Operand(cp)); |
| 1775 | verified_true.Branch(eq); |
| 1776 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1777 | verified_true.Bind(); |
| 1778 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1779 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1780 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1781 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1785 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1786 | #ifdef DEBUG |
| 1787 | int original_height = frame_->height(); |
| 1788 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1789 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1790 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1791 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1792 | // Pop context. |
| 1793 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1794 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1795 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1796 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1800 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1801 | #ifdef DEBUG |
| 1802 | int original_height = frame_->height(); |
| 1803 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1804 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1805 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1806 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1807 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1808 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1809 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1810 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1811 | JumpTarget next_test; |
| 1812 | JumpTarget fall_through; |
| 1813 | JumpTarget default_entry; |
| 1814 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1815 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1816 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1817 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1818 | |
| 1819 | for (int i = 0; i < length; i++) { |
| 1820 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1821 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1822 | // Remember the default clause and compile it at the end. |
| 1823 | default_clause = clause; |
| 1824 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1827 | Comment cmnt(masm_, "[ Case clause"); |
| 1828 | // Compile the test. |
| 1829 | next_test.Bind(); |
| 1830 | next_test.Unuse(); |
| 1831 | // Duplicate TOS. |
| 1832 | __ ldr(r0, frame_->Top()); |
| 1833 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1834 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1835 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1836 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1837 | // Before entering the body from the test, remove the switch value from |
| 1838 | // the stack. |
| 1839 | frame_->Drop(); |
| 1840 | |
| 1841 | // Label the body so that fall through is enabled. |
| 1842 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1843 | default_exit.Bind(); |
| 1844 | } else { |
| 1845 | fall_through.Bind(); |
| 1846 | fall_through.Unuse(); |
| 1847 | } |
| 1848 | VisitStatementsAndSpill(clause->statements()); |
| 1849 | |
| 1850 | // If control flow can fall through from the body, jump to the next body |
| 1851 | // or the end of the statement. |
| 1852 | if (frame_ != NULL) { |
| 1853 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1854 | default_entry.Jump(); |
| 1855 | } else { |
| 1856 | fall_through.Jump(); |
| 1857 | } |
| 1858 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1861 | // The final "test" removes the switch value. |
| 1862 | next_test.Bind(); |
| 1863 | frame_->Drop(); |
| 1864 | |
| 1865 | // If there is a default clause, compile it. |
| 1866 | if (default_clause != NULL) { |
| 1867 | Comment cmnt(masm_, "[ Default clause"); |
| 1868 | default_entry.Bind(); |
| 1869 | VisitStatementsAndSpill(default_clause->statements()); |
| 1870 | // If control flow can fall out of the default and there is a case after |
| 1871 | // it, jup to that case's body. |
| 1872 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1873 | default_exit.Jump(); |
| 1874 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1875 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1876 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1877 | if (fall_through.is_linked()) { |
| 1878 | fall_through.Bind(); |
| 1879 | } |
| 1880 | |
| 1881 | if (node->break_target()->is_linked()) { |
| 1882 | node->break_target()->Bind(); |
| 1883 | } |
| 1884 | node->break_target()->Unuse(); |
| 1885 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1889 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1890 | #ifdef DEBUG |
| 1891 | int original_height = frame_->height(); |
| 1892 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1893 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1894 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1895 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1896 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1897 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 1898 | IncrementLoopNesting(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1899 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1900 | // Label the top of the loop for the backward CFG edge. If the test |
| 1901 | // is always true we can use the continue target, and if the test is |
| 1902 | // always false there is no need. |
| 1903 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1904 | switch (info) { |
| 1905 | case ALWAYS_TRUE: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1906 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1907 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1908 | break; |
| 1909 | case ALWAYS_FALSE: |
| 1910 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1911 | break; |
| 1912 | case DONT_KNOW: |
| 1913 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1914 | body.Bind(); |
| 1915 | break; |
| 1916 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1917 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1918 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1919 | VisitAndSpill(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1920 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1921 | // Compile the test. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1922 | switch (info) { |
| 1923 | case ALWAYS_TRUE: |
| 1924 | // If control can fall off the end of the body, jump back to the |
| 1925 | // top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1926 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1927 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1928 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1929 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1930 | case ALWAYS_FALSE: |
| 1931 | // If we have a continue in the body, we only have to bind its |
| 1932 | // jump target. |
| 1933 | if (node->continue_target()->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1934 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1935 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1936 | break; |
| 1937 | case DONT_KNOW: |
| 1938 | // We have to compile the test expression if it can be reached by |
| 1939 | // control flow falling out of the body or via continue. |
| 1940 | if (node->continue_target()->is_linked()) { |
| 1941 | node->continue_target()->Bind(); |
| 1942 | } |
| 1943 | if (has_valid_frame()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1944 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 1945 | CodeForDoWhileConditionPosition(node); |
| 1946 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1947 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1948 | // A invalid frame here indicates that control did not |
| 1949 | // fall out of the test expression. |
| 1950 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1951 | } |
| 1952 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1953 | break; |
| 1954 | } |
| 1955 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1956 | if (node->break_target()->is_linked()) { |
| 1957 | node->break_target()->Bind(); |
| 1958 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 1959 | DecrementLoopNesting(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1960 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1961 | } |
| 1962 | |
| 1963 | |
| 1964 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 1965 | #ifdef DEBUG |
| 1966 | int original_height = frame_->height(); |
| 1967 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1968 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1969 | Comment cmnt(masm_, "[ WhileStatement"); |
| 1970 | CodeForStatementPosition(node); |
| 1971 | |
| 1972 | // If the test is never true and has no side effects there is no need |
| 1973 | // to compile the test or body. |
| 1974 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1975 | if (info == ALWAYS_FALSE) return; |
| 1976 | |
| 1977 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 1978 | IncrementLoopNesting(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1979 | |
| 1980 | // Label the top of the loop with the continue target for the backward |
| 1981 | // CFG edge. |
| 1982 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1983 | node->continue_target()->Bind(); |
| 1984 | |
| 1985 | if (info == DONT_KNOW) { |
| 1986 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1987 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1988 | if (has_valid_frame()) { |
| 1989 | // A NULL frame indicates that control did not fall out of the |
| 1990 | // test expression. |
| 1991 | Branch(false, node->break_target()); |
| 1992 | } |
| 1993 | if (has_valid_frame() || body.is_linked()) { |
| 1994 | body.Bind(); |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | if (has_valid_frame()) { |
| 1999 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 2000 | VisitAndSpill(node->body()); |
| 2001 | |
| 2002 | // If control flow can fall out of the body, jump back to the top. |
| 2003 | if (has_valid_frame()) { |
| 2004 | node->continue_target()->Jump(); |
| 2005 | } |
| 2006 | } |
| 2007 | if (node->break_target()->is_linked()) { |
| 2008 | node->break_target()->Bind(); |
| 2009 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 2010 | DecrementLoopNesting(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2011 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2012 | } |
| 2013 | |
| 2014 | |
| 2015 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 2016 | #ifdef DEBUG |
| 2017 | int original_height = frame_->height(); |
| 2018 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2019 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2020 | Comment cmnt(masm_, "[ ForStatement"); |
| 2021 | CodeForStatementPosition(node); |
| 2022 | if (node->init() != NULL) { |
| 2023 | VisitAndSpill(node->init()); |
| 2024 | } |
| 2025 | |
| 2026 | // If the test is never true there is no need to compile the test or |
| 2027 | // body. |
| 2028 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 2029 | if (info == ALWAYS_FALSE) return; |
| 2030 | |
| 2031 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 2032 | IncrementLoopNesting(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2033 | |
| 2034 | // If there is no update statement, label the top of the loop with the |
| 2035 | // continue target, otherwise with the loop target. |
| 2036 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 2037 | if (node->next() == NULL) { |
| 2038 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 2039 | node->continue_target()->Bind(); |
| 2040 | } else { |
| 2041 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 2042 | loop.Bind(); |
| 2043 | } |
| 2044 | |
| 2045 | // If the test is always true, there is no need to compile it. |
| 2046 | if (info == DONT_KNOW) { |
| 2047 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2048 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2049 | if (has_valid_frame()) { |
| 2050 | Branch(false, node->break_target()); |
| 2051 | } |
| 2052 | if (has_valid_frame() || body.is_linked()) { |
| 2053 | body.Bind(); |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | if (has_valid_frame()) { |
| 2058 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 2059 | VisitAndSpill(node->body()); |
| 2060 | |
| 2061 | if (node->next() == NULL) { |
| 2062 | // If there is no update statement and control flow can fall out |
| 2063 | // of the loop, jump directly to the continue label. |
| 2064 | if (has_valid_frame()) { |
| 2065 | node->continue_target()->Jump(); |
| 2066 | } |
| 2067 | } else { |
| 2068 | // If there is an update statement and control flow can reach it |
| 2069 | // via falling out of the body of the loop or continuing, we |
| 2070 | // compile the update statement. |
| 2071 | if (node->continue_target()->is_linked()) { |
| 2072 | node->continue_target()->Bind(); |
| 2073 | } |
| 2074 | if (has_valid_frame()) { |
| 2075 | // Record source position of the statement as this code which is |
| 2076 | // after the code for the body actually belongs to the loop |
| 2077 | // statement and not the body. |
| 2078 | CodeForStatementPosition(node); |
| 2079 | VisitAndSpill(node->next()); |
| 2080 | loop.Jump(); |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | if (node->break_target()->is_linked()) { |
| 2085 | node->break_target()->Bind(); |
| 2086 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 2087 | DecrementLoopNesting(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2088 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2092 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2093 | #ifdef DEBUG |
| 2094 | int original_height = frame_->height(); |
| 2095 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2096 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2097 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2098 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2099 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2100 | JumpTarget primitive; |
| 2101 | JumpTarget jsobject; |
| 2102 | JumpTarget fixed_array; |
| 2103 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 2104 | JumpTarget end_del_check; |
| 2105 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2106 | |
| 2107 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2108 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2109 | |
| 2110 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 2111 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2112 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2113 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2114 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2115 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2116 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2117 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2118 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2119 | |
| 2120 | // Stack layout in body: |
| 2121 | // [iteration counter (Smi)] |
| 2122 | // [length of array] |
| 2123 | // [FixedArray] |
| 2124 | // [Map or 0] |
| 2125 | // [Object] |
| 2126 | |
| 2127 | // Check if enumerable is already a JSObject |
| 2128 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2129 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2130 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2131 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2132 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2133 | primitive.Bind(); |
| 2134 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2135 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2136 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2137 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2138 | // Get the set of properties (as a FixedArray or Map). |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2139 | // r0: value to be iterated over |
| 2140 | frame_->EmitPush(r0); // Push the object being iterated over. |
| 2141 | |
| 2142 | // Check cache validity in generated code. This is a fast case for |
| 2143 | // the JSObject::IsSimpleEnum cache validity checks. If we cannot |
| 2144 | // guarantee cache validity, call the runtime system to check cache |
| 2145 | // validity or get the property names in a fixed array. |
| 2146 | JumpTarget call_runtime; |
| 2147 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 2148 | JumpTarget check_prototype; |
| 2149 | JumpTarget use_cache; |
| 2150 | __ mov(r1, Operand(r0)); |
| 2151 | loop.Bind(); |
| 2152 | // Check that there are no elements. |
| 2153 | __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 2154 | __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex); |
| 2155 | __ cmp(r2, r4); |
| 2156 | call_runtime.Branch(ne); |
| 2157 | // Check that instance descriptors are not empty so that we can |
| 2158 | // check for an enum cache. Leave the map in r3 for the subsequent |
| 2159 | // prototype load. |
| 2160 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 2161 | __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset)); |
| 2162 | __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex); |
| 2163 | __ cmp(r2, ip); |
| 2164 | call_runtime.Branch(eq); |
| 2165 | // Check that there in an enum cache in the non-empty instance |
| 2166 | // descriptors. This is the case if the next enumeration index |
| 2167 | // field does not contain a smi. |
| 2168 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset)); |
| 2169 | __ tst(r2, Operand(kSmiTagMask)); |
| 2170 | call_runtime.Branch(eq); |
| 2171 | // For all objects but the receiver, check that the cache is empty. |
| 2172 | // r4: empty fixed array root. |
| 2173 | __ cmp(r1, r0); |
| 2174 | check_prototype.Branch(eq); |
| 2175 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 2176 | __ cmp(r2, r4); |
| 2177 | call_runtime.Branch(ne); |
| 2178 | check_prototype.Bind(); |
| 2179 | // Load the prototype from the map and loop if non-null. |
| 2180 | __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 2181 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2182 | __ cmp(r1, ip); |
| 2183 | loop.Branch(ne); |
| 2184 | // The enum cache is valid. Load the map of the object being |
| 2185 | // iterated over and use the cache for the iteration. |
| 2186 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 2187 | use_cache.Jump(); |
| 2188 | |
| 2189 | call_runtime.Bind(); |
| 2190 | // Call the runtime to get the property names for the object. |
| 2191 | 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] | 2192 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2193 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2194 | // If we got a map from the runtime call, we can do a fast |
| 2195 | // modification check. Otherwise, we got a fixed array, and we have |
| 2196 | // to do a slow check. |
| 2197 | // r0: map or fixed array (result from call to |
| 2198 | // Runtime::kGetPropertyNamesFast) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2199 | __ mov(r2, Operand(r0)); |
| 2200 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2201 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 2202 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2203 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2204 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2205 | use_cache.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2206 | // Get enum cache |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2207 | // r0: map (either the result from a call to |
| 2208 | // Runtime::kGetPropertyNamesFast or has been fetched directly from |
| 2209 | // the object) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2210 | __ mov(r1, Operand(r0)); |
| 2211 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 2212 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 2213 | __ ldr(r2, |
| 2214 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 2215 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2216 | frame_->EmitPush(r0); // map |
| 2217 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2218 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2219 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2220 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2221 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2222 | frame_->EmitPush(r0); |
| 2223 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2224 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2225 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2226 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2227 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 2228 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2229 | |
| 2230 | // 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] | 2231 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2232 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2233 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2234 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2235 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2236 | |
| 2237 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2238 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2239 | // sp[0] : index |
| 2240 | // sp[1] : array/enum cache length |
| 2241 | // sp[2] : array or enum cache |
| 2242 | // sp[3] : 0 or map |
| 2243 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2244 | // Grab the current frame's height for the break and continue |
| 2245 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2246 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 2247 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2248 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2249 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 2250 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 2251 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 2252 | node->break_target()->Branch(hs); |
| 2253 | |
| 2254 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2255 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2256 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2257 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2258 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 2259 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 2260 | |
| 2261 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2262 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2263 | // Check if this (still) matches the map of the enumerable. |
| 2264 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2265 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2266 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 2267 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2268 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2269 | |
| 2270 | // 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] | 2271 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 2272 | frame_->EmitPush(r0); |
| 2273 | frame_->EmitPush(r3); // push entry |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2274 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2275 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2276 | |
| 2277 | // 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] | 2278 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2279 | __ cmp(r3, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2280 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2281 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2282 | end_del_check.Bind(); |
| 2283 | // Store the entry in the 'each' expression and take another spin in the |
| 2284 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 2285 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2286 | { Reference each(this, node->each()); |
| 2287 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2288 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2289 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 2290 | frame_->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2291 | each.SetValue(NOT_CONST_INIT); |
| 2292 | frame_->Drop(2); |
| 2293 | } else { |
| 2294 | // If the reference was to a slot we rely on the convenient property |
| 2295 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 2296 | // right on top of or right underneath a zero-sized reference. |
| 2297 | each.SetValue(NOT_CONST_INIT); |
| 2298 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2299 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2302 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2303 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2304 | VisitAndSpill(node->body()); |
| 2305 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2306 | // Next. Reestablish a spilled frame in case we are coming here via |
| 2307 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2308 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2309 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2310 | frame_->EmitPop(r0); |
| 2311 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 2312 | frame_->EmitPush(r0); |
| 2313 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2314 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2315 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 2316 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2317 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2318 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2319 | |
| 2320 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2321 | exit.Bind(); |
| 2322 | node->continue_target()->Unuse(); |
| 2323 | node->break_target()->Unuse(); |
| 2324 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2328 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2329 | #ifdef DEBUG |
| 2330 | int original_height = frame_->height(); |
| 2331 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2332 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2333 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2334 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2335 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2336 | JumpTarget try_block; |
| 2337 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2338 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2339 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2340 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2341 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2342 | |
| 2343 | // Store the caught exception in the catch variable. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2344 | Variable* catch_var = node->catch_var()->var(); |
| 2345 | ASSERT(catch_var != NULL && catch_var->slot() != NULL); |
| 2346 | StoreToSlot(catch_var->slot(), NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2347 | |
| 2348 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2349 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2350 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2351 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2352 | if (frame_ != NULL) { |
| 2353 | exit.Jump(); |
| 2354 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2355 | |
| 2356 | |
| 2357 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2358 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2359 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2360 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2361 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2362 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2363 | // Shadow the labels for all escapes from the try block, including |
| 2364 | // returns. During shadowing, the original label is hidden as the |
| 2365 | // LabelShadow and operations on the original actually affect the |
| 2366 | // shadowing label. |
| 2367 | // |
| 2368 | // We should probably try to unify the escaping labels and the return |
| 2369 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2370 | int nof_escapes = node->escaping_targets()->length(); |
| 2371 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2372 | |
| 2373 | // Add the shadow target for the function return. |
| 2374 | static const int kReturnShadowIndex = 0; |
| 2375 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2376 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2377 | function_return_is_shadowed_ = true; |
| 2378 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2379 | |
| 2380 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2381 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2382 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2386 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2387 | |
| 2388 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2389 | // After shadowing stops, the original labels are unshadowed and the |
| 2390 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2391 | bool has_unlinks = false; |
| 2392 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2393 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2394 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2395 | } |
| 2396 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2397 | |
| 2398 | // Get an external reference to the handler address. |
| 2399 | ExternalReference handler_address(Top::k_handler_address); |
| 2400 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2401 | // If we can fall off the end of the try block, unlink from try chain. |
| 2402 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2403 | // The next handler address is on top of the frame. Unlink from |
| 2404 | // the handler list and drop the rest of this handler from the |
| 2405 | // frame. |
| 2406 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2407 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2408 | __ mov(r3, Operand(handler_address)); |
| 2409 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2410 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2411 | if (has_unlinks) { |
| 2412 | exit.Jump(); |
| 2413 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2416 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2417 | // jumped to. Deallocate each shadow target. |
| 2418 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2419 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2420 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2421 | shadows[i]->Bind(); |
| 2422 | // Because we can be jumping here (to spilled code) from unspilled |
| 2423 | // code, we need to reestablish a spilled frame at this block. |
| 2424 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2425 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2426 | // Reload sp from the top handler, because some statements that we |
| 2427 | // 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] | 2428 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2429 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2430 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2431 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2432 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2433 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2434 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2435 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2436 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2437 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2438 | frame_->PrepareForReturn(); |
| 2439 | } |
| 2440 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2444 | exit.Bind(); |
| 2445 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2449 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2450 | #ifdef DEBUG |
| 2451 | int original_height = frame_->height(); |
| 2452 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2453 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2454 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2455 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2456 | |
| 2457 | // State: Used to keep track of reason for entering the finally |
| 2458 | // block. Should probably be extended to hold information for |
| 2459 | // break/continue from within the try block. |
| 2460 | enum { FALLING, THROWING, JUMPING }; |
| 2461 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2462 | JumpTarget try_block; |
| 2463 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2464 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2465 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2466 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2467 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2468 | // In case of thrown exceptions, this is where we continue. |
| 2469 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2470 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2471 | |
| 2472 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2473 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2474 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2475 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2476 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2477 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2478 | // Shadow the labels for all escapes from the try block, including |
| 2479 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2480 | // operations on the original actually affect the shadowing label. |
| 2481 | // |
| 2482 | // We should probably try to unify the escaping labels and the return |
| 2483 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2484 | int nof_escapes = node->escaping_targets()->length(); |
| 2485 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2486 | |
| 2487 | // Add the shadow target for the function return. |
| 2488 | static const int kReturnShadowIndex = 0; |
| 2489 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2490 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2491 | function_return_is_shadowed_ = true; |
| 2492 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2493 | |
| 2494 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2495 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2496 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2500 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2501 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2502 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2503 | // After shadowing stops, the original labels are unshadowed and the |
| 2504 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2505 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2506 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2507 | shadows[i]->StopShadowing(); |
| 2508 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2509 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2510 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2511 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2512 | // Get an external reference to the handler address. |
| 2513 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2514 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2515 | // If we can fall off the end of the try block, unlink from the try |
| 2516 | // chain and set the state on the frame to FALLING. |
| 2517 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2518 | // The next handler address is on top of the frame. |
| 2519 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2520 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2521 | __ mov(r3, Operand(handler_address)); |
| 2522 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2523 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2524 | |
| 2525 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2526 | // state in r2, then jump around the unlink blocks if any. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2527 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2528 | frame_->EmitPush(r0); |
| 2529 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2530 | if (nof_unlinks > 0) { |
| 2531 | finally_block.Jump(); |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | // Generate code to unlink and set the state for the (formerly) |
| 2536 | // shadowing targets that have been jumped to. |
| 2537 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2538 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2539 | // If we have come from the shadowed return, the return value is |
| 2540 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2541 | // until it is pushed. |
| 2542 | // |
| 2543 | // Because we can be jumping here (to spilled code) from |
| 2544 | // unspilled code, we need to reestablish a spilled frame at |
| 2545 | // this block. |
| 2546 | shadows[i]->Bind(); |
| 2547 | frame_->SpillAll(); |
| 2548 | |
| 2549 | // Reload sp from the top handler, because some statements that |
| 2550 | // we break from (eg, for...in) may have left stuff on the |
| 2551 | // stack. |
| 2552 | __ mov(r3, Operand(handler_address)); |
| 2553 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2554 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2555 | |
| 2556 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2557 | // handler address is currently on top of the frame. |
| 2558 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2559 | frame_->EmitPop(r1); |
| 2560 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2561 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2562 | |
| 2563 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2564 | // If this label shadowed the function return, materialize the |
| 2565 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2566 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2567 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2568 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2569 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2570 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2571 | } |
| 2572 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2573 | if (--nof_unlinks > 0) { |
| 2574 | // If this is not the last unlink block, jump around the next. |
| 2575 | finally_block.Jump(); |
| 2576 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2577 | } |
| 2578 | } |
| 2579 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2580 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2581 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2582 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2583 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2584 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2585 | |
| 2586 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2587 | // and the state - while evaluating the finally block. |
| 2588 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2589 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2590 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2591 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2592 | if (has_valid_frame()) { |
| 2593 | // Restore state and return value or faked TOS. |
| 2594 | frame_->EmitPop(r2); |
| 2595 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2598 | // Generate code to jump to the right destination for all used |
| 2599 | // formerly shadowing targets. Deallocate each shadow target. |
| 2600 | for (int i = 0; i < shadows.length(); i++) { |
| 2601 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2602 | JumpTarget* original = shadows[i]->other_target(); |
| 2603 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2604 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2605 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2606 | skip.Branch(ne); |
| 2607 | frame_->PrepareForReturn(); |
| 2608 | original->Jump(); |
| 2609 | skip.Bind(); |
| 2610 | } else { |
| 2611 | original->Branch(eq); |
| 2612 | } |
| 2613 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2614 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2615 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2616 | if (has_valid_frame()) { |
| 2617 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2618 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2619 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2620 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2621 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2622 | // Rethrow exception. |
| 2623 | frame_->EmitPush(r0); |
| 2624 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2625 | |
| 2626 | // Done. |
| 2627 | exit.Bind(); |
| 2628 | } |
| 2629 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2633 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2634 | #ifdef DEBUG |
| 2635 | int original_height = frame_->height(); |
| 2636 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2637 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2638 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2639 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2640 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2641 | frame_->DebugBreak(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2642 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2643 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2644 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2648 | void CodeGenerator::InstantiateFunction( |
| 2649 | Handle<SharedFunctionInfo> function_info) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2650 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2651 | __ mov(r0, Operand(function_info)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2652 | // Use the fast case closure allocation code that allocates in new |
| 2653 | // space for nested functions that don't need literals cloning. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2654 | if (scope()->is_function_scope() && function_info->num_literals() == 0) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2655 | FastNewClosureStub stub; |
| 2656 | frame_->EmitPush(r0); |
| 2657 | frame_->CallStub(&stub, 1); |
| 2658 | frame_->EmitPush(r0); |
| 2659 | } else { |
| 2660 | // Create a new closure. |
| 2661 | frame_->EmitPush(cp); |
| 2662 | frame_->EmitPush(r0); |
| 2663 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2664 | frame_->EmitPush(r0); |
| 2665 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2669 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2670 | #ifdef DEBUG |
| 2671 | int original_height = frame_->height(); |
| 2672 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2673 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2674 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2675 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2676 | // Build the function info and instantiate it. |
| 2677 | Handle<SharedFunctionInfo> function_info = |
| 2678 | Compiler::BuildFunctionInfo(node, script(), this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2679 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2680 | if (HasStackOverflow()) { |
| 2681 | ASSERT(frame_->height() == original_height); |
| 2682 | return; |
| 2683 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2684 | InstantiateFunction(function_info); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2685 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2689 | void CodeGenerator::VisitSharedFunctionInfoLiteral( |
| 2690 | SharedFunctionInfoLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2691 | #ifdef DEBUG |
| 2692 | int original_height = frame_->height(); |
| 2693 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2694 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2695 | Comment cmnt(masm_, "[ SharedFunctionInfoLiteral"); |
| 2696 | InstantiateFunction(node->shared_function_info()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2697 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2701 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2702 | #ifdef DEBUG |
| 2703 | int original_height = frame_->height(); |
| 2704 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2705 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2706 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2707 | JumpTarget then; |
| 2708 | JumpTarget else_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2709 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2710 | if (has_valid_frame()) { |
| 2711 | Branch(false, &else_); |
| 2712 | } |
| 2713 | if (has_valid_frame() || then.is_linked()) { |
| 2714 | then.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2715 | LoadAndSpill(node->then_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2716 | } |
| 2717 | if (else_.is_linked()) { |
| 2718 | JumpTarget exit; |
| 2719 | if (has_valid_frame()) exit.Jump(); |
| 2720 | else_.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2721 | LoadAndSpill(node->else_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2722 | if (exit.is_linked()) exit.Bind(); |
| 2723 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2724 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2728 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
| 2729 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2730 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2731 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2732 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2733 | JumpTarget slow; |
| 2734 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2735 | |
| 2736 | // Generate fast-case code for variables that might be shadowed by |
| 2737 | // eval-introduced variables. Eval is used a lot without |
| 2738 | // introducing variables. In those cases, we do not want to |
| 2739 | // perform a runtime call for all variables in the scope |
| 2740 | // containing the eval. |
| 2741 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2742 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2743 | // If there was no control flow to slow, we can exit early. |
| 2744 | if (!slow.is_linked()) { |
| 2745 | frame_->EmitPush(r0); |
| 2746 | return; |
| 2747 | } |
| 2748 | |
| 2749 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2750 | |
| 2751 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2752 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2753 | // Only generate the fast case for locals that rewrite to slots. |
| 2754 | // This rules out argument loads. |
| 2755 | if (potential_slot != NULL) { |
| 2756 | __ ldr(r0, |
| 2757 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2758 | r1, |
| 2759 | r2, |
| 2760 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2761 | if (potential_slot->var()->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2762 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2763 | __ cmp(r0, ip); |
| 2764 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2765 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2766 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2767 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2768 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2769 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2770 | } |
| 2771 | } |
| 2772 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2773 | slow.Bind(); |
| 2774 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2775 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2776 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2777 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2778 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2779 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2780 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2781 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2782 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2783 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2784 | done.Bind(); |
| 2785 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2786 | |
| 2787 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2788 | Register scratch = VirtualFrame::scratch0(); |
| 2789 | frame_->EmitPush(SlotOperand(slot, scratch)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2790 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2791 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2792 | // initialized yet) which needs to be converted into the 'undefined' |
| 2793 | // value. |
| 2794 | Comment cmnt(masm_, "[ Unhole const"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2795 | frame_->EmitPop(scratch); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2796 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2797 | __ cmp(scratch, ip); |
| 2798 | __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex, eq); |
| 2799 | frame_->EmitPush(scratch); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2805 | void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) { |
| 2806 | ASSERT(slot != NULL); |
| 2807 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2808 | VirtualFrame::SpilledScope spilled_scope(frame_); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2809 | ASSERT(slot->var()->is_dynamic()); |
| 2810 | |
| 2811 | // For now, just do a runtime call. |
| 2812 | frame_->EmitPush(cp); |
| 2813 | __ mov(r0, Operand(slot->var()->name())); |
| 2814 | frame_->EmitPush(r0); |
| 2815 | |
| 2816 | if (init_state == CONST_INIT) { |
| 2817 | // Same as the case for a normal store, but ignores attribute |
| 2818 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 2819 | // const properties (introduced via eval("const foo = (some |
| 2820 | // expr);")). Also, uses the current function context instead of |
| 2821 | // the top context. |
| 2822 | // |
| 2823 | // Note that we must declare the foo upon entry of eval(), via a |
| 2824 | // context slot declaration, but we cannot initialize it at the |
| 2825 | // same time, because the const declaration may be at the end of |
| 2826 | // the eval code (sigh...) and the const variable may have been |
| 2827 | // used before (where its value is 'undefined'). Thus, we can only |
| 2828 | // do the initialization when we actually encounter the expression |
| 2829 | // and when the expression operands are defined and valid, and |
| 2830 | // thus we need the split into 2 operations: declaration of the |
| 2831 | // context slot followed by initialization. |
| 2832 | frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 2833 | } else { |
| 2834 | frame_->CallRuntime(Runtime::kStoreContextSlot, 3); |
| 2835 | } |
| 2836 | // Storing a variable must keep the (new) value on the expression |
| 2837 | // stack. This is necessary for compiling assignment expressions. |
| 2838 | frame_->EmitPush(r0); |
| 2839 | |
| 2840 | } else { |
| 2841 | ASSERT(!slot->var()->is_dynamic()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2842 | Register scratch = VirtualFrame::scratch0(); |
| 2843 | VirtualFrame::RegisterAllocationScope scope(this); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2844 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2845 | // The frame must be spilled when branching to this target. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2846 | JumpTarget exit; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2847 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2848 | if (init_state == CONST_INIT) { |
| 2849 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 2850 | // Only the first const initialization must be executed (the slot |
| 2851 | // still contains 'the hole' value). When the assignment is |
| 2852 | // executed, the code is identical to a normal store (see below). |
| 2853 | Comment cmnt(masm_, "[ Init const"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2854 | __ ldr(scratch, SlotOperand(slot, scratch)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2855 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2856 | __ cmp(scratch, ip); |
| 2857 | frame_->SpillAll(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2858 | exit.Branch(ne); |
| 2859 | } |
| 2860 | |
| 2861 | // We must execute the store. Storing a variable must keep the |
| 2862 | // (new) value on the stack. This is necessary for compiling |
| 2863 | // assignment expressions. |
| 2864 | // |
| 2865 | // Note: We will reach here even with slot->var()->mode() == |
| 2866 | // Variable::CONST because of const declarations which will |
| 2867 | // initialize consts to 'the hole' value and by doing so, end up |
| 2868 | // calling this code. r2 may be loaded with context; used below in |
| 2869 | // RecordWrite. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2870 | Register tos = frame_->Peek(); |
| 2871 | __ str(tos, SlotOperand(slot, scratch)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2872 | if (slot->type() == Slot::CONTEXT) { |
| 2873 | // Skip write barrier if the written value is a smi. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2874 | __ tst(tos, Operand(kSmiTagMask)); |
| 2875 | // We don't use tos any more after here. |
| 2876 | VirtualFrame::SpilledScope spilled_scope(frame_); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2877 | exit.Branch(eq); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2878 | // scratch is loaded with context when calling SlotOperand above. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2879 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 2880 | __ mov(r3, Operand(offset)); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2881 | // r1 could be identical with tos, but that doesn't matter. |
| 2882 | __ RecordWrite(scratch, r3, r1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2883 | } |
| 2884 | // If we definitely did not jump over the assignment, we do not need |
| 2885 | // to bind the exit label. Doing so can defeat peephole |
| 2886 | // optimization. |
| 2887 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2888 | frame_->SpillAll(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2889 | exit.Bind(); |
| 2890 | } |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2895 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2896 | TypeofState typeof_state, |
| 2897 | Register tmp, |
| 2898 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2899 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2900 | // Check that no extension objects have been created by calls to |
| 2901 | // eval from the current scope to the global scope. |
| 2902 | Register context = cp; |
| 2903 | Scope* s = scope(); |
| 2904 | while (s != NULL) { |
| 2905 | if (s->num_heap_slots() > 0) { |
| 2906 | if (s->calls_eval()) { |
| 2907 | // Check that extension is NULL. |
| 2908 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2909 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2910 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2911 | } |
| 2912 | // Load next context in chain. |
| 2913 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2914 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2915 | context = tmp; |
| 2916 | } |
| 2917 | // If no outer scope calls eval, we do not need to check more |
| 2918 | // context extensions. |
| 2919 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2920 | s = s->outer_scope(); |
| 2921 | } |
| 2922 | |
| 2923 | if (s->is_eval_scope()) { |
| 2924 | Label next, fast; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2925 | __ Move(tmp, context); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2926 | __ bind(&next); |
| 2927 | // Terminate at global context. |
| 2928 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2929 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2930 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2931 | __ b(eq, &fast); |
| 2932 | // Check that extension is NULL. |
| 2933 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2934 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2935 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2936 | // Load next context in chain. |
| 2937 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2938 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2939 | __ b(&next); |
| 2940 | __ bind(&fast); |
| 2941 | } |
| 2942 | |
| 2943 | // All extension objects were empty and it is safe to use a global |
| 2944 | // load IC call. |
| 2945 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2946 | // Load the global object. |
| 2947 | LoadGlobal(); |
| 2948 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2949 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2950 | // Call IC stub. |
| 2951 | if (typeof_state == INSIDE_TYPEOF) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2952 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2953 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2954 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2957 | // Drop the global object. The result is in r0. |
| 2958 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2962 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2963 | #ifdef DEBUG |
| 2964 | int original_height = frame_->height(); |
| 2965 | #endif |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2966 | Comment cmnt(masm_, "[ Slot"); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2967 | LoadFromSlot(node, NOT_INSIDE_TYPEOF); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2968 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2969 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2970 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2971 | |
| 2972 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2973 | #ifdef DEBUG |
| 2974 | int original_height = frame_->height(); |
| 2975 | #endif |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2976 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2977 | |
| 2978 | Variable* var = node->var(); |
| 2979 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2980 | if (expr != NULL) { |
| 2981 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2982 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2983 | ASSERT(var->is_global()); |
| 2984 | Reference ref(this, node); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2985 | ref.GetValue(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2986 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2987 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2991 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2992 | #ifdef DEBUG |
| 2993 | int original_height = frame_->height(); |
| 2994 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2995 | Comment cmnt(masm_, "[ Literal"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2996 | Register reg = frame_->GetTOSRegister(); |
| 2997 | __ mov(reg, Operand(node->handle())); |
| 2998 | frame_->EmitPush(reg); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2999 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3003 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3004 | #ifdef DEBUG |
| 3005 | int original_height = frame_->height(); |
| 3006 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3007 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3008 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 3009 | |
| 3010 | // Retrieve the literal array and check the allocated entry. |
| 3011 | |
| 3012 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3013 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3014 | |
| 3015 | // Load the literals array of the function. |
| 3016 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 3017 | |
| 3018 | // Load the literal at the ast saved index. |
| 3019 | int literal_offset = |
| 3020 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 3021 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 3022 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3023 | JumpTarget done; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3024 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 3025 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3026 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3027 | |
| 3028 | // If the entry is undefined we call the runtime system to computed |
| 3029 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3030 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3031 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3032 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3033 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3034 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3035 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3036 | frame_->EmitPush(r0); |
| 3037 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3038 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3039 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3040 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3041 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3042 | frame_->EmitPush(r2); |
| 3043 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3047 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3048 | #ifdef DEBUG |
| 3049 | int original_height = frame_->height(); |
| 3050 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3051 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3052 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 3053 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3054 | // Load the function of this activation. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3055 | __ ldr(r3, frame_->Function()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3056 | // Literal array. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3057 | __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3058 | // Literal index. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3059 | __ mov(r2, Operand(Smi::FromInt(node->literal_index()))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3060 | // Constant properties. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3061 | __ mov(r1, Operand(node->constant_properties())); |
| 3062 | // Should the object literal have fast elements? |
| 3063 | __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0))); |
| 3064 | frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3065 | if (node->depth() > 1) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3066 | frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3067 | } else { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3068 | frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3069 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3070 | frame_->EmitPush(r0); // save the result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3071 | for (int i = 0; i < node->properties()->length(); i++) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3072 | // At the start of each iteration, the top of stack contains |
| 3073 | // the newly created object literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3074 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 3075 | Literal* key = property->key(); |
| 3076 | Expression* value = property->value(); |
| 3077 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3078 | case ObjectLiteral::Property::CONSTANT: |
| 3079 | break; |
| 3080 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 3081 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 3082 | // else fall through |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3083 | case ObjectLiteral::Property::COMPUTED: |
| 3084 | if (key->handle()->IsSymbol()) { |
| 3085 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 3086 | LoadAndSpill(value); |
| 3087 | frame_->EmitPop(r0); |
| 3088 | __ mov(r2, Operand(key->handle())); |
| 3089 | __ ldr(r1, frame_->Top()); // Load the receiver. |
| 3090 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
| 3091 | break; |
| 3092 | } |
| 3093 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3094 | case ObjectLiteral::Property::PROTOTYPE: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3095 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3096 | frame_->EmitPush(r0); // dup the result |
| 3097 | LoadAndSpill(key); |
| 3098 | LoadAndSpill(value); |
| 3099 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3100 | break; |
| 3101 | } |
| 3102 | case ObjectLiteral::Property::SETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3103 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3104 | frame_->EmitPush(r0); |
| 3105 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3106 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3107 | frame_->EmitPush(r0); |
| 3108 | LoadAndSpill(value); |
| 3109 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3110 | break; |
| 3111 | } |
| 3112 | case ObjectLiteral::Property::GETTER: { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3113 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3114 | frame_->EmitPush(r0); |
| 3115 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3116 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3117 | frame_->EmitPush(r0); |
| 3118 | LoadAndSpill(value); |
| 3119 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3120 | break; |
| 3121 | } |
| 3122 | } |
| 3123 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3124 | ASSERT(frame_->height() == original_height + 1); |
| 3125 | } |
| 3126 | |
| 3127 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3128 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3129 | #ifdef DEBUG |
| 3130 | int original_height = frame_->height(); |
| 3131 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3132 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3133 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3134 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3135 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3136 | __ ldr(r2, frame_->Function()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3137 | // Load the literals array of the function. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3138 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3139 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3140 | __ mov(r0, Operand(node->constant_elements())); |
| 3141 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3142 | int length = node->values()->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3143 | if (node->depth() > 1) { |
| 3144 | frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3145 | } else if (length > FastCloneShallowArrayStub::kMaximumLength) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3146 | frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3147 | } else { |
| 3148 | FastCloneShallowArrayStub stub(length); |
| 3149 | frame_->CallStub(&stub, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3150 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3151 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3152 | // r0: created object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3153 | |
| 3154 | // Generate code to set the elements in the array that are not |
| 3155 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3156 | for (int i = 0; i < node->values()->length(); i++) { |
| 3157 | Expression* value = node->values()->at(i); |
| 3158 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3159 | // If value is a literal the property value is already set in the |
| 3160 | // boilerplate object. |
| 3161 | if (value->AsLiteral() != NULL) continue; |
| 3162 | // If value is a materialized literal the property value is already set |
| 3163 | // in the boilerplate object if it is simple. |
| 3164 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3165 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3166 | // The property must be set by generated code. |
| 3167 | LoadAndSpill(value); |
| 3168 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3169 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3170 | // Fetch the object literal. |
| 3171 | __ ldr(r1, frame_->Top()); |
| 3172 | // Get the elements array. |
| 3173 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3174 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3175 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3176 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3177 | __ str(r0, FieldMemOperand(r1, offset)); |
| 3178 | |
| 3179 | // Update the write barrier for the array address. |
| 3180 | __ mov(r3, Operand(offset)); |
| 3181 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3182 | } |
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 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3187 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3188 | #ifdef DEBUG |
| 3189 | int original_height = frame_->height(); |
| 3190 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3191 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3192 | // Call runtime routine to allocate the catch extension object and |
| 3193 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3194 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 3195 | LoadAndSpill(node->key()); |
| 3196 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3197 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 3198 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3199 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3203 | void CodeGenerator::VisitAssignment(Assignment* node) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3204 | VirtualFrame::RegisterAllocationScope scope(this); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3205 | #ifdef DEBUG |
| 3206 | int original_height = frame_->height(); |
| 3207 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3208 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3209 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3210 | { Reference target(this, node->target(), node->is_compound()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3211 | if (target.is_illegal()) { |
| 3212 | // Fool the virtual frame into thinking that we left the assignment's |
| 3213 | // value on the frame. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3214 | Register tos = frame_->GetTOSRegister(); |
| 3215 | __ mov(tos, Operand(Smi::FromInt(0))); |
| 3216 | frame_->EmitPush(tos); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3217 | ASSERT(frame_->height() == original_height + 1); |
| 3218 | return; |
| 3219 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3220 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3221 | if (node->op() == Token::ASSIGN || |
| 3222 | node->op() == Token::INIT_VAR || |
| 3223 | node->op() == Token::INIT_CONST) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3224 | Load(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3225 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3226 | } else { // Assignment is a compound assignment. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3227 | // Get the old value of the lhs. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3228 | target.GetValue(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3229 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3230 | bool overwrite = |
| 3231 | (node->value()->AsBinaryOperation() != NULL && |
| 3232 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3233 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3234 | VirtualFrameSmiOperation(node->binary_op(), |
| 3235 | literal->handle(), |
| 3236 | false, |
| 3237 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3238 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3239 | Load(node->value()); |
| 3240 | VirtualFrameBinaryOperation(node->binary_op(), |
| 3241 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3242 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3243 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3244 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 3245 | if (var != NULL && |
| 3246 | (var->mode() == Variable::CONST) && |
| 3247 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 3248 | // Assignment ignored - leave the value on the stack. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3249 | UnloadReference(&target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3250 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3251 | CodeForSourcePosition(node->position()); |
| 3252 | if (node->op() == Token::INIT_CONST) { |
| 3253 | // Dynamic constant initializations must use the function context |
| 3254 | // and initialize the actual constant declared. Dynamic variable |
| 3255 | // initializations are simply assignments and use SetValue. |
| 3256 | target.SetValue(CONST_INIT); |
| 3257 | } else { |
| 3258 | target.SetValue(NOT_CONST_INIT); |
| 3259 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3260 | } |
| 3261 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3262 | ASSERT(frame_->height() == original_height + 1); |
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::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3267 | #ifdef DEBUG |
| 3268 | int original_height = frame_->height(); |
| 3269 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3270 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3271 | Comment cmnt(masm_, "[ Throw"); |
| 3272 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3273 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3274 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3275 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 3276 | frame_->EmitPush(r0); |
| 3277 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3281 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3282 | #ifdef DEBUG |
| 3283 | int original_height = frame_->height(); |
| 3284 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3285 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3286 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3287 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3288 | { Reference property(this, node); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3289 | property.GetValue(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3290 | } |
| 3291 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3295 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3296 | #ifdef DEBUG |
| 3297 | int original_height = frame_->height(); |
| 3298 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3299 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3300 | Comment cmnt(masm_, "[ Call"); |
| 3301 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3302 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3303 | ZoneList<Expression*>* args = node->arguments(); |
| 3304 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3305 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3306 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3307 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 3308 | Property* property = function->AsProperty(); |
| 3309 | |
| 3310 | // ------------------------------------------------------------------------ |
| 3311 | // Fast-case: Use inline caching. |
| 3312 | // --- |
| 3313 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 3314 | // must be resolved after the arguments have been evaluated. The IC code |
| 3315 | // automatically handles this by loading the arguments before the function |
| 3316 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 3317 | // ------------------------------------------------------------------------ |
| 3318 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3319 | if (var != NULL && var->is_possibly_eval()) { |
| 3320 | // ---------------------------------- |
| 3321 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 3322 | // ---------------------------------- |
| 3323 | |
| 3324 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 3325 | // resolve the function we need to call and the receiver of the |
| 3326 | // call. Then we call the resolved function using the given |
| 3327 | // arguments. |
| 3328 | // Prepare stack for call to resolved function. |
| 3329 | LoadAndSpill(function); |
| 3330 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 3331 | frame_->EmitPush(r2); // Slot for receiver |
| 3332 | int arg_count = args->length(); |
| 3333 | for (int i = 0; i < arg_count; i++) { |
| 3334 | LoadAndSpill(args->at(i)); |
| 3335 | } |
| 3336 | |
| 3337 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 3338 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3339 | frame_->EmitPush(r1); |
| 3340 | if (arg_count > 0) { |
| 3341 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3342 | frame_->EmitPush(r1); |
| 3343 | } else { |
| 3344 | frame_->EmitPush(r2); |
| 3345 | } |
| 3346 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3347 | // Push the receiver. |
| 3348 | __ ldr(r1, frame_->Receiver()); |
| 3349 | frame_->EmitPush(r1); |
| 3350 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3351 | // Resolve the call. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3352 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3353 | |
| 3354 | // 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] | 3355 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3356 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3357 | |
| 3358 | // Call the function. |
| 3359 | CodeForSourcePosition(node->position()); |
| 3360 | |
| 3361 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3362 | CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3363 | frame_->CallStub(&call_function, arg_count + 1); |
| 3364 | |
| 3365 | __ ldr(cp, frame_->Context()); |
| 3366 | // Remove the function from the stack. |
| 3367 | frame_->Drop(); |
| 3368 | frame_->EmitPush(r0); |
| 3369 | |
| 3370 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3371 | // ---------------------------------- |
| 3372 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3373 | // ---------------------------------- |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3374 | // Pass the global object as the receiver and let the IC stub |
| 3375 | // patch the stack to use the global proxy as 'this' in the |
| 3376 | // invoked function. |
| 3377 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3378 | |
| 3379 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3380 | int arg_count = args->length(); |
| 3381 | for (int i = 0; i < arg_count; i++) { |
| 3382 | LoadAndSpill(args->at(i)); |
| 3383 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3384 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3385 | // Setup the name register and call the IC initialization code. |
| 3386 | __ mov(r2, Operand(var->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3387 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3388 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3389 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3390 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3391 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3392 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3393 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3394 | |
| 3395 | } else if (var != NULL && var->slot() != NULL && |
| 3396 | var->slot()->type() == Slot::LOOKUP) { |
| 3397 | // ---------------------------------- |
| 3398 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3399 | // ---------------------------------- |
| 3400 | |
| 3401 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3402 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3403 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3404 | frame_->EmitPush(r0); |
| 3405 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3406 | // r0: slot value; r1: receiver |
| 3407 | |
| 3408 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3409 | frame_->EmitPush(r0); // function |
| 3410 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3411 | |
| 3412 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3413 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3414 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3415 | |
| 3416 | } else if (property != NULL) { |
| 3417 | // Check if the key is a literal string. |
| 3418 | Literal* literal = property->key()->AsLiteral(); |
| 3419 | |
| 3420 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3421 | // ------------------------------------------------------------------ |
| 3422 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3423 | // ------------------------------------------------------------------ |
| 3424 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3425 | LoadAndSpill(property->obj()); // Receiver. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3426 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3427 | int arg_count = args->length(); |
| 3428 | for (int i = 0; i < arg_count; i++) { |
| 3429 | LoadAndSpill(args->at(i)); |
| 3430 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3431 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3432 | // Set the name register and call the IC initialization code. |
| 3433 | __ mov(r2, Operand(literal->handle())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3434 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3435 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3436 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3437 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3438 | __ ldr(cp, frame_->Context()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3439 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3440 | |
| 3441 | } else { |
| 3442 | // ------------------------------------------- |
| 3443 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3444 | // ------------------------------------------- |
| 3445 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3446 | LoadAndSpill(property->obj()); |
| 3447 | LoadAndSpill(property->key()); |
| 3448 | EmitKeyedLoad(false); |
| 3449 | frame_->Drop(); // key |
| 3450 | // Put the function below the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3451 | if (property->is_synthetic()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3452 | // Use the global receiver. |
| 3453 | frame_->Drop(); |
| 3454 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3455 | LoadGlobalReceiver(r0); |
| 3456 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3457 | frame_->EmitPop(r1); // receiver |
| 3458 | frame_->EmitPush(r0); // function |
| 3459 | frame_->EmitPush(r1); // receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3462 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3463 | CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3464 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | } else { |
| 3468 | // ---------------------------------- |
| 3469 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3470 | // ---------------------------------- |
| 3471 | |
| 3472 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3473 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3474 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3475 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3476 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3477 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3478 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3479 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3480 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3481 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3482 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
| 3485 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3486 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3487 | #ifdef DEBUG |
| 3488 | int original_height = frame_->height(); |
| 3489 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3490 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3491 | Comment cmnt(masm_, "[ CallNew"); |
| 3492 | |
| 3493 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3494 | // expression in new calls must be evaluated before the |
| 3495 | // arguments. This is different from ordinary calls, where the |
| 3496 | // actual function to call is resolved after the arguments have been |
| 3497 | // evaluated. |
| 3498 | |
| 3499 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3500 | // receiver. There is no need to use the global proxy here because |
| 3501 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3502 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3503 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3504 | |
| 3505 | // Push the arguments ("left-to-right") on the stack. |
| 3506 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3507 | int arg_count = args->length(); |
| 3508 | for (int i = 0; i < arg_count; i++) { |
| 3509 | LoadAndSpill(args->at(i)); |
| 3510 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3511 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3512 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3513 | __ mov(r0, Operand(arg_count)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3514 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3515 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3516 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3517 | // Call the construct call builtin that handles allocation and |
| 3518 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3519 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3520 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3521 | frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3522 | |
| 3523 | // 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] | 3524 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3525 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3529 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3530 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3531 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3532 | JumpTarget leave, null, function, non_function_constructor; |
| 3533 | |
| 3534 | // Load the object into r0. |
| 3535 | LoadAndSpill(args->at(0)); |
| 3536 | frame_->EmitPop(r0); |
| 3537 | |
| 3538 | // If the object is a smi, we return null. |
| 3539 | __ tst(r0, Operand(kSmiTagMask)); |
| 3540 | null.Branch(eq); |
| 3541 | |
| 3542 | // Check that the object is a JS object but take special care of JS |
| 3543 | // functions to make sure they have 'Function' as their class. |
| 3544 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3545 | null.Branch(lt); |
| 3546 | |
| 3547 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3548 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3549 | // LAST_JS_OBJECT_TYPE. |
| 3550 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3551 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3552 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3553 | function.Branch(eq); |
| 3554 | |
| 3555 | // Check if the constructor in the map is a function. |
| 3556 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3557 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3558 | non_function_constructor.Branch(ne); |
| 3559 | |
| 3560 | // The r0 register now contains the constructor function. Grab the |
| 3561 | // instance class name from there. |
| 3562 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3563 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3564 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3565 | leave.Jump(); |
| 3566 | |
| 3567 | // Functions have class 'Function'. |
| 3568 | function.Bind(); |
| 3569 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3570 | frame_->EmitPush(r0); |
| 3571 | leave.Jump(); |
| 3572 | |
| 3573 | // Objects with a non-function constructor have class 'Object'. |
| 3574 | non_function_constructor.Bind(); |
| 3575 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3576 | frame_->EmitPush(r0); |
| 3577 | leave.Jump(); |
| 3578 | |
| 3579 | // Non-JS objects have class null. |
| 3580 | null.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3581 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3582 | frame_->EmitPush(r0); |
| 3583 | |
| 3584 | // All done. |
| 3585 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3589 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3590 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3591 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3592 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3593 | LoadAndSpill(args->at(0)); |
| 3594 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3595 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3596 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3597 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3598 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3599 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3600 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3601 | // Load the value. |
| 3602 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3603 | leave.Bind(); |
| 3604 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3608 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3609 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3610 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3611 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3612 | LoadAndSpill(args->at(0)); // Load the object. |
| 3613 | LoadAndSpill(args->at(1)); // Load the value. |
| 3614 | frame_->EmitPop(r0); // r0 contains value |
| 3615 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3616 | // if (object->IsSmi()) return object. |
| 3617 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3618 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3619 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3620 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3621 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3622 | // Store the value. |
| 3623 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3624 | // Update the write barrier. |
| 3625 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3626 | __ RecordWrite(r1, r2, r3); |
| 3627 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3628 | leave.Bind(); |
| 3629 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3633 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3634 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3635 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3636 | LoadAndSpill(args->at(0)); |
| 3637 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3638 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3639 | cc_reg_ = eq; |
| 3640 | } |
| 3641 | |
| 3642 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3643 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3644 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3645 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3646 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3647 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3648 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3649 | LoadAndSpill(args->at(1)); |
| 3650 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3651 | __ CallRuntime(Runtime::kLog, 2); |
| 3652 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3653 | #endif |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3654 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3655 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3659 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3660 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3661 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3662 | LoadAndSpill(args->at(0)); |
| 3663 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3664 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3665 | cc_reg_ = eq; |
| 3666 | } |
| 3667 | |
| 3668 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3669 | // Generates the Math.pow method - currently just calls runtime. |
| 3670 | void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) { |
| 3671 | ASSERT(args->length() == 2); |
| 3672 | Load(args->at(0)); |
| 3673 | Load(args->at(1)); |
| 3674 | frame_->CallRuntime(Runtime::kMath_pow, 2); |
| 3675 | frame_->EmitPush(r0); |
| 3676 | } |
| 3677 | |
| 3678 | |
| 3679 | // Generates the Math.sqrt method - currently just calls runtime. |
| 3680 | void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) { |
| 3681 | ASSERT(args->length() == 1); |
| 3682 | Load(args->at(0)); |
| 3683 | frame_->CallRuntime(Runtime::kMath_sqrt, 1); |
| 3684 | frame_->EmitPush(r0); |
| 3685 | } |
| 3686 | |
| 3687 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3688 | // This should generate code that performs a charCodeAt() call or returns |
| 3689 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3690 | // 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] | 3691 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3692 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3693 | ASSERT(args->length() == 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3694 | Comment(masm_, "[ GenerateFastCharCodeAt"); |
| 3695 | |
| 3696 | LoadAndSpill(args->at(0)); |
| 3697 | LoadAndSpill(args->at(1)); |
| 3698 | frame_->EmitPop(r0); // Index. |
| 3699 | frame_->EmitPop(r1); // String. |
| 3700 | |
| 3701 | Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string; |
| 3702 | |
| 3703 | __ tst(r1, Operand(kSmiTagMask)); |
| 3704 | __ b(eq, &slow); // The 'string' was a Smi. |
| 3705 | |
| 3706 | ASSERT(kSmiTag == 0); |
| 3707 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
| 3708 | __ b(ne, &slow); // The index was negative or not a Smi. |
| 3709 | |
| 3710 | __ bind(&try_again_with_new_string); |
| 3711 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 3712 | __ b(ge, &slow); |
| 3713 | |
| 3714 | // Now r2 has the string type. |
| 3715 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3716 | // Now r3 has the length of the string. Compare with the index. |
| 3717 | __ cmp(r3, Operand(r0, LSR, kSmiTagSize)); |
| 3718 | __ b(le, &slow); |
| 3719 | |
| 3720 | // Here we know the index is in range. Check that string is sequential. |
| 3721 | ASSERT_EQ(0, kSeqStringTag); |
| 3722 | __ tst(r2, Operand(kStringRepresentationMask)); |
| 3723 | __ b(ne, ¬_a_flat_string); |
| 3724 | |
| 3725 | // Check whether it is an ASCII string. |
| 3726 | ASSERT_EQ(0, kTwoByteStringTag); |
| 3727 | __ tst(r2, Operand(kStringEncodingMask)); |
| 3728 | __ b(ne, &ascii_string); |
| 3729 | |
| 3730 | // 2-byte string. We can add without shifting since the Smi tag size is the |
| 3731 | // log2 of the number of bytes in a two-byte character. |
| 3732 | ASSERT_EQ(1, kSmiTagSize); |
| 3733 | ASSERT_EQ(0, kSmiShiftSize); |
| 3734 | __ add(r1, r1, Operand(r0)); |
| 3735 | __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize)); |
| 3736 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3737 | __ jmp(&end); |
| 3738 | |
| 3739 | __ bind(&ascii_string); |
| 3740 | __ add(r1, r1, Operand(r0, LSR, kSmiTagSize)); |
| 3741 | __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 3742 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3743 | __ jmp(&end); |
| 3744 | |
| 3745 | __ bind(¬_a_flat_string); |
| 3746 | __ and_(r2, r2, Operand(kStringRepresentationMask)); |
| 3747 | __ cmp(r2, Operand(kConsStringTag)); |
| 3748 | __ b(ne, &slow); |
| 3749 | |
| 3750 | // ConsString. |
| 3751 | // Check that the right hand side is the empty string (ie if this is really a |
| 3752 | // flat string in a cons string). If that is not the case we would rather go |
| 3753 | // to the runtime system now, to flatten the string. |
| 3754 | __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset)); |
| 3755 | __ LoadRoot(r3, Heap::kEmptyStringRootIndex); |
| 3756 | __ cmp(r2, Operand(r3)); |
| 3757 | __ b(ne, &slow); |
| 3758 | |
| 3759 | // Get the first of the two strings. |
| 3760 | __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset)); |
| 3761 | __ jmp(&try_again_with_new_string); |
| 3762 | |
| 3763 | __ bind(&slow); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3764 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3765 | |
| 3766 | __ bind(&end); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3767 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3771 | void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) { |
| 3772 | Comment(masm_, "[ GenerateCharFromCode"); |
| 3773 | ASSERT(args->length() == 1); |
| 3774 | |
| 3775 | LoadAndSpill(args->at(0)); |
| 3776 | frame_->EmitPop(r0); |
| 3777 | |
| 3778 | JumpTarget slow_case; |
| 3779 | JumpTarget exit; |
| 3780 | |
| 3781 | // Fast case of Heap::LookupSingleCharacterStringFromCode. |
| 3782 | ASSERT(kSmiTag == 0); |
| 3783 | ASSERT(kSmiShiftSize == 0); |
| 3784 | ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1)); |
| 3785 | __ tst(r0, Operand(kSmiTagMask | |
| 3786 | ((~String::kMaxAsciiCharCode) << kSmiTagSize))); |
| 3787 | slow_case.Branch(nz); |
| 3788 | |
| 3789 | ASSERT(kSmiTag == 0); |
| 3790 | __ mov(r1, Operand(Factory::single_character_string_cache())); |
| 3791 | __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 3792 | __ ldr(r1, MemOperand(r1, FixedArray::kHeaderSize - kHeapObjectTag)); |
| 3793 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 3794 | __ cmp(r1, ip); |
| 3795 | slow_case.Branch(eq); |
| 3796 | |
| 3797 | frame_->EmitPush(r1); |
| 3798 | exit.Jump(); |
| 3799 | |
| 3800 | slow_case.Bind(); |
| 3801 | frame_->EmitPush(r0); |
| 3802 | frame_->CallRuntime(Runtime::kCharFromCode, 1); |
| 3803 | frame_->EmitPush(r0); |
| 3804 | |
| 3805 | exit.Bind(); |
| 3806 | } |
| 3807 | |
| 3808 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3809 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3810 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3811 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3812 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3813 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3814 | // We need the CC bits to come out as not_equal in the case where the |
| 3815 | // object is a smi. This can't be done with the usual test opcode so |
| 3816 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3817 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3818 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3819 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3820 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3821 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3822 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3823 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3824 | cc_reg_ = eq; |
| 3825 | } |
| 3826 | |
| 3827 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3828 | void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3829 | VirtualFrame::SpilledScope spilled_scope(frame_); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3830 | ASSERT(args->length() == 1); |
| 3831 | LoadAndSpill(args->at(0)); |
| 3832 | JumpTarget answer; |
| 3833 | // We need the CC bits to come out as not_equal in the case where the |
| 3834 | // object is a smi. This can't be done with the usual test opcode so |
| 3835 | // we use XOR to get the right CC bits. |
| 3836 | frame_->EmitPop(r0); |
| 3837 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3838 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
| 3839 | answer.Branch(ne); |
| 3840 | // It is a heap object - get the map. Check if the object is a regexp. |
| 3841 | __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE); |
| 3842 | answer.Bind(); |
| 3843 | cc_reg_ = eq; |
| 3844 | } |
| 3845 | |
| 3846 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3847 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 3848 | // This generates a fast version of: |
| 3849 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3850 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3851 | ASSERT(args->length() == 1); |
| 3852 | LoadAndSpill(args->at(0)); |
| 3853 | frame_->EmitPop(r1); |
| 3854 | __ tst(r1, Operand(kSmiTagMask)); |
| 3855 | false_target()->Branch(eq); |
| 3856 | |
| 3857 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 3858 | __ cmp(r1, ip); |
| 3859 | true_target()->Branch(eq); |
| 3860 | |
| 3861 | Register map_reg = r2; |
| 3862 | __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3863 | // Undetectable objects behave like undefined when tested with typeof. |
| 3864 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 3865 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3866 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 3867 | false_target()->Branch(eq); |
| 3868 | |
| 3869 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 3870 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3871 | false_target()->Branch(lt); |
| 3872 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
| 3873 | cc_reg_ = le; |
| 3874 | } |
| 3875 | |
| 3876 | |
| 3877 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 3878 | // This generates a fast version of: |
| 3879 | // (%_ClassOf(arg) === 'Function') |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3880 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3881 | ASSERT(args->length() == 1); |
| 3882 | LoadAndSpill(args->at(0)); |
| 3883 | frame_->EmitPop(r0); |
| 3884 | __ tst(r0, Operand(kSmiTagMask)); |
| 3885 | false_target()->Branch(eq); |
| 3886 | Register map_reg = r2; |
| 3887 | __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE); |
| 3888 | cc_reg_ = eq; |
| 3889 | } |
| 3890 | |
| 3891 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3892 | void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3893 | VirtualFrame::SpilledScope spilled_scope(frame_); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3894 | ASSERT(args->length() == 1); |
| 3895 | LoadAndSpill(args->at(0)); |
| 3896 | frame_->EmitPop(r0); |
| 3897 | __ tst(r0, Operand(kSmiTagMask)); |
| 3898 | false_target()->Branch(eq); |
| 3899 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3900 | __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3901 | __ tst(r1, Operand(1 << Map::kIsUndetectable)); |
| 3902 | cc_reg_ = ne; |
| 3903 | } |
| 3904 | |
| 3905 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3906 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3907 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3908 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3909 | |
| 3910 | // Get the frame pointer for the calling frame. |
| 3911 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3912 | |
| 3913 | // Skip the arguments adaptor frame if it exists. |
| 3914 | Label check_frame_marker; |
| 3915 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3916 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3917 | __ b(ne, &check_frame_marker); |
| 3918 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3919 | |
| 3920 | // Check the marker in the calling frame. |
| 3921 | __ bind(&check_frame_marker); |
| 3922 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3923 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3924 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3928 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3929 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3930 | ASSERT(args->length() == 0); |
| 3931 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3932 | Label exit; |
| 3933 | |
| 3934 | // Get the number of formal parameters. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3935 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3936 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3937 | // Check if the calling frame is an arguments adaptor frame. |
| 3938 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3939 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 3940 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 3941 | __ b(ne, &exit); |
| 3942 | |
| 3943 | // Arguments adaptor case: Read the arguments length from the |
| 3944 | // adaptor frame. |
| 3945 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 3946 | |
| 3947 | __ bind(&exit); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3948 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3952 | void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3953 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3954 | ASSERT(args->length() == 1); |
| 3955 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3956 | // Satisfy contract with ArgumentsAccessStub: |
| 3957 | // 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] | 3958 | LoadAndSpill(args->at(0)); |
| 3959 | frame_->EmitPop(r1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3960 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3961 | |
| 3962 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3963 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3964 | frame_->CallStub(&stub, 0); |
| 3965 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3969 | void CodeGenerator::GenerateRandomHeapNumber( |
| 3970 | ZoneList<Expression*>* args) { |
| 3971 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3972 | ASSERT(args->length() == 0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3973 | |
| 3974 | Label slow_allocate_heapnumber; |
| 3975 | Label heapnumber_allocated; |
| 3976 | |
| 3977 | __ AllocateHeapNumber(r0, r1, r2, &slow_allocate_heapnumber); |
| 3978 | __ jmp(&heapnumber_allocated); |
| 3979 | |
| 3980 | __ bind(&slow_allocate_heapnumber); |
| 3981 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3982 | __ push(r0); |
| 3983 | __ CallRuntime(Runtime::kNumberUnaryMinus, 1); |
| 3984 | |
| 3985 | __ bind(&heapnumber_allocated); |
| 3986 | __ PrepareCallCFunction(1, r1); |
| 3987 | __ CallCFunction( |
| 3988 | ExternalReference::fill_heap_number_with_random_function(), 1); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3989 | frame_->EmitPush(r0); |
| 3990 | } |
| 3991 | |
| 3992 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3993 | void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) { |
| 3994 | ASSERT_EQ(2, args->length()); |
| 3995 | |
| 3996 | Load(args->at(0)); |
| 3997 | Load(args->at(1)); |
| 3998 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3999 | StringAddStub stub(NO_STRING_ADD_FLAGS); |
| 4000 | frame_->CallStub(&stub, 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 4001 | frame_->EmitPush(r0); |
| 4002 | } |
| 4003 | |
| 4004 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4005 | void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) { |
| 4006 | ASSERT_EQ(3, args->length()); |
| 4007 | |
| 4008 | Load(args->at(0)); |
| 4009 | Load(args->at(1)); |
| 4010 | Load(args->at(2)); |
| 4011 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4012 | SubStringStub stub; |
| 4013 | frame_->CallStub(&stub, 3); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4014 | frame_->EmitPush(r0); |
| 4015 | } |
| 4016 | |
| 4017 | |
| 4018 | void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) { |
| 4019 | ASSERT_EQ(2, args->length()); |
| 4020 | |
| 4021 | Load(args->at(0)); |
| 4022 | Load(args->at(1)); |
| 4023 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4024 | StringCompareStub stub; |
| 4025 | frame_->CallStub(&stub, 2); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4026 | frame_->EmitPush(r0); |
| 4027 | } |
| 4028 | |
| 4029 | |
| 4030 | void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) { |
| 4031 | ASSERT_EQ(4, args->length()); |
| 4032 | |
| 4033 | Load(args->at(0)); |
| 4034 | Load(args->at(1)); |
| 4035 | Load(args->at(2)); |
| 4036 | Load(args->at(3)); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 4037 | RegExpExecStub stub; |
| 4038 | frame_->CallStub(&stub, 4); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4039 | frame_->EmitPush(r0); |
| 4040 | } |
| 4041 | |
| 4042 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 4043 | void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) { |
| 4044 | // No stub. This code only occurs a few times in regexp.js. |
| 4045 | const int kMaxInlineLength = 100; |
| 4046 | ASSERT_EQ(3, args->length()); |
| 4047 | Load(args->at(0)); // Size of array, smi. |
| 4048 | Load(args->at(1)); // "index" property value. |
| 4049 | Load(args->at(2)); // "input" property value. |
| 4050 | { |
| 4051 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 4052 | Label slowcase; |
| 4053 | Label done; |
| 4054 | __ ldr(r1, MemOperand(sp, kPointerSize * 2)); |
| 4055 | STATIC_ASSERT(kSmiTag == 0); |
| 4056 | STATIC_ASSERT(kSmiTagSize == 1); |
| 4057 | __ tst(r1, Operand(kSmiTagMask)); |
| 4058 | __ b(ne, &slowcase); |
| 4059 | __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength))); |
| 4060 | __ b(hi, &slowcase); |
| 4061 | // Smi-tagging is equivalent to multiplying by 2. |
| 4062 | // Allocate RegExpResult followed by FixedArray with size in ebx. |
| 4063 | // JSArray: [Map][empty properties][Elements][Length-smi][index][input] |
| 4064 | // Elements: [Map][Length][..elements..] |
| 4065 | // Size of JSArray with two in-object properties and the header of a |
| 4066 | // FixedArray. |
| 4067 | int objects_size = |
| 4068 | (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize; |
| 4069 | __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize)); |
| 4070 | __ add(r2, r5, Operand(objects_size)); |
| 4071 | __ AllocateInNewSpace(r2, // In: Size, in words. |
| 4072 | r0, // Out: Start of allocation (tagged). |
| 4073 | r3, // Scratch register. |
| 4074 | r4, // Scratch register. |
| 4075 | &slowcase, |
| 4076 | TAG_OBJECT); |
| 4077 | // r0: Start of allocated area, object-tagged. |
| 4078 | // r1: Number of elements in array, as smi. |
| 4079 | // r5: Number of elements, untagged. |
| 4080 | |
| 4081 | // Set JSArray map to global.regexp_result_map(). |
| 4082 | // Set empty properties FixedArray. |
| 4083 | // Set elements to point to FixedArray allocated right after the JSArray. |
| 4084 | // Interleave operations for better latency. |
| 4085 | __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 4086 | __ add(r3, r0, Operand(JSRegExpResult::kSize)); |
| 4087 | __ mov(r4, Operand(Factory::empty_fixed_array())); |
| 4088 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 4089 | __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 4090 | __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX)); |
| 4091 | __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset)); |
| 4092 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4093 | |
| 4094 | // Set input, index and length fields from arguments. |
| 4095 | __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit())); |
| 4096 | __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset)); |
| 4097 | __ add(sp, sp, Operand(kPointerSize)); |
| 4098 | __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset)); |
| 4099 | __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset)); |
| 4100 | |
| 4101 | // Fill out the elements FixedArray. |
| 4102 | // r0: JSArray, tagged. |
| 4103 | // r3: FixedArray, tagged. |
| 4104 | // r5: Number of elements in array, untagged. |
| 4105 | |
| 4106 | // Set map. |
| 4107 | __ mov(r2, Operand(Factory::fixed_array_map())); |
| 4108 | __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 4109 | // Set FixedArray length. |
| 4110 | __ str(r5, FieldMemOperand(r3, FixedArray::kLengthOffset)); |
| 4111 | // Fill contents of fixed-array with the-hole. |
| 4112 | __ mov(r2, Operand(Factory::the_hole_value())); |
| 4113 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 4114 | // Fill fixed array elements with hole. |
| 4115 | // r0: JSArray, tagged. |
| 4116 | // r2: the hole. |
| 4117 | // r3: Start of elements in FixedArray. |
| 4118 | // r5: Number of elements to fill. |
| 4119 | Label loop; |
| 4120 | __ tst(r5, Operand(r5)); |
| 4121 | __ bind(&loop); |
| 4122 | __ b(le, &done); // Jump if r1 is negative or zero. |
| 4123 | __ sub(r5, r5, Operand(1), SetCC); |
| 4124 | __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2)); |
| 4125 | __ jmp(&loop); |
| 4126 | |
| 4127 | __ bind(&slowcase); |
| 4128 | __ CallRuntime(Runtime::kRegExpConstructResult, 3); |
| 4129 | |
| 4130 | __ bind(&done); |
| 4131 | } |
| 4132 | frame_->Forget(3); |
| 4133 | frame_->EmitPush(r0); |
| 4134 | } |
| 4135 | |
| 4136 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 4137 | class DeferredSearchCache: public DeferredCode { |
| 4138 | public: |
| 4139 | DeferredSearchCache(Register dst, Register cache, Register key) |
| 4140 | : dst_(dst), cache_(cache), key_(key) { |
| 4141 | set_comment("[ DeferredSearchCache"); |
| 4142 | } |
| 4143 | |
| 4144 | virtual void Generate(); |
| 4145 | |
| 4146 | private: |
| 4147 | Register dst_, cache_, key_; |
| 4148 | }; |
| 4149 | |
| 4150 | |
| 4151 | void DeferredSearchCache::Generate() { |
| 4152 | __ push(cache_); |
| 4153 | __ push(key_); |
| 4154 | __ CallRuntime(Runtime::kGetFromCache, 2); |
| 4155 | if (!dst_.is(r0)) { |
| 4156 | __ mov(dst_, r0); |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | |
| 4161 | void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) { |
| 4162 | ASSERT_EQ(2, args->length()); |
| 4163 | |
| 4164 | ASSERT_NE(NULL, args->at(0)->AsLiteral()); |
| 4165 | int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value(); |
| 4166 | |
| 4167 | Handle<FixedArray> jsfunction_result_caches( |
| 4168 | Top::global_context()->jsfunction_result_caches()); |
| 4169 | if (jsfunction_result_caches->length() <= cache_id) { |
| 4170 | __ Abort("Attempt to use undefined cache."); |
| 4171 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
| 4172 | frame_->EmitPush(r0); |
| 4173 | return; |
| 4174 | } |
| 4175 | Handle<FixedArray> cache_obj( |
| 4176 | FixedArray::cast(jsfunction_result_caches->get(cache_id))); |
| 4177 | |
| 4178 | Load(args->at(1)); |
| 4179 | frame_->EmitPop(r2); |
| 4180 | |
| 4181 | DeferredSearchCache* deferred = new DeferredSearchCache(r0, r1, r2); |
| 4182 | |
| 4183 | const int kFingerOffset = |
| 4184 | FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex); |
| 4185 | ASSERT(kSmiTag == 0 && kSmiTagSize == 1); |
| 4186 | __ mov(r1, Operand(cache_obj)); |
| 4187 | __ ldr(r0, FieldMemOperand(r1, kFingerOffset)); |
| 4188 | // r0 now holds finger offset as a smi. |
| 4189 | __ add(r3, r1, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 4190 | // r3 now points to the start of fixed array elements. |
| 4191 | __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex)); |
| 4192 | // Note side effect of PreIndex: r3 now points to the key of the pair. |
| 4193 | __ cmp(r2, r0); |
| 4194 | deferred->Branch(ne); |
| 4195 | |
| 4196 | __ ldr(r0, MemOperand(r3, kPointerSize)); |
| 4197 | |
| 4198 | deferred->BindExit(); |
| 4199 | frame_->EmitPush(r0); |
| 4200 | } |
| 4201 | |
| 4202 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4203 | void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) { |
| 4204 | ASSERT_EQ(args->length(), 1); |
| 4205 | |
| 4206 | // Load the argument on the stack and jump to the runtime. |
| 4207 | Load(args->at(0)); |
| 4208 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 4209 | NumberToStringStub stub; |
| 4210 | frame_->CallStub(&stub, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4211 | frame_->EmitPush(r0); |
| 4212 | } |
| 4213 | |
| 4214 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4215 | void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) { |
| 4216 | Comment cmnt(masm_, "[ GenerateCallFunction"); |
| 4217 | |
| 4218 | ASSERT(args->length() >= 2); |
| 4219 | |
| 4220 | int n_args = args->length() - 2; // for receiver and function. |
| 4221 | Load(args->at(0)); // receiver |
| 4222 | for (int i = 0; i < n_args; i++) { |
| 4223 | Load(args->at(i + 1)); |
| 4224 | } |
| 4225 | Load(args->at(n_args + 1)); // function |
| 4226 | frame_->CallJSFunction(n_args); |
| 4227 | frame_->EmitPush(r0); |
| 4228 | } |
| 4229 | |
| 4230 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4231 | void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) { |
| 4232 | ASSERT_EQ(args->length(), 1); |
| 4233 | // Load the argument on the stack and jump to the runtime. |
| 4234 | Load(args->at(0)); |
| 4235 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 4236 | frame_->EmitPush(r0); |
| 4237 | } |
| 4238 | |
| 4239 | |
| 4240 | void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) { |
| 4241 | ASSERT_EQ(args->length(), 1); |
| 4242 | // Load the argument on the stack and jump to the runtime. |
| 4243 | Load(args->at(0)); |
| 4244 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 4245 | frame_->EmitPush(r0); |
| 4246 | } |
| 4247 | |
| 4248 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4249 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4250 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4251 | ASSERT(args->length() == 2); |
| 4252 | |
| 4253 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4254 | LoadAndSpill(args->at(0)); |
| 4255 | LoadAndSpill(args->at(1)); |
| 4256 | frame_->EmitPop(r0); |
| 4257 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4258 | __ cmp(r0, Operand(r1)); |
| 4259 | cc_reg_ = eq; |
| 4260 | } |
| 4261 | |
| 4262 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4263 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4264 | #ifdef DEBUG |
| 4265 | int original_height = frame_->height(); |
| 4266 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4267 | VirtualFrame::SpilledScope spilled_scope(frame_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4268 | if (CheckForInlineRuntimeCall(node)) { |
| 4269 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4270 | (!has_cc() && frame_->height() == original_height + 1)); |
| 4271 | return; |
| 4272 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4273 | |
| 4274 | ZoneList<Expression*>* args = node->arguments(); |
| 4275 | Comment cmnt(masm_, "[ CallRuntime"); |
| 4276 | Runtime::Function* function = node->function(); |
| 4277 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4278 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4279 | // Prepare stack for calling JS runtime function. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4280 | // Push the builtins object found in the current global object. |
| 4281 | __ ldr(r1, GlobalObject()); |
| 4282 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4283 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4284 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4285 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4286 | // Push the arguments ("left-to-right"). |
| 4287 | int arg_count = args->length(); |
| 4288 | for (int i = 0; i < arg_count; i++) { |
| 4289 | LoadAndSpill(args->at(i)); |
| 4290 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4291 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4292 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4293 | // Call the JS runtime function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4294 | __ mov(r2, Operand(node->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4295 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 4296 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4297 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4298 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4299 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4300 | } else { |
| 4301 | // Call the C runtime function. |
| 4302 | frame_->CallRuntime(function, arg_count); |
| 4303 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4304 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4305 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4309 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4310 | #ifdef DEBUG |
| 4311 | int original_height = frame_->height(); |
| 4312 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4313 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4314 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 4315 | |
| 4316 | Token::Value op = node->op(); |
| 4317 | |
| 4318 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4319 | LoadConditionAndSpill(node->expression(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4320 | false_target(), |
| 4321 | true_target(), |
| 4322 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4323 | // LoadCondition may (and usually does) leave a test and branch to |
| 4324 | // be emitted by the caller. In that case, negate the condition. |
| 4325 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4326 | |
| 4327 | } else if (op == Token::DELETE) { |
| 4328 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4329 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4330 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4331 | LoadAndSpill(property->obj()); |
| 4332 | LoadAndSpill(property->key()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4333 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4334 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4335 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4336 | Slot* slot = variable->slot(); |
| 4337 | if (variable->is_global()) { |
| 4338 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4339 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4340 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4341 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4342 | |
| 4343 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 4344 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4345 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4346 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4347 | frame_->EmitPush(r0); |
| 4348 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4349 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4350 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4351 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4352 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4353 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4354 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4355 | } else { |
| 4356 | // Default: Result of deleting non-global, not dynamically |
| 4357 | // introduced variables is false. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4358 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4359 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4360 | |
| 4361 | } else { |
| 4362 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4363 | LoadAndSpill(node->expression()); // may have side-effects |
| 4364 | frame_->Drop(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4365 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4366 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4367 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4368 | |
| 4369 | } else if (op == Token::TYPEOF) { |
| 4370 | // Special case for loading the typeof expression; see comment on |
| 4371 | // LoadTypeofExpression(). |
| 4372 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4373 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 4374 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4375 | |
| 4376 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4377 | bool overwrite = |
| 4378 | (node->expression()->AsBinaryOperation() != NULL && |
| 4379 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4380 | LoadAndSpill(node->expression()); |
| 4381 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4382 | switch (op) { |
| 4383 | case Token::NOT: |
| 4384 | case Token::DELETE: |
| 4385 | case Token::TYPEOF: |
| 4386 | UNREACHABLE(); // handled above |
| 4387 | break; |
| 4388 | |
| 4389 | case Token::SUB: { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4390 | GenericUnaryOpStub stub(Token::SUB, overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4391 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4392 | break; |
| 4393 | } |
| 4394 | |
| 4395 | case Token::BIT_NOT: { |
| 4396 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4397 | JumpTarget smi_label; |
| 4398 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4399 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4400 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4401 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4402 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 4403 | frame_->CallStub(&stub, 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4404 | continue_label.Jump(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4405 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4406 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4407 | __ mvn(r0, Operand(r0)); |
| 4408 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4409 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4410 | break; |
| 4411 | } |
| 4412 | |
| 4413 | case Token::VOID: |
| 4414 | // since the stack top is cached in r0, popping and then |
| 4415 | // pushing a value can be done by just writing to r0. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4416 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4417 | break; |
| 4418 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4419 | case Token::ADD: { |
| 4420 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4421 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4422 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4423 | continue_label.Branch(eq); |
| 4424 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4425 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4426 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4427 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4428 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4429 | default: |
| 4430 | UNREACHABLE(); |
| 4431 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4432 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4433 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4434 | ASSERT(!has_valid_frame() || |
| 4435 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4436 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4440 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4441 | #ifdef DEBUG |
| 4442 | int original_height = frame_->height(); |
| 4443 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4444 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4445 | Comment cmnt(masm_, "[ CountOperation"); |
| 4446 | |
| 4447 | bool is_postfix = node->is_postfix(); |
| 4448 | bool is_increment = node->op() == Token::INC; |
| 4449 | |
| 4450 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 4451 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 4452 | |
| 4453 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4454 | if (is_postfix) { |
| 4455 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4456 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4457 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4458 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4459 | // A constant reference is not saved to, so a constant reference is not a |
| 4460 | // compound assignment reference. |
| 4461 | { Reference target(this, node->expression(), !is_const); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4462 | if (target.is_illegal()) { |
| 4463 | // Spoof the virtual frame to have the expected height (one higher |
| 4464 | // than on entry). |
| 4465 | if (!is_postfix) { |
| 4466 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 4467 | frame_->EmitPush(r0); |
| 4468 | } |
| 4469 | ASSERT(frame_->height() == original_height + 1); |
| 4470 | return; |
| 4471 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4472 | target.GetValue(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4473 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4474 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4475 | JumpTarget slow; |
| 4476 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4477 | |
| 4478 | // Load the value (1) into register r1. |
| 4479 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 4480 | |
| 4481 | // Check for smi operand. |
| 4482 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4483 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4484 | |
| 4485 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4486 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4487 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4488 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4489 | |
| 4490 | // Perform optimistic increment/decrement. |
| 4491 | if (is_increment) { |
| 4492 | __ add(r0, r0, Operand(r1), SetCC); |
| 4493 | } else { |
| 4494 | __ sub(r0, r0, Operand(r1), SetCC); |
| 4495 | } |
| 4496 | |
| 4497 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4498 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4499 | |
| 4500 | // Revert optimistic increment/decrement. |
| 4501 | if (is_increment) { |
| 4502 | __ sub(r0, r0, Operand(r1)); |
| 4503 | } else { |
| 4504 | __ add(r0, r0, Operand(r1)); |
| 4505 | } |
| 4506 | |
| 4507 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4508 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4509 | { |
| 4510 | // Convert the operand to a number. |
| 4511 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4512 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4513 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4514 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4515 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4516 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4519 | // Compute the new value. |
| 4520 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 4521 | frame_->EmitPush(r0); |
| 4522 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4523 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4524 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4525 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 4526 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4530 | exit.Bind(); |
| 4531 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4532 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4536 | if (is_postfix) frame_->EmitPop(r0); |
| 4537 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4541 | void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4542 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 4543 | // operators must yield the result of one of the two expressions |
| 4544 | // before any ToBoolean() conversions. This means that the value |
| 4545 | // produced by a && or || operator is not necessarily a boolean. |
| 4546 | |
| 4547 | // NOTE: If the left hand side produces a materialized value (not in |
| 4548 | // the CC register), we force the right hand side to do the |
| 4549 | // same. This is necessary because we may have to branch to the exit |
| 4550 | // after evaluating the left hand side (due to the shortcut |
| 4551 | // semantics), but the compiler must (statically) know if the result |
| 4552 | // of compiling the binary operation is materialized or not. |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4553 | if (node->op() == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4554 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4555 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4556 | &is_true, |
| 4557 | false_target(), |
| 4558 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4559 | if (has_valid_frame() && !has_cc()) { |
| 4560 | // 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] | 4561 | JumpTarget pop_and_continue; |
| 4562 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4563 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4564 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4565 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4566 | // Avoid popping the result if it converts to 'false' using the |
| 4567 | // standard ToBoolean() conversion as described in ECMA-262, |
| 4568 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4569 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4570 | Branch(false, &exit); |
| 4571 | |
| 4572 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4573 | pop_and_continue.Bind(); |
| 4574 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4575 | |
| 4576 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4577 | is_true.Bind(); |
| 4578 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4579 | |
| 4580 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4581 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4582 | } else if (has_cc() || is_true.is_linked()) { |
| 4583 | // The left-hand side is either (a) partially compiled to |
| 4584 | // control flow with a final branch left to emit or (b) fully |
| 4585 | // compiled to control flow and possibly true. |
| 4586 | if (has_cc()) { |
| 4587 | Branch(false, false_target()); |
| 4588 | } |
| 4589 | is_true.Bind(); |
| 4590 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4591 | true_target(), |
| 4592 | false_target(), |
| 4593 | false); |
| 4594 | } else { |
| 4595 | // Nothing to do. |
| 4596 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4599 | } else { |
| 4600 | ASSERT(node->op() == Token::OR); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4601 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4602 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4603 | true_target(), |
| 4604 | &is_false, |
| 4605 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4606 | if (has_valid_frame() && !has_cc()) { |
| 4607 | // 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] | 4608 | JumpTarget pop_and_continue; |
| 4609 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4610 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4611 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4612 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4613 | // Avoid popping the result if it converts to 'true' using the |
| 4614 | // standard ToBoolean() conversion as described in ECMA-262, |
| 4615 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4616 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4617 | Branch(true, &exit); |
| 4618 | |
| 4619 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4620 | pop_and_continue.Bind(); |
| 4621 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4622 | |
| 4623 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4624 | is_false.Bind(); |
| 4625 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4626 | |
| 4627 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4628 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4629 | } else if (has_cc() || is_false.is_linked()) { |
| 4630 | // The left-hand side is either (a) partially compiled to |
| 4631 | // control flow with a final branch left to emit or (b) fully |
| 4632 | // compiled to control flow and possibly false. |
| 4633 | if (has_cc()) { |
| 4634 | Branch(true, true_target()); |
| 4635 | } |
| 4636 | is_false.Bind(); |
| 4637 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4638 | true_target(), |
| 4639 | false_target(), |
| 4640 | false); |
| 4641 | } else { |
| 4642 | // Nothing to do. |
| 4643 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4644 | } |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4645 | } |
| 4646 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4647 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4648 | |
| 4649 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
| 4650 | #ifdef DEBUG |
| 4651 | int original_height = frame_->height(); |
| 4652 | #endif |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4653 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 4654 | |
| 4655 | if (node->op() == Token::AND || node->op() == Token::OR) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4656 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 4657 | GenerateLogicalBooleanOperation(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4658 | } else { |
| 4659 | // Optimize for the case where (at least) one of the expressions |
| 4660 | // is a literal small integer. |
| 4661 | Literal* lliteral = node->left()->AsLiteral(); |
| 4662 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4663 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 4664 | // never return a constant/immutable object. |
| 4665 | bool overwrite_left = |
| 4666 | (node->left()->AsBinaryOperation() != NULL && |
| 4667 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 4668 | bool overwrite_right = |
| 4669 | (node->right()->AsBinaryOperation() != NULL && |
| 4670 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4671 | |
| 4672 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4673 | VirtualFrame::RegisterAllocationScope scope(this); |
| 4674 | Load(node->left()); |
| 4675 | VirtualFrameSmiOperation( |
| 4676 | node->op(), |
| 4677 | rliteral->handle(), |
| 4678 | false, |
| 4679 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4680 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4681 | VirtualFrame::RegisterAllocationScope scope(this); |
| 4682 | Load(node->right()); |
| 4683 | VirtualFrameSmiOperation(node->op(), |
| 4684 | lliteral->handle(), |
| 4685 | true, |
| 4686 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4687 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4688 | VirtualFrame::RegisterAllocationScope scope(this); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4689 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 4690 | if (overwrite_left) { |
| 4691 | overwrite_mode = OVERWRITE_LEFT; |
| 4692 | } else if (overwrite_right) { |
| 4693 | overwrite_mode = OVERWRITE_RIGHT; |
| 4694 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4695 | Load(node->left()); |
| 4696 | Load(node->right()); |
| 4697 | VirtualFrameBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4698 | } |
| 4699 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4700 | ASSERT(!has_valid_frame() || |
| 4701 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4702 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4706 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4707 | #ifdef DEBUG |
| 4708 | int original_height = frame_->height(); |
| 4709 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4710 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4711 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4712 | frame_->EmitPush(r0); |
| 4713 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4717 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4718 | #ifdef DEBUG |
| 4719 | int original_height = frame_->height(); |
| 4720 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4721 | Comment cmnt(masm_, "[ CompareOperation"); |
| 4722 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4723 | VirtualFrame::RegisterAllocationScope nonspilled_scope(this); |
| 4724 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4725 | // Get the expressions from the node. |
| 4726 | Expression* left = node->left(); |
| 4727 | Expression* right = node->right(); |
| 4728 | Token::Value op = node->op(); |
| 4729 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4730 | // To make null checks efficient, we check if either left or right is the |
| 4731 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 4732 | // instead of calling the (very) general runtime routine for checking |
| 4733 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4734 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4735 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4736 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4737 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4738 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4739 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4740 | if (left_is_null || right_is_null) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4741 | Load(left_is_null ? right : left); |
| 4742 | Register tos = frame_->PopToRegister(); |
| 4743 | // JumpTargets can't cope with register allocation yet. |
| 4744 | frame_->SpillAll(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4745 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4746 | __ cmp(tos, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4747 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4748 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4749 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4750 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4751 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4752 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4753 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4754 | __ cmp(tos, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4755 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4756 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4757 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4758 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4759 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4760 | // It can be an undetectable object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4761 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
| 4762 | __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 4763 | __ and_(tos, tos, Operand(1 << Map::kIsUndetectable)); |
| 4764 | __ cmp(tos, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4767 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4768 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4769 | return; |
| 4770 | } |
| 4771 | } |
| 4772 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4773 | // To make typeof testing for natives implemented in JavaScript really |
| 4774 | // efficient, we generate special code for expressions of the form: |
| 4775 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4776 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 4777 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4778 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4779 | (right->AsLiteral() != NULL && |
| 4780 | right->AsLiteral()->handle()->IsString())) { |
| 4781 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4782 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4783 | // Load the operand, move it to a register. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4784 | LoadTypeofExpression(operation->expression()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4785 | Register tos = frame_->PopToRegister(); |
| 4786 | |
| 4787 | // JumpTargets can't cope with register allocation yet. |
| 4788 | frame_->SpillAll(); |
| 4789 | |
| 4790 | Register scratch = VirtualFrame::scratch0(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4791 | |
| 4792 | if (check->Equals(Heap::number_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4793 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4794 | true_target()->Branch(eq); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4795 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4796 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4797 | __ cmp(tos, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4798 | cc_reg_ = eq; |
| 4799 | |
| 4800 | } else if (check->Equals(Heap::string_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4801 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4802 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4803 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4804 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4805 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4806 | // It can be an undetectable string object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4807 | __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 4808 | __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable)); |
| 4809 | __ cmp(scratch, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4810 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4811 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4812 | __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset)); |
| 4813 | __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4814 | cc_reg_ = lt; |
| 4815 | |
| 4816 | } else if (check->Equals(Heap::boolean_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4817 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4818 | __ cmp(tos, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4819 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4820 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4821 | __ cmp(tos, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4822 | cc_reg_ = eq; |
| 4823 | |
| 4824 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4825 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4826 | __ cmp(tos, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4827 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4828 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4829 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4830 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4831 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4832 | // It can be an undetectable object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4833 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
| 4834 | __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 4835 | __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable)); |
| 4836 | __ cmp(scratch, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4837 | |
| 4838 | cc_reg_ = eq; |
| 4839 | |
| 4840 | } else if (check->Equals(Heap::function_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4841 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4842 | false_target()->Branch(eq); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4843 | Register map_reg = scratch; |
| 4844 | __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4845 | true_target()->Branch(eq); |
| 4846 | // Regular expressions are callable so typeof == 'function'. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4847 | __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4848 | cc_reg_ = eq; |
| 4849 | |
| 4850 | } else if (check->Equals(Heap::object_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4851 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4852 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4853 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4854 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4855 | __ cmp(tos, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4856 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4857 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4858 | Register map_reg = scratch; |
| 4859 | __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4860 | false_target()->Branch(eq); |
| 4861 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4862 | // It can be an undetectable object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4863 | __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 4864 | __ and_(tos, tos, Operand(1 << Map::kIsUndetectable)); |
| 4865 | __ cmp(tos, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4866 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4867 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4868 | __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4869 | __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4870 | false_target()->Branch(lt); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4871 | __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4872 | cc_reg_ = le; |
| 4873 | |
| 4874 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4875 | // Uncommon case: typeof testing against a string literal that is |
| 4876 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4877 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4878 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4879 | ASSERT(!has_valid_frame() || |
| 4880 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4881 | return; |
| 4882 | } |
| 4883 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4884 | switch (op) { |
| 4885 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4886 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4887 | break; |
| 4888 | |
| 4889 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4890 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4891 | break; |
| 4892 | |
| 4893 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4894 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4895 | break; |
| 4896 | |
| 4897 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4898 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4899 | break; |
| 4900 | |
| 4901 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4902 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4903 | break; |
| 4904 | |
| 4905 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4906 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4907 | break; |
| 4908 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4909 | case Token::IN: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4910 | VirtualFrame::SpilledScope scope(frame_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4911 | LoadAndSpill(left); |
| 4912 | LoadAndSpill(right); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4913 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4914 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4915 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4916 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4917 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4918 | case Token::INSTANCEOF: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 4919 | VirtualFrame::SpilledScope scope(frame_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4920 | LoadAndSpill(left); |
| 4921 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4922 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4923 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4924 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4925 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4926 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4927 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4928 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4929 | |
| 4930 | default: |
| 4931 | UNREACHABLE(); |
| 4932 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4933 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4934 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4938 | void CodeGenerator::EmitKeyedLoad(bool is_global) { |
| 4939 | Comment cmnt(masm_, "[ Load from keyed Property"); |
| 4940 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 4941 | RelocInfo::Mode rmode = is_global |
| 4942 | ? RelocInfo::CODE_TARGET_CONTEXT |
| 4943 | : RelocInfo::CODE_TARGET; |
| 4944 | frame_->CallCodeObject(ic, rmode, 0); |
| 4945 | } |
| 4946 | |
| 4947 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4948 | #ifdef DEBUG |
| 4949 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4950 | #endif |
| 4951 | |
| 4952 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4953 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4954 | #define __ ACCESS_MASM(masm) |
| 4955 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4956 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4957 | Handle<String> Reference::GetName() { |
| 4958 | ASSERT(type_ == NAMED); |
| 4959 | Property* property = expression_->AsProperty(); |
| 4960 | if (property == NULL) { |
| 4961 | // Global variable reference treated as a named property reference. |
| 4962 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4963 | ASSERT(proxy->AsVariable() != NULL); |
| 4964 | ASSERT(proxy->AsVariable()->is_global()); |
| 4965 | return proxy->name(); |
| 4966 | } else { |
| 4967 | Literal* raw_name = property->key()->AsLiteral(); |
| 4968 | ASSERT(raw_name != NULL); |
| 4969 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4970 | } |
| 4971 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4972 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4973 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4974 | void Reference::GetValue() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4975 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4976 | ASSERT(!is_illegal()); |
| 4977 | ASSERT(!cgen_->has_cc()); |
| 4978 | MacroAssembler* masm = cgen_->masm(); |
| 4979 | Property* property = expression_->AsProperty(); |
| 4980 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4981 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | switch (type_) { |
| 4985 | case SLOT: { |
| 4986 | Comment cmnt(masm, "[ Load from Slot"); |
| 4987 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4988 | ASSERT(slot != NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4989 | cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4990 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4991 | } |
| 4992 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4993 | case NAMED: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4994 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4995 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4996 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4997 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4998 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4999 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5000 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5001 | ASSERT(var == NULL || var->is_global()); |
| 5002 | RelocInfo::Mode rmode = (var == NULL) |
| 5003 | ? RelocInfo::CODE_TARGET |
| 5004 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5005 | frame->CallCodeObject(ic, rmode, 0); |
| 5006 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5007 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5010 | case KEYED: { |
| 5011 | // TODO(181): Implement inlined version of array indexing once |
| 5012 | // loop nesting is properly tracked on ARM. |
| 5013 | ASSERT(property != NULL); |
| 5014 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 5015 | ASSERT(var == NULL || var->is_global()); |
| 5016 | cgen_->EmitKeyedLoad(var != NULL); |
| 5017 | cgen_->frame()->EmitPush(r0); |
| 5018 | break; |
| 5019 | } |
| 5020 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5021 | default: |
| 5022 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5023 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5024 | |
| 5025 | if (!persist_after_get_) { |
| 5026 | cgen_->UnloadReference(this); |
| 5027 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5031 | void Reference::SetValue(InitState init_state) { |
| 5032 | ASSERT(!is_illegal()); |
| 5033 | ASSERT(!cgen_->has_cc()); |
| 5034 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5035 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5036 | Property* property = expression_->AsProperty(); |
| 5037 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5038 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5039 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5040 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5041 | switch (type_) { |
| 5042 | case SLOT: { |
| 5043 | Comment cmnt(masm, "[ Store to Slot"); |
| 5044 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5045 | cgen_->StoreToSlot(slot, init_state); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5046 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5047 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5050 | case NAMED: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5051 | VirtualFrame::SpilledScope scope(frame); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5052 | Comment cmnt(masm, "[ Store to named Property"); |
| 5053 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5054 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5055 | Handle<String> name(GetName()); |
| 5056 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5057 | frame->EmitPop(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5058 | frame->EmitPop(r1); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5059 | __ mov(r2, Operand(name)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5060 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5061 | frame->EmitPush(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5062 | set_unloaded(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5063 | break; |
| 5064 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5065 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5066 | case KEYED: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5067 | VirtualFrame::SpilledScope scope(frame); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5068 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 5069 | Property* property = expression_->AsProperty(); |
| 5070 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5071 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5072 | |
| 5073 | // Call IC code. |
| 5074 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5075 | frame->EmitPop(r0); // value |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5076 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5077 | frame->EmitPush(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5078 | cgen_->UnloadReference(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5079 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5080 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5081 | |
| 5082 | default: |
| 5083 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5084 | } |
| 5085 | } |
| 5086 | |
| 5087 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5088 | void FastNewClosureStub::Generate(MacroAssembler* masm) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5089 | // Create a new closure from the given function info in new |
| 5090 | // space. Set the context to the current context in cp. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5091 | Label gc; |
| 5092 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5093 | // Pop the function info from the stack. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5094 | __ pop(r3); |
| 5095 | |
| 5096 | // Attempt to allocate new JSFunction in new space. |
| 5097 | __ AllocateInNewSpace(JSFunction::kSize / kPointerSize, |
| 5098 | r0, |
| 5099 | r1, |
| 5100 | r2, |
| 5101 | &gc, |
| 5102 | TAG_OBJECT); |
| 5103 | |
| 5104 | // Compute the function map in the current global context and set that |
| 5105 | // as the map of the allocated object. |
| 5106 | __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 5107 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 5108 | __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX))); |
| 5109 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 5110 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5111 | // Initialize the rest of the function. We don't have to update the |
| 5112 | // write barrier because the allocated object is in new space. |
| 5113 | __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex); |
| 5114 | __ LoadRoot(r2, Heap::kTheHoleValueRootIndex); |
| 5115 | __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset)); |
| 5116 | __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 5117 | __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset)); |
| 5118 | __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 5119 | __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset)); |
| 5120 | __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5121 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5122 | // Return result. The argument function info has been popped already. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5123 | __ Ret(); |
| 5124 | |
| 5125 | // Create a new closure through the slower runtime call. |
| 5126 | __ bind(&gc); |
| 5127 | __ push(cp); |
| 5128 | __ push(r3); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5129 | __ TailCallRuntime(Runtime::kNewClosure, 2, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | |
| 5133 | void FastNewContextStub::Generate(MacroAssembler* masm) { |
| 5134 | // Try to allocate the context in new space. |
| 5135 | Label gc; |
| 5136 | int length = slots_ + Context::MIN_CONTEXT_SLOTS; |
| 5137 | |
| 5138 | // Attempt to allocate the context in new space. |
| 5139 | __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize), |
| 5140 | r0, |
| 5141 | r1, |
| 5142 | r2, |
| 5143 | &gc, |
| 5144 | TAG_OBJECT); |
| 5145 | |
| 5146 | // Load the function from the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5147 | __ ldr(r3, MemOperand(sp, 0)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5148 | |
| 5149 | // Setup the object header. |
| 5150 | __ LoadRoot(r2, Heap::kContextMapRootIndex); |
| 5151 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 5152 | __ mov(r2, Operand(length)); |
| 5153 | __ str(r2, FieldMemOperand(r0, Array::kLengthOffset)); |
| 5154 | |
| 5155 | // Setup the fixed slots. |
| 5156 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 5157 | __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 5158 | __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 5159 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
| 5160 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX))); |
| 5161 | |
| 5162 | // Copy the global object from the surrounding context. |
| 5163 | __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 5164 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 5165 | |
| 5166 | // Initialize the rest of the slots to undefined. |
| 5167 | __ LoadRoot(r1, Heap::kUndefinedValueRootIndex); |
| 5168 | for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) { |
| 5169 | __ str(r1, MemOperand(r0, Context::SlotOffset(i))); |
| 5170 | } |
| 5171 | |
| 5172 | // Remove the on-stack argument and return. |
| 5173 | __ mov(cp, r0); |
| 5174 | __ pop(); |
| 5175 | __ Ret(); |
| 5176 | |
| 5177 | // Need to collect. Call into runtime system. |
| 5178 | __ bind(&gc); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5179 | __ TailCallRuntime(Runtime::kNewContext, 1, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5183 | void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) { |
| 5184 | // Stack layout on entry: |
| 5185 | // |
| 5186 | // [sp]: constant elements. |
| 5187 | // [sp + kPointerSize]: literal index. |
| 5188 | // [sp + (2 * kPointerSize)]: literals array. |
| 5189 | |
| 5190 | // All sizes here are multiples of kPointerSize. |
| 5191 | int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0; |
| 5192 | int size = JSArray::kSize + elements_size; |
| 5193 | |
| 5194 | // Load boilerplate object into r3 and check if we need to create a |
| 5195 | // boilerplate. |
| 5196 | Label slow_case; |
| 5197 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 5198 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 5199 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 5200 | __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5201 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 5202 | __ cmp(r3, ip); |
| 5203 | __ b(eq, &slow_case); |
| 5204 | |
| 5205 | // Allocate both the JS array and the elements array in one big |
| 5206 | // allocation. This avoids multiple limit checks. |
| 5207 | __ AllocateInNewSpace(size / kPointerSize, |
| 5208 | r0, |
| 5209 | r1, |
| 5210 | r2, |
| 5211 | &slow_case, |
| 5212 | TAG_OBJECT); |
| 5213 | |
| 5214 | // Copy the JS array part. |
| 5215 | for (int i = 0; i < JSArray::kSize; i += kPointerSize) { |
| 5216 | if ((i != JSArray::kElementsOffset) || (length_ == 0)) { |
| 5217 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 5218 | __ str(r1, FieldMemOperand(r0, i)); |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | if (length_ > 0) { |
| 5223 | // Get hold of the elements array of the boilerplate and setup the |
| 5224 | // elements pointer in the resulting object. |
| 5225 | __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset)); |
| 5226 | __ add(r2, r0, Operand(JSArray::kSize)); |
| 5227 | __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 5228 | |
| 5229 | // Copy the elements array. |
| 5230 | for (int i = 0; i < elements_size; i += kPointerSize) { |
| 5231 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 5232 | __ str(r1, FieldMemOperand(r2, i)); |
| 5233 | } |
| 5234 | } |
| 5235 | |
| 5236 | // Return and remove the on-stack parameters. |
| 5237 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 5238 | __ Ret(); |
| 5239 | |
| 5240 | __ bind(&slow_case); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5241 | __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5245 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 5246 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 5247 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 5248 | // scratch register. Destroys the source register. No GC occurs during this |
| 5249 | // stub so you don't have to set up the frame. |
| 5250 | class ConvertToDoubleStub : public CodeStub { |
| 5251 | public: |
| 5252 | ConvertToDoubleStub(Register result_reg_1, |
| 5253 | Register result_reg_2, |
| 5254 | Register source_reg, |
| 5255 | Register scratch_reg) |
| 5256 | : result1_(result_reg_1), |
| 5257 | result2_(result_reg_2), |
| 5258 | source_(source_reg), |
| 5259 | zeros_(scratch_reg) { } |
| 5260 | |
| 5261 | private: |
| 5262 | Register result1_; |
| 5263 | Register result2_; |
| 5264 | Register source_; |
| 5265 | Register zeros_; |
| 5266 | |
| 5267 | // Minor key encoding in 16 bits. |
| 5268 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 5269 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 5270 | |
| 5271 | Major MajorKey() { return ConvertToDouble; } |
| 5272 | int MinorKey() { |
| 5273 | // Encode the parameters in a unique 16 bit value. |
| 5274 | return result1_.code() + |
| 5275 | (result2_.code() << 4) + |
| 5276 | (source_.code() << 8) + |
| 5277 | (zeros_.code() << 12); |
| 5278 | } |
| 5279 | |
| 5280 | void Generate(MacroAssembler* masm); |
| 5281 | |
| 5282 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 5283 | |
| 5284 | #ifdef DEBUG |
| 5285 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 5286 | #endif |
| 5287 | }; |
| 5288 | |
| 5289 | |
| 5290 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 5291 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 5292 | Register exponent = result1_; |
| 5293 | Register mantissa = result2_; |
| 5294 | #else |
| 5295 | Register exponent = result2_; |
| 5296 | Register mantissa = result1_; |
| 5297 | #endif |
| 5298 | Label not_special; |
| 5299 | // Convert from Smi to integer. |
| 5300 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 5301 | // Move sign bit from source to destination. This works because the sign bit |
| 5302 | // in the exponent word of the double has the same position and polarity as |
| 5303 | // the 2's complement sign bit in a Smi. |
| 5304 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 5305 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 5306 | // Subtract from 0 if source was negative. |
| 5307 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5308 | |
| 5309 | // We have -1, 0 or 1, which we treat specially. Register source_ contains |
| 5310 | // absolute value: it is either equal to 1 (special case of -1 and 1), |
| 5311 | // greater than 1 (not a special case) or less than 1 (special case of 0). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5312 | __ cmp(source_, Operand(1)); |
| 5313 | __ b(gt, ¬_special); |
| 5314 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5315 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 5316 | static const uint32_t exponent_word_for_1 = |
| 5317 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5318 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5319 | // 1, 0 and -1 all have 0 for the second word. |
| 5320 | __ mov(mantissa, Operand(0)); |
| 5321 | __ Ret(); |
| 5322 | |
| 5323 | __ bind(¬_special); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5324 | // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5325 | // Gets the wrong answer for 0, but we already checked for that case above. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5326 | __ CountLeadingZeros(source_, mantissa, zeros_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5327 | // Compute exponent and or it into the exponent register. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5328 | // We use mantissa as a scratch register here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5329 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 5330 | __ orr(exponent, |
| 5331 | exponent, |
| 5332 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 5333 | // Shift up the source chopping the top bit off. |
| 5334 | __ add(zeros_, zeros_, Operand(1)); |
| 5335 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 5336 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 5337 | // Compute lower part of fraction (last 12 bits). |
| 5338 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 5339 | // And the top (top 20 bits). |
| 5340 | __ orr(exponent, |
| 5341 | exponent, |
| 5342 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 5343 | __ Ret(); |
| 5344 | } |
| 5345 | |
| 5346 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5347 | // See comment for class. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5348 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5349 | Label max_negative_int; |
| 5350 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 5351 | // We test for the special value that has a different exponent. This test |
| 5352 | // has the neat side effect of setting the flags according to the sign. |
| 5353 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5354 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5355 | __ b(eq, &max_negative_int); |
| 5356 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 5357 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 5358 | uint32_t non_smi_exponent = |
| 5359 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5360 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 5361 | // Set the sign bit in scratch_ if the value was negative. |
| 5362 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 5363 | // Subtract from 0 if the value was negative. |
| 5364 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 5365 | // We should be masking the implict first digit of the mantissa away here, |
| 5366 | // but it just ends up combining harmlessly with the last digit of the |
| 5367 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 5368 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 5369 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 5370 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5371 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 5372 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 5373 | HeapNumber::kExponentOffset)); |
| 5374 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 5375 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 5376 | HeapNumber::kMantissaOffset)); |
| 5377 | __ Ret(); |
| 5378 | |
| 5379 | __ bind(&max_negative_int); |
| 5380 | // The max negative int32 is stored as a positive number in the mantissa of |
| 5381 | // a double because it uses a sign bit instead of using two's complement. |
| 5382 | // The actual mantissa bits stored are all 0 because the implicit most |
| 5383 | // significant 1 bit is not stored. |
| 5384 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 5385 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 5386 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 5387 | __ mov(ip, Operand(0)); |
| 5388 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 5389 | __ Ret(); |
| 5390 | } |
| 5391 | |
| 5392 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5393 | // Handle the case where the lhs and rhs are the same object. |
| 5394 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 5395 | // for "identity and not NaN". |
| 5396 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 5397 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5398 | Condition cc, |
| 5399 | bool never_nan_nan) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5400 | Label not_identical; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5401 | Label heap_number, return_equal; |
| 5402 | Register exp_mask_reg = r5; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5403 | __ cmp(r0, Operand(r1)); |
| 5404 | __ b(ne, ¬_identical); |
| 5405 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5406 | // The two objects are identical. If we know that one of them isn't NaN then |
| 5407 | // we now know they test equal. |
| 5408 | if (cc != eq || !never_nan_nan) { |
| 5409 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5410 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5411 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 5412 | // so we do the second best thing - test it ourselves. |
| 5413 | // They are both equal and they are not both Smis so both of them are not |
| 5414 | // Smis. If it's not a heap number, then return equal. |
| 5415 | if (cc == lt || cc == gt) { |
| 5416 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5417 | __ b(ge, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5418 | } else { |
| 5419 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5420 | __ b(eq, &heap_number); |
| 5421 | // Comparing JS objects with <=, >= is complicated. |
| 5422 | if (cc != eq) { |
| 5423 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 5424 | __ b(ge, slow); |
| 5425 | // Normally here we fall through to return_equal, but undefined is |
| 5426 | // special: (undefined == undefined) == true, but |
| 5427 | // (undefined <= undefined) == false! See ECMAScript 11.8.5. |
| 5428 | if (cc == le || cc == ge) { |
| 5429 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 5430 | __ b(ne, &return_equal); |
| 5431 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 5432 | __ cmp(r0, Operand(r2)); |
| 5433 | __ b(ne, &return_equal); |
| 5434 | if (cc == le) { |
| 5435 | // undefined <= undefined should fail. |
| 5436 | __ mov(r0, Operand(GREATER)); |
| 5437 | } else { |
| 5438 | // undefined >= undefined should fail. |
| 5439 | __ mov(r0, Operand(LESS)); |
| 5440 | } |
| 5441 | __ mov(pc, Operand(lr)); // Return. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5442 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5443 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5444 | } |
| 5445 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5446 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5447 | __ bind(&return_equal); |
| 5448 | if (cc == lt) { |
| 5449 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 5450 | } else if (cc == gt) { |
| 5451 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 5452 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5453 | __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5454 | } |
| 5455 | __ mov(pc, Operand(lr)); // Return. |
| 5456 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5457 | if (cc != eq || !never_nan_nan) { |
| 5458 | // For less and greater we don't have to check for NaN since the result of |
| 5459 | // x < x is false regardless. For the others here is some code to check |
| 5460 | // for NaN. |
| 5461 | if (cc != lt && cc != gt) { |
| 5462 | __ bind(&heap_number); |
| 5463 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 5464 | // not NaN. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5465 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5466 | // The representation of NaN values has all exponent bits (52..62) set, |
| 5467 | // and not all mantissa bits (0..51) clear. |
| 5468 | // Read top bits of double representation (second word of value). |
| 5469 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5470 | // Test that exponent bits are all set. |
| 5471 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 5472 | __ cmp(r3, Operand(exp_mask_reg)); |
| 5473 | __ b(ne, &return_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5474 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5475 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 5476 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 5477 | // Or with all low-bits of mantissa. |
| 5478 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 5479 | __ orr(r0, r3, Operand(r2), SetCC); |
| 5480 | // 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] | 5481 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if |
| 5482 | // not (it's a NaN). For <= and >= we need to load r0 with the failing |
| 5483 | // value if it's a NaN. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5484 | if (cc != eq) { |
| 5485 | // All-zero means Infinity means equal. |
| 5486 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 5487 | if (cc == le) { |
| 5488 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 5489 | } else { |
| 5490 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 5491 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5492 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5493 | __ mov(pc, Operand(lr)); // Return. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5494 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5495 | // No fall through here. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5496 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5497 | |
| 5498 | __ bind(¬_identical); |
| 5499 | } |
| 5500 | |
| 5501 | |
| 5502 | // See comment at call site. |
| 5503 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5504 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5505 | Label* slow, |
| 5506 | bool strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5507 | Label rhs_is_smi; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5508 | __ tst(r0, Operand(kSmiTagMask)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5509 | __ b(eq, &rhs_is_smi); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5510 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5511 | // 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] | 5512 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5513 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5514 | // 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] | 5515 | // succeed. Return non-equal (r0 is already not zero) |
| 5516 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 5517 | } else { |
| 5518 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 5519 | // the runtime. |
| 5520 | __ b(ne, slow); |
| 5521 | } |
| 5522 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5523 | // Lhs (r1) is a smi, rhs (r0) is a number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5524 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5525 | // Convert lhs to a double in d7 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5526 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5527 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 5528 | __ vmov(s15, r7); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5529 | __ vcvt_f64_s32(d7, s15); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5530 | // Load the double from rhs, tagged HeapNumber r0, to d6. |
| 5531 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5532 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5533 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5534 | __ push(lr); |
| 5535 | // Convert lhs to a double in r2, r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5536 | __ mov(r7, Operand(r1)); |
| 5537 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5538 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5539 | // Load rhs to a double in r0, r1. |
| 5540 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5541 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5542 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5545 | // 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] | 5546 | // since it's a smi. |
| 5547 | __ jmp(lhs_not_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5548 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5549 | __ bind(&rhs_is_smi); |
| 5550 | // 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] | 5551 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5552 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5553 | // 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] | 5554 | // succeed. Return non-equal. |
| 5555 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 5556 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 5557 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5558 | // 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] | 5559 | // the runtime. |
| 5560 | __ b(ne, slow); |
| 5561 | } |
| 5562 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5563 | // Rhs (r0) is a smi, lhs (r1) is a heap number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5564 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5565 | // Convert rhs to a double in d6 . |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5566 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5567 | // Load the double from lhs, tagged HeapNumber r1, to d7. |
| 5568 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5569 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5570 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 5571 | __ vmov(s13, r7); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 5572 | __ vcvt_f64_s32(d6, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5573 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5574 | __ push(lr); |
| 5575 | // Load lhs to a double in r2, r3. |
| 5576 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5577 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5578 | // Convert rhs to a double in r0, r1. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5579 | __ mov(r7, Operand(r0)); |
| 5580 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5581 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5582 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5583 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5584 | // Fall through to both_loaded_as_doubles. |
| 5585 | } |
| 5586 | |
| 5587 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5588 | void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5589 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5590 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5591 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5592 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5593 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5594 | Label one_is_nan, neither_is_nan; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5595 | Label lhs_not_nan_exp_mask_is_loaded; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5596 | |
| 5597 | Register exp_mask_reg = r5; |
| 5598 | |
| 5599 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5600 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 5601 | __ cmp(r4, Operand(exp_mask_reg)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5602 | __ b(ne, &lhs_not_nan_exp_mask_is_loaded); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5603 | __ mov(r4, |
| 5604 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5605 | SetCC); |
| 5606 | __ b(ne, &one_is_nan); |
| 5607 | __ cmp(lhs_mantissa, Operand(0)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5608 | __ b(ne, &one_is_nan); |
| 5609 | |
| 5610 | __ bind(lhs_not_nan); |
| 5611 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 5612 | __ bind(&lhs_not_nan_exp_mask_is_loaded); |
| 5613 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 5614 | __ cmp(r4, Operand(exp_mask_reg)); |
| 5615 | __ b(ne, &neither_is_nan); |
| 5616 | __ mov(r4, |
| 5617 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 5618 | SetCC); |
| 5619 | __ b(ne, &one_is_nan); |
| 5620 | __ cmp(rhs_mantissa, Operand(0)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5621 | __ b(eq, &neither_is_nan); |
| 5622 | |
| 5623 | __ bind(&one_is_nan); |
| 5624 | // NaN comparisons always fail. |
| 5625 | // Load whatever we need in r0 to make the comparison fail. |
| 5626 | if (cc == lt || cc == le) { |
| 5627 | __ mov(r0, Operand(GREATER)); |
| 5628 | } else { |
| 5629 | __ mov(r0, Operand(LESS)); |
| 5630 | } |
| 5631 | __ mov(pc, Operand(lr)); // Return. |
| 5632 | |
| 5633 | __ bind(&neither_is_nan); |
| 5634 | } |
| 5635 | |
| 5636 | |
| 5637 | // See comment at call site. |
| 5638 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 5639 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5640 | Register rhs_exponent = exp_first ? r0 : r1; |
| 5641 | Register lhs_exponent = exp_first ? r2 : r3; |
| 5642 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 5643 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5644 | |
| 5645 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 5646 | if (cc == eq) { |
| 5647 | // Doubles are not equal unless they have the same bit pattern. |
| 5648 | // Exception: 0 and -0. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5649 | __ cmp(rhs_mantissa, Operand(lhs_mantissa)); |
| 5650 | __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5651 | // Return non-zero if the numbers are unequal. |
| 5652 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 5653 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5654 | __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5655 | // If exponents are equal then return 0. |
| 5656 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 5657 | |
| 5658 | // Exponents are unequal. The only way we can return that the numbers |
| 5659 | // are equal is if one is -0 and the other is 0. We already dealt |
| 5660 | // with the case where both are -0 or both are 0. |
| 5661 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 5662 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 5663 | // equal. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5664 | __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5665 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 5666 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 5667 | // Now they are equal if and only if the lhs exponent is zero in its |
| 5668 | // low 31 bits. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5669 | __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5670 | __ mov(pc, Operand(lr)); |
| 5671 | } else { |
| 5672 | // Call a native function to do a comparison between two non-NaNs. |
| 5673 | // Call C routine that may not cause GC or other trouble. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 5674 | __ push(lr); |
| 5675 | __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments. |
| 5676 | __ CallCFunction(ExternalReference::compare_doubles(), 4); |
| 5677 | __ pop(pc); // Return. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5678 | } |
| 5679 | } |
| 5680 | |
| 5681 | |
| 5682 | // See comment at call site. |
| 5683 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 5684 | // If either operand is a JSObject or an oddball value, then they are |
| 5685 | // not equal since their pointers are different. |
| 5686 | // There is no test for undetectability in strict equality. |
| 5687 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 5688 | Label first_non_object; |
| 5689 | // Get the type of the first operand into r2 and compare it with |
| 5690 | // FIRST_JS_OBJECT_TYPE. |
| 5691 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 5692 | __ b(lt, &first_non_object); |
| 5693 | |
| 5694 | // Return non-zero (r0 is not zero) |
| 5695 | Label return_not_equal; |
| 5696 | __ bind(&return_not_equal); |
| 5697 | __ mov(pc, Operand(lr)); // Return. |
| 5698 | |
| 5699 | __ bind(&first_non_object); |
| 5700 | // Check for oddballs: true, false, null, undefined. |
| 5701 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 5702 | __ b(eq, &return_not_equal); |
| 5703 | |
| 5704 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 5705 | __ b(ge, &return_not_equal); |
| 5706 | |
| 5707 | // Check for oddballs: true, false, null, undefined. |
| 5708 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 5709 | __ b(eq, &return_not_equal); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5710 | |
| 5711 | // Now that we have the types we might as well check for symbol-symbol. |
| 5712 | // Ensure that no non-strings have the symbol bit set. |
| 5713 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5714 | ASSERT(kSymbolTag != 0); |
| 5715 | __ and_(r2, r2, Operand(r3)); |
| 5716 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5717 | __ b(ne, &return_not_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | |
| 5721 | // See comment at call site. |
| 5722 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 5723 | Label* both_loaded_as_doubles, |
| 5724 | Label* not_heap_numbers, |
| 5725 | Label* slow) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5726 | __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5727 | __ b(ne, not_heap_numbers); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5728 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5729 | __ cmp(r2, r3); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5730 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 5731 | |
| 5732 | // Both are heap numbers. Load them up then jump to the code we have |
| 5733 | // for that. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5734 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5735 | CpuFeatures::Scope scope(VFP3); |
| 5736 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 5737 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 5738 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 5739 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 5740 | } else { |
| 5741 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5742 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5743 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5744 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5745 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5746 | __ jmp(both_loaded_as_doubles); |
| 5747 | } |
| 5748 | |
| 5749 | |
| 5750 | // Fast negative check for symbol-to-symbol equality. |
| 5751 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 5752 | // r2 is object type of r0. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5753 | // Ensure that no non-strings have the symbol bit set. |
| 5754 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5755 | ASSERT(kSymbolTag != 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5756 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5757 | __ b(eq, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5758 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5759 | __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5760 | __ tst(r3, Operand(kIsSymbolMask)); |
| 5761 | __ b(eq, slow); |
| 5762 | |
| 5763 | // Both are symbols. We already checked they weren't the same pointer |
| 5764 | // so they are not equal. |
| 5765 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 5766 | __ mov(pc, Operand(lr)); // Return. |
| 5767 | } |
| 5768 | |
| 5769 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5770 | void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm, |
| 5771 | Register object, |
| 5772 | Register result, |
| 5773 | Register scratch1, |
| 5774 | Register scratch2, |
| 5775 | bool object_is_smi, |
| 5776 | Label* not_found) { |
| 5777 | // Currently only lookup for smis. Check for smi if object is not known to be |
| 5778 | // a smi. |
| 5779 | if (!object_is_smi) { |
| 5780 | ASSERT(kSmiTag == 0); |
| 5781 | __ tst(object, Operand(kSmiTagMask)); |
| 5782 | __ b(ne, not_found); |
| 5783 | } |
| 5784 | |
| 5785 | // Use of registers. Register result is used as a temporary. |
| 5786 | Register number_string_cache = result; |
| 5787 | Register mask = scratch1; |
| 5788 | Register scratch = scratch2; |
| 5789 | |
| 5790 | // Load the number string cache. |
| 5791 | __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex); |
| 5792 | |
| 5793 | // Make the hash mask from the length of the number string cache. It |
| 5794 | // contains two elements (number and string) for each cache entry. |
| 5795 | __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset)); |
| 5796 | // Divide length by two (length is not a smi). |
| 5797 | __ mov(mask, Operand(mask, ASR, 1)); |
| 5798 | __ sub(mask, mask, Operand(1)); // Make mask. |
| 5799 | |
| 5800 | // Calculate the entry in the number string cache. The hash value in the |
| 5801 | // number string cache for smis is just the smi value. |
| 5802 | __ and_(scratch, mask, Operand(object, ASR, 1)); |
| 5803 | |
| 5804 | // Calculate address of entry in string cache: each entry consists |
| 5805 | // of two pointer sized fields. |
| 5806 | __ add(scratch, |
| 5807 | number_string_cache, |
| 5808 | Operand(scratch, LSL, kPointerSizeLog2 + 1)); |
| 5809 | |
| 5810 | // Check if the entry is the smi we are looking for. |
| 5811 | Register object1 = scratch1; |
| 5812 | __ ldr(object1, FieldMemOperand(scratch, FixedArray::kHeaderSize)); |
| 5813 | __ cmp(object, object1); |
| 5814 | __ b(ne, not_found); |
| 5815 | |
| 5816 | // Get the result from the cache. |
| 5817 | __ ldr(result, |
| 5818 | FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize)); |
| 5819 | |
| 5820 | __ IncrementCounter(&Counters::number_to_string_native, |
| 5821 | 1, |
| 5822 | scratch1, |
| 5823 | scratch2); |
| 5824 | } |
| 5825 | |
| 5826 | |
| 5827 | void NumberToStringStub::Generate(MacroAssembler* masm) { |
| 5828 | Label runtime; |
| 5829 | |
| 5830 | __ ldr(r1, MemOperand(sp, 0)); |
| 5831 | |
| 5832 | // Generate code to lookup number in the number string cache. |
| 5833 | GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, false, &runtime); |
| 5834 | __ add(sp, sp, Operand(1 * kPointerSize)); |
| 5835 | __ Ret(); |
| 5836 | |
| 5837 | __ bind(&runtime); |
| 5838 | // Handle number to string in the runtime system if not found in the cache. |
| 5839 | __ TailCallRuntime(Runtime::kNumberToString, 1, 1); |
| 5840 | } |
| 5841 | |
| 5842 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5843 | // On entry r0 (rhs) and r1 (lhs) are the values to be compared. |
| 5844 | // On exit r0 is 0, positive or negative to indicate the result of |
| 5845 | // the comparison. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5846 | void CompareStub::Generate(MacroAssembler* masm) { |
| 5847 | Label slow; // Call builtin. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5848 | Label not_smis, both_loaded_as_doubles, lhs_not_nan; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5849 | |
| 5850 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 5851 | // it is certain that at least one operand isn't a smi. |
| 5852 | |
| 5853 | // Handle the case where the objects are identical. Either returns the answer |
| 5854 | // 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] | 5855 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5856 | |
| 5857 | // If either is a Smi (we know that not both are), then they can only |
| 5858 | // be strictly equal if the other is a HeapNumber. |
| 5859 | ASSERT_EQ(0, kSmiTag); |
| 5860 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 5861 | __ and_(r2, r0, Operand(r1)); |
| 5862 | __ tst(r2, Operand(kSmiTagMask)); |
| 5863 | __ b(ne, ¬_smis); |
| 5864 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 5865 | // 1) Return the answer. |
| 5866 | // 2) Go to slow. |
| 5867 | // 3) Fall through to both_loaded_as_doubles. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5868 | // 4) Jump to lhs_not_nan. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5869 | // 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] | 5870 | // comparison. If VFP3 is supported the double values of the numbers have |
| 5871 | // been loaded into d7 and d6. Otherwise, the double values have been loaded |
| 5872 | // into r0, r1, r2, and r3. |
| 5873 | EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5874 | |
| 5875 | __ bind(&both_loaded_as_doubles); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5876 | // The arguments have been converted to doubles and stored in d6 and d7, if |
| 5877 | // VFP3 is supported, or in r0, r1, r2, and r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5878 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5879 | __ bind(&lhs_not_nan); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5880 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5881 | Label no_nan; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5882 | // ARMv7 VFP3 instructions to implement double precision comparison. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5883 | __ vcmp(d7, d6); |
| 5884 | __ vmrs(pc); // Move vector status bits to normal status bits. |
| 5885 | Label nan; |
| 5886 | __ b(vs, &nan); |
| 5887 | __ mov(r0, Operand(EQUAL), LeaveCC, eq); |
| 5888 | __ mov(r0, Operand(LESS), LeaveCC, lt); |
| 5889 | __ mov(r0, Operand(GREATER), LeaveCC, gt); |
| 5890 | __ mov(pc, Operand(lr)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5891 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5892 | __ bind(&nan); |
| 5893 | // If one of the sides was a NaN then the v flag is set. Load r0 with |
| 5894 | // whatever it takes to make the comparison fail, since comparisons with NaN |
| 5895 | // always fail. |
| 5896 | if (cc_ == lt || cc_ == le) { |
| 5897 | __ mov(r0, Operand(GREATER)); |
| 5898 | } else { |
| 5899 | __ mov(r0, Operand(LESS)); |
| 5900 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5901 | __ mov(pc, Operand(lr)); |
| 5902 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5903 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 5904 | // fall through if neither is a NaN. Also binds lhs_not_nan. |
| 5905 | EmitNanCheck(masm, &lhs_not_nan, cc_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5906 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 5907 | // answer. Never falls through. |
| 5908 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 5909 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5910 | |
| 5911 | __ bind(¬_smis); |
| 5912 | // At this point we know we are dealing with two different objects, |
| 5913 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 5914 | if (strict_) { |
| 5915 | // This returns non-equal for some object types, or falls through if it |
| 5916 | // was not lucky. |
| 5917 | EmitStrictTwoHeapObjectCompare(masm); |
| 5918 | } |
| 5919 | |
| 5920 | Label check_for_symbols; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5921 | Label flat_string_check; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5922 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 5923 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 5924 | // 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] | 5925 | // 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] | 5926 | EmitCheckForTwoHeapNumbers(masm, |
| 5927 | &both_loaded_as_doubles, |
| 5928 | &check_for_symbols, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5929 | &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5930 | |
| 5931 | __ bind(&check_for_symbols); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5932 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 5933 | // symbols. |
| 5934 | if (cc_ == eq && !strict_) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5935 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5936 | // of r0 on entry. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5937 | EmitCheckForSymbols(masm, &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5938 | } |
| 5939 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5940 | // Check for both being sequential ASCII strings, and inline if that is the |
| 5941 | // case. |
| 5942 | __ bind(&flat_string_check); |
| 5943 | |
| 5944 | __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow); |
| 5945 | |
| 5946 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 5947 | StringCompareStub::GenerateCompareFlatAsciiStrings(masm, |
| 5948 | r1, |
| 5949 | r0, |
| 5950 | r2, |
| 5951 | r3, |
| 5952 | r4, |
| 5953 | r5); |
| 5954 | // Never falls through to here. |
| 5955 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5956 | __ bind(&slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5957 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5958 | __ push(r1); |
| 5959 | __ push(r0); |
| 5960 | // Figure out which native to call and setup the arguments. |
| 5961 | Builtins::JavaScript native; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5962 | if (cc_ == eq) { |
| 5963 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 5964 | } else { |
| 5965 | native = Builtins::COMPARE; |
| 5966 | int ncr; // NaN compare result |
| 5967 | if (cc_ == lt || cc_ == le) { |
| 5968 | ncr = GREATER; |
| 5969 | } else { |
| 5970 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 5971 | ncr = LESS; |
| 5972 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5973 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 5974 | __ push(r0); |
| 5975 | } |
| 5976 | |
| 5977 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 5978 | // tagged as a small integer. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5979 | __ InvokeBuiltin(native, JUMP_JS); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5980 | } |
| 5981 | |
| 5982 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5983 | // We fall into this code if the operands were Smis, but the result was |
| 5984 | // 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] | 5985 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 5986 | // to call the C-implemented binary fp operation routines we need to end up |
| 5987 | // with the double precision floating point operands in r0 and r1 (for the |
| 5988 | // value in r1) and r2 and r3 (for the value in r0). |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5989 | void GenericBinaryOpStub::HandleBinaryOpSlowCases( |
| 5990 | MacroAssembler* masm, |
| 5991 | Label* not_smi, |
| 5992 | Register lhs, |
| 5993 | Register rhs, |
| 5994 | const Builtins::JavaScript& builtin) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5995 | Label slow, slow_pop_2_first, do_the_call; |
| 5996 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5997 | bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5998 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 5999 | ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6000 | |
| 6001 | if (ShouldGenerateSmiCode()) { |
| 6002 | // Smi-smi case (overflow). |
| 6003 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 6004 | // The new heap number is in r5. r6 and r7 are scratch. |
| 6005 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
| 6006 | |
| 6007 | // If we have floating point hardware, inline ADD, SUB, MUL, and DIV, |
| 6008 | // using registers d7 and d6 for the double values. |
| 6009 | if (use_fp_registers) { |
| 6010 | CpuFeatures::Scope scope(VFP3); |
| 6011 | __ mov(r7, Operand(rhs, ASR, kSmiTagSize)); |
| 6012 | __ vmov(s15, r7); |
| 6013 | __ vcvt_f64_s32(d7, s15); |
| 6014 | __ mov(r7, Operand(lhs, ASR, kSmiTagSize)); |
| 6015 | __ vmov(s13, r7); |
| 6016 | __ vcvt_f64_s32(d6, s13); |
| 6017 | } else { |
| 6018 | // Write Smi from rhs to r3 and r2 in double format. r6 is scratch. |
| 6019 | __ mov(r7, Operand(rhs)); |
| 6020 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 6021 | __ push(lr); |
| 6022 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 6023 | // Write Smi from lhs to r1 and r0 in double format. r6 is scratch. |
| 6024 | __ mov(r7, Operand(lhs)); |
| 6025 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 6026 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 6027 | __ pop(lr); |
| 6028 | } |
| 6029 | __ jmp(&do_the_call); // Tail call. No return. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6030 | } |
| 6031 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6032 | // We branch here if at least one of r0 and r1 is not a Smi. |
| 6033 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6034 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6035 | if (lhs.is(r0)) { |
| 6036 | __ Swap(r0, r1, ip); |
| 6037 | } |
| 6038 | |
| 6039 | if (ShouldGenerateFPCode()) { |
| 6040 | if (runtime_operands_type_ == BinaryOpIC::DEFAULT) { |
| 6041 | switch (op_) { |
| 6042 | case Token::ADD: |
| 6043 | case Token::SUB: |
| 6044 | case Token::MUL: |
| 6045 | case Token::DIV: |
| 6046 | GenerateTypeTransition(masm); |
| 6047 | break; |
| 6048 | |
| 6049 | default: |
| 6050 | break; |
| 6051 | } |
| 6052 | } |
| 6053 | |
| 6054 | if (mode_ == NO_OVERWRITE) { |
| 6055 | // In the case where there is no chance of an overwritable float we may as |
| 6056 | // well do the allocation immediately while r0 and r1 are untouched. |
| 6057 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
| 6058 | } |
| 6059 | |
| 6060 | // Move r0 to a double in r2-r3. |
| 6061 | __ tst(r0, Operand(kSmiTagMask)); |
| 6062 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 6063 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 6064 | __ b(ne, &slow); |
| 6065 | if (mode_ == OVERWRITE_RIGHT) { |
| 6066 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 6067 | } |
| 6068 | if (use_fp_registers) { |
| 6069 | CpuFeatures::Scope scope(VFP3); |
| 6070 | // Load the double from tagged HeapNumber r0 to d7. |
| 6071 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 6072 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 6073 | } else { |
| 6074 | // Calling convention says that second double is in r2 and r3. |
| 6075 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 6076 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 6077 | } |
| 6078 | __ jmp(&finished_loading_r0); |
| 6079 | __ bind(&r0_is_smi); |
| 6080 | if (mode_ == OVERWRITE_RIGHT) { |
| 6081 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 6082 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
| 6083 | } |
| 6084 | |
| 6085 | if (use_fp_registers) { |
| 6086 | CpuFeatures::Scope scope(VFP3); |
| 6087 | // Convert smi in r0 to double in d7. |
| 6088 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 6089 | __ vmov(s15, r7); |
| 6090 | __ vcvt_f64_s32(d7, s15); |
| 6091 | } else { |
| 6092 | // Write Smi from r0 to r3 and r2 in double format. |
| 6093 | __ mov(r7, Operand(r0)); |
| 6094 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 6095 | __ push(lr); |
| 6096 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 6097 | __ pop(lr); |
| 6098 | } |
| 6099 | |
| 6100 | __ bind(&finished_loading_r0); |
| 6101 | |
| 6102 | // Move r1 to a double in r0-r1. |
| 6103 | __ tst(r1, Operand(kSmiTagMask)); |
| 6104 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 6105 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 6106 | __ b(ne, &slow); |
| 6107 | if (mode_ == OVERWRITE_LEFT) { |
| 6108 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
| 6109 | } |
| 6110 | if (use_fp_registers) { |
| 6111 | CpuFeatures::Scope scope(VFP3); |
| 6112 | // Load the double from tagged HeapNumber r1 to d6. |
| 6113 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 6114 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 6115 | } else { |
| 6116 | // Calling convention says that first double is in r0 and r1. |
| 6117 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 6118 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 6119 | } |
| 6120 | __ jmp(&finished_loading_r1); |
| 6121 | __ bind(&r1_is_smi); |
| 6122 | if (mode_ == OVERWRITE_LEFT) { |
| 6123 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 6124 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
| 6125 | } |
| 6126 | |
| 6127 | if (use_fp_registers) { |
| 6128 | CpuFeatures::Scope scope(VFP3); |
| 6129 | // Convert smi in r1 to double in d6. |
| 6130 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 6131 | __ vmov(s13, r7); |
| 6132 | __ vcvt_f64_s32(d6, s13); |
| 6133 | } else { |
| 6134 | // Write Smi from r1 to r1 and r0 in double format. |
| 6135 | __ mov(r7, Operand(r1)); |
| 6136 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 6137 | __ push(lr); |
| 6138 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 6139 | __ pop(lr); |
| 6140 | } |
| 6141 | |
| 6142 | __ bind(&finished_loading_r1); |
| 6143 | |
| 6144 | __ bind(&do_the_call); |
| 6145 | // If we are inlining the operation using VFP3 instructions for |
| 6146 | // add, subtract, multiply, or divide, the arguments are in d6 and d7. |
| 6147 | if (use_fp_registers) { |
| 6148 | CpuFeatures::Scope scope(VFP3); |
| 6149 | // ARMv7 VFP3 instructions to implement |
| 6150 | // double precision, add, subtract, multiply, divide. |
| 6151 | |
| 6152 | if (Token::MUL == op_) { |
| 6153 | __ vmul(d5, d6, d7); |
| 6154 | } else if (Token::DIV == op_) { |
| 6155 | __ vdiv(d5, d6, d7); |
| 6156 | } else if (Token::ADD == op_) { |
| 6157 | __ vadd(d5, d6, d7); |
| 6158 | } else if (Token::SUB == op_) { |
| 6159 | __ vsub(d5, d6, d7); |
| 6160 | } else { |
| 6161 | UNREACHABLE(); |
| 6162 | } |
| 6163 | __ sub(r0, r5, Operand(kHeapObjectTag)); |
| 6164 | __ vstr(d5, r0, HeapNumber::kValueOffset); |
| 6165 | __ add(r0, r0, Operand(kHeapObjectTag)); |
| 6166 | __ mov(pc, lr); |
| 6167 | } else { |
| 6168 | // If we did not inline the operation, then the arguments are in: |
| 6169 | // r0: Left value (least significant part of mantissa). |
| 6170 | // r1: Left value (sign, exponent, top of mantissa). |
| 6171 | // r2: Right value (least significant part of mantissa). |
| 6172 | // r3: Right value (sign, exponent, top of mantissa). |
| 6173 | // r5: Address of heap number for result. |
| 6174 | |
| 6175 | __ push(lr); // For later. |
| 6176 | __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments. |
| 6177 | // Call C routine that may not cause GC or other trouble. r5 is callee |
| 6178 | // save. |
| 6179 | __ CallCFunction(ExternalReference::double_fp_operation(op_), 4); |
| 6180 | // Store answer in the overwritable heap number. |
| 6181 | #if !defined(USE_ARM_EABI) |
| 6182 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 6183 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 6184 | // substract the tag from r5. |
| 6185 | __ sub(r4, r5, Operand(kHeapObjectTag)); |
| 6186 | __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset)); |
| 6187 | #else |
| 6188 | // Double returned in registers 0 and 1. |
| 6189 | __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset)); |
| 6190 | __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4)); |
| 6191 | #endif |
| 6192 | __ mov(r0, Operand(r5)); |
| 6193 | // And we are done. |
| 6194 | __ pop(pc); |
| 6195 | } |
| 6196 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6197 | // We jump to here if something goes wrong (one param is not a number of any |
| 6198 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6199 | __ bind(&slow); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6200 | |
| 6201 | // Push arguments to the stack |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6202 | __ push(r1); |
| 6203 | __ push(r0); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6204 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6205 | if (Token::ADD == op_) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6206 | // Test for string arguments before calling runtime. |
| 6207 | // r1 : first argument |
| 6208 | // r0 : second argument |
| 6209 | // sp[0] : second argument |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6210 | // sp[4] : first argument |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6211 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6212 | Label not_strings, not_string1, string1, string1_smi2; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6213 | __ tst(r1, Operand(kSmiTagMask)); |
| 6214 | __ b(eq, ¬_string1); |
| 6215 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 6216 | __ b(ge, ¬_string1); |
| 6217 | |
| 6218 | // First argument is a a string, test second. |
| 6219 | __ tst(r0, Operand(kSmiTagMask)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6220 | __ b(eq, &string1_smi2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6221 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 6222 | __ b(ge, &string1); |
| 6223 | |
| 6224 | // First and second argument are strings. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6225 | StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB); |
| 6226 | __ TailCallStub(&string_add_stub); |
| 6227 | |
| 6228 | __ bind(&string1_smi2); |
| 6229 | // First argument is a string, second is a smi. Try to lookup the number |
| 6230 | // string for the smi in the number string cache. |
| 6231 | NumberToStringStub::GenerateLookupNumberStringCache( |
| 6232 | masm, r0, r2, r4, r5, true, &string1); |
| 6233 | |
| 6234 | // Replace second argument on stack and tailcall string add stub to make |
| 6235 | // the result. |
| 6236 | __ str(r2, MemOperand(sp, 0)); |
| 6237 | __ TailCallStub(&string_add_stub); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6238 | |
| 6239 | // Only first argument is a string. |
| 6240 | __ bind(&string1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6241 | __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS); |
| 6242 | |
| 6243 | // First argument was not a string, test second. |
| 6244 | __ bind(¬_string1); |
| 6245 | __ tst(r0, Operand(kSmiTagMask)); |
| 6246 | __ b(eq, ¬_strings); |
| 6247 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 6248 | __ b(ge, ¬_strings); |
| 6249 | |
| 6250 | // Only second argument is a string. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6251 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 6252 | |
| 6253 | __ bind(¬_strings); |
| 6254 | } |
| 6255 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6256 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6257 | } |
| 6258 | |
| 6259 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6260 | // 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] | 6261 | // 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] | 6262 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 6263 | // 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] | 6264 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 6265 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6266 | static void GetInt32(MacroAssembler* masm, |
| 6267 | Register source, |
| 6268 | Register dest, |
| 6269 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6270 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6271 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6272 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6273 | // Get exponent word. |
| 6274 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 6275 | // Get exponent alone in scratch2. |
| 6276 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6277 | // Load dest with zero. We use this either for the final shift or |
| 6278 | // for the answer. |
| 6279 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6280 | // 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] | 6281 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 6282 | // the exponent that we are fastest at and also the highest exponent we can |
| 6283 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6284 | const uint32_t non_smi_exponent = |
| 6285 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 6286 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6287 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 6288 | __ b(eq, &right_exponent); |
| 6289 | // If the exponent is higher than that then go to slow case. This catches |
| 6290 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 6291 | __ b(gt, slow); |
| 6292 | |
| 6293 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 6294 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 6295 | // it rounds to zero. |
| 6296 | const uint32_t zero_exponent = |
| 6297 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 6298 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 6299 | // Dest already has a Smi zero. |
| 6300 | __ b(lt, &done); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6301 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 6302 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 6303 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 6304 | // We now have the exponent in dest. Subtract from 30 to get |
| 6305 | // how much to shift down. |
| 6306 | __ rsb(dest, dest, Operand(30)); |
| 6307 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6308 | __ bind(&right_exponent); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6309 | if (CpuFeatures::IsSupported(VFP3)) { |
| 6310 | CpuFeatures::Scope scope(VFP3); |
| 6311 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 6312 | // conversion using round to zero. |
| 6313 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 6314 | __ vmov(d7, scratch2, scratch); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6315 | __ vcvt_s32_f64(s15, d7); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 6316 | __ vmov(dest, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6317 | } else { |
| 6318 | // Get the top bits of the mantissa. |
| 6319 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 6320 | // Put back the implicit 1. |
| 6321 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 6322 | // Shift up the mantissa bits to take up the space the exponent used to |
| 6323 | // take. We just orred in the implicit bit so that took care of one and |
| 6324 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 6325 | // distance. |
| 6326 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 6327 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 6328 | // Put sign in zero flag. |
| 6329 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 6330 | // Get the second half of the double. For some exponents we don't |
| 6331 | // actually need this because the bits get shifted out again, but |
| 6332 | // it's probably slower to test than just to do it. |
| 6333 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 6334 | // Shift down 22 bits to get the last 10 bits. |
| 6335 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 6336 | // Move down according to the exponent. |
| 6337 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 6338 | // Fix sign if sign bit was set. |
| 6339 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 6340 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6341 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6344 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 6345 | // whether both inputs are either Smis or at least heap numbers that can be |
| 6346 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 6347 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 6348 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 6349 | // write the answer into (either by allocating or by overwriting). |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6350 | // On entry the operands are in lhs and rhs. On exit the answer is in r0. |
| 6351 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm, |
| 6352 | Register lhs, |
| 6353 | Register rhs) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6354 | Label slow, result_not_a_smi; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6355 | Label rhs_is_smi, lhs_is_smi; |
| 6356 | Label done_checking_rhs, done_checking_lhs; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6357 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6358 | __ tst(lhs, Operand(kSmiTagMask)); |
| 6359 | __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number. |
| 6360 | __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6361 | __ b(ne, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6362 | GetInt32(masm, lhs, r3, r5, r4, &slow); |
| 6363 | __ jmp(&done_checking_lhs); |
| 6364 | __ bind(&lhs_is_smi); |
| 6365 | __ mov(r3, Operand(lhs, ASR, 1)); |
| 6366 | __ bind(&done_checking_lhs); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6367 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6368 | __ tst(rhs, Operand(kSmiTagMask)); |
| 6369 | __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number. |
| 6370 | __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6371 | __ b(ne, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6372 | GetInt32(masm, rhs, r2, r5, r4, &slow); |
| 6373 | __ jmp(&done_checking_rhs); |
| 6374 | __ bind(&rhs_is_smi); |
| 6375 | __ mov(r2, Operand(rhs, ASR, 1)); |
| 6376 | __ bind(&done_checking_rhs); |
| 6377 | |
| 6378 | ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6379 | |
| 6380 | // r0 and r1: Original operands (Smi or heap numbers). |
| 6381 | // r2 and r3: Signed int32 operands. |
| 6382 | switch (op_) { |
| 6383 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 6384 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 6385 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 6386 | case Token::SAR: |
| 6387 | // Use only the 5 least significant bits of the shift count. |
| 6388 | __ and_(r2, r2, Operand(0x1f)); |
| 6389 | __ mov(r2, Operand(r3, ASR, r2)); |
| 6390 | break; |
| 6391 | case Token::SHR: |
| 6392 | // Use only the 5 least significant bits of the shift count. |
| 6393 | __ and_(r2, r2, Operand(0x1f)); |
| 6394 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 6395 | // SHR is special because it is required to produce a positive answer. |
| 6396 | // The code below for writing into heap numbers isn't capable of writing |
| 6397 | // the register as an unsigned int so we go to slow case if we hit this |
| 6398 | // case. |
| 6399 | __ b(mi, &slow); |
| 6400 | break; |
| 6401 | case Token::SHL: |
| 6402 | // Use only the 5 least significant bits of the shift count. |
| 6403 | __ and_(r2, r2, Operand(0x1f)); |
| 6404 | __ mov(r2, Operand(r3, LSL, r2)); |
| 6405 | break; |
| 6406 | default: UNREACHABLE(); |
| 6407 | } |
| 6408 | // check that the *signed* result fits in a smi |
| 6409 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 6410 | __ b(mi, &result_not_a_smi); |
| 6411 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 6412 | __ Ret(); |
| 6413 | |
| 6414 | Label have_to_allocate, got_a_heap_number; |
| 6415 | __ bind(&result_not_a_smi); |
| 6416 | switch (mode_) { |
| 6417 | case OVERWRITE_RIGHT: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6418 | __ tst(rhs, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6419 | __ b(eq, &have_to_allocate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6420 | __ mov(r5, Operand(rhs)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6421 | break; |
| 6422 | } |
| 6423 | case OVERWRITE_LEFT: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6424 | __ tst(lhs, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6425 | __ b(eq, &have_to_allocate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6426 | __ mov(r5, Operand(lhs)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6427 | break; |
| 6428 | } |
| 6429 | case NO_OVERWRITE: { |
| 6430 | // Get a new heap number in r5. r6 and r7 are scratch. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6431 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6432 | } |
| 6433 | default: break; |
| 6434 | } |
| 6435 | __ bind(&got_a_heap_number); |
| 6436 | // r2: Answer as signed int32. |
| 6437 | // r5: Heap number to write answer into. |
| 6438 | |
| 6439 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 6440 | // result. |
| 6441 | __ mov(r0, Operand(r5)); |
| 6442 | |
| 6443 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 6444 | // r3 as scratch. r0 is preserved and returned. |
| 6445 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 6446 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 6447 | |
| 6448 | if (mode_ != NO_OVERWRITE) { |
| 6449 | __ bind(&have_to_allocate); |
| 6450 | // Get a new heap number in r5. r6 and r7 are scratch. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6451 | __ AllocateHeapNumber(r5, r6, r7, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6452 | __ jmp(&got_a_heap_number); |
| 6453 | } |
| 6454 | |
| 6455 | // If all else failed then we go to the runtime system. |
| 6456 | __ bind(&slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6457 | __ push(lhs); // restore stack |
| 6458 | __ push(rhs); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6459 | switch (op_) { |
| 6460 | case Token::BIT_OR: |
| 6461 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 6462 | break; |
| 6463 | case Token::BIT_AND: |
| 6464 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 6465 | break; |
| 6466 | case Token::BIT_XOR: |
| 6467 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 6468 | break; |
| 6469 | case Token::SAR: |
| 6470 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 6471 | break; |
| 6472 | case Token::SHR: |
| 6473 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 6474 | break; |
| 6475 | case Token::SHL: |
| 6476 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 6477 | break; |
| 6478 | default: |
| 6479 | UNREACHABLE(); |
| 6480 | } |
| 6481 | } |
| 6482 | |
| 6483 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6484 | // Can we multiply by x with max two shifts and an add. |
| 6485 | // This answers yes to all integers from 2 to 10. |
| 6486 | static bool IsEasyToMultiplyBy(int x) { |
| 6487 | if (x < 2) return false; // Avoid special cases. |
| 6488 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 6489 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 6490 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 6491 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 6492 | return false; |
| 6493 | } |
| 6494 | |
| 6495 | |
| 6496 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 6497 | // Source and destination may be the same register. This routine does |
| 6498 | // not set carry and overflow the way a mul instruction would. |
| 6499 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 6500 | Register source, |
| 6501 | Register destination, |
| 6502 | int known_int) { |
| 6503 | if (IsPowerOf2(known_int)) { |
| 6504 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 6505 | } else if (PopCountLessThanEqual2(known_int)) { |
| 6506 | int first_bit = BitPosition(known_int); |
| 6507 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 6508 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 6509 | if (first_bit != 0) { |
| 6510 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 6511 | } |
| 6512 | } else { |
| 6513 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 6514 | int the_bit = BitPosition(known_int + 1); |
| 6515 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 6516 | } |
| 6517 | } |
| 6518 | |
| 6519 | |
| 6520 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 6521 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 6522 | // a result that needs shifting. |
| 6523 | static void MultiplyByKnownInt2( |
| 6524 | MacroAssembler* masm, |
| 6525 | Register result, |
| 6526 | Register source, |
| 6527 | Register known_int_register, // Smi tagged. |
| 6528 | int known_int, |
| 6529 | int* required_shift) { // Including Smi tag shift |
| 6530 | switch (known_int) { |
| 6531 | case 3: |
| 6532 | __ add(result, source, Operand(source, LSL, 1)); |
| 6533 | *required_shift = 1; |
| 6534 | break; |
| 6535 | case 5: |
| 6536 | __ add(result, source, Operand(source, LSL, 2)); |
| 6537 | *required_shift = 1; |
| 6538 | break; |
| 6539 | case 6: |
| 6540 | __ add(result, source, Operand(source, LSL, 1)); |
| 6541 | *required_shift = 2; |
| 6542 | break; |
| 6543 | case 7: |
| 6544 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 6545 | *required_shift = 1; |
| 6546 | break; |
| 6547 | case 9: |
| 6548 | __ add(result, source, Operand(source, LSL, 3)); |
| 6549 | *required_shift = 1; |
| 6550 | break; |
| 6551 | case 10: |
| 6552 | __ add(result, source, Operand(source, LSL, 2)); |
| 6553 | *required_shift = 2; |
| 6554 | break; |
| 6555 | default: |
| 6556 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 6557 | __ mul(result, source, known_int_register); |
| 6558 | *required_shift = 0; |
| 6559 | } |
| 6560 | } |
| 6561 | |
| 6562 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 6563 | const char* GenericBinaryOpStub::GetName() { |
| 6564 | if (name_ != NULL) return name_; |
| 6565 | const int len = 100; |
| 6566 | name_ = Bootstrapper::AllocateAutoDeletedArray(len); |
| 6567 | if (name_ == NULL) return "OOM"; |
| 6568 | const char* op_name = Token::Name(op_); |
| 6569 | const char* overwrite_name; |
| 6570 | switch (mode_) { |
| 6571 | case NO_OVERWRITE: overwrite_name = "Alloc"; break; |
| 6572 | case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; |
| 6573 | case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; |
| 6574 | default: overwrite_name = "UnknownOverwrite"; break; |
| 6575 | } |
| 6576 | |
| 6577 | OS::SNPrintF(Vector<char>(name_, len), |
| 6578 | "GenericBinaryOpStub_%s_%s%s", |
| 6579 | op_name, |
| 6580 | overwrite_name, |
| 6581 | specialized_on_rhs_ ? "_ConstantRhs" : 0); |
| 6582 | return name_; |
| 6583 | } |
| 6584 | |
| 6585 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6586 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6587 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6588 | // lhs_ : x |
| 6589 | // rhs_ : y |
| 6590 | // r0 : result |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6591 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6592 | Register result = r0; |
| 6593 | Register lhs = lhs_; |
| 6594 | Register rhs = rhs_; |
| 6595 | |
| 6596 | // This code can't cope with other register allocations yet. |
| 6597 | ASSERT(result.is(r0) && |
| 6598 | ((lhs.is(r0) && rhs.is(r1)) || |
| 6599 | (lhs.is(r1) && rhs.is(r0)))); |
| 6600 | |
| 6601 | Register smi_test_reg = VirtualFrame::scratch0(); |
| 6602 | Register scratch = VirtualFrame::scratch1(); |
| 6603 | |
| 6604 | // All ops need to know whether we are dealing with two Smis. Set up |
| 6605 | // smi_test_reg to tell us that. |
| 6606 | if (ShouldGenerateSmiCode()) { |
| 6607 | __ orr(smi_test_reg, lhs, Operand(rhs)); |
| 6608 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6609 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6610 | switch (op_) { |
| 6611 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6612 | Label not_smi; |
| 6613 | // Fast path. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6614 | if (ShouldGenerateSmiCode()) { |
| 6615 | ASSERT(kSmiTag == 0); // Adjust code below. |
| 6616 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 6617 | __ b(ne, ¬_smi); |
| 6618 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 6619 | // Return if no overflow. |
| 6620 | __ Ret(vc); |
| 6621 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 6622 | } |
| 6623 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::ADD); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6624 | break; |
| 6625 | } |
| 6626 | |
| 6627 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6628 | Label not_smi; |
| 6629 | // Fast path. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6630 | if (ShouldGenerateSmiCode()) { |
| 6631 | ASSERT(kSmiTag == 0); // Adjust code below. |
| 6632 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 6633 | __ b(ne, ¬_smi); |
| 6634 | if (lhs.is(r1)) { |
| 6635 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 6636 | // Return if no overflow. |
| 6637 | __ Ret(vc); |
| 6638 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 6639 | } else { |
| 6640 | __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically. |
| 6641 | // Return if no overflow. |
| 6642 | __ Ret(vc); |
| 6643 | __ add(r0, r0, Operand(r1)); // Revert optimistic subtract. |
| 6644 | } |
| 6645 | } |
| 6646 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::SUB); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6647 | break; |
| 6648 | } |
| 6649 | |
| 6650 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6651 | Label not_smi, slow; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6652 | if (ShouldGenerateSmiCode()) { |
| 6653 | ASSERT(kSmiTag == 0); // adjust code below |
| 6654 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 6655 | Register scratch2 = smi_test_reg; |
| 6656 | smi_test_reg = no_reg; |
| 6657 | __ b(ne, ¬_smi); |
| 6658 | // Remove tag from one operand (but keep sign), so that result is Smi. |
| 6659 | __ mov(ip, Operand(rhs, ASR, kSmiTagSize)); |
| 6660 | // Do multiplication |
| 6661 | // scratch = lower 32 bits of ip * lhs. |
| 6662 | __ smull(scratch, scratch2, lhs, ip); |
| 6663 | // Go slow on overflows (overflow bit is not set). |
| 6664 | __ mov(ip, Operand(scratch, ASR, 31)); |
| 6665 | // No overflow if higher 33 bits are identical. |
| 6666 | __ cmp(ip, Operand(scratch2)); |
| 6667 | __ b(ne, &slow); |
| 6668 | // Go slow on zero result to handle -0. |
| 6669 | __ tst(scratch, Operand(scratch)); |
| 6670 | __ mov(result, Operand(scratch), LeaveCC, ne); |
| 6671 | __ Ret(ne); |
| 6672 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 6673 | // We know one of them was zero. |
| 6674 | __ add(scratch2, rhs, Operand(lhs), SetCC); |
| 6675 | __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 6676 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 6677 | // Slow case. We fall through here if we multiplied a negative number |
| 6678 | // with 0, because that would mean we should produce -0. |
| 6679 | __ bind(&slow); |
| 6680 | } |
| 6681 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::MUL); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6682 | break; |
| 6683 | } |
| 6684 | |
| 6685 | case Token::DIV: |
| 6686 | case Token::MOD: { |
| 6687 | Label not_smi; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6688 | if (ShouldGenerateSmiCode() && specialized_on_rhs_) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6689 | Label smi_is_unsuitable; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6690 | __ BranchOnNotSmi(lhs, ¬_smi); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6691 | if (IsPowerOf2(constant_rhs_)) { |
| 6692 | if (op_ == Token::MOD) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6693 | __ and_(rhs, |
| 6694 | lhs, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6695 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 6696 | SetCC); |
| 6697 | // We now have the answer, but if the input was negative we also |
| 6698 | // have the sign bit. Our work is done if the result is |
| 6699 | // positive or zero: |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6700 | if (!rhs.is(r0)) { |
| 6701 | __ mov(r0, rhs, LeaveCC, pl); |
| 6702 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6703 | __ Ret(pl); |
| 6704 | // A mod of a negative left hand side must return a negative number. |
| 6705 | // Unfortunately if the answer is 0 then we must return -0. And we |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6706 | // already optimistically trashed rhs so we may need to restore it. |
| 6707 | __ eor(rhs, rhs, Operand(0x80000000u), SetCC); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6708 | // Next two instructions are conditional on the answer being -0. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6709 | __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6710 | __ b(eq, &smi_is_unsuitable); |
| 6711 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6712 | __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_))); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6713 | } else { |
| 6714 | ASSERT(op_ == Token::DIV); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6715 | __ tst(lhs, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6716 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 6717 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 6718 | int shift = 0; |
| 6719 | int d = constant_rhs_; |
| 6720 | while ((d & 1) == 0) { |
| 6721 | d >>= 1; |
| 6722 | shift++; |
| 6723 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6724 | __ mov(r0, Operand(lhs, LSR, shift)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6725 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 6726 | } |
| 6727 | } else { |
| 6728 | // Not a power of 2. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6729 | __ tst(lhs, Operand(0x80000000u)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6730 | __ b(ne, &smi_is_unsuitable); |
| 6731 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 6732 | // multiplying. |
| 6733 | double divisor = 1.0 / constant_rhs_; |
| 6734 | int shift = 32; |
| 6735 | double scale = 4294967296.0; // 1 << 32. |
| 6736 | uint32_t mul; |
| 6737 | // Maximise the precision of the fixed point reciprocal. |
| 6738 | while (true) { |
| 6739 | mul = static_cast<uint32_t>(scale * divisor); |
| 6740 | if (mul >= 0x7fffffff) break; |
| 6741 | scale *= 2.0; |
| 6742 | shift++; |
| 6743 | } |
| 6744 | mul++; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6745 | Register scratch2 = smi_test_reg; |
| 6746 | smi_test_reg = no_reg; |
| 6747 | __ mov(scratch2, Operand(mul)); |
| 6748 | __ umull(scratch, scratch2, scratch2, lhs); |
| 6749 | __ mov(scratch2, Operand(scratch2, LSR, shift - 31)); |
| 6750 | // scratch2 is lhs / rhs. scratch2 is not Smi tagged. |
| 6751 | // rhs is still the known rhs. rhs is Smi tagged. |
| 6752 | // lhs is still the unkown lhs. lhs is Smi tagged. |
| 6753 | int required_scratch_shift = 0; // Including the Smi tag shift of 1. |
| 6754 | // scratch = scratch2 * rhs. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6755 | MultiplyByKnownInt2(masm, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6756 | scratch, |
| 6757 | scratch2, |
| 6758 | rhs, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6759 | constant_rhs_, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6760 | &required_scratch_shift); |
| 6761 | // scratch << required_scratch_shift is now the Smi tagged rhs * |
| 6762 | // (lhs / rhs) where / indicates integer division. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6763 | if (op_ == Token::DIV) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6764 | __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6765 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6766 | __ mov(result, Operand(scratch2, LSL, kSmiTagSize)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6767 | } else { |
| 6768 | ASSERT(op_ == Token::MOD); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6769 | __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6770 | } |
| 6771 | } |
| 6772 | __ Ret(); |
| 6773 | __ bind(&smi_is_unsuitable); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6774 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6775 | HandleBinaryOpSlowCases( |
| 6776 | masm, |
| 6777 | ¬_smi, |
| 6778 | lhs, |
| 6779 | rhs, |
| 6780 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6781 | break; |
| 6782 | } |
| 6783 | |
| 6784 | case Token::BIT_OR: |
| 6785 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6786 | case Token::BIT_XOR: |
| 6787 | case Token::SAR: |
| 6788 | case Token::SHR: |
| 6789 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6790 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6791 | ASSERT(kSmiTag == 0); // adjust code below |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6792 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6793 | __ b(ne, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6794 | Register scratch2 = smi_test_reg; |
| 6795 | smi_test_reg = no_reg; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6796 | switch (op_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6797 | case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break; |
| 6798 | case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break; |
| 6799 | case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6800 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6801 | // Remove tags from right operand. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6802 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 6803 | __ mov(result, Operand(lhs, ASR, scratch2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6804 | // Smi tag result. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6805 | __ bic(result, result, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6806 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6807 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6808 | // Remove tags from operands. We can't do this on a 31 bit number |
| 6809 | // because then the 0s get shifted into bit 30 instead of bit 31. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6810 | __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x |
| 6811 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 6812 | __ mov(scratch, Operand(scratch, LSR, scratch2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6813 | // Unsigned shift is not allowed to produce a negative number, so |
| 6814 | // check the sign bit and the sign bit after Smi tagging. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6815 | __ tst(scratch, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6816 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6817 | // Smi tag result. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6818 | __ mov(result, Operand(scratch, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6819 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6820 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6821 | // Remove tags from operands. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6822 | __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x |
| 6823 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 6824 | __ mov(scratch, Operand(scratch, LSL, scratch2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6825 | // Check that the signed result fits in a Smi. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6826 | __ add(scratch2, scratch, Operand(0x40000000), SetCC); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6827 | __ b(mi, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6828 | __ mov(result, Operand(scratch, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6829 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6830 | default: UNREACHABLE(); |
| 6831 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6832 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6833 | __ bind(&slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6834 | HandleNonSmiBitwiseOp(masm, lhs, rhs); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6835 | break; |
| 6836 | } |
| 6837 | |
| 6838 | default: UNREACHABLE(); |
| 6839 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6840 | // This code should be unreachable. |
| 6841 | __ stop("Unreachable"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6842 | |
| 6843 | // Generate an unreachable reference to the DEFAULT stub so that it can be |
| 6844 | // found at the end of this stub when clearing ICs at GC. |
| 6845 | // TODO(kaznacheev): Check performance impact and get rid of this. |
| 6846 | if (runtime_operands_type_ != BinaryOpIC::DEFAULT) { |
| 6847 | GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT); |
| 6848 | __ CallStub(&uninit); |
| 6849 | } |
| 6850 | } |
| 6851 | |
| 6852 | |
| 6853 | void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) { |
| 6854 | Label get_result; |
| 6855 | |
| 6856 | __ push(r1); |
| 6857 | __ push(r0); |
| 6858 | |
| 6859 | // Internal frame is necessary to handle exceptions properly. |
| 6860 | __ EnterInternalFrame(); |
| 6861 | // Call the stub proper to get the result in r0. |
| 6862 | __ Call(&get_result); |
| 6863 | __ LeaveInternalFrame(); |
| 6864 | |
| 6865 | __ push(r0); |
| 6866 | |
| 6867 | __ mov(r0, Operand(Smi::FromInt(MinorKey()))); |
| 6868 | __ push(r0); |
| 6869 | __ mov(r0, Operand(Smi::FromInt(op_))); |
| 6870 | __ push(r0); |
| 6871 | __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_))); |
| 6872 | __ push(r0); |
| 6873 | |
| 6874 | __ TailCallExternalReference( |
| 6875 | ExternalReference(IC_Utility(IC::kBinaryOp_Patch)), |
| 6876 | 6, |
| 6877 | 1); |
| 6878 | |
| 6879 | // The entry point for the result calculation is assumed to be immediately |
| 6880 | // after this sequence. |
| 6881 | __ bind(&get_result); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6885 | Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6886 | GenericBinaryOpStub stub(key, type_info); |
| 6887 | return stub.GetCode(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6888 | } |
| 6889 | |
| 6890 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6891 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 6892 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 6893 | // argument, so give it a Smi. |
| 6894 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6895 | __ push(r0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6896 | __ TailCallRuntime(Runtime::kStackGuard, 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6897 | |
| 6898 | __ StubReturn(1); |
| 6899 | } |
| 6900 | |
| 6901 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6902 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6903 | Label slow, done; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6904 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6905 | if (op_ == Token::SUB) { |
| 6906 | // Check whether the value is a smi. |
| 6907 | Label try_float; |
| 6908 | __ tst(r0, Operand(kSmiTagMask)); |
| 6909 | __ b(ne, &try_float); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6910 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6911 | // Go slow case if the value of the expression is zero |
| 6912 | // to make sure that we switch between 0 and -0. |
| 6913 | __ cmp(r0, Operand(0)); |
| 6914 | __ b(eq, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6915 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6916 | // The value of the expression is a smi that is not zero. Try |
| 6917 | // optimistic subtraction '0 - value'. |
| 6918 | __ rsb(r1, r0, Operand(0), SetCC); |
| 6919 | __ b(vs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6920 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6921 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 6922 | __ b(&done); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6923 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6924 | __ bind(&try_float); |
| 6925 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6926 | __ b(ne, &slow); |
| 6927 | // r0 is a heap number. Get a new heap number in r1. |
| 6928 | if (overwrite_) { |
| 6929 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6930 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6931 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6932 | } else { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6933 | __ AllocateHeapNumber(r1, r2, r3, &slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6934 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 6935 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6936 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 6937 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6938 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 6939 | __ mov(r0, Operand(r1)); |
| 6940 | } |
| 6941 | } else if (op_ == Token::BIT_NOT) { |
| 6942 | // Check if the operand is a heap number. |
| 6943 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6944 | __ b(ne, &slow); |
| 6945 | |
| 6946 | // Convert the heap number is r0 to an untagged integer in r1. |
| 6947 | GetInt32(masm, r0, r1, r2, r3, &slow); |
| 6948 | |
| 6949 | // Do the bitwise operation (move negated) and check if the result |
| 6950 | // fits in a smi. |
| 6951 | Label try_float; |
| 6952 | __ mvn(r1, Operand(r1)); |
| 6953 | __ add(r2, r1, Operand(0x40000000), SetCC); |
| 6954 | __ b(mi, &try_float); |
| 6955 | __ mov(r0, Operand(r1, LSL, kSmiTagSize)); |
| 6956 | __ b(&done); |
| 6957 | |
| 6958 | __ bind(&try_float); |
| 6959 | if (!overwrite_) { |
| 6960 | // Allocate a fresh heap number, but don't overwrite r0 until |
| 6961 | // we're sure we can do it without going through the slow case |
| 6962 | // that needs the value in r0. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6963 | __ AllocateHeapNumber(r2, r3, r4, &slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6964 | __ mov(r0, Operand(r2)); |
| 6965 | } |
| 6966 | |
| 6967 | // WriteInt32ToHeapNumberStub does not trigger GC, so we do not |
| 6968 | // have to set up a frame. |
| 6969 | WriteInt32ToHeapNumberStub stub(r1, r0, r2); |
| 6970 | __ push(lr); |
| 6971 | __ Call(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 6972 | __ pop(lr); |
| 6973 | } else { |
| 6974 | UNIMPLEMENTED(); |
| 6975 | } |
| 6976 | |
| 6977 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6978 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6979 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6980 | // Handle the slow case by jumping to the JavaScript builtin. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6981 | __ bind(&slow); |
| 6982 | __ push(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6983 | switch (op_) { |
| 6984 | case Token::SUB: |
| 6985 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 6986 | break; |
| 6987 | case Token::BIT_NOT: |
| 6988 | __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS); |
| 6989 | break; |
| 6990 | default: |
| 6991 | UNREACHABLE(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6992 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6993 | } |
| 6994 | |
| 6995 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6996 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6997 | // r0 holds the exception. |
| 6998 | |
| 6999 | // Adjust this code if not the case. |
| 7000 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 7001 | |
| 7002 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7003 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 7004 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7005 | |
| 7006 | // Restore the next handler and frame pointer, discard handler state. |
| 7007 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 7008 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7009 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7010 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 7011 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 7012 | |
| 7013 | // Before returning we restore the context from the frame pointer if |
| 7014 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 7015 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7016 | __ cmp(fp, Operand(0)); |
| 7017 | // Set cp to NULL if fp is NULL. |
| 7018 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 7019 | // Restore cp otherwise. |
| 7020 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7021 | #ifdef DEBUG |
| 7022 | if (FLAG_debug_code) { |
| 7023 | __ mov(lr, Operand(pc)); |
| 7024 | } |
| 7025 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7026 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7027 | __ pop(pc); |
| 7028 | } |
| 7029 | |
| 7030 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7031 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 7032 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7033 | // Adjust this code if not the case. |
| 7034 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 7035 | |
| 7036 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7037 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7038 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7039 | |
| 7040 | // Unwind the handlers until the ENTRY handler is found. |
| 7041 | Label loop, done; |
| 7042 | __ bind(&loop); |
| 7043 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7044 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 7045 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7046 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 7047 | __ b(eq, &done); |
| 7048 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7049 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 7050 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7051 | __ jmp(&loop); |
| 7052 | __ bind(&done); |
| 7053 | |
| 7054 | // 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] | 7055 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7056 | __ pop(r2); |
| 7057 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7058 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7059 | if (type == OUT_OF_MEMORY) { |
| 7060 | // Set external caught exception to false. |
| 7061 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 7062 | __ mov(r0, Operand(false)); |
| 7063 | __ mov(r2, Operand(external_caught)); |
| 7064 | __ str(r0, MemOperand(r2)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7065 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7066 | // Set pending exception and r0 to out of memory exception. |
| 7067 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 7068 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 7069 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 7070 | __ str(r0, MemOperand(r2)); |
| 7071 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7072 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7073 | // Stack layout at this point. See also StackHandlerConstants. |
| 7074 | // sp -> state (ENTRY) |
| 7075 | // fp |
| 7076 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7077 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7078 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 7079 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 7080 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 7081 | // Before returning we restore the context from the frame pointer if |
| 7082 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 7083 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7084 | __ cmp(fp, Operand(0)); |
| 7085 | // Set cp to NULL if fp is NULL. |
| 7086 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 7087 | // Restore cp otherwise. |
| 7088 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7089 | #ifdef DEBUG |
| 7090 | if (FLAG_debug_code) { |
| 7091 | __ mov(lr, Operand(pc)); |
| 7092 | } |
| 7093 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7094 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7095 | __ pop(pc); |
| 7096 | } |
| 7097 | |
| 7098 | |
| 7099 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 7100 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7101 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7102 | Label* throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7103 | bool do_gc, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 7104 | bool always_allocate, |
| 7105 | int frame_alignment_skew) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7106 | // r0: result parameter for PerformGC, if any |
| 7107 | // r4: number of arguments including receiver (C callee-saved) |
| 7108 | // r5: pointer to builtin function (C callee-saved) |
| 7109 | // r6: pointer to the first argument (C callee-saved) |
| 7110 | |
| 7111 | if (do_gc) { |
| 7112 | // Passing r0. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 7113 | __ PrepareCallCFunction(1, r1); |
| 7114 | __ CallCFunction(ExternalReference::perform_gc_function(), 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7115 | } |
| 7116 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7117 | ExternalReference scope_depth = |
| 7118 | ExternalReference::heap_always_allocate_scope_depth(); |
| 7119 | if (always_allocate) { |
| 7120 | __ mov(r0, Operand(scope_depth)); |
| 7121 | __ ldr(r1, MemOperand(r0)); |
| 7122 | __ add(r1, r1, Operand(1)); |
| 7123 | __ str(r1, MemOperand(r0)); |
| 7124 | } |
| 7125 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7126 | // Call C built-in. |
| 7127 | // r0 = argc, r1 = argv |
| 7128 | __ mov(r0, Operand(r4)); |
| 7129 | __ mov(r1, Operand(r6)); |
| 7130 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 7131 | int frame_alignment = MacroAssembler::ActivationFrameAlignment(); |
| 7132 | int frame_alignment_mask = frame_alignment - 1; |
| 7133 | #if defined(V8_HOST_ARCH_ARM) |
| 7134 | if (FLAG_debug_code) { |
| 7135 | if (frame_alignment > kPointerSize) { |
| 7136 | Label alignment_as_expected; |
| 7137 | ASSERT(IsPowerOf2(frame_alignment)); |
| 7138 | __ sub(r2, sp, Operand(frame_alignment_skew)); |
| 7139 | __ tst(r2, Operand(frame_alignment_mask)); |
| 7140 | __ b(eq, &alignment_as_expected); |
| 7141 | // Don't use Check here, as it will call Runtime_Abort re-entering here. |
| 7142 | __ stop("Unexpected alignment"); |
| 7143 | __ bind(&alignment_as_expected); |
| 7144 | } |
| 7145 | } |
| 7146 | #endif |
| 7147 | |
| 7148 | // Just before the call (jump) below lr is pushed, so the actual alignment is |
| 7149 | // adding one to the current skew. |
| 7150 | int alignment_before_call = |
| 7151 | (frame_alignment_skew + kPointerSize) & frame_alignment_mask; |
| 7152 | if (alignment_before_call > 0) { |
| 7153 | // Push until the alignment before the call is met. |
| 7154 | __ mov(r2, Operand(0)); |
| 7155 | for (int i = alignment_before_call; |
| 7156 | (i & frame_alignment_mask) != 0; |
| 7157 | i += kPointerSize) { |
| 7158 | __ push(r2); |
| 7159 | } |
| 7160 | } |
| 7161 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7162 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 7163 | // frames, we need to know where the return address is. Right now, |
| 7164 | // we push it on the stack to be able to find it again, but we never |
| 7165 | // restore from it in case of changes, which makes it impossible to |
| 7166 | // support moving the C entry code stub. This should be fixed, but currently |
| 7167 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 7168 | // sequence that it is not moving ever. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 7169 | masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4 |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7170 | masm->push(lr); |
| 7171 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7172 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 7173 | // Restore sp back to before aligning the stack. |
| 7174 | if (alignment_before_call > 0) { |
| 7175 | __ add(sp, sp, Operand(alignment_before_call)); |
| 7176 | } |
| 7177 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7178 | if (always_allocate) { |
| 7179 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 7180 | // though (contain the result). |
| 7181 | __ mov(r2, Operand(scope_depth)); |
| 7182 | __ ldr(r3, MemOperand(r2)); |
| 7183 | __ sub(r3, r3, Operand(1)); |
| 7184 | __ str(r3, MemOperand(r2)); |
| 7185 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7186 | |
| 7187 | // check for failure result |
| 7188 | Label failure_returned; |
| 7189 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 7190 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 7191 | __ add(r2, r0, Operand(1)); |
| 7192 | __ tst(r2, Operand(kFailureTagMask)); |
| 7193 | __ b(eq, &failure_returned); |
| 7194 | |
| 7195 | // Exit C frame and return. |
| 7196 | // r0:r1: result |
| 7197 | // sp: stack pointer |
| 7198 | // fp: frame pointer |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7199 | __ LeaveExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7200 | |
| 7201 | // check if we should retry or throw exception |
| 7202 | Label retry; |
| 7203 | __ bind(&failure_returned); |
| 7204 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 7205 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 7206 | __ b(eq, &retry); |
| 7207 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7208 | // Special handling of out of memory exceptions. |
| 7209 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 7210 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 7211 | __ b(eq, throw_out_of_memory_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7212 | |
| 7213 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 7214 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7215 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 7216 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7217 | __ ldr(r0, MemOperand(ip)); |
| 7218 | __ str(r3, MemOperand(ip)); |
| 7219 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7220 | // Special handling of termination exceptions which are uncatchable |
| 7221 | // by javascript code. |
| 7222 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 7223 | __ b(eq, throw_termination_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7224 | |
| 7225 | // Handle normal exception. |
| 7226 | __ jmp(throw_normal_exception); |
| 7227 | |
| 7228 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 7229 | } |
| 7230 | |
| 7231 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7232 | void CEntryStub::Generate(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7233 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 7234 | // r0: number of arguments including receiver |
| 7235 | // r1: pointer to builtin function |
| 7236 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7237 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7238 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7239 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7240 | // Result returned in r0 or r0+r1 by default. |
| 7241 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7242 | // NOTE: Invocations of builtins may return failure objects |
| 7243 | // instead of a proper result. The builtin entry handles |
| 7244 | // this by performing a garbage collection and retrying the |
| 7245 | // builtin once. |
| 7246 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7247 | // Enter the exit frame that transitions from JavaScript to C++. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7248 | __ EnterExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7249 | |
| 7250 | // r4: number of arguments (C callee-saved) |
| 7251 | // r5: pointer to builtin function (C callee-saved) |
| 7252 | // r6: pointer to first argument (C callee-saved) |
| 7253 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7254 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7255 | Label throw_termination_exception; |
| 7256 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7257 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 7258 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7259 | GenerateCore(masm, |
| 7260 | &throw_normal_exception, |
| 7261 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7262 | &throw_out_of_memory_exception, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 7263 | false, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 7264 | false, |
| 7265 | -kPointerSize); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7266 | |
| 7267 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7268 | GenerateCore(masm, |
| 7269 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7270 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7271 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7272 | true, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 7273 | false, |
| 7274 | 0); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7275 | |
| 7276 | // Do full GC and retry runtime call one final time. |
| 7277 | Failure* failure = Failure::InternalError(); |
| 7278 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 7279 | GenerateCore(masm, |
| 7280 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7281 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7282 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7283 | true, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 7284 | true, |
| 7285 | kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7286 | |
| 7287 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 7288 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 7289 | |
| 7290 | __ bind(&throw_termination_exception); |
| 7291 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7292 | |
| 7293 | __ bind(&throw_normal_exception); |
| 7294 | GenerateThrowTOS(masm); |
| 7295 | } |
| 7296 | |
| 7297 | |
| 7298 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 7299 | // r0: code entry |
| 7300 | // r1: function |
| 7301 | // r2: receiver |
| 7302 | // r3: argc |
| 7303 | // [sp+0]: argv |
| 7304 | |
| 7305 | Label invoke, exit; |
| 7306 | |
| 7307 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 7308 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7309 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7310 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 7311 | |
| 7312 | // Get address of argv, see stm above. |
| 7313 | // r0: code entry |
| 7314 | // r1: function |
| 7315 | // r2: receiver |
| 7316 | // r3: argc |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7317 | __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7318 | |
| 7319 | // Push a frame with special values setup to mark it as an entry frame. |
| 7320 | // r0: code entry |
| 7321 | // r1: function |
| 7322 | // r2: receiver |
| 7323 | // r3: argc |
| 7324 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7325 | __ 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] | 7326 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 7327 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7328 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 7329 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 7330 | __ ldr(r5, MemOperand(r5)); |
| 7331 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 7332 | |
| 7333 | // Setup frame pointer for the frame to be pushed. |
| 7334 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 7335 | |
| 7336 | // Call a faked try-block that does the invoke. |
| 7337 | __ bl(&invoke); |
| 7338 | |
| 7339 | // Caught exception: Store result (exception) in the pending |
| 7340 | // exception field in the JSEnv and return a failure sentinel. |
| 7341 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 7342 | // 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] | 7343 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7344 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 7345 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7346 | __ b(&exit); |
| 7347 | |
| 7348 | // Invoke: Link this frame into the handler chain. |
| 7349 | __ bind(&invoke); |
| 7350 | // Must preserve r0-r4, r5-r7 are available. |
| 7351 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7352 | // If an exception not caught by another handler occurs, this handler |
| 7353 | // returns control to the code after the bl(&invoke) above, which |
| 7354 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 7355 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7356 | |
| 7357 | // Clear any pending exceptions. |
| 7358 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 7359 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 7360 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7361 | __ str(r5, MemOperand(ip)); |
| 7362 | |
| 7363 | // Invoke the function by calling through JS entry trampoline builtin. |
| 7364 | // Notice that we cannot store a reference to the trampoline code directly in |
| 7365 | // this stub, because runtime stubs are not traversed when doing GC. |
| 7366 | |
| 7367 | // Expected registers by Builtins::JSEntryTrampoline |
| 7368 | // r0: code entry |
| 7369 | // r1: function |
| 7370 | // r2: receiver |
| 7371 | // r3: argc |
| 7372 | // r4: argv |
| 7373 | if (is_construct) { |
| 7374 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 7375 | __ mov(ip, Operand(construct_entry)); |
| 7376 | } else { |
| 7377 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 7378 | __ mov(ip, Operand(entry)); |
| 7379 | } |
| 7380 | __ ldr(ip, MemOperand(ip)); // deref address |
| 7381 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7382 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 7383 | // macro for the add instruction because we don't want the coverage tool |
| 7384 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7385 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7386 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7387 | |
| 7388 | // Unlink this frame from the handler chain. When reading the |
| 7389 | // address of the next handler, there is no need to use the address |
| 7390 | // displacement since the current stack pointer (sp) points directly |
| 7391 | // to the stack handler. |
| 7392 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 7393 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 7394 | __ str(r3, MemOperand(ip)); |
| 7395 | // No need to restore registers |
| 7396 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 7397 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7398 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7399 | __ bind(&exit); // r0 holds result |
| 7400 | // Restore the top frame descriptors from the stack. |
| 7401 | __ pop(r3); |
| 7402 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 7403 | __ str(r3, MemOperand(ip)); |
| 7404 | |
| 7405 | // Reset the stack to the callee saved registers. |
| 7406 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 7407 | |
| 7408 | // Restore callee-saved registers and return. |
| 7409 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7410 | if (FLAG_debug_code) { |
| 7411 | __ mov(lr, Operand(pc)); |
| 7412 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7413 | #endif |
| 7414 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 7415 | } |
| 7416 | |
| 7417 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7418 | // This stub performs an instanceof, calling the builtin function if |
| 7419 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 7420 | // be an instance of (these are fetched from the stack). |
| 7421 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 7422 | // Get the object - slow case for smis (we may need to throw an exception |
| 7423 | // depending on the rhs). |
| 7424 | Label slow, loop, is_instance, is_not_instance; |
| 7425 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 7426 | __ BranchOnSmi(r0, &slow); |
| 7427 | |
| 7428 | // Check that the left hand is a JS object and put map in r3. |
| 7429 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 7430 | __ b(lt, &slow); |
| 7431 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 7432 | __ b(gt, &slow); |
| 7433 | |
| 7434 | // 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] | 7435 | __ ldr(r1, MemOperand(sp, 0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7436 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 7437 | |
| 7438 | // Check that the function prototype is a JS object. |
| 7439 | __ BranchOnSmi(r4, &slow); |
| 7440 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 7441 | __ b(lt, &slow); |
| 7442 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 7443 | __ b(gt, &slow); |
| 7444 | |
| 7445 | // Register mapping: r3 is object map and r4 is function prototype. |
| 7446 | // Get prototype of object into r2. |
| 7447 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 7448 | |
| 7449 | // Loop through the prototype chain looking for the function prototype. |
| 7450 | __ bind(&loop); |
| 7451 | __ cmp(r2, Operand(r4)); |
| 7452 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 7453 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 7454 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7455 | __ b(eq, &is_not_instance); |
| 7456 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 7457 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 7458 | __ jmp(&loop); |
| 7459 | |
| 7460 | __ bind(&is_instance); |
| 7461 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 7462 | __ pop(); |
| 7463 | __ pop(); |
| 7464 | __ mov(pc, Operand(lr)); // Return. |
| 7465 | |
| 7466 | __ bind(&is_not_instance); |
| 7467 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 7468 | __ pop(); |
| 7469 | __ pop(); |
| 7470 | __ mov(pc, Operand(lr)); // Return. |
| 7471 | |
| 7472 | // Slow-case. Tail call builtin. |
| 7473 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7474 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 7475 | } |
| 7476 | |
| 7477 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7478 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 7479 | // The displacement is the offset of the last parameter (if any) |
| 7480 | // relative to the frame pointer. |
| 7481 | static const int kDisplacement = |
| 7482 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7483 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7484 | // Check that the key is a smi. |
| 7485 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7486 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7487 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7488 | // Check if the calling frame is an arguments adaptor frame. |
| 7489 | Label adaptor; |
| 7490 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 7491 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7492 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7493 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7494 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7495 | // Check index against formal parameters count limit passed in |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 7496 | // through register r0. Use unsigned comparison to get negative |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7497 | // check for free. |
| 7498 | __ cmp(r1, r0); |
| 7499 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7500 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7501 | // Read the argument from the stack and return it. |
| 7502 | __ sub(r3, r0, r1); |
| 7503 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 7504 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 7505 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7506 | |
| 7507 | // Arguments adaptor case: Check index against actual arguments |
| 7508 | // limit found in the arguments adaptor frame. Use unsigned |
| 7509 | // comparison to get negative check for free. |
| 7510 | __ bind(&adaptor); |
| 7511 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 7512 | __ cmp(r1, r0); |
| 7513 | __ b(cs, &slow); |
| 7514 | |
| 7515 | // Read the argument from the adaptor frame and return it. |
| 7516 | __ sub(r3, r0, r1); |
| 7517 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 7518 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 7519 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7520 | |
| 7521 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 7522 | // by calling the runtime system. |
| 7523 | __ bind(&slow); |
| 7524 | __ push(r1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7525 | __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7526 | } |
| 7527 | |
| 7528 | |
| 7529 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7530 | // sp[0] : number of parameters |
| 7531 | // sp[4] : receiver displacement |
| 7532 | // sp[8] : function |
| 7533 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7534 | // Check if the calling frame is an arguments adaptor frame. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7535 | Label adaptor_frame, try_allocate, runtime; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7536 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 7537 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7538 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7539 | __ b(eq, &adaptor_frame); |
| 7540 | |
| 7541 | // Get the length from the frame. |
| 7542 | __ ldr(r1, MemOperand(sp, 0)); |
| 7543 | __ b(&try_allocate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7544 | |
| 7545 | // Patch the arguments.length and the parameters pointer. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7546 | __ bind(&adaptor_frame); |
| 7547 | __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 7548 | __ str(r1, MemOperand(sp, 0)); |
| 7549 | __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7550 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 7551 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 7552 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7553 | // Try the new space allocation. Start out with computing the size |
| 7554 | // of the arguments object and the elements array (in words, not |
| 7555 | // bytes because AllocateInNewSpace expects words). |
| 7556 | Label add_arguments_object; |
| 7557 | __ bind(&try_allocate); |
| 7558 | __ cmp(r1, Operand(0)); |
| 7559 | __ b(eq, &add_arguments_object); |
| 7560 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 7561 | __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize)); |
| 7562 | __ bind(&add_arguments_object); |
| 7563 | __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize)); |
| 7564 | |
| 7565 | // Do the allocation of both objects in one go. |
| 7566 | __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT); |
| 7567 | |
| 7568 | // Get the arguments boilerplate from the current (global) context. |
| 7569 | int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX); |
| 7570 | __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 7571 | __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset)); |
| 7572 | __ ldr(r4, MemOperand(r4, offset)); |
| 7573 | |
| 7574 | // Copy the JS object part. |
| 7575 | for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) { |
| 7576 | __ ldr(r3, FieldMemOperand(r4, i)); |
| 7577 | __ str(r3, FieldMemOperand(r0, i)); |
| 7578 | } |
| 7579 | |
| 7580 | // Setup the callee in-object property. |
| 7581 | ASSERT(Heap::arguments_callee_index == 0); |
| 7582 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 7583 | __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize)); |
| 7584 | |
| 7585 | // Get the length (smi tagged) and set that as an in-object property too. |
| 7586 | ASSERT(Heap::arguments_length_index == 1); |
| 7587 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 7588 | __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize)); |
| 7589 | |
| 7590 | // If there are no actual arguments, we're done. |
| 7591 | Label done; |
| 7592 | __ cmp(r1, Operand(0)); |
| 7593 | __ b(eq, &done); |
| 7594 | |
| 7595 | // Get the parameters pointer from the stack and untag the length. |
| 7596 | __ ldr(r2, MemOperand(sp, 1 * kPointerSize)); |
| 7597 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 7598 | |
| 7599 | // Setup the elements pointer in the allocated arguments object and |
| 7600 | // initialize the header in the elements fixed array. |
| 7601 | __ add(r4, r0, Operand(Heap::kArgumentsObjectSize)); |
| 7602 | __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 7603 | __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex); |
| 7604 | __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset)); |
| 7605 | __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset)); |
| 7606 | |
| 7607 | // Copy the fixed array slots. |
| 7608 | Label loop; |
| 7609 | // Setup r4 to point to the first array slot. |
| 7610 | __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 7611 | __ bind(&loop); |
| 7612 | // Pre-decrement r2 with kPointerSize on each iteration. |
| 7613 | // Pre-decrement in order to skip receiver. |
| 7614 | __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex)); |
| 7615 | // Post-increment r4 with kPointerSize on each iteration. |
| 7616 | __ str(r3, MemOperand(r4, kPointerSize, PostIndex)); |
| 7617 | __ sub(r1, r1, Operand(1)); |
| 7618 | __ cmp(r1, Operand(0)); |
| 7619 | __ b(ne, &loop); |
| 7620 | |
| 7621 | // Return and remove the on-stack parameters. |
| 7622 | __ bind(&done); |
| 7623 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7624 | __ Ret(); |
| 7625 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7626 | // Do the runtime call to allocate the arguments object. |
| 7627 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7628 | __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7629 | } |
| 7630 | |
| 7631 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame^] | 7632 | void RegExpExecStub::Generate(MacroAssembler* masm) { |
| 7633 | // Just jump directly to runtime if native RegExp is not selected at compile |
| 7634 | // time or if regexp entry in generated code is turned off runtime switch or |
| 7635 | // at compilation. |
| 7636 | #ifndef V8_NATIVE_REGEXP |
| 7637 | __ TailCallRuntime(Runtime::kRegExpExec, 4, 1); |
| 7638 | #else // V8_NATIVE_REGEXP |
| 7639 | if (!FLAG_regexp_entry_native) { |
| 7640 | __ TailCallRuntime(Runtime::kRegExpExec, 4, 1); |
| 7641 | return; |
| 7642 | } |
| 7643 | |
| 7644 | // Stack frame on entry. |
| 7645 | // sp[0]: last_match_info (expected JSArray) |
| 7646 | // sp[4]: previous index |
| 7647 | // sp[8]: subject string |
| 7648 | // sp[12]: JSRegExp object |
| 7649 | |
| 7650 | static const int kLastMatchInfoOffset = 0 * kPointerSize; |
| 7651 | static const int kPreviousIndexOffset = 1 * kPointerSize; |
| 7652 | static const int kSubjectOffset = 2 * kPointerSize; |
| 7653 | static const int kJSRegExpOffset = 3 * kPointerSize; |
| 7654 | |
| 7655 | Label runtime, invoke_regexp; |
| 7656 | |
| 7657 | // Allocation of registers for this function. These are in callee save |
| 7658 | // registers and will be preserved by the call to the native RegExp code, as |
| 7659 | // this code is called using the normal C calling convention. When calling |
| 7660 | // directly from generated code the native RegExp code will not do a GC and |
| 7661 | // therefore the content of these registers are safe to use after the call. |
| 7662 | Register subject = r4; |
| 7663 | Register regexp_data = r5; |
| 7664 | Register last_match_info_elements = r6; |
| 7665 | |
| 7666 | // Ensure that a RegExp stack is allocated. |
| 7667 | ExternalReference address_of_regexp_stack_memory_address = |
| 7668 | ExternalReference::address_of_regexp_stack_memory_address(); |
| 7669 | ExternalReference address_of_regexp_stack_memory_size = |
| 7670 | ExternalReference::address_of_regexp_stack_memory_size(); |
| 7671 | __ mov(r0, Operand(address_of_regexp_stack_memory_size)); |
| 7672 | __ ldr(r0, MemOperand(r0, 0)); |
| 7673 | __ tst(r0, Operand(r0)); |
| 7674 | __ b(eq, &runtime); |
| 7675 | |
| 7676 | // Check that the first argument is a JSRegExp object. |
| 7677 | __ ldr(r0, MemOperand(sp, kJSRegExpOffset)); |
| 7678 | ASSERT_EQ(0, kSmiTag); |
| 7679 | __ tst(r0, Operand(kSmiTagMask)); |
| 7680 | __ b(eq, &runtime); |
| 7681 | __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE); |
| 7682 | __ b(ne, &runtime); |
| 7683 | |
| 7684 | // Check that the RegExp has been compiled (data contains a fixed array). |
| 7685 | __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset)); |
| 7686 | if (FLAG_debug_code) { |
| 7687 | __ tst(regexp_data, Operand(kSmiTagMask)); |
| 7688 | __ Check(nz, "Unexpected type for RegExp data, FixedArray expected"); |
| 7689 | __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE); |
| 7690 | __ Check(eq, "Unexpected type for RegExp data, FixedArray expected"); |
| 7691 | } |
| 7692 | |
| 7693 | // regexp_data: RegExp data (FixedArray) |
| 7694 | // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP. |
| 7695 | __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset)); |
| 7696 | __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP))); |
| 7697 | __ b(ne, &runtime); |
| 7698 | |
| 7699 | // regexp_data: RegExp data (FixedArray) |
| 7700 | // Check that the number of captures fit in the static offsets vector buffer. |
| 7701 | __ ldr(r2, |
| 7702 | FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset)); |
| 7703 | // Calculate number of capture registers (number_of_captures + 1) * 2. This |
| 7704 | // uses the asumption that smis are 2 * their untagged value. |
| 7705 | ASSERT_EQ(0, kSmiTag); |
| 7706 | ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize); |
| 7707 | __ add(r2, r2, Operand(2)); // r2 was a smi. |
| 7708 | // Check that the static offsets vector buffer is large enough. |
| 7709 | __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize)); |
| 7710 | __ b(hi, &runtime); |
| 7711 | |
| 7712 | // r2: Number of capture registers |
| 7713 | // regexp_data: RegExp data (FixedArray) |
| 7714 | // Check that the second argument is a string. |
| 7715 | __ ldr(subject, MemOperand(sp, kSubjectOffset)); |
| 7716 | __ tst(subject, Operand(kSmiTagMask)); |
| 7717 | __ b(eq, &runtime); |
| 7718 | Condition is_string = masm->IsObjectStringType(subject, r0); |
| 7719 | __ b(NegateCondition(is_string), &runtime); |
| 7720 | // Get the length of the string to r3. |
| 7721 | __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset)); |
| 7722 | |
| 7723 | // r2: Number of capture registers |
| 7724 | // r3: Length of subject string |
| 7725 | // subject: Subject string |
| 7726 | // regexp_data: RegExp data (FixedArray) |
| 7727 | // Check that the third argument is a positive smi less than the subject |
| 7728 | // string length. A negative value will be greater (unsigned comparison). |
| 7729 | __ ldr(r0, MemOperand(sp, kPreviousIndexOffset)); |
| 7730 | __ cmp(r3, Operand(r0, ASR, kSmiTagSize + kSmiShiftSize)); |
| 7731 | __ b(ls, &runtime); |
| 7732 | |
| 7733 | // r2: Number of capture registers |
| 7734 | // subject: Subject string |
| 7735 | // regexp_data: RegExp data (FixedArray) |
| 7736 | // Check that the fourth object is a JSArray object. |
| 7737 | __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset)); |
| 7738 | __ tst(r0, Operand(kSmiTagMask)); |
| 7739 | __ b(eq, &runtime); |
| 7740 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
| 7741 | __ b(ne, &runtime); |
| 7742 | // Check that the JSArray is in fast case. |
| 7743 | __ ldr(last_match_info_elements, |
| 7744 | FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 7745 | __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset)); |
| 7746 | __ cmp(r0, Operand(Factory::fixed_array_map())); |
| 7747 | __ b(ne, &runtime); |
| 7748 | // Check that the last match info has space for the capture registers and the |
| 7749 | // additional information. |
| 7750 | __ ldr(r0, |
| 7751 | FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset)); |
| 7752 | __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead)); |
| 7753 | __ cmp(r2, r0); |
| 7754 | __ b(gt, &runtime); |
| 7755 | |
| 7756 | // subject: Subject string |
| 7757 | // regexp_data: RegExp data (FixedArray) |
| 7758 | // Check the representation and encoding of the subject string. |
| 7759 | Label seq_string; |
| 7760 | const int kStringRepresentationEncodingMask = |
| 7761 | kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask; |
| 7762 | __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset)); |
| 7763 | __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset)); |
| 7764 | __ and_(r1, r0, Operand(kStringRepresentationEncodingMask)); |
| 7765 | // First check for sequential string. |
| 7766 | ASSERT_EQ(0, kStringTag); |
| 7767 | ASSERT_EQ(0, kSeqStringTag); |
| 7768 | __ tst(r1, Operand(kIsNotStringMask | kStringRepresentationMask)); |
| 7769 | __ b(eq, &seq_string); |
| 7770 | |
| 7771 | // subject: Subject string |
| 7772 | // regexp_data: RegExp data (FixedArray) |
| 7773 | // Check for flat cons string. |
| 7774 | // A flat cons string is a cons string where the second part is the empty |
| 7775 | // string. In that case the subject string is just the first part of the cons |
| 7776 | // string. Also in this case the first part of the cons string is known to be |
| 7777 | // a sequential string or an external string. |
| 7778 | __ and_(r0, r0, Operand(kStringRepresentationMask)); |
| 7779 | __ cmp(r0, Operand(kConsStringTag)); |
| 7780 | __ b(ne, &runtime); |
| 7781 | __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset)); |
| 7782 | __ LoadRoot(r1, Heap::kEmptyStringRootIndex); |
| 7783 | __ cmp(r0, r1); |
| 7784 | __ b(ne, &runtime); |
| 7785 | __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset)); |
| 7786 | __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset)); |
| 7787 | __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset)); |
| 7788 | ASSERT_EQ(0, kSeqStringTag); |
| 7789 | __ tst(r0, Operand(kStringRepresentationMask)); |
| 7790 | __ b(nz, &runtime); |
| 7791 | __ and_(r1, r0, Operand(kStringRepresentationEncodingMask)); |
| 7792 | |
| 7793 | __ bind(&seq_string); |
| 7794 | // r1: suject string type & kStringRepresentationEncodingMask |
| 7795 | // subject: Subject string |
| 7796 | // regexp_data: RegExp data (FixedArray) |
| 7797 | // Check that the irregexp code has been generated for an ascii string. If |
| 7798 | // it has, the field contains a code object otherwise it contains the hole. |
| 7799 | #ifdef DEBUG |
| 7800 | const int kSeqAsciiString = kStringTag | kSeqStringTag | kAsciiStringTag; |
| 7801 | const int kSeqTwoByteString = kStringTag | kSeqStringTag | kTwoByteStringTag; |
| 7802 | CHECK_EQ(4, kSeqAsciiString); |
| 7803 | CHECK_EQ(0, kSeqTwoByteString); |
| 7804 | #endif |
| 7805 | // Find the code object based on the assumptions above. |
| 7806 | __ mov(r3, Operand(r1, ASR, 2), SetCC); |
| 7807 | __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne); |
| 7808 | __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq); |
| 7809 | |
| 7810 | // Check that the irregexp code has been generated for the actual string |
| 7811 | // encoding. If it has, the field contains a code object otherwise it contains |
| 7812 | // the hole. |
| 7813 | __ CompareObjectType(r7, r0, r0, CODE_TYPE); |
| 7814 | __ b(ne, &runtime); |
| 7815 | |
| 7816 | // r3: encoding of subject string (1 if ascii, 0 if two_byte); |
| 7817 | // r7: code |
| 7818 | // subject: Subject string |
| 7819 | // regexp_data: RegExp data (FixedArray) |
| 7820 | // Load used arguments before starting to push arguments for call to native |
| 7821 | // RegExp code to avoid handling changing stack height. |
| 7822 | __ ldr(r1, MemOperand(sp, kPreviousIndexOffset)); |
| 7823 | __ mov(r1, Operand(r1, ASR, kSmiTagSize)); |
| 7824 | |
| 7825 | // r1: previous index |
| 7826 | // r3: encoding of subject string (1 if ascii, 0 if two_byte); |
| 7827 | // r7: code |
| 7828 | // subject: Subject string |
| 7829 | // regexp_data: RegExp data (FixedArray) |
| 7830 | // All checks done. Now push arguments for native regexp code. |
| 7831 | __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2); |
| 7832 | |
| 7833 | static const int kRegExpExecuteArguments = 7; |
| 7834 | __ push(lr); |
| 7835 | __ PrepareCallCFunction(kRegExpExecuteArguments, r0); |
| 7836 | |
| 7837 | // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript. |
| 7838 | __ mov(r0, Operand(1)); |
| 7839 | __ str(r0, MemOperand(sp, 2 * kPointerSize)); |
| 7840 | |
| 7841 | // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area. |
| 7842 | __ mov(r0, Operand(address_of_regexp_stack_memory_address)); |
| 7843 | __ ldr(r0, MemOperand(r0, 0)); |
| 7844 | __ mov(r2, Operand(address_of_regexp_stack_memory_size)); |
| 7845 | __ ldr(r2, MemOperand(r2, 0)); |
| 7846 | __ add(r0, r0, Operand(r2)); |
| 7847 | __ str(r0, MemOperand(sp, 1 * kPointerSize)); |
| 7848 | |
| 7849 | // Argument 5 (sp[0]): static offsets vector buffer. |
| 7850 | __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector())); |
| 7851 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 7852 | |
| 7853 | // For arguments 4 and 3 get string length, calculate start of string data and |
| 7854 | // calculate the shift of the index (0 for ASCII and 1 for two byte). |
| 7855 | __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset)); |
| 7856 | ASSERT_EQ(SeqAsciiString::kHeaderSize, SeqTwoByteString::kHeaderSize); |
| 7857 | __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 7858 | __ eor(r3, r3, Operand(1)); |
| 7859 | // Argument 4 (r3): End of string data |
| 7860 | // Argument 3 (r2): Start of string data |
| 7861 | __ add(r2, r9, Operand(r1, LSL, r3)); |
| 7862 | __ add(r3, r9, Operand(r0, LSL, r3)); |
| 7863 | |
| 7864 | // Argument 2 (r1): Previous index. |
| 7865 | // Already there |
| 7866 | |
| 7867 | // Argument 1 (r0): Subject string. |
| 7868 | __ mov(r0, subject); |
| 7869 | |
| 7870 | // Locate the code entry and call it. |
| 7871 | __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 7872 | __ CallCFunction(r7, kRegExpExecuteArguments); |
| 7873 | __ pop(lr); |
| 7874 | |
| 7875 | // r0: result |
| 7876 | // subject: subject string (callee saved) |
| 7877 | // regexp_data: RegExp data (callee saved) |
| 7878 | // last_match_info_elements: Last match info elements (callee saved) |
| 7879 | |
| 7880 | // Check the result. |
| 7881 | Label success; |
| 7882 | __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS)); |
| 7883 | __ b(eq, &success); |
| 7884 | Label failure; |
| 7885 | __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE)); |
| 7886 | __ b(eq, &failure); |
| 7887 | __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION)); |
| 7888 | // If not exception it can only be retry. Handle that in the runtime system. |
| 7889 | __ b(ne, &runtime); |
| 7890 | // Result must now be exception. If there is no pending exception already a |
| 7891 | // stack overflow (on the backtrack stack) was detected in RegExp code but |
| 7892 | // haven't created the exception yet. Handle that in the runtime system. |
| 7893 | // TODO(592): Rerunning the RegExp to get the stack overflow exception. |
| 7894 | __ mov(r0, Operand(ExternalReference::the_hole_value_location())); |
| 7895 | __ ldr(r0, MemOperand(r0, 0)); |
| 7896 | __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 7897 | __ ldr(r1, MemOperand(r1, 0)); |
| 7898 | __ cmp(r0, r1); |
| 7899 | __ b(eq, &runtime); |
| 7900 | __ bind(&failure); |
| 7901 | // For failure and exception return null. |
| 7902 | __ mov(r0, Operand(Factory::null_value())); |
| 7903 | __ add(sp, sp, Operand(4 * kPointerSize)); |
| 7904 | __ Ret(); |
| 7905 | |
| 7906 | // Process the result from the native regexp code. |
| 7907 | __ bind(&success); |
| 7908 | __ ldr(r1, |
| 7909 | FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset)); |
| 7910 | // Calculate number of capture registers (number_of_captures + 1) * 2. |
| 7911 | ASSERT_EQ(0, kSmiTag); |
| 7912 | ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize); |
| 7913 | __ add(r1, r1, Operand(2)); // r1 was a smi. |
| 7914 | |
| 7915 | // r1: number of capture registers |
| 7916 | // r4: subject string |
| 7917 | // Store the capture count. |
| 7918 | __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi. |
| 7919 | __ str(r2, FieldMemOperand(last_match_info_elements, |
| 7920 | RegExpImpl::kLastCaptureCountOffset)); |
| 7921 | // Store last subject and last input. |
| 7922 | __ mov(r3, last_match_info_elements); // Moved up to reduce latency. |
| 7923 | __ mov(r2, Operand(RegExpImpl::kLastSubjectOffset)); // Ditto. |
| 7924 | __ str(subject, |
| 7925 | FieldMemOperand(last_match_info_elements, |
| 7926 | RegExpImpl::kLastSubjectOffset)); |
| 7927 | __ RecordWrite(r3, r2, r7); |
| 7928 | __ str(subject, |
| 7929 | FieldMemOperand(last_match_info_elements, |
| 7930 | RegExpImpl::kLastInputOffset)); |
| 7931 | __ mov(r3, last_match_info_elements); |
| 7932 | __ mov(r2, Operand(RegExpImpl::kLastInputOffset)); |
| 7933 | __ RecordWrite(r3, r2, r7); |
| 7934 | |
| 7935 | // Get the static offsets vector filled by the native regexp code. |
| 7936 | ExternalReference address_of_static_offsets_vector = |
| 7937 | ExternalReference::address_of_static_offsets_vector(); |
| 7938 | __ mov(r2, Operand(address_of_static_offsets_vector)); |
| 7939 | |
| 7940 | // r1: number of capture registers |
| 7941 | // r2: offsets vector |
| 7942 | Label next_capture, done; |
| 7943 | // Capture register counter starts from number of capture registers and |
| 7944 | // counts down until wraping after zero. |
| 7945 | __ add(r0, |
| 7946 | last_match_info_elements, |
| 7947 | Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag)); |
| 7948 | __ bind(&next_capture); |
| 7949 | __ sub(r1, r1, Operand(1), SetCC); |
| 7950 | __ b(mi, &done); |
| 7951 | // Read the value from the static offsets vector buffer. |
| 7952 | __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex)); |
| 7953 | // Store the smi value in the last match info. |
| 7954 | __ mov(r3, Operand(r3, LSL, kSmiTagSize)); |
| 7955 | __ str(r3, MemOperand(r0, kPointerSize, PostIndex)); |
| 7956 | __ jmp(&next_capture); |
| 7957 | __ bind(&done); |
| 7958 | |
| 7959 | // Return last match info. |
| 7960 | __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset)); |
| 7961 | __ add(sp, sp, Operand(4 * kPointerSize)); |
| 7962 | __ Ret(); |
| 7963 | |
| 7964 | // Do the runtime call to execute the regexp. |
| 7965 | __ bind(&runtime); |
| 7966 | __ TailCallRuntime(Runtime::kRegExpExec, 4, 1); |
| 7967 | #endif // V8_NATIVE_REGEXP |
| 7968 | } |
| 7969 | |
| 7970 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7971 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 7972 | Label slow; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7973 | |
| 7974 | // If the receiver might be a value (string, number or boolean) check for this |
| 7975 | // and box it if it is. |
| 7976 | if (ReceiverMightBeValue()) { |
| 7977 | // Get the receiver from the stack. |
| 7978 | // function, receiver [, arguments] |
| 7979 | Label receiver_is_value, receiver_is_js_object; |
| 7980 | __ ldr(r1, MemOperand(sp, argc_ * kPointerSize)); |
| 7981 | |
| 7982 | // Check if receiver is a smi (which is a number value). |
| 7983 | __ BranchOnSmi(r1, &receiver_is_value); |
| 7984 | |
| 7985 | // Check if the receiver is a valid JS object. |
| 7986 | __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 7987 | __ b(ge, &receiver_is_js_object); |
| 7988 | |
| 7989 | // Call the runtime to box the value. |
| 7990 | __ bind(&receiver_is_value); |
| 7991 | __ EnterInternalFrame(); |
| 7992 | __ push(r1); |
| 7993 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
| 7994 | __ LeaveInternalFrame(); |
| 7995 | __ str(r0, MemOperand(sp, argc_ * kPointerSize)); |
| 7996 | |
| 7997 | __ bind(&receiver_is_js_object); |
| 7998 | } |
| 7999 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8000 | // Get the function to call from the stack. |
| 8001 | // function, receiver [, arguments] |
| 8002 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 8003 | |
| 8004 | // Check that the function is really a JavaScript function. |
| 8005 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8006 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8007 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8008 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8009 | __ b(ne, &slow); |
| 8010 | |
| 8011 | // Fast-case: Invoke the function now. |
| 8012 | // r1: pushed function |
| 8013 | ParameterCount actual(argc_); |
| 8014 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 8015 | |
| 8016 | // Slow-case: Non-function called. |
| 8017 | __ bind(&slow); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8018 | // CALL_NON_FUNCTION expects the non-function callee as receiver (instead |
| 8019 | // of the original receiver from the call site). |
| 8020 | __ str(r1, MemOperand(sp, argc_ * kPointerSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8021 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 8022 | __ mov(r2, Operand(0)); |
| 8023 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 8024 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 8025 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8026 | } |
| 8027 | |
| 8028 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8029 | // Unfortunately you have to run without snapshots to see most of these |
| 8030 | // names in the profile since most compare stubs end up in the snapshot. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8031 | const char* CompareStub::GetName() { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8032 | if (name_ != NULL) return name_; |
| 8033 | const int kMaxNameLength = 100; |
| 8034 | name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength); |
| 8035 | if (name_ == NULL) return "OOM"; |
| 8036 | |
| 8037 | const char* cc_name; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8038 | switch (cc_) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8039 | case lt: cc_name = "LT"; break; |
| 8040 | case gt: cc_name = "GT"; break; |
| 8041 | case le: cc_name = "LE"; break; |
| 8042 | case ge: cc_name = "GE"; break; |
| 8043 | case eq: cc_name = "EQ"; break; |
| 8044 | case ne: cc_name = "NE"; break; |
| 8045 | default: cc_name = "UnknownCondition"; break; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8046 | } |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8047 | |
| 8048 | const char* strict_name = ""; |
| 8049 | if (strict_ && (cc_ == eq || cc_ == ne)) { |
| 8050 | strict_name = "_STRICT"; |
| 8051 | } |
| 8052 | |
| 8053 | const char* never_nan_nan_name = ""; |
| 8054 | if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) { |
| 8055 | never_nan_nan_name = "_NO_NAN"; |
| 8056 | } |
| 8057 | |
| 8058 | const char* include_number_compare_name = ""; |
| 8059 | if (!include_number_compare_) { |
| 8060 | include_number_compare_name = "_NO_NUMBER"; |
| 8061 | } |
| 8062 | |
| 8063 | OS::SNPrintF(Vector<char>(name_, kMaxNameLength), |
| 8064 | "CompareStub_%s%s%s%s", |
| 8065 | cc_name, |
| 8066 | strict_name, |
| 8067 | never_nan_nan_name, |
| 8068 | include_number_compare_name); |
| 8069 | return name_; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8070 | } |
| 8071 | |
| 8072 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8073 | int CompareStub::MinorKey() { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 8074 | // Encode the three parameters in a unique 16 bit value. To avoid duplicate |
| 8075 | // stubs the never NaN NaN condition is only taken into account if the |
| 8076 | // condition is equals. |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8077 | ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 8078 | return ConditionField::encode(static_cast<unsigned>(cc_) >> 28) |
| 8079 | | StrictField::encode(strict_) |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8080 | | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false) |
| 8081 | | IncludeNumberCompareField::encode(include_number_compare_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8082 | } |
| 8083 | |
| 8084 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8085 | void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm, |
| 8086 | Register dest, |
| 8087 | Register src, |
| 8088 | Register count, |
| 8089 | Register scratch, |
| 8090 | bool ascii) { |
| 8091 | Label loop; |
| 8092 | Label done; |
| 8093 | // This loop just copies one character at a time, as it is only used for very |
| 8094 | // short strings. |
| 8095 | if (!ascii) { |
| 8096 | __ add(count, count, Operand(count), SetCC); |
| 8097 | } else { |
| 8098 | __ cmp(count, Operand(0)); |
| 8099 | } |
| 8100 | __ b(eq, &done); |
| 8101 | |
| 8102 | __ bind(&loop); |
| 8103 | __ ldrb(scratch, MemOperand(src, 1, PostIndex)); |
| 8104 | // Perform sub between load and dependent store to get the load time to |
| 8105 | // complete. |
| 8106 | __ sub(count, count, Operand(1), SetCC); |
| 8107 | __ strb(scratch, MemOperand(dest, 1, PostIndex)); |
| 8108 | // last iteration. |
| 8109 | __ b(gt, &loop); |
| 8110 | |
| 8111 | __ bind(&done); |
| 8112 | } |
| 8113 | |
| 8114 | |
| 8115 | enum CopyCharactersFlags { |
| 8116 | COPY_ASCII = 1, |
| 8117 | DEST_ALWAYS_ALIGNED = 2 |
| 8118 | }; |
| 8119 | |
| 8120 | |
| 8121 | void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm, |
| 8122 | Register dest, |
| 8123 | Register src, |
| 8124 | Register count, |
| 8125 | Register scratch1, |
| 8126 | Register scratch2, |
| 8127 | Register scratch3, |
| 8128 | Register scratch4, |
| 8129 | Register scratch5, |
| 8130 | int flags) { |
| 8131 | bool ascii = (flags & COPY_ASCII) != 0; |
| 8132 | bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0; |
| 8133 | |
| 8134 | if (dest_always_aligned && FLAG_debug_code) { |
| 8135 | // Check that destination is actually word aligned if the flag says |
| 8136 | // that it is. |
| 8137 | __ tst(dest, Operand(kPointerAlignmentMask)); |
| 8138 | __ Check(eq, "Destination of copy not aligned."); |
| 8139 | } |
| 8140 | |
| 8141 | const int kReadAlignment = 4; |
| 8142 | const int kReadAlignmentMask = kReadAlignment - 1; |
| 8143 | // Ensure that reading an entire aligned word containing the last character |
| 8144 | // of a string will not read outside the allocated area (because we pad up |
| 8145 | // to kObjectAlignment). |
| 8146 | ASSERT(kObjectAlignment >= kReadAlignment); |
| 8147 | // Assumes word reads and writes are little endian. |
| 8148 | // Nothing to do for zero characters. |
| 8149 | Label done; |
| 8150 | if (!ascii) { |
| 8151 | __ add(count, count, Operand(count), SetCC); |
| 8152 | } else { |
| 8153 | __ cmp(count, Operand(0)); |
| 8154 | } |
| 8155 | __ b(eq, &done); |
| 8156 | |
| 8157 | // Assume that you cannot read (or write) unaligned. |
| 8158 | Label byte_loop; |
| 8159 | // Must copy at least eight bytes, otherwise just do it one byte at a time. |
| 8160 | __ cmp(count, Operand(8)); |
| 8161 | __ add(count, dest, Operand(count)); |
| 8162 | Register limit = count; // Read until src equals this. |
| 8163 | __ b(lt, &byte_loop); |
| 8164 | |
| 8165 | if (!dest_always_aligned) { |
| 8166 | // Align dest by byte copying. Copies between zero and three bytes. |
| 8167 | __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC); |
| 8168 | Label dest_aligned; |
| 8169 | __ b(eq, &dest_aligned); |
| 8170 | __ cmp(scratch4, Operand(2)); |
| 8171 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex)); |
| 8172 | __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le); |
| 8173 | __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt); |
| 8174 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 8175 | __ strb(scratch2, MemOperand(dest, 1, PostIndex), le); |
| 8176 | __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt); |
| 8177 | __ bind(&dest_aligned); |
| 8178 | } |
| 8179 | |
| 8180 | Label simple_loop; |
| 8181 | |
| 8182 | __ sub(scratch4, dest, Operand(src)); |
| 8183 | __ and_(scratch4, scratch4, Operand(0x03), SetCC); |
| 8184 | __ b(eq, &simple_loop); |
| 8185 | // Shift register is number of bits in a source word that |
| 8186 | // must be combined with bits in the next source word in order |
| 8187 | // to create a destination word. |
| 8188 | |
| 8189 | // Complex loop for src/dst that are not aligned the same way. |
| 8190 | { |
| 8191 | Label loop; |
| 8192 | __ mov(scratch4, Operand(scratch4, LSL, 3)); |
| 8193 | Register left_shift = scratch4; |
| 8194 | __ and_(src, src, Operand(~3)); // Round down to load previous word. |
| 8195 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 8196 | // Store the "shift" most significant bits of scratch in the least |
| 8197 | // signficant bits (i.e., shift down by (32-shift)). |
| 8198 | __ rsb(scratch2, left_shift, Operand(32)); |
| 8199 | Register right_shift = scratch2; |
| 8200 | __ mov(scratch1, Operand(scratch1, LSR, right_shift)); |
| 8201 | |
| 8202 | __ bind(&loop); |
| 8203 | __ ldr(scratch3, MemOperand(src, 4, PostIndex)); |
| 8204 | __ sub(scratch5, limit, Operand(dest)); |
| 8205 | __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift)); |
| 8206 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 8207 | __ mov(scratch1, Operand(scratch3, LSR, right_shift)); |
| 8208 | // Loop if four or more bytes left to copy. |
| 8209 | // Compare to eight, because we did the subtract before increasing dst. |
| 8210 | __ sub(scratch5, scratch5, Operand(8), SetCC); |
| 8211 | __ b(ge, &loop); |
| 8212 | } |
| 8213 | // There is now between zero and three bytes left to copy (negative that |
| 8214 | // number is in scratch5), and between one and three bytes already read into |
| 8215 | // scratch1 (eight times that number in scratch4). We may have read past |
| 8216 | // the end of the string, but because objects are aligned, we have not read |
| 8217 | // past the end of the object. |
| 8218 | // Find the minimum of remaining characters to move and preloaded characters |
| 8219 | // and write those as bytes. |
| 8220 | __ add(scratch5, scratch5, Operand(4), SetCC); |
| 8221 | __ b(eq, &done); |
| 8222 | __ cmp(scratch4, Operand(scratch5, LSL, 3), ne); |
| 8223 | // Move minimum of bytes read and bytes left to copy to scratch4. |
| 8224 | __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt); |
| 8225 | // Between one and three (value in scratch5) characters already read into |
| 8226 | // scratch ready to write. |
| 8227 | __ cmp(scratch5, Operand(2)); |
| 8228 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 8229 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge); |
| 8230 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge); |
| 8231 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt); |
| 8232 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt); |
| 8233 | // Copy any remaining bytes. |
| 8234 | __ b(&byte_loop); |
| 8235 | |
| 8236 | // Simple loop. |
| 8237 | // Copy words from src to dst, until less than four bytes left. |
| 8238 | // Both src and dest are word aligned. |
| 8239 | __ bind(&simple_loop); |
| 8240 | { |
| 8241 | Label loop; |
| 8242 | __ bind(&loop); |
| 8243 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 8244 | __ sub(scratch3, limit, Operand(dest)); |
| 8245 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 8246 | // Compare to 8, not 4, because we do the substraction before increasing |
| 8247 | // dest. |
| 8248 | __ cmp(scratch3, Operand(8)); |
| 8249 | __ b(ge, &loop); |
| 8250 | } |
| 8251 | |
| 8252 | // Copy bytes from src to dst until dst hits limit. |
| 8253 | __ bind(&byte_loop); |
| 8254 | __ cmp(dest, Operand(limit)); |
| 8255 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt); |
| 8256 | __ b(ge, &done); |
| 8257 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 8258 | __ b(&byte_loop); |
| 8259 | |
| 8260 | __ bind(&done); |
| 8261 | } |
| 8262 | |
| 8263 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8264 | void StringStubBase::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm, |
| 8265 | Register c1, |
| 8266 | Register c2, |
| 8267 | Register scratch1, |
| 8268 | Register scratch2, |
| 8269 | Register scratch3, |
| 8270 | Register scratch4, |
| 8271 | Register scratch5, |
| 8272 | Label* not_found) { |
| 8273 | // Register scratch3 is the general scratch register in this function. |
| 8274 | Register scratch = scratch3; |
| 8275 | |
| 8276 | // Make sure that both characters are not digits as such strings has a |
| 8277 | // different hash algorithm. Don't try to look for these in the symbol table. |
| 8278 | Label not_array_index; |
| 8279 | __ sub(scratch, c1, Operand(static_cast<int>('0'))); |
| 8280 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 8281 | __ b(hi, ¬_array_index); |
| 8282 | __ sub(scratch, c2, Operand(static_cast<int>('0'))); |
| 8283 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 8284 | |
| 8285 | // If check failed combine both characters into single halfword. |
| 8286 | // This is required by the contract of the method: code at the |
| 8287 | // not_found branch expects this combination in c1 register |
| 8288 | __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls); |
| 8289 | __ b(ls, not_found); |
| 8290 | |
| 8291 | __ bind(¬_array_index); |
| 8292 | // Calculate the two character string hash. |
| 8293 | Register hash = scratch1; |
| 8294 | GenerateHashInit(masm, hash, c1); |
| 8295 | GenerateHashAddCharacter(masm, hash, c2); |
| 8296 | GenerateHashGetHash(masm, hash); |
| 8297 | |
| 8298 | // Collect the two characters in a register. |
| 8299 | Register chars = c1; |
| 8300 | __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte)); |
| 8301 | |
| 8302 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 8303 | // hash: hash of two character string. |
| 8304 | |
| 8305 | // Load symbol table |
| 8306 | // Load address of first element of the symbol table. |
| 8307 | Register symbol_table = c2; |
| 8308 | __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex); |
| 8309 | |
| 8310 | // Load undefined value |
| 8311 | Register undefined = scratch4; |
| 8312 | __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex); |
| 8313 | |
| 8314 | // Calculate capacity mask from the symbol table capacity. |
| 8315 | Register mask = scratch2; |
| 8316 | __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset)); |
| 8317 | __ mov(mask, Operand(mask, ASR, 1)); |
| 8318 | __ sub(mask, mask, Operand(1)); |
| 8319 | |
| 8320 | // Calculate untagged address of the first element of the symbol table. |
| 8321 | Register first_symbol_table_element = symbol_table; |
| 8322 | __ add(first_symbol_table_element, symbol_table, |
| 8323 | Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag)); |
| 8324 | |
| 8325 | // Registers |
| 8326 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 8327 | // hash: hash of two character string |
| 8328 | // mask: capacity mask |
| 8329 | // first_symbol_table_element: address of the first element of |
| 8330 | // the symbol table |
| 8331 | // scratch: - |
| 8332 | |
| 8333 | // Perform a number of probes in the symbol table. |
| 8334 | static const int kProbes = 4; |
| 8335 | Label found_in_symbol_table; |
| 8336 | Label next_probe[kProbes]; |
| 8337 | for (int i = 0; i < kProbes; i++) { |
| 8338 | Register candidate = scratch5; // Scratch register contains candidate. |
| 8339 | |
| 8340 | // Calculate entry in symbol table. |
| 8341 | if (i > 0) { |
| 8342 | __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i))); |
| 8343 | } else { |
| 8344 | __ mov(candidate, hash); |
| 8345 | } |
| 8346 | |
| 8347 | __ and_(candidate, candidate, Operand(mask)); |
| 8348 | |
| 8349 | // Load the entry from the symble table. |
| 8350 | ASSERT_EQ(1, SymbolTable::kEntrySize); |
| 8351 | __ ldr(candidate, |
| 8352 | MemOperand(first_symbol_table_element, |
| 8353 | candidate, |
| 8354 | LSL, |
| 8355 | kPointerSizeLog2)); |
| 8356 | |
| 8357 | // If entry is undefined no string with this hash can be found. |
| 8358 | __ cmp(candidate, undefined); |
| 8359 | __ b(eq, not_found); |
| 8360 | |
| 8361 | // If length is not 2 the string is not a candidate. |
| 8362 | __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset)); |
| 8363 | __ cmp(scratch, Operand(2)); |
| 8364 | __ b(ne, &next_probe[i]); |
| 8365 | |
| 8366 | // Check that the candidate is a non-external ascii string. |
| 8367 | __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset)); |
| 8368 | __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset)); |
| 8369 | __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, |
| 8370 | &next_probe[i]); |
| 8371 | |
| 8372 | // Check if the two characters match. |
| 8373 | // Assumes that word load is little endian. |
| 8374 | __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize)); |
| 8375 | __ cmp(chars, scratch); |
| 8376 | __ b(eq, &found_in_symbol_table); |
| 8377 | __ bind(&next_probe[i]); |
| 8378 | } |
| 8379 | |
| 8380 | // No matching 2 character string found by probing. |
| 8381 | __ jmp(not_found); |
| 8382 | |
| 8383 | // Scratch register contains result when we fall through to here. |
| 8384 | Register result = scratch; |
| 8385 | __ bind(&found_in_symbol_table); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8386 | __ Move(r0, result); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8387 | } |
| 8388 | |
| 8389 | |
| 8390 | void StringStubBase::GenerateHashInit(MacroAssembler* masm, |
| 8391 | Register hash, |
| 8392 | Register character) { |
| 8393 | // hash = character + (character << 10); |
| 8394 | __ add(hash, character, Operand(character, LSL, 10)); |
| 8395 | // hash ^= hash >> 6; |
| 8396 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 8397 | } |
| 8398 | |
| 8399 | |
| 8400 | void StringStubBase::GenerateHashAddCharacter(MacroAssembler* masm, |
| 8401 | Register hash, |
| 8402 | Register character) { |
| 8403 | // hash += character; |
| 8404 | __ add(hash, hash, Operand(character)); |
| 8405 | // hash += hash << 10; |
| 8406 | __ add(hash, hash, Operand(hash, LSL, 10)); |
| 8407 | // hash ^= hash >> 6; |
| 8408 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 8409 | } |
| 8410 | |
| 8411 | |
| 8412 | void StringStubBase::GenerateHashGetHash(MacroAssembler* masm, |
| 8413 | Register hash) { |
| 8414 | // hash += hash << 3; |
| 8415 | __ add(hash, hash, Operand(hash, LSL, 3)); |
| 8416 | // hash ^= hash >> 11; |
| 8417 | __ eor(hash, hash, Operand(hash, ASR, 11)); |
| 8418 | // hash += hash << 15; |
| 8419 | __ add(hash, hash, Operand(hash, LSL, 15), SetCC); |
| 8420 | |
| 8421 | // if (hash == 0) hash = 27; |
| 8422 | __ mov(hash, Operand(27), LeaveCC, nz); |
| 8423 | } |
| 8424 | |
| 8425 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8426 | void SubStringStub::Generate(MacroAssembler* masm) { |
| 8427 | Label runtime; |
| 8428 | |
| 8429 | // Stack frame on entry. |
| 8430 | // lr: return address |
| 8431 | // sp[0]: to |
| 8432 | // sp[4]: from |
| 8433 | // sp[8]: string |
| 8434 | |
| 8435 | // This stub is called from the native-call %_SubString(...), so |
| 8436 | // nothing can be assumed about the arguments. It is tested that: |
| 8437 | // "string" is a sequential string, |
| 8438 | // both "from" and "to" are smis, and |
| 8439 | // 0 <= from <= to <= string.length. |
| 8440 | // If any of these assumptions fail, we call the runtime system. |
| 8441 | |
| 8442 | static const int kToOffset = 0 * kPointerSize; |
| 8443 | static const int kFromOffset = 1 * kPointerSize; |
| 8444 | static const int kStringOffset = 2 * kPointerSize; |
| 8445 | |
| 8446 | |
| 8447 | // Check bounds and smi-ness. |
| 8448 | __ ldr(r7, MemOperand(sp, kToOffset)); |
| 8449 | __ ldr(r6, MemOperand(sp, kFromOffset)); |
| 8450 | ASSERT_EQ(0, kSmiTag); |
| 8451 | ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize); |
| 8452 | // I.e., arithmetic shift right by one un-smi-tags. |
| 8453 | __ mov(r2, Operand(r7, ASR, 1), SetCC); |
| 8454 | __ mov(r3, Operand(r6, ASR, 1), SetCC, cc); |
| 8455 | // If either r2 or r6 had the smi tag bit set, then carry is set now. |
| 8456 | __ b(cs, &runtime); // Either "from" or "to" is not a smi. |
| 8457 | __ b(mi, &runtime); // From is negative. |
| 8458 | |
| 8459 | __ sub(r2, r2, Operand(r3), SetCC); |
| 8460 | __ b(mi, &runtime); // Fail if from > to. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8461 | // Special handling of sub-strings of length 1 and 2. One character strings |
| 8462 | // are handled in the runtime system (looked up in the single character |
| 8463 | // cache). Two character strings are looked for in the symbol cache. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8464 | __ cmp(r2, Operand(2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8465 | __ b(lt, &runtime); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8466 | |
| 8467 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8468 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8469 | // r6: from (smi) |
| 8470 | // r7: to (smi) |
| 8471 | |
| 8472 | // Make sure first argument is a sequential (or flat) string. |
| 8473 | __ ldr(r5, MemOperand(sp, kStringOffset)); |
| 8474 | ASSERT_EQ(0, kSmiTag); |
| 8475 | __ tst(r5, Operand(kSmiTagMask)); |
| 8476 | __ b(eq, &runtime); |
| 8477 | Condition is_string = masm->IsObjectStringType(r5, r1); |
| 8478 | __ b(NegateCondition(is_string), &runtime); |
| 8479 | |
| 8480 | // r1: instance type |
| 8481 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8482 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8483 | // r5: string |
| 8484 | // r6: from (smi) |
| 8485 | // r7: to (smi) |
| 8486 | Label seq_string; |
| 8487 | __ and_(r4, r1, Operand(kStringRepresentationMask)); |
| 8488 | ASSERT(kSeqStringTag < kConsStringTag); |
| 8489 | ASSERT(kExternalStringTag > kConsStringTag); |
| 8490 | __ cmp(r4, Operand(kConsStringTag)); |
| 8491 | __ b(gt, &runtime); // External strings go to runtime. |
| 8492 | __ b(lt, &seq_string); // Sequential strings are handled directly. |
| 8493 | |
| 8494 | // Cons string. Try to recurse (once) on the first substring. |
| 8495 | // (This adds a little more generality than necessary to handle flattened |
| 8496 | // cons strings, but not much). |
| 8497 | __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset)); |
| 8498 | __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset)); |
| 8499 | __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8500 | __ tst(r1, Operand(kStringRepresentationMask)); |
| 8501 | ASSERT_EQ(0, kSeqStringTag); |
| 8502 | __ b(ne, &runtime); // Cons and External strings go to runtime. |
| 8503 | |
| 8504 | // Definitly a sequential string. |
| 8505 | __ bind(&seq_string); |
| 8506 | |
| 8507 | // r1: instance type. |
| 8508 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8509 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8510 | // r5: string |
| 8511 | // r6: from (smi) |
| 8512 | // r7: to (smi) |
| 8513 | __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset)); |
| 8514 | __ cmp(r4, Operand(r7, ASR, 1)); |
| 8515 | __ b(lt, &runtime); // Fail if to > length. |
| 8516 | |
| 8517 | // r1: instance type. |
| 8518 | // r2: result string length. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8519 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8520 | // r5: string. |
| 8521 | // r6: from offset (smi) |
| 8522 | // Check for flat ascii string. |
| 8523 | Label non_ascii_flat; |
| 8524 | __ tst(r1, Operand(kStringEncodingMask)); |
| 8525 | ASSERT_EQ(0, kTwoByteStringTag); |
| 8526 | __ b(eq, &non_ascii_flat); |
| 8527 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8528 | Label result_longer_than_two; |
| 8529 | __ cmp(r2, Operand(2)); |
| 8530 | __ b(gt, &result_longer_than_two); |
| 8531 | |
| 8532 | // Sub string of length 2 requested. |
| 8533 | // Get the two characters forming the sub string. |
| 8534 | __ add(r5, r5, Operand(r3)); |
| 8535 | __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize)); |
| 8536 | __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1)); |
| 8537 | |
| 8538 | // Try to lookup two character string in symbol table. |
| 8539 | Label make_two_character_string; |
| 8540 | GenerateTwoCharacterSymbolTableProbe(masm, r3, r4, r1, r5, r6, r7, r9, |
| 8541 | &make_two_character_string); |
| 8542 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 8543 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 8544 | __ Ret(); |
| 8545 | |
| 8546 | // r2: result string length. |
| 8547 | // r3: two characters combined into halfword in little endian byte order. |
| 8548 | __ bind(&make_two_character_string); |
| 8549 | __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime); |
| 8550 | __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 8551 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 8552 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 8553 | __ Ret(); |
| 8554 | |
| 8555 | __ bind(&result_longer_than_two); |
| 8556 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8557 | // Allocate the result. |
| 8558 | __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime); |
| 8559 | |
| 8560 | // r0: result string. |
| 8561 | // r2: result string length. |
| 8562 | // r5: string. |
| 8563 | // r6: from offset (smi) |
| 8564 | // Locate first character of result. |
| 8565 | __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8566 | // Locate 'from' character of string. |
| 8567 | __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8568 | __ add(r5, r5, Operand(r6, ASR, 1)); |
| 8569 | |
| 8570 | // r0: result string. |
| 8571 | // r1: first character of result string. |
| 8572 | // r2: result string length. |
| 8573 | // r5: first character of sub string to copy. |
| 8574 | ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask); |
| 8575 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 8576 | COPY_ASCII | DEST_ALWAYS_ALIGNED); |
| 8577 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 8578 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 8579 | __ Ret(); |
| 8580 | |
| 8581 | __ bind(&non_ascii_flat); |
| 8582 | // r2: result string length. |
| 8583 | // r5: string. |
| 8584 | // r6: from offset (smi) |
| 8585 | // Check for flat two byte string. |
| 8586 | |
| 8587 | // Allocate the result. |
| 8588 | __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime); |
| 8589 | |
| 8590 | // r0: result string. |
| 8591 | // r2: result string length. |
| 8592 | // r5: string. |
| 8593 | // Locate first character of result. |
| 8594 | __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8595 | // Locate 'from' character of string. |
| 8596 | __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8597 | // As "from" is a smi it is 2 times the value which matches the size of a two |
| 8598 | // byte character. |
| 8599 | __ add(r5, r5, Operand(r6)); |
| 8600 | |
| 8601 | // r0: result string. |
| 8602 | // r1: first character of result. |
| 8603 | // r2: result length. |
| 8604 | // r5: first character of string to copy. |
| 8605 | ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask); |
| 8606 | GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 8607 | DEST_ALWAYS_ALIGNED); |
| 8608 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 8609 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 8610 | __ Ret(); |
| 8611 | |
| 8612 | // Just jump to runtime to create the sub string. |
| 8613 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8614 | __ TailCallRuntime(Runtime::kSubString, 3, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8615 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8616 | |
| 8617 | |
| 8618 | void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 8619 | Register left, |
| 8620 | Register right, |
| 8621 | Register scratch1, |
| 8622 | Register scratch2, |
| 8623 | Register scratch3, |
| 8624 | Register scratch4) { |
| 8625 | Label compare_lengths; |
| 8626 | // Find minimum length and length difference. |
| 8627 | __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset)); |
| 8628 | __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset)); |
| 8629 | __ sub(scratch3, scratch1, Operand(scratch2), SetCC); |
| 8630 | Register length_delta = scratch3; |
| 8631 | __ mov(scratch1, scratch2, LeaveCC, gt); |
| 8632 | Register min_length = scratch1; |
| 8633 | __ tst(min_length, Operand(min_length)); |
| 8634 | __ b(eq, &compare_lengths); |
| 8635 | |
| 8636 | // Setup registers so that we only need to increment one register |
| 8637 | // in the loop. |
| 8638 | __ add(scratch2, min_length, |
| 8639 | Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8640 | __ add(left, left, Operand(scratch2)); |
| 8641 | __ add(right, right, Operand(scratch2)); |
| 8642 | // Registers left and right points to the min_length character of strings. |
| 8643 | __ rsb(min_length, min_length, Operand(-1)); |
| 8644 | Register index = min_length; |
| 8645 | // Index starts at -min_length. |
| 8646 | |
| 8647 | { |
| 8648 | // Compare loop. |
| 8649 | Label loop; |
| 8650 | __ bind(&loop); |
| 8651 | // Compare characters. |
| 8652 | __ add(index, index, Operand(1), SetCC); |
| 8653 | __ ldrb(scratch2, MemOperand(left, index), ne); |
| 8654 | __ ldrb(scratch4, MemOperand(right, index), ne); |
| 8655 | // Skip to compare lengths with eq condition true. |
| 8656 | __ b(eq, &compare_lengths); |
| 8657 | __ cmp(scratch2, scratch4); |
| 8658 | __ b(eq, &loop); |
| 8659 | // Fallthrough with eq condition false. |
| 8660 | } |
| 8661 | // Compare lengths - strings up to min-length are equal. |
| 8662 | __ bind(&compare_lengths); |
| 8663 | ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0)); |
| 8664 | // Use zero length_delta as result. |
| 8665 | __ mov(r0, Operand(length_delta), SetCC, eq); |
| 8666 | // Fall through to here if characters compare not-equal. |
| 8667 | __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt); |
| 8668 | __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt); |
| 8669 | __ Ret(); |
| 8670 | } |
| 8671 | |
| 8672 | |
| 8673 | void StringCompareStub::Generate(MacroAssembler* masm) { |
| 8674 | Label runtime; |
| 8675 | |
| 8676 | // Stack frame on entry. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8677 | // sp[0]: right string |
| 8678 | // sp[4]: left string |
| 8679 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left |
| 8680 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8681 | |
| 8682 | Label not_same; |
| 8683 | __ cmp(r0, r1); |
| 8684 | __ b(ne, ¬_same); |
| 8685 | ASSERT_EQ(0, EQUAL); |
| 8686 | ASSERT_EQ(0, kSmiTag); |
| 8687 | __ mov(r0, Operand(Smi::FromInt(EQUAL))); |
| 8688 | __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2); |
| 8689 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8690 | __ Ret(); |
| 8691 | |
| 8692 | __ bind(¬_same); |
| 8693 | |
| 8694 | // Check that both objects are sequential ascii strings. |
| 8695 | __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime); |
| 8696 | |
| 8697 | // Compare flat ascii strings natively. Remove arguments from stack first. |
| 8698 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 8699 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8700 | GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5); |
| 8701 | |
| 8702 | // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater) |
| 8703 | // tagged as a small integer. |
| 8704 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8705 | __ TailCallRuntime(Runtime::kStringCompare, 2, 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8706 | } |
| 8707 | |
| 8708 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8709 | void StringAddStub::Generate(MacroAssembler* masm) { |
| 8710 | Label string_add_runtime; |
| 8711 | // Stack on entry: |
| 8712 | // sp[0]: second argument. |
| 8713 | // sp[4]: first argument. |
| 8714 | |
| 8715 | // Load the two arguments. |
| 8716 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument. |
| 8717 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument. |
| 8718 | |
| 8719 | // Make sure that both arguments are strings if not known in advance. |
| 8720 | if (string_check_) { |
| 8721 | ASSERT_EQ(0, kSmiTag); |
| 8722 | __ JumpIfEitherSmi(r0, r1, &string_add_runtime); |
| 8723 | // Load instance types. |
| 8724 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8725 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 8726 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8727 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 8728 | ASSERT_EQ(0, kStringTag); |
| 8729 | // If either is not a string, go to runtime. |
| 8730 | __ tst(r4, Operand(kIsNotStringMask)); |
| 8731 | __ tst(r5, Operand(kIsNotStringMask), eq); |
| 8732 | __ b(ne, &string_add_runtime); |
| 8733 | } |
| 8734 | |
| 8735 | // Both arguments are strings. |
| 8736 | // r0: first string |
| 8737 | // r1: second string |
| 8738 | // r4: first string instance type (if string_check_) |
| 8739 | // r5: second string instance type (if string_check_) |
| 8740 | { |
| 8741 | Label strings_not_empty; |
| 8742 | // Check if either of the strings are empty. In that case return the other. |
| 8743 | __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset)); |
| 8744 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
| 8745 | __ cmp(r2, Operand(0)); // Test if first string is empty. |
| 8746 | __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second. |
| 8747 | __ cmp(r3, Operand(0), ne); // Else test if second string is empty. |
| 8748 | __ b(ne, &strings_not_empty); // If either string was empty, return r0. |
| 8749 | |
| 8750 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8751 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8752 | __ Ret(); |
| 8753 | |
| 8754 | __ bind(&strings_not_empty); |
| 8755 | } |
| 8756 | |
| 8757 | // Both strings are non-empty. |
| 8758 | // r0: first string |
| 8759 | // r1: second string |
| 8760 | // r2: length of first string |
| 8761 | // r3: length of second string |
| 8762 | // r4: first string instance type (if string_check_) |
| 8763 | // r5: second string instance type (if string_check_) |
| 8764 | // Look at the length of the result of adding the two strings. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8765 | Label string_add_flat_result, longer_than_two; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8766 | // Adding two lengths can't overflow. |
| 8767 | ASSERT(String::kMaxLength * 2 > String::kMaxLength); |
| 8768 | __ add(r6, r2, Operand(r3)); |
| 8769 | // Use the runtime system when adding two one character strings, as it |
| 8770 | // contains optimizations for this specific case using the symbol table. |
| 8771 | __ cmp(r6, Operand(2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8772 | __ b(ne, &longer_than_two); |
| 8773 | |
| 8774 | // Check that both strings are non-external ascii strings. |
| 8775 | if (!string_check_) { |
| 8776 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8777 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 8778 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8779 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 8780 | } |
| 8781 | __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7, |
| 8782 | &string_add_runtime); |
| 8783 | |
| 8784 | // Get the two characters forming the sub string. |
| 8785 | __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 8786 | __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 8787 | |
| 8788 | // Try to lookup two character string in symbol table. If it is not found |
| 8789 | // just allocate a new one. |
| 8790 | Label make_two_character_string; |
| 8791 | GenerateTwoCharacterSymbolTableProbe(masm, r2, r3, r6, r7, r4, r5, r9, |
| 8792 | &make_two_character_string); |
| 8793 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8794 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8795 | __ Ret(); |
| 8796 | |
| 8797 | __ bind(&make_two_character_string); |
| 8798 | // Resulting string has length 2 and first chars of two strings |
| 8799 | // are combined into single halfword in r2 register. |
| 8800 | // So we can fill resulting string without two loops by a single |
| 8801 | // halfword store instruction (which assumes that processor is |
| 8802 | // in a little endian mode) |
| 8803 | __ mov(r6, Operand(2)); |
| 8804 | __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime); |
| 8805 | __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 8806 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8807 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8808 | __ Ret(); |
| 8809 | |
| 8810 | __ bind(&longer_than_two); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8811 | // Check if resulting string will be flat. |
| 8812 | __ cmp(r6, Operand(String::kMinNonFlatLength)); |
| 8813 | __ b(lt, &string_add_flat_result); |
| 8814 | // Handle exceptionally long strings in the runtime system. |
| 8815 | ASSERT((String::kMaxLength & 0x80000000) == 0); |
| 8816 | ASSERT(IsPowerOf2(String::kMaxLength + 1)); |
| 8817 | // kMaxLength + 1 is representable as shifted literal, kMaxLength is not. |
| 8818 | __ cmp(r6, Operand(String::kMaxLength + 1)); |
| 8819 | __ b(hs, &string_add_runtime); |
| 8820 | |
| 8821 | // If result is not supposed to be flat, allocate a cons string object. |
| 8822 | // If both strings are ascii the result is an ascii cons string. |
| 8823 | if (!string_check_) { |
| 8824 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8825 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 8826 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8827 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 8828 | } |
| 8829 | Label non_ascii, allocated; |
| 8830 | ASSERT_EQ(0, kTwoByteStringTag); |
| 8831 | __ tst(r4, Operand(kStringEncodingMask)); |
| 8832 | __ tst(r5, Operand(kStringEncodingMask), ne); |
| 8833 | __ b(eq, &non_ascii); |
| 8834 | |
| 8835 | // Allocate an ASCII cons string. |
| 8836 | __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime); |
| 8837 | __ bind(&allocated); |
| 8838 | // Fill the fields of the cons string. |
| 8839 | __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset)); |
| 8840 | __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset)); |
| 8841 | __ mov(r0, Operand(r7)); |
| 8842 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8843 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8844 | __ Ret(); |
| 8845 | |
| 8846 | __ bind(&non_ascii); |
| 8847 | // Allocate a two byte cons string. |
| 8848 | __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime); |
| 8849 | __ jmp(&allocated); |
| 8850 | |
| 8851 | // Handle creating a flat result. First check that both strings are |
| 8852 | // sequential and that they have the same encoding. |
| 8853 | // r0: first string |
| 8854 | // r1: second string |
| 8855 | // r2: length of first string |
| 8856 | // r3: length of second string |
| 8857 | // r4: first string instance type (if string_check_) |
| 8858 | // r5: second string instance type (if string_check_) |
| 8859 | // r6: sum of lengths. |
| 8860 | __ bind(&string_add_flat_result); |
| 8861 | if (!string_check_) { |
| 8862 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8863 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 8864 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 8865 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 8866 | } |
| 8867 | // Check that both strings are sequential. |
| 8868 | ASSERT_EQ(0, kSeqStringTag); |
| 8869 | __ tst(r4, Operand(kStringRepresentationMask)); |
| 8870 | __ tst(r5, Operand(kStringRepresentationMask), eq); |
| 8871 | __ b(ne, &string_add_runtime); |
| 8872 | // Now check if both strings have the same encoding (ASCII/Two-byte). |
| 8873 | // r0: first string. |
| 8874 | // r1: second string. |
| 8875 | // r2: length of first string. |
| 8876 | // r3: length of second string. |
| 8877 | // r6: sum of lengths.. |
| 8878 | Label non_ascii_string_add_flat_result; |
| 8879 | ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test. |
| 8880 | __ eor(r7, r4, Operand(r5)); |
| 8881 | __ tst(r7, Operand(kStringEncodingMask)); |
| 8882 | __ b(ne, &string_add_runtime); |
| 8883 | // And see if it's ASCII or two-byte. |
| 8884 | __ tst(r4, Operand(kStringEncodingMask)); |
| 8885 | __ b(eq, &non_ascii_string_add_flat_result); |
| 8886 | |
| 8887 | // Both strings are sequential ASCII strings. We also know that they are |
| 8888 | // short (since the sum of the lengths is less than kMinNonFlatLength). |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8889 | // r6: length of resulting flat string |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8890 | __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 8891 | // Locate first character of result. |
| 8892 | __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8893 | // Locate first character of first argument. |
| 8894 | __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8895 | // r0: first character of first string. |
| 8896 | // r1: second string. |
| 8897 | // r2: length of first string. |
| 8898 | // r3: length of second string. |
| 8899 | // r6: first character of result. |
| 8900 | // r7: result string. |
| 8901 | GenerateCopyCharacters(masm, r6, r0, r2, r4, true); |
| 8902 | |
| 8903 | // Load second argument and locate first character. |
| 8904 | __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 8905 | // r1: first character of second string. |
| 8906 | // r3: length of second string. |
| 8907 | // r6: next character of result. |
| 8908 | // r7: result string. |
| 8909 | GenerateCopyCharacters(masm, r6, r1, r3, r4, true); |
| 8910 | __ mov(r0, Operand(r7)); |
| 8911 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8912 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8913 | __ Ret(); |
| 8914 | |
| 8915 | __ bind(&non_ascii_string_add_flat_result); |
| 8916 | // Both strings are sequential two byte strings. |
| 8917 | // r0: first string. |
| 8918 | // r1: second string. |
| 8919 | // r2: length of first string. |
| 8920 | // r3: length of second string. |
| 8921 | // r6: sum of length of strings. |
| 8922 | __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 8923 | // r0: first string. |
| 8924 | // r1: second string. |
| 8925 | // r2: length of first string. |
| 8926 | // r3: length of second string. |
| 8927 | // r7: result string. |
| 8928 | |
| 8929 | // Locate first character of result. |
| 8930 | __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8931 | // Locate first character of first argument. |
| 8932 | __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8933 | |
| 8934 | // r0: first character of first string. |
| 8935 | // r1: second string. |
| 8936 | // r2: length of first string. |
| 8937 | // r3: length of second string. |
| 8938 | // r6: first character of result. |
| 8939 | // r7: result string. |
| 8940 | GenerateCopyCharacters(masm, r6, r0, r2, r4, false); |
| 8941 | |
| 8942 | // Locate first character of second argument. |
| 8943 | __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 8944 | |
| 8945 | // r1: first character of second string. |
| 8946 | // r3: length of second string. |
| 8947 | // r6: next character of result (after copy of first string). |
| 8948 | // r7: result string. |
| 8949 | GenerateCopyCharacters(masm, r6, r1, r3, r4, false); |
| 8950 | |
| 8951 | __ mov(r0, Operand(r7)); |
| 8952 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 8953 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 8954 | __ Ret(); |
| 8955 | |
| 8956 | // Just jump to runtime to add the two strings. |
| 8957 | __ bind(&string_add_runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8958 | __ TailCallRuntime(Runtime::kStringAdd, 2, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8959 | } |
| 8960 | |
| 8961 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8962 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8963 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8964 | } } // namespace v8::internal |