ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "debug.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 34 | #include "parser.h" |
| 35 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 37 | #include "scopes.h" |
| 38 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 43 | #define __ ACCESS_MASM(masm_) |
| 44 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 45 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 46 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 47 | Condition cc, |
| 48 | bool never_nan_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 49 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 50 | Label* rhs_not_nan, |
| 51 | Label* slow, |
| 52 | bool strict); |
| 53 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
| 54 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 55 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 56 | Register source, |
| 57 | Register destination, |
| 58 | int known_int); |
| 59 | static bool IsEasyToMultiplyBy(int x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 63 | // ------------------------------------------------------------------------- |
| 64 | // Platform-specific DeferredCode functions. |
| 65 | |
| 66 | void DeferredCode::SaveRegisters() { |
| 67 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 68 | int action = registers_[i]; |
| 69 | if (action == kPush) { |
| 70 | __ push(RegisterAllocator::ToRegister(i)); |
| 71 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 72 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void DeferredCode::RestoreRegisters() { |
| 79 | // Restore registers in reverse order due to the stack. |
| 80 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 81 | int action = registers_[i]; |
| 82 | if (action == kPush) { |
| 83 | __ pop(RegisterAllocator::ToRegister(i)); |
| 84 | } else if (action != kIgnore) { |
| 85 | action &= ~kSyncedFlag; |
| 86 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 91 | |
| 92 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 93 | // CodeGenState implementation. |
| 94 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 95 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 96 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 97 | true_target_(NULL), |
| 98 | false_target_(NULL), |
| 99 | previous_(NULL) { |
| 100 | owner_->set_state(this); |
| 101 | } |
| 102 | |
| 103 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 104 | CodeGenState::CodeGenState(CodeGenerator* owner, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 105 | JumpTarget* true_target, |
| 106 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 107 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 108 | true_target_(true_target), |
| 109 | false_target_(false_target), |
| 110 | previous_(owner->state()) { |
| 111 | owner_->set_state(this); |
| 112 | } |
| 113 | |
| 114 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 115 | CodeGenState::~CodeGenState() { |
| 116 | ASSERT(owner_->state() == this); |
| 117 | owner_->set_state(previous_); |
| 118 | } |
| 119 | |
| 120 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 121 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 122 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 124 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 125 | bool is_eval) |
| 126 | : is_eval_(is_eval), |
| 127 | script_(script), |
| 128 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 130 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 131 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 132 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | cc_reg_(al), |
| 134 | state_(NULL), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 135 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 140 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 142 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | // cp: callee's context |
| 144 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 145 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 146 | // Record the position for debugging purposes. |
| 147 | CodeForFunctionPosition(fun); |
| 148 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | ZoneList<Statement*>* body = fun->body(); |
| 150 | |
| 151 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 152 | ASSERT(scope_ == NULL); |
| 153 | scope_ = fun->scope(); |
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; |
| 160 | { |
| 161 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 163 | // Entry: |
| 164 | // Stack: receiver, arguments |
| 165 | // lr: return address |
| 166 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 168 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 169 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 170 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 171 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | // tos: code slot |
| 173 | #ifdef DEBUG |
| 174 | if (strlen(FLAG_stop_at) > 0 && |
| 175 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 176 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 177 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | } |
| 179 | #endif |
| 180 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 181 | // Allocate space for locals and initialize them. This also checks |
| 182 | // for stack overflow. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 183 | frame_->AllocateStackSlots(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 184 | // Initialize the function return target after the locals are set |
| 185 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 186 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 187 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 189 | VirtualFrame::SpilledScope spilled_scope; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 190 | int heap_slots = scope_->num_heap_slots(); |
| 191 | if (heap_slots > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | // Allocate local context. |
| 193 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 194 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 195 | frame_->EmitPush(r0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 196 | if (heap_slots <= FastNewContextStub::kMaximumSlots) { |
| 197 | FastNewContextStub stub(heap_slots); |
| 198 | frame_->CallStub(&stub, 1); |
| 199 | } else { |
| 200 | frame_->CallRuntime(Runtime::kNewContext, 1); |
| 201 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 202 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 203 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 204 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 205 | __ cmp(r0, Operand(cp)); |
| 206 | verified_true.Branch(eq); |
| 207 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 208 | verified_true.Bind(); |
| 209 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 210 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 211 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | } |
| 213 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 214 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | // 1) only needed if we have a context |
| 216 | // 2) no need to recompute context ptr every single time |
| 217 | // 3) don't copy parameter operand code from SlotOperand! |
| 218 | { |
| 219 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 220 | |
| 221 | // Note that iteration order is relevant here! If we have the same |
| 222 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 223 | // needs to be copied into the context, it must be the last argument |
| 224 | // passed to the parameter that needs to be copied. This is a rare |
| 225 | // case so we don't check for it, instead we rely on the copying |
| 226 | // order: such a parameter is copied repeatedly into the same |
| 227 | // context location and thus the last value is what is seen inside |
| 228 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 229 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 230 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 231 | Slot* slot = par->slot(); |
| 232 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 233 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 234 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | // Loads r2 with context; used below in RecordWrite. |
| 236 | __ str(r1, SlotOperand(slot, r2)); |
| 237 | // Load the offset into r3. |
| 238 | int slot_offset = |
| 239 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 240 | __ mov(r3, Operand(slot_offset)); |
| 241 | __ RecordWrite(r2, r3, r1); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 246 | // Store the arguments object. This must happen after context |
| 247 | // initialization because the arguments object may be stored in the |
| 248 | // context. |
| 249 | if (scope_->arguments() != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 250 | Comment cmnt(masm_, "[ allocate arguments object"); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 251 | ASSERT(scope_->arguments_shadow() != NULL); |
| 252 | Variable* arguments = scope_->arguments()->var(); |
| 253 | Variable* shadow = scope_->arguments_shadow()->var(); |
| 254 | ASSERT(arguments != NULL && arguments->slot() != NULL); |
| 255 | ASSERT(shadow != NULL && shadow->slot() != NULL); |
| 256 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
| 257 | __ ldr(r2, frame_->Function()); |
| 258 | // The receiver is below the arguments, the return address, and the |
| 259 | // frame pointer on the stack. |
| 260 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
| 261 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
| 262 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 263 | frame_->Adjust(3); |
| 264 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
| 265 | frame_->CallStub(&stub, 3); |
| 266 | frame_->EmitPush(r0); |
| 267 | StoreToSlot(arguments->slot(), NOT_CONST_INIT); |
| 268 | StoreToSlot(shadow->slot(), NOT_CONST_INIT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 269 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 270 | } |
| 271 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 272 | // Initialize ThisFunction reference if present. |
| 273 | if (scope_->is_function_scope() && scope_->function() != NULL) { |
| 274 | __ mov(ip, Operand(Factory::the_hole_value())); |
| 275 | frame_->EmitPush(ip); |
| 276 | StoreToSlot(scope_->function()->slot(), NOT_CONST_INIT); |
| 277 | } |
| 278 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 279 | // Generate code to 'execute' declarations and initialize functions |
| 280 | // (source elements). In case of an illegal redeclaration we need to |
| 281 | // handle that instead of processing the declarations. |
| 282 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 284 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 285 | } else { |
| 286 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 287 | ProcessDeclarations(scope_->declarations()); |
| 288 | // Bail out if a stack-overflow exception occurred when processing |
| 289 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 290 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 291 | } |
| 292 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 293 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 294 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 295 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 296 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | |
| 298 | // Compile the body of the function in a vanilla state. Don't |
| 299 | // bother compiling all the code if the scope has an illegal |
| 300 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 301 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | Comment cmnt(masm_, "[ function body"); |
| 303 | #ifdef DEBUG |
| 304 | bool is_builtin = Bootstrapper::IsActive(); |
| 305 | bool should_trace = |
| 306 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 307 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 308 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 309 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 310 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 312 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 313 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 314 | } |
| 315 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 316 | // Generate the return sequence if necessary. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 317 | if (has_valid_frame() || function_return_.is_linked()) { |
| 318 | if (!function_return_.is_linked()) { |
| 319 | CodeForReturnPosition(fun); |
| 320 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 321 | // exit |
| 322 | // r0: result |
| 323 | // sp: stack pointer |
| 324 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 325 | // cp: callee's context |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 326 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 327 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 328 | function_return_.Bind(); |
| 329 | if (FLAG_trace) { |
| 330 | // Push the return value on the stack as the parameter. |
| 331 | // Runtime::TraceExit returns the parameter as it is. |
| 332 | frame_->EmitPush(r0); |
| 333 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 334 | } |
| 335 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 336 | // Add a label for checking the size of the code used for returning. |
| 337 | Label check_exit_codesize; |
| 338 | masm_->bind(&check_exit_codesize); |
| 339 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 340 | // Calculate the exact length of the return sequence and make sure that |
| 341 | // the constant pool is not emitted inside of the return sequence. |
| 342 | int32_t sp_delta = (scope_->num_parameters() + 1) * kPointerSize; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 343 | int return_sequence_length = Assembler::kJSReturnSequenceLength; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 344 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 345 | // Additional mov instruction generated. |
| 346 | return_sequence_length++; |
| 347 | } |
| 348 | masm_->BlockConstPoolFor(return_sequence_length); |
| 349 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 350 | // Tear down the frame which will restore the caller's frame pointer and |
| 351 | // the link register. |
| 352 | frame_->Exit(); |
| 353 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 354 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 355 | // tool from instrumenting as we rely on the code size here. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 356 | masm_->add(sp, sp, Operand(sp_delta)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 357 | masm_->Jump(lr); |
| 358 | |
| 359 | // 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] | 360 | // expected by the debugger. The add instruction above is an addressing |
| 361 | // mode 1 instruction where there are restrictions on which immediate values |
| 362 | // can be encoded in the instruction and which immediate values requires |
| 363 | // use of an additional instruction for moving the immediate to a temporary |
| 364 | // register. |
| 365 | ASSERT_EQ(return_sequence_length, |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 366 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 367 | } |
| 368 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | ASSERT(!has_cc()); |
| 371 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 372 | ASSERT(!function_return_is_shadowed_); |
| 373 | function_return_.Unuse(); |
| 374 | DeleteFrame(); |
| 375 | |
| 376 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 377 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 378 | ProcessDeferred(); |
| 379 | } |
| 380 | |
| 381 | allocator_ = NULL; |
| 382 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 386 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 387 | // Currently, this assertion will fail if we try to assign to |
| 388 | // a constant variable that is constant because it is read-only |
| 389 | // (such as the variable referring to a named function expression). |
| 390 | // We need to implement assignments to read-only variables. |
| 391 | // Ideally, we should do this during AST generation (by converting |
| 392 | // such assignments into expression statements); however, in general |
| 393 | // we may not be able to make the decision until past AST generation, |
| 394 | // that is when the entire program is known. |
| 395 | ASSERT(slot != NULL); |
| 396 | int index = slot->index(); |
| 397 | switch (slot->type()) { |
| 398 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 399 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 400 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 401 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 402 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 403 | |
| 404 | case Slot::CONTEXT: { |
| 405 | // Follow the context chain if necessary. |
| 406 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 407 | Register context = cp; |
| 408 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 409 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 410 | // Load the closure. |
| 411 | // (All contexts, even 'with' contexts, have a closure, |
| 412 | // and it is the same for all contexts inside a function. |
| 413 | // There is no need to go to the function context first.) |
| 414 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 415 | // Load the function context (which is the incoming, outer context). |
| 416 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 417 | context = tmp; |
| 418 | } |
| 419 | // We may have a 'with' context now. Get the function context. |
| 420 | // (In fact this mov may never be the needed, since the scope analysis |
| 421 | // may not permit a direct context access in this case and thus we are |
| 422 | // always at a function context. However it is safe to dereference be- |
| 423 | // cause the function context of a function context is itself. Before |
| 424 | // deleting this mov we should try to create a counter-example first, |
| 425 | // though...) |
| 426 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 427 | return ContextOperand(tmp, index); |
| 428 | } |
| 429 | |
| 430 | default: |
| 431 | UNREACHABLE(); |
| 432 | return MemOperand(r0, 0); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 437 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 438 | Slot* slot, |
| 439 | Register tmp, |
| 440 | Register tmp2, |
| 441 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 442 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 443 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 444 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 445 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 446 | if (s->num_heap_slots() > 0) { |
| 447 | if (s->calls_eval()) { |
| 448 | // Check that extension is NULL. |
| 449 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 450 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 451 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 452 | } |
| 453 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 454 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 455 | context = tmp; |
| 456 | } |
| 457 | } |
| 458 | // Check that last extension is NULL. |
| 459 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 460 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 461 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 462 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 463 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 467 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 468 | // (partially) translated into branches, or it may have set the condition |
| 469 | // code register. If force_cc is set, the value is forced to set the |
| 470 | // condition code register and no value is pushed. If the condition code |
| 471 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 472 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 473 | void CodeGenerator::LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 474 | JumpTarget* true_target, |
| 475 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 476 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 477 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 478 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 480 | { CodeGenState new_state(this, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 481 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 482 | |
| 483 | // If we hit a stack overflow, we may not have actually visited |
| 484 | // the expression. In that case, we ensure that we have a |
| 485 | // valid-looking frame state because we will continue to generate |
| 486 | // code as we unwind the C++ stack. |
| 487 | // |
| 488 | // It's possible to have both a stack overflow and a valid frame |
| 489 | // state (eg, a subexpression overflowed, visiting it returned |
| 490 | // with a dummied frame state, and visiting this expression |
| 491 | // returned with a normal-looking state). |
| 492 | if (HasStackOverflow() && |
| 493 | has_valid_frame() && |
| 494 | !has_cc() && |
| 495 | frame_->height() == original_height) { |
| 496 | true_target->Jump(); |
| 497 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 498 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 499 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 500 | // Convert the TOS value to a boolean in the condition code register. |
| 501 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 502 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 503 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 504 | ASSERT(!has_valid_frame() || |
| 505 | (has_cc() && frame_->height() == original_height) || |
| 506 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 510 | void CodeGenerator::Load(Expression* expr) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 511 | #ifdef DEBUG |
| 512 | int original_height = frame_->height(); |
| 513 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 514 | JumpTarget true_target; |
| 515 | JumpTarget false_target; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 516 | LoadCondition(expr, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 517 | |
| 518 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 519 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 520 | JumpTarget loaded; |
| 521 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 522 | materialize_true.Branch(cc_reg_); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 523 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 524 | frame_->EmitPush(r0); |
| 525 | loaded.Jump(); |
| 526 | materialize_true.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 527 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 528 | frame_->EmitPush(r0); |
| 529 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 530 | cc_reg_ = al; |
| 531 | } |
| 532 | |
| 533 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 534 | // We have at least one condition value that has been "translated" |
| 535 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 536 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 537 | if (frame_ != NULL) { |
| 538 | loaded.Jump(); // Don't lose the current TOS. |
| 539 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 541 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 542 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 543 | true_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 544 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 545 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 546 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 547 | // If both "true" and "false" need to be loaded jump across the code for |
| 548 | // "false". |
| 549 | if (both) { |
| 550 | loaded.Jump(); |
| 551 | } |
| 552 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 554 | false_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 555 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 556 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 557 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 558 | // A value is loaded on all paths reaching this point. |
| 559 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 561 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 562 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 563 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 567 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 568 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 569 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 570 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 574 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 575 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 576 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 577 | __ ldr(scratch, |
| 578 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 579 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 583 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 584 | // Special handling of identifiers as subexpressions of typeof. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 585 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 586 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 587 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 588 | // For a global variable we build the property reference |
| 589 | // <global>.<variable> and perform a (regular non-contextual) property |
| 590 | // load to make sure we do not get reference errors. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 591 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 592 | Literal key(variable->name()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 593 | Property property(&global, &key, RelocInfo::kNoPosition); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 594 | Reference ref(this, &property); |
| 595 | ref.GetValueAndSpill(); |
| 596 | } else if (variable != NULL && variable->slot() != NULL) { |
| 597 | // For a variable that rewrites to a slot, we signal it is the immediate |
| 598 | // subexpression of a typeof. |
| 599 | LoadFromSlot(variable->slot(), INSIDE_TYPEOF); |
| 600 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 601 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 602 | // Anything else can be handled normally. |
| 603 | LoadAndSpill(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 608 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 609 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 610 | cgen->LoadReference(this); |
| 611 | } |
| 612 | |
| 613 | |
| 614 | Reference::~Reference() { |
| 615 | cgen_->UnloadReference(this); |
| 616 | } |
| 617 | |
| 618 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 619 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 620 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 621 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 622 | Expression* e = ref->expression(); |
| 623 | Property* property = e->AsProperty(); |
| 624 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 625 | |
| 626 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 627 | // The expression is either a property or a variable proxy that rewrites |
| 628 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 629 | LoadAndSpill(property->obj()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 630 | if (property->key()->IsPropertyName()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 631 | ref->set_type(Reference::NAMED); |
| 632 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 633 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 634 | ref->set_type(Reference::KEYED); |
| 635 | } |
| 636 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 637 | // The expression is a variable proxy that does not rewrite to a |
| 638 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 639 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | LoadGlobal(); |
| 641 | ref->set_type(Reference::NAMED); |
| 642 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 643 | ASSERT(var->slot() != NULL); |
| 644 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 645 | } |
| 646 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 647 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 648 | LoadAndSpill(e); |
| 649 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| 653 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 654 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 655 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 656 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 657 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 658 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 659 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 660 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 661 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 662 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 667 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 668 | // register to a boolean in the condition code register. The code |
| 669 | // 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] | 670 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 671 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 672 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 673 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 674 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 675 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 676 | |
| 677 | // Fast case checks |
| 678 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 679 | // Check if the value is 'false'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 680 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 681 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 682 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 683 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 684 | // Check if the value is 'true'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 685 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 686 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 687 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 688 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 689 | // Check if the value is 'undefined'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 690 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 691 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 692 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 693 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 694 | // Check if the value is a smi. |
| 695 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 696 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 697 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 698 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 699 | |
| 700 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 701 | frame_->EmitPush(r0); |
| 702 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 703 | // Convert the result (r0) to a condition code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 704 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 705 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 706 | |
| 707 | cc_reg_ = ne; |
| 708 | } |
| 709 | |
| 710 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 711 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 712 | OverwriteMode overwrite_mode, |
| 713 | int constant_rhs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 714 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 715 | // sp[0] : y |
| 716 | // sp[1] : x |
| 717 | // result : r0 |
| 718 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 719 | // Stub is entered with a call: 'return address' is in lr. |
| 720 | switch (op) { |
| 721 | case Token::ADD: // fall through. |
| 722 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 723 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 724 | case Token::DIV: |
| 725 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 726 | case Token::BIT_OR: |
| 727 | case Token::BIT_AND: |
| 728 | case Token::BIT_XOR: |
| 729 | case Token::SHL: |
| 730 | case Token::SHR: |
| 731 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 732 | frame_->EmitPop(r0); // r0 : y |
| 733 | frame_->EmitPop(r1); // r1 : x |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 734 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 735 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 736 | break; |
| 737 | } |
| 738 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 739 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 740 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 741 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 742 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 743 | break; |
| 744 | |
| 745 | default: |
| 746 | // Other cases should have been handled before this point. |
| 747 | UNREACHABLE(); |
| 748 | break; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 753 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 754 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 755 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 756 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 757 | bool reversed, |
| 758 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 759 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 760 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 761 | reversed_(reversed), |
| 762 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 763 | set_comment("[ DeferredInlinedSmiOperation"); |
| 764 | } |
| 765 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 766 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 767 | |
| 768 | private: |
| 769 | Token::Value op_; |
| 770 | int value_; |
| 771 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 772 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 773 | }; |
| 774 | |
| 775 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 776 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 777 | switch (op_) { |
| 778 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 779 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 780 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 781 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 782 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 783 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 784 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 785 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 786 | } |
| 787 | break; |
| 788 | } |
| 789 | |
| 790 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 791 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 792 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 793 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 794 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 795 | } else { |
| 796 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 797 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 798 | } |
| 799 | break; |
| 800 | } |
| 801 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 802 | // For these operations there is no optimistic operation that needs to be |
| 803 | // reverted. |
| 804 | case Token::MUL: |
| 805 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 806 | case Token::BIT_OR: |
| 807 | case Token::BIT_XOR: |
| 808 | case Token::BIT_AND: { |
| 809 | if (reversed_) { |
| 810 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 811 | } else { |
| 812 | __ mov(r1, Operand(r0)); |
| 813 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 814 | } |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | case Token::SHL: |
| 819 | case Token::SHR: |
| 820 | case Token::SAR: { |
| 821 | if (!reversed_) { |
| 822 | __ mov(r1, Operand(r0)); |
| 823 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 824 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 825 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 826 | } |
| 827 | break; |
| 828 | } |
| 829 | |
| 830 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 831 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 832 | UNREACHABLE(); |
| 833 | break; |
| 834 | } |
| 835 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 836 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 837 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 841 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 842 | x &= x - 1; |
| 843 | return (x & (x - 1)) == 0; |
| 844 | } |
| 845 | |
| 846 | |
| 847 | // Returns the index of the lowest bit set. |
| 848 | static int BitPosition(unsigned x) { |
| 849 | int bit_posn = 0; |
| 850 | while ((x & 0xf) == 0) { |
| 851 | bit_posn += 4; |
| 852 | x >>= 4; |
| 853 | } |
| 854 | while ((x & 1) == 0) { |
| 855 | bit_posn++; |
| 856 | x >>= 1; |
| 857 | } |
| 858 | return bit_posn; |
| 859 | } |
| 860 | |
| 861 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 862 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 863 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 864 | bool reversed, |
| 865 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 866 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 867 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 868 | // some possible smi operations (like + and -) when (at least) one |
| 869 | // of the operands is a literal smi. With this optimization, the |
| 870 | // performance of the system is increased by ~15%, and the generated |
| 871 | // code size is increased by ~1% (measured on a combination of |
| 872 | // different benchmarks). |
| 873 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 874 | // sp[0] : operand |
| 875 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 876 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 877 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 878 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 879 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 880 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 881 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 882 | switch (op) { |
| 883 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 884 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 885 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 886 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 887 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 888 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 889 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 890 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 891 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 892 | break; |
| 893 | } |
| 894 | |
| 895 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 896 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 897 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 898 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 899 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 900 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 901 | } else { |
| 902 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 903 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 904 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 905 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 906 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 907 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 908 | break; |
| 909 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 910 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 911 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 912 | case Token::BIT_OR: |
| 913 | case Token::BIT_XOR: |
| 914 | case Token::BIT_AND: { |
| 915 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 916 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 917 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 918 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 919 | switch (op) { |
| 920 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 921 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 922 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 923 | default: UNREACHABLE(); |
| 924 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 925 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 926 | break; |
| 927 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 928 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 929 | case Token::SHL: |
| 930 | case Token::SHR: |
| 931 | case Token::SAR: { |
| 932 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 933 | something_to_inline = false; |
| 934 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 935 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 936 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 937 | DeferredCode* deferred = |
| 938 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
| 939 | __ tst(r0, Operand(kSmiTagMask)); |
| 940 | deferred->Branch(ne); |
| 941 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 942 | switch (op) { |
| 943 | case Token::SHL: { |
| 944 | if (shift_value != 0) { |
| 945 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 946 | } |
| 947 | // check that the *unsigned* result fits in a smi |
| 948 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 949 | deferred->Branch(mi); |
| 950 | break; |
| 951 | } |
| 952 | case Token::SHR: { |
| 953 | // LSR by immediate 0 means shifting 32 bits. |
| 954 | if (shift_value != 0) { |
| 955 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 956 | } |
| 957 | // check that the *unsigned* result fits in a smi |
| 958 | // neither of the two high-order bits can be set: |
| 959 | // - 0x80000000: high bit would be lost when smi tagging |
| 960 | // - 0x40000000: this number would convert to negative when |
| 961 | // smi tagging these two cases can only happen with shifts |
| 962 | // by 0 or 1 when handed a valid smi |
| 963 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 964 | deferred->Branch(ne); |
| 965 | break; |
| 966 | } |
| 967 | case Token::SAR: { |
| 968 | if (shift_value != 0) { |
| 969 | // ASR by immediate 0 means shifting 32 bits. |
| 970 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 971 | } |
| 972 | break; |
| 973 | } |
| 974 | default: UNREACHABLE(); |
| 975 | } |
| 976 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 977 | deferred->BindExit(); |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | case Token::MOD: { |
| 982 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 983 | something_to_inline = false; |
| 984 | break; |
| 985 | } |
| 986 | DeferredCode* deferred = |
| 987 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 988 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 989 | __ tst(r0, Operand(mask)); |
| 990 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 991 | mask = (int_value << kSmiTagSize) - 1; |
| 992 | __ and_(r0, r0, Operand(mask)); |
| 993 | deferred->BindExit(); |
| 994 | break; |
| 995 | } |
| 996 | |
| 997 | case Token::MUL: { |
| 998 | if (!IsEasyToMultiplyBy(int_value)) { |
| 999 | something_to_inline = false; |
| 1000 | break; |
| 1001 | } |
| 1002 | DeferredCode* deferred = |
| 1003 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1004 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1005 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1006 | unsigned mask = 0x80000000u; |
| 1007 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1008 | mask |= mask >> 1; |
| 1009 | } |
| 1010 | mask |= kSmiTagMask; |
| 1011 | // This does a single mask that checks for a too high value in a |
| 1012 | // conservative way and for a non-Smi. It also filters out negative |
| 1013 | // numbers, unfortunately, but since this code is inline we prefer |
| 1014 | // brevity to comprehensiveness. |
| 1015 | __ tst(r0, Operand(mask)); |
| 1016 | deferred->Branch(ne); |
| 1017 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 1018 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1023 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1024 | break; |
| 1025 | } |
| 1026 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1027 | if (!something_to_inline) { |
| 1028 | if (!reversed) { |
| 1029 | frame_->EmitPush(r0); |
| 1030 | __ mov(r0, Operand(value)); |
| 1031 | frame_->EmitPush(r0); |
| 1032 | GenericBinaryOperation(op, mode, int_value); |
| 1033 | } else { |
| 1034 | __ mov(ip, Operand(value)); |
| 1035 | frame_->EmitPush(ip); |
| 1036 | frame_->EmitPush(r0); |
| 1037 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1038 | } |
| 1039 | } |
| 1040 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1041 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1045 | void CodeGenerator::Comparison(Condition cc, |
| 1046 | Expression* left, |
| 1047 | Expression* right, |
| 1048 | bool strict) { |
| 1049 | if (left != NULL) LoadAndSpill(left); |
| 1050 | if (right != NULL) LoadAndSpill(right); |
| 1051 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1052 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1053 | // sp[0] : y |
| 1054 | // sp[1] : x |
| 1055 | // result : cc register |
| 1056 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1057 | // Strict only makes sense for equality comparisons. |
| 1058 | ASSERT(!strict || cc == eq); |
| 1059 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1060 | JumpTarget exit; |
| 1061 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1062 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1063 | if (cc == gt || cc == le) { |
| 1064 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1065 | frame_->EmitPop(r1); |
| 1066 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1067 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1068 | frame_->EmitPop(r0); |
| 1069 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1070 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1071 | __ orr(r2, r0, Operand(r1)); |
| 1072 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1073 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1074 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1075 | // Perform non-smi comparison by stub. |
| 1076 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1077 | // We call with 0 args because there are 0 on the stack. |
| 1078 | CompareStub stub(cc, strict); |
| 1079 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1080 | __ cmp(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1081 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1082 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1083 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1084 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1085 | __ cmp(r1, Operand(r0)); |
| 1086 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1087 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1088 | cc_reg_ = cc; |
| 1089 | } |
| 1090 | |
| 1091 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1092 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1093 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1094 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1095 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1096 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1097 | int arg_count = args->length(); |
| 1098 | for (int i = 0; i < arg_count; i++) { |
| 1099 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1100 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1101 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1102 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1103 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1104 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1105 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1106 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 1107 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1108 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1111 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1112 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1116 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1117 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1118 | ASSERT(has_cc()); |
| 1119 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1120 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1121 | cc_reg_ = al; |
| 1122 | } |
| 1123 | |
| 1124 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1125 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1126 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1127 | Comment cmnt(masm_, "[ check stack"); |
| 1128 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1129 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1130 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1131 | // gives a return address 12 bytes down. |
| 1132 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1133 | masm_->cmp(sp, Operand(ip)); |
| 1134 | StackCheckStub stub; |
| 1135 | // Call the stub if lower. |
| 1136 | masm_->mov(pc, |
| 1137 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1138 | RelocInfo::CODE_TARGET), |
| 1139 | LeaveCC, |
| 1140 | lo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1144 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1145 | #ifdef DEBUG |
| 1146 | int original_height = frame_->height(); |
| 1147 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1148 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1149 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1150 | VisitAndSpill(statements->at(i)); |
| 1151 | } |
| 1152 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1153 | } |
| 1154 | |
| 1155 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1156 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1157 | #ifdef DEBUG |
| 1158 | int original_height = frame_->height(); |
| 1159 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1160 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1161 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1162 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1163 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1164 | VisitStatementsAndSpill(node->statements()); |
| 1165 | if (node->break_target()->is_linked()) { |
| 1166 | node->break_target()->Bind(); |
| 1167 | } |
| 1168 | node->break_target()->Unuse(); |
| 1169 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1173 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1174 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1175 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1176 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1177 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1178 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1179 | frame_->EmitPush(r0); |
| 1180 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1181 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1185 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1186 | #ifdef DEBUG |
| 1187 | int original_height = frame_->height(); |
| 1188 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1189 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1190 | Comment cmnt(masm_, "[ Declaration"); |
| 1191 | Variable* var = node->proxy()->var(); |
| 1192 | ASSERT(var != NULL); // must have been resolved |
| 1193 | Slot* slot = var->slot(); |
| 1194 | |
| 1195 | // If it was not possible to allocate the variable at compile time, |
| 1196 | // we need to "declare" it at runtime to make sure it actually |
| 1197 | // exists in the local context. |
| 1198 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1199 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1200 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1201 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1202 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1203 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1204 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1205 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1206 | // Declaration nodes are always declared in only two modes. |
| 1207 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1208 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1209 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1210 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1211 | // Push initial value, if any. |
| 1212 | // Note: For variables we must not push an initial value (such as |
| 1213 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1214 | // must not destroy the current value. |
| 1215 | if (node->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1216 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1217 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1218 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1219 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1220 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1221 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1222 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1223 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1224 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1225 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1226 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | ASSERT(!var->is_global()); |
| 1231 | |
| 1232 | // If we have a function or a constant, we need to initialize the variable. |
| 1233 | Expression* val = NULL; |
| 1234 | if (node->mode() == Variable::CONST) { |
| 1235 | val = new Literal(Factory::the_hole_value()); |
| 1236 | } else { |
| 1237 | val = node->fun(); // NULL if we don't have a function |
| 1238 | } |
| 1239 | |
| 1240 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1241 | { |
| 1242 | // Set initial value. |
| 1243 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1244 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1245 | target.SetValue(NOT_CONST_INIT); |
| 1246 | // The reference is removed from the stack (preserving TOS) when |
| 1247 | // it goes out of scope. |
| 1248 | } |
| 1249 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1250 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1251 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1252 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1256 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1257 | #ifdef DEBUG |
| 1258 | int original_height = frame_->height(); |
| 1259 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1260 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1261 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1262 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1263 | Expression* expression = node->expression(); |
| 1264 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1265 | LoadAndSpill(expression); |
| 1266 | frame_->Drop(); |
| 1267 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1271 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1272 | #ifdef DEBUG |
| 1273 | int original_height = frame_->height(); |
| 1274 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1275 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1276 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1277 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1278 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1279 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1283 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1284 | #ifdef DEBUG |
| 1285 | int original_height = frame_->height(); |
| 1286 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1287 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1288 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1289 | // Generate different code depending on which parts of the if statement |
| 1290 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1291 | bool has_then_stm = node->HasThenStatement(); |
| 1292 | bool has_else_stm = node->HasElseStatement(); |
| 1293 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1294 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1296 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1297 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1298 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1299 | JumpTarget then; |
| 1300 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1301 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1302 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1303 | if (frame_ != NULL) { |
| 1304 | Branch(false, &else_); |
| 1305 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1306 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1307 | if (frame_ != NULL || then.is_linked()) { |
| 1308 | then.Bind(); |
| 1309 | VisitAndSpill(node->then_statement()); |
| 1310 | } |
| 1311 | if (frame_ != NULL) { |
| 1312 | exit.Jump(); |
| 1313 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1314 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1315 | if (else_.is_linked()) { |
| 1316 | else_.Bind(); |
| 1317 | VisitAndSpill(node->else_statement()); |
| 1318 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1319 | |
| 1320 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1321 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1322 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1323 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1324 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1325 | LoadConditionAndSpill(node->condition(), &then, &exit, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1326 | if (frame_ != NULL) { |
| 1327 | Branch(false, &exit); |
| 1328 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1329 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1330 | if (frame_ != NULL || then.is_linked()) { |
| 1331 | then.Bind(); |
| 1332 | VisitAndSpill(node->then_statement()); |
| 1333 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1334 | |
| 1335 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1336 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1337 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1338 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1339 | // if (!cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1340 | LoadConditionAndSpill(node->condition(), &exit, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1341 | if (frame_ != NULL) { |
| 1342 | Branch(true, &exit); |
| 1343 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1344 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1345 | if (frame_ != NULL || else_.is_linked()) { |
| 1346 | else_.Bind(); |
| 1347 | VisitAndSpill(node->else_statement()); |
| 1348 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1349 | |
| 1350 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1351 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1352 | ASSERT(!has_then_stm && !has_else_stm); |
| 1353 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1354 | LoadConditionAndSpill(node->condition(), &exit, &exit, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1355 | if (frame_ != NULL) { |
| 1356 | if (has_cc()) { |
| 1357 | cc_reg_ = al; |
| 1358 | } else { |
| 1359 | frame_->Drop(); |
| 1360 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1365 | if (exit.is_linked()) { |
| 1366 | exit.Bind(); |
| 1367 | } |
| 1368 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1372 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1373 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1374 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1375 | CodeForStatementPosition(node); |
| 1376 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1380 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1381 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1382 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1383 | CodeForStatementPosition(node); |
| 1384 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1388 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1389 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1390 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1391 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1392 | CodeForStatementPosition(node); |
| 1393 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1394 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1395 | frame_->EmitPop(r0); |
| 1396 | function_return_.Jump(); |
| 1397 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1398 | // Pop the result from the frame and prepare the frame for |
| 1399 | // returning thus making it easier to merge. |
| 1400 | frame_->EmitPop(r0); |
| 1401 | frame_->PrepareForReturn(); |
| 1402 | |
| 1403 | function_return_.Jump(); |
| 1404 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1408 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1409 | #ifdef DEBUG |
| 1410 | int original_height = frame_->height(); |
| 1411 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1412 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1413 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1414 | CodeForStatementPosition(node); |
| 1415 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1416 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1417 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1418 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1419 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1420 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1421 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1422 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1423 | __ cmp(r0, Operand(cp)); |
| 1424 | verified_true.Branch(eq); |
| 1425 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1426 | verified_true.Bind(); |
| 1427 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1428 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1429 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1430 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1434 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1435 | #ifdef DEBUG |
| 1436 | int original_height = frame_->height(); |
| 1437 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1438 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1439 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1440 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1441 | // Pop context. |
| 1442 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1443 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1444 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1445 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1449 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1450 | #ifdef DEBUG |
| 1451 | int original_height = frame_->height(); |
| 1452 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1453 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1454 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1455 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1456 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1457 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1458 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1459 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1460 | JumpTarget next_test; |
| 1461 | JumpTarget fall_through; |
| 1462 | JumpTarget default_entry; |
| 1463 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1464 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1465 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1466 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1467 | |
| 1468 | for (int i = 0; i < length; i++) { |
| 1469 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1470 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1471 | // Remember the default clause and compile it at the end. |
| 1472 | default_clause = clause; |
| 1473 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1476 | Comment cmnt(masm_, "[ Case clause"); |
| 1477 | // Compile the test. |
| 1478 | next_test.Bind(); |
| 1479 | next_test.Unuse(); |
| 1480 | // Duplicate TOS. |
| 1481 | __ ldr(r0, frame_->Top()); |
| 1482 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1483 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1484 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1485 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1486 | // Before entering the body from the test, remove the switch value from |
| 1487 | // the stack. |
| 1488 | frame_->Drop(); |
| 1489 | |
| 1490 | // Label the body so that fall through is enabled. |
| 1491 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1492 | default_exit.Bind(); |
| 1493 | } else { |
| 1494 | fall_through.Bind(); |
| 1495 | fall_through.Unuse(); |
| 1496 | } |
| 1497 | VisitStatementsAndSpill(clause->statements()); |
| 1498 | |
| 1499 | // If control flow can fall through from the body, jump to the next body |
| 1500 | // or the end of the statement. |
| 1501 | if (frame_ != NULL) { |
| 1502 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1503 | default_entry.Jump(); |
| 1504 | } else { |
| 1505 | fall_through.Jump(); |
| 1506 | } |
| 1507 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1510 | // The final "test" removes the switch value. |
| 1511 | next_test.Bind(); |
| 1512 | frame_->Drop(); |
| 1513 | |
| 1514 | // If there is a default clause, compile it. |
| 1515 | if (default_clause != NULL) { |
| 1516 | Comment cmnt(masm_, "[ Default clause"); |
| 1517 | default_entry.Bind(); |
| 1518 | VisitStatementsAndSpill(default_clause->statements()); |
| 1519 | // If control flow can fall out of the default and there is a case after |
| 1520 | // it, jup to that case's body. |
| 1521 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1522 | default_exit.Jump(); |
| 1523 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1524 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1525 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1526 | if (fall_through.is_linked()) { |
| 1527 | fall_through.Bind(); |
| 1528 | } |
| 1529 | |
| 1530 | if (node->break_target()->is_linked()) { |
| 1531 | node->break_target()->Bind(); |
| 1532 | } |
| 1533 | node->break_target()->Unuse(); |
| 1534 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1538 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* 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 |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1542 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1543 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1544 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1545 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1546 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1547 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1548 | // Label the top of the loop for the backward CFG edge. If the test |
| 1549 | // is always true we can use the continue target, and if the test is |
| 1550 | // always false there is no need. |
| 1551 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1552 | switch (info) { |
| 1553 | case ALWAYS_TRUE: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1554 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1555 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1556 | break; |
| 1557 | case ALWAYS_FALSE: |
| 1558 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1559 | break; |
| 1560 | case DONT_KNOW: |
| 1561 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1562 | body.Bind(); |
| 1563 | break; |
| 1564 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1565 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1566 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1567 | VisitAndSpill(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1568 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1569 | // Compile the test. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1570 | switch (info) { |
| 1571 | case ALWAYS_TRUE: |
| 1572 | // If control can fall off the end of the body, jump back to the |
| 1573 | // top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1574 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1575 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1576 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1577 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1578 | case ALWAYS_FALSE: |
| 1579 | // If we have a continue in the body, we only have to bind its |
| 1580 | // jump target. |
| 1581 | if (node->continue_target()->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1582 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1583 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1584 | break; |
| 1585 | case DONT_KNOW: |
| 1586 | // We have to compile the test expression if it can be reached by |
| 1587 | // control flow falling out of the body or via continue. |
| 1588 | if (node->continue_target()->is_linked()) { |
| 1589 | node->continue_target()->Bind(); |
| 1590 | } |
| 1591 | if (has_valid_frame()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1592 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 1593 | CodeForDoWhileConditionPosition(node); |
| 1594 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1595 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1596 | // A invalid frame here indicates that control did not |
| 1597 | // fall out of the test expression. |
| 1598 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1599 | } |
| 1600 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1601 | break; |
| 1602 | } |
| 1603 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1604 | if (node->break_target()->is_linked()) { |
| 1605 | node->break_target()->Bind(); |
| 1606 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1607 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1608 | } |
| 1609 | |
| 1610 | |
| 1611 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 1612 | #ifdef DEBUG |
| 1613 | int original_height = frame_->height(); |
| 1614 | #endif |
| 1615 | VirtualFrame::SpilledScope spilled_scope; |
| 1616 | Comment cmnt(masm_, "[ WhileStatement"); |
| 1617 | CodeForStatementPosition(node); |
| 1618 | |
| 1619 | // If the test is never true and has no side effects there is no need |
| 1620 | // to compile the test or body. |
| 1621 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1622 | if (info == ALWAYS_FALSE) return; |
| 1623 | |
| 1624 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1625 | |
| 1626 | // Label the top of the loop with the continue target for the backward |
| 1627 | // CFG edge. |
| 1628 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1629 | node->continue_target()->Bind(); |
| 1630 | |
| 1631 | if (info == DONT_KNOW) { |
| 1632 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1633 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1634 | if (has_valid_frame()) { |
| 1635 | // A NULL frame indicates that control did not fall out of the |
| 1636 | // test expression. |
| 1637 | Branch(false, node->break_target()); |
| 1638 | } |
| 1639 | if (has_valid_frame() || body.is_linked()) { |
| 1640 | body.Bind(); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | if (has_valid_frame()) { |
| 1645 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1646 | VisitAndSpill(node->body()); |
| 1647 | |
| 1648 | // If control flow can fall out of the body, jump back to the top. |
| 1649 | if (has_valid_frame()) { |
| 1650 | node->continue_target()->Jump(); |
| 1651 | } |
| 1652 | } |
| 1653 | if (node->break_target()->is_linked()) { |
| 1654 | node->break_target()->Bind(); |
| 1655 | } |
| 1656 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1657 | } |
| 1658 | |
| 1659 | |
| 1660 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 1661 | #ifdef DEBUG |
| 1662 | int original_height = frame_->height(); |
| 1663 | #endif |
| 1664 | VirtualFrame::SpilledScope spilled_scope; |
| 1665 | Comment cmnt(masm_, "[ ForStatement"); |
| 1666 | CodeForStatementPosition(node); |
| 1667 | if (node->init() != NULL) { |
| 1668 | VisitAndSpill(node->init()); |
| 1669 | } |
| 1670 | |
| 1671 | // If the test is never true there is no need to compile the test or |
| 1672 | // body. |
| 1673 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1674 | if (info == ALWAYS_FALSE) return; |
| 1675 | |
| 1676 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1677 | |
| 1678 | // If there is no update statement, label the top of the loop with the |
| 1679 | // continue target, otherwise with the loop target. |
| 1680 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1681 | if (node->next() == NULL) { |
| 1682 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1683 | node->continue_target()->Bind(); |
| 1684 | } else { |
| 1685 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1686 | loop.Bind(); |
| 1687 | } |
| 1688 | |
| 1689 | // If the test is always true, there is no need to compile it. |
| 1690 | if (info == DONT_KNOW) { |
| 1691 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1692 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1693 | if (has_valid_frame()) { |
| 1694 | Branch(false, node->break_target()); |
| 1695 | } |
| 1696 | if (has_valid_frame() || body.is_linked()) { |
| 1697 | body.Bind(); |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | if (has_valid_frame()) { |
| 1702 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1703 | VisitAndSpill(node->body()); |
| 1704 | |
| 1705 | if (node->next() == NULL) { |
| 1706 | // If there is no update statement and control flow can fall out |
| 1707 | // of the loop, jump directly to the continue label. |
| 1708 | if (has_valid_frame()) { |
| 1709 | node->continue_target()->Jump(); |
| 1710 | } |
| 1711 | } else { |
| 1712 | // If there is an update statement and control flow can reach it |
| 1713 | // via falling out of the body of the loop or continuing, we |
| 1714 | // compile the update statement. |
| 1715 | if (node->continue_target()->is_linked()) { |
| 1716 | node->continue_target()->Bind(); |
| 1717 | } |
| 1718 | if (has_valid_frame()) { |
| 1719 | // Record source position of the statement as this code which is |
| 1720 | // after the code for the body actually belongs to the loop |
| 1721 | // statement and not the body. |
| 1722 | CodeForStatementPosition(node); |
| 1723 | VisitAndSpill(node->next()); |
| 1724 | loop.Jump(); |
| 1725 | } |
| 1726 | } |
| 1727 | } |
| 1728 | if (node->break_target()->is_linked()) { |
| 1729 | node->break_target()->Bind(); |
| 1730 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1731 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1735 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1736 | #ifdef DEBUG |
| 1737 | int original_height = frame_->height(); |
| 1738 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1739 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1740 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1741 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1742 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1743 | JumpTarget primitive; |
| 1744 | JumpTarget jsobject; |
| 1745 | JumpTarget fixed_array; |
| 1746 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1747 | JumpTarget end_del_check; |
| 1748 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1749 | |
| 1750 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1751 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1752 | |
| 1753 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1754 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1755 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1756 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 1757 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1758 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1759 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1760 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1761 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1762 | |
| 1763 | // Stack layout in body: |
| 1764 | // [iteration counter (Smi)] |
| 1765 | // [length of array] |
| 1766 | // [FixedArray] |
| 1767 | // [Map or 0] |
| 1768 | // [Object] |
| 1769 | |
| 1770 | // Check if enumerable is already a JSObject |
| 1771 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1772 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1773 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1774 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1775 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1776 | primitive.Bind(); |
| 1777 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1778 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1779 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1780 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1781 | // Get the set of properties (as a FixedArray or Map). |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1782 | // r0: value to be iterated over |
| 1783 | frame_->EmitPush(r0); // Push the object being iterated over. |
| 1784 | |
| 1785 | // Check cache validity in generated code. This is a fast case for |
| 1786 | // the JSObject::IsSimpleEnum cache validity checks. If we cannot |
| 1787 | // guarantee cache validity, call the runtime system to check cache |
| 1788 | // validity or get the property names in a fixed array. |
| 1789 | JumpTarget call_runtime; |
| 1790 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1791 | JumpTarget check_prototype; |
| 1792 | JumpTarget use_cache; |
| 1793 | __ mov(r1, Operand(r0)); |
| 1794 | loop.Bind(); |
| 1795 | // Check that there are no elements. |
| 1796 | __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 1797 | __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex); |
| 1798 | __ cmp(r2, r4); |
| 1799 | call_runtime.Branch(ne); |
| 1800 | // Check that instance descriptors are not empty so that we can |
| 1801 | // check for an enum cache. Leave the map in r3 for the subsequent |
| 1802 | // prototype load. |
| 1803 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1804 | __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset)); |
| 1805 | __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex); |
| 1806 | __ cmp(r2, ip); |
| 1807 | call_runtime.Branch(eq); |
| 1808 | // Check that there in an enum cache in the non-empty instance |
| 1809 | // descriptors. This is the case if the next enumeration index |
| 1810 | // field does not contain a smi. |
| 1811 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset)); |
| 1812 | __ tst(r2, Operand(kSmiTagMask)); |
| 1813 | call_runtime.Branch(eq); |
| 1814 | // For all objects but the receiver, check that the cache is empty. |
| 1815 | // r4: empty fixed array root. |
| 1816 | __ cmp(r1, r0); |
| 1817 | check_prototype.Branch(eq); |
| 1818 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1819 | __ cmp(r2, r4); |
| 1820 | call_runtime.Branch(ne); |
| 1821 | check_prototype.Bind(); |
| 1822 | // Load the prototype from the map and loop if non-null. |
| 1823 | __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 1824 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1825 | __ cmp(r1, ip); |
| 1826 | loop.Branch(ne); |
| 1827 | // The enum cache is valid. Load the map of the object being |
| 1828 | // iterated over and use the cache for the iteration. |
| 1829 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1830 | use_cache.Jump(); |
| 1831 | |
| 1832 | call_runtime.Bind(); |
| 1833 | // Call the runtime to get the property names for the object. |
| 1834 | 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] | 1835 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1836 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1837 | // If we got a map from the runtime call, we can do a fast |
| 1838 | // modification check. Otherwise, we got a fixed array, and we have |
| 1839 | // to do a slow check. |
| 1840 | // r0: map or fixed array (result from call to |
| 1841 | // Runtime::kGetPropertyNamesFast) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1842 | __ mov(r2, Operand(r0)); |
| 1843 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1844 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 1845 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1846 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1847 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1848 | use_cache.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1849 | // Get enum cache |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1850 | // r0: map (either the result from a call to |
| 1851 | // Runtime::kGetPropertyNamesFast or has been fetched directly from |
| 1852 | // the object) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1853 | __ mov(r1, Operand(r0)); |
| 1854 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1855 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1856 | __ ldr(r2, |
| 1857 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1858 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1859 | frame_->EmitPush(r0); // map |
| 1860 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1861 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1862 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1863 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1864 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1865 | frame_->EmitPush(r0); |
| 1866 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1867 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1868 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1869 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1870 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1871 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1872 | |
| 1873 | // 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] | 1874 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1875 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1876 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1877 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1878 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1879 | |
| 1880 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1881 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1882 | // sp[0] : index |
| 1883 | // sp[1] : array/enum cache length |
| 1884 | // sp[2] : array or enum cache |
| 1885 | // sp[3] : 0 or map |
| 1886 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1887 | // Grab the current frame's height for the break and continue |
| 1888 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1889 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1890 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1891 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1892 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1893 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1894 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1895 | node->break_target()->Branch(hs); |
| 1896 | |
| 1897 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1898 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1899 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1900 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1901 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1902 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1903 | |
| 1904 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1905 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1906 | // Check if this (still) matches the map of the enumerable. |
| 1907 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1908 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1909 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1910 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1911 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1912 | |
| 1913 | // 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] | 1914 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1915 | frame_->EmitPush(r0); |
| 1916 | frame_->EmitPush(r3); // push entry |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1917 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1918 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1919 | |
| 1920 | // 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] | 1921 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1922 | __ cmp(r3, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1923 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1924 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1925 | end_del_check.Bind(); |
| 1926 | // Store the entry in the 'each' expression and take another spin in the |
| 1927 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1928 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1929 | { Reference each(this, node->each()); |
| 1930 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1931 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1932 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1933 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1934 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1935 | // If the reference was to a slot we rely on the convenient property |
| 1936 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1937 | // right on top of or right underneath a zero-sized reference. |
| 1938 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1939 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1940 | // It's safe to pop the value lying on top of the reference before |
| 1941 | // unloading the reference itself (which preserves the top of stack, |
| 1942 | // ie, now the topmost value of the non-zero sized reference), since |
| 1943 | // we will discard the top of stack after unloading the reference |
| 1944 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1945 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1946 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1949 | // Discard the i'th entry pushed above or else the remainder of the |
| 1950 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1951 | frame_->Drop(); |
| 1952 | |
| 1953 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1954 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1955 | VisitAndSpill(node->body()); |
| 1956 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1957 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1958 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1959 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1960 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1961 | frame_->EmitPop(r0); |
| 1962 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1963 | frame_->EmitPush(r0); |
| 1964 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1965 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1966 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1967 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1968 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1969 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1970 | |
| 1971 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1972 | exit.Bind(); |
| 1973 | node->continue_target()->Unuse(); |
| 1974 | node->break_target()->Unuse(); |
| 1975 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1979 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1980 | #ifdef DEBUG |
| 1981 | int original_height = frame_->height(); |
| 1982 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1983 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1984 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1985 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1986 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1987 | JumpTarget try_block; |
| 1988 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1989 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1990 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1991 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1992 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Store the caught exception in the catch variable. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 1995 | Variable* catch_var = node->catch_var()->var(); |
| 1996 | ASSERT(catch_var != NULL && catch_var->slot() != NULL); |
| 1997 | StoreToSlot(catch_var->slot(), NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1998 | |
| 1999 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2000 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2001 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2002 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2003 | if (frame_ != NULL) { |
| 2004 | exit.Jump(); |
| 2005 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2006 | |
| 2007 | |
| 2008 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2009 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2010 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2011 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2012 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2013 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2014 | // Shadow the labels for all escapes from the try block, including |
| 2015 | // returns. During shadowing, the original label is hidden as the |
| 2016 | // LabelShadow and operations on the original actually affect the |
| 2017 | // shadowing label. |
| 2018 | // |
| 2019 | // We should probably try to unify the escaping labels and the return |
| 2020 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2021 | int nof_escapes = node->escaping_targets()->length(); |
| 2022 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2023 | |
| 2024 | // Add the shadow target for the function return. |
| 2025 | static const int kReturnShadowIndex = 0; |
| 2026 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2027 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2028 | function_return_is_shadowed_ = true; |
| 2029 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2030 | |
| 2031 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2032 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2033 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2037 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2038 | |
| 2039 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2040 | // After shadowing stops, the original labels are unshadowed and the |
| 2041 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2042 | bool has_unlinks = false; |
| 2043 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2044 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2045 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2046 | } |
| 2047 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2048 | |
| 2049 | // Get an external reference to the handler address. |
| 2050 | ExternalReference handler_address(Top::k_handler_address); |
| 2051 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2052 | // If we can fall off the end of the try block, unlink from try chain. |
| 2053 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2054 | // The next handler address is on top of the frame. Unlink from |
| 2055 | // the handler list and drop the rest of this handler from the |
| 2056 | // frame. |
| 2057 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2058 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2059 | __ mov(r3, Operand(handler_address)); |
| 2060 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2061 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2062 | if (has_unlinks) { |
| 2063 | exit.Jump(); |
| 2064 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2067 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2068 | // jumped to. Deallocate each shadow target. |
| 2069 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2070 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2071 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2072 | shadows[i]->Bind(); |
| 2073 | // Because we can be jumping here (to spilled code) from unspilled |
| 2074 | // code, we need to reestablish a spilled frame at this block. |
| 2075 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2076 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2077 | // Reload sp from the top handler, because some statements that we |
| 2078 | // 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] | 2079 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2080 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2081 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2082 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2083 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2084 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2085 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2086 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2087 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2088 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2089 | frame_->PrepareForReturn(); |
| 2090 | } |
| 2091 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | } |
| 2093 | } |
| 2094 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2095 | exit.Bind(); |
| 2096 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2100 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2101 | #ifdef DEBUG |
| 2102 | int original_height = frame_->height(); |
| 2103 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2104 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2105 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2106 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2107 | |
| 2108 | // State: Used to keep track of reason for entering the finally |
| 2109 | // block. Should probably be extended to hold information for |
| 2110 | // break/continue from within the try block. |
| 2111 | enum { FALLING, THROWING, JUMPING }; |
| 2112 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2113 | JumpTarget try_block; |
| 2114 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2115 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2116 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2117 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2118 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2119 | // In case of thrown exceptions, this is where we continue. |
| 2120 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2121 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2122 | |
| 2123 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2124 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2125 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2126 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2127 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2128 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2129 | // Shadow the labels for all escapes from the try block, including |
| 2130 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2131 | // operations on the original actually affect the shadowing label. |
| 2132 | // |
| 2133 | // We should probably try to unify the escaping labels and the return |
| 2134 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2135 | int nof_escapes = node->escaping_targets()->length(); |
| 2136 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2137 | |
| 2138 | // Add the shadow target for the function return. |
| 2139 | static const int kReturnShadowIndex = 0; |
| 2140 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2141 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2142 | function_return_is_shadowed_ = true; |
| 2143 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2144 | |
| 2145 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2146 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2147 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2151 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2152 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2153 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2154 | // After shadowing stops, the original labels are unshadowed and the |
| 2155 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2156 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2157 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2158 | shadows[i]->StopShadowing(); |
| 2159 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2160 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2161 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2162 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2163 | // Get an external reference to the handler address. |
| 2164 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2165 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2166 | // If we can fall off the end of the try block, unlink from the try |
| 2167 | // chain and set the state on the frame to FALLING. |
| 2168 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2169 | // The next handler address is on top of the frame. |
| 2170 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2171 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2172 | __ mov(r3, Operand(handler_address)); |
| 2173 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2174 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2175 | |
| 2176 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2177 | // state in r2, then jump around the unlink blocks if any. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2178 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2179 | frame_->EmitPush(r0); |
| 2180 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2181 | if (nof_unlinks > 0) { |
| 2182 | finally_block.Jump(); |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | // Generate code to unlink and set the state for the (formerly) |
| 2187 | // shadowing targets that have been jumped to. |
| 2188 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2189 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2190 | // If we have come from the shadowed return, the return value is |
| 2191 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2192 | // until it is pushed. |
| 2193 | // |
| 2194 | // Because we can be jumping here (to spilled code) from |
| 2195 | // unspilled code, we need to reestablish a spilled frame at |
| 2196 | // this block. |
| 2197 | shadows[i]->Bind(); |
| 2198 | frame_->SpillAll(); |
| 2199 | |
| 2200 | // Reload sp from the top handler, because some statements that |
| 2201 | // we break from (eg, for...in) may have left stuff on the |
| 2202 | // stack. |
| 2203 | __ mov(r3, Operand(handler_address)); |
| 2204 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2205 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2206 | |
| 2207 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2208 | // handler address is currently on top of the frame. |
| 2209 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2210 | frame_->EmitPop(r1); |
| 2211 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2212 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2213 | |
| 2214 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2215 | // If this label shadowed the function return, materialize the |
| 2216 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2217 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2218 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2219 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2220 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2221 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2222 | } |
| 2223 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2224 | if (--nof_unlinks > 0) { |
| 2225 | // If this is not the last unlink block, jump around the next. |
| 2226 | finally_block.Jump(); |
| 2227 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2231 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2232 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2233 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2234 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2235 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2236 | |
| 2237 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2238 | // and the state - while evaluating the finally block. |
| 2239 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2240 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2241 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2242 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2243 | if (has_valid_frame()) { |
| 2244 | // Restore state and return value or faked TOS. |
| 2245 | frame_->EmitPop(r2); |
| 2246 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2249 | // Generate code to jump to the right destination for all used |
| 2250 | // formerly shadowing targets. Deallocate each shadow target. |
| 2251 | for (int i = 0; i < shadows.length(); i++) { |
| 2252 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2253 | JumpTarget* original = shadows[i]->other_target(); |
| 2254 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2255 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2256 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2257 | skip.Branch(ne); |
| 2258 | frame_->PrepareForReturn(); |
| 2259 | original->Jump(); |
| 2260 | skip.Bind(); |
| 2261 | } else { |
| 2262 | original->Branch(eq); |
| 2263 | } |
| 2264 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2265 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2266 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2267 | if (has_valid_frame()) { |
| 2268 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2269 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2270 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2271 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2272 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2273 | // Rethrow exception. |
| 2274 | frame_->EmitPush(r0); |
| 2275 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2276 | |
| 2277 | // Done. |
| 2278 | exit.Bind(); |
| 2279 | } |
| 2280 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2284 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2285 | #ifdef DEBUG |
| 2286 | int original_height = frame_->height(); |
| 2287 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2288 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2289 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2290 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2291 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2292 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2293 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2294 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2295 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2299 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2300 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2301 | ASSERT(boilerplate->IsBoilerplate()); |
| 2302 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2303 | __ mov(r0, Operand(boilerplate)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 2304 | // Use the fast case closure allocation code that allocates in new |
| 2305 | // space for nested functions that don't need literals cloning. |
| 2306 | if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) { |
| 2307 | FastNewClosureStub stub; |
| 2308 | frame_->EmitPush(r0); |
| 2309 | frame_->CallStub(&stub, 1); |
| 2310 | frame_->EmitPush(r0); |
| 2311 | } else { |
| 2312 | // Create a new closure. |
| 2313 | frame_->EmitPush(cp); |
| 2314 | frame_->EmitPush(r0); |
| 2315 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2316 | frame_->EmitPush(r0); |
| 2317 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2321 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2322 | #ifdef DEBUG |
| 2323 | int original_height = frame_->height(); |
| 2324 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2325 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2326 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2327 | |
| 2328 | // Build the function boilerplate and instantiate it. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2329 | Handle<JSFunction> boilerplate = |
| 2330 | Compiler::BuildBoilerplate(node, script_, this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2331 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2332 | if (HasStackOverflow()) { |
| 2333 | ASSERT(frame_->height() == original_height); |
| 2334 | return; |
| 2335 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2336 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2337 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2341 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2342 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2343 | #ifdef DEBUG |
| 2344 | int original_height = frame_->height(); |
| 2345 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2346 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2347 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2348 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2349 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2353 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2354 | #ifdef DEBUG |
| 2355 | int original_height = frame_->height(); |
| 2356 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2357 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2358 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2359 | JumpTarget then; |
| 2360 | JumpTarget else_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2361 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2362 | if (has_valid_frame()) { |
| 2363 | Branch(false, &else_); |
| 2364 | } |
| 2365 | if (has_valid_frame() || then.is_linked()) { |
| 2366 | then.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2367 | LoadAndSpill(node->then_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2368 | } |
| 2369 | if (else_.is_linked()) { |
| 2370 | JumpTarget exit; |
| 2371 | if (has_valid_frame()) exit.Jump(); |
| 2372 | else_.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2373 | LoadAndSpill(node->else_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2374 | if (exit.is_linked()) exit.Bind(); |
| 2375 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2376 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2380 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2381 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2382 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2383 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2384 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2385 | JumpTarget slow; |
| 2386 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2387 | |
| 2388 | // Generate fast-case code for variables that might be shadowed by |
| 2389 | // eval-introduced variables. Eval is used a lot without |
| 2390 | // introducing variables. In those cases, we do not want to |
| 2391 | // perform a runtime call for all variables in the scope |
| 2392 | // containing the eval. |
| 2393 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2394 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2395 | // If there was no control flow to slow, we can exit early. |
| 2396 | if (!slow.is_linked()) { |
| 2397 | frame_->EmitPush(r0); |
| 2398 | return; |
| 2399 | } |
| 2400 | |
| 2401 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2402 | |
| 2403 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2404 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2405 | // Only generate the fast case for locals that rewrite to slots. |
| 2406 | // This rules out argument loads. |
| 2407 | if (potential_slot != NULL) { |
| 2408 | __ ldr(r0, |
| 2409 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2410 | r1, |
| 2411 | r2, |
| 2412 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2413 | if (potential_slot->var()->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2414 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2415 | __ cmp(r0, ip); |
| 2416 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2417 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2418 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2419 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2420 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2421 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2425 | slow.Bind(); |
| 2426 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2427 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2428 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2429 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2430 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2431 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2432 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2433 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2434 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2435 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2436 | done.Bind(); |
| 2437 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2438 | |
| 2439 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2440 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2441 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2442 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2443 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2444 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2445 | // initialized yet) which needs to be converted into the 'undefined' |
| 2446 | // value. |
| 2447 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2448 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2449 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2450 | __ cmp(r0, ip); |
| 2451 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2452 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 2458 | void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) { |
| 2459 | ASSERT(slot != NULL); |
| 2460 | if (slot->type() == Slot::LOOKUP) { |
| 2461 | ASSERT(slot->var()->is_dynamic()); |
| 2462 | |
| 2463 | // For now, just do a runtime call. |
| 2464 | frame_->EmitPush(cp); |
| 2465 | __ mov(r0, Operand(slot->var()->name())); |
| 2466 | frame_->EmitPush(r0); |
| 2467 | |
| 2468 | if (init_state == CONST_INIT) { |
| 2469 | // Same as the case for a normal store, but ignores attribute |
| 2470 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 2471 | // const properties (introduced via eval("const foo = (some |
| 2472 | // expr);")). Also, uses the current function context instead of |
| 2473 | // the top context. |
| 2474 | // |
| 2475 | // Note that we must declare the foo upon entry of eval(), via a |
| 2476 | // context slot declaration, but we cannot initialize it at the |
| 2477 | // same time, because the const declaration may be at the end of |
| 2478 | // the eval code (sigh...) and the const variable may have been |
| 2479 | // used before (where its value is 'undefined'). Thus, we can only |
| 2480 | // do the initialization when we actually encounter the expression |
| 2481 | // and when the expression operands are defined and valid, and |
| 2482 | // thus we need the split into 2 operations: declaration of the |
| 2483 | // context slot followed by initialization. |
| 2484 | frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 2485 | } else { |
| 2486 | frame_->CallRuntime(Runtime::kStoreContextSlot, 3); |
| 2487 | } |
| 2488 | // Storing a variable must keep the (new) value on the expression |
| 2489 | // stack. This is necessary for compiling assignment expressions. |
| 2490 | frame_->EmitPush(r0); |
| 2491 | |
| 2492 | } else { |
| 2493 | ASSERT(!slot->var()->is_dynamic()); |
| 2494 | |
| 2495 | JumpTarget exit; |
| 2496 | if (init_state == CONST_INIT) { |
| 2497 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 2498 | // Only the first const initialization must be executed (the slot |
| 2499 | // still contains 'the hole' value). When the assignment is |
| 2500 | // executed, the code is identical to a normal store (see below). |
| 2501 | Comment cmnt(masm_, "[ Init const"); |
| 2502 | __ ldr(r2, SlotOperand(slot, r2)); |
| 2503 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2504 | __ cmp(r2, ip); |
| 2505 | exit.Branch(ne); |
| 2506 | } |
| 2507 | |
| 2508 | // We must execute the store. Storing a variable must keep the |
| 2509 | // (new) value on the stack. This is necessary for compiling |
| 2510 | // assignment expressions. |
| 2511 | // |
| 2512 | // Note: We will reach here even with slot->var()->mode() == |
| 2513 | // Variable::CONST because of const declarations which will |
| 2514 | // initialize consts to 'the hole' value and by doing so, end up |
| 2515 | // calling this code. r2 may be loaded with context; used below in |
| 2516 | // RecordWrite. |
| 2517 | frame_->EmitPop(r0); |
| 2518 | __ str(r0, SlotOperand(slot, r2)); |
| 2519 | frame_->EmitPush(r0); |
| 2520 | if (slot->type() == Slot::CONTEXT) { |
| 2521 | // Skip write barrier if the written value is a smi. |
| 2522 | __ tst(r0, Operand(kSmiTagMask)); |
| 2523 | exit.Branch(eq); |
| 2524 | // r2 is loaded with context when calling SlotOperand above. |
| 2525 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 2526 | __ mov(r3, Operand(offset)); |
| 2527 | __ RecordWrite(r2, r3, r1); |
| 2528 | } |
| 2529 | // If we definitely did not jump over the assignment, we do not need |
| 2530 | // to bind the exit label. Doing so can defeat peephole |
| 2531 | // optimization. |
| 2532 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
| 2533 | exit.Bind(); |
| 2534 | } |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2539 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2540 | TypeofState typeof_state, |
| 2541 | Register tmp, |
| 2542 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2543 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2544 | // Check that no extension objects have been created by calls to |
| 2545 | // eval from the current scope to the global scope. |
| 2546 | Register context = cp; |
| 2547 | Scope* s = scope(); |
| 2548 | while (s != NULL) { |
| 2549 | if (s->num_heap_slots() > 0) { |
| 2550 | if (s->calls_eval()) { |
| 2551 | // Check that extension is NULL. |
| 2552 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2553 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2554 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2555 | } |
| 2556 | // Load next context in chain. |
| 2557 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2558 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2559 | context = tmp; |
| 2560 | } |
| 2561 | // If no outer scope calls eval, we do not need to check more |
| 2562 | // context extensions. |
| 2563 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2564 | s = s->outer_scope(); |
| 2565 | } |
| 2566 | |
| 2567 | if (s->is_eval_scope()) { |
| 2568 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2569 | if (!context.is(tmp)) { |
| 2570 | __ mov(tmp, Operand(context)); |
| 2571 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2572 | __ bind(&next); |
| 2573 | // Terminate at global context. |
| 2574 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2575 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2576 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2577 | __ b(eq, &fast); |
| 2578 | // Check that extension is NULL. |
| 2579 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2580 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2581 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2582 | // Load next context in chain. |
| 2583 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2584 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2585 | __ b(&next); |
| 2586 | __ bind(&fast); |
| 2587 | } |
| 2588 | |
| 2589 | // All extension objects were empty and it is safe to use a global |
| 2590 | // load IC call. |
| 2591 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2592 | // Load the global object. |
| 2593 | LoadGlobal(); |
| 2594 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2595 | Result name(r2); |
| 2596 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2597 | // Call IC stub. |
| 2598 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2599 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2600 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2601 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2604 | // Drop the global object. The result is in r0. |
| 2605 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2609 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2610 | #ifdef DEBUG |
| 2611 | int original_height = frame_->height(); |
| 2612 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2613 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2614 | Comment cmnt(masm_, "[ Slot"); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2615 | LoadFromSlot(node, NOT_INSIDE_TYPEOF); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2616 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2617 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2618 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2619 | |
| 2620 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2621 | #ifdef DEBUG |
| 2622 | int original_height = frame_->height(); |
| 2623 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2624 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2625 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2626 | |
| 2627 | Variable* var = node->var(); |
| 2628 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2629 | if (expr != NULL) { |
| 2630 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2631 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2632 | ASSERT(var->is_global()); |
| 2633 | Reference ref(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2634 | ref.GetValueAndSpill(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2635 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2636 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2640 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2641 | #ifdef DEBUG |
| 2642 | int original_height = frame_->height(); |
| 2643 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2644 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2645 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2646 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2647 | frame_->EmitPush(r0); |
| 2648 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2652 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2653 | #ifdef DEBUG |
| 2654 | int original_height = frame_->height(); |
| 2655 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2656 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2657 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2658 | |
| 2659 | // Retrieve the literal array and check the allocated entry. |
| 2660 | |
| 2661 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2662 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2663 | |
| 2664 | // Load the literals array of the function. |
| 2665 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2666 | |
| 2667 | // Load the literal at the ast saved index. |
| 2668 | int literal_offset = |
| 2669 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2670 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2671 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2672 | JumpTarget done; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2673 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2674 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2675 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2676 | |
| 2677 | // If the entry is undefined we call the runtime system to computed |
| 2678 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2679 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2680 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2681 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2682 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2683 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2684 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2685 | frame_->EmitPush(r0); |
| 2686 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2687 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2688 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2689 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2690 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2691 | frame_->EmitPush(r2); |
| 2692 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2696 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2697 | #ifdef DEBUG |
| 2698 | int original_height = frame_->height(); |
| 2699 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2700 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2701 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2702 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2703 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 2704 | __ ldr(r2, frame_->Function()); |
| 2705 | // Literal array. |
| 2706 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
| 2707 | // Literal index. |
| 2708 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
| 2709 | // Constant properties. |
| 2710 | __ mov(r0, Operand(node->constant_properties())); |
| 2711 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
| 2712 | if (node->depth() > 1) { |
| 2713 | frame_->CallRuntime(Runtime::kCreateObjectLiteral, 3); |
| 2714 | } else { |
| 2715 | frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2716 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2717 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 2718 | // r0: created object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2719 | |
| 2720 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2721 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2722 | Literal* key = property->key(); |
| 2723 | Expression* value = property->value(); |
| 2724 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2725 | case ObjectLiteral::Property::CONSTANT: |
| 2726 | break; |
| 2727 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2728 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2729 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2730 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2731 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2732 | frame_->EmitPush(r0); // dup the result |
| 2733 | LoadAndSpill(key); |
| 2734 | LoadAndSpill(value); |
| 2735 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2736 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2737 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2738 | break; |
| 2739 | } |
| 2740 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2741 | frame_->EmitPush(r0); |
| 2742 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2743 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2744 | frame_->EmitPush(r0); |
| 2745 | LoadAndSpill(value); |
| 2746 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2747 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2748 | break; |
| 2749 | } |
| 2750 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2751 | frame_->EmitPush(r0); |
| 2752 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2753 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2754 | frame_->EmitPush(r0); |
| 2755 | LoadAndSpill(value); |
| 2756 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2757 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2758 | break; |
| 2759 | } |
| 2760 | } |
| 2761 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2762 | ASSERT(frame_->height() == original_height + 1); |
| 2763 | } |
| 2764 | |
| 2765 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2766 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2767 | #ifdef DEBUG |
| 2768 | int original_height = frame_->height(); |
| 2769 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2770 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2771 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2772 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2773 | // Load the function of this activation. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 2774 | __ ldr(r2, frame_->Function()); |
| 2775 | // Literals array. |
| 2776 | __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset)); |
| 2777 | // Literal index. |
| 2778 | __ mov(r1, Operand(Smi::FromInt(node->literal_index()))); |
| 2779 | // Constant elements. |
| 2780 | __ mov(r0, Operand(node->constant_elements())); |
| 2781 | frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit()); |
| 2782 | if (node->depth() > 1) { |
| 2783 | frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3); |
| 2784 | } else { |
| 2785 | frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2786 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2787 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 2788 | // r0: created object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2789 | |
| 2790 | // Generate code to set the elements in the array that are not |
| 2791 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2792 | for (int i = 0; i < node->values()->length(); i++) { |
| 2793 | Expression* value = node->values()->at(i); |
| 2794 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2795 | // If value is a literal the property value is already set in the |
| 2796 | // boilerplate object. |
| 2797 | if (value->AsLiteral() != NULL) continue; |
| 2798 | // If value is a materialized literal the property value is already set |
| 2799 | // in the boilerplate object if it is simple. |
| 2800 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2801 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2802 | // The property must be set by generated code. |
| 2803 | LoadAndSpill(value); |
| 2804 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2805 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2806 | // Fetch the object literal. |
| 2807 | __ ldr(r1, frame_->Top()); |
| 2808 | // Get the elements array. |
| 2809 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2810 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2811 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2812 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2813 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2814 | |
| 2815 | // Update the write barrier for the array address. |
| 2816 | __ mov(r3, Operand(offset)); |
| 2817 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2818 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2819 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2823 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2824 | #ifdef DEBUG |
| 2825 | int original_height = frame_->height(); |
| 2826 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2827 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2828 | // Call runtime routine to allocate the catch extension object and |
| 2829 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2830 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2831 | LoadAndSpill(node->key()); |
| 2832 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2833 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2834 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2835 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2839 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2840 | #ifdef DEBUG |
| 2841 | int original_height = frame_->height(); |
| 2842 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2843 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2844 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2845 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2846 | { Reference target(this, node->target()); |
| 2847 | if (target.is_illegal()) { |
| 2848 | // Fool the virtual frame into thinking that we left the assignment's |
| 2849 | // value on the frame. |
| 2850 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2851 | frame_->EmitPush(r0); |
| 2852 | ASSERT(frame_->height() == original_height + 1); |
| 2853 | return; |
| 2854 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2855 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2856 | if (node->op() == Token::ASSIGN || |
| 2857 | node->op() == Token::INIT_VAR || |
| 2858 | node->op() == Token::INIT_CONST) { |
| 2859 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2860 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2861 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2862 | // +=, *= and similar binary assignments. |
| 2863 | // Get the old value of the lhs. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2864 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2865 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2866 | bool overwrite = |
| 2867 | (node->value()->AsBinaryOperation() != NULL && |
| 2868 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2869 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2870 | SmiOperation(node->binary_op(), |
| 2871 | literal->handle(), |
| 2872 | false, |
| 2873 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2874 | frame_->EmitPush(r0); |
| 2875 | |
| 2876 | } else { |
| 2877 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2878 | GenericBinaryOperation(node->binary_op(), |
| 2879 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2880 | frame_->EmitPush(r0); |
| 2881 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2882 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2883 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2884 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2885 | if (var != NULL && |
| 2886 | (var->mode() == Variable::CONST) && |
| 2887 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2888 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2889 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2890 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2891 | CodeForSourcePosition(node->position()); |
| 2892 | if (node->op() == Token::INIT_CONST) { |
| 2893 | // Dynamic constant initializations must use the function context |
| 2894 | // and initialize the actual constant declared. Dynamic variable |
| 2895 | // initializations are simply assignments and use SetValue. |
| 2896 | target.SetValue(CONST_INIT); |
| 2897 | } else { |
| 2898 | target.SetValue(NOT_CONST_INIT); |
| 2899 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2900 | } |
| 2901 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2902 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2906 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2907 | #ifdef DEBUG |
| 2908 | int original_height = frame_->height(); |
| 2909 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2910 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2911 | Comment cmnt(masm_, "[ Throw"); |
| 2912 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2913 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2914 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2915 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2916 | frame_->EmitPush(r0); |
| 2917 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
| 2920 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2921 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2922 | #ifdef DEBUG |
| 2923 | int original_height = frame_->height(); |
| 2924 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2925 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2926 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2927 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2928 | { Reference property(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2929 | property.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2930 | } |
| 2931 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2935 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2936 | #ifdef DEBUG |
| 2937 | int original_height = frame_->height(); |
| 2938 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2939 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2940 | Comment cmnt(masm_, "[ Call"); |
| 2941 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2942 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2943 | ZoneList<Expression*>* args = node->arguments(); |
| 2944 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2945 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2946 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2947 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2948 | Property* property = function->AsProperty(); |
| 2949 | |
| 2950 | // ------------------------------------------------------------------------ |
| 2951 | // Fast-case: Use inline caching. |
| 2952 | // --- |
| 2953 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2954 | // must be resolved after the arguments have been evaluated. The IC code |
| 2955 | // automatically handles this by loading the arguments before the function |
| 2956 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2957 | // ------------------------------------------------------------------------ |
| 2958 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2959 | if (var != NULL && var->is_possibly_eval()) { |
| 2960 | // ---------------------------------- |
| 2961 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 2962 | // ---------------------------------- |
| 2963 | |
| 2964 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 2965 | // resolve the function we need to call and the receiver of the |
| 2966 | // call. Then we call the resolved function using the given |
| 2967 | // arguments. |
| 2968 | // Prepare stack for call to resolved function. |
| 2969 | LoadAndSpill(function); |
| 2970 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 2971 | frame_->EmitPush(r2); // Slot for receiver |
| 2972 | int arg_count = args->length(); |
| 2973 | for (int i = 0; i < arg_count; i++) { |
| 2974 | LoadAndSpill(args->at(i)); |
| 2975 | } |
| 2976 | |
| 2977 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 2978 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 2979 | frame_->EmitPush(r1); |
| 2980 | if (arg_count > 0) { |
| 2981 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 2982 | frame_->EmitPush(r1); |
| 2983 | } else { |
| 2984 | frame_->EmitPush(r2); |
| 2985 | } |
| 2986 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 2987 | // Push the receiver. |
| 2988 | __ ldr(r1, frame_->Receiver()); |
| 2989 | frame_->EmitPush(r1); |
| 2990 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2991 | // Resolve the call. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 2992 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2993 | |
| 2994 | // 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^] | 2995 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2996 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 2997 | |
| 2998 | // Call the function. |
| 2999 | CodeForSourcePosition(node->position()); |
| 3000 | |
| 3001 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3002 | CallFunctionStub call_function(arg_count, in_loop); |
| 3003 | frame_->CallStub(&call_function, arg_count + 1); |
| 3004 | |
| 3005 | __ ldr(cp, frame_->Context()); |
| 3006 | // Remove the function from the stack. |
| 3007 | frame_->Drop(); |
| 3008 | frame_->EmitPush(r0); |
| 3009 | |
| 3010 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3011 | // ---------------------------------- |
| 3012 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3013 | // ---------------------------------- |
| 3014 | |
| 3015 | // Push the name of the function and the receiver onto the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3016 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3017 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3018 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3019 | // Pass the global object as the receiver and let the IC stub |
| 3020 | // patch the stack to use the global proxy as 'this' in the |
| 3021 | // invoked function. |
| 3022 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3023 | |
| 3024 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3025 | int arg_count = args->length(); |
| 3026 | for (int i = 0; i < arg_count; i++) { |
| 3027 | LoadAndSpill(args->at(i)); |
| 3028 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3029 | |
| 3030 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3031 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3032 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3033 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3034 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3035 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3036 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3037 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3038 | frame_->Drop(); |
| 3039 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3040 | |
| 3041 | } else if (var != NULL && var->slot() != NULL && |
| 3042 | var->slot()->type() == Slot::LOOKUP) { |
| 3043 | // ---------------------------------- |
| 3044 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3045 | // ---------------------------------- |
| 3046 | |
| 3047 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3048 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3049 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3050 | frame_->EmitPush(r0); |
| 3051 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3052 | // r0: slot value; r1: receiver |
| 3053 | |
| 3054 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3055 | frame_->EmitPush(r0); // function |
| 3056 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3057 | |
| 3058 | // Call the function. |
| 3059 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3060 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3061 | |
| 3062 | } else if (property != NULL) { |
| 3063 | // Check if the key is a literal string. |
| 3064 | Literal* literal = property->key()->AsLiteral(); |
| 3065 | |
| 3066 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3067 | // ------------------------------------------------------------------ |
| 3068 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3069 | // ------------------------------------------------------------------ |
| 3070 | |
| 3071 | // Push the name of the function and the receiver onto the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3072 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3073 | frame_->EmitPush(r0); |
| 3074 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3075 | |
| 3076 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3077 | int arg_count = args->length(); |
| 3078 | for (int i = 0; i < arg_count; i++) { |
| 3079 | LoadAndSpill(args->at(i)); |
| 3080 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3081 | |
| 3082 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3083 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3084 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3085 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3086 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3087 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3088 | |
| 3089 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3090 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3091 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3092 | frame_->EmitPush(r0); // push after get rid of function from the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3093 | |
| 3094 | } else { |
| 3095 | // ------------------------------------------- |
| 3096 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3097 | // ------------------------------------------- |
| 3098 | |
| 3099 | // Load the function to call from the property through a reference. |
| 3100 | Reference ref(this, property); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3101 | ref.GetValueAndSpill(); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3102 | |
| 3103 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3104 | if (property->is_synthetic()) { |
| 3105 | LoadGlobalReceiver(r0); |
| 3106 | } else { |
| 3107 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3108 | frame_->EmitPush(r0); |
| 3109 | } |
| 3110 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3111 | // Call the function. |
| 3112 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3113 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | } else { |
| 3117 | // ---------------------------------- |
| 3118 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3119 | // ---------------------------------- |
| 3120 | |
| 3121 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3122 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3123 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3124 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3125 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3126 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3127 | // Call the function. |
| 3128 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3129 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3130 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3131 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3135 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3136 | #ifdef DEBUG |
| 3137 | int original_height = frame_->height(); |
| 3138 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3139 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3140 | Comment cmnt(masm_, "[ CallNew"); |
| 3141 | |
| 3142 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3143 | // expression in new calls must be evaluated before the |
| 3144 | // arguments. This is different from ordinary calls, where the |
| 3145 | // actual function to call is resolved after the arguments have been |
| 3146 | // evaluated. |
| 3147 | |
| 3148 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3149 | // receiver. There is no need to use the global proxy here because |
| 3150 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3151 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3152 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3153 | |
| 3154 | // Push the arguments ("left-to-right") on the stack. |
| 3155 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3156 | int arg_count = args->length(); |
| 3157 | for (int i = 0; i < arg_count; i++) { |
| 3158 | LoadAndSpill(args->at(i)); |
| 3159 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3160 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3161 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3162 | Result num_args(r0); |
| 3163 | __ mov(r0, Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3164 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3165 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3166 | Result function(r1); |
| 3167 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3168 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3169 | // Call the construct call builtin that handles allocation and |
| 3170 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3171 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3172 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3173 | frame_->CallCodeObject(ic, |
| 3174 | RelocInfo::CONSTRUCT_CALL, |
| 3175 | &num_args, |
| 3176 | &function, |
| 3177 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3178 | |
| 3179 | // 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] | 3180 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3181 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3185 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3186 | VirtualFrame::SpilledScope spilled_scope; |
| 3187 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3188 | JumpTarget leave, null, function, non_function_constructor; |
| 3189 | |
| 3190 | // Load the object into r0. |
| 3191 | LoadAndSpill(args->at(0)); |
| 3192 | frame_->EmitPop(r0); |
| 3193 | |
| 3194 | // If the object is a smi, we return null. |
| 3195 | __ tst(r0, Operand(kSmiTagMask)); |
| 3196 | null.Branch(eq); |
| 3197 | |
| 3198 | // Check that the object is a JS object but take special care of JS |
| 3199 | // functions to make sure they have 'Function' as their class. |
| 3200 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3201 | null.Branch(lt); |
| 3202 | |
| 3203 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3204 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3205 | // LAST_JS_OBJECT_TYPE. |
| 3206 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3207 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3208 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3209 | function.Branch(eq); |
| 3210 | |
| 3211 | // Check if the constructor in the map is a function. |
| 3212 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3213 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3214 | non_function_constructor.Branch(ne); |
| 3215 | |
| 3216 | // The r0 register now contains the constructor function. Grab the |
| 3217 | // instance class name from there. |
| 3218 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3219 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3220 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3221 | leave.Jump(); |
| 3222 | |
| 3223 | // Functions have class 'Function'. |
| 3224 | function.Bind(); |
| 3225 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3226 | frame_->EmitPush(r0); |
| 3227 | leave.Jump(); |
| 3228 | |
| 3229 | // Objects with a non-function constructor have class 'Object'. |
| 3230 | non_function_constructor.Bind(); |
| 3231 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3232 | frame_->EmitPush(r0); |
| 3233 | leave.Jump(); |
| 3234 | |
| 3235 | // Non-JS objects have class null. |
| 3236 | null.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3237 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3238 | frame_->EmitPush(r0); |
| 3239 | |
| 3240 | // All done. |
| 3241 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3245 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3246 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3247 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3248 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3249 | LoadAndSpill(args->at(0)); |
| 3250 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3251 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3252 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3253 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3254 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3255 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3256 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3257 | // Load the value. |
| 3258 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3259 | leave.Bind(); |
| 3260 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3264 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3265 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3266 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3267 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3268 | LoadAndSpill(args->at(0)); // Load the object. |
| 3269 | LoadAndSpill(args->at(1)); // Load the value. |
| 3270 | frame_->EmitPop(r0); // r0 contains value |
| 3271 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3272 | // if (object->IsSmi()) return object. |
| 3273 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3274 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3275 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3276 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3277 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3278 | // Store the value. |
| 3279 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3280 | // Update the write barrier. |
| 3281 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3282 | __ RecordWrite(r1, r2, r3); |
| 3283 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3284 | leave.Bind(); |
| 3285 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3289 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3290 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3291 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3292 | LoadAndSpill(args->at(0)); |
| 3293 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3294 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3295 | cc_reg_ = eq; |
| 3296 | } |
| 3297 | |
| 3298 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3299 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3300 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3301 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3302 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3303 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3304 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3305 | LoadAndSpill(args->at(1)); |
| 3306 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3307 | __ CallRuntime(Runtime::kLog, 2); |
| 3308 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3309 | #endif |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3310 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3311 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3315 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3316 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3317 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3318 | LoadAndSpill(args->at(0)); |
| 3319 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3320 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3321 | cc_reg_ = eq; |
| 3322 | } |
| 3323 | |
| 3324 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3325 | // This should generate code that performs a charCodeAt() call or returns |
| 3326 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3327 | // 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] | 3328 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3329 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3330 | ASSERT(args->length() == 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3331 | Comment(masm_, "[ GenerateFastCharCodeAt"); |
| 3332 | |
| 3333 | LoadAndSpill(args->at(0)); |
| 3334 | LoadAndSpill(args->at(1)); |
| 3335 | frame_->EmitPop(r0); // Index. |
| 3336 | frame_->EmitPop(r1); // String. |
| 3337 | |
| 3338 | Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string; |
| 3339 | |
| 3340 | __ tst(r1, Operand(kSmiTagMask)); |
| 3341 | __ b(eq, &slow); // The 'string' was a Smi. |
| 3342 | |
| 3343 | ASSERT(kSmiTag == 0); |
| 3344 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
| 3345 | __ b(ne, &slow); // The index was negative or not a Smi. |
| 3346 | |
| 3347 | __ bind(&try_again_with_new_string); |
| 3348 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 3349 | __ b(ge, &slow); |
| 3350 | |
| 3351 | // Now r2 has the string type. |
| 3352 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3353 | // Now r3 has the length of the string. Compare with the index. |
| 3354 | __ cmp(r3, Operand(r0, LSR, kSmiTagSize)); |
| 3355 | __ b(le, &slow); |
| 3356 | |
| 3357 | // Here we know the index is in range. Check that string is sequential. |
| 3358 | ASSERT_EQ(0, kSeqStringTag); |
| 3359 | __ tst(r2, Operand(kStringRepresentationMask)); |
| 3360 | __ b(ne, ¬_a_flat_string); |
| 3361 | |
| 3362 | // Check whether it is an ASCII string. |
| 3363 | ASSERT_EQ(0, kTwoByteStringTag); |
| 3364 | __ tst(r2, Operand(kStringEncodingMask)); |
| 3365 | __ b(ne, &ascii_string); |
| 3366 | |
| 3367 | // 2-byte string. We can add without shifting since the Smi tag size is the |
| 3368 | // log2 of the number of bytes in a two-byte character. |
| 3369 | ASSERT_EQ(1, kSmiTagSize); |
| 3370 | ASSERT_EQ(0, kSmiShiftSize); |
| 3371 | __ add(r1, r1, Operand(r0)); |
| 3372 | __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize)); |
| 3373 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3374 | __ jmp(&end); |
| 3375 | |
| 3376 | __ bind(&ascii_string); |
| 3377 | __ add(r1, r1, Operand(r0, LSR, kSmiTagSize)); |
| 3378 | __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 3379 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3380 | __ jmp(&end); |
| 3381 | |
| 3382 | __ bind(¬_a_flat_string); |
| 3383 | __ and_(r2, r2, Operand(kStringRepresentationMask)); |
| 3384 | __ cmp(r2, Operand(kConsStringTag)); |
| 3385 | __ b(ne, &slow); |
| 3386 | |
| 3387 | // ConsString. |
| 3388 | // Check that the right hand side is the empty string (ie if this is really a |
| 3389 | // flat string in a cons string). If that is not the case we would rather go |
| 3390 | // to the runtime system now, to flatten the string. |
| 3391 | __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset)); |
| 3392 | __ LoadRoot(r3, Heap::kEmptyStringRootIndex); |
| 3393 | __ cmp(r2, Operand(r3)); |
| 3394 | __ b(ne, &slow); |
| 3395 | |
| 3396 | // Get the first of the two strings. |
| 3397 | __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset)); |
| 3398 | __ jmp(&try_again_with_new_string); |
| 3399 | |
| 3400 | __ bind(&slow); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3401 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3402 | |
| 3403 | __ bind(&end); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3404 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3408 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3409 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3410 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3411 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3412 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3413 | // We need the CC bits to come out as not_equal in the case where the |
| 3414 | // object is a smi. This can't be done with the usual test opcode so |
| 3415 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3416 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3417 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3418 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3419 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3420 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3421 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3422 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3423 | cc_reg_ = eq; |
| 3424 | } |
| 3425 | |
| 3426 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3427 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 3428 | // This generates a fast version of: |
| 3429 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
| 3430 | VirtualFrame::SpilledScope spilled_scope; |
| 3431 | ASSERT(args->length() == 1); |
| 3432 | LoadAndSpill(args->at(0)); |
| 3433 | frame_->EmitPop(r1); |
| 3434 | __ tst(r1, Operand(kSmiTagMask)); |
| 3435 | false_target()->Branch(eq); |
| 3436 | |
| 3437 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 3438 | __ cmp(r1, ip); |
| 3439 | true_target()->Branch(eq); |
| 3440 | |
| 3441 | Register map_reg = r2; |
| 3442 | __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3443 | // Undetectable objects behave like undefined when tested with typeof. |
| 3444 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 3445 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3446 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 3447 | false_target()->Branch(eq); |
| 3448 | |
| 3449 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 3450 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3451 | false_target()->Branch(lt); |
| 3452 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
| 3453 | cc_reg_ = le; |
| 3454 | } |
| 3455 | |
| 3456 | |
| 3457 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 3458 | // This generates a fast version of: |
| 3459 | // (%_ClassOf(arg) === 'Function') |
| 3460 | VirtualFrame::SpilledScope spilled_scope; |
| 3461 | ASSERT(args->length() == 1); |
| 3462 | LoadAndSpill(args->at(0)); |
| 3463 | frame_->EmitPop(r0); |
| 3464 | __ tst(r0, Operand(kSmiTagMask)); |
| 3465 | false_target()->Branch(eq); |
| 3466 | Register map_reg = r2; |
| 3467 | __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE); |
| 3468 | cc_reg_ = eq; |
| 3469 | } |
| 3470 | |
| 3471 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3472 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3473 | VirtualFrame::SpilledScope spilled_scope; |
| 3474 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3475 | |
| 3476 | // Get the frame pointer for the calling frame. |
| 3477 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3478 | |
| 3479 | // Skip the arguments adaptor frame if it exists. |
| 3480 | Label check_frame_marker; |
| 3481 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3482 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3483 | __ b(ne, &check_frame_marker); |
| 3484 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3485 | |
| 3486 | // Check the marker in the calling frame. |
| 3487 | __ bind(&check_frame_marker); |
| 3488 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3489 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3490 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3494 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3495 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3496 | ASSERT(args->length() == 0); |
| 3497 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3498 | // Seed the result with the formal parameters count, which will be used |
| 3499 | // in case no arguments adaptor frame is found below the current frame. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3500 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3501 | |
| 3502 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3503 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3504 | frame_->CallStub(&stub, 0); |
| 3505 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3509 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3510 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3511 | ASSERT(args->length() == 1); |
| 3512 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3513 | // Satisfy contract with ArgumentsAccessStub: |
| 3514 | // 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] | 3515 | LoadAndSpill(args->at(0)); |
| 3516 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3517 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3518 | |
| 3519 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3520 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3521 | frame_->CallStub(&stub, 0); |
| 3522 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3526 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3527 | VirtualFrame::SpilledScope spilled_scope; |
| 3528 | ASSERT(args->length() == 0); |
| 3529 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3530 | RelocInfo::RUNTIME_ENTRY); |
| 3531 | frame_->EmitPush(r0); |
| 3532 | } |
| 3533 | |
| 3534 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3535 | void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) { |
| 3536 | ASSERT_EQ(2, args->length()); |
| 3537 | |
| 3538 | Load(args->at(0)); |
| 3539 | Load(args->at(1)); |
| 3540 | |
| 3541 | frame_->CallRuntime(Runtime::kStringAdd, 2); |
| 3542 | frame_->EmitPush(r0); |
| 3543 | } |
| 3544 | |
| 3545 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 3546 | void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) { |
| 3547 | ASSERT_EQ(3, args->length()); |
| 3548 | |
| 3549 | Load(args->at(0)); |
| 3550 | Load(args->at(1)); |
| 3551 | Load(args->at(2)); |
| 3552 | |
| 3553 | frame_->CallRuntime(Runtime::kSubString, 3); |
| 3554 | frame_->EmitPush(r0); |
| 3555 | } |
| 3556 | |
| 3557 | |
| 3558 | void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) { |
| 3559 | ASSERT_EQ(2, args->length()); |
| 3560 | |
| 3561 | Load(args->at(0)); |
| 3562 | Load(args->at(1)); |
| 3563 | |
| 3564 | frame_->CallRuntime(Runtime::kStringCompare, 2); |
| 3565 | frame_->EmitPush(r0); |
| 3566 | } |
| 3567 | |
| 3568 | |
| 3569 | void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) { |
| 3570 | ASSERT_EQ(4, args->length()); |
| 3571 | |
| 3572 | Load(args->at(0)); |
| 3573 | Load(args->at(1)); |
| 3574 | Load(args->at(2)); |
| 3575 | Load(args->at(3)); |
| 3576 | |
| 3577 | frame_->CallRuntime(Runtime::kRegExpExec, 4); |
| 3578 | frame_->EmitPush(r0); |
| 3579 | } |
| 3580 | |
| 3581 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3582 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3583 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3584 | ASSERT(args->length() == 2); |
| 3585 | |
| 3586 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3587 | LoadAndSpill(args->at(0)); |
| 3588 | LoadAndSpill(args->at(1)); |
| 3589 | frame_->EmitPop(r0); |
| 3590 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3591 | __ cmp(r0, Operand(r1)); |
| 3592 | cc_reg_ = eq; |
| 3593 | } |
| 3594 | |
| 3595 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3596 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3597 | #ifdef DEBUG |
| 3598 | int original_height = frame_->height(); |
| 3599 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3600 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3601 | if (CheckForInlineRuntimeCall(node)) { |
| 3602 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3603 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3604 | return; |
| 3605 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3606 | |
| 3607 | ZoneList<Expression*>* args = node->arguments(); |
| 3608 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3609 | Runtime::Function* function = node->function(); |
| 3610 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3611 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3612 | // Prepare stack for calling JS runtime function. |
| 3613 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3614 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3615 | // Push the builtins object found in the current global object. |
| 3616 | __ ldr(r1, GlobalObject()); |
| 3617 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3618 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3619 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3620 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3621 | // Push the arguments ("left-to-right"). |
| 3622 | int arg_count = args->length(); |
| 3623 | for (int i = 0; i < arg_count; i++) { |
| 3624 | LoadAndSpill(args->at(i)); |
| 3625 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3626 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3627 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3628 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3629 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3630 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3631 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3632 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3633 | frame_->Drop(); |
| 3634 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3635 | } else { |
| 3636 | // Call the C runtime function. |
| 3637 | frame_->CallRuntime(function, arg_count); |
| 3638 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3639 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3640 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3644 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3645 | #ifdef DEBUG |
| 3646 | int original_height = frame_->height(); |
| 3647 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3648 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3649 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3650 | |
| 3651 | Token::Value op = node->op(); |
| 3652 | |
| 3653 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3654 | LoadConditionAndSpill(node->expression(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3655 | false_target(), |
| 3656 | true_target(), |
| 3657 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3658 | // LoadCondition may (and usually does) leave a test and branch to |
| 3659 | // be emitted by the caller. In that case, negate the condition. |
| 3660 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3661 | |
| 3662 | } else if (op == Token::DELETE) { |
| 3663 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3664 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3665 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3666 | LoadAndSpill(property->obj()); |
| 3667 | LoadAndSpill(property->key()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3668 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3669 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3670 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3671 | Slot* slot = variable->slot(); |
| 3672 | if (variable->is_global()) { |
| 3673 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3674 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3675 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3676 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3677 | |
| 3678 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3679 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3680 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3681 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3682 | frame_->EmitPush(r0); |
| 3683 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3684 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3685 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3686 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3687 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3688 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3689 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3690 | } else { |
| 3691 | // Default: Result of deleting non-global, not dynamically |
| 3692 | // introduced variables is false. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3693 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3694 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3695 | |
| 3696 | } else { |
| 3697 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3698 | LoadAndSpill(node->expression()); // may have side-effects |
| 3699 | frame_->Drop(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3700 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3701 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3702 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3703 | |
| 3704 | } else if (op == Token::TYPEOF) { |
| 3705 | // Special case for loading the typeof expression; see comment on |
| 3706 | // LoadTypeofExpression(). |
| 3707 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3708 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3709 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3710 | |
| 3711 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3712 | LoadAndSpill(node->expression()); |
| 3713 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3714 | switch (op) { |
| 3715 | case Token::NOT: |
| 3716 | case Token::DELETE: |
| 3717 | case Token::TYPEOF: |
| 3718 | UNREACHABLE(); // handled above |
| 3719 | break; |
| 3720 | |
| 3721 | case Token::SUB: { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3722 | bool overwrite = |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3723 | (node->expression()->AsBinaryOperation() != NULL && |
| 3724 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 3725 | GenericUnaryOpStub stub(Token::SUB, overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3726 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3727 | break; |
| 3728 | } |
| 3729 | |
| 3730 | case Token::BIT_NOT: { |
| 3731 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3732 | JumpTarget smi_label; |
| 3733 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3734 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3735 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3736 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3737 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3738 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3739 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3740 | continue_label.Jump(); |
| 3741 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3742 | __ mvn(r0, Operand(r0)); |
| 3743 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3744 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3745 | break; |
| 3746 | } |
| 3747 | |
| 3748 | case Token::VOID: |
| 3749 | // since the stack top is cached in r0, popping and then |
| 3750 | // pushing a value can be done by just writing to r0. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3751 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3752 | break; |
| 3753 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3754 | case Token::ADD: { |
| 3755 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3756 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3757 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3758 | continue_label.Branch(eq); |
| 3759 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3760 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3761 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3762 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3763 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3764 | default: |
| 3765 | UNREACHABLE(); |
| 3766 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3767 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3768 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3769 | ASSERT(!has_valid_frame() || |
| 3770 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3771 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3775 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3776 | #ifdef DEBUG |
| 3777 | int original_height = frame_->height(); |
| 3778 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3779 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3780 | Comment cmnt(masm_, "[ CountOperation"); |
| 3781 | |
| 3782 | bool is_postfix = node->is_postfix(); |
| 3783 | bool is_increment = node->op() == Token::INC; |
| 3784 | |
| 3785 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3786 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3787 | |
| 3788 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3789 | if (is_postfix) { |
| 3790 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3791 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3792 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3793 | |
| 3794 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3795 | if (target.is_illegal()) { |
| 3796 | // Spoof the virtual frame to have the expected height (one higher |
| 3797 | // than on entry). |
| 3798 | if (!is_postfix) { |
| 3799 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3800 | frame_->EmitPush(r0); |
| 3801 | } |
| 3802 | ASSERT(frame_->height() == original_height + 1); |
| 3803 | return; |
| 3804 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3805 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3806 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3807 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3808 | JumpTarget slow; |
| 3809 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3810 | |
| 3811 | // Load the value (1) into register r1. |
| 3812 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3813 | |
| 3814 | // Check for smi operand. |
| 3815 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3816 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3817 | |
| 3818 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3819 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3820 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3821 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3822 | |
| 3823 | // Perform optimistic increment/decrement. |
| 3824 | if (is_increment) { |
| 3825 | __ add(r0, r0, Operand(r1), SetCC); |
| 3826 | } else { |
| 3827 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3828 | } |
| 3829 | |
| 3830 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3831 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3832 | |
| 3833 | // Revert optimistic increment/decrement. |
| 3834 | if (is_increment) { |
| 3835 | __ sub(r0, r0, Operand(r1)); |
| 3836 | } else { |
| 3837 | __ add(r0, r0, Operand(r1)); |
| 3838 | } |
| 3839 | |
| 3840 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3841 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3842 | { |
| 3843 | // Convert the operand to a number. |
| 3844 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3845 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3846 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3847 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3848 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3849 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3852 | // Compute the new value. |
| 3853 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3854 | frame_->EmitPush(r0); |
| 3855 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3856 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3857 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3858 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3859 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3863 | exit.Bind(); |
| 3864 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3865 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3869 | if (is_postfix) frame_->EmitPop(r0); |
| 3870 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3874 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3875 | #ifdef DEBUG |
| 3876 | int original_height = frame_->height(); |
| 3877 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3878 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3879 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3880 | Token::Value op = node->op(); |
| 3881 | |
| 3882 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3883 | // operators must yield the result of one of the two expressions |
| 3884 | // before any ToBoolean() conversions. This means that the value |
| 3885 | // produced by a && or || operator is not necessarily a boolean. |
| 3886 | |
| 3887 | // NOTE: If the left hand side produces a materialized value (not in |
| 3888 | // the CC register), we force the right hand side to do the |
| 3889 | // same. This is necessary because we may have to branch to the exit |
| 3890 | // after evaluating the left hand side (due to the shortcut |
| 3891 | // semantics), but the compiler must (statically) know if the result |
| 3892 | // of compiling the binary operation is materialized or not. |
| 3893 | |
| 3894 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3895 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3896 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3897 | &is_true, |
| 3898 | false_target(), |
| 3899 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3900 | if (has_valid_frame() && !has_cc()) { |
| 3901 | // 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] | 3902 | JumpTarget pop_and_continue; |
| 3903 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3904 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3905 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3906 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3907 | // Avoid popping the result if it converts to 'false' using the |
| 3908 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3909 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3910 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3911 | Branch(false, &exit); |
| 3912 | |
| 3913 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3914 | pop_and_continue.Bind(); |
| 3915 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3916 | |
| 3917 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3918 | is_true.Bind(); |
| 3919 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3920 | |
| 3921 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3922 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3923 | } else if (has_cc() || is_true.is_linked()) { |
| 3924 | // The left-hand side is either (a) partially compiled to |
| 3925 | // control flow with a final branch left to emit or (b) fully |
| 3926 | // compiled to control flow and possibly true. |
| 3927 | if (has_cc()) { |
| 3928 | Branch(false, false_target()); |
| 3929 | } |
| 3930 | is_true.Bind(); |
| 3931 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3932 | true_target(), |
| 3933 | false_target(), |
| 3934 | false); |
| 3935 | } else { |
| 3936 | // Nothing to do. |
| 3937 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3941 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3942 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3943 | true_target(), |
| 3944 | &is_false, |
| 3945 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3946 | if (has_valid_frame() && !has_cc()) { |
| 3947 | // The left-hand side result is on top of the virtual frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3948 | JumpTarget pop_and_continue; |
| 3949 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3950 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3951 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3952 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3953 | // Avoid popping the result if it converts to 'true' using the |
| 3954 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3955 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3956 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3957 | Branch(true, &exit); |
| 3958 | |
| 3959 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3960 | pop_and_continue.Bind(); |
| 3961 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3962 | |
| 3963 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3964 | is_false.Bind(); |
| 3965 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3966 | |
| 3967 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3968 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3969 | } else if (has_cc() || is_false.is_linked()) { |
| 3970 | // The left-hand side is either (a) partially compiled to |
| 3971 | // control flow with a final branch left to emit or (b) fully |
| 3972 | // compiled to control flow and possibly false. |
| 3973 | if (has_cc()) { |
| 3974 | Branch(true, true_target()); |
| 3975 | } |
| 3976 | is_false.Bind(); |
| 3977 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3978 | true_target(), |
| 3979 | false_target(), |
| 3980 | false); |
| 3981 | } else { |
| 3982 | // Nothing to do. |
| 3983 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | } else { |
| 3987 | // Optimize for the case where (at least) one of the expressions |
| 3988 | // is a literal small integer. |
| 3989 | Literal* lliteral = node->left()->AsLiteral(); |
| 3990 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3991 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 3992 | // never return a constant/immutable object. |
| 3993 | bool overwrite_left = |
| 3994 | (node->left()->AsBinaryOperation() != NULL && |
| 3995 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3996 | bool overwrite_right = |
| 3997 | (node->right()->AsBinaryOperation() != NULL && |
| 3998 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3999 | |
| 4000 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4001 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4002 | SmiOperation(node->op(), |
| 4003 | rliteral->handle(), |
| 4004 | false, |
| 4005 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4006 | |
| 4007 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4008 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4009 | SmiOperation(node->op(), |
| 4010 | lliteral->handle(), |
| 4011 | true, |
| 4012 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4013 | |
| 4014 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4015 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 4016 | if (overwrite_left) { |
| 4017 | overwrite_mode = OVERWRITE_LEFT; |
| 4018 | } else if (overwrite_right) { |
| 4019 | overwrite_mode = OVERWRITE_RIGHT; |
| 4020 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4021 | LoadAndSpill(node->left()); |
| 4022 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4023 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4024 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4025 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4026 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4027 | ASSERT(!has_valid_frame() || |
| 4028 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4029 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4033 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4034 | #ifdef DEBUG |
| 4035 | int original_height = frame_->height(); |
| 4036 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4037 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4038 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4039 | frame_->EmitPush(r0); |
| 4040 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4044 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4045 | #ifdef DEBUG |
| 4046 | int original_height = frame_->height(); |
| 4047 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4048 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4049 | Comment cmnt(masm_, "[ CompareOperation"); |
| 4050 | |
| 4051 | // Get the expressions from the node. |
| 4052 | Expression* left = node->left(); |
| 4053 | Expression* right = node->right(); |
| 4054 | Token::Value op = node->op(); |
| 4055 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4056 | // To make null checks efficient, we check if either left or right is the |
| 4057 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 4058 | // instead of calling the (very) general runtime routine for checking |
| 4059 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4060 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4061 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4062 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4063 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4064 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4065 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4066 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4067 | LoadAndSpill(left_is_null ? right : left); |
| 4068 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4069 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4070 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4071 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4072 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4073 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4074 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4075 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4076 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4077 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4078 | __ cmp(r0, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4079 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4080 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4081 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4082 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4083 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4084 | // It can be an undetectable object. |
| 4085 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4086 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 4087 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 4088 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4091 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4092 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4093 | return; |
| 4094 | } |
| 4095 | } |
| 4096 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4097 | // To make typeof testing for natives implemented in JavaScript really |
| 4098 | // efficient, we generate special code for expressions of the form: |
| 4099 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4100 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 4101 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4102 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4103 | (right->AsLiteral() != NULL && |
| 4104 | right->AsLiteral()->handle()->IsString())) { |
| 4105 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4106 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4107 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4108 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4109 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4110 | |
| 4111 | if (check->Equals(Heap::number_symbol())) { |
| 4112 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4113 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4114 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4115 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 4116 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4117 | cc_reg_ = eq; |
| 4118 | |
| 4119 | } else if (check->Equals(Heap::string_symbol())) { |
| 4120 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4121 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4122 | |
| 4123 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4124 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4125 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4126 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4127 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4128 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4129 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4130 | |
| 4131 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 4132 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 4133 | cc_reg_ = lt; |
| 4134 | |
| 4135 | } else if (check->Equals(Heap::boolean_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4136 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 4137 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4138 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4139 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 4140 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4141 | cc_reg_ = eq; |
| 4142 | |
| 4143 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4144 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4145 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4146 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4147 | |
| 4148 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4149 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4150 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4151 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4152 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4153 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4154 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4155 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4156 | |
| 4157 | cc_reg_ = eq; |
| 4158 | |
| 4159 | } else if (check->Equals(Heap::function_symbol())) { |
| 4160 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4161 | false_target()->Branch(eq); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4162 | Register map_reg = r2; |
| 4163 | __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE); |
| 4164 | true_target()->Branch(eq); |
| 4165 | // Regular expressions are callable so typeof == 'function'. |
| 4166 | __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4167 | cc_reg_ = eq; |
| 4168 | |
| 4169 | } else if (check->Equals(Heap::object_symbol())) { |
| 4170 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4171 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4172 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4173 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4174 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4175 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4176 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4177 | Register map_reg = r2; |
| 4178 | __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE); |
| 4179 | false_target()->Branch(eq); |
| 4180 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4181 | // It can be an undetectable object. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4182 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4183 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4184 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4185 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4186 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4187 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4188 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4189 | false_target()->Branch(lt); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4190 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4191 | cc_reg_ = le; |
| 4192 | |
| 4193 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4194 | // Uncommon case: typeof testing against a string literal that is |
| 4195 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4196 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4197 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4198 | ASSERT(!has_valid_frame() || |
| 4199 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4200 | return; |
| 4201 | } |
| 4202 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4203 | switch (op) { |
| 4204 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4205 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4206 | break; |
| 4207 | |
| 4208 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4209 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4210 | break; |
| 4211 | |
| 4212 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4213 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4214 | break; |
| 4215 | |
| 4216 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4217 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4218 | break; |
| 4219 | |
| 4220 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4221 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4222 | break; |
| 4223 | |
| 4224 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4225 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4226 | break; |
| 4227 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4228 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4229 | LoadAndSpill(left); |
| 4230 | LoadAndSpill(right); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4231 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4232 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4233 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4234 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4235 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4236 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4237 | LoadAndSpill(left); |
| 4238 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4239 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4240 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4241 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4242 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4243 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4244 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4245 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4246 | |
| 4247 | default: |
| 4248 | UNREACHABLE(); |
| 4249 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4250 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4251 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4255 | #ifdef DEBUG |
| 4256 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4257 | #endif |
| 4258 | |
| 4259 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4260 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4261 | #define __ ACCESS_MASM(masm) |
| 4262 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4263 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4264 | Handle<String> Reference::GetName() { |
| 4265 | ASSERT(type_ == NAMED); |
| 4266 | Property* property = expression_->AsProperty(); |
| 4267 | if (property == NULL) { |
| 4268 | // Global variable reference treated as a named property reference. |
| 4269 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4270 | ASSERT(proxy->AsVariable() != NULL); |
| 4271 | ASSERT(proxy->AsVariable()->is_global()); |
| 4272 | return proxy->name(); |
| 4273 | } else { |
| 4274 | Literal* raw_name = property->key()->AsLiteral(); |
| 4275 | ASSERT(raw_name != NULL); |
| 4276 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4277 | } |
| 4278 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4279 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4280 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4281 | void Reference::GetValue() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4282 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4283 | ASSERT(!is_illegal()); |
| 4284 | ASSERT(!cgen_->has_cc()); |
| 4285 | MacroAssembler* masm = cgen_->masm(); |
| 4286 | Property* property = expression_->AsProperty(); |
| 4287 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4288 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | switch (type_) { |
| 4292 | case SLOT: { |
| 4293 | Comment cmnt(masm, "[ Load from Slot"); |
| 4294 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4295 | ASSERT(slot != NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4296 | cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4297 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4300 | case NAMED: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4301 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4302 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4303 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4304 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4305 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4306 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4307 | Result name_reg(r2); |
| 4308 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4309 | ASSERT(var == NULL || var->is_global()); |
| 4310 | RelocInfo::Mode rmode = (var == NULL) |
| 4311 | ? RelocInfo::CODE_TARGET |
| 4312 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4313 | frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4314 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4315 | break; |
| 4316 | } |
| 4317 | |
| 4318 | case KEYED: { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4319 | // TODO(181): Implement inlined version of array indexing once |
| 4320 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4321 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4322 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4323 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4324 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4325 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4326 | ASSERT(var == NULL || var->is_global()); |
| 4327 | RelocInfo::Mode rmode = (var == NULL) |
| 4328 | ? RelocInfo::CODE_TARGET |
| 4329 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4330 | frame->CallCodeObject(ic, rmode, 0); |
| 4331 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4332 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | default: |
| 4336 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4337 | } |
| 4338 | } |
| 4339 | |
| 4340 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4341 | void Reference::SetValue(InitState init_state) { |
| 4342 | ASSERT(!is_illegal()); |
| 4343 | ASSERT(!cgen_->has_cc()); |
| 4344 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4345 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4346 | Property* property = expression_->AsProperty(); |
| 4347 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4348 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4349 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4350 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4351 | switch (type_) { |
| 4352 | case SLOT: { |
| 4353 | Comment cmnt(masm, "[ Store to Slot"); |
| 4354 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4355 | cgen_->StoreToSlot(slot, init_state); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4356 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4359 | case NAMED: { |
| 4360 | Comment cmnt(masm, "[ Store to named Property"); |
| 4361 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4362 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4363 | Handle<String> name(GetName()); |
| 4364 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4365 | Result value(r0); |
| 4366 | frame->EmitPop(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4367 | |
| 4368 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4369 | Result property_name(r2); |
| 4370 | __ mov(r2, Operand(name)); |
| 4371 | frame->CallCodeObject(ic, |
| 4372 | RelocInfo::CODE_TARGET, |
| 4373 | &value, |
| 4374 | &property_name, |
| 4375 | 0); |
| 4376 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4377 | break; |
| 4378 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4379 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4380 | case KEYED: { |
| 4381 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4382 | Property* property = expression_->AsProperty(); |
| 4383 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4384 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4385 | |
| 4386 | // Call IC code. |
| 4387 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4388 | // TODO(1222589): Make the IC grab the values from the stack. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4389 | Result value(r0); |
| 4390 | frame->EmitPop(r0); // value |
| 4391 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4392 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4393 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4394 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4395 | |
| 4396 | default: |
| 4397 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4402 | void FastNewClosureStub::Generate(MacroAssembler* masm) { |
| 4403 | // Clone the boilerplate in new space. Set the context to the |
| 4404 | // current context in cp. |
| 4405 | Label gc; |
| 4406 | |
| 4407 | // Pop the boilerplate function from the stack. |
| 4408 | __ pop(r3); |
| 4409 | |
| 4410 | // Attempt to allocate new JSFunction in new space. |
| 4411 | __ AllocateInNewSpace(JSFunction::kSize / kPointerSize, |
| 4412 | r0, |
| 4413 | r1, |
| 4414 | r2, |
| 4415 | &gc, |
| 4416 | TAG_OBJECT); |
| 4417 | |
| 4418 | // Compute the function map in the current global context and set that |
| 4419 | // as the map of the allocated object. |
| 4420 | __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4421 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 4422 | __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX))); |
| 4423 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4424 | |
| 4425 | // Clone the rest of the boilerplate fields. We don't have to update |
| 4426 | // the write barrier because the allocated object is in new space. |
| 4427 | for (int offset = kPointerSize; |
| 4428 | offset < JSFunction::kSize; |
| 4429 | offset += kPointerSize) { |
| 4430 | if (offset == JSFunction::kContextOffset) { |
| 4431 | __ str(cp, FieldMemOperand(r0, offset)); |
| 4432 | } else { |
| 4433 | __ ldr(r1, FieldMemOperand(r3, offset)); |
| 4434 | __ str(r1, FieldMemOperand(r0, offset)); |
| 4435 | } |
| 4436 | } |
| 4437 | |
| 4438 | // Return result. The argument boilerplate has been popped already. |
| 4439 | __ Ret(); |
| 4440 | |
| 4441 | // Create a new closure through the slower runtime call. |
| 4442 | __ bind(&gc); |
| 4443 | __ push(cp); |
| 4444 | __ push(r3); |
| 4445 | __ TailCallRuntime(ExternalReference(Runtime::kNewClosure), 2, 1); |
| 4446 | } |
| 4447 | |
| 4448 | |
| 4449 | void FastNewContextStub::Generate(MacroAssembler* masm) { |
| 4450 | // Try to allocate the context in new space. |
| 4451 | Label gc; |
| 4452 | int length = slots_ + Context::MIN_CONTEXT_SLOTS; |
| 4453 | |
| 4454 | // Attempt to allocate the context in new space. |
| 4455 | __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize), |
| 4456 | r0, |
| 4457 | r1, |
| 4458 | r2, |
| 4459 | &gc, |
| 4460 | TAG_OBJECT); |
| 4461 | |
| 4462 | // Load the function from the stack. |
| 4463 | __ ldr(r3, MemOperand(sp, 0 * kPointerSize)); |
| 4464 | |
| 4465 | // Setup the object header. |
| 4466 | __ LoadRoot(r2, Heap::kContextMapRootIndex); |
| 4467 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4468 | __ mov(r2, Operand(length)); |
| 4469 | __ str(r2, FieldMemOperand(r0, Array::kLengthOffset)); |
| 4470 | |
| 4471 | // Setup the fixed slots. |
| 4472 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 4473 | __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 4474 | __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 4475 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
| 4476 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX))); |
| 4477 | |
| 4478 | // Copy the global object from the surrounding context. |
| 4479 | __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4480 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 4481 | |
| 4482 | // Initialize the rest of the slots to undefined. |
| 4483 | __ LoadRoot(r1, Heap::kUndefinedValueRootIndex); |
| 4484 | for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) { |
| 4485 | __ str(r1, MemOperand(r0, Context::SlotOffset(i))); |
| 4486 | } |
| 4487 | |
| 4488 | // Remove the on-stack argument and return. |
| 4489 | __ mov(cp, r0); |
| 4490 | __ pop(); |
| 4491 | __ Ret(); |
| 4492 | |
| 4493 | // Need to collect. Call into runtime system. |
| 4494 | __ bind(&gc); |
| 4495 | __ TailCallRuntime(ExternalReference(Runtime::kNewContext), 1, 1); |
| 4496 | } |
| 4497 | |
| 4498 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4499 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4500 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4501 | // (31 instead of 32). |
| 4502 | static void CountLeadingZeros( |
| 4503 | MacroAssembler* masm, |
| 4504 | Register source, |
| 4505 | Register scratch, |
| 4506 | Register zeros) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4507 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4508 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4509 | #else |
| 4510 | __ mov(zeros, Operand(0)); |
| 4511 | __ mov(scratch, source); |
| 4512 | // Top 16. |
| 4513 | __ tst(scratch, Operand(0xffff0000)); |
| 4514 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4515 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4516 | // Top 8. |
| 4517 | __ tst(scratch, Operand(0xff000000)); |
| 4518 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4519 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4520 | // Top 4. |
| 4521 | __ tst(scratch, Operand(0xf0000000)); |
| 4522 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4523 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4524 | // Top 2. |
| 4525 | __ tst(scratch, Operand(0xc0000000)); |
| 4526 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4527 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4528 | // Top bit. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4529 | __ tst(scratch, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4530 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4531 | #endif |
| 4532 | } |
| 4533 | |
| 4534 | |
| 4535 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4536 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4537 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4538 | // scratch register. Destroys the source register. No GC occurs during this |
| 4539 | // stub so you don't have to set up the frame. |
| 4540 | class ConvertToDoubleStub : public CodeStub { |
| 4541 | public: |
| 4542 | ConvertToDoubleStub(Register result_reg_1, |
| 4543 | Register result_reg_2, |
| 4544 | Register source_reg, |
| 4545 | Register scratch_reg) |
| 4546 | : result1_(result_reg_1), |
| 4547 | result2_(result_reg_2), |
| 4548 | source_(source_reg), |
| 4549 | zeros_(scratch_reg) { } |
| 4550 | |
| 4551 | private: |
| 4552 | Register result1_; |
| 4553 | Register result2_; |
| 4554 | Register source_; |
| 4555 | Register zeros_; |
| 4556 | |
| 4557 | // Minor key encoding in 16 bits. |
| 4558 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4559 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4560 | |
| 4561 | Major MajorKey() { return ConvertToDouble; } |
| 4562 | int MinorKey() { |
| 4563 | // Encode the parameters in a unique 16 bit value. |
| 4564 | return result1_.code() + |
| 4565 | (result2_.code() << 4) + |
| 4566 | (source_.code() << 8) + |
| 4567 | (zeros_.code() << 12); |
| 4568 | } |
| 4569 | |
| 4570 | void Generate(MacroAssembler* masm); |
| 4571 | |
| 4572 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4573 | |
| 4574 | #ifdef DEBUG |
| 4575 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4576 | #endif |
| 4577 | }; |
| 4578 | |
| 4579 | |
| 4580 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4581 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4582 | Register exponent = result1_; |
| 4583 | Register mantissa = result2_; |
| 4584 | #else |
| 4585 | Register exponent = result2_; |
| 4586 | Register mantissa = result1_; |
| 4587 | #endif |
| 4588 | Label not_special; |
| 4589 | // Convert from Smi to integer. |
| 4590 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4591 | // Move sign bit from source to destination. This works because the sign bit |
| 4592 | // in the exponent word of the double has the same position and polarity as |
| 4593 | // the 2's complement sign bit in a Smi. |
| 4594 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4595 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4596 | // Subtract from 0 if source was negative. |
| 4597 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4598 | __ cmp(source_, Operand(1)); |
| 4599 | __ b(gt, ¬_special); |
| 4600 | |
| 4601 | // We have -1, 0 or 1, which we treat specially. |
| 4602 | __ cmp(source_, Operand(0)); |
| 4603 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4604 | static const uint32_t exponent_word_for_1 = |
| 4605 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4606 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4607 | // 1, 0 and -1 all have 0 for the second word. |
| 4608 | __ mov(mantissa, Operand(0)); |
| 4609 | __ Ret(); |
| 4610 | |
| 4611 | __ bind(¬_special); |
| 4612 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4613 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4614 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4615 | // Compute exponent and or it into the exponent register. |
| 4616 | // We use result2 as a scratch register here. |
| 4617 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4618 | __ orr(exponent, |
| 4619 | exponent, |
| 4620 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4621 | // Shift up the source chopping the top bit off. |
| 4622 | __ add(zeros_, zeros_, Operand(1)); |
| 4623 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4624 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4625 | // Compute lower part of fraction (last 12 bits). |
| 4626 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4627 | // And the top (top 20 bits). |
| 4628 | __ orr(exponent, |
| 4629 | exponent, |
| 4630 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4631 | __ Ret(); |
| 4632 | } |
| 4633 | |
| 4634 | |
| 4635 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4636 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4637 | // so you don't have to set up the frame. |
| 4638 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4639 | public: |
| 4640 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4641 | Register the_heap_number, |
| 4642 | Register scratch) |
| 4643 | : the_int_(the_int), |
| 4644 | the_heap_number_(the_heap_number), |
| 4645 | scratch_(scratch) { } |
| 4646 | |
| 4647 | private: |
| 4648 | Register the_int_; |
| 4649 | Register the_heap_number_; |
| 4650 | Register scratch_; |
| 4651 | |
| 4652 | // Minor key encoding in 16 bits. |
| 4653 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4654 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4655 | |
| 4656 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4657 | int MinorKey() { |
| 4658 | // Encode the parameters in a unique 16 bit value. |
| 4659 | return the_int_.code() + |
| 4660 | (the_heap_number_.code() << 4) + |
| 4661 | (scratch_.code() << 8); |
| 4662 | } |
| 4663 | |
| 4664 | void Generate(MacroAssembler* masm); |
| 4665 | |
| 4666 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4667 | |
| 4668 | #ifdef DEBUG |
| 4669 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4670 | #endif |
| 4671 | }; |
| 4672 | |
| 4673 | |
| 4674 | // See comment for class. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4675 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4676 | Label max_negative_int; |
| 4677 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4678 | // We test for the special value that has a different exponent. This test |
| 4679 | // has the neat side effect of setting the flags according to the sign. |
| 4680 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4681 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4682 | __ b(eq, &max_negative_int); |
| 4683 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4684 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4685 | uint32_t non_smi_exponent = |
| 4686 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4687 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4688 | // Set the sign bit in scratch_ if the value was negative. |
| 4689 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4690 | // Subtract from 0 if the value was negative. |
| 4691 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4692 | // We should be masking the implict first digit of the mantissa away here, |
| 4693 | // but it just ends up combining harmlessly with the last digit of the |
| 4694 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4695 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4696 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4697 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4698 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4699 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4700 | HeapNumber::kExponentOffset)); |
| 4701 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4702 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4703 | HeapNumber::kMantissaOffset)); |
| 4704 | __ Ret(); |
| 4705 | |
| 4706 | __ bind(&max_negative_int); |
| 4707 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4708 | // a double because it uses a sign bit instead of using two's complement. |
| 4709 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4710 | // significant 1 bit is not stored. |
| 4711 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4712 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4713 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4714 | __ mov(ip, Operand(0)); |
| 4715 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4716 | __ Ret(); |
| 4717 | } |
| 4718 | |
| 4719 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4720 | // Handle the case where the lhs and rhs are the same object. |
| 4721 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4722 | // for "identity and not NaN". |
| 4723 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4724 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4725 | Condition cc, |
| 4726 | bool never_nan_nan) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4727 | Label not_identical; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4728 | Label heap_number, return_equal; |
| 4729 | Register exp_mask_reg = r5; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4730 | __ cmp(r0, Operand(r1)); |
| 4731 | __ b(ne, ¬_identical); |
| 4732 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4733 | // The two objects are identical. If we know that one of them isn't NaN then |
| 4734 | // we now know they test equal. |
| 4735 | if (cc != eq || !never_nan_nan) { |
| 4736 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4737 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4738 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4739 | // so we do the second best thing - test it ourselves. |
| 4740 | // They are both equal and they are not both Smis so both of them are not |
| 4741 | // Smis. If it's not a heap number, then return equal. |
| 4742 | if (cc == lt || cc == gt) { |
| 4743 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4744 | __ b(ge, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4745 | } else { |
| 4746 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4747 | __ b(eq, &heap_number); |
| 4748 | // Comparing JS objects with <=, >= is complicated. |
| 4749 | if (cc != eq) { |
| 4750 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4751 | __ b(ge, slow); |
| 4752 | // Normally here we fall through to return_equal, but undefined is |
| 4753 | // special: (undefined == undefined) == true, but |
| 4754 | // (undefined <= undefined) == false! See ECMAScript 11.8.5. |
| 4755 | if (cc == le || cc == ge) { |
| 4756 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 4757 | __ b(ne, &return_equal); |
| 4758 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 4759 | __ cmp(r0, Operand(r2)); |
| 4760 | __ b(ne, &return_equal); |
| 4761 | if (cc == le) { |
| 4762 | // undefined <= undefined should fail. |
| 4763 | __ mov(r0, Operand(GREATER)); |
| 4764 | } else { |
| 4765 | // undefined >= undefined should fail. |
| 4766 | __ mov(r0, Operand(LESS)); |
| 4767 | } |
| 4768 | __ mov(pc, Operand(lr)); // Return. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4769 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4770 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4771 | } |
| 4772 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4773 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4774 | __ bind(&return_equal); |
| 4775 | if (cc == lt) { |
| 4776 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4777 | } else if (cc == gt) { |
| 4778 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4779 | } else { |
| 4780 | __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves. |
| 4781 | } |
| 4782 | __ mov(pc, Operand(lr)); // Return. |
| 4783 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4784 | if (cc != eq || !never_nan_nan) { |
| 4785 | // For less and greater we don't have to check for NaN since the result of |
| 4786 | // x < x is false regardless. For the others here is some code to check |
| 4787 | // for NaN. |
| 4788 | if (cc != lt && cc != gt) { |
| 4789 | __ bind(&heap_number); |
| 4790 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4791 | // not NaN. |
| 4792 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4793 | // and not all mantissa bits (0..51) clear. |
| 4794 | // Read top bits of double representation (second word of value). |
| 4795 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4796 | // Test that exponent bits are all set. |
| 4797 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4798 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4799 | __ b(ne, &return_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4800 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4801 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4802 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4803 | // Or with all low-bits of mantissa. |
| 4804 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4805 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4806 | // For equal we already have the right value in r0: Return zero (equal) |
| 4807 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if not |
| 4808 | // (it's a NaN). For <= and >= we need to load r0 with the failing value |
| 4809 | // if it's a NaN. |
| 4810 | if (cc != eq) { |
| 4811 | // All-zero means Infinity means equal. |
| 4812 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 4813 | if (cc == le) { |
| 4814 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 4815 | } else { |
| 4816 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 4817 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4818 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4819 | __ mov(pc, Operand(lr)); // Return. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4820 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 4821 | // No fall through here. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4822 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4823 | |
| 4824 | __ bind(¬_identical); |
| 4825 | } |
| 4826 | |
| 4827 | |
| 4828 | // See comment at call site. |
| 4829 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 4830 | Label* rhs_not_nan, |
| 4831 | Label* slow, |
| 4832 | bool strict) { |
| 4833 | Label lhs_is_smi; |
| 4834 | __ tst(r0, Operand(kSmiTagMask)); |
| 4835 | __ b(eq, &lhs_is_smi); |
| 4836 | |
| 4837 | // Rhs is a Smi. Check whether the non-smi is a heap number. |
| 4838 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4839 | if (strict) { |
| 4840 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4841 | // succeed. Return non-equal (r0 is already not zero) |
| 4842 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4843 | } else { |
| 4844 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4845 | // the runtime. |
| 4846 | __ b(ne, slow); |
| 4847 | } |
| 4848 | |
| 4849 | // Rhs is a smi, lhs is a number. |
| 4850 | __ push(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4851 | |
| 4852 | if (CpuFeatures::IsSupported(VFP3)) { |
| 4853 | CpuFeatures::Scope scope(VFP3); |
| 4854 | __ IntegerToDoubleConversionWithVFP3(r1, r3, r2); |
| 4855 | } else { |
| 4856 | __ mov(r7, Operand(r1)); |
| 4857 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4858 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4859 | } |
| 4860 | |
| 4861 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4862 | // r3 and r2 are rhs as double. |
| 4863 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4864 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4865 | // We now have both loaded as doubles but we can skip the lhs nan check |
| 4866 | // since it's a Smi. |
| 4867 | __ pop(lr); |
| 4868 | __ jmp(rhs_not_nan); |
| 4869 | |
| 4870 | __ bind(&lhs_is_smi); |
| 4871 | // Lhs is a Smi. Check whether the non-smi is a heap number. |
| 4872 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4873 | if (strict) { |
| 4874 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4875 | // succeed. Return non-equal. |
| 4876 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 4877 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4878 | } else { |
| 4879 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4880 | // the runtime. |
| 4881 | __ b(ne, slow); |
| 4882 | } |
| 4883 | |
| 4884 | // Lhs is a smi, rhs is a number. |
| 4885 | // r0 is Smi and r1 is heap number. |
| 4886 | __ push(lr); |
| 4887 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4888 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4889 | |
| 4890 | if (CpuFeatures::IsSupported(VFP3)) { |
| 4891 | CpuFeatures::Scope scope(VFP3); |
| 4892 | __ IntegerToDoubleConversionWithVFP3(r0, r1, r0); |
| 4893 | } else { |
| 4894 | __ mov(r7, Operand(r0)); |
| 4895 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4896 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4897 | } |
| 4898 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4899 | __ pop(lr); |
| 4900 | // Fall through to both_loaded_as_doubles. |
| 4901 | } |
| 4902 | |
| 4903 | |
| 4904 | void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) { |
| 4905 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4906 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4907 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4908 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4909 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4910 | Label one_is_nan, neither_is_nan; |
| 4911 | |
| 4912 | Register exp_mask_reg = r5; |
| 4913 | |
| 4914 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4915 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 4916 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4917 | __ b(ne, rhs_not_nan); |
| 4918 | __ mov(r4, |
| 4919 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4920 | SetCC); |
| 4921 | __ b(ne, &one_is_nan); |
| 4922 | __ cmp(rhs_mantissa, Operand(0)); |
| 4923 | __ b(ne, &one_is_nan); |
| 4924 | |
| 4925 | __ bind(rhs_not_nan); |
| 4926 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4927 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 4928 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4929 | __ b(ne, &neither_is_nan); |
| 4930 | __ mov(r4, |
| 4931 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4932 | SetCC); |
| 4933 | __ b(ne, &one_is_nan); |
| 4934 | __ cmp(lhs_mantissa, Operand(0)); |
| 4935 | __ b(eq, &neither_is_nan); |
| 4936 | |
| 4937 | __ bind(&one_is_nan); |
| 4938 | // NaN comparisons always fail. |
| 4939 | // Load whatever we need in r0 to make the comparison fail. |
| 4940 | if (cc == lt || cc == le) { |
| 4941 | __ mov(r0, Operand(GREATER)); |
| 4942 | } else { |
| 4943 | __ mov(r0, Operand(LESS)); |
| 4944 | } |
| 4945 | __ mov(pc, Operand(lr)); // Return. |
| 4946 | |
| 4947 | __ bind(&neither_is_nan); |
| 4948 | } |
| 4949 | |
| 4950 | |
| 4951 | // See comment at call site. |
| 4952 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 4953 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4954 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4955 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4956 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4957 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4958 | |
| 4959 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 4960 | if (cc == eq) { |
| 4961 | // Doubles are not equal unless they have the same bit pattern. |
| 4962 | // Exception: 0 and -0. |
| 4963 | __ cmp(lhs_mantissa, Operand(rhs_mantissa)); |
| 4964 | __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne); |
| 4965 | // Return non-zero if the numbers are unequal. |
| 4966 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 4967 | |
| 4968 | __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC); |
| 4969 | // If exponents are equal then return 0. |
| 4970 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 4971 | |
| 4972 | // Exponents are unequal. The only way we can return that the numbers |
| 4973 | // are equal is if one is -0 and the other is 0. We already dealt |
| 4974 | // with the case where both are -0 or both are 0. |
| 4975 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 4976 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 4977 | // equal. |
| 4978 | __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC); |
| 4979 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 4980 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 4981 | // Now they are equal if and only if the lhs exponent is zero in its |
| 4982 | // low 31 bits. |
| 4983 | __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize)); |
| 4984 | __ mov(pc, Operand(lr)); |
| 4985 | } else { |
| 4986 | // Call a native function to do a comparison between two non-NaNs. |
| 4987 | // Call C routine that may not cause GC or other trouble. |
| 4988 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 4989 | __ Jump(r5); // Tail call. |
| 4990 | } |
| 4991 | } |
| 4992 | |
| 4993 | |
| 4994 | // See comment at call site. |
| 4995 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 4996 | // If either operand is a JSObject or an oddball value, then they are |
| 4997 | // not equal since their pointers are different. |
| 4998 | // There is no test for undetectability in strict equality. |
| 4999 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 5000 | Label first_non_object; |
| 5001 | // Get the type of the first operand into r2 and compare it with |
| 5002 | // FIRST_JS_OBJECT_TYPE. |
| 5003 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 5004 | __ b(lt, &first_non_object); |
| 5005 | |
| 5006 | // Return non-zero (r0 is not zero) |
| 5007 | Label return_not_equal; |
| 5008 | __ bind(&return_not_equal); |
| 5009 | __ mov(pc, Operand(lr)); // Return. |
| 5010 | |
| 5011 | __ bind(&first_non_object); |
| 5012 | // Check for oddballs: true, false, null, undefined. |
| 5013 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 5014 | __ b(eq, &return_not_equal); |
| 5015 | |
| 5016 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 5017 | __ b(ge, &return_not_equal); |
| 5018 | |
| 5019 | // Check for oddballs: true, false, null, undefined. |
| 5020 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 5021 | __ b(eq, &return_not_equal); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 5022 | |
| 5023 | // Now that we have the types we might as well check for symbol-symbol. |
| 5024 | // Ensure that no non-strings have the symbol bit set. |
| 5025 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5026 | ASSERT(kSymbolTag != 0); |
| 5027 | __ and_(r2, r2, Operand(r3)); |
| 5028 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5029 | __ b(ne, &return_not_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
| 5032 | |
| 5033 | // See comment at call site. |
| 5034 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 5035 | Label* both_loaded_as_doubles, |
| 5036 | Label* not_heap_numbers, |
| 5037 | Label* slow) { |
| 5038 | __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE); |
| 5039 | __ b(ne, not_heap_numbers); |
| 5040 | __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE); |
| 5041 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 5042 | |
| 5043 | // Both are heap numbers. Load them up then jump to the code we have |
| 5044 | // for that. |
| 5045 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 5046 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 5047 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 5048 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 5049 | __ jmp(both_loaded_as_doubles); |
| 5050 | } |
| 5051 | |
| 5052 | |
| 5053 | // Fast negative check for symbol-to-symbol equality. |
| 5054 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 5055 | // r2 is object type of r0. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 5056 | // Ensure that no non-strings have the symbol bit set. |
| 5057 | ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE); |
| 5058 | ASSERT(kSymbolTag != 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5059 | __ tst(r2, Operand(kIsSymbolMask)); |
| 5060 | __ b(eq, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 5061 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5062 | __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5063 | __ tst(r3, Operand(kIsSymbolMask)); |
| 5064 | __ b(eq, slow); |
| 5065 | |
| 5066 | // Both are symbols. We already checked they weren't the same pointer |
| 5067 | // so they are not equal. |
| 5068 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 5069 | __ mov(pc, Operand(lr)); // Return. |
| 5070 | } |
| 5071 | |
| 5072 | |
| 5073 | // On entry r0 and r1 are the things to be compared. On exit r0 is 0, |
| 5074 | // positive or negative to indicate the result of the comparison. |
| 5075 | void CompareStub::Generate(MacroAssembler* masm) { |
| 5076 | Label slow; // Call builtin. |
| 5077 | Label not_smis, both_loaded_as_doubles, rhs_not_nan; |
| 5078 | |
| 5079 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 5080 | // it is certain that at least one operand isn't a smi. |
| 5081 | |
| 5082 | // Handle the case where the objects are identical. Either returns the answer |
| 5083 | // 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^] | 5084 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5085 | |
| 5086 | // If either is a Smi (we know that not both are), then they can only |
| 5087 | // be strictly equal if the other is a HeapNumber. |
| 5088 | ASSERT_EQ(0, kSmiTag); |
| 5089 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 5090 | __ and_(r2, r0, Operand(r1)); |
| 5091 | __ tst(r2, Operand(kSmiTagMask)); |
| 5092 | __ b(ne, ¬_smis); |
| 5093 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 5094 | // 1) Return the answer. |
| 5095 | // 2) Go to slow. |
| 5096 | // 3) Fall through to both_loaded_as_doubles. |
| 5097 | // 4) Jump to rhs_not_nan. |
| 5098 | // In cases 3 and 4 we have found out we were dealing with a number-number |
| 5099 | // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles. |
| 5100 | EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_); |
| 5101 | |
| 5102 | __ bind(&both_loaded_as_doubles); |
| 5103 | // r0, r1, r2, r3 are the double representations of the left hand side |
| 5104 | // and the right hand side. |
| 5105 | |
| 5106 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 5107 | // fall through if neither is a NaN. Also binds rhs_not_nan. |
| 5108 | EmitNanCheck(masm, &rhs_not_nan, cc_); |
| 5109 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5110 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5111 | CpuFeatures::Scope scope(VFP3); |
| 5112 | // ARMv7 VFP3 instructions to implement double precision comparison. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5113 | __ vmov(d6, r0, r1); |
| 5114 | __ vmov(d7, r2, r3); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5115 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5116 | __ vcmp(d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5117 | __ vmrs(pc); |
| 5118 | __ mov(r0, Operand(0), LeaveCC, eq); |
| 5119 | __ mov(r0, Operand(1), LeaveCC, lt); |
| 5120 | __ mvn(r0, Operand(0), LeaveCC, gt); |
| 5121 | __ mov(pc, Operand(lr)); |
| 5122 | } else { |
| 5123 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 5124 | // answer. Never falls through. |
| 5125 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 5126 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5127 | |
| 5128 | __ bind(¬_smis); |
| 5129 | // At this point we know we are dealing with two different objects, |
| 5130 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 5131 | if (strict_) { |
| 5132 | // This returns non-equal for some object types, or falls through if it |
| 5133 | // was not lucky. |
| 5134 | EmitStrictTwoHeapObjectCompare(masm); |
| 5135 | } |
| 5136 | |
| 5137 | Label check_for_symbols; |
| 5138 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 5139 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 5140 | // that case. If the inputs are not doubles then jumps to check_for_symbols. |
| 5141 | // In this case r2 will contain the type of r0. |
| 5142 | EmitCheckForTwoHeapNumbers(masm, |
| 5143 | &both_loaded_as_doubles, |
| 5144 | &check_for_symbols, |
| 5145 | &slow); |
| 5146 | |
| 5147 | __ bind(&check_for_symbols); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 5148 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 5149 | // symbols. |
| 5150 | if (cc_ == eq && !strict_) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5151 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5152 | // of r0 on entry. |
| 5153 | EmitCheckForSymbols(masm, &slow); |
| 5154 | } |
| 5155 | |
| 5156 | __ bind(&slow); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5157 | __ push(r1); |
| 5158 | __ push(r0); |
| 5159 | // Figure out which native to call and setup the arguments. |
| 5160 | Builtins::JavaScript native; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5161 | if (cc_ == eq) { |
| 5162 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 5163 | } else { |
| 5164 | native = Builtins::COMPARE; |
| 5165 | int ncr; // NaN compare result |
| 5166 | if (cc_ == lt || cc_ == le) { |
| 5167 | ncr = GREATER; |
| 5168 | } else { |
| 5169 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 5170 | ncr = LESS; |
| 5171 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5172 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 5173 | __ push(r0); |
| 5174 | } |
| 5175 | |
| 5176 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 5177 | // tagged as a small integer. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 5178 | __ InvokeBuiltin(native, JUMP_JS); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5182 | // Allocates a heap number or jumps to the label if the young space is full and |
| 5183 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5184 | static void AllocateHeapNumber( |
| 5185 | MacroAssembler* masm, |
| 5186 | Label* need_gc, // Jump here if young space is full. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5187 | Register result, // The tagged address of the new heap number. |
| 5188 | Register scratch1, // A scratch register. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5189 | Register scratch2) { // Another scratch register. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5190 | // Allocate an object in the heap for the heap number and tag it as a heap |
| 5191 | // object. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5192 | __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize, |
| 5193 | result, |
| 5194 | scratch1, |
| 5195 | scratch2, |
| 5196 | need_gc, |
| 5197 | TAG_OBJECT); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5198 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5199 | // Get heap number map and store it in the allocated object. |
| 5200 | __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex); |
| 5201 | __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5202 | } |
| 5203 | |
| 5204 | |
| 5205 | // We fall into this code if the operands were Smis, but the result was |
| 5206 | // 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] | 5207 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 5208 | // to call the C-implemented binary fp operation routines we need to end up |
| 5209 | // with the double precision floating point operands in r0 and r1 (for the |
| 5210 | // value in r1) and r2 and r3 (for the value in r0). |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5211 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 5212 | Label* not_smi, |
| 5213 | const Builtins::JavaScript& builtin, |
| 5214 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5215 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5216 | Label slow, slow_pop_2_first, do_the_call; |
| 5217 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 5218 | // Smi-smi case (overflow). |
| 5219 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 5220 | // The new heap number is in r5. r6 and r7 are scratch. |
| 5221 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5222 | |
| 5223 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5224 | CpuFeatures::Scope scope(VFP3); |
| 5225 | __ IntegerToDoubleConversionWithVFP3(r0, r3, r2); |
| 5226 | __ IntegerToDoubleConversionWithVFP3(r1, r1, r0); |
| 5227 | } else { |
| 5228 | // Write Smi from r0 to r3 and r2 in double format. r6 is scratch. |
| 5229 | __ mov(r7, Operand(r0)); |
| 5230 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5231 | __ push(lr); |
| 5232 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 5233 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 5234 | __ mov(r7, Operand(r1)); |
| 5235 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5236 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 5237 | __ pop(lr); |
| 5238 | } |
| 5239 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5240 | __ jmp(&do_the_call); // Tail call. No return. |
| 5241 | |
| 5242 | // We jump to here if something goes wrong (one param is not a number of any |
| 5243 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5244 | __ bind(&slow); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5245 | |
| 5246 | // Push arguments to the stack |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5247 | __ push(r1); |
| 5248 | __ push(r0); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5249 | |
| 5250 | if (Token::ADD == operation) { |
| 5251 | // Test for string arguments before calling runtime. |
| 5252 | // r1 : first argument |
| 5253 | // r0 : second argument |
| 5254 | // sp[0] : second argument |
| 5255 | // sp[1] : first argument |
| 5256 | |
| 5257 | Label not_strings, not_string1, string1; |
| 5258 | __ tst(r1, Operand(kSmiTagMask)); |
| 5259 | __ b(eq, ¬_string1); |
| 5260 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 5261 | __ b(ge, ¬_string1); |
| 5262 | |
| 5263 | // First argument is a a string, test second. |
| 5264 | __ tst(r0, Operand(kSmiTagMask)); |
| 5265 | __ b(eq, &string1); |
| 5266 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5267 | __ b(ge, &string1); |
| 5268 | |
| 5269 | // First and second argument are strings. |
| 5270 | __ TailCallRuntime(ExternalReference(Runtime::kStringAdd), 2, 1); |
| 5271 | |
| 5272 | // Only first argument is a string. |
| 5273 | __ bind(&string1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5274 | __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS); |
| 5275 | |
| 5276 | // First argument was not a string, test second. |
| 5277 | __ bind(¬_string1); |
| 5278 | __ tst(r0, Operand(kSmiTagMask)); |
| 5279 | __ b(eq, ¬_strings); |
| 5280 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 5281 | __ b(ge, ¬_strings); |
| 5282 | |
| 5283 | // Only second argument is a string. |
| 5284 | __ b(¬_strings); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5285 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 5286 | |
| 5287 | __ bind(¬_strings); |
| 5288 | } |
| 5289 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5290 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5291 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5292 | // We branch here if at least one of r0 and r1 is not a Smi. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5293 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5294 | if (mode == NO_OVERWRITE) { |
| 5295 | // In the case where there is no chance of an overwritable float we may as |
| 5296 | // well do the allocation immediately while r0 and r1 are untouched. |
| 5297 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5298 | } |
| 5299 | |
| 5300 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5301 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5302 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5303 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5304 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5305 | if (mode == OVERWRITE_RIGHT) { |
| 5306 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5307 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5308 | // Calling convention says that second double is in r2 and r3. |
| 5309 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5310 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5311 | __ jmp(&finished_loading_r0); |
| 5312 | __ bind(&r0_is_smi); |
| 5313 | if (mode == OVERWRITE_RIGHT) { |
| 5314 | // We can't overwrite a Smi so get address of new heap number into r5. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5315 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5316 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5317 | |
| 5318 | |
| 5319 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5320 | CpuFeatures::Scope scope(VFP3); |
| 5321 | __ IntegerToDoubleConversionWithVFP3(r0, r3, r2); |
| 5322 | } else { |
| 5323 | // Write Smi from r0 to r3 and r2 in double format. |
| 5324 | __ mov(r7, Operand(r0)); |
| 5325 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5326 | __ push(lr); |
| 5327 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5328 | __ pop(lr); |
| 5329 | } |
| 5330 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5331 | __ bind(&finished_loading_r0); |
| 5332 | |
| 5333 | // Move r1 to a double in r0-r1. |
| 5334 | __ tst(r1, Operand(kSmiTagMask)); |
| 5335 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5336 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5337 | __ b(ne, &slow); |
| 5338 | if (mode == OVERWRITE_LEFT) { |
| 5339 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5340 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5341 | // Calling convention says that first double is in r0 and r1. |
| 5342 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5343 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5344 | __ jmp(&finished_loading_r1); |
| 5345 | __ bind(&r1_is_smi); |
| 5346 | if (mode == OVERWRITE_LEFT) { |
| 5347 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5348 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5349 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5350 | |
| 5351 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5352 | CpuFeatures::Scope scope(VFP3); |
| 5353 | __ IntegerToDoubleConversionWithVFP3(r1, r1, r0); |
| 5354 | } else { |
| 5355 | // Write Smi from r1 to r1 and r0 in double format. |
| 5356 | __ mov(r7, Operand(r1)); |
| 5357 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5358 | __ push(lr); |
| 5359 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5360 | __ pop(lr); |
| 5361 | } |
| 5362 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5363 | __ bind(&finished_loading_r1); |
| 5364 | |
| 5365 | __ bind(&do_the_call); |
| 5366 | // r0: Left value (least significant part of mantissa). |
| 5367 | // r1: Left value (sign, exponent, top of mantissa). |
| 5368 | // r2: Right value (least significant part of mantissa). |
| 5369 | // r3: Right value (sign, exponent, top of mantissa). |
| 5370 | // r5: Address of heap number for result. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5371 | |
| 5372 | if (CpuFeatures::IsSupported(VFP3) && |
| 5373 | ((Token::MUL == operation) || |
| 5374 | (Token::DIV == operation) || |
| 5375 | (Token::ADD == operation) || |
| 5376 | (Token::SUB == operation))) { |
| 5377 | CpuFeatures::Scope scope(VFP3); |
| 5378 | // ARMv7 VFP3 instructions to implement |
| 5379 | // double precision, add, subtract, multiply, divide. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5380 | __ vmov(d6, r0, r1); |
| 5381 | __ vmov(d7, r2, r3); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5382 | |
| 5383 | if (Token::MUL == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5384 | __ vmul(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5385 | } else if (Token::DIV == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5386 | __ vdiv(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5387 | } else if (Token::ADD == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5388 | __ vadd(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5389 | } else if (Token::SUB == operation) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5390 | __ vsub(d5, d6, d7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5391 | } else { |
| 5392 | UNREACHABLE(); |
| 5393 | } |
| 5394 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5395 | __ vmov(r0, r1, d5); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5396 | |
| 5397 | __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset)); |
| 5398 | __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4)); |
| 5399 | __ mov(r0, Operand(r5)); |
| 5400 | __ mov(pc, lr); |
| 5401 | return; |
| 5402 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5403 | __ push(lr); // For later. |
| 5404 | __ push(r5); // Address of heap number that is answer. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5405 | __ AlignStack(0); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5406 | // Call C routine that may not cause GC or other trouble. |
| 5407 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5408 | __ Call(r5); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5409 | __ pop(r4); // Address of heap number. |
| 5410 | __ cmp(r4, Operand(Smi::FromInt(0))); |
| 5411 | __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5412 | // Store answer in the overwritable heap number. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5413 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5414 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5415 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5416 | // substract the tag from r4. |
| 5417 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5418 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5419 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5420 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5421 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5422 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5423 | #endif |
| 5424 | __ mov(r0, Operand(r4)); |
| 5425 | // And we are done. |
| 5426 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5430 | // 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] | 5431 | // 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] | 5432 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5433 | // 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] | 5434 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5435 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5436 | static void GetInt32(MacroAssembler* masm, |
| 5437 | Register source, |
| 5438 | Register dest, |
| 5439 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5440 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5441 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5442 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5443 | // Get exponent word. |
| 5444 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5445 | // Get exponent alone in scratch2. |
| 5446 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5447 | // Load dest with zero. We use this either for the final shift or |
| 5448 | // for the answer. |
| 5449 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5450 | // 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] | 5451 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5452 | // the exponent that we are fastest at and also the highest exponent we can |
| 5453 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5454 | const uint32_t non_smi_exponent = |
| 5455 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5456 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5457 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5458 | __ b(eq, &right_exponent); |
| 5459 | // If the exponent is higher than that then go to slow case. This catches |
| 5460 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5461 | __ b(gt, slow); |
| 5462 | |
| 5463 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5464 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5465 | // it rounds to zero. |
| 5466 | const uint32_t zero_exponent = |
| 5467 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5468 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5469 | // Dest already has a Smi zero. |
| 5470 | __ b(lt, &done); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5471 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 5472 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5473 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5474 | // We now have the exponent in dest. Subtract from 30 to get |
| 5475 | // how much to shift down. |
| 5476 | __ rsb(dest, dest, Operand(30)); |
| 5477 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5478 | __ bind(&right_exponent); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5479 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5480 | CpuFeatures::Scope scope(VFP3); |
| 5481 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 5482 | // conversion using round to zero. |
| 5483 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5484 | __ vmov(d7, scratch2, scratch); |
| 5485 | __ vcvt(s15, d7); |
| 5486 | __ vmov(dest, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5487 | } else { |
| 5488 | // Get the top bits of the mantissa. |
| 5489 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5490 | // Put back the implicit 1. |
| 5491 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5492 | // Shift up the mantissa bits to take up the space the exponent used to |
| 5493 | // take. We just orred in the implicit bit so that took care of one and |
| 5494 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 5495 | // distance. |
| 5496 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5497 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5498 | // Put sign in zero flag. |
| 5499 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 5500 | // Get the second half of the double. For some exponents we don't |
| 5501 | // actually need this because the bits get shifted out again, but |
| 5502 | // it's probably slower to test than just to do it. |
| 5503 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5504 | // Shift down 22 bits to get the last 10 bits. |
| 5505 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5506 | // Move down according to the exponent. |
| 5507 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 5508 | // Fix sign if sign bit was set. |
| 5509 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 5510 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5511 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5512 | } |
| 5513 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5514 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5515 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5516 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5517 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5518 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5519 | // write the answer into (either by allocating or by overwriting). |
| 5520 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5521 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5522 | Label slow, result_not_a_smi; |
| 5523 | Label r0_is_smi, r1_is_smi; |
| 5524 | Label done_checking_r0, done_checking_r1; |
| 5525 | |
| 5526 | __ tst(r1, Operand(kSmiTagMask)); |
| 5527 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5528 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5529 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5530 | GetInt32(masm, r1, r3, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5531 | __ jmp(&done_checking_r1); |
| 5532 | __ bind(&r1_is_smi); |
| 5533 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5534 | __ bind(&done_checking_r1); |
| 5535 | |
| 5536 | __ tst(r0, Operand(kSmiTagMask)); |
| 5537 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5538 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5539 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5540 | GetInt32(masm, r0, r2, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5541 | __ jmp(&done_checking_r0); |
| 5542 | __ bind(&r0_is_smi); |
| 5543 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5544 | __ bind(&done_checking_r0); |
| 5545 | |
| 5546 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5547 | // r2 and r3: Signed int32 operands. |
| 5548 | switch (op_) { |
| 5549 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5550 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5551 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5552 | case Token::SAR: |
| 5553 | // Use only the 5 least significant bits of the shift count. |
| 5554 | __ and_(r2, r2, Operand(0x1f)); |
| 5555 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5556 | break; |
| 5557 | case Token::SHR: |
| 5558 | // Use only the 5 least significant bits of the shift count. |
| 5559 | __ and_(r2, r2, Operand(0x1f)); |
| 5560 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5561 | // SHR is special because it is required to produce a positive answer. |
| 5562 | // The code below for writing into heap numbers isn't capable of writing |
| 5563 | // the register as an unsigned int so we go to slow case if we hit this |
| 5564 | // case. |
| 5565 | __ b(mi, &slow); |
| 5566 | break; |
| 5567 | case Token::SHL: |
| 5568 | // Use only the 5 least significant bits of the shift count. |
| 5569 | __ and_(r2, r2, Operand(0x1f)); |
| 5570 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5571 | break; |
| 5572 | default: UNREACHABLE(); |
| 5573 | } |
| 5574 | // check that the *signed* result fits in a smi |
| 5575 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5576 | __ b(mi, &result_not_a_smi); |
| 5577 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5578 | __ Ret(); |
| 5579 | |
| 5580 | Label have_to_allocate, got_a_heap_number; |
| 5581 | __ bind(&result_not_a_smi); |
| 5582 | switch (mode_) { |
| 5583 | case OVERWRITE_RIGHT: { |
| 5584 | __ tst(r0, Operand(kSmiTagMask)); |
| 5585 | __ b(eq, &have_to_allocate); |
| 5586 | __ mov(r5, Operand(r0)); |
| 5587 | break; |
| 5588 | } |
| 5589 | case OVERWRITE_LEFT: { |
| 5590 | __ tst(r1, Operand(kSmiTagMask)); |
| 5591 | __ b(eq, &have_to_allocate); |
| 5592 | __ mov(r5, Operand(r1)); |
| 5593 | break; |
| 5594 | } |
| 5595 | case NO_OVERWRITE: { |
| 5596 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5597 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5598 | } |
| 5599 | default: break; |
| 5600 | } |
| 5601 | __ bind(&got_a_heap_number); |
| 5602 | // r2: Answer as signed int32. |
| 5603 | // r5: Heap number to write answer into. |
| 5604 | |
| 5605 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5606 | // result. |
| 5607 | __ mov(r0, Operand(r5)); |
| 5608 | |
| 5609 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5610 | // r3 as scratch. r0 is preserved and returned. |
| 5611 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5612 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5613 | |
| 5614 | if (mode_ != NO_OVERWRITE) { |
| 5615 | __ bind(&have_to_allocate); |
| 5616 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5617 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5618 | __ jmp(&got_a_heap_number); |
| 5619 | } |
| 5620 | |
| 5621 | // If all else failed then we go to the runtime system. |
| 5622 | __ bind(&slow); |
| 5623 | __ push(r1); // restore stack |
| 5624 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5625 | switch (op_) { |
| 5626 | case Token::BIT_OR: |
| 5627 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5628 | break; |
| 5629 | case Token::BIT_AND: |
| 5630 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5631 | break; |
| 5632 | case Token::BIT_XOR: |
| 5633 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5634 | break; |
| 5635 | case Token::SAR: |
| 5636 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5637 | break; |
| 5638 | case Token::SHR: |
| 5639 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5640 | break; |
| 5641 | case Token::SHL: |
| 5642 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5643 | break; |
| 5644 | default: |
| 5645 | UNREACHABLE(); |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5650 | // Can we multiply by x with max two shifts and an add. |
| 5651 | // This answers yes to all integers from 2 to 10. |
| 5652 | static bool IsEasyToMultiplyBy(int x) { |
| 5653 | if (x < 2) return false; // Avoid special cases. |
| 5654 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 5655 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 5656 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 5657 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 5658 | return false; |
| 5659 | } |
| 5660 | |
| 5661 | |
| 5662 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 5663 | // Source and destination may be the same register. This routine does |
| 5664 | // not set carry and overflow the way a mul instruction would. |
| 5665 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 5666 | Register source, |
| 5667 | Register destination, |
| 5668 | int known_int) { |
| 5669 | if (IsPowerOf2(known_int)) { |
| 5670 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 5671 | } else if (PopCountLessThanEqual2(known_int)) { |
| 5672 | int first_bit = BitPosition(known_int); |
| 5673 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 5674 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 5675 | if (first_bit != 0) { |
| 5676 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 5677 | } |
| 5678 | } else { |
| 5679 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 5680 | int the_bit = BitPosition(known_int + 1); |
| 5681 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 5682 | } |
| 5683 | } |
| 5684 | |
| 5685 | |
| 5686 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 5687 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 5688 | // a result that needs shifting. |
| 5689 | static void MultiplyByKnownInt2( |
| 5690 | MacroAssembler* masm, |
| 5691 | Register result, |
| 5692 | Register source, |
| 5693 | Register known_int_register, // Smi tagged. |
| 5694 | int known_int, |
| 5695 | int* required_shift) { // Including Smi tag shift |
| 5696 | switch (known_int) { |
| 5697 | case 3: |
| 5698 | __ add(result, source, Operand(source, LSL, 1)); |
| 5699 | *required_shift = 1; |
| 5700 | break; |
| 5701 | case 5: |
| 5702 | __ add(result, source, Operand(source, LSL, 2)); |
| 5703 | *required_shift = 1; |
| 5704 | break; |
| 5705 | case 6: |
| 5706 | __ add(result, source, Operand(source, LSL, 1)); |
| 5707 | *required_shift = 2; |
| 5708 | break; |
| 5709 | case 7: |
| 5710 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 5711 | *required_shift = 1; |
| 5712 | break; |
| 5713 | case 9: |
| 5714 | __ add(result, source, Operand(source, LSL, 3)); |
| 5715 | *required_shift = 1; |
| 5716 | break; |
| 5717 | case 10: |
| 5718 | __ add(result, source, Operand(source, LSL, 2)); |
| 5719 | *required_shift = 2; |
| 5720 | break; |
| 5721 | default: |
| 5722 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 5723 | __ mul(result, source, known_int_register); |
| 5724 | *required_shift = 0; |
| 5725 | } |
| 5726 | } |
| 5727 | |
| 5728 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5729 | const char* GenericBinaryOpStub::GetName() { |
| 5730 | if (name_ != NULL) return name_; |
| 5731 | const int len = 100; |
| 5732 | name_ = Bootstrapper::AllocateAutoDeletedArray(len); |
| 5733 | if (name_ == NULL) return "OOM"; |
| 5734 | const char* op_name = Token::Name(op_); |
| 5735 | const char* overwrite_name; |
| 5736 | switch (mode_) { |
| 5737 | case NO_OVERWRITE: overwrite_name = "Alloc"; break; |
| 5738 | case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; |
| 5739 | case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; |
| 5740 | default: overwrite_name = "UnknownOverwrite"; break; |
| 5741 | } |
| 5742 | |
| 5743 | OS::SNPrintF(Vector<char>(name_, len), |
| 5744 | "GenericBinaryOpStub_%s_%s%s", |
| 5745 | op_name, |
| 5746 | overwrite_name, |
| 5747 | specialized_on_rhs_ ? "_ConstantRhs" : 0); |
| 5748 | return name_; |
| 5749 | } |
| 5750 | |
| 5751 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5752 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5753 | // r1 : x |
| 5754 | // r0 : y |
| 5755 | // result : r0 |
| 5756 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5757 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 5758 | // tell us that. |
| 5759 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 5760 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5761 | switch (op_) { |
| 5762 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5763 | Label not_smi; |
| 5764 | // Fast path. |
| 5765 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5766 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5767 | __ b(ne, ¬_smi); |
| 5768 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 5769 | // Return if no overflow. |
| 5770 | __ Ret(vc); |
| 5771 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 5772 | |
| 5773 | HandleBinaryOpSlowCases(masm, |
| 5774 | ¬_smi, |
| 5775 | Builtins::ADD, |
| 5776 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5777 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5778 | break; |
| 5779 | } |
| 5780 | |
| 5781 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5782 | Label not_smi; |
| 5783 | // Fast path. |
| 5784 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5785 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5786 | __ b(ne, ¬_smi); |
| 5787 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 5788 | // Return if no overflow. |
| 5789 | __ Ret(vc); |
| 5790 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 5791 | |
| 5792 | HandleBinaryOpSlowCases(masm, |
| 5793 | ¬_smi, |
| 5794 | Builtins::SUB, |
| 5795 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5796 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5797 | break; |
| 5798 | } |
| 5799 | |
| 5800 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5801 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5802 | ASSERT(kSmiTag == 0); // adjust code below |
| 5803 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5804 | __ b(ne, ¬_smi); |
| 5805 | // Remove tag from one operand (but keep sign), so that result is Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5806 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5807 | // Do multiplication |
| 5808 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 5809 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5810 | __ mov(ip, Operand(r3, ASR, 31)); |
| 5811 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 5812 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5813 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5814 | __ tst(r3, Operand(r3)); |
| 5815 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5816 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5817 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 5818 | // We know one of them was zero. |
| 5819 | __ add(r2, r0, Operand(r1), SetCC); |
| 5820 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 5821 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 5822 | // Slow case. We fall through here if we multiplied a negative number |
| 5823 | // with 0, because that would mean we should produce -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5824 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5825 | |
| 5826 | HandleBinaryOpSlowCases(masm, |
| 5827 | ¬_smi, |
| 5828 | Builtins::MUL, |
| 5829 | Token::MUL, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5830 | mode_); |
| 5831 | break; |
| 5832 | } |
| 5833 | |
| 5834 | case Token::DIV: |
| 5835 | case Token::MOD: { |
| 5836 | Label not_smi; |
| 5837 | if (specialized_on_rhs_) { |
| 5838 | Label smi_is_unsuitable; |
| 5839 | __ BranchOnNotSmi(r1, ¬_smi); |
| 5840 | if (IsPowerOf2(constant_rhs_)) { |
| 5841 | if (op_ == Token::MOD) { |
| 5842 | __ and_(r0, |
| 5843 | r1, |
| 5844 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 5845 | SetCC); |
| 5846 | // We now have the answer, but if the input was negative we also |
| 5847 | // have the sign bit. Our work is done if the result is |
| 5848 | // positive or zero: |
| 5849 | __ Ret(pl); |
| 5850 | // A mod of a negative left hand side must return a negative number. |
| 5851 | // Unfortunately if the answer is 0 then we must return -0. And we |
| 5852 | // already optimistically trashed r0 so we may need to restore it. |
| 5853 | __ eor(r0, r0, Operand(0x80000000u), SetCC); |
| 5854 | // Next two instructions are conditional on the answer being -0. |
| 5855 | __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
| 5856 | __ b(eq, &smi_is_unsuitable); |
| 5857 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
| 5858 | __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_))); |
| 5859 | } else { |
| 5860 | ASSERT(op_ == Token::DIV); |
| 5861 | __ tst(r1, |
| 5862 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 5863 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 5864 | int shift = 0; |
| 5865 | int d = constant_rhs_; |
| 5866 | while ((d & 1) == 0) { |
| 5867 | d >>= 1; |
| 5868 | shift++; |
| 5869 | } |
| 5870 | __ mov(r0, Operand(r1, LSR, shift)); |
| 5871 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 5872 | } |
| 5873 | } else { |
| 5874 | // Not a power of 2. |
| 5875 | __ tst(r1, Operand(0x80000000u)); |
| 5876 | __ b(ne, &smi_is_unsuitable); |
| 5877 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 5878 | // multiplying. |
| 5879 | double divisor = 1.0 / constant_rhs_; |
| 5880 | int shift = 32; |
| 5881 | double scale = 4294967296.0; // 1 << 32. |
| 5882 | uint32_t mul; |
| 5883 | // Maximise the precision of the fixed point reciprocal. |
| 5884 | while (true) { |
| 5885 | mul = static_cast<uint32_t>(scale * divisor); |
| 5886 | if (mul >= 0x7fffffff) break; |
| 5887 | scale *= 2.0; |
| 5888 | shift++; |
| 5889 | } |
| 5890 | mul++; |
| 5891 | __ mov(r2, Operand(mul)); |
| 5892 | __ umull(r3, r2, r2, r1); |
| 5893 | __ mov(r2, Operand(r2, LSR, shift - 31)); |
| 5894 | // r2 is r1 / rhs. r2 is not Smi tagged. |
| 5895 | // r0 is still the known rhs. r0 is Smi tagged. |
| 5896 | // r1 is still the unkown lhs. r1 is Smi tagged. |
| 5897 | int required_r4_shift = 0; // Including the Smi tag shift of 1. |
| 5898 | // r4 = r2 * r0. |
| 5899 | MultiplyByKnownInt2(masm, |
| 5900 | r4, |
| 5901 | r2, |
| 5902 | r0, |
| 5903 | constant_rhs_, |
| 5904 | &required_r4_shift); |
| 5905 | // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs). |
| 5906 | if (op_ == Token::DIV) { |
| 5907 | __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC); |
| 5908 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
| 5909 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5910 | } else { |
| 5911 | ASSERT(op_ == Token::MOD); |
| 5912 | __ sub(r0, r1, Operand(r4, LSL, required_r4_shift)); |
| 5913 | } |
| 5914 | } |
| 5915 | __ Ret(); |
| 5916 | __ bind(&smi_is_unsuitable); |
| 5917 | } else { |
| 5918 | __ jmp(¬_smi); |
| 5919 | } |
| 5920 | HandleBinaryOpSlowCases(masm, |
| 5921 | ¬_smi, |
| 5922 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV, |
| 5923 | op_, |
| 5924 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5925 | break; |
| 5926 | } |
| 5927 | |
| 5928 | case Token::BIT_OR: |
| 5929 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5930 | case Token::BIT_XOR: |
| 5931 | case Token::SAR: |
| 5932 | case Token::SHR: |
| 5933 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5934 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5935 | ASSERT(kSmiTag == 0); // adjust code below |
| 5936 | __ tst(r2, Operand(kSmiTagMask)); |
| 5937 | __ b(ne, &slow); |
| 5938 | switch (op_) { |
| 5939 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 5940 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 5941 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5942 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5943 | // Remove tags from right operand. |
| 5944 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5945 | // Use only the 5 least significant bits of the shift count. |
| 5946 | __ and_(r2, r2, Operand(0x1f)); |
| 5947 | __ mov(r0, Operand(r1, ASR, r2)); |
| 5948 | // Smi tag result. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5949 | __ bic(r0, r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5950 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5951 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5952 | // Remove tags from operands. We can't do this on a 31 bit number |
| 5953 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 5954 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5955 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5956 | // Use only the 5 least significant bits of the shift count. |
| 5957 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5958 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5959 | // Unsigned shift is not allowed to produce a negative number, so |
| 5960 | // check the sign bit and the sign bit after Smi tagging. |
| 5961 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5962 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5963 | // Smi tag result. |
| 5964 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5965 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5966 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5967 | // Remove tags from operands. |
| 5968 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5969 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5970 | // Use only the 5 least significant bits of the shift count. |
| 5971 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5972 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5973 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5974 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 5975 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5976 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5977 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5978 | default: UNREACHABLE(); |
| 5979 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5980 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5981 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5982 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5983 | break; |
| 5984 | } |
| 5985 | |
| 5986 | default: UNREACHABLE(); |
| 5987 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5988 | // This code should be unreachable. |
| 5989 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5990 | } |
| 5991 | |
| 5992 | |
| 5993 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5994 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 5995 | // argument, so give it a Smi. |
| 5996 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5997 | __ push(r0); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5998 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5999 | |
| 6000 | __ StubReturn(1); |
| 6001 | } |
| 6002 | |
| 6003 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 6004 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
| 6005 | ASSERT(op_ == Token::SUB); |
| 6006 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6007 | Label undo; |
| 6008 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6009 | Label not_smi; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6010 | |
| 6011 | // Enter runtime system if the value is not a smi. |
| 6012 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6013 | __ b(ne, ¬_smi); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6014 | |
| 6015 | // Enter runtime system if the value of the expression is zero |
| 6016 | // to make sure that we switch between 0 and -0. |
| 6017 | __ cmp(r0, Operand(0)); |
| 6018 | __ b(eq, &slow); |
| 6019 | |
| 6020 | // The value of the expression is a smi that is not zero. Try |
| 6021 | // optimistic subtraction '0 - value'. |
| 6022 | __ rsb(r1, r0, Operand(0), SetCC); |
| 6023 | __ b(vs, &slow); |
| 6024 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6025 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 6026 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6027 | |
| 6028 | // Enter runtime system. |
| 6029 | __ bind(&slow); |
| 6030 | __ push(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6031 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 6032 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6033 | __ bind(¬_smi); |
| 6034 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 6035 | __ b(ne, &slow); |
| 6036 | // r0 is a heap number. Get a new heap number in r1. |
| 6037 | if (overwrite_) { |
| 6038 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6039 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6040 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 6041 | } else { |
| 6042 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6043 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6044 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6045 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6046 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 6047 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 6048 | __ mov(r0, Operand(r1)); |
| 6049 | } |
| 6050 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
| 6053 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6054 | int CEntryStub::MinorKey() { |
| 6055 | ASSERT(result_size_ <= 2); |
| 6056 | // Result returned in r0 or r0+r1 by default. |
| 6057 | return 0; |
| 6058 | } |
| 6059 | |
| 6060 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6061 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6062 | // r0 holds the exception. |
| 6063 | |
| 6064 | // Adjust this code if not the case. |
| 6065 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6066 | |
| 6067 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6068 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 6069 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6070 | |
| 6071 | // Restore the next handler and frame pointer, discard handler state. |
| 6072 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 6073 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6074 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6075 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6076 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 6077 | |
| 6078 | // Before returning we restore the context from the frame pointer if |
| 6079 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6080 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6081 | __ cmp(fp, Operand(0)); |
| 6082 | // Set cp to NULL if fp is NULL. |
| 6083 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6084 | // Restore cp otherwise. |
| 6085 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6086 | #ifdef DEBUG |
| 6087 | if (FLAG_debug_code) { |
| 6088 | __ mov(lr, Operand(pc)); |
| 6089 | } |
| 6090 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6091 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6092 | __ pop(pc); |
| 6093 | } |
| 6094 | |
| 6095 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6096 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 6097 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6098 | // Adjust this code if not the case. |
| 6099 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 6100 | |
| 6101 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6102 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6103 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6104 | |
| 6105 | // Unwind the handlers until the ENTRY handler is found. |
| 6106 | Label loop, done; |
| 6107 | __ bind(&loop); |
| 6108 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6109 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 6110 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6111 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 6112 | __ b(eq, &done); |
| 6113 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6114 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 6115 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6116 | __ jmp(&loop); |
| 6117 | __ bind(&done); |
| 6118 | |
| 6119 | // 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] | 6120 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6121 | __ pop(r2); |
| 6122 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6123 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6124 | if (type == OUT_OF_MEMORY) { |
| 6125 | // Set external caught exception to false. |
| 6126 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 6127 | __ mov(r0, Operand(false)); |
| 6128 | __ mov(r2, Operand(external_caught)); |
| 6129 | __ str(r0, MemOperand(r2)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6130 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6131 | // Set pending exception and r0 to out of memory exception. |
| 6132 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6133 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6134 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 6135 | __ str(r0, MemOperand(r2)); |
| 6136 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6137 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6138 | // Stack layout at this point. See also StackHandlerConstants. |
| 6139 | // sp -> state (ENTRY) |
| 6140 | // fp |
| 6141 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6142 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6143 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 6144 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 6145 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 6146 | // Before returning we restore the context from the frame pointer if |
| 6147 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 6148 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6149 | __ cmp(fp, Operand(0)); |
| 6150 | // Set cp to NULL if fp is NULL. |
| 6151 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 6152 | // Restore cp otherwise. |
| 6153 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6154 | #ifdef DEBUG |
| 6155 | if (FLAG_debug_code) { |
| 6156 | __ mov(lr, Operand(pc)); |
| 6157 | } |
| 6158 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6159 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6160 | __ pop(pc); |
| 6161 | } |
| 6162 | |
| 6163 | |
| 6164 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 6165 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6166 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6167 | Label* throw_out_of_memory_exception, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6168 | ExitFrame::Mode mode, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6169 | bool do_gc, |
| 6170 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6171 | // r0: result parameter for PerformGC, if any |
| 6172 | // r4: number of arguments including receiver (C callee-saved) |
| 6173 | // r5: pointer to builtin function (C callee-saved) |
| 6174 | // r6: pointer to the first argument (C callee-saved) |
| 6175 | |
| 6176 | if (do_gc) { |
| 6177 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6178 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 6179 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6182 | ExternalReference scope_depth = |
| 6183 | ExternalReference::heap_always_allocate_scope_depth(); |
| 6184 | if (always_allocate) { |
| 6185 | __ mov(r0, Operand(scope_depth)); |
| 6186 | __ ldr(r1, MemOperand(r0)); |
| 6187 | __ add(r1, r1, Operand(1)); |
| 6188 | __ str(r1, MemOperand(r0)); |
| 6189 | } |
| 6190 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6191 | // Call C built-in. |
| 6192 | // r0 = argc, r1 = argv |
| 6193 | __ mov(r0, Operand(r4)); |
| 6194 | __ mov(r1, Operand(r6)); |
| 6195 | |
| 6196 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 6197 | // frames, we need to know where the return address is. Right now, |
| 6198 | // we push it on the stack to be able to find it again, but we never |
| 6199 | // restore from it in case of changes, which makes it impossible to |
| 6200 | // support moving the C entry code stub. This should be fixed, but currently |
| 6201 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 6202 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6203 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 6204 | masm->push(lr); |
| 6205 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6206 | |
| 6207 | if (always_allocate) { |
| 6208 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 6209 | // though (contain the result). |
| 6210 | __ mov(r2, Operand(scope_depth)); |
| 6211 | __ ldr(r3, MemOperand(r2)); |
| 6212 | __ sub(r3, r3, Operand(1)); |
| 6213 | __ str(r3, MemOperand(r2)); |
| 6214 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6215 | |
| 6216 | // check for failure result |
| 6217 | Label failure_returned; |
| 6218 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 6219 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 6220 | __ add(r2, r0, Operand(1)); |
| 6221 | __ tst(r2, Operand(kFailureTagMask)); |
| 6222 | __ b(eq, &failure_returned); |
| 6223 | |
| 6224 | // Exit C frame and return. |
| 6225 | // r0:r1: result |
| 6226 | // sp: stack pointer |
| 6227 | // fp: frame pointer |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6228 | __ LeaveExitFrame(mode); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6229 | |
| 6230 | // check if we should retry or throw exception |
| 6231 | Label retry; |
| 6232 | __ bind(&failure_returned); |
| 6233 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 6234 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 6235 | __ b(eq, &retry); |
| 6236 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6237 | // Special handling of out of memory exceptions. |
| 6238 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6239 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6240 | __ b(eq, throw_out_of_memory_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6241 | |
| 6242 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6243 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6244 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6245 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6246 | __ ldr(r0, MemOperand(ip)); |
| 6247 | __ str(r3, MemOperand(ip)); |
| 6248 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6249 | // Special handling of termination exceptions which are uncatchable |
| 6250 | // by javascript code. |
| 6251 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 6252 | __ b(eq, throw_termination_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6253 | |
| 6254 | // Handle normal exception. |
| 6255 | __ jmp(throw_normal_exception); |
| 6256 | |
| 6257 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 6258 | } |
| 6259 | |
| 6260 | |
| 6261 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 6262 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 6263 | // r0: number of arguments including receiver |
| 6264 | // r1: pointer to builtin function |
| 6265 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6266 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6267 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6268 | |
| 6269 | // NOTE: Invocations of builtins may return failure objects |
| 6270 | // instead of a proper result. The builtin entry handles |
| 6271 | // this by performing a garbage collection and retrying the |
| 6272 | // builtin once. |
| 6273 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6274 | ExitFrame::Mode mode = is_debug_break |
| 6275 | ? ExitFrame::MODE_DEBUG |
| 6276 | : ExitFrame::MODE_NORMAL; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6277 | |
| 6278 | // Enter the exit frame that transitions from JavaScript to C++. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6279 | __ EnterExitFrame(mode); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6280 | |
| 6281 | // r4: number of arguments (C callee-saved) |
| 6282 | // r5: pointer to builtin function (C callee-saved) |
| 6283 | // r6: pointer to first argument (C callee-saved) |
| 6284 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6285 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6286 | Label throw_termination_exception; |
| 6287 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6288 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6289 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6290 | GenerateCore(masm, |
| 6291 | &throw_normal_exception, |
| 6292 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6293 | &throw_out_of_memory_exception, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6294 | mode, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6295 | false, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6296 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6297 | |
| 6298 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6299 | GenerateCore(masm, |
| 6300 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6301 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6302 | &throw_out_of_memory_exception, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6303 | mode, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6304 | true, |
| 6305 | false); |
| 6306 | |
| 6307 | // Do full GC and retry runtime call one final time. |
| 6308 | Failure* failure = Failure::InternalError(); |
| 6309 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 6310 | GenerateCore(masm, |
| 6311 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6312 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6313 | &throw_out_of_memory_exception, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6314 | mode, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6315 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6316 | true); |
| 6317 | |
| 6318 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6319 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 6320 | |
| 6321 | __ bind(&throw_termination_exception); |
| 6322 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6323 | |
| 6324 | __ bind(&throw_normal_exception); |
| 6325 | GenerateThrowTOS(masm); |
| 6326 | } |
| 6327 | |
| 6328 | |
| 6329 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 6330 | // r0: code entry |
| 6331 | // r1: function |
| 6332 | // r2: receiver |
| 6333 | // r3: argc |
| 6334 | // [sp+0]: argv |
| 6335 | |
| 6336 | Label invoke, exit; |
| 6337 | |
| 6338 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 6339 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6340 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6341 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 6342 | |
| 6343 | // Get address of argv, see stm above. |
| 6344 | // r0: code entry |
| 6345 | // r1: function |
| 6346 | // r2: receiver |
| 6347 | // r3: argc |
| 6348 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 6349 | __ ldr(r4, MemOperand(r4)); // argv |
| 6350 | |
| 6351 | // Push a frame with special values setup to mark it as an entry frame. |
| 6352 | // r0: code entry |
| 6353 | // r1: function |
| 6354 | // r2: receiver |
| 6355 | // r3: argc |
| 6356 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6357 | __ 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] | 6358 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 6359 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6360 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 6361 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6362 | __ ldr(r5, MemOperand(r5)); |
| 6363 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 6364 | |
| 6365 | // Setup frame pointer for the frame to be pushed. |
| 6366 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6367 | |
| 6368 | // Call a faked try-block that does the invoke. |
| 6369 | __ bl(&invoke); |
| 6370 | |
| 6371 | // Caught exception: Store result (exception) in the pending |
| 6372 | // exception field in the JSEnv and return a failure sentinel. |
| 6373 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 6374 | // 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] | 6375 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6376 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6377 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6378 | __ b(&exit); |
| 6379 | |
| 6380 | // Invoke: Link this frame into the handler chain. |
| 6381 | __ bind(&invoke); |
| 6382 | // Must preserve r0-r4, r5-r7 are available. |
| 6383 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6384 | // If an exception not caught by another handler occurs, this handler |
| 6385 | // returns control to the code after the bl(&invoke) above, which |
| 6386 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 6387 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6388 | |
| 6389 | // Clear any pending exceptions. |
| 6390 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6391 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6392 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6393 | __ str(r5, MemOperand(ip)); |
| 6394 | |
| 6395 | // Invoke the function by calling through JS entry trampoline builtin. |
| 6396 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6397 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6398 | |
| 6399 | // Expected registers by Builtins::JSEntryTrampoline |
| 6400 | // r0: code entry |
| 6401 | // r1: function |
| 6402 | // r2: receiver |
| 6403 | // r3: argc |
| 6404 | // r4: argv |
| 6405 | if (is_construct) { |
| 6406 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6407 | __ mov(ip, Operand(construct_entry)); |
| 6408 | } else { |
| 6409 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6410 | __ mov(ip, Operand(entry)); |
| 6411 | } |
| 6412 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6413 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6414 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6415 | // macro for the add instruction because we don't want the coverage tool |
| 6416 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6417 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6418 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6419 | |
| 6420 | // Unlink this frame from the handler chain. When reading the |
| 6421 | // address of the next handler, there is no need to use the address |
| 6422 | // displacement since the current stack pointer (sp) points directly |
| 6423 | // to the stack handler. |
| 6424 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6425 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6426 | __ str(r3, MemOperand(ip)); |
| 6427 | // No need to restore registers |
| 6428 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6429 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6430 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6431 | __ bind(&exit); // r0 holds result |
| 6432 | // Restore the top frame descriptors from the stack. |
| 6433 | __ pop(r3); |
| 6434 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6435 | __ str(r3, MemOperand(ip)); |
| 6436 | |
| 6437 | // Reset the stack to the callee saved registers. |
| 6438 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6439 | |
| 6440 | // Restore callee-saved registers and return. |
| 6441 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6442 | if (FLAG_debug_code) { |
| 6443 | __ mov(lr, Operand(pc)); |
| 6444 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6445 | #endif |
| 6446 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6447 | } |
| 6448 | |
| 6449 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6450 | // This stub performs an instanceof, calling the builtin function if |
| 6451 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6452 | // be an instance of (these are fetched from the stack). |
| 6453 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6454 | // Get the object - slow case for smis (we may need to throw an exception |
| 6455 | // depending on the rhs). |
| 6456 | Label slow, loop, is_instance, is_not_instance; |
| 6457 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6458 | __ BranchOnSmi(r0, &slow); |
| 6459 | |
| 6460 | // Check that the left hand is a JS object and put map in r3. |
| 6461 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6462 | __ b(lt, &slow); |
| 6463 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6464 | __ b(gt, &slow); |
| 6465 | |
| 6466 | // Get the prototype of the function (r4 is result, r2 is scratch). |
| 6467 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 6468 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6469 | |
| 6470 | // Check that the function prototype is a JS object. |
| 6471 | __ BranchOnSmi(r4, &slow); |
| 6472 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6473 | __ b(lt, &slow); |
| 6474 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6475 | __ b(gt, &slow); |
| 6476 | |
| 6477 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6478 | // Get prototype of object into r2. |
| 6479 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6480 | |
| 6481 | // Loop through the prototype chain looking for the function prototype. |
| 6482 | __ bind(&loop); |
| 6483 | __ cmp(r2, Operand(r4)); |
| 6484 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6485 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 6486 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6487 | __ b(eq, &is_not_instance); |
| 6488 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6489 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6490 | __ jmp(&loop); |
| 6491 | |
| 6492 | __ bind(&is_instance); |
| 6493 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6494 | __ pop(); |
| 6495 | __ pop(); |
| 6496 | __ mov(pc, Operand(lr)); // Return. |
| 6497 | |
| 6498 | __ bind(&is_not_instance); |
| 6499 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6500 | __ pop(); |
| 6501 | __ pop(); |
| 6502 | __ mov(pc, Operand(lr)); // Return. |
| 6503 | |
| 6504 | // Slow-case. Tail call builtin. |
| 6505 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6506 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6507 | } |
| 6508 | |
| 6509 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6510 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6511 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6512 | Label adaptor; |
| 6513 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6514 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6515 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6516 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6517 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6518 | // Nothing to do: The formal number of parameters has already been |
| 6519 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6520 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6521 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6522 | // Arguments adaptor case: Read the arguments length from the |
| 6523 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6524 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6525 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6526 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6530 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6531 | // The displacement is the offset of the last parameter (if any) |
| 6532 | // relative to the frame pointer. |
| 6533 | static const int kDisplacement = |
| 6534 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6535 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6536 | // Check that the key is a smi. |
| 6537 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6538 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6539 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6540 | // Check if the calling frame is an arguments adaptor frame. |
| 6541 | Label adaptor; |
| 6542 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6543 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6544 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6545 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6546 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6547 | // Check index against formal parameters count limit passed in |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6548 | // through register r0. Use unsigned comparison to get negative |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6549 | // check for free. |
| 6550 | __ cmp(r1, r0); |
| 6551 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6552 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6553 | // Read the argument from the stack and return it. |
| 6554 | __ sub(r3, r0, r1); |
| 6555 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6556 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6557 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6558 | |
| 6559 | // Arguments adaptor case: Check index against actual arguments |
| 6560 | // limit found in the arguments adaptor frame. Use unsigned |
| 6561 | // comparison to get negative check for free. |
| 6562 | __ bind(&adaptor); |
| 6563 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6564 | __ cmp(r1, r0); |
| 6565 | __ b(cs, &slow); |
| 6566 | |
| 6567 | // Read the argument from the adaptor frame and return it. |
| 6568 | __ sub(r3, r0, r1); |
| 6569 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6570 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6571 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6572 | |
| 6573 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6574 | // by calling the runtime system. |
| 6575 | __ bind(&slow); |
| 6576 | __ push(r1); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6577 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6578 | } |
| 6579 | |
| 6580 | |
| 6581 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 6582 | // Check if the calling frame is an arguments adaptor frame. |
| 6583 | Label runtime; |
| 6584 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6585 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6586 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6587 | __ b(ne, &runtime); |
| 6588 | |
| 6589 | // Patch the arguments.length and the parameters pointer. |
| 6590 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6591 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 6592 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6593 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6594 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6595 | |
| 6596 | // Do the runtime call to allocate the arguments object. |
| 6597 | __ bind(&runtime); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6598 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6599 | } |
| 6600 | |
| 6601 | |
| 6602 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6603 | Label slow; |
| 6604 | // Get the function to call from the stack. |
| 6605 | // function, receiver [, arguments] |
| 6606 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6607 | |
| 6608 | // Check that the function is really a JavaScript function. |
| 6609 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6610 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6611 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6612 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6613 | __ b(ne, &slow); |
| 6614 | |
| 6615 | // Fast-case: Invoke the function now. |
| 6616 | // r1: pushed function |
| 6617 | ParameterCount actual(argc_); |
| 6618 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6619 | |
| 6620 | // Slow-case: Non-function called. |
| 6621 | __ bind(&slow); |
| 6622 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6623 | __ mov(r2, Operand(0)); |
| 6624 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6625 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6626 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6627 | } |
| 6628 | |
| 6629 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 6630 | const char* CompareStub::GetName() { |
| 6631 | switch (cc_) { |
| 6632 | case lt: return "CompareStub_LT"; |
| 6633 | case gt: return "CompareStub_GT"; |
| 6634 | case le: return "CompareStub_LE"; |
| 6635 | case ge: return "CompareStub_GE"; |
| 6636 | case ne: { |
| 6637 | if (strict_) { |
| 6638 | if (never_nan_nan_) { |
| 6639 | return "CompareStub_NE_STRICT_NO_NAN"; |
| 6640 | } else { |
| 6641 | return "CompareStub_NE_STRICT"; |
| 6642 | } |
| 6643 | } else { |
| 6644 | if (never_nan_nan_) { |
| 6645 | return "CompareStub_NE_NO_NAN"; |
| 6646 | } else { |
| 6647 | return "CompareStub_NE"; |
| 6648 | } |
| 6649 | } |
| 6650 | } |
| 6651 | case eq: { |
| 6652 | if (strict_) { |
| 6653 | if (never_nan_nan_) { |
| 6654 | return "CompareStub_EQ_STRICT_NO_NAN"; |
| 6655 | } else { |
| 6656 | return "CompareStub_EQ_STRICT"; |
| 6657 | } |
| 6658 | } else { |
| 6659 | if (never_nan_nan_) { |
| 6660 | return "CompareStub_EQ_NO_NAN"; |
| 6661 | } else { |
| 6662 | return "CompareStub_EQ"; |
| 6663 | } |
| 6664 | } |
| 6665 | } |
| 6666 | default: return "CompareStub"; |
| 6667 | } |
| 6668 | } |
| 6669 | |
| 6670 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6671 | int CompareStub::MinorKey() { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame^] | 6672 | // Encode the three parameters in a unique 16 bit value. |
| 6673 | ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16)); |
| 6674 | int nnn_value = (never_nan_nan_ ? 2 : 0); |
| 6675 | if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs. |
| 6676 | return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6677 | } |
| 6678 | |
| 6679 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6680 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6681 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6682 | } } // namespace v8::internal |