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" |
| 32 | #include "debug.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 33 | #include "parser.h" |
| 34 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 36 | #include "scopes.h" |
| 37 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 42 | #define __ ACCESS_MASM(masm_) |
| 43 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 44 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 45 | Label* slow, |
| 46 | Condition cc); |
| 47 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 48 | Label* rhs_not_nan, |
| 49 | Label* slow, |
| 50 | bool strict); |
| 51 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
| 52 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 53 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 54 | Register source, |
| 55 | Register destination, |
| 56 | int known_int); |
| 57 | static bool IsEasyToMultiplyBy(int x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 61 | // ------------------------------------------------------------------------- |
| 62 | // Platform-specific DeferredCode functions. |
| 63 | |
| 64 | void DeferredCode::SaveRegisters() { |
| 65 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 66 | int action = registers_[i]; |
| 67 | if (action == kPush) { |
| 68 | __ push(RegisterAllocator::ToRegister(i)); |
| 69 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 70 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | void DeferredCode::RestoreRegisters() { |
| 77 | // Restore registers in reverse order due to the stack. |
| 78 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 79 | int action = registers_[i]; |
| 80 | if (action == kPush) { |
| 81 | __ pop(RegisterAllocator::ToRegister(i)); |
| 82 | } else if (action != kIgnore) { |
| 83 | action &= ~kSyncedFlag; |
| 84 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 89 | |
| 90 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 91 | // CodeGenState implementation. |
| 92 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 93 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 94 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 95 | typeof_state_(NOT_INSIDE_TYPEOF), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 96 | true_target_(NULL), |
| 97 | false_target_(NULL), |
| 98 | previous_(NULL) { |
| 99 | owner_->set_state(this); |
| 100 | } |
| 101 | |
| 102 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 103 | CodeGenState::CodeGenState(CodeGenerator* owner, |
| 104 | TypeofState typeof_state, |
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), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 108 | typeof_state_(typeof_state), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 109 | true_target_(true_target), |
| 110 | false_target_(false_target), |
| 111 | previous_(owner->state()) { |
| 112 | owner_->set_state(this); |
| 113 | } |
| 114 | |
| 115 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 116 | CodeGenState::~CodeGenState() { |
| 117 | ASSERT(owner_->state() == this); |
| 118 | owner_->set_state(previous_); |
| 119 | } |
| 120 | |
| 121 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 122 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 123 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 125 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 126 | bool is_eval) |
| 127 | : is_eval_(is_eval), |
| 128 | script_(script), |
| 129 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 131 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 132 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 133 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | cc_reg_(al), |
| 135 | state_(NULL), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 136 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 141 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 143 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | // cp: callee's context |
| 145 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 146 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 147 | ZoneList<Statement*>* body = fun->body(); |
| 148 | |
| 149 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 150 | ASSERT(scope_ == NULL); |
| 151 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 152 | ASSERT(allocator_ == NULL); |
| 153 | RegisterAllocator register_allocator(this); |
| 154 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 155 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 156 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 157 | cc_reg_ = al; |
| 158 | { |
| 159 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 160 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 161 | // Entry: |
| 162 | // Stack: receiver, arguments |
| 163 | // lr: return address |
| 164 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 166 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 168 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 169 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | // tos: code slot |
| 171 | #ifdef DEBUG |
| 172 | if (strlen(FLAG_stop_at) > 0 && |
| 173 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 174 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 175 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 176 | } |
| 177 | #endif |
| 178 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 179 | // Allocate space for locals and initialize them. This also checks |
| 180 | // for stack overflow. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 181 | frame_->AllocateStackSlots(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 182 | // Initialize the function return target after the locals are set |
| 183 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 184 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 185 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 187 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 188 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 189 | // Allocate local context. |
| 190 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 191 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 192 | frame_->EmitPush(r0); |
| 193 | frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 194 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 195 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 196 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 197 | __ cmp(r0, Operand(cp)); |
| 198 | verified_true.Branch(eq); |
| 199 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 200 | verified_true.Bind(); |
| 201 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 203 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 206 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | // 1) only needed if we have a context |
| 208 | // 2) no need to recompute context ptr every single time |
| 209 | // 3) don't copy parameter operand code from SlotOperand! |
| 210 | { |
| 211 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 212 | |
| 213 | // Note that iteration order is relevant here! If we have the same |
| 214 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 215 | // needs to be copied into the context, it must be the last argument |
| 216 | // passed to the parameter that needs to be copied. This is a rare |
| 217 | // case so we don't check for it, instead we rely on the copying |
| 218 | // order: such a parameter is copied repeatedly into the same |
| 219 | // context location and thus the last value is what is seen inside |
| 220 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 221 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 222 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | Slot* slot = par->slot(); |
| 224 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 225 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 226 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 227 | // Loads r2 with context; used below in RecordWrite. |
| 228 | __ str(r1, SlotOperand(slot, r2)); |
| 229 | // Load the offset into r3. |
| 230 | int slot_offset = |
| 231 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 232 | __ mov(r3, Operand(slot_offset)); |
| 233 | __ RecordWrite(r2, r3, r1); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 238 | // Store the arguments object. This must happen after context |
| 239 | // initialization because the arguments object may be stored in the |
| 240 | // context. |
| 241 | if (scope_->arguments() != NULL) { |
| 242 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 243 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 244 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 245 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 246 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 247 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 248 | // The receiver is below the arguments, the return address, |
| 249 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 250 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 251 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 252 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 253 | frame_->Adjust(3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 254 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 255 | frame_->CallStub(&stub, 3); |
| 256 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 257 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 258 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 259 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 261 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 262 | } |
| 263 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 264 | // Generate code to 'execute' declarations and initialize functions |
| 265 | // (source elements). In case of an illegal redeclaration we need to |
| 266 | // handle that instead of processing the declarations. |
| 267 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 269 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 270 | } else { |
| 271 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 272 | ProcessDeclarations(scope_->declarations()); |
| 273 | // Bail out if a stack-overflow exception occurred when processing |
| 274 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 275 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | } |
| 277 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 278 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 279 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 280 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 281 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | |
| 283 | // Compile the body of the function in a vanilla state. Don't |
| 284 | // bother compiling all the code if the scope has an illegal |
| 285 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 286 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | Comment cmnt(masm_, "[ function body"); |
| 288 | #ifdef DEBUG |
| 289 | bool is_builtin = Bootstrapper::IsActive(); |
| 290 | bool should_trace = |
| 291 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 292 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 293 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 294 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 295 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 297 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | } |
| 300 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 301 | // Generate the return sequence if necessary. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 302 | if (has_valid_frame() || function_return_.is_linked()) { |
| 303 | if (!function_return_.is_linked()) { |
| 304 | CodeForReturnPosition(fun); |
| 305 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 306 | // exit |
| 307 | // r0: result |
| 308 | // sp: stack pointer |
| 309 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 310 | // cp: callee's context |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 311 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 312 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 313 | function_return_.Bind(); |
| 314 | if (FLAG_trace) { |
| 315 | // Push the return value on the stack as the parameter. |
| 316 | // Runtime::TraceExit returns the parameter as it is. |
| 317 | frame_->EmitPush(r0); |
| 318 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 319 | } |
| 320 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 321 | // Add a label for checking the size of the code used for returning. |
| 322 | Label check_exit_codesize; |
| 323 | masm_->bind(&check_exit_codesize); |
| 324 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 325 | // Tear down the frame which will restore the caller's frame pointer and |
| 326 | // the link register. |
| 327 | frame_->Exit(); |
| 328 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 329 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 330 | // tool from instrumenting as we rely on the code size here. |
| 331 | masm_->add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
| 332 | masm_->Jump(lr); |
| 333 | |
| 334 | // Check that the size of the code used for returning matches what is |
| 335 | // expected by the debugger. |
| 336 | ASSERT_EQ(kJSReturnSequenceLength, |
| 337 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 338 | } |
| 339 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 340 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | ASSERT(!has_cc()); |
| 342 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 343 | ASSERT(!function_return_is_shadowed_); |
| 344 | function_return_.Unuse(); |
| 345 | DeleteFrame(); |
| 346 | |
| 347 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 348 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 349 | ProcessDeferred(); |
| 350 | } |
| 351 | |
| 352 | allocator_ = NULL; |
| 353 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 357 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 358 | // Currently, this assertion will fail if we try to assign to |
| 359 | // a constant variable that is constant because it is read-only |
| 360 | // (such as the variable referring to a named function expression). |
| 361 | // We need to implement assignments to read-only variables. |
| 362 | // Ideally, we should do this during AST generation (by converting |
| 363 | // such assignments into expression statements); however, in general |
| 364 | // we may not be able to make the decision until past AST generation, |
| 365 | // that is when the entire program is known. |
| 366 | ASSERT(slot != NULL); |
| 367 | int index = slot->index(); |
| 368 | switch (slot->type()) { |
| 369 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 370 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 371 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 372 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 373 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 374 | |
| 375 | case Slot::CONTEXT: { |
| 376 | // Follow the context chain if necessary. |
| 377 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 378 | Register context = cp; |
| 379 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 380 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 381 | // Load the closure. |
| 382 | // (All contexts, even 'with' contexts, have a closure, |
| 383 | // and it is the same for all contexts inside a function. |
| 384 | // There is no need to go to the function context first.) |
| 385 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 386 | // Load the function context (which is the incoming, outer context). |
| 387 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 388 | context = tmp; |
| 389 | } |
| 390 | // We may have a 'with' context now. Get the function context. |
| 391 | // (In fact this mov may never be the needed, since the scope analysis |
| 392 | // may not permit a direct context access in this case and thus we are |
| 393 | // always at a function context. However it is safe to dereference be- |
| 394 | // cause the function context of a function context is itself. Before |
| 395 | // deleting this mov we should try to create a counter-example first, |
| 396 | // though...) |
| 397 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 398 | return ContextOperand(tmp, index); |
| 399 | } |
| 400 | |
| 401 | default: |
| 402 | UNREACHABLE(); |
| 403 | return MemOperand(r0, 0); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 408 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 409 | Slot* slot, |
| 410 | Register tmp, |
| 411 | Register tmp2, |
| 412 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 413 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 414 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 415 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 416 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 417 | if (s->num_heap_slots() > 0) { |
| 418 | if (s->calls_eval()) { |
| 419 | // Check that extension is NULL. |
| 420 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 421 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 422 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 423 | } |
| 424 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 425 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 426 | context = tmp; |
| 427 | } |
| 428 | } |
| 429 | // Check that last extension is NULL. |
| 430 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 431 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 432 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 433 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 434 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 438 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 439 | // (partially) translated into branches, or it may have set the condition |
| 440 | // code register. If force_cc is set, the value is forced to set the |
| 441 | // condition code register and no value is pushed. If the condition code |
| 442 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 443 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 444 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 445 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 446 | JumpTarget* true_target, |
| 447 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 448 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 449 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 450 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 451 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 452 | { CodeGenState new_state(this, typeof_state, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 453 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 454 | |
| 455 | // If we hit a stack overflow, we may not have actually visited |
| 456 | // the expression. In that case, we ensure that we have a |
| 457 | // valid-looking frame state because we will continue to generate |
| 458 | // code as we unwind the C++ stack. |
| 459 | // |
| 460 | // It's possible to have both a stack overflow and a valid frame |
| 461 | // state (eg, a subexpression overflowed, visiting it returned |
| 462 | // with a dummied frame state, and visiting this expression |
| 463 | // returned with a normal-looking state). |
| 464 | if (HasStackOverflow() && |
| 465 | has_valid_frame() && |
| 466 | !has_cc() && |
| 467 | frame_->height() == original_height) { |
| 468 | true_target->Jump(); |
| 469 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 470 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 471 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 472 | // Convert the TOS value to a boolean in the condition code register. |
| 473 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 474 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 475 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 476 | ASSERT(!has_valid_frame() || |
| 477 | (has_cc() && frame_->height() == original_height) || |
| 478 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 482 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 483 | #ifdef DEBUG |
| 484 | int original_height = frame_->height(); |
| 485 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 486 | JumpTarget true_target; |
| 487 | JumpTarget false_target; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 488 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 489 | |
| 490 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 491 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 492 | JumpTarget loaded; |
| 493 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 494 | materialize_true.Branch(cc_reg_); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 495 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 496 | frame_->EmitPush(r0); |
| 497 | loaded.Jump(); |
| 498 | materialize_true.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 499 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 500 | frame_->EmitPush(r0); |
| 501 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 502 | cc_reg_ = al; |
| 503 | } |
| 504 | |
| 505 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 506 | // We have at least one condition value that has been "translated" |
| 507 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 508 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 509 | if (frame_ != NULL) { |
| 510 | loaded.Jump(); // Don't lose the current TOS. |
| 511 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 512 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 513 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 515 | true_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 516 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 517 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 519 | // If both "true" and "false" need to be loaded jump across the code for |
| 520 | // "false". |
| 521 | if (both) { |
| 522 | loaded.Jump(); |
| 523 | } |
| 524 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 525 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 526 | false_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 527 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 528 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 529 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 530 | // A value is loaded on all paths reaching this point. |
| 531 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 533 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 534 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 535 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 539 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 540 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 541 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 542 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 546 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 547 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 548 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 549 | __ ldr(scratch, |
| 550 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 551 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 555 | // TODO(1241834): Get rid of this function in favor of just using Load, now |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 556 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 557 | // variables w/o reference errors elsewhere. |
| 558 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 559 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 561 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 562 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 563 | // the variable as if through '<global>.<variable>' to make sure we |
| 564 | // do not get reference errors. |
| 565 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 566 | Literal key(variable->name()); |
| 567 | // TODO(1241834): Fetch the position from the variable instead of using |
| 568 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 569 | Property property(&global, &key, RelocInfo::kNoPosition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 570 | LoadAndSpill(&property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 572 | LoadAndSpill(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 577 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 578 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 579 | cgen->LoadReference(this); |
| 580 | } |
| 581 | |
| 582 | |
| 583 | Reference::~Reference() { |
| 584 | cgen_->UnloadReference(this); |
| 585 | } |
| 586 | |
| 587 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 588 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 589 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 590 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 591 | Expression* e = ref->expression(); |
| 592 | Property* property = e->AsProperty(); |
| 593 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 594 | |
| 595 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 596 | // The expression is either a property or a variable proxy that rewrites |
| 597 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 598 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 599 | // We use a named reference if the key is a literal symbol, unless it is |
| 600 | // a string that can be legally parsed as an integer. This is because |
| 601 | // otherwise we will not get into the slow case code that handles [] on |
| 602 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | Literal* literal = property->key()->AsLiteral(); |
| 604 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 605 | if (literal != NULL && |
| 606 | literal->handle()->IsSymbol() && |
| 607 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 608 | ref->set_type(Reference::NAMED); |
| 609 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 610 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 611 | ref->set_type(Reference::KEYED); |
| 612 | } |
| 613 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 614 | // The expression is a variable proxy that does not rewrite to a |
| 615 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 617 | LoadGlobal(); |
| 618 | ref->set_type(Reference::NAMED); |
| 619 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 620 | ASSERT(var->slot() != NULL); |
| 621 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 622 | } |
| 623 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 624 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 625 | LoadAndSpill(e); |
| 626 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
| 630 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 631 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 632 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 633 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 634 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 635 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 636 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 637 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 638 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 639 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 644 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 645 | // register to a boolean in the condition code register. The code |
| 646 | // 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] | 647 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 648 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 649 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 650 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 651 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 652 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 653 | |
| 654 | // Fast case checks |
| 655 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 656 | // Check if the value is 'false'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 657 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 658 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 659 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 660 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 661 | // Check if the value is 'true'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 662 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 663 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 664 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 665 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 666 | // Check if the value is 'undefined'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 667 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 668 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 669 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 671 | // Check if the value is a smi. |
| 672 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 673 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 674 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 675 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 676 | |
| 677 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 678 | frame_->EmitPush(r0); |
| 679 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 680 | // Convert the result (r0) to a condition code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 681 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 682 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 683 | |
| 684 | cc_reg_ = ne; |
| 685 | } |
| 686 | |
| 687 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 688 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 689 | OverwriteMode overwrite_mode, |
| 690 | int constant_rhs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 691 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 692 | // sp[0] : y |
| 693 | // sp[1] : x |
| 694 | // result : r0 |
| 695 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 696 | // Stub is entered with a call: 'return address' is in lr. |
| 697 | switch (op) { |
| 698 | case Token::ADD: // fall through. |
| 699 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 700 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 701 | case Token::DIV: |
| 702 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 703 | case Token::BIT_OR: |
| 704 | case Token::BIT_AND: |
| 705 | case Token::BIT_XOR: |
| 706 | case Token::SHL: |
| 707 | case Token::SHR: |
| 708 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 709 | frame_->EmitPop(r0); // r0 : y |
| 710 | frame_->EmitPop(r1); // r1 : x |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 711 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 712 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 716 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 717 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 718 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 719 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 720 | break; |
| 721 | |
| 722 | default: |
| 723 | // Other cases should have been handled before this point. |
| 724 | UNREACHABLE(); |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 730 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 731 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 732 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 733 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 734 | bool reversed, |
| 735 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 736 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 737 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 738 | reversed_(reversed), |
| 739 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 740 | set_comment("[ DeferredInlinedSmiOperation"); |
| 741 | } |
| 742 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 743 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 744 | |
| 745 | private: |
| 746 | Token::Value op_; |
| 747 | int value_; |
| 748 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 749 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 750 | }; |
| 751 | |
| 752 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 753 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 754 | switch (op_) { |
| 755 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 756 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 757 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 758 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 759 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 760 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 761 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 762 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 763 | } |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 768 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 769 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 770 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 771 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 772 | } else { |
| 773 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 774 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 775 | } |
| 776 | break; |
| 777 | } |
| 778 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 779 | // For these operations there is no optimistic operation that needs to be |
| 780 | // reverted. |
| 781 | case Token::MUL: |
| 782 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 783 | case Token::BIT_OR: |
| 784 | case Token::BIT_XOR: |
| 785 | case Token::BIT_AND: { |
| 786 | if (reversed_) { |
| 787 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 788 | } else { |
| 789 | __ mov(r1, Operand(r0)); |
| 790 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 791 | } |
| 792 | break; |
| 793 | } |
| 794 | |
| 795 | case Token::SHL: |
| 796 | case Token::SHR: |
| 797 | case Token::SAR: { |
| 798 | if (!reversed_) { |
| 799 | __ mov(r1, Operand(r0)); |
| 800 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 801 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 802 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 803 | } |
| 804 | break; |
| 805 | } |
| 806 | |
| 807 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 808 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 809 | UNREACHABLE(); |
| 810 | break; |
| 811 | } |
| 812 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 813 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 814 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 818 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 819 | x &= x - 1; |
| 820 | return (x & (x - 1)) == 0; |
| 821 | } |
| 822 | |
| 823 | |
| 824 | // Returns the index of the lowest bit set. |
| 825 | static int BitPosition(unsigned x) { |
| 826 | int bit_posn = 0; |
| 827 | while ((x & 0xf) == 0) { |
| 828 | bit_posn += 4; |
| 829 | x >>= 4; |
| 830 | } |
| 831 | while ((x & 1) == 0) { |
| 832 | bit_posn++; |
| 833 | x >>= 1; |
| 834 | } |
| 835 | return bit_posn; |
| 836 | } |
| 837 | |
| 838 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 839 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 840 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 841 | bool reversed, |
| 842 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 843 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 844 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 845 | // some possible smi operations (like + and -) when (at least) one |
| 846 | // of the operands is a literal smi. With this optimization, the |
| 847 | // performance of the system is increased by ~15%, and the generated |
| 848 | // code size is increased by ~1% (measured on a combination of |
| 849 | // different benchmarks). |
| 850 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 851 | // sp[0] : operand |
| 852 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 853 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 854 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 855 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 856 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 858 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 859 | switch (op) { |
| 860 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 861 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 862 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 863 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 864 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 865 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 866 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 867 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 868 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 869 | break; |
| 870 | } |
| 871 | |
| 872 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 873 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 874 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 875 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 876 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 877 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 878 | } else { |
| 879 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 880 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 881 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 882 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 883 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 884 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 885 | break; |
| 886 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 887 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 888 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 889 | case Token::BIT_OR: |
| 890 | case Token::BIT_XOR: |
| 891 | case Token::BIT_AND: { |
| 892 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 893 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 894 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 895 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 896 | switch (op) { |
| 897 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 898 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 899 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 900 | default: UNREACHABLE(); |
| 901 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 902 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 903 | break; |
| 904 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 905 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 906 | case Token::SHL: |
| 907 | case Token::SHR: |
| 908 | case Token::SAR: { |
| 909 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 910 | something_to_inline = false; |
| 911 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 912 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 913 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 914 | DeferredCode* deferred = |
| 915 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
| 916 | __ tst(r0, Operand(kSmiTagMask)); |
| 917 | deferred->Branch(ne); |
| 918 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 919 | switch (op) { |
| 920 | case Token::SHL: { |
| 921 | if (shift_value != 0) { |
| 922 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 923 | } |
| 924 | // check that the *unsigned* result fits in a smi |
| 925 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 926 | deferred->Branch(mi); |
| 927 | break; |
| 928 | } |
| 929 | case Token::SHR: { |
| 930 | // LSR by immediate 0 means shifting 32 bits. |
| 931 | if (shift_value != 0) { |
| 932 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 933 | } |
| 934 | // check that the *unsigned* result fits in a smi |
| 935 | // neither of the two high-order bits can be set: |
| 936 | // - 0x80000000: high bit would be lost when smi tagging |
| 937 | // - 0x40000000: this number would convert to negative when |
| 938 | // smi tagging these two cases can only happen with shifts |
| 939 | // by 0 or 1 when handed a valid smi |
| 940 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 941 | deferred->Branch(ne); |
| 942 | break; |
| 943 | } |
| 944 | case Token::SAR: { |
| 945 | if (shift_value != 0) { |
| 946 | // ASR by immediate 0 means shifting 32 bits. |
| 947 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 948 | } |
| 949 | break; |
| 950 | } |
| 951 | default: UNREACHABLE(); |
| 952 | } |
| 953 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 954 | deferred->BindExit(); |
| 955 | break; |
| 956 | } |
| 957 | |
| 958 | case Token::MOD: { |
| 959 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 960 | something_to_inline = false; |
| 961 | break; |
| 962 | } |
| 963 | DeferredCode* deferred = |
| 964 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 965 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 966 | __ tst(r0, Operand(mask)); |
| 967 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 968 | mask = (int_value << kSmiTagSize) - 1; |
| 969 | __ and_(r0, r0, Operand(mask)); |
| 970 | deferred->BindExit(); |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | case Token::MUL: { |
| 975 | if (!IsEasyToMultiplyBy(int_value)) { |
| 976 | something_to_inline = false; |
| 977 | break; |
| 978 | } |
| 979 | DeferredCode* deferred = |
| 980 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 981 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 982 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 983 | unsigned mask = 0x80000000u; |
| 984 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 985 | mask |= mask >> 1; |
| 986 | } |
| 987 | mask |= kSmiTagMask; |
| 988 | // This does a single mask that checks for a too high value in a |
| 989 | // conservative way and for a non-Smi. It also filters out negative |
| 990 | // numbers, unfortunately, but since this code is inline we prefer |
| 991 | // brevity to comprehensiveness. |
| 992 | __ tst(r0, Operand(mask)); |
| 993 | deferred->Branch(ne); |
| 994 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 995 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 996 | break; |
| 997 | } |
| 998 | |
| 999 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1000 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1001 | break; |
| 1002 | } |
| 1003 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1004 | if (!something_to_inline) { |
| 1005 | if (!reversed) { |
| 1006 | frame_->EmitPush(r0); |
| 1007 | __ mov(r0, Operand(value)); |
| 1008 | frame_->EmitPush(r0); |
| 1009 | GenericBinaryOperation(op, mode, int_value); |
| 1010 | } else { |
| 1011 | __ mov(ip, Operand(value)); |
| 1012 | frame_->EmitPush(ip); |
| 1013 | frame_->EmitPush(r0); |
| 1014 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1015 | } |
| 1016 | } |
| 1017 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1018 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1022 | void CodeGenerator::Comparison(Condition cc, |
| 1023 | Expression* left, |
| 1024 | Expression* right, |
| 1025 | bool strict) { |
| 1026 | if (left != NULL) LoadAndSpill(left); |
| 1027 | if (right != NULL) LoadAndSpill(right); |
| 1028 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1029 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1030 | // sp[0] : y |
| 1031 | // sp[1] : x |
| 1032 | // result : cc register |
| 1033 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1034 | // Strict only makes sense for equality comparisons. |
| 1035 | ASSERT(!strict || cc == eq); |
| 1036 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1037 | JumpTarget exit; |
| 1038 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1039 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1040 | if (cc == gt || cc == le) { |
| 1041 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1042 | frame_->EmitPop(r1); |
| 1043 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1044 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1045 | frame_->EmitPop(r0); |
| 1046 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1047 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1048 | __ orr(r2, r0, Operand(r1)); |
| 1049 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1050 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1051 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1052 | // Perform non-smi comparison by stub. |
| 1053 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1054 | // We call with 0 args because there are 0 on the stack. |
| 1055 | CompareStub stub(cc, strict); |
| 1056 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1057 | __ cmp(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1058 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1059 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1060 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1061 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1062 | __ cmp(r1, Operand(r0)); |
| 1063 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1064 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1065 | cc_reg_ = cc; |
| 1066 | } |
| 1067 | |
| 1068 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1069 | class CallFunctionStub: public CodeStub { |
| 1070 | public: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1071 | CallFunctionStub(int argc, InLoopFlag in_loop) |
| 1072 | : argc_(argc), in_loop_(in_loop) {} |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1073 | |
| 1074 | void Generate(MacroAssembler* masm); |
| 1075 | |
| 1076 | private: |
| 1077 | int argc_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1078 | InLoopFlag in_loop_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1079 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1080 | #if defined(DEBUG) |
| 1081 | void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); } |
| 1082 | #endif // defined(DEBUG) |
| 1083 | |
| 1084 | Major MajorKey() { return CallFunction; } |
| 1085 | int MinorKey() { return argc_; } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1086 | InLoopFlag InLoop() { return in_loop_; } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1087 | }; |
| 1088 | |
| 1089 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1090 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1091 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1092 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1093 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1094 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1095 | int arg_count = args->length(); |
| 1096 | for (int i = 0; i < arg_count; i++) { |
| 1097 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1098 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1099 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1100 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1101 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1102 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1103 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1104 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 1105 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1106 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1109 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1110 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1114 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1115 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1116 | ASSERT(has_cc()); |
| 1117 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1118 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1119 | cc_reg_ = al; |
| 1120 | } |
| 1121 | |
| 1122 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1123 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1124 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 1125 | Comment cmnt(masm_, "[ check stack"); |
| 1126 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1127 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1128 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1129 | // gives a return address 12 bytes down. |
| 1130 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1131 | masm_->cmp(sp, Operand(ip)); |
| 1132 | StackCheckStub stub; |
| 1133 | // Call the stub if lower. |
| 1134 | masm_->mov(pc, |
| 1135 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1136 | RelocInfo::CODE_TARGET), |
| 1137 | LeaveCC, |
| 1138 | lo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1142 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1143 | #ifdef DEBUG |
| 1144 | int original_height = frame_->height(); |
| 1145 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1146 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1147 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1148 | VisitAndSpill(statements->at(i)); |
| 1149 | } |
| 1150 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1151 | } |
| 1152 | |
| 1153 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1154 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1155 | #ifdef DEBUG |
| 1156 | int original_height = frame_->height(); |
| 1157 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1158 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1159 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1160 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1161 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1162 | VisitStatementsAndSpill(node->statements()); |
| 1163 | if (node->break_target()->is_linked()) { |
| 1164 | node->break_target()->Bind(); |
| 1165 | } |
| 1166 | node->break_target()->Unuse(); |
| 1167 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1171 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1172 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 1173 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1174 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1175 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1176 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1177 | frame_->EmitPush(r0); |
| 1178 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1179 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1183 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1184 | #ifdef DEBUG |
| 1185 | int original_height = frame_->height(); |
| 1186 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1187 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1188 | Comment cmnt(masm_, "[ Declaration"); |
| 1189 | Variable* var = node->proxy()->var(); |
| 1190 | ASSERT(var != NULL); // must have been resolved |
| 1191 | Slot* slot = var->slot(); |
| 1192 | |
| 1193 | // If it was not possible to allocate the variable at compile time, |
| 1194 | // we need to "declare" it at runtime to make sure it actually |
| 1195 | // exists in the local context. |
| 1196 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1197 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1198 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1199 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1200 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1201 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1202 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1203 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1204 | // Declaration nodes are always declared in only two modes. |
| 1205 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1206 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1207 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1208 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1209 | // Push initial value, if any. |
| 1210 | // Note: For variables we must not push an initial value (such as |
| 1211 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1212 | // must not destroy the current value. |
| 1213 | if (node->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1214 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1215 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1216 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1217 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1218 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1219 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1220 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1222 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1223 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1224 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1225 | return; |
| 1226 | } |
| 1227 | |
| 1228 | ASSERT(!var->is_global()); |
| 1229 | |
| 1230 | // If we have a function or a constant, we need to initialize the variable. |
| 1231 | Expression* val = NULL; |
| 1232 | if (node->mode() == Variable::CONST) { |
| 1233 | val = new Literal(Factory::the_hole_value()); |
| 1234 | } else { |
| 1235 | val = node->fun(); // NULL if we don't have a function |
| 1236 | } |
| 1237 | |
| 1238 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1239 | { |
| 1240 | // Set initial value. |
| 1241 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1242 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1243 | target.SetValue(NOT_CONST_INIT); |
| 1244 | // The reference is removed from the stack (preserving TOS) when |
| 1245 | // it goes out of scope. |
| 1246 | } |
| 1247 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1248 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1249 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1250 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1254 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1255 | #ifdef DEBUG |
| 1256 | int original_height = frame_->height(); |
| 1257 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1258 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1259 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1260 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1261 | Expression* expression = node->expression(); |
| 1262 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1263 | LoadAndSpill(expression); |
| 1264 | frame_->Drop(); |
| 1265 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1269 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1270 | #ifdef DEBUG |
| 1271 | int original_height = frame_->height(); |
| 1272 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1273 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1274 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1275 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1276 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1277 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1281 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1282 | #ifdef DEBUG |
| 1283 | int original_height = frame_->height(); |
| 1284 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1285 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1286 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1287 | // Generate different code depending on which parts of the if statement |
| 1288 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1289 | bool has_then_stm = node->HasThenStatement(); |
| 1290 | bool has_else_stm = node->HasElseStatement(); |
| 1291 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1292 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1294 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1296 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1297 | JumpTarget then; |
| 1298 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1299 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1300 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1301 | &then, &else_, true); |
| 1302 | if (frame_ != NULL) { |
| 1303 | Branch(false, &else_); |
| 1304 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1305 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1306 | if (frame_ != NULL || then.is_linked()) { |
| 1307 | then.Bind(); |
| 1308 | VisitAndSpill(node->then_statement()); |
| 1309 | } |
| 1310 | if (frame_ != NULL) { |
| 1311 | exit.Jump(); |
| 1312 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1313 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1314 | if (else_.is_linked()) { |
| 1315 | else_.Bind(); |
| 1316 | VisitAndSpill(node->else_statement()); |
| 1317 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1318 | |
| 1319 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1320 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1321 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1322 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1323 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1324 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1325 | &then, &exit, true); |
| 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) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1340 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1341 | &exit, &else_, true); |
| 1342 | if (frame_ != NULL) { |
| 1343 | Branch(true, &exit); |
| 1344 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1345 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1346 | if (frame_ != NULL || else_.is_linked()) { |
| 1347 | else_.Bind(); |
| 1348 | VisitAndSpill(node->else_statement()); |
| 1349 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1350 | |
| 1351 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1352 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1353 | ASSERT(!has_then_stm && !has_else_stm); |
| 1354 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1355 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1356 | &exit, &exit, false); |
| 1357 | if (frame_ != NULL) { |
| 1358 | if (has_cc()) { |
| 1359 | cc_reg_ = al; |
| 1360 | } else { |
| 1361 | frame_->Drop(); |
| 1362 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1367 | if (exit.is_linked()) { |
| 1368 | exit.Bind(); |
| 1369 | } |
| 1370 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1374 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1375 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1376 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1377 | CodeForStatementPosition(node); |
| 1378 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1382 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1383 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1384 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1385 | CodeForStatementPosition(node); |
| 1386 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1390 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1391 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1393 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1394 | CodeForStatementPosition(node); |
| 1395 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1396 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1397 | frame_->EmitPop(r0); |
| 1398 | function_return_.Jump(); |
| 1399 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1400 | // Pop the result from the frame and prepare the frame for |
| 1401 | // returning thus making it easier to merge. |
| 1402 | frame_->EmitPop(r0); |
| 1403 | frame_->PrepareForReturn(); |
| 1404 | |
| 1405 | function_return_.Jump(); |
| 1406 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1410 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1411 | #ifdef DEBUG |
| 1412 | int original_height = frame_->height(); |
| 1413 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1414 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1415 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1416 | CodeForStatementPosition(node); |
| 1417 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1418 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1419 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1420 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1421 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1422 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1423 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1424 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1425 | __ cmp(r0, Operand(cp)); |
| 1426 | verified_true.Branch(eq); |
| 1427 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1428 | verified_true.Bind(); |
| 1429 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1430 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1431 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1432 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1436 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1437 | #ifdef DEBUG |
| 1438 | int original_height = frame_->height(); |
| 1439 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1440 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1441 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1442 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1443 | // Pop context. |
| 1444 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1445 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1446 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1447 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1451 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1452 | #ifdef DEBUG |
| 1453 | int original_height = frame_->height(); |
| 1454 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1455 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1456 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1457 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1458 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1459 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1460 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1461 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1462 | JumpTarget next_test; |
| 1463 | JumpTarget fall_through; |
| 1464 | JumpTarget default_entry; |
| 1465 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1466 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1467 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1468 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1469 | |
| 1470 | for (int i = 0; i < length; i++) { |
| 1471 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1472 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1473 | // Remember the default clause and compile it at the end. |
| 1474 | default_clause = clause; |
| 1475 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1478 | Comment cmnt(masm_, "[ Case clause"); |
| 1479 | // Compile the test. |
| 1480 | next_test.Bind(); |
| 1481 | next_test.Unuse(); |
| 1482 | // Duplicate TOS. |
| 1483 | __ ldr(r0, frame_->Top()); |
| 1484 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1485 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1486 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1487 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1488 | // Before entering the body from the test, remove the switch value from |
| 1489 | // the stack. |
| 1490 | frame_->Drop(); |
| 1491 | |
| 1492 | // Label the body so that fall through is enabled. |
| 1493 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1494 | default_exit.Bind(); |
| 1495 | } else { |
| 1496 | fall_through.Bind(); |
| 1497 | fall_through.Unuse(); |
| 1498 | } |
| 1499 | VisitStatementsAndSpill(clause->statements()); |
| 1500 | |
| 1501 | // If control flow can fall through from the body, jump to the next body |
| 1502 | // or the end of the statement. |
| 1503 | if (frame_ != NULL) { |
| 1504 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1505 | default_entry.Jump(); |
| 1506 | } else { |
| 1507 | fall_through.Jump(); |
| 1508 | } |
| 1509 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1512 | // The final "test" removes the switch value. |
| 1513 | next_test.Bind(); |
| 1514 | frame_->Drop(); |
| 1515 | |
| 1516 | // If there is a default clause, compile it. |
| 1517 | if (default_clause != NULL) { |
| 1518 | Comment cmnt(masm_, "[ Default clause"); |
| 1519 | default_entry.Bind(); |
| 1520 | VisitStatementsAndSpill(default_clause->statements()); |
| 1521 | // If control flow can fall out of the default and there is a case after |
| 1522 | // it, jup to that case's body. |
| 1523 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1524 | default_exit.Jump(); |
| 1525 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1526 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1527 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1528 | if (fall_through.is_linked()) { |
| 1529 | fall_through.Bind(); |
| 1530 | } |
| 1531 | |
| 1532 | if (node->break_target()->is_linked()) { |
| 1533 | node->break_target()->Bind(); |
| 1534 | } |
| 1535 | node->break_target()->Unuse(); |
| 1536 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1540 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1541 | #ifdef DEBUG |
| 1542 | int original_height = frame_->height(); |
| 1543 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1544 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1545 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1546 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1547 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1548 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1549 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1550 | // Label the top of the loop for the backward CFG edge. If the test |
| 1551 | // is always true we can use the continue target, and if the test is |
| 1552 | // always false there is no need. |
| 1553 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1554 | switch (info) { |
| 1555 | case ALWAYS_TRUE: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1556 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1557 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1558 | break; |
| 1559 | case ALWAYS_FALSE: |
| 1560 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1561 | break; |
| 1562 | case DONT_KNOW: |
| 1563 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1564 | body.Bind(); |
| 1565 | break; |
| 1566 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1567 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1568 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1569 | VisitAndSpill(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1570 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1571 | // Compile the test. |
| 1572 | switch (info) { |
| 1573 | case ALWAYS_TRUE: |
| 1574 | // If control can fall off the end of the body, jump back to the |
| 1575 | // top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1576 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1577 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1578 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1579 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1580 | case ALWAYS_FALSE: |
| 1581 | // If we have a continue in the body, we only have to bind its |
| 1582 | // jump target. |
| 1583 | if (node->continue_target()->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1584 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1585 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1586 | break; |
| 1587 | case DONT_KNOW: |
| 1588 | // We have to compile the test expression if it can be reached by |
| 1589 | // control flow falling out of the body or via continue. |
| 1590 | if (node->continue_target()->is_linked()) { |
| 1591 | node->continue_target()->Bind(); |
| 1592 | } |
| 1593 | if (has_valid_frame()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1594 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1595 | &body, node->break_target(), true); |
| 1596 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1597 | // A invalid frame here indicates that control did not |
| 1598 | // fall out of the test expression. |
| 1599 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1600 | } |
| 1601 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1602 | break; |
| 1603 | } |
| 1604 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1605 | if (node->break_target()->is_linked()) { |
| 1606 | node->break_target()->Bind(); |
| 1607 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1608 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1609 | } |
| 1610 | |
| 1611 | |
| 1612 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 1613 | #ifdef DEBUG |
| 1614 | int original_height = frame_->height(); |
| 1615 | #endif |
| 1616 | VirtualFrame::SpilledScope spilled_scope; |
| 1617 | Comment cmnt(masm_, "[ WhileStatement"); |
| 1618 | CodeForStatementPosition(node); |
| 1619 | |
| 1620 | // If the test is never true and has no side effects there is no need |
| 1621 | // to compile the test or body. |
| 1622 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1623 | if (info == ALWAYS_FALSE) return; |
| 1624 | |
| 1625 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1626 | |
| 1627 | // Label the top of the loop with the continue target for the backward |
| 1628 | // CFG edge. |
| 1629 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1630 | node->continue_target()->Bind(); |
| 1631 | |
| 1632 | if (info == DONT_KNOW) { |
| 1633 | JumpTarget body; |
| 1634 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1635 | &body, node->break_target(), true); |
| 1636 | if (has_valid_frame()) { |
| 1637 | // A NULL frame indicates that control did not fall out of the |
| 1638 | // test expression. |
| 1639 | Branch(false, node->break_target()); |
| 1640 | } |
| 1641 | if (has_valid_frame() || body.is_linked()) { |
| 1642 | body.Bind(); |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | if (has_valid_frame()) { |
| 1647 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1648 | VisitAndSpill(node->body()); |
| 1649 | |
| 1650 | // If control flow can fall out of the body, jump back to the top. |
| 1651 | if (has_valid_frame()) { |
| 1652 | node->continue_target()->Jump(); |
| 1653 | } |
| 1654 | } |
| 1655 | if (node->break_target()->is_linked()) { |
| 1656 | node->break_target()->Bind(); |
| 1657 | } |
| 1658 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1659 | } |
| 1660 | |
| 1661 | |
| 1662 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 1663 | #ifdef DEBUG |
| 1664 | int original_height = frame_->height(); |
| 1665 | #endif |
| 1666 | VirtualFrame::SpilledScope spilled_scope; |
| 1667 | Comment cmnt(masm_, "[ ForStatement"); |
| 1668 | CodeForStatementPosition(node); |
| 1669 | if (node->init() != NULL) { |
| 1670 | VisitAndSpill(node->init()); |
| 1671 | } |
| 1672 | |
| 1673 | // If the test is never true there is no need to compile the test or |
| 1674 | // body. |
| 1675 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1676 | if (info == ALWAYS_FALSE) return; |
| 1677 | |
| 1678 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1679 | |
| 1680 | // If there is no update statement, label the top of the loop with the |
| 1681 | // continue target, otherwise with the loop target. |
| 1682 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1683 | if (node->next() == NULL) { |
| 1684 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1685 | node->continue_target()->Bind(); |
| 1686 | } else { |
| 1687 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1688 | loop.Bind(); |
| 1689 | } |
| 1690 | |
| 1691 | // If the test is always true, there is no need to compile it. |
| 1692 | if (info == DONT_KNOW) { |
| 1693 | JumpTarget body; |
| 1694 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1695 | &body, node->break_target(), true); |
| 1696 | if (has_valid_frame()) { |
| 1697 | Branch(false, node->break_target()); |
| 1698 | } |
| 1699 | if (has_valid_frame() || body.is_linked()) { |
| 1700 | body.Bind(); |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | if (has_valid_frame()) { |
| 1705 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1706 | VisitAndSpill(node->body()); |
| 1707 | |
| 1708 | if (node->next() == NULL) { |
| 1709 | // If there is no update statement and control flow can fall out |
| 1710 | // of the loop, jump directly to the continue label. |
| 1711 | if (has_valid_frame()) { |
| 1712 | node->continue_target()->Jump(); |
| 1713 | } |
| 1714 | } else { |
| 1715 | // If there is an update statement and control flow can reach it |
| 1716 | // via falling out of the body of the loop or continuing, we |
| 1717 | // compile the update statement. |
| 1718 | if (node->continue_target()->is_linked()) { |
| 1719 | node->continue_target()->Bind(); |
| 1720 | } |
| 1721 | if (has_valid_frame()) { |
| 1722 | // Record source position of the statement as this code which is |
| 1723 | // after the code for the body actually belongs to the loop |
| 1724 | // statement and not the body. |
| 1725 | CodeForStatementPosition(node); |
| 1726 | VisitAndSpill(node->next()); |
| 1727 | loop.Jump(); |
| 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | if (node->break_target()->is_linked()) { |
| 1732 | node->break_target()->Bind(); |
| 1733 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1734 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1738 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1739 | #ifdef DEBUG |
| 1740 | int original_height = frame_->height(); |
| 1741 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1742 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1743 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1744 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1745 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1746 | JumpTarget primitive; |
| 1747 | JumpTarget jsobject; |
| 1748 | JumpTarget fixed_array; |
| 1749 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1750 | JumpTarget end_del_check; |
| 1751 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1752 | |
| 1753 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1754 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1755 | |
| 1756 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1757 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1758 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1759 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 1760 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1761 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1762 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1763 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1764 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1765 | |
| 1766 | // Stack layout in body: |
| 1767 | // [iteration counter (Smi)] |
| 1768 | // [length of array] |
| 1769 | // [FixedArray] |
| 1770 | // [Map or 0] |
| 1771 | // [Object] |
| 1772 | |
| 1773 | // Check if enumerable is already a JSObject |
| 1774 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1775 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1776 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1777 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1778 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1779 | primitive.Bind(); |
| 1780 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1781 | Result arg_count(r0); |
| 1782 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1783 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1784 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1785 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1786 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1787 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1788 | frame_->EmitPush(r0); |
| 1789 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1790 | |
| 1791 | // If we got a Map, we can do a fast modification check. |
| 1792 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1793 | __ mov(r2, Operand(r0)); |
| 1794 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1795 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 1796 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1797 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1798 | |
| 1799 | // Get enum cache |
| 1800 | __ mov(r1, Operand(r0)); |
| 1801 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1802 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1803 | __ ldr(r2, |
| 1804 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1805 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1806 | frame_->EmitPush(r0); // map |
| 1807 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1808 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1809 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1810 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1811 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1812 | frame_->EmitPush(r0); |
| 1813 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1814 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1815 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1816 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1817 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1818 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1819 | |
| 1820 | // 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] | 1821 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1822 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1823 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1824 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1825 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1826 | |
| 1827 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1828 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1829 | // sp[0] : index |
| 1830 | // sp[1] : array/enum cache length |
| 1831 | // sp[2] : array or enum cache |
| 1832 | // sp[3] : 0 or map |
| 1833 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1834 | // Grab the current frame's height for the break and continue |
| 1835 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1836 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1837 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1838 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1839 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1840 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1841 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1842 | node->break_target()->Branch(hs); |
| 1843 | |
| 1844 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1845 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1846 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1847 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1848 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1849 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1850 | |
| 1851 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1852 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1853 | // Check if this (still) matches the map of the enumerable. |
| 1854 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1855 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1856 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1857 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1858 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | |
| 1860 | // 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] | 1861 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1862 | frame_->EmitPush(r0); |
| 1863 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1864 | Result arg_count_reg(r0); |
| 1865 | __ mov(r0, Operand(1)); |
| 1866 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, &arg_count_reg, 2); |
| 1867 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | |
| 1869 | // 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] | 1870 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1871 | __ cmp(r3, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1872 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1873 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1874 | end_del_check.Bind(); |
| 1875 | // Store the entry in the 'each' expression and take another spin in the |
| 1876 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1877 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1878 | { Reference each(this, node->each()); |
| 1879 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1880 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1881 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1882 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1883 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1884 | // If the reference was to a slot we rely on the convenient property |
| 1885 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1886 | // right on top of or right underneath a zero-sized reference. |
| 1887 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1888 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1889 | // It's safe to pop the value lying on top of the reference before |
| 1890 | // unloading the reference itself (which preserves the top of stack, |
| 1891 | // ie, now the topmost value of the non-zero sized reference), since |
| 1892 | // we will discard the top of stack after unloading the reference |
| 1893 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1894 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1895 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1896 | } |
| 1897 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1898 | // Discard the i'th entry pushed above or else the remainder of the |
| 1899 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1900 | frame_->Drop(); |
| 1901 | |
| 1902 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1903 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1904 | VisitAndSpill(node->body()); |
| 1905 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1906 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1907 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1908 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1909 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1910 | frame_->EmitPop(r0); |
| 1911 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1912 | frame_->EmitPush(r0); |
| 1913 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1914 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1915 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1916 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1917 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1918 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1919 | |
| 1920 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1921 | exit.Bind(); |
| 1922 | node->continue_target()->Unuse(); |
| 1923 | node->break_target()->Unuse(); |
| 1924 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1928 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1929 | #ifdef DEBUG |
| 1930 | int original_height = frame_->height(); |
| 1931 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1932 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1933 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1934 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1935 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1936 | JumpTarget try_block; |
| 1937 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1938 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1939 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1940 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1941 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1942 | |
| 1943 | // Store the caught exception in the catch variable. |
| 1944 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1945 | ASSERT(ref.is_slot()); |
| 1946 | // Here we make use of the convenient property that it doesn't matter |
| 1947 | // whether a value is immediately on top of or underneath a zero-sized |
| 1948 | // reference. |
| 1949 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1953 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1954 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1955 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 1956 | if (frame_ != NULL) { |
| 1957 | exit.Jump(); |
| 1958 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1959 | |
| 1960 | |
| 1961 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1962 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1963 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1964 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 1965 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1966 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1967 | // Shadow the labels for all escapes from the try block, including |
| 1968 | // returns. During shadowing, the original label is hidden as the |
| 1969 | // LabelShadow and operations on the original actually affect the |
| 1970 | // shadowing label. |
| 1971 | // |
| 1972 | // We should probably try to unify the escaping labels and the return |
| 1973 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1974 | int nof_escapes = node->escaping_targets()->length(); |
| 1975 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 1976 | |
| 1977 | // Add the shadow target for the function return. |
| 1978 | static const int kReturnShadowIndex = 0; |
| 1979 | shadows.Add(new ShadowTarget(&function_return_)); |
| 1980 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 1981 | function_return_is_shadowed_ = true; |
| 1982 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 1983 | |
| 1984 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1985 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1986 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1990 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1991 | |
| 1992 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1993 | // After shadowing stops, the original labels are unshadowed and the |
| 1994 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1995 | bool has_unlinks = false; |
| 1996 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1997 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1998 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 1999 | } |
| 2000 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2001 | |
| 2002 | // Get an external reference to the handler address. |
| 2003 | ExternalReference handler_address(Top::k_handler_address); |
| 2004 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2005 | // If we can fall off the end of the try block, unlink from try chain. |
| 2006 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2007 | // The next handler address is on top of the frame. Unlink from |
| 2008 | // the handler list and drop the rest of this handler from the |
| 2009 | // frame. |
| 2010 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2011 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2012 | __ mov(r3, Operand(handler_address)); |
| 2013 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2014 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2015 | if (has_unlinks) { |
| 2016 | exit.Jump(); |
| 2017 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2020 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2021 | // jumped to. Deallocate each shadow target. |
| 2022 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2023 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2024 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2025 | shadows[i]->Bind(); |
| 2026 | // Because we can be jumping here (to spilled code) from unspilled |
| 2027 | // code, we need to reestablish a spilled frame at this block. |
| 2028 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2029 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2030 | // Reload sp from the top handler, because some statements that we |
| 2031 | // 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] | 2032 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2033 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2034 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2035 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2036 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2037 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2038 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2039 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2040 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2041 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2042 | frame_->PrepareForReturn(); |
| 2043 | } |
| 2044 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2048 | exit.Bind(); |
| 2049 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2053 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2054 | #ifdef DEBUG |
| 2055 | int original_height = frame_->height(); |
| 2056 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2057 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2058 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2059 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2060 | |
| 2061 | // State: Used to keep track of reason for entering the finally |
| 2062 | // block. Should probably be extended to hold information for |
| 2063 | // break/continue from within the try block. |
| 2064 | enum { FALLING, THROWING, JUMPING }; |
| 2065 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2066 | JumpTarget try_block; |
| 2067 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2068 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2069 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2070 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2071 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2072 | // In case of thrown exceptions, this is where we continue. |
| 2073 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2074 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2075 | |
| 2076 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2077 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2078 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2079 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2080 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2081 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2082 | // Shadow the labels for all escapes from the try block, including |
| 2083 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2084 | // operations on the original actually affect the shadowing label. |
| 2085 | // |
| 2086 | // We should probably try to unify the escaping labels and the return |
| 2087 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2088 | int nof_escapes = node->escaping_targets()->length(); |
| 2089 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2090 | |
| 2091 | // Add the shadow target for the function return. |
| 2092 | static const int kReturnShadowIndex = 0; |
| 2093 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2094 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2095 | function_return_is_shadowed_ = true; |
| 2096 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2097 | |
| 2098 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2099 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2100 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2104 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2105 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2106 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2107 | // After shadowing stops, the original labels are unshadowed and the |
| 2108 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2109 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2110 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2111 | shadows[i]->StopShadowing(); |
| 2112 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2113 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2114 | function_return_is_shadowed_ = function_return_was_shadowed; |
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 | // Get an external reference to the handler address. |
| 2117 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2118 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2119 | // If we can fall off the end of the try block, unlink from the try |
| 2120 | // chain and set the state on the frame to FALLING. |
| 2121 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2122 | // The next handler address is on top of the frame. |
| 2123 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2124 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2125 | __ mov(r3, Operand(handler_address)); |
| 2126 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2127 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2128 | |
| 2129 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2130 | // state in r2, then jump around the unlink blocks if any. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2131 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2132 | frame_->EmitPush(r0); |
| 2133 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2134 | if (nof_unlinks > 0) { |
| 2135 | finally_block.Jump(); |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | // Generate code to unlink and set the state for the (formerly) |
| 2140 | // shadowing targets that have been jumped to. |
| 2141 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2142 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2143 | // If we have come from the shadowed return, the return value is |
| 2144 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2145 | // until it is pushed. |
| 2146 | // |
| 2147 | // Because we can be jumping here (to spilled code) from |
| 2148 | // unspilled code, we need to reestablish a spilled frame at |
| 2149 | // this block. |
| 2150 | shadows[i]->Bind(); |
| 2151 | frame_->SpillAll(); |
| 2152 | |
| 2153 | // Reload sp from the top handler, because some statements that |
| 2154 | // we break from (eg, for...in) may have left stuff on the |
| 2155 | // stack. |
| 2156 | __ mov(r3, Operand(handler_address)); |
| 2157 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2158 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2159 | |
| 2160 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2161 | // handler address is currently on top of the frame. |
| 2162 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2163 | frame_->EmitPop(r1); |
| 2164 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2165 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2166 | |
| 2167 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2168 | // If this label shadowed the function return, materialize the |
| 2169 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2170 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2171 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2172 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2173 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2174 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2175 | } |
| 2176 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2177 | if (--nof_unlinks > 0) { |
| 2178 | // If this is not the last unlink block, jump around the next. |
| 2179 | finally_block.Jump(); |
| 2180 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2184 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2185 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2186 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2187 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2188 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2189 | |
| 2190 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2191 | // and the state - while evaluating the finally block. |
| 2192 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2193 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2194 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2195 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2196 | if (has_valid_frame()) { |
| 2197 | // Restore state and return value or faked TOS. |
| 2198 | frame_->EmitPop(r2); |
| 2199 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2202 | // Generate code to jump to the right destination for all used |
| 2203 | // formerly shadowing targets. Deallocate each shadow target. |
| 2204 | for (int i = 0; i < shadows.length(); i++) { |
| 2205 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2206 | JumpTarget* original = shadows[i]->other_target(); |
| 2207 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2208 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2209 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2210 | skip.Branch(ne); |
| 2211 | frame_->PrepareForReturn(); |
| 2212 | original->Jump(); |
| 2213 | skip.Bind(); |
| 2214 | } else { |
| 2215 | original->Branch(eq); |
| 2216 | } |
| 2217 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2218 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2219 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2220 | if (has_valid_frame()) { |
| 2221 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2222 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2223 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2224 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2225 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2226 | // Rethrow exception. |
| 2227 | frame_->EmitPush(r0); |
| 2228 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2229 | |
| 2230 | // Done. |
| 2231 | exit.Bind(); |
| 2232 | } |
| 2233 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2237 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2238 | #ifdef DEBUG |
| 2239 | int original_height = frame_->height(); |
| 2240 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2241 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2242 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2243 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2244 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2245 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2246 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2247 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2248 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2252 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2253 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2254 | ASSERT(boilerplate->IsBoilerplate()); |
| 2255 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2256 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2257 | frame_->EmitPush(cp); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 2258 | __ mov(r0, Operand(boilerplate)); |
| 2259 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2260 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2261 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2265 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2266 | #ifdef DEBUG |
| 2267 | int original_height = frame_->height(); |
| 2268 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2269 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2270 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2271 | |
| 2272 | // Build the function boilerplate and instantiate it. |
| 2273 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2274 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2275 | if (HasStackOverflow()) { |
| 2276 | ASSERT(frame_->height() == original_height); |
| 2277 | return; |
| 2278 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2279 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2280 | ASSERT(frame_->height() == original_height + 1); |
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::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2285 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2286 | #ifdef DEBUG |
| 2287 | int original_height = frame_->height(); |
| 2288 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2289 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2290 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2291 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2292 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2296 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2297 | #ifdef DEBUG |
| 2298 | int original_height = frame_->height(); |
| 2299 | #endif |
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 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2302 | JumpTarget then; |
| 2303 | JumpTarget else_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2304 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 2305 | &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2306 | if (has_valid_frame()) { |
| 2307 | Branch(false, &else_); |
| 2308 | } |
| 2309 | if (has_valid_frame() || then.is_linked()) { |
| 2310 | then.Bind(); |
| 2311 | LoadAndSpill(node->then_expression(), typeof_state()); |
| 2312 | } |
| 2313 | if (else_.is_linked()) { |
| 2314 | JumpTarget exit; |
| 2315 | if (has_valid_frame()) exit.Jump(); |
| 2316 | else_.Bind(); |
| 2317 | LoadAndSpill(node->else_expression(), typeof_state()); |
| 2318 | if (exit.is_linked()) exit.Bind(); |
| 2319 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2320 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2324 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2325 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2326 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2327 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2328 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2329 | JumpTarget slow; |
| 2330 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2331 | |
| 2332 | // Generate fast-case code for variables that might be shadowed by |
| 2333 | // eval-introduced variables. Eval is used a lot without |
| 2334 | // introducing variables. In those cases, we do not want to |
| 2335 | // perform a runtime call for all variables in the scope |
| 2336 | // containing the eval. |
| 2337 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2338 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2339 | // If there was no control flow to slow, we can exit early. |
| 2340 | if (!slow.is_linked()) { |
| 2341 | frame_->EmitPush(r0); |
| 2342 | return; |
| 2343 | } |
| 2344 | |
| 2345 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2346 | |
| 2347 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2348 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2349 | // Only generate the fast case for locals that rewrite to slots. |
| 2350 | // This rules out argument loads. |
| 2351 | if (potential_slot != NULL) { |
| 2352 | __ ldr(r0, |
| 2353 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2354 | r1, |
| 2355 | r2, |
| 2356 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2357 | if (potential_slot->var()->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2358 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2359 | __ cmp(r0, ip); |
| 2360 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2361 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2362 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2363 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2364 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2365 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2366 | } |
| 2367 | } |
| 2368 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2369 | slow.Bind(); |
| 2370 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2371 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2372 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2373 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2374 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2375 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2376 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2377 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2378 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2379 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2380 | done.Bind(); |
| 2381 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2382 | |
| 2383 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2384 | // Note: We would like to keep the assert below, but it fires because of |
| 2385 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2386 | // ASSERT(!slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2387 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2388 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2389 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2390 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2391 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2392 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2393 | // initialized yet) which needs to be converted into the 'undefined' |
| 2394 | // value. |
| 2395 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2396 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2397 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2398 | __ cmp(r0, ip); |
| 2399 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2400 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2406 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2407 | TypeofState typeof_state, |
| 2408 | Register tmp, |
| 2409 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2410 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2411 | // Check that no extension objects have been created by calls to |
| 2412 | // eval from the current scope to the global scope. |
| 2413 | Register context = cp; |
| 2414 | Scope* s = scope(); |
| 2415 | while (s != NULL) { |
| 2416 | if (s->num_heap_slots() > 0) { |
| 2417 | if (s->calls_eval()) { |
| 2418 | // Check that extension is NULL. |
| 2419 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2420 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2421 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2422 | } |
| 2423 | // Load next context in chain. |
| 2424 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2425 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2426 | context = tmp; |
| 2427 | } |
| 2428 | // If no outer scope calls eval, we do not need to check more |
| 2429 | // context extensions. |
| 2430 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2431 | s = s->outer_scope(); |
| 2432 | } |
| 2433 | |
| 2434 | if (s->is_eval_scope()) { |
| 2435 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2436 | if (!context.is(tmp)) { |
| 2437 | __ mov(tmp, Operand(context)); |
| 2438 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2439 | __ bind(&next); |
| 2440 | // Terminate at global context. |
| 2441 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2442 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2443 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2444 | __ b(eq, &fast); |
| 2445 | // Check that extension is NULL. |
| 2446 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2447 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2448 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2449 | // Load next context in chain. |
| 2450 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2451 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2452 | __ b(&next); |
| 2453 | __ bind(&fast); |
| 2454 | } |
| 2455 | |
| 2456 | // All extension objects were empty and it is safe to use a global |
| 2457 | // load IC call. |
| 2458 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2459 | // Load the global object. |
| 2460 | LoadGlobal(); |
| 2461 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2462 | Result name(r2); |
| 2463 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2464 | // Call IC stub. |
| 2465 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2466 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2467 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2468 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2471 | // Drop the global object. The result is in r0. |
| 2472 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2476 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2477 | #ifdef DEBUG |
| 2478 | int original_height = frame_->height(); |
| 2479 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2480 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2481 | Comment cmnt(masm_, "[ Slot"); |
| 2482 | LoadFromSlot(node, typeof_state()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2483 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2484 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2485 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2486 | |
| 2487 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2488 | #ifdef DEBUG |
| 2489 | int original_height = frame_->height(); |
| 2490 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2491 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2492 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2493 | |
| 2494 | Variable* var = node->var(); |
| 2495 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2496 | if (expr != NULL) { |
| 2497 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2498 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2499 | ASSERT(var->is_global()); |
| 2500 | Reference ref(this, node); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2501 | ref.GetValueAndSpill(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2502 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2503 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2507 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2508 | #ifdef DEBUG |
| 2509 | int original_height = frame_->height(); |
| 2510 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2511 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2512 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2513 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2514 | frame_->EmitPush(r0); |
| 2515 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2519 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2520 | #ifdef DEBUG |
| 2521 | int original_height = frame_->height(); |
| 2522 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2523 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2524 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2525 | |
| 2526 | // Retrieve the literal array and check the allocated entry. |
| 2527 | |
| 2528 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2529 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2530 | |
| 2531 | // Load the literals array of the function. |
| 2532 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2533 | |
| 2534 | // Load the literal at the ast saved index. |
| 2535 | int literal_offset = |
| 2536 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2537 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2538 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2539 | JumpTarget done; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2540 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2541 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2542 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2543 | |
| 2544 | // If the entry is undefined we call the runtime system to computed |
| 2545 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2546 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2547 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2548 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2549 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2550 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2551 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2552 | frame_->EmitPush(r0); |
| 2553 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2554 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2555 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2556 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2557 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2558 | frame_->EmitPush(r2); |
| 2559 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | // This deferred code stub will be used for creating the boilerplate |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2564 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2565 | // Each created boilerplate is stored in the JSFunction and they are |
| 2566 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2567 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2568 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2569 | explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2570 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2571 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2572 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2573 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2574 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2575 | private: |
| 2576 | ObjectLiteral* node_; |
| 2577 | }; |
| 2578 | |
| 2579 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2580 | void DeferredObjectLiteral::Generate() { |
| 2581 | // Argument is passed in r1. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2582 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2583 | // If the entry is undefined we call the runtime system to compute |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2584 | // the literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2585 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2586 | __ push(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2587 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2588 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2589 | __ push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2590 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2591 | __ mov(r0, Operand(node_->constant_properties())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2592 | __ push(r0); |
| 2593 | __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2594 | __ mov(r2, Operand(r0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2595 | // Result is returned in r2. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2599 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2600 | #ifdef DEBUG |
| 2601 | int original_height = frame_->height(); |
| 2602 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2603 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2604 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2605 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2606 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2607 | |
| 2608 | // Retrieve the literal array and check the allocated entry. |
| 2609 | |
| 2610 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2611 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2612 | |
| 2613 | // Load the literals array of the function. |
| 2614 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2615 | |
| 2616 | // Load the literal at the ast saved index. |
| 2617 | int literal_offset = |
| 2618 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2619 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2620 | |
| 2621 | // Check whether we need to materialize the object literal boilerplate. |
| 2622 | // If so, jump to the deferred code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2623 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2624 | __ cmp(r2, Operand(ip)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2625 | deferred->Branch(eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2626 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2627 | |
| 2628 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2629 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2630 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2631 | // Clone the boilerplate object. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2632 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2633 | if (node->depth() == 1) { |
| 2634 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2635 | } |
| 2636 | frame_->CallRuntime(clone_function_id, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2637 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2638 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2639 | |
| 2640 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2641 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2642 | Literal* key = property->key(); |
| 2643 | Expression* value = property->value(); |
| 2644 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2645 | case ObjectLiteral::Property::CONSTANT: |
| 2646 | break; |
| 2647 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2648 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2649 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2650 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2651 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2652 | frame_->EmitPush(r0); // dup the result |
| 2653 | LoadAndSpill(key); |
| 2654 | LoadAndSpill(value); |
| 2655 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2656 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2657 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2658 | break; |
| 2659 | } |
| 2660 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2661 | frame_->EmitPush(r0); |
| 2662 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2663 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2664 | frame_->EmitPush(r0); |
| 2665 | LoadAndSpill(value); |
| 2666 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2667 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2668 | break; |
| 2669 | } |
| 2670 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2671 | frame_->EmitPush(r0); |
| 2672 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2673 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2674 | frame_->EmitPush(r0); |
| 2675 | LoadAndSpill(value); |
| 2676 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2677 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2678 | break; |
| 2679 | } |
| 2680 | } |
| 2681 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2682 | ASSERT(frame_->height() == original_height + 1); |
| 2683 | } |
| 2684 | |
| 2685 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2686 | // This deferred code stub will be used for creating the boilerplate |
| 2687 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2688 | // Each created boilerplate is stored in the JSFunction and they are |
| 2689 | // therefore context dependent. |
| 2690 | class DeferredArrayLiteral: public DeferredCode { |
| 2691 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2692 | explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2693 | set_comment("[ DeferredArrayLiteral"); |
| 2694 | } |
| 2695 | |
| 2696 | virtual void Generate(); |
| 2697 | |
| 2698 | private: |
| 2699 | ArrayLiteral* node_; |
| 2700 | }; |
| 2701 | |
| 2702 | |
| 2703 | void DeferredArrayLiteral::Generate() { |
| 2704 | // Argument is passed in r1. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2705 | |
| 2706 | // If the entry is undefined we call the runtime system to computed |
| 2707 | // the literal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2708 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2709 | __ push(r1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2710 | // Literal index (1). |
| 2711 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2712 | __ push(r0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2713 | // Constant properties (2). |
| 2714 | __ mov(r0, Operand(node_->literals())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2715 | __ push(r0); |
| 2716 | __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2717 | __ mov(r2, Operand(r0)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2718 | // Result is returned in r2. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2722 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2723 | #ifdef DEBUG |
| 2724 | int original_height = frame_->height(); |
| 2725 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2726 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2727 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2728 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2729 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2730 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2731 | // Retrieve the literal array and check the allocated entry. |
| 2732 | |
| 2733 | // Load the function of this activation. |
| 2734 | __ ldr(r1, frame_->Function()); |
| 2735 | |
| 2736 | // Load the literals array of the function. |
| 2737 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2738 | |
| 2739 | // Load the literal at the ast saved index. |
| 2740 | int literal_offset = |
| 2741 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2742 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2743 | |
| 2744 | // Check whether we need to materialize the object literal boilerplate. |
| 2745 | // If so, jump to the deferred code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2746 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2747 | __ cmp(r2, Operand(ip)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2748 | deferred->Branch(eq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2749 | deferred->BindExit(); |
| 2750 | |
| 2751 | // Push the object literal boilerplate. |
| 2752 | frame_->EmitPush(r2); |
| 2753 | |
| 2754 | // Clone the boilerplate object. |
| 2755 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2756 | if (node->depth() == 1) { |
| 2757 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2758 | } |
| 2759 | frame_->CallRuntime(clone_function_id, 1); |
| 2760 | frame_->EmitPush(r0); // save the result |
| 2761 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2762 | |
| 2763 | // Generate code to set the elements in the array that are not |
| 2764 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2765 | for (int i = 0; i < node->values()->length(); i++) { |
| 2766 | Expression* value = node->values()->at(i); |
| 2767 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2768 | // If value is a literal the property value is already set in the |
| 2769 | // boilerplate object. |
| 2770 | if (value->AsLiteral() != NULL) continue; |
| 2771 | // If value is a materialized literal the property value is already set |
| 2772 | // in the boilerplate object if it is simple. |
| 2773 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2774 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2775 | // The property must be set by generated code. |
| 2776 | LoadAndSpill(value); |
| 2777 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2778 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2779 | // Fetch the object literal. |
| 2780 | __ ldr(r1, frame_->Top()); |
| 2781 | // Get the elements array. |
| 2782 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2783 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2784 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2785 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2786 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2787 | |
| 2788 | // Update the write barrier for the array address. |
| 2789 | __ mov(r3, Operand(offset)); |
| 2790 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2791 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2792 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2796 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2797 | #ifdef DEBUG |
| 2798 | int original_height = frame_->height(); |
| 2799 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2800 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2801 | // Call runtime routine to allocate the catch extension object and |
| 2802 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2803 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2804 | LoadAndSpill(node->key()); |
| 2805 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2806 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2807 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2808 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2812 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2813 | #ifdef DEBUG |
| 2814 | int original_height = frame_->height(); |
| 2815 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2816 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2817 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2818 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2819 | { Reference target(this, node->target()); |
| 2820 | if (target.is_illegal()) { |
| 2821 | // Fool the virtual frame into thinking that we left the assignment's |
| 2822 | // value on the frame. |
| 2823 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2824 | frame_->EmitPush(r0); |
| 2825 | ASSERT(frame_->height() == original_height + 1); |
| 2826 | return; |
| 2827 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2828 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2829 | if (node->op() == Token::ASSIGN || |
| 2830 | node->op() == Token::INIT_VAR || |
| 2831 | node->op() == Token::INIT_CONST) { |
| 2832 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2833 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2834 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2835 | // +=, *= and similar binary assignments. |
| 2836 | // Get the old value of the lhs. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2837 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 2838 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2839 | bool overwrite = |
| 2840 | (node->value()->AsBinaryOperation() != NULL && |
| 2841 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2842 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2843 | SmiOperation(node->binary_op(), |
| 2844 | literal->handle(), |
| 2845 | false, |
| 2846 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2847 | frame_->EmitPush(r0); |
| 2848 | |
| 2849 | } else { |
| 2850 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2851 | GenericBinaryOperation(node->binary_op(), |
| 2852 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2853 | frame_->EmitPush(r0); |
| 2854 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2855 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2856 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2857 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2858 | if (var != NULL && |
| 2859 | (var->mode() == Variable::CONST) && |
| 2860 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2861 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2862 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2863 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2864 | CodeForSourcePosition(node->position()); |
| 2865 | if (node->op() == Token::INIT_CONST) { |
| 2866 | // Dynamic constant initializations must use the function context |
| 2867 | // and initialize the actual constant declared. Dynamic variable |
| 2868 | // initializations are simply assignments and use SetValue. |
| 2869 | target.SetValue(CONST_INIT); |
| 2870 | } else { |
| 2871 | target.SetValue(NOT_CONST_INIT); |
| 2872 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2873 | } |
| 2874 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2875 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2879 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2880 | #ifdef DEBUG |
| 2881 | int original_height = frame_->height(); |
| 2882 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2883 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2884 | Comment cmnt(masm_, "[ Throw"); |
| 2885 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2886 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2887 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2888 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2889 | frame_->EmitPush(r0); |
| 2890 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2894 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2895 | #ifdef DEBUG |
| 2896 | int original_height = frame_->height(); |
| 2897 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2898 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2899 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2900 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2901 | { Reference property(this, node); |
| 2902 | property.GetValueAndSpill(typeof_state()); |
| 2903 | } |
| 2904 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2908 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2909 | #ifdef DEBUG |
| 2910 | int original_height = frame_->height(); |
| 2911 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2912 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2913 | Comment cmnt(masm_, "[ Call"); |
| 2914 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2915 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2916 | ZoneList<Expression*>* args = node->arguments(); |
| 2917 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2918 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2919 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2920 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2921 | Property* property = function->AsProperty(); |
| 2922 | |
| 2923 | // ------------------------------------------------------------------------ |
| 2924 | // Fast-case: Use inline caching. |
| 2925 | // --- |
| 2926 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2927 | // must be resolved after the arguments have been evaluated. The IC code |
| 2928 | // automatically handles this by loading the arguments before the function |
| 2929 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2930 | // ------------------------------------------------------------------------ |
| 2931 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2932 | if (var != NULL && var->is_possibly_eval()) { |
| 2933 | // ---------------------------------- |
| 2934 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 2935 | // ---------------------------------- |
| 2936 | |
| 2937 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 2938 | // resolve the function we need to call and the receiver of the |
| 2939 | // call. Then we call the resolved function using the given |
| 2940 | // arguments. |
| 2941 | // Prepare stack for call to resolved function. |
| 2942 | LoadAndSpill(function); |
| 2943 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 2944 | frame_->EmitPush(r2); // Slot for receiver |
| 2945 | int arg_count = args->length(); |
| 2946 | for (int i = 0; i < arg_count; i++) { |
| 2947 | LoadAndSpill(args->at(i)); |
| 2948 | } |
| 2949 | |
| 2950 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 2951 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 2952 | frame_->EmitPush(r1); |
| 2953 | if (arg_count > 0) { |
| 2954 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 2955 | frame_->EmitPush(r1); |
| 2956 | } else { |
| 2957 | frame_->EmitPush(r2); |
| 2958 | } |
| 2959 | |
| 2960 | // Resolve the call. |
| 2961 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
| 2962 | |
| 2963 | // Touch up stack with the right values for the function and the receiver. |
| 2964 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
| 2965 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
| 2966 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
| 2967 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 2968 | |
| 2969 | // Call the function. |
| 2970 | CodeForSourcePosition(node->position()); |
| 2971 | |
| 2972 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 2973 | CallFunctionStub call_function(arg_count, in_loop); |
| 2974 | frame_->CallStub(&call_function, arg_count + 1); |
| 2975 | |
| 2976 | __ ldr(cp, frame_->Context()); |
| 2977 | // Remove the function from the stack. |
| 2978 | frame_->Drop(); |
| 2979 | frame_->EmitPush(r0); |
| 2980 | |
| 2981 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2982 | // ---------------------------------- |
| 2983 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 2984 | // ---------------------------------- |
| 2985 | |
| 2986 | // 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] | 2987 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2988 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2989 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2990 | // Pass the global object as the receiver and let the IC stub |
| 2991 | // patch the stack to use the global proxy as 'this' in the |
| 2992 | // invoked function. |
| 2993 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2994 | |
| 2995 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2996 | int arg_count = args->length(); |
| 2997 | for (int i = 0; i < arg_count; i++) { |
| 2998 | LoadAndSpill(args->at(i)); |
| 2999 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3000 | |
| 3001 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3002 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3003 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3004 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3005 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3006 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3007 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3008 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3009 | frame_->Drop(); |
| 3010 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3011 | |
| 3012 | } else if (var != NULL && var->slot() != NULL && |
| 3013 | var->slot()->type() == Slot::LOOKUP) { |
| 3014 | // ---------------------------------- |
| 3015 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3016 | // ---------------------------------- |
| 3017 | |
| 3018 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3019 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3020 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3021 | frame_->EmitPush(r0); |
| 3022 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3023 | // r0: slot value; r1: receiver |
| 3024 | |
| 3025 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3026 | frame_->EmitPush(r0); // function |
| 3027 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3028 | |
| 3029 | // Call the function. |
| 3030 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3031 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3032 | |
| 3033 | } else if (property != NULL) { |
| 3034 | // Check if the key is a literal string. |
| 3035 | Literal* literal = property->key()->AsLiteral(); |
| 3036 | |
| 3037 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3038 | // ------------------------------------------------------------------ |
| 3039 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3040 | // ------------------------------------------------------------------ |
| 3041 | |
| 3042 | // 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] | 3043 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3044 | frame_->EmitPush(r0); |
| 3045 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3046 | |
| 3047 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3048 | int arg_count = args->length(); |
| 3049 | for (int i = 0; i < arg_count; i++) { |
| 3050 | LoadAndSpill(args->at(i)); |
| 3051 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3052 | |
| 3053 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3054 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3055 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3056 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3057 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3058 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3059 | |
| 3060 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3061 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3062 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3063 | 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] | 3064 | |
| 3065 | } else { |
| 3066 | // ------------------------------------------- |
| 3067 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3068 | // ------------------------------------------- |
| 3069 | |
| 3070 | // Load the function to call from the property through a reference. |
| 3071 | Reference ref(this, property); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3072 | ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3073 | |
| 3074 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3075 | if (property->is_synthetic()) { |
| 3076 | LoadGlobalReceiver(r0); |
| 3077 | } else { |
| 3078 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3079 | frame_->EmitPush(r0); |
| 3080 | } |
| 3081 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3082 | // Call the function. |
| 3083 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3084 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | } else { |
| 3088 | // ---------------------------------- |
| 3089 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3090 | // ---------------------------------- |
| 3091 | |
| 3092 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3093 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3094 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3095 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3096 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3097 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3098 | // Call the function. |
| 3099 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3100 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3101 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3102 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3106 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3107 | #ifdef DEBUG |
| 3108 | int original_height = frame_->height(); |
| 3109 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3110 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3111 | Comment cmnt(masm_, "[ CallNew"); |
| 3112 | |
| 3113 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3114 | // expression in new calls must be evaluated before the |
| 3115 | // arguments. This is different from ordinary calls, where the |
| 3116 | // actual function to call is resolved after the arguments have been |
| 3117 | // evaluated. |
| 3118 | |
| 3119 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3120 | // receiver. There is no need to use the global proxy here because |
| 3121 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3122 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3123 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Push the arguments ("left-to-right") on the stack. |
| 3126 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3127 | int arg_count = args->length(); |
| 3128 | for (int i = 0; i < arg_count; i++) { |
| 3129 | LoadAndSpill(args->at(i)); |
| 3130 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3131 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3132 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3133 | Result num_args(r0); |
| 3134 | __ mov(r0, Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3135 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3136 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3137 | Result function(r1); |
| 3138 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3139 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3140 | // Call the construct call builtin that handles allocation and |
| 3141 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3142 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3143 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3144 | frame_->CallCodeObject(ic, |
| 3145 | RelocInfo::CONSTRUCT_CALL, |
| 3146 | &num_args, |
| 3147 | &function, |
| 3148 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3149 | |
| 3150 | // 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] | 3151 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3152 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3156 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3157 | VirtualFrame::SpilledScope spilled_scope; |
| 3158 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3159 | JumpTarget leave, null, function, non_function_constructor; |
| 3160 | |
| 3161 | // Load the object into r0. |
| 3162 | LoadAndSpill(args->at(0)); |
| 3163 | frame_->EmitPop(r0); |
| 3164 | |
| 3165 | // If the object is a smi, we return null. |
| 3166 | __ tst(r0, Operand(kSmiTagMask)); |
| 3167 | null.Branch(eq); |
| 3168 | |
| 3169 | // Check that the object is a JS object but take special care of JS |
| 3170 | // functions to make sure they have 'Function' as their class. |
| 3171 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3172 | null.Branch(lt); |
| 3173 | |
| 3174 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3175 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3176 | // LAST_JS_OBJECT_TYPE. |
| 3177 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3178 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3179 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3180 | function.Branch(eq); |
| 3181 | |
| 3182 | // Check if the constructor in the map is a function. |
| 3183 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3184 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3185 | non_function_constructor.Branch(ne); |
| 3186 | |
| 3187 | // The r0 register now contains the constructor function. Grab the |
| 3188 | // instance class name from there. |
| 3189 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3190 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3191 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3192 | leave.Jump(); |
| 3193 | |
| 3194 | // Functions have class 'Function'. |
| 3195 | function.Bind(); |
| 3196 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3197 | frame_->EmitPush(r0); |
| 3198 | leave.Jump(); |
| 3199 | |
| 3200 | // Objects with a non-function constructor have class 'Object'. |
| 3201 | non_function_constructor.Bind(); |
| 3202 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3203 | frame_->EmitPush(r0); |
| 3204 | leave.Jump(); |
| 3205 | |
| 3206 | // Non-JS objects have class null. |
| 3207 | null.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3208 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3209 | frame_->EmitPush(r0); |
| 3210 | |
| 3211 | // All done. |
| 3212 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
| 3215 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3216 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3217 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3218 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3219 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3220 | LoadAndSpill(args->at(0)); |
| 3221 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3222 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3223 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3224 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3225 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3226 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3227 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3228 | // Load the value. |
| 3229 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3230 | leave.Bind(); |
| 3231 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3235 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3236 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3237 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3238 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3239 | LoadAndSpill(args->at(0)); // Load the object. |
| 3240 | LoadAndSpill(args->at(1)); // Load the value. |
| 3241 | frame_->EmitPop(r0); // r0 contains value |
| 3242 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3243 | // if (object->IsSmi()) return object. |
| 3244 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3245 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3246 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3247 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3248 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3249 | // Store the value. |
| 3250 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3251 | // Update the write barrier. |
| 3252 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3253 | __ RecordWrite(r1, r2, r3); |
| 3254 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3255 | leave.Bind(); |
| 3256 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3260 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3261 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3262 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3263 | LoadAndSpill(args->at(0)); |
| 3264 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3265 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3266 | cc_reg_ = eq; |
| 3267 | } |
| 3268 | |
| 3269 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3270 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3271 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3272 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3273 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3274 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3275 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3276 | LoadAndSpill(args->at(1)); |
| 3277 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3278 | __ CallRuntime(Runtime::kLog, 2); |
| 3279 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3280 | #endif |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3281 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3282 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3286 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3287 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3288 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3289 | LoadAndSpill(args->at(0)); |
| 3290 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3291 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3292 | cc_reg_ = eq; |
| 3293 | } |
| 3294 | |
| 3295 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3296 | // This should generate code that performs a charCodeAt() call or returns |
| 3297 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3298 | // 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] | 3299 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3300 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3301 | ASSERT(args->length() == 2); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3302 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3303 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3307 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3308 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3309 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3310 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3311 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3312 | // We need the CC bits to come out as not_equal in the case where the |
| 3313 | // object is a smi. This can't be done with the usual test opcode so |
| 3314 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3315 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3316 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3317 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3318 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3319 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3320 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3321 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3322 | cc_reg_ = eq; |
| 3323 | } |
| 3324 | |
| 3325 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3326 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3327 | VirtualFrame::SpilledScope spilled_scope; |
| 3328 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3329 | |
| 3330 | // Get the frame pointer for the calling frame. |
| 3331 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3332 | |
| 3333 | // Skip the arguments adaptor frame if it exists. |
| 3334 | Label check_frame_marker; |
| 3335 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3336 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3337 | __ b(ne, &check_frame_marker); |
| 3338 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3339 | |
| 3340 | // Check the marker in the calling frame. |
| 3341 | __ bind(&check_frame_marker); |
| 3342 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3343 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3344 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3348 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3349 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3350 | ASSERT(args->length() == 0); |
| 3351 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3352 | // Seed the result with the formal parameters count, which will be used |
| 3353 | // 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] | 3354 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3355 | |
| 3356 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3357 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3358 | frame_->CallStub(&stub, 0); |
| 3359 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3363 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3364 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3365 | ASSERT(args->length() == 1); |
| 3366 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3367 | // Satisfy contract with ArgumentsAccessStub: |
| 3368 | // 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] | 3369 | LoadAndSpill(args->at(0)); |
| 3370 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3371 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3372 | |
| 3373 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3374 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3375 | frame_->CallStub(&stub, 0); |
| 3376 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3380 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3381 | VirtualFrame::SpilledScope spilled_scope; |
| 3382 | ASSERT(args->length() == 0); |
| 3383 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3384 | RelocInfo::RUNTIME_ENTRY); |
| 3385 | frame_->EmitPush(r0); |
| 3386 | } |
| 3387 | |
| 3388 | |
| 3389 | void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) { |
| 3390 | VirtualFrame::SpilledScope spilled_scope; |
| 3391 | LoadAndSpill(args->at(0)); |
| 3392 | switch (op) { |
| 3393 | case SIN: |
| 3394 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 3395 | break; |
| 3396 | case COS: |
| 3397 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 3398 | break; |
| 3399 | } |
| 3400 | frame_->EmitPush(r0); |
| 3401 | } |
| 3402 | |
| 3403 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3404 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3405 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3406 | ASSERT(args->length() == 2); |
| 3407 | |
| 3408 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3409 | LoadAndSpill(args->at(0)); |
| 3410 | LoadAndSpill(args->at(1)); |
| 3411 | frame_->EmitPop(r0); |
| 3412 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3413 | __ cmp(r0, Operand(r1)); |
| 3414 | cc_reg_ = eq; |
| 3415 | } |
| 3416 | |
| 3417 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3418 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3419 | #ifdef DEBUG |
| 3420 | int original_height = frame_->height(); |
| 3421 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3422 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3423 | if (CheckForInlineRuntimeCall(node)) { |
| 3424 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3425 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3426 | return; |
| 3427 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3428 | |
| 3429 | ZoneList<Expression*>* args = node->arguments(); |
| 3430 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3431 | Runtime::Function* function = node->function(); |
| 3432 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3433 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3434 | // Prepare stack for calling JS runtime function. |
| 3435 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3436 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3437 | // Push the builtins object found in the current global object. |
| 3438 | __ ldr(r1, GlobalObject()); |
| 3439 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3440 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3441 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3442 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3443 | // Push the arguments ("left-to-right"). |
| 3444 | int arg_count = args->length(); |
| 3445 | for (int i = 0; i < arg_count; i++) { |
| 3446 | LoadAndSpill(args->at(i)); |
| 3447 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3448 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3449 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3450 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3451 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3452 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3453 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3454 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3455 | frame_->Drop(); |
| 3456 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3457 | } else { |
| 3458 | // Call the C runtime function. |
| 3459 | frame_->CallRuntime(function, arg_count); |
| 3460 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3461 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3462 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
| 3465 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3466 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3467 | #ifdef DEBUG |
| 3468 | int original_height = frame_->height(); |
| 3469 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3470 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3471 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3472 | |
| 3473 | Token::Value op = node->op(); |
| 3474 | |
| 3475 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3476 | LoadConditionAndSpill(node->expression(), |
| 3477 | NOT_INSIDE_TYPEOF, |
| 3478 | false_target(), |
| 3479 | true_target(), |
| 3480 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3481 | // LoadCondition may (and usually does) leave a test and branch to |
| 3482 | // be emitted by the caller. In that case, negate the condition. |
| 3483 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3484 | |
| 3485 | } else if (op == Token::DELETE) { |
| 3486 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3487 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3488 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3489 | LoadAndSpill(property->obj()); |
| 3490 | LoadAndSpill(property->key()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3491 | Result arg_count(r0); |
| 3492 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3493 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3494 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3495 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3496 | Slot* slot = variable->slot(); |
| 3497 | if (variable->is_global()) { |
| 3498 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3499 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3500 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3501 | Result arg_count(r0); |
| 3502 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3503 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3504 | |
| 3505 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3506 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3507 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3508 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3509 | frame_->EmitPush(r0); |
| 3510 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3511 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3512 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3513 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3514 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3515 | Result arg_count(r0); |
| 3516 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3517 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3518 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3519 | } else { |
| 3520 | // Default: Result of deleting non-global, not dynamically |
| 3521 | // introduced variables is false. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3522 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3523 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3524 | |
| 3525 | } else { |
| 3526 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3527 | LoadAndSpill(node->expression()); // may have side-effects |
| 3528 | frame_->Drop(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3529 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3530 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3531 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3532 | |
| 3533 | } else if (op == Token::TYPEOF) { |
| 3534 | // Special case for loading the typeof expression; see comment on |
| 3535 | // LoadTypeofExpression(). |
| 3536 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3537 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3538 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3539 | |
| 3540 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3541 | LoadAndSpill(node->expression()); |
| 3542 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3543 | switch (op) { |
| 3544 | case Token::NOT: |
| 3545 | case Token::DELETE: |
| 3546 | case Token::TYPEOF: |
| 3547 | UNREACHABLE(); // handled above |
| 3548 | break; |
| 3549 | |
| 3550 | case Token::SUB: { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3551 | bool overwrite = |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3552 | (node->expression()->AsBinaryOperation() != NULL && |
| 3553 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3554 | UnarySubStub stub(overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3555 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3556 | break; |
| 3557 | } |
| 3558 | |
| 3559 | case Token::BIT_NOT: { |
| 3560 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3561 | JumpTarget smi_label; |
| 3562 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3563 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3564 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3565 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3566 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3567 | Result arg_count(r0); |
| 3568 | __ mov(r0, Operand(0)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3569 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3570 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3571 | continue_label.Jump(); |
| 3572 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3573 | __ mvn(r0, Operand(r0)); |
| 3574 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3575 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3576 | break; |
| 3577 | } |
| 3578 | |
| 3579 | case Token::VOID: |
| 3580 | // since the stack top is cached in r0, popping and then |
| 3581 | // pushing a value can be done by just writing to r0. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3582 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3583 | break; |
| 3584 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3585 | case Token::ADD: { |
| 3586 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3587 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3588 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3589 | continue_label.Branch(eq); |
| 3590 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3591 | Result arg_count(r0); |
| 3592 | __ mov(r0, Operand(0)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3593 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3594 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3595 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3596 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3597 | default: |
| 3598 | UNREACHABLE(); |
| 3599 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3600 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3601 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3602 | ASSERT(!has_valid_frame() || |
| 3603 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3604 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3608 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3609 | #ifdef DEBUG |
| 3610 | int original_height = frame_->height(); |
| 3611 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3612 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3613 | Comment cmnt(masm_, "[ CountOperation"); |
| 3614 | |
| 3615 | bool is_postfix = node->is_postfix(); |
| 3616 | bool is_increment = node->op() == Token::INC; |
| 3617 | |
| 3618 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3619 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3620 | |
| 3621 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3622 | if (is_postfix) { |
| 3623 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3624 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3625 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3626 | |
| 3627 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3628 | if (target.is_illegal()) { |
| 3629 | // Spoof the virtual frame to have the expected height (one higher |
| 3630 | // than on entry). |
| 3631 | if (!is_postfix) { |
| 3632 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3633 | frame_->EmitPush(r0); |
| 3634 | } |
| 3635 | ASSERT(frame_->height() == original_height + 1); |
| 3636 | return; |
| 3637 | } |
| 3638 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 3639 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3640 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3641 | JumpTarget slow; |
| 3642 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3643 | |
| 3644 | // Load the value (1) into register r1. |
| 3645 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3646 | |
| 3647 | // Check for smi operand. |
| 3648 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3649 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3650 | |
| 3651 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3652 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3653 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3654 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3655 | |
| 3656 | // Perform optimistic increment/decrement. |
| 3657 | if (is_increment) { |
| 3658 | __ add(r0, r0, Operand(r1), SetCC); |
| 3659 | } else { |
| 3660 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3661 | } |
| 3662 | |
| 3663 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3664 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3665 | |
| 3666 | // Revert optimistic increment/decrement. |
| 3667 | if (is_increment) { |
| 3668 | __ sub(r0, r0, Operand(r1)); |
| 3669 | } else { |
| 3670 | __ add(r0, r0, Operand(r1)); |
| 3671 | } |
| 3672 | |
| 3673 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3674 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3675 | { |
| 3676 | // Convert the operand to a number. |
| 3677 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3678 | Result arg_count(r0); |
| 3679 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3680 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3681 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3682 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3683 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3684 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3687 | // Compute the new value. |
| 3688 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3689 | frame_->EmitPush(r0); |
| 3690 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3691 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3692 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3693 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3694 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3698 | exit.Bind(); |
| 3699 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3700 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3704 | if (is_postfix) frame_->EmitPop(r0); |
| 3705 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3709 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3710 | #ifdef DEBUG |
| 3711 | int original_height = frame_->height(); |
| 3712 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3713 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3714 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3715 | Token::Value op = node->op(); |
| 3716 | |
| 3717 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3718 | // operators must yield the result of one of the two expressions |
| 3719 | // before any ToBoolean() conversions. This means that the value |
| 3720 | // produced by a && or || operator is not necessarily a boolean. |
| 3721 | |
| 3722 | // NOTE: If the left hand side produces a materialized value (not in |
| 3723 | // the CC register), we force the right hand side to do the |
| 3724 | // same. This is necessary because we may have to branch to the exit |
| 3725 | // after evaluating the left hand side (due to the shortcut |
| 3726 | // semantics), but the compiler must (statically) know if the result |
| 3727 | // of compiling the binary operation is materialized or not. |
| 3728 | |
| 3729 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3730 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3731 | LoadConditionAndSpill(node->left(), |
| 3732 | NOT_INSIDE_TYPEOF, |
| 3733 | &is_true, |
| 3734 | false_target(), |
| 3735 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3736 | if (has_valid_frame() && !has_cc()) { |
| 3737 | // 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] | 3738 | JumpTarget pop_and_continue; |
| 3739 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3740 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3741 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3742 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3743 | // Avoid popping the result if it converts to 'false' using the |
| 3744 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3745 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3746 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3747 | Branch(false, &exit); |
| 3748 | |
| 3749 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3750 | pop_and_continue.Bind(); |
| 3751 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3752 | |
| 3753 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3754 | is_true.Bind(); |
| 3755 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3756 | |
| 3757 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3758 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3759 | } else if (has_cc() || is_true.is_linked()) { |
| 3760 | // The left-hand side is either (a) partially compiled to |
| 3761 | // control flow with a final branch left to emit or (b) fully |
| 3762 | // compiled to control flow and possibly true. |
| 3763 | if (has_cc()) { |
| 3764 | Branch(false, false_target()); |
| 3765 | } |
| 3766 | is_true.Bind(); |
| 3767 | LoadConditionAndSpill(node->right(), |
| 3768 | NOT_INSIDE_TYPEOF, |
| 3769 | true_target(), |
| 3770 | false_target(), |
| 3771 | false); |
| 3772 | } else { |
| 3773 | // Nothing to do. |
| 3774 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3778 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3779 | LoadConditionAndSpill(node->left(), |
| 3780 | NOT_INSIDE_TYPEOF, |
| 3781 | true_target(), |
| 3782 | &is_false, |
| 3783 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3784 | if (has_valid_frame() && !has_cc()) { |
| 3785 | // 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] | 3786 | JumpTarget pop_and_continue; |
| 3787 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3788 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3789 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3790 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3791 | // Avoid popping the result if it converts to 'true' using the |
| 3792 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3793 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3794 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3795 | Branch(true, &exit); |
| 3796 | |
| 3797 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3798 | pop_and_continue.Bind(); |
| 3799 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3800 | |
| 3801 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3802 | is_false.Bind(); |
| 3803 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3804 | |
| 3805 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3806 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3807 | } else if (has_cc() || is_false.is_linked()) { |
| 3808 | // The left-hand side is either (a) partially compiled to |
| 3809 | // control flow with a final branch left to emit or (b) fully |
| 3810 | // compiled to control flow and possibly false. |
| 3811 | if (has_cc()) { |
| 3812 | Branch(true, true_target()); |
| 3813 | } |
| 3814 | is_false.Bind(); |
| 3815 | LoadConditionAndSpill(node->right(), |
| 3816 | NOT_INSIDE_TYPEOF, |
| 3817 | true_target(), |
| 3818 | false_target(), |
| 3819 | false); |
| 3820 | } else { |
| 3821 | // Nothing to do. |
| 3822 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | } else { |
| 3826 | // Optimize for the case where (at least) one of the expressions |
| 3827 | // is a literal small integer. |
| 3828 | Literal* lliteral = node->left()->AsLiteral(); |
| 3829 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3830 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 3831 | // never return a constant/immutable object. |
| 3832 | bool overwrite_left = |
| 3833 | (node->left()->AsBinaryOperation() != NULL && |
| 3834 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3835 | bool overwrite_right = |
| 3836 | (node->right()->AsBinaryOperation() != NULL && |
| 3837 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3838 | |
| 3839 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3840 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3841 | SmiOperation(node->op(), |
| 3842 | rliteral->handle(), |
| 3843 | false, |
| 3844 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3845 | |
| 3846 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3847 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3848 | SmiOperation(node->op(), |
| 3849 | lliteral->handle(), |
| 3850 | true, |
| 3851 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3852 | |
| 3853 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3854 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 3855 | if (overwrite_left) { |
| 3856 | overwrite_mode = OVERWRITE_LEFT; |
| 3857 | } else if (overwrite_right) { |
| 3858 | overwrite_mode = OVERWRITE_RIGHT; |
| 3859 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3860 | LoadAndSpill(node->left()); |
| 3861 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3862 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3863 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3864 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3865 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3866 | ASSERT(!has_valid_frame() || |
| 3867 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3868 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3872 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3873 | #ifdef DEBUG |
| 3874 | int original_height = frame_->height(); |
| 3875 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3876 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3877 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3878 | frame_->EmitPush(r0); |
| 3879 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3883 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3884 | #ifdef DEBUG |
| 3885 | int original_height = frame_->height(); |
| 3886 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3887 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3888 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3889 | |
| 3890 | // Get the expressions from the node. |
| 3891 | Expression* left = node->left(); |
| 3892 | Expression* right = node->right(); |
| 3893 | Token::Value op = node->op(); |
| 3894 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3895 | // To make null checks efficient, we check if either left or right is the |
| 3896 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3897 | // instead of calling the (very) general runtime routine for checking |
| 3898 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3899 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3900 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3901 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3902 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3903 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 3904 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3905 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3906 | LoadAndSpill(left_is_null ? right : left); |
| 3907 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3908 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 3909 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3910 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3911 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 3912 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3913 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3914 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3915 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3916 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 3917 | __ cmp(r0, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3918 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3919 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3920 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3921 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3922 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3923 | // It can be an undetectable object. |
| 3924 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3925 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 3926 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 3927 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3930 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3931 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3932 | return; |
| 3933 | } |
| 3934 | } |
| 3935 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3936 | // To make typeof testing for natives implemented in JavaScript really |
| 3937 | // efficient, we generate special code for expressions of the form: |
| 3938 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3939 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 3940 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 3941 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 3942 | (right->AsLiteral() != NULL && |
| 3943 | right->AsLiteral()->handle()->IsString())) { |
| 3944 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 3945 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3946 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3947 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3948 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3949 | |
| 3950 | if (check->Equals(Heap::number_symbol())) { |
| 3951 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3952 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3953 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3954 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 3955 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3956 | cc_reg_ = eq; |
| 3957 | |
| 3958 | } else if (check->Equals(Heap::string_symbol())) { |
| 3959 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3960 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3961 | |
| 3962 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3963 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3964 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3965 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3966 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3967 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3968 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3969 | |
| 3970 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3971 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 3972 | cc_reg_ = lt; |
| 3973 | |
| 3974 | } else if (check->Equals(Heap::boolean_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3975 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 3976 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3977 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3978 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 3979 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3980 | cc_reg_ = eq; |
| 3981 | |
| 3982 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3983 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 3984 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3985 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3986 | |
| 3987 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3988 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3989 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3990 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3991 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3992 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3993 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3994 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 3995 | |
| 3996 | cc_reg_ = eq; |
| 3997 | |
| 3998 | } else if (check->Equals(Heap::function_symbol())) { |
| 3999 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4000 | false_target()->Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4001 | __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4002 | cc_reg_ = eq; |
| 4003 | |
| 4004 | } else if (check->Equals(Heap::object_symbol())) { |
| 4005 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4006 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4007 | |
| 4008 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4009 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4010 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4011 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4012 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4013 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4014 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 4015 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4016 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4017 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4018 | |
| 4019 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4020 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4021 | false_target()->Branch(lt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4022 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 4023 | cc_reg_ = le; |
| 4024 | |
| 4025 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4026 | // Uncommon case: typeof testing against a string literal that is |
| 4027 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4028 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4029 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4030 | ASSERT(!has_valid_frame() || |
| 4031 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4032 | return; |
| 4033 | } |
| 4034 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4035 | switch (op) { |
| 4036 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4037 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4038 | break; |
| 4039 | |
| 4040 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4041 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4042 | break; |
| 4043 | |
| 4044 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4045 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4046 | break; |
| 4047 | |
| 4048 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4049 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4050 | break; |
| 4051 | |
| 4052 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4053 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4054 | break; |
| 4055 | |
| 4056 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4057 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4058 | break; |
| 4059 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4060 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4061 | LoadAndSpill(left); |
| 4062 | LoadAndSpill(right); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4063 | Result arg_count(r0); |
| 4064 | __ mov(r0, Operand(1)); // not counting receiver |
| 4065 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, &arg_count, 2); |
| 4066 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4067 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4068 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4069 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4070 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4071 | LoadAndSpill(left); |
| 4072 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4073 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4074 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4075 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4076 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4077 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4078 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4079 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4080 | |
| 4081 | default: |
| 4082 | UNREACHABLE(); |
| 4083 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4084 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4085 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4089 | #ifdef DEBUG |
| 4090 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4091 | #endif |
| 4092 | |
| 4093 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4094 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4095 | #define __ ACCESS_MASM(masm) |
| 4096 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4097 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4098 | Handle<String> Reference::GetName() { |
| 4099 | ASSERT(type_ == NAMED); |
| 4100 | Property* property = expression_->AsProperty(); |
| 4101 | if (property == NULL) { |
| 4102 | // Global variable reference treated as a named property reference. |
| 4103 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4104 | ASSERT(proxy->AsVariable() != NULL); |
| 4105 | ASSERT(proxy->AsVariable()->is_global()); |
| 4106 | return proxy->name(); |
| 4107 | } else { |
| 4108 | Literal* raw_name = property->key()->AsLiteral(); |
| 4109 | ASSERT(raw_name != NULL); |
| 4110 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4111 | } |
| 4112 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4113 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4114 | |
| 4115 | void Reference::GetValue(TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4116 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4117 | ASSERT(!is_illegal()); |
| 4118 | ASSERT(!cgen_->has_cc()); |
| 4119 | MacroAssembler* masm = cgen_->masm(); |
| 4120 | Property* property = expression_->AsProperty(); |
| 4121 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4122 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | switch (type_) { |
| 4126 | case SLOT: { |
| 4127 | Comment cmnt(masm, "[ Load from Slot"); |
| 4128 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4129 | ASSERT(slot != NULL); |
| 4130 | cgen_->LoadFromSlot(slot, typeof_state); |
| 4131 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4134 | case NAMED: { |
| 4135 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4136 | // distinction between expressions in a typeof and not in a typeof. If |
| 4137 | // there is a chance that reference errors can be thrown below, we |
| 4138 | // must distinguish between the two kinds of loads (typeof expression |
| 4139 | // loads must not throw a reference error). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4140 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4141 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4142 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4143 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4144 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4145 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4146 | Result name_reg(r2); |
| 4147 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4148 | ASSERT(var == NULL || var->is_global()); |
| 4149 | RelocInfo::Mode rmode = (var == NULL) |
| 4150 | ? RelocInfo::CODE_TARGET |
| 4151 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4152 | frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4153 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4154 | break; |
| 4155 | } |
| 4156 | |
| 4157 | case KEYED: { |
| 4158 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4159 | // distinction between expressions in a typeof and not in a typeof. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4160 | |
| 4161 | // TODO(181): Implement inlined version of array indexing once |
| 4162 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4163 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4164 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4165 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4166 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4167 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4168 | ASSERT(var == NULL || var->is_global()); |
| 4169 | RelocInfo::Mode rmode = (var == NULL) |
| 4170 | ? RelocInfo::CODE_TARGET |
| 4171 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4172 | frame->CallCodeObject(ic, rmode, 0); |
| 4173 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4174 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | default: |
| 4178 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4183 | void Reference::SetValue(InitState init_state) { |
| 4184 | ASSERT(!is_illegal()); |
| 4185 | ASSERT(!cgen_->has_cc()); |
| 4186 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4187 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4188 | Property* property = expression_->AsProperty(); |
| 4189 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4190 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4191 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4192 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4193 | switch (type_) { |
| 4194 | case SLOT: { |
| 4195 | Comment cmnt(masm, "[ Store to Slot"); |
| 4196 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4197 | ASSERT(slot != NULL); |
| 4198 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4199 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4200 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4201 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4202 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4203 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4204 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4205 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4206 | if (init_state == CONST_INIT) { |
| 4207 | // Same as the case for a normal store, but ignores attribute |
| 4208 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4209 | // const properties (introduced via eval("const foo = (some |
| 4210 | // expr);")). Also, uses the current function context instead of |
| 4211 | // the top context. |
| 4212 | // |
| 4213 | // Note that we must declare the foo upon entry of eval(), via a |
| 4214 | // context slot declaration, but we cannot initialize it at the |
| 4215 | // same time, because the const declaration may be at the end of |
| 4216 | // the eval code (sigh...) and the const variable may have been |
| 4217 | // used before (where its value is 'undefined'). Thus, we can only |
| 4218 | // do the initialization when we actually encounter the expression |
| 4219 | // and when the expression operands are defined and valid, and |
| 4220 | // thus we need the split into 2 operations: declaration of the |
| 4221 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4222 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4223 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4224 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4225 | } |
| 4226 | // Storing a variable must keep the (new) value on the expression |
| 4227 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4228 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4229 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4230 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4231 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4232 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4233 | JumpTarget exit; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4234 | if (init_state == CONST_INIT) { |
| 4235 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4236 | // Only the first const initialization must be executed (the slot |
| 4237 | // still contains 'the hole' value). When the assignment is |
| 4238 | // executed, the code is identical to a normal store (see below). |
| 4239 | Comment cmnt(masm, "[ Init const"); |
| 4240 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4241 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 4242 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4243 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4244 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4245 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4246 | // We must execute the store. Storing a variable must keep the |
| 4247 | // (new) value on the stack. This is necessary for compiling |
| 4248 | // assignment expressions. |
| 4249 | // |
| 4250 | // Note: We will reach here even with slot->var()->mode() == |
| 4251 | // Variable::CONST because of const declarations which will |
| 4252 | // initialize consts to 'the hole' value and by doing so, end up |
| 4253 | // calling this code. r2 may be loaded with context; used below in |
| 4254 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4255 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4256 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4257 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4258 | if (slot->type() == Slot::CONTEXT) { |
| 4259 | // Skip write barrier if the written value is a smi. |
| 4260 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4261 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4262 | // r2 is loaded with context when calling SlotOperand above. |
| 4263 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4264 | __ mov(r3, Operand(offset)); |
| 4265 | __ RecordWrite(r2, r3, r1); |
| 4266 | } |
| 4267 | // If we definitely did not jump over the assignment, we do not need |
| 4268 | // to bind the exit label. Doing so can defeat peephole |
| 4269 | // optimization. |
| 4270 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4271 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4277 | case NAMED: { |
| 4278 | Comment cmnt(masm, "[ Store to named Property"); |
| 4279 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4280 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4281 | Handle<String> name(GetName()); |
| 4282 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4283 | Result value(r0); |
| 4284 | frame->EmitPop(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4285 | |
| 4286 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4287 | Result property_name(r2); |
| 4288 | __ mov(r2, Operand(name)); |
| 4289 | frame->CallCodeObject(ic, |
| 4290 | RelocInfo::CODE_TARGET, |
| 4291 | &value, |
| 4292 | &property_name, |
| 4293 | 0); |
| 4294 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4295 | break; |
| 4296 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4297 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4298 | case KEYED: { |
| 4299 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4300 | Property* property = expression_->AsProperty(); |
| 4301 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4302 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4303 | |
| 4304 | // Call IC code. |
| 4305 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4306 | // 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] | 4307 | Result value(r0); |
| 4308 | frame->EmitPop(r0); // value |
| 4309 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4310 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4311 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4312 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4313 | |
| 4314 | default: |
| 4315 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4320 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4321 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4322 | // (31 instead of 32). |
| 4323 | static void CountLeadingZeros( |
| 4324 | MacroAssembler* masm, |
| 4325 | Register source, |
| 4326 | Register scratch, |
| 4327 | Register zeros) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4328 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4329 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4330 | #else |
| 4331 | __ mov(zeros, Operand(0)); |
| 4332 | __ mov(scratch, source); |
| 4333 | // Top 16. |
| 4334 | __ tst(scratch, Operand(0xffff0000)); |
| 4335 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4336 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4337 | // Top 8. |
| 4338 | __ tst(scratch, Operand(0xff000000)); |
| 4339 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4340 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4341 | // Top 4. |
| 4342 | __ tst(scratch, Operand(0xf0000000)); |
| 4343 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4344 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4345 | // Top 2. |
| 4346 | __ tst(scratch, Operand(0xc0000000)); |
| 4347 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4348 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4349 | // Top bit. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4350 | __ tst(scratch, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4351 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4352 | #endif |
| 4353 | } |
| 4354 | |
| 4355 | |
| 4356 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4357 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4358 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4359 | // scratch register. Destroys the source register. No GC occurs during this |
| 4360 | // stub so you don't have to set up the frame. |
| 4361 | class ConvertToDoubleStub : public CodeStub { |
| 4362 | public: |
| 4363 | ConvertToDoubleStub(Register result_reg_1, |
| 4364 | Register result_reg_2, |
| 4365 | Register source_reg, |
| 4366 | Register scratch_reg) |
| 4367 | : result1_(result_reg_1), |
| 4368 | result2_(result_reg_2), |
| 4369 | source_(source_reg), |
| 4370 | zeros_(scratch_reg) { } |
| 4371 | |
| 4372 | private: |
| 4373 | Register result1_; |
| 4374 | Register result2_; |
| 4375 | Register source_; |
| 4376 | Register zeros_; |
| 4377 | |
| 4378 | // Minor key encoding in 16 bits. |
| 4379 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4380 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4381 | |
| 4382 | Major MajorKey() { return ConvertToDouble; } |
| 4383 | int MinorKey() { |
| 4384 | // Encode the parameters in a unique 16 bit value. |
| 4385 | return result1_.code() + |
| 4386 | (result2_.code() << 4) + |
| 4387 | (source_.code() << 8) + |
| 4388 | (zeros_.code() << 12); |
| 4389 | } |
| 4390 | |
| 4391 | void Generate(MacroAssembler* masm); |
| 4392 | |
| 4393 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4394 | |
| 4395 | #ifdef DEBUG |
| 4396 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4397 | #endif |
| 4398 | }; |
| 4399 | |
| 4400 | |
| 4401 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4402 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4403 | Register exponent = result1_; |
| 4404 | Register mantissa = result2_; |
| 4405 | #else |
| 4406 | Register exponent = result2_; |
| 4407 | Register mantissa = result1_; |
| 4408 | #endif |
| 4409 | Label not_special; |
| 4410 | // Convert from Smi to integer. |
| 4411 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4412 | // Move sign bit from source to destination. This works because the sign bit |
| 4413 | // in the exponent word of the double has the same position and polarity as |
| 4414 | // the 2's complement sign bit in a Smi. |
| 4415 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4416 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4417 | // Subtract from 0 if source was negative. |
| 4418 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4419 | __ cmp(source_, Operand(1)); |
| 4420 | __ b(gt, ¬_special); |
| 4421 | |
| 4422 | // We have -1, 0 or 1, which we treat specially. |
| 4423 | __ cmp(source_, Operand(0)); |
| 4424 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4425 | static const uint32_t exponent_word_for_1 = |
| 4426 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4427 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4428 | // 1, 0 and -1 all have 0 for the second word. |
| 4429 | __ mov(mantissa, Operand(0)); |
| 4430 | __ Ret(); |
| 4431 | |
| 4432 | __ bind(¬_special); |
| 4433 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4434 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4435 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4436 | // Compute exponent and or it into the exponent register. |
| 4437 | // We use result2 as a scratch register here. |
| 4438 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4439 | __ orr(exponent, |
| 4440 | exponent, |
| 4441 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4442 | // Shift up the source chopping the top bit off. |
| 4443 | __ add(zeros_, zeros_, Operand(1)); |
| 4444 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4445 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4446 | // Compute lower part of fraction (last 12 bits). |
| 4447 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4448 | // And the top (top 20 bits). |
| 4449 | __ orr(exponent, |
| 4450 | exponent, |
| 4451 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4452 | __ Ret(); |
| 4453 | } |
| 4454 | |
| 4455 | |
| 4456 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4457 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4458 | // so you don't have to set up the frame. |
| 4459 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4460 | public: |
| 4461 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4462 | Register the_heap_number, |
| 4463 | Register scratch) |
| 4464 | : the_int_(the_int), |
| 4465 | the_heap_number_(the_heap_number), |
| 4466 | scratch_(scratch) { } |
| 4467 | |
| 4468 | private: |
| 4469 | Register the_int_; |
| 4470 | Register the_heap_number_; |
| 4471 | Register scratch_; |
| 4472 | |
| 4473 | // Minor key encoding in 16 bits. |
| 4474 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4475 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4476 | |
| 4477 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4478 | int MinorKey() { |
| 4479 | // Encode the parameters in a unique 16 bit value. |
| 4480 | return the_int_.code() + |
| 4481 | (the_heap_number_.code() << 4) + |
| 4482 | (scratch_.code() << 8); |
| 4483 | } |
| 4484 | |
| 4485 | void Generate(MacroAssembler* masm); |
| 4486 | |
| 4487 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4488 | |
| 4489 | #ifdef DEBUG |
| 4490 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4491 | #endif |
| 4492 | }; |
| 4493 | |
| 4494 | |
| 4495 | // See comment for class. |
| 4496 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) { |
| 4497 | Label max_negative_int; |
| 4498 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4499 | // We test for the special value that has a different exponent. This test |
| 4500 | // has the neat side effect of setting the flags according to the sign. |
| 4501 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4502 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4503 | __ b(eq, &max_negative_int); |
| 4504 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4505 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4506 | uint32_t non_smi_exponent = |
| 4507 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4508 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4509 | // Set the sign bit in scratch_ if the value was negative. |
| 4510 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4511 | // Subtract from 0 if the value was negative. |
| 4512 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4513 | // We should be masking the implict first digit of the mantissa away here, |
| 4514 | // but it just ends up combining harmlessly with the last digit of the |
| 4515 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4516 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4517 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4518 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4519 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4520 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4521 | HeapNumber::kExponentOffset)); |
| 4522 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4523 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4524 | HeapNumber::kMantissaOffset)); |
| 4525 | __ Ret(); |
| 4526 | |
| 4527 | __ bind(&max_negative_int); |
| 4528 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4529 | // a double because it uses a sign bit instead of using two's complement. |
| 4530 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4531 | // significant 1 bit is not stored. |
| 4532 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4533 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4534 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4535 | __ mov(ip, Operand(0)); |
| 4536 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4537 | __ Ret(); |
| 4538 | } |
| 4539 | |
| 4540 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4541 | // Handle the case where the lhs and rhs are the same object. |
| 4542 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4543 | // for "identity and not NaN". |
| 4544 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4545 | Label* slow, |
| 4546 | Condition cc) { |
| 4547 | Label not_identical; |
| 4548 | __ cmp(r0, Operand(r1)); |
| 4549 | __ b(ne, ¬_identical); |
| 4550 | |
| 4551 | Register exp_mask_reg = r5; |
| 4552 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4553 | |
| 4554 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4555 | // so we do the second best thing - test it ourselves. |
| 4556 | Label heap_number, return_equal; |
| 4557 | // They are both equal and they are not both Smis so both of them are not |
| 4558 | // Smis. If it's not a heap number, then return equal. |
| 4559 | if (cc == lt || cc == gt) { |
| 4560 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
| 4561 | __ b(ge, slow); |
| 4562 | } else { |
| 4563 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4564 | __ b(eq, &heap_number); |
| 4565 | // Comparing JS objects with <=, >= is complicated. |
| 4566 | if (cc != eq) { |
| 4567 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4568 | __ b(ge, slow); |
| 4569 | } |
| 4570 | } |
| 4571 | __ bind(&return_equal); |
| 4572 | if (cc == lt) { |
| 4573 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4574 | } else if (cc == gt) { |
| 4575 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4576 | } else { |
| 4577 | __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves. |
| 4578 | } |
| 4579 | __ mov(pc, Operand(lr)); // Return. |
| 4580 | |
| 4581 | // For less and greater we don't have to check for NaN since the result of |
| 4582 | // x < x is false regardless. For the others here is some code to check |
| 4583 | // for NaN. |
| 4584 | if (cc != lt && cc != gt) { |
| 4585 | __ bind(&heap_number); |
| 4586 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4587 | // not NaN. |
| 4588 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4589 | // and not all mantissa bits (0..51) clear. |
| 4590 | // Read top bits of double representation (second word of value). |
| 4591 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4592 | // Test that exponent bits are all set. |
| 4593 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4594 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4595 | __ b(ne, &return_equal); |
| 4596 | |
| 4597 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4598 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4599 | // Or with all low-bits of mantissa. |
| 4600 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4601 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4602 | // For equal we already have the right value in r0: Return zero (equal) |
| 4603 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if not |
| 4604 | // (it's a NaN). For <= and >= we need to load r0 with the failing value |
| 4605 | // if it's a NaN. |
| 4606 | if (cc != eq) { |
| 4607 | // All-zero means Infinity means equal. |
| 4608 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 4609 | if (cc == le) { |
| 4610 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 4611 | } else { |
| 4612 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 4613 | } |
| 4614 | } |
| 4615 | __ mov(pc, Operand(lr)); // Return. |
| 4616 | } |
| 4617 | // No fall through here. |
| 4618 | |
| 4619 | __ bind(¬_identical); |
| 4620 | } |
| 4621 | |
| 4622 | |
| 4623 | // See comment at call site. |
| 4624 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 4625 | Label* rhs_not_nan, |
| 4626 | Label* slow, |
| 4627 | bool strict) { |
| 4628 | Label lhs_is_smi; |
| 4629 | __ tst(r0, Operand(kSmiTagMask)); |
| 4630 | __ b(eq, &lhs_is_smi); |
| 4631 | |
| 4632 | // Rhs is a Smi. Check whether the non-smi is a heap number. |
| 4633 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4634 | if (strict) { |
| 4635 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4636 | // succeed. Return non-equal (r0 is already not zero) |
| 4637 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4638 | } else { |
| 4639 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4640 | // the runtime. |
| 4641 | __ b(ne, slow); |
| 4642 | } |
| 4643 | |
| 4644 | // Rhs is a smi, lhs is a number. |
| 4645 | __ push(lr); |
| 4646 | __ mov(r7, Operand(r1)); |
| 4647 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4648 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4649 | // r3 and r2 are rhs as double. |
| 4650 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4651 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4652 | // We now have both loaded as doubles but we can skip the lhs nan check |
| 4653 | // since it's a Smi. |
| 4654 | __ pop(lr); |
| 4655 | __ jmp(rhs_not_nan); |
| 4656 | |
| 4657 | __ bind(&lhs_is_smi); |
| 4658 | // Lhs is a Smi. Check whether the non-smi is a heap number. |
| 4659 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4660 | if (strict) { |
| 4661 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4662 | // succeed. Return non-equal. |
| 4663 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 4664 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4665 | } else { |
| 4666 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4667 | // the runtime. |
| 4668 | __ b(ne, slow); |
| 4669 | } |
| 4670 | |
| 4671 | // Lhs is a smi, rhs is a number. |
| 4672 | // r0 is Smi and r1 is heap number. |
| 4673 | __ push(lr); |
| 4674 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4675 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4676 | __ mov(r7, Operand(r0)); |
| 4677 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4678 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4679 | __ pop(lr); |
| 4680 | // Fall through to both_loaded_as_doubles. |
| 4681 | } |
| 4682 | |
| 4683 | |
| 4684 | void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) { |
| 4685 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4686 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4687 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4688 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4689 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4690 | Label one_is_nan, neither_is_nan; |
| 4691 | |
| 4692 | Register exp_mask_reg = r5; |
| 4693 | |
| 4694 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4695 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 4696 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4697 | __ b(ne, rhs_not_nan); |
| 4698 | __ mov(r4, |
| 4699 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4700 | SetCC); |
| 4701 | __ b(ne, &one_is_nan); |
| 4702 | __ cmp(rhs_mantissa, Operand(0)); |
| 4703 | __ b(ne, &one_is_nan); |
| 4704 | |
| 4705 | __ bind(rhs_not_nan); |
| 4706 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4707 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 4708 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4709 | __ b(ne, &neither_is_nan); |
| 4710 | __ mov(r4, |
| 4711 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4712 | SetCC); |
| 4713 | __ b(ne, &one_is_nan); |
| 4714 | __ cmp(lhs_mantissa, Operand(0)); |
| 4715 | __ b(eq, &neither_is_nan); |
| 4716 | |
| 4717 | __ bind(&one_is_nan); |
| 4718 | // NaN comparisons always fail. |
| 4719 | // Load whatever we need in r0 to make the comparison fail. |
| 4720 | if (cc == lt || cc == le) { |
| 4721 | __ mov(r0, Operand(GREATER)); |
| 4722 | } else { |
| 4723 | __ mov(r0, Operand(LESS)); |
| 4724 | } |
| 4725 | __ mov(pc, Operand(lr)); // Return. |
| 4726 | |
| 4727 | __ bind(&neither_is_nan); |
| 4728 | } |
| 4729 | |
| 4730 | |
| 4731 | // See comment at call site. |
| 4732 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 4733 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4734 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4735 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4736 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4737 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4738 | |
| 4739 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 4740 | if (cc == eq) { |
| 4741 | // Doubles are not equal unless they have the same bit pattern. |
| 4742 | // Exception: 0 and -0. |
| 4743 | __ cmp(lhs_mantissa, Operand(rhs_mantissa)); |
| 4744 | __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne); |
| 4745 | // Return non-zero if the numbers are unequal. |
| 4746 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 4747 | |
| 4748 | __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC); |
| 4749 | // If exponents are equal then return 0. |
| 4750 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 4751 | |
| 4752 | // Exponents are unequal. The only way we can return that the numbers |
| 4753 | // are equal is if one is -0 and the other is 0. We already dealt |
| 4754 | // with the case where both are -0 or both are 0. |
| 4755 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 4756 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 4757 | // equal. |
| 4758 | __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC); |
| 4759 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 4760 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 4761 | // Now they are equal if and only if the lhs exponent is zero in its |
| 4762 | // low 31 bits. |
| 4763 | __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize)); |
| 4764 | __ mov(pc, Operand(lr)); |
| 4765 | } else { |
| 4766 | // Call a native function to do a comparison between two non-NaNs. |
| 4767 | // Call C routine that may not cause GC or other trouble. |
| 4768 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 4769 | __ Jump(r5); // Tail call. |
| 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | |
| 4774 | // See comment at call site. |
| 4775 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 4776 | // If either operand is a JSObject or an oddball value, then they are |
| 4777 | // not equal since their pointers are different. |
| 4778 | // There is no test for undetectability in strict equality. |
| 4779 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 4780 | Label first_non_object; |
| 4781 | // Get the type of the first operand into r2 and compare it with |
| 4782 | // FIRST_JS_OBJECT_TYPE. |
| 4783 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 4784 | __ b(lt, &first_non_object); |
| 4785 | |
| 4786 | // Return non-zero (r0 is not zero) |
| 4787 | Label return_not_equal; |
| 4788 | __ bind(&return_not_equal); |
| 4789 | __ mov(pc, Operand(lr)); // Return. |
| 4790 | |
| 4791 | __ bind(&first_non_object); |
| 4792 | // Check for oddballs: true, false, null, undefined. |
| 4793 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 4794 | __ b(eq, &return_not_equal); |
| 4795 | |
| 4796 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 4797 | __ b(ge, &return_not_equal); |
| 4798 | |
| 4799 | // Check for oddballs: true, false, null, undefined. |
| 4800 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 4801 | __ b(eq, &return_not_equal); |
| 4802 | } |
| 4803 | |
| 4804 | |
| 4805 | // See comment at call site. |
| 4806 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 4807 | Label* both_loaded_as_doubles, |
| 4808 | Label* not_heap_numbers, |
| 4809 | Label* slow) { |
| 4810 | __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE); |
| 4811 | __ b(ne, not_heap_numbers); |
| 4812 | __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE); |
| 4813 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 4814 | |
| 4815 | // Both are heap numbers. Load them up then jump to the code we have |
| 4816 | // for that. |
| 4817 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4818 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4819 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4820 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4821 | __ jmp(both_loaded_as_doubles); |
| 4822 | } |
| 4823 | |
| 4824 | |
| 4825 | // Fast negative check for symbol-to-symbol equality. |
| 4826 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 4827 | // r2 is object type of r0. |
| 4828 | __ tst(r2, Operand(kIsNotStringMask)); |
| 4829 | __ b(ne, slow); |
| 4830 | __ tst(r2, Operand(kIsSymbolMask)); |
| 4831 | __ b(eq, slow); |
| 4832 | __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE); |
| 4833 | __ b(ge, slow); |
| 4834 | __ tst(r3, Operand(kIsSymbolMask)); |
| 4835 | __ b(eq, slow); |
| 4836 | |
| 4837 | // Both are symbols. We already checked they weren't the same pointer |
| 4838 | // so they are not equal. |
| 4839 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 4840 | __ mov(pc, Operand(lr)); // Return. |
| 4841 | } |
| 4842 | |
| 4843 | |
| 4844 | // On entry r0 and r1 are the things to be compared. On exit r0 is 0, |
| 4845 | // positive or negative to indicate the result of the comparison. |
| 4846 | void CompareStub::Generate(MacroAssembler* masm) { |
| 4847 | Label slow; // Call builtin. |
| 4848 | Label not_smis, both_loaded_as_doubles, rhs_not_nan; |
| 4849 | |
| 4850 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 4851 | // it is certain that at least one operand isn't a smi. |
| 4852 | |
| 4853 | // Handle the case where the objects are identical. Either returns the answer |
| 4854 | // or goes to slow. Only falls through if the objects were not identical. |
| 4855 | EmitIdenticalObjectComparison(masm, &slow, cc_); |
| 4856 | |
| 4857 | // If either is a Smi (we know that not both are), then they can only |
| 4858 | // be strictly equal if the other is a HeapNumber. |
| 4859 | ASSERT_EQ(0, kSmiTag); |
| 4860 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 4861 | __ and_(r2, r0, Operand(r1)); |
| 4862 | __ tst(r2, Operand(kSmiTagMask)); |
| 4863 | __ b(ne, ¬_smis); |
| 4864 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 4865 | // 1) Return the answer. |
| 4866 | // 2) Go to slow. |
| 4867 | // 3) Fall through to both_loaded_as_doubles. |
| 4868 | // 4) Jump to rhs_not_nan. |
| 4869 | // In cases 3 and 4 we have found out we were dealing with a number-number |
| 4870 | // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles. |
| 4871 | EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_); |
| 4872 | |
| 4873 | __ bind(&both_loaded_as_doubles); |
| 4874 | // r0, r1, r2, r3 are the double representations of the left hand side |
| 4875 | // and the right hand side. |
| 4876 | |
| 4877 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 4878 | // fall through if neither is a NaN. Also binds rhs_not_nan. |
| 4879 | EmitNanCheck(masm, &rhs_not_nan, cc_); |
| 4880 | |
| 4881 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 4882 | // answer. Never falls through. |
| 4883 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 4884 | |
| 4885 | __ bind(¬_smis); |
| 4886 | // At this point we know we are dealing with two different objects, |
| 4887 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 4888 | if (strict_) { |
| 4889 | // This returns non-equal for some object types, or falls through if it |
| 4890 | // was not lucky. |
| 4891 | EmitStrictTwoHeapObjectCompare(masm); |
| 4892 | } |
| 4893 | |
| 4894 | Label check_for_symbols; |
| 4895 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 4896 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 4897 | // that case. If the inputs are not doubles then jumps to check_for_symbols. |
| 4898 | // In this case r2 will contain the type of r0. |
| 4899 | EmitCheckForTwoHeapNumbers(masm, |
| 4900 | &both_loaded_as_doubles, |
| 4901 | &check_for_symbols, |
| 4902 | &slow); |
| 4903 | |
| 4904 | __ bind(&check_for_symbols); |
| 4905 | if (cc_ == eq) { |
| 4906 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 4907 | // of r0 on entry. |
| 4908 | EmitCheckForSymbols(masm, &slow); |
| 4909 | } |
| 4910 | |
| 4911 | __ bind(&slow); |
| 4912 | __ push(lr); |
| 4913 | __ push(r1); |
| 4914 | __ push(r0); |
| 4915 | // Figure out which native to call and setup the arguments. |
| 4916 | Builtins::JavaScript native; |
| 4917 | int arg_count = 1; // Not counting receiver. |
| 4918 | if (cc_ == eq) { |
| 4919 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 4920 | } else { |
| 4921 | native = Builtins::COMPARE; |
| 4922 | int ncr; // NaN compare result |
| 4923 | if (cc_ == lt || cc_ == le) { |
| 4924 | ncr = GREATER; |
| 4925 | } else { |
| 4926 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 4927 | ncr = LESS; |
| 4928 | } |
| 4929 | arg_count++; |
| 4930 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 4931 | __ push(r0); |
| 4932 | } |
| 4933 | |
| 4934 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 4935 | // tagged as a small integer. |
| 4936 | __ mov(r0, Operand(arg_count)); |
| 4937 | __ InvokeBuiltin(native, CALL_JS); |
| 4938 | __ cmp(r0, Operand(0)); |
| 4939 | __ pop(pc); |
| 4940 | } |
| 4941 | |
| 4942 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4943 | // Allocates a heap number or jumps to the label if the young space is full and |
| 4944 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4945 | static void AllocateHeapNumber( |
| 4946 | MacroAssembler* masm, |
| 4947 | Label* need_gc, // Jump here if young space is full. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4948 | Register result, // The tagged address of the new heap number. |
| 4949 | Register scratch1, // A scratch register. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4950 | Register scratch2) { // Another scratch register. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4951 | // Allocate an object in the heap for the heap number and tag it as a heap |
| 4952 | // object. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4953 | __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize, |
| 4954 | result, |
| 4955 | scratch1, |
| 4956 | scratch2, |
| 4957 | need_gc, |
| 4958 | TAG_OBJECT); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4959 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4960 | // Get heap number map and store it in the allocated object. |
| 4961 | __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex); |
| 4962 | __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | |
| 4966 | // We fall into this code if the operands were Smis, but the result was |
| 4967 | // 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] | 4968 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 4969 | // to call the C-implemented binary fp operation routines we need to end up |
| 4970 | // with the double precision floating point operands in r0 and r1 (for the |
| 4971 | // 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] | 4972 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 4973 | Label* not_smi, |
| 4974 | const Builtins::JavaScript& builtin, |
| 4975 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4976 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4977 | Label slow, slow_pop_2_first, do_the_call; |
| 4978 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 4979 | // Smi-smi case (overflow). |
| 4980 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 4981 | // The new heap number is in r5. r6 and r7 are scratch. |
| 4982 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4983 | // Write Smi from r0 to r3 and r2 in double format. r6 is scratch. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4984 | __ mov(r7, Operand(r0)); |
| 4985 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4986 | __ push(lr); |
| 4987 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4988 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 4989 | __ mov(r7, Operand(r1)); |
| 4990 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4991 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4992 | __ pop(lr); |
| 4993 | __ jmp(&do_the_call); // Tail call. No return. |
| 4994 | |
| 4995 | // We jump to here if something goes wrong (one param is not a number of any |
| 4996 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4997 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4998 | __ push(r1); |
| 4999 | __ push(r0); |
| 5000 | __ mov(r0, Operand(1)); // Set number of arguments. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5001 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5002 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5003 | // 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] | 5004 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5005 | if (mode == NO_OVERWRITE) { |
| 5006 | // In the case where there is no chance of an overwritable float we may as |
| 5007 | // well do the allocation immediately while r0 and r1 are untouched. |
| 5008 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5009 | } |
| 5010 | |
| 5011 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5012 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5013 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5014 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5015 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5016 | if (mode == OVERWRITE_RIGHT) { |
| 5017 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5018 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5019 | // Calling convention says that second double is in r2 and r3. |
| 5020 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5021 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5022 | __ jmp(&finished_loading_r0); |
| 5023 | __ bind(&r0_is_smi); |
| 5024 | if (mode == OVERWRITE_RIGHT) { |
| 5025 | // 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] | 5026 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5027 | } |
| 5028 | // Write Smi from r0 to r3 and r2 in double format. |
| 5029 | __ mov(r7, Operand(r0)); |
| 5030 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5031 | __ push(lr); |
| 5032 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5033 | __ pop(lr); |
| 5034 | __ bind(&finished_loading_r0); |
| 5035 | |
| 5036 | // Move r1 to a double in r0-r1. |
| 5037 | __ tst(r1, Operand(kSmiTagMask)); |
| 5038 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5039 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5040 | __ b(ne, &slow); |
| 5041 | if (mode == OVERWRITE_LEFT) { |
| 5042 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5043 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5044 | // Calling convention says that first double is in r0 and r1. |
| 5045 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5046 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5047 | __ jmp(&finished_loading_r1); |
| 5048 | __ bind(&r1_is_smi); |
| 5049 | if (mode == OVERWRITE_LEFT) { |
| 5050 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5051 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5052 | } |
| 5053 | // Write Smi from r1 to r1 and r0 in double format. |
| 5054 | __ mov(r7, Operand(r1)); |
| 5055 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5056 | __ push(lr); |
| 5057 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5058 | __ pop(lr); |
| 5059 | __ bind(&finished_loading_r1); |
| 5060 | |
| 5061 | __ bind(&do_the_call); |
| 5062 | // r0: Left value (least significant part of mantissa). |
| 5063 | // r1: Left value (sign, exponent, top of mantissa). |
| 5064 | // r2: Right value (least significant part of mantissa). |
| 5065 | // r3: Right value (sign, exponent, top of mantissa). |
| 5066 | // r5: Address of heap number for result. |
| 5067 | __ push(lr); // For later. |
| 5068 | __ push(r5); // Address of heap number that is answer. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5069 | __ AlignStack(0); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5070 | // Call C routine that may not cause GC or other trouble. |
| 5071 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5072 | __ Call(r5); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5073 | __ pop(r4); // Address of heap number. |
| 5074 | __ cmp(r4, Operand(Smi::FromInt(0))); |
| 5075 | __ 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] | 5076 | // Store answer in the overwritable heap number. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5077 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5078 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5079 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5080 | // substract the tag from r4. |
| 5081 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5082 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5083 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5084 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5085 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5086 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5087 | #endif |
| 5088 | __ mov(r0, Operand(r4)); |
| 5089 | // And we are done. |
| 5090 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5091 | } |
| 5092 | |
| 5093 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5094 | // 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] | 5095 | // 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] | 5096 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5097 | // 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] | 5098 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5099 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5100 | static void GetInt32(MacroAssembler* masm, |
| 5101 | Register source, |
| 5102 | Register dest, |
| 5103 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5104 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5105 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5106 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5107 | // Get exponent word. |
| 5108 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5109 | // Get exponent alone in scratch2. |
| 5110 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5111 | // Load dest with zero. We use this either for the final shift or |
| 5112 | // for the answer. |
| 5113 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5114 | // 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] | 5115 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5116 | // the exponent that we are fastest at and also the highest exponent we can |
| 5117 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5118 | const uint32_t non_smi_exponent = |
| 5119 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5120 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5121 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5122 | __ b(eq, &right_exponent); |
| 5123 | // If the exponent is higher than that then go to slow case. This catches |
| 5124 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5125 | __ b(gt, slow); |
| 5126 | |
| 5127 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5128 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5129 | // it rounds to zero. |
| 5130 | const uint32_t zero_exponent = |
| 5131 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5132 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5133 | // Dest already has a Smi zero. |
| 5134 | __ b(lt, &done); |
| 5135 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5136 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5137 | // We now have the exponent in dest. Subtract from 30 to get |
| 5138 | // how much to shift down. |
| 5139 | __ rsb(dest, dest, Operand(30)); |
| 5140 | |
| 5141 | __ bind(&right_exponent); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5142 | // Get the top bits of the mantissa. |
| 5143 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5144 | // Put back the implicit 1. |
| 5145 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5146 | // Shift up the mantissa bits to take up the space the exponent used to take. |
| 5147 | // We just orred in the implicit bit so that took care of one and we want to |
| 5148 | // leave the sign bit 0 so we subtract 2 bits from the shift distance. |
| 5149 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5150 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5151 | // Put sign in zero flag. |
| 5152 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5153 | // Get the second half of the double. For some exponents we don't actually |
| 5154 | // need this because the bits get shifted out again, but it's probably slower |
| 5155 | // to test than just to do it. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5156 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5157 | // Shift down 22 bits to get the last 10 bits. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5158 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5159 | // Move down according to the exponent. |
| 5160 | __ mov(dest, Operand(scratch, LSR, dest)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5161 | // Fix sign if sign bit was set. |
| 5162 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5163 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | |
| 5167 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5168 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5169 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5170 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5171 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5172 | // write the answer into (either by allocating or by overwriting). |
| 5173 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5174 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5175 | Label slow, result_not_a_smi; |
| 5176 | Label r0_is_smi, r1_is_smi; |
| 5177 | Label done_checking_r0, done_checking_r1; |
| 5178 | |
| 5179 | __ tst(r1, Operand(kSmiTagMask)); |
| 5180 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5181 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5182 | __ b(ne, &slow); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5183 | GetInt32(masm, r1, r3, r4, r5, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5184 | __ jmp(&done_checking_r1); |
| 5185 | __ bind(&r1_is_smi); |
| 5186 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5187 | __ bind(&done_checking_r1); |
| 5188 | |
| 5189 | __ tst(r0, Operand(kSmiTagMask)); |
| 5190 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5191 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5192 | __ b(ne, &slow); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5193 | GetInt32(masm, r0, r2, r4, r5, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5194 | __ jmp(&done_checking_r0); |
| 5195 | __ bind(&r0_is_smi); |
| 5196 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5197 | __ bind(&done_checking_r0); |
| 5198 | |
| 5199 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5200 | // r2 and r3: Signed int32 operands. |
| 5201 | switch (op_) { |
| 5202 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5203 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5204 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5205 | case Token::SAR: |
| 5206 | // Use only the 5 least significant bits of the shift count. |
| 5207 | __ and_(r2, r2, Operand(0x1f)); |
| 5208 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5209 | break; |
| 5210 | case Token::SHR: |
| 5211 | // Use only the 5 least significant bits of the shift count. |
| 5212 | __ and_(r2, r2, Operand(0x1f)); |
| 5213 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5214 | // SHR is special because it is required to produce a positive answer. |
| 5215 | // The code below for writing into heap numbers isn't capable of writing |
| 5216 | // the register as an unsigned int so we go to slow case if we hit this |
| 5217 | // case. |
| 5218 | __ b(mi, &slow); |
| 5219 | break; |
| 5220 | case Token::SHL: |
| 5221 | // Use only the 5 least significant bits of the shift count. |
| 5222 | __ and_(r2, r2, Operand(0x1f)); |
| 5223 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5224 | break; |
| 5225 | default: UNREACHABLE(); |
| 5226 | } |
| 5227 | // check that the *signed* result fits in a smi |
| 5228 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5229 | __ b(mi, &result_not_a_smi); |
| 5230 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5231 | __ Ret(); |
| 5232 | |
| 5233 | Label have_to_allocate, got_a_heap_number; |
| 5234 | __ bind(&result_not_a_smi); |
| 5235 | switch (mode_) { |
| 5236 | case OVERWRITE_RIGHT: { |
| 5237 | __ tst(r0, Operand(kSmiTagMask)); |
| 5238 | __ b(eq, &have_to_allocate); |
| 5239 | __ mov(r5, Operand(r0)); |
| 5240 | break; |
| 5241 | } |
| 5242 | case OVERWRITE_LEFT: { |
| 5243 | __ tst(r1, Operand(kSmiTagMask)); |
| 5244 | __ b(eq, &have_to_allocate); |
| 5245 | __ mov(r5, Operand(r1)); |
| 5246 | break; |
| 5247 | } |
| 5248 | case NO_OVERWRITE: { |
| 5249 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5250 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5251 | } |
| 5252 | default: break; |
| 5253 | } |
| 5254 | __ bind(&got_a_heap_number); |
| 5255 | // r2: Answer as signed int32. |
| 5256 | // r5: Heap number to write answer into. |
| 5257 | |
| 5258 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5259 | // result. |
| 5260 | __ mov(r0, Operand(r5)); |
| 5261 | |
| 5262 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5263 | // r3 as scratch. r0 is preserved and returned. |
| 5264 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5265 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5266 | |
| 5267 | if (mode_ != NO_OVERWRITE) { |
| 5268 | __ bind(&have_to_allocate); |
| 5269 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5270 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5271 | __ jmp(&got_a_heap_number); |
| 5272 | } |
| 5273 | |
| 5274 | // If all else failed then we go to the runtime system. |
| 5275 | __ bind(&slow); |
| 5276 | __ push(r1); // restore stack |
| 5277 | __ push(r0); |
| 5278 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 5279 | switch (op_) { |
| 5280 | case Token::BIT_OR: |
| 5281 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5282 | break; |
| 5283 | case Token::BIT_AND: |
| 5284 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5285 | break; |
| 5286 | case Token::BIT_XOR: |
| 5287 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5288 | break; |
| 5289 | case Token::SAR: |
| 5290 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5291 | break; |
| 5292 | case Token::SHR: |
| 5293 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5294 | break; |
| 5295 | case Token::SHL: |
| 5296 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5297 | break; |
| 5298 | default: |
| 5299 | UNREACHABLE(); |
| 5300 | } |
| 5301 | } |
| 5302 | |
| 5303 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5304 | // Can we multiply by x with max two shifts and an add. |
| 5305 | // This answers yes to all integers from 2 to 10. |
| 5306 | static bool IsEasyToMultiplyBy(int x) { |
| 5307 | if (x < 2) return false; // Avoid special cases. |
| 5308 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 5309 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 5310 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 5311 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 5312 | return false; |
| 5313 | } |
| 5314 | |
| 5315 | |
| 5316 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 5317 | // Source and destination may be the same register. This routine does |
| 5318 | // not set carry and overflow the way a mul instruction would. |
| 5319 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 5320 | Register source, |
| 5321 | Register destination, |
| 5322 | int known_int) { |
| 5323 | if (IsPowerOf2(known_int)) { |
| 5324 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 5325 | } else if (PopCountLessThanEqual2(known_int)) { |
| 5326 | int first_bit = BitPosition(known_int); |
| 5327 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 5328 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 5329 | if (first_bit != 0) { |
| 5330 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 5331 | } |
| 5332 | } else { |
| 5333 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 5334 | int the_bit = BitPosition(known_int + 1); |
| 5335 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 5336 | } |
| 5337 | } |
| 5338 | |
| 5339 | |
| 5340 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 5341 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 5342 | // a result that needs shifting. |
| 5343 | static void MultiplyByKnownInt2( |
| 5344 | MacroAssembler* masm, |
| 5345 | Register result, |
| 5346 | Register source, |
| 5347 | Register known_int_register, // Smi tagged. |
| 5348 | int known_int, |
| 5349 | int* required_shift) { // Including Smi tag shift |
| 5350 | switch (known_int) { |
| 5351 | case 3: |
| 5352 | __ add(result, source, Operand(source, LSL, 1)); |
| 5353 | *required_shift = 1; |
| 5354 | break; |
| 5355 | case 5: |
| 5356 | __ add(result, source, Operand(source, LSL, 2)); |
| 5357 | *required_shift = 1; |
| 5358 | break; |
| 5359 | case 6: |
| 5360 | __ add(result, source, Operand(source, LSL, 1)); |
| 5361 | *required_shift = 2; |
| 5362 | break; |
| 5363 | case 7: |
| 5364 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 5365 | *required_shift = 1; |
| 5366 | break; |
| 5367 | case 9: |
| 5368 | __ add(result, source, Operand(source, LSL, 3)); |
| 5369 | *required_shift = 1; |
| 5370 | break; |
| 5371 | case 10: |
| 5372 | __ add(result, source, Operand(source, LSL, 2)); |
| 5373 | *required_shift = 2; |
| 5374 | break; |
| 5375 | default: |
| 5376 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 5377 | __ mul(result, source, known_int_register); |
| 5378 | *required_shift = 0; |
| 5379 | } |
| 5380 | } |
| 5381 | |
| 5382 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5383 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5384 | // r1 : x |
| 5385 | // r0 : y |
| 5386 | // result : r0 |
| 5387 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5388 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 5389 | // tell us that. |
| 5390 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 5391 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5392 | switch (op_) { |
| 5393 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5394 | Label not_smi; |
| 5395 | // Fast path. |
| 5396 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5397 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5398 | __ b(ne, ¬_smi); |
| 5399 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 5400 | // Return if no overflow. |
| 5401 | __ Ret(vc); |
| 5402 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 5403 | |
| 5404 | HandleBinaryOpSlowCases(masm, |
| 5405 | ¬_smi, |
| 5406 | Builtins::ADD, |
| 5407 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5408 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5409 | break; |
| 5410 | } |
| 5411 | |
| 5412 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5413 | Label not_smi; |
| 5414 | // Fast path. |
| 5415 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5416 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5417 | __ b(ne, ¬_smi); |
| 5418 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 5419 | // Return if no overflow. |
| 5420 | __ Ret(vc); |
| 5421 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 5422 | |
| 5423 | HandleBinaryOpSlowCases(masm, |
| 5424 | ¬_smi, |
| 5425 | Builtins::SUB, |
| 5426 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5427 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5428 | break; |
| 5429 | } |
| 5430 | |
| 5431 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5432 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5433 | ASSERT(kSmiTag == 0); // adjust code below |
| 5434 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5435 | __ b(ne, ¬_smi); |
| 5436 | // 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] | 5437 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5438 | // Do multiplication |
| 5439 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 5440 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5441 | __ mov(ip, Operand(r3, ASR, 31)); |
| 5442 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 5443 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5444 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5445 | __ tst(r3, Operand(r3)); |
| 5446 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5447 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5448 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 5449 | // We know one of them was zero. |
| 5450 | __ add(r2, r0, Operand(r1), SetCC); |
| 5451 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 5452 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 5453 | // Slow case. We fall through here if we multiplied a negative number |
| 5454 | // with 0, because that would mean we should produce -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5455 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5456 | |
| 5457 | HandleBinaryOpSlowCases(masm, |
| 5458 | ¬_smi, |
| 5459 | Builtins::MUL, |
| 5460 | Token::MUL, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5461 | mode_); |
| 5462 | break; |
| 5463 | } |
| 5464 | |
| 5465 | case Token::DIV: |
| 5466 | case Token::MOD: { |
| 5467 | Label not_smi; |
| 5468 | if (specialized_on_rhs_) { |
| 5469 | Label smi_is_unsuitable; |
| 5470 | __ BranchOnNotSmi(r1, ¬_smi); |
| 5471 | if (IsPowerOf2(constant_rhs_)) { |
| 5472 | if (op_ == Token::MOD) { |
| 5473 | __ and_(r0, |
| 5474 | r1, |
| 5475 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 5476 | SetCC); |
| 5477 | // We now have the answer, but if the input was negative we also |
| 5478 | // have the sign bit. Our work is done if the result is |
| 5479 | // positive or zero: |
| 5480 | __ Ret(pl); |
| 5481 | // A mod of a negative left hand side must return a negative number. |
| 5482 | // Unfortunately if the answer is 0 then we must return -0. And we |
| 5483 | // already optimistically trashed r0 so we may need to restore it. |
| 5484 | __ eor(r0, r0, Operand(0x80000000u), SetCC); |
| 5485 | // Next two instructions are conditional on the answer being -0. |
| 5486 | __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
| 5487 | __ b(eq, &smi_is_unsuitable); |
| 5488 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
| 5489 | __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_))); |
| 5490 | } else { |
| 5491 | ASSERT(op_ == Token::DIV); |
| 5492 | __ tst(r1, |
| 5493 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 5494 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 5495 | int shift = 0; |
| 5496 | int d = constant_rhs_; |
| 5497 | while ((d & 1) == 0) { |
| 5498 | d >>= 1; |
| 5499 | shift++; |
| 5500 | } |
| 5501 | __ mov(r0, Operand(r1, LSR, shift)); |
| 5502 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 5503 | } |
| 5504 | } else { |
| 5505 | // Not a power of 2. |
| 5506 | __ tst(r1, Operand(0x80000000u)); |
| 5507 | __ b(ne, &smi_is_unsuitable); |
| 5508 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 5509 | // multiplying. |
| 5510 | double divisor = 1.0 / constant_rhs_; |
| 5511 | int shift = 32; |
| 5512 | double scale = 4294967296.0; // 1 << 32. |
| 5513 | uint32_t mul; |
| 5514 | // Maximise the precision of the fixed point reciprocal. |
| 5515 | while (true) { |
| 5516 | mul = static_cast<uint32_t>(scale * divisor); |
| 5517 | if (mul >= 0x7fffffff) break; |
| 5518 | scale *= 2.0; |
| 5519 | shift++; |
| 5520 | } |
| 5521 | mul++; |
| 5522 | __ mov(r2, Operand(mul)); |
| 5523 | __ umull(r3, r2, r2, r1); |
| 5524 | __ mov(r2, Operand(r2, LSR, shift - 31)); |
| 5525 | // r2 is r1 / rhs. r2 is not Smi tagged. |
| 5526 | // r0 is still the known rhs. r0 is Smi tagged. |
| 5527 | // r1 is still the unkown lhs. r1 is Smi tagged. |
| 5528 | int required_r4_shift = 0; // Including the Smi tag shift of 1. |
| 5529 | // r4 = r2 * r0. |
| 5530 | MultiplyByKnownInt2(masm, |
| 5531 | r4, |
| 5532 | r2, |
| 5533 | r0, |
| 5534 | constant_rhs_, |
| 5535 | &required_r4_shift); |
| 5536 | // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs). |
| 5537 | if (op_ == Token::DIV) { |
| 5538 | __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC); |
| 5539 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
| 5540 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5541 | } else { |
| 5542 | ASSERT(op_ == Token::MOD); |
| 5543 | __ sub(r0, r1, Operand(r4, LSL, required_r4_shift)); |
| 5544 | } |
| 5545 | } |
| 5546 | __ Ret(); |
| 5547 | __ bind(&smi_is_unsuitable); |
| 5548 | } else { |
| 5549 | __ jmp(¬_smi); |
| 5550 | } |
| 5551 | HandleBinaryOpSlowCases(masm, |
| 5552 | ¬_smi, |
| 5553 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV, |
| 5554 | op_, |
| 5555 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5556 | break; |
| 5557 | } |
| 5558 | |
| 5559 | case Token::BIT_OR: |
| 5560 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5561 | case Token::BIT_XOR: |
| 5562 | case Token::SAR: |
| 5563 | case Token::SHR: |
| 5564 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5565 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5566 | ASSERT(kSmiTag == 0); // adjust code below |
| 5567 | __ tst(r2, Operand(kSmiTagMask)); |
| 5568 | __ b(ne, &slow); |
| 5569 | switch (op_) { |
| 5570 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 5571 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 5572 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5573 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5574 | // Remove tags from right operand. |
| 5575 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5576 | // Use only the 5 least significant bits of the shift count. |
| 5577 | __ and_(r2, r2, Operand(0x1f)); |
| 5578 | __ mov(r0, Operand(r1, ASR, r2)); |
| 5579 | // Smi tag result. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5580 | __ bic(r0, r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5581 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5582 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5583 | // Remove tags from operands. We can't do this on a 31 bit number |
| 5584 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 5585 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5586 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5587 | // Use only the 5 least significant bits of the shift count. |
| 5588 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5589 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5590 | // Unsigned shift is not allowed to produce a negative number, so |
| 5591 | // check the sign bit and the sign bit after Smi tagging. |
| 5592 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5593 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5594 | // Smi tag result. |
| 5595 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5596 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5597 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5598 | // Remove tags from operands. |
| 5599 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5600 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5601 | // Use only the 5 least significant bits of the shift count. |
| 5602 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5603 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5604 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5605 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 5606 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5607 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5608 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5609 | default: UNREACHABLE(); |
| 5610 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5611 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5612 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5613 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5614 | break; |
| 5615 | } |
| 5616 | |
| 5617 | default: UNREACHABLE(); |
| 5618 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5619 | // This code should be unreachable. |
| 5620 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
| 5623 | |
| 5624 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5625 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 5626 | // argument, so give it a Smi. |
| 5627 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5628 | __ push(r0); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5629 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5630 | |
| 5631 | __ StubReturn(1); |
| 5632 | } |
| 5633 | |
| 5634 | |
| 5635 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 5636 | Label undo; |
| 5637 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5638 | Label not_smi; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5639 | |
| 5640 | // Enter runtime system if the value is not a smi. |
| 5641 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5642 | __ b(ne, ¬_smi); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5643 | |
| 5644 | // Enter runtime system if the value of the expression is zero |
| 5645 | // to make sure that we switch between 0 and -0. |
| 5646 | __ cmp(r0, Operand(0)); |
| 5647 | __ b(eq, &slow); |
| 5648 | |
| 5649 | // The value of the expression is a smi that is not zero. Try |
| 5650 | // optimistic subtraction '0 - value'. |
| 5651 | __ rsb(r1, r0, Operand(0), SetCC); |
| 5652 | __ b(vs, &slow); |
| 5653 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5654 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 5655 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5656 | |
| 5657 | // Enter runtime system. |
| 5658 | __ bind(&slow); |
| 5659 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5660 | __ mov(r0, Operand(0)); // Set number of arguments. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5661 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 5662 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5663 | __ bind(¬_smi); |
| 5664 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 5665 | __ b(ne, &slow); |
| 5666 | // r0 is a heap number. Get a new heap number in r1. |
| 5667 | if (overwrite_) { |
| 5668 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5669 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5670 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5671 | } else { |
| 5672 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5673 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5674 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5675 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5676 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5677 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 5678 | __ mov(r0, Operand(r1)); |
| 5679 | } |
| 5680 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5681 | } |
| 5682 | |
| 5683 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5684 | int CEntryStub::MinorKey() { |
| 5685 | ASSERT(result_size_ <= 2); |
| 5686 | // Result returned in r0 or r0+r1 by default. |
| 5687 | return 0; |
| 5688 | } |
| 5689 | |
| 5690 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5691 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5692 | // r0 holds the exception. |
| 5693 | |
| 5694 | // Adjust this code if not the case. |
| 5695 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5696 | |
| 5697 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5698 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 5699 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5700 | |
| 5701 | // Restore the next handler and frame pointer, discard handler state. |
| 5702 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5703 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5704 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5705 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5706 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 5707 | |
| 5708 | // Before returning we restore the context from the frame pointer if |
| 5709 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5710 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5711 | __ cmp(fp, Operand(0)); |
| 5712 | // Set cp to NULL if fp is NULL. |
| 5713 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5714 | // Restore cp otherwise. |
| 5715 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5716 | #ifdef DEBUG |
| 5717 | if (FLAG_debug_code) { |
| 5718 | __ mov(lr, Operand(pc)); |
| 5719 | } |
| 5720 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5721 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5722 | __ pop(pc); |
| 5723 | } |
| 5724 | |
| 5725 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5726 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 5727 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5728 | // Adjust this code if not the case. |
| 5729 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5730 | |
| 5731 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5732 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5733 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5734 | |
| 5735 | // Unwind the handlers until the ENTRY handler is found. |
| 5736 | Label loop, done; |
| 5737 | __ bind(&loop); |
| 5738 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5739 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 5740 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5741 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 5742 | __ b(eq, &done); |
| 5743 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5744 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 5745 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5746 | __ jmp(&loop); |
| 5747 | __ bind(&done); |
| 5748 | |
| 5749 | // 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] | 5750 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5751 | __ pop(r2); |
| 5752 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5753 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5754 | if (type == OUT_OF_MEMORY) { |
| 5755 | // Set external caught exception to false. |
| 5756 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 5757 | __ mov(r0, Operand(false)); |
| 5758 | __ mov(r2, Operand(external_caught)); |
| 5759 | __ str(r0, MemOperand(r2)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5760 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5761 | // Set pending exception and r0 to out of memory exception. |
| 5762 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5763 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5764 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 5765 | __ str(r0, MemOperand(r2)); |
| 5766 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5767 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5768 | // Stack layout at this point. See also StackHandlerConstants. |
| 5769 | // sp -> state (ENTRY) |
| 5770 | // fp |
| 5771 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5772 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5773 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 5774 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5775 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 5776 | // Before returning we restore the context from the frame pointer if |
| 5777 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5778 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5779 | __ cmp(fp, Operand(0)); |
| 5780 | // Set cp to NULL if fp is NULL. |
| 5781 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5782 | // Restore cp otherwise. |
| 5783 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5784 | #ifdef DEBUG |
| 5785 | if (FLAG_debug_code) { |
| 5786 | __ mov(lr, Operand(pc)); |
| 5787 | } |
| 5788 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5789 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5790 | __ pop(pc); |
| 5791 | } |
| 5792 | |
| 5793 | |
| 5794 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 5795 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5796 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5797 | Label* throw_out_of_memory_exception, |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 5798 | ExitFrame::Mode mode, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5799 | bool do_gc, |
| 5800 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5801 | // r0: result parameter for PerformGC, if any |
| 5802 | // r4: number of arguments including receiver (C callee-saved) |
| 5803 | // r5: pointer to builtin function (C callee-saved) |
| 5804 | // r6: pointer to the first argument (C callee-saved) |
| 5805 | |
| 5806 | if (do_gc) { |
| 5807 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5808 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 5809 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5812 | ExternalReference scope_depth = |
| 5813 | ExternalReference::heap_always_allocate_scope_depth(); |
| 5814 | if (always_allocate) { |
| 5815 | __ mov(r0, Operand(scope_depth)); |
| 5816 | __ ldr(r1, MemOperand(r0)); |
| 5817 | __ add(r1, r1, Operand(1)); |
| 5818 | __ str(r1, MemOperand(r0)); |
| 5819 | } |
| 5820 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5821 | // Call C built-in. |
| 5822 | // r0 = argc, r1 = argv |
| 5823 | __ mov(r0, Operand(r4)); |
| 5824 | __ mov(r1, Operand(r6)); |
| 5825 | |
| 5826 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 5827 | // frames, we need to know where the return address is. Right now, |
| 5828 | // we push it on the stack to be able to find it again, but we never |
| 5829 | // restore from it in case of changes, which makes it impossible to |
| 5830 | // support moving the C entry code stub. This should be fixed, but currently |
| 5831 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 5832 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5833 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 5834 | masm->push(lr); |
| 5835 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5836 | |
| 5837 | if (always_allocate) { |
| 5838 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 5839 | // though (contain the result). |
| 5840 | __ mov(r2, Operand(scope_depth)); |
| 5841 | __ ldr(r3, MemOperand(r2)); |
| 5842 | __ sub(r3, r3, Operand(1)); |
| 5843 | __ str(r3, MemOperand(r2)); |
| 5844 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5845 | |
| 5846 | // check for failure result |
| 5847 | Label failure_returned; |
| 5848 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 5849 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 5850 | __ add(r2, r0, Operand(1)); |
| 5851 | __ tst(r2, Operand(kFailureTagMask)); |
| 5852 | __ b(eq, &failure_returned); |
| 5853 | |
| 5854 | // Exit C frame and return. |
| 5855 | // r0:r1: result |
| 5856 | // sp: stack pointer |
| 5857 | // fp: frame pointer |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 5858 | __ LeaveExitFrame(mode); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5859 | |
| 5860 | // check if we should retry or throw exception |
| 5861 | Label retry; |
| 5862 | __ bind(&failure_returned); |
| 5863 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 5864 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 5865 | __ b(eq, &retry); |
| 5866 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5867 | // Special handling of out of memory exceptions. |
| 5868 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5869 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5870 | __ b(eq, throw_out_of_memory_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5871 | |
| 5872 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5873 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5874 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5875 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5876 | __ ldr(r0, MemOperand(ip)); |
| 5877 | __ str(r3, MemOperand(ip)); |
| 5878 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5879 | // Special handling of termination exceptions which are uncatchable |
| 5880 | // by javascript code. |
| 5881 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 5882 | __ b(eq, throw_termination_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5883 | |
| 5884 | // Handle normal exception. |
| 5885 | __ jmp(throw_normal_exception); |
| 5886 | |
| 5887 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 5888 | } |
| 5889 | |
| 5890 | |
| 5891 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 5892 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 5893 | // r0: number of arguments including receiver |
| 5894 | // r1: pointer to builtin function |
| 5895 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5896 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5897 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5898 | |
| 5899 | // NOTE: Invocations of builtins may return failure objects |
| 5900 | // instead of a proper result. The builtin entry handles |
| 5901 | // this by performing a garbage collection and retrying the |
| 5902 | // builtin once. |
| 5903 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 5904 | ExitFrame::Mode mode = is_debug_break |
| 5905 | ? ExitFrame::MODE_DEBUG |
| 5906 | : ExitFrame::MODE_NORMAL; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5907 | |
| 5908 | // Enter the exit frame that transitions from JavaScript to C++. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 5909 | __ EnterExitFrame(mode); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5910 | |
| 5911 | // r4: number of arguments (C callee-saved) |
| 5912 | // r5: pointer to builtin function (C callee-saved) |
| 5913 | // r6: pointer to first argument (C callee-saved) |
| 5914 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5915 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5916 | Label throw_termination_exception; |
| 5917 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5918 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 5919 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5920 | GenerateCore(masm, |
| 5921 | &throw_normal_exception, |
| 5922 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5923 | &throw_out_of_memory_exception, |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 5924 | mode, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 5925 | false, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5926 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5927 | |
| 5928 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5929 | GenerateCore(masm, |
| 5930 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5931 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5932 | &throw_out_of_memory_exception, |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 5933 | mode, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5934 | true, |
| 5935 | false); |
| 5936 | |
| 5937 | // Do full GC and retry runtime call one final time. |
| 5938 | Failure* failure = Failure::InternalError(); |
| 5939 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 5940 | GenerateCore(masm, |
| 5941 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5942 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5943 | &throw_out_of_memory_exception, |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame^] | 5944 | mode, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5945 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5946 | true); |
| 5947 | |
| 5948 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5949 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 5950 | |
| 5951 | __ bind(&throw_termination_exception); |
| 5952 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5953 | |
| 5954 | __ bind(&throw_normal_exception); |
| 5955 | GenerateThrowTOS(masm); |
| 5956 | } |
| 5957 | |
| 5958 | |
| 5959 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 5960 | // r0: code entry |
| 5961 | // r1: function |
| 5962 | // r2: receiver |
| 5963 | // r3: argc |
| 5964 | // [sp+0]: argv |
| 5965 | |
| 5966 | Label invoke, exit; |
| 5967 | |
| 5968 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 5969 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5970 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5971 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 5972 | |
| 5973 | // Get address of argv, see stm above. |
| 5974 | // r0: code entry |
| 5975 | // r1: function |
| 5976 | // r2: receiver |
| 5977 | // r3: argc |
| 5978 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 5979 | __ ldr(r4, MemOperand(r4)); // argv |
| 5980 | |
| 5981 | // Push a frame with special values setup to mark it as an entry frame. |
| 5982 | // r0: code entry |
| 5983 | // r1: function |
| 5984 | // r2: receiver |
| 5985 | // r3: argc |
| 5986 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5987 | __ 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] | 5988 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 5989 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5990 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 5991 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5992 | __ ldr(r5, MemOperand(r5)); |
| 5993 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 5994 | |
| 5995 | // Setup frame pointer for the frame to be pushed. |
| 5996 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5997 | |
| 5998 | // Call a faked try-block that does the invoke. |
| 5999 | __ bl(&invoke); |
| 6000 | |
| 6001 | // Caught exception: Store result (exception) in the pending |
| 6002 | // exception field in the JSEnv and return a failure sentinel. |
| 6003 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 6004 | // 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] | 6005 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6006 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6007 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6008 | __ b(&exit); |
| 6009 | |
| 6010 | // Invoke: Link this frame into the handler chain. |
| 6011 | __ bind(&invoke); |
| 6012 | // Must preserve r0-r4, r5-r7 are available. |
| 6013 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6014 | // If an exception not caught by another handler occurs, this handler |
| 6015 | // returns control to the code after the bl(&invoke) above, which |
| 6016 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 6017 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6018 | |
| 6019 | // Clear any pending exceptions. |
| 6020 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6021 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6022 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6023 | __ str(r5, MemOperand(ip)); |
| 6024 | |
| 6025 | // Invoke the function by calling through JS entry trampoline builtin. |
| 6026 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6027 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6028 | |
| 6029 | // Expected registers by Builtins::JSEntryTrampoline |
| 6030 | // r0: code entry |
| 6031 | // r1: function |
| 6032 | // r2: receiver |
| 6033 | // r3: argc |
| 6034 | // r4: argv |
| 6035 | if (is_construct) { |
| 6036 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6037 | __ mov(ip, Operand(construct_entry)); |
| 6038 | } else { |
| 6039 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6040 | __ mov(ip, Operand(entry)); |
| 6041 | } |
| 6042 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6043 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6044 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6045 | // macro for the add instruction because we don't want the coverage tool |
| 6046 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6047 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6048 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6049 | |
| 6050 | // Unlink this frame from the handler chain. When reading the |
| 6051 | // address of the next handler, there is no need to use the address |
| 6052 | // displacement since the current stack pointer (sp) points directly |
| 6053 | // to the stack handler. |
| 6054 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6055 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6056 | __ str(r3, MemOperand(ip)); |
| 6057 | // No need to restore registers |
| 6058 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6059 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6060 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6061 | __ bind(&exit); // r0 holds result |
| 6062 | // Restore the top frame descriptors from the stack. |
| 6063 | __ pop(r3); |
| 6064 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6065 | __ str(r3, MemOperand(ip)); |
| 6066 | |
| 6067 | // Reset the stack to the callee saved registers. |
| 6068 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6069 | |
| 6070 | // Restore callee-saved registers and return. |
| 6071 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6072 | if (FLAG_debug_code) { |
| 6073 | __ mov(lr, Operand(pc)); |
| 6074 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6075 | #endif |
| 6076 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6077 | } |
| 6078 | |
| 6079 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6080 | // This stub performs an instanceof, calling the builtin function if |
| 6081 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6082 | // be an instance of (these are fetched from the stack). |
| 6083 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6084 | // Get the object - slow case for smis (we may need to throw an exception |
| 6085 | // depending on the rhs). |
| 6086 | Label slow, loop, is_instance, is_not_instance; |
| 6087 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6088 | __ BranchOnSmi(r0, &slow); |
| 6089 | |
| 6090 | // Check that the left hand is a JS object and put map in r3. |
| 6091 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6092 | __ b(lt, &slow); |
| 6093 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6094 | __ b(gt, &slow); |
| 6095 | |
| 6096 | // Get the prototype of the function (r4 is result, r2 is scratch). |
| 6097 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 6098 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6099 | |
| 6100 | // Check that the function prototype is a JS object. |
| 6101 | __ BranchOnSmi(r4, &slow); |
| 6102 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6103 | __ b(lt, &slow); |
| 6104 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6105 | __ b(gt, &slow); |
| 6106 | |
| 6107 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6108 | // Get prototype of object into r2. |
| 6109 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6110 | |
| 6111 | // Loop through the prototype chain looking for the function prototype. |
| 6112 | __ bind(&loop); |
| 6113 | __ cmp(r2, Operand(r4)); |
| 6114 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6115 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 6116 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6117 | __ b(eq, &is_not_instance); |
| 6118 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6119 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6120 | __ jmp(&loop); |
| 6121 | |
| 6122 | __ bind(&is_instance); |
| 6123 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6124 | __ pop(); |
| 6125 | __ pop(); |
| 6126 | __ mov(pc, Operand(lr)); // Return. |
| 6127 | |
| 6128 | __ bind(&is_not_instance); |
| 6129 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6130 | __ pop(); |
| 6131 | __ pop(); |
| 6132 | __ mov(pc, Operand(lr)); // Return. |
| 6133 | |
| 6134 | // Slow-case. Tail call builtin. |
| 6135 | __ bind(&slow); |
| 6136 | __ mov(r0, Operand(1)); // Arg count without receiver. |
| 6137 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6138 | } |
| 6139 | |
| 6140 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6141 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6142 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6143 | Label adaptor; |
| 6144 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6145 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6146 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6147 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6148 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6149 | // Nothing to do: The formal number of parameters has already been |
| 6150 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6151 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6152 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6153 | // Arguments adaptor case: Read the arguments length from the |
| 6154 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6155 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6156 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6157 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6158 | } |
| 6159 | |
| 6160 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6161 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6162 | // The displacement is the offset of the last parameter (if any) |
| 6163 | // relative to the frame pointer. |
| 6164 | static const int kDisplacement = |
| 6165 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6166 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6167 | // Check that the key is a smi. |
| 6168 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6169 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6170 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6171 | // Check if the calling frame is an arguments adaptor frame. |
| 6172 | Label adaptor; |
| 6173 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6174 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6175 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6176 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6177 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6178 | // Check index against formal parameters count limit passed in |
| 6179 | // through register eax. Use unsigned comparison to get negative |
| 6180 | // check for free. |
| 6181 | __ cmp(r1, r0); |
| 6182 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6183 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6184 | // Read the argument from the stack and return it. |
| 6185 | __ sub(r3, r0, r1); |
| 6186 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6187 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6188 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6189 | |
| 6190 | // Arguments adaptor case: Check index against actual arguments |
| 6191 | // limit found in the arguments adaptor frame. Use unsigned |
| 6192 | // comparison to get negative check for free. |
| 6193 | __ bind(&adaptor); |
| 6194 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6195 | __ cmp(r1, r0); |
| 6196 | __ b(cs, &slow); |
| 6197 | |
| 6198 | // Read the argument from the adaptor frame and return it. |
| 6199 | __ sub(r3, r0, r1); |
| 6200 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6201 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6202 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6203 | |
| 6204 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6205 | // by calling the runtime system. |
| 6206 | __ bind(&slow); |
| 6207 | __ push(r1); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6208 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6209 | } |
| 6210 | |
| 6211 | |
| 6212 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 6213 | // Check if the calling frame is an arguments adaptor frame. |
| 6214 | Label runtime; |
| 6215 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6216 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6217 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6218 | __ b(ne, &runtime); |
| 6219 | |
| 6220 | // Patch the arguments.length and the parameters pointer. |
| 6221 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6222 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 6223 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6224 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6225 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6226 | |
| 6227 | // Do the runtime call to allocate the arguments object. |
| 6228 | __ bind(&runtime); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6229 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | |
| 6233 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6234 | Label slow; |
| 6235 | // Get the function to call from the stack. |
| 6236 | // function, receiver [, arguments] |
| 6237 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6238 | |
| 6239 | // Check that the function is really a JavaScript function. |
| 6240 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6241 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6242 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6243 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6244 | __ b(ne, &slow); |
| 6245 | |
| 6246 | // Fast-case: Invoke the function now. |
| 6247 | // r1: pushed function |
| 6248 | ParameterCount actual(argc_); |
| 6249 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6250 | |
| 6251 | // Slow-case: Non-function called. |
| 6252 | __ bind(&slow); |
| 6253 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6254 | __ mov(r2, Operand(0)); |
| 6255 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6256 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6257 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
| 6260 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6261 | int CompareStub::MinorKey() { |
| 6262 | // Encode the two parameters in a unique 16 bit value. |
| 6263 | ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15)); |
| 6264 | return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0); |
| 6265 | } |
| 6266 | |
| 6267 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6268 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6269 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6270 | } } // namespace v8::internal |