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), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 136 | function_return_is_shadowed_(false), |
| 137 | in_spilled_code_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 142 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 144 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | // cp: callee's context |
| 146 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 147 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | ZoneList<Statement*>* body = fun->body(); |
| 149 | |
| 150 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 151 | ASSERT(scope_ == NULL); |
| 152 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 153 | ASSERT(allocator_ == NULL); |
| 154 | RegisterAllocator register_allocator(this); |
| 155 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 156 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 157 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 158 | cc_reg_ = al; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 159 | set_in_spilled_code(false); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 160 | { |
| 161 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 163 | // Entry: |
| 164 | // Stack: receiver, arguments |
| 165 | // lr: return address |
| 166 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 168 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 169 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 170 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 171 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | // tos: code slot |
| 173 | #ifdef DEBUG |
| 174 | if (strlen(FLAG_stop_at) > 0 && |
| 175 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 176 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 177 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | } |
| 179 | #endif |
| 180 | |
| 181 | // Allocate space for locals and initialize them. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 182 | frame_->AllocateStackSlots(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 183 | // Initialize the function return target after the locals are set |
| 184 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 185 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 186 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 188 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 189 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 190 | // Allocate local context. |
| 191 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 192 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 193 | frame_->EmitPush(r0); |
| 194 | frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 195 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 196 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 197 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 198 | __ cmp(r0, Operand(cp)); |
| 199 | verified_true.Branch(eq); |
| 200 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 201 | verified_true.Bind(); |
| 202 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 204 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 205 | } |
| 206 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 207 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 208 | // 1) only needed if we have a context |
| 209 | // 2) no need to recompute context ptr every single time |
| 210 | // 3) don't copy parameter operand code from SlotOperand! |
| 211 | { |
| 212 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 213 | |
| 214 | // Note that iteration order is relevant here! If we have the same |
| 215 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 216 | // needs to be copied into the context, it must be the last argument |
| 217 | // passed to the parameter that needs to be copied. This is a rare |
| 218 | // case so we don't check for it, instead we rely on the copying |
| 219 | // order: such a parameter is copied repeatedly into the same |
| 220 | // context location and thus the last value is what is seen inside |
| 221 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 222 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 223 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | Slot* slot = par->slot(); |
| 225 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 226 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 227 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | // Loads r2 with context; used below in RecordWrite. |
| 229 | __ str(r1, SlotOperand(slot, r2)); |
| 230 | // Load the offset into r3. |
| 231 | int slot_offset = |
| 232 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 233 | __ mov(r3, Operand(slot_offset)); |
| 234 | __ RecordWrite(r2, r3, r1); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 239 | // Store the arguments object. This must happen after context |
| 240 | // initialization because the arguments object may be stored in the |
| 241 | // context. |
| 242 | if (scope_->arguments() != NULL) { |
| 243 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 245 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 246 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 247 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 248 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 249 | // The receiver is below the arguments, the return address, |
| 250 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 251 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 252 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 253 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 254 | frame_->Adjust(3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 255 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 256 | frame_->CallStub(&stub, 3); |
| 257 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 258 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 259 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 260 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 261 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 262 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 263 | } |
| 264 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 265 | // Generate code to 'execute' declarations and initialize functions |
| 266 | // (source elements). In case of an illegal redeclaration we need to |
| 267 | // handle that instead of processing the declarations. |
| 268 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 270 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | } else { |
| 272 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 273 | ProcessDeclarations(scope_->declarations()); |
| 274 | // Bail out if a stack-overflow exception occurred when processing |
| 275 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 276 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | } |
| 278 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 279 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 280 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 281 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 282 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | CheckStack(); |
| 284 | |
| 285 | // Compile the body of the function in a vanilla state. Don't |
| 286 | // bother compiling all the code if the scope has an illegal |
| 287 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 288 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 289 | Comment cmnt(masm_, "[ function body"); |
| 290 | #ifdef DEBUG |
| 291 | bool is_builtin = Bootstrapper::IsActive(); |
| 292 | bool should_trace = |
| 293 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 294 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 295 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 296 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 297 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 299 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | } |
| 302 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 303 | // Generate the return sequence if necessary. |
| 304 | if (frame_ != NULL || function_return_.is_linked()) { |
| 305 | // exit |
| 306 | // r0: result |
| 307 | // sp: stack pointer |
| 308 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 309 | // cp: callee's context |
| 310 | __ mov(r0, Operand(Factory::undefined_value())); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 311 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 312 | function_return_.Bind(); |
| 313 | if (FLAG_trace) { |
| 314 | // Push the return value on the stack as the parameter. |
| 315 | // Runtime::TraceExit returns the parameter as it is. |
| 316 | frame_->EmitPush(r0); |
| 317 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 318 | } |
| 319 | |
| 320 | // Tear down the frame which will restore the caller's frame pointer and |
| 321 | // the link register. |
| 322 | frame_->Exit(); |
| 323 | |
| 324 | __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 325 | __ Jump(lr); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 326 | } |
| 327 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 328 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 329 | ASSERT(!has_cc()); |
| 330 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 331 | ASSERT(!function_return_is_shadowed_); |
| 332 | function_return_.Unuse(); |
| 333 | DeleteFrame(); |
| 334 | |
| 335 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 336 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 337 | ProcessDeferred(); |
| 338 | } |
| 339 | |
| 340 | allocator_ = NULL; |
| 341 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 345 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 346 | // Currently, this assertion will fail if we try to assign to |
| 347 | // a constant variable that is constant because it is read-only |
| 348 | // (such as the variable referring to a named function expression). |
| 349 | // We need to implement assignments to read-only variables. |
| 350 | // Ideally, we should do this during AST generation (by converting |
| 351 | // such assignments into expression statements); however, in general |
| 352 | // we may not be able to make the decision until past AST generation, |
| 353 | // that is when the entire program is known. |
| 354 | ASSERT(slot != NULL); |
| 355 | int index = slot->index(); |
| 356 | switch (slot->type()) { |
| 357 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 358 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 359 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 360 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 361 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 362 | |
| 363 | case Slot::CONTEXT: { |
| 364 | // Follow the context chain if necessary. |
| 365 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 366 | Register context = cp; |
| 367 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 368 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 369 | // Load the closure. |
| 370 | // (All contexts, even 'with' contexts, have a closure, |
| 371 | // and it is the same for all contexts inside a function. |
| 372 | // There is no need to go to the function context first.) |
| 373 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 374 | // Load the function context (which is the incoming, outer context). |
| 375 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 376 | context = tmp; |
| 377 | } |
| 378 | // We may have a 'with' context now. Get the function context. |
| 379 | // (In fact this mov may never be the needed, since the scope analysis |
| 380 | // may not permit a direct context access in this case and thus we are |
| 381 | // always at a function context. However it is safe to dereference be- |
| 382 | // cause the function context of a function context is itself. Before |
| 383 | // deleting this mov we should try to create a counter-example first, |
| 384 | // though...) |
| 385 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 386 | return ContextOperand(tmp, index); |
| 387 | } |
| 388 | |
| 389 | default: |
| 390 | UNREACHABLE(); |
| 391 | return MemOperand(r0, 0); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 396 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 397 | Slot* slot, |
| 398 | Register tmp, |
| 399 | Register tmp2, |
| 400 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 401 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 402 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 403 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 404 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 405 | if (s->num_heap_slots() > 0) { |
| 406 | if (s->calls_eval()) { |
| 407 | // Check that extension is NULL. |
| 408 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 409 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 410 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 411 | } |
| 412 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 413 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 414 | context = tmp; |
| 415 | } |
| 416 | } |
| 417 | // Check that last extension is NULL. |
| 418 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 419 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 420 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 421 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 422 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 426 | void CodeGenerator::LoadConditionAndSpill(Expression* expression, |
| 427 | TypeofState typeof_state, |
| 428 | JumpTarget* true_target, |
| 429 | JumpTarget* false_target, |
| 430 | bool force_control) { |
| 431 | ASSERT(in_spilled_code()); |
| 432 | set_in_spilled_code(false); |
| 433 | LoadCondition(expression, typeof_state, true_target, false_target, |
| 434 | force_control); |
| 435 | if (frame_ != NULL) { |
| 436 | frame_->SpillAll(); |
| 437 | } |
| 438 | set_in_spilled_code(true); |
| 439 | } |
| 440 | |
| 441 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 442 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 443 | // (partially) translated into branches, or it may have set the condition |
| 444 | // code register. If force_cc is set, the value is forced to set the |
| 445 | // condition code register and no value is pushed. If the condition code |
| 446 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 447 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 448 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 449 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 450 | JumpTarget* true_target, |
| 451 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 452 | bool force_cc) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 453 | ASSERT(!in_spilled_code()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 454 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 455 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 456 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 457 | { CodeGenState new_state(this, typeof_state, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 458 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 459 | |
| 460 | // If we hit a stack overflow, we may not have actually visited |
| 461 | // the expression. In that case, we ensure that we have a |
| 462 | // valid-looking frame state because we will continue to generate |
| 463 | // code as we unwind the C++ stack. |
| 464 | // |
| 465 | // It's possible to have both a stack overflow and a valid frame |
| 466 | // state (eg, a subexpression overflowed, visiting it returned |
| 467 | // with a dummied frame state, and visiting this expression |
| 468 | // returned with a normal-looking state). |
| 469 | if (HasStackOverflow() && |
| 470 | has_valid_frame() && |
| 471 | !has_cc() && |
| 472 | frame_->height() == original_height) { |
| 473 | true_target->Jump(); |
| 474 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 475 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 476 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 477 | // Convert the TOS value to a boolean in the condition code register. |
| 478 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 480 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 481 | ASSERT(!has_valid_frame() || |
| 482 | (has_cc() && frame_->height() == original_height) || |
| 483 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 487 | void CodeGenerator::LoadAndSpill(Expression* expression, |
| 488 | TypeofState typeof_state) { |
| 489 | ASSERT(in_spilled_code()); |
| 490 | set_in_spilled_code(false); |
| 491 | Load(expression, typeof_state); |
| 492 | frame_->SpillAll(); |
| 493 | set_in_spilled_code(true); |
| 494 | } |
| 495 | |
| 496 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 497 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 498 | #ifdef DEBUG |
| 499 | int original_height = frame_->height(); |
| 500 | #endif |
| 501 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 502 | JumpTarget true_target; |
| 503 | JumpTarget false_target; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 504 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 505 | |
| 506 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 507 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 508 | JumpTarget loaded; |
| 509 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 510 | materialize_true.Branch(cc_reg_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 511 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 512 | frame_->EmitPush(r0); |
| 513 | loaded.Jump(); |
| 514 | materialize_true.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 515 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 516 | frame_->EmitPush(r0); |
| 517 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | cc_reg_ = al; |
| 519 | } |
| 520 | |
| 521 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 522 | // We have at least one condition value that has been "translated" |
| 523 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 524 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 525 | if (frame_ != NULL) { |
| 526 | loaded.Jump(); // Don't lose the current TOS. |
| 527 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 528 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 529 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 530 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 531 | true_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 532 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 533 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 534 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 535 | // If both "true" and "false" need to be loaded jump across the code for |
| 536 | // "false". |
| 537 | if (both) { |
| 538 | loaded.Jump(); |
| 539 | } |
| 540 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 541 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 542 | false_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 543 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 544 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 546 | // A value is loaded on all paths reaching this point. |
| 547 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 548 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 549 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 551 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 555 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 556 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 557 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 558 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 562 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 563 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 564 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 565 | __ ldr(scratch, |
| 566 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 567 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | // 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] | 572 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 573 | // variables w/o reference errors elsewhere. |
| 574 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 575 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 576 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 577 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 578 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 579 | // the variable as if through '<global>.<variable>' to make sure we |
| 580 | // do not get reference errors. |
| 581 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 582 | Literal key(variable->name()); |
| 583 | // TODO(1241834): Fetch the position from the variable instead of using |
| 584 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 585 | Property property(&global, &key, RelocInfo::kNoPosition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 586 | LoadAndSpill(&property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 587 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 588 | LoadAndSpill(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 593 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 594 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 595 | cgen->LoadReference(this); |
| 596 | } |
| 597 | |
| 598 | |
| 599 | Reference::~Reference() { |
| 600 | cgen_->UnloadReference(this); |
| 601 | } |
| 602 | |
| 603 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 604 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 605 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 606 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | Expression* e = ref->expression(); |
| 608 | Property* property = e->AsProperty(); |
| 609 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 610 | |
| 611 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 612 | // The expression is either a property or a variable proxy that rewrites |
| 613 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 614 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 615 | // We use a named reference if the key is a literal symbol, unless it is |
| 616 | // a string that can be legally parsed as an integer. This is because |
| 617 | // otherwise we will not get into the slow case code that handles [] on |
| 618 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 619 | Literal* literal = property->key()->AsLiteral(); |
| 620 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 621 | if (literal != NULL && |
| 622 | literal->handle()->IsSymbol() && |
| 623 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 624 | ref->set_type(Reference::NAMED); |
| 625 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 626 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | ref->set_type(Reference::KEYED); |
| 628 | } |
| 629 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 630 | // The expression is a variable proxy that does not rewrite to a |
| 631 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 632 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 633 | LoadGlobal(); |
| 634 | ref->set_type(Reference::NAMED); |
| 635 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 636 | ASSERT(var->slot() != NULL); |
| 637 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 638 | } |
| 639 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 640 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 641 | LoadAndSpill(e); |
| 642 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
| 646 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 647 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 648 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 649 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 650 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 651 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 652 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 653 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 654 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 655 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 660 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 661 | // register to a boolean in the condition code register. The code |
| 662 | // 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] | 663 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 664 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 665 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 666 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 667 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 668 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | |
| 670 | // Fast case checks |
| 671 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 672 | // Check if the value is 'false'. |
| 673 | __ cmp(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 674 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 675 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 676 | // Check if the value is 'true'. |
| 677 | __ cmp(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 678 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 679 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 680 | // Check if the value is 'undefined'. |
| 681 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 682 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 683 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 684 | // Check if the value is a smi. |
| 685 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 686 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 687 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 688 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 689 | |
| 690 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 691 | frame_->EmitPush(r0); |
| 692 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 693 | // Convert the result (r0) to a condition code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 694 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 695 | |
| 696 | cc_reg_ = ne; |
| 697 | } |
| 698 | |
| 699 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 700 | class GenericBinaryOpStub : public CodeStub { |
| 701 | public: |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 702 | GenericBinaryOpStub(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 703 | OverwriteMode mode, |
| 704 | int constant_rhs = CodeGenerator::kUnknownIntValue) |
| 705 | : op_(op), |
| 706 | mode_(mode), |
| 707 | constant_rhs_(constant_rhs), |
| 708 | specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)) { } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 709 | |
| 710 | private: |
| 711 | Token::Value op_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 712 | OverwriteMode mode_; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 713 | int constant_rhs_; |
| 714 | bool specialized_on_rhs_; |
| 715 | |
| 716 | static const int kMaxKnownRhs = 0x40000000; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 717 | |
| 718 | // Minor key encoding in 16 bits. |
| 719 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 720 | class OpBits: public BitField<Token::Value, 2, 6> {}; |
| 721 | class KnownIntBits: public BitField<int, 8, 8> {}; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 722 | |
| 723 | Major MajorKey() { return GenericBinaryOp; } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 724 | int MinorKey() { |
| 725 | // Encode the parameters in a unique 16 bit value. |
| 726 | return OpBits::encode(op_) |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 727 | | ModeBits::encode(mode_) |
| 728 | | KnownIntBits::encode(MinorKeyForKnownInt()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 729 | } |
| 730 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 731 | void Generate(MacroAssembler* masm); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 732 | void HandleNonSmiBitwiseOp(MacroAssembler* masm); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 733 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 734 | static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) { |
| 735 | if (constant_rhs == CodeGenerator::kUnknownIntValue) return false; |
| 736 | if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3; |
| 737 | if (op == Token::MOD) { |
| 738 | if (constant_rhs <= 1) return false; |
| 739 | if (constant_rhs <= 10) return true; |
| 740 | if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true; |
| 741 | return false; |
| 742 | } |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | int MinorKeyForKnownInt() { |
| 747 | if (!specialized_on_rhs_) return 0; |
| 748 | if (constant_rhs_ <= 10) return constant_rhs_ + 1; |
| 749 | ASSERT(IsPowerOf2(constant_rhs_)); |
| 750 | int key = 12; |
| 751 | int d = constant_rhs_; |
| 752 | while ((d & 1) == 0) { |
| 753 | key++; |
| 754 | d >>= 1; |
| 755 | } |
| 756 | return key; |
| 757 | } |
| 758 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 759 | const char* GetName() { |
| 760 | switch (op_) { |
| 761 | case Token::ADD: return "GenericBinaryOpStub_ADD"; |
| 762 | case Token::SUB: return "GenericBinaryOpStub_SUB"; |
| 763 | case Token::MUL: return "GenericBinaryOpStub_MUL"; |
| 764 | case Token::DIV: return "GenericBinaryOpStub_DIV"; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 765 | case Token::MOD: return "GenericBinaryOpStub_MOD"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 766 | case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR"; |
| 767 | case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND"; |
| 768 | case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR"; |
| 769 | case Token::SAR: return "GenericBinaryOpStub_SAR"; |
| 770 | case Token::SHL: return "GenericBinaryOpStub_SHL"; |
| 771 | case Token::SHR: return "GenericBinaryOpStub_SHR"; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 772 | default: return "GenericBinaryOpStub"; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | #ifdef DEBUG |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 777 | void Print() { |
| 778 | if (!specialized_on_rhs_) { |
| 779 | PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); |
| 780 | } else { |
| 781 | PrintF("GenericBinaryOpStub (%s by %d)\n", |
| 782 | Token::String(op_), |
| 783 | constant_rhs_); |
| 784 | } |
| 785 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 786 | #endif |
| 787 | }; |
| 788 | |
| 789 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 790 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 791 | OverwriteMode overwrite_mode, |
| 792 | int constant_rhs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 793 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 794 | // sp[0] : y |
| 795 | // sp[1] : x |
| 796 | // result : r0 |
| 797 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 798 | // Stub is entered with a call: 'return address' is in lr. |
| 799 | switch (op) { |
| 800 | case Token::ADD: // fall through. |
| 801 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 802 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 803 | case Token::DIV: |
| 804 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 805 | case Token::BIT_OR: |
| 806 | case Token::BIT_AND: |
| 807 | case Token::BIT_XOR: |
| 808 | case Token::SHL: |
| 809 | case Token::SHR: |
| 810 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 811 | frame_->EmitPop(r0); // r0 : y |
| 812 | frame_->EmitPop(r1); // r1 : x |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 813 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 814 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 815 | break; |
| 816 | } |
| 817 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 818 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 819 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 820 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 821 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 822 | break; |
| 823 | |
| 824 | default: |
| 825 | // Other cases should have been handled before this point. |
| 826 | UNREACHABLE(); |
| 827 | break; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 832 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 833 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 834 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 835 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 836 | bool reversed, |
| 837 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 838 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 839 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 840 | reversed_(reversed), |
| 841 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 842 | set_comment("[ DeferredInlinedSmiOperation"); |
| 843 | } |
| 844 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 845 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 846 | |
| 847 | private: |
| 848 | Token::Value op_; |
| 849 | int value_; |
| 850 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 851 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 852 | }; |
| 853 | |
| 854 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 855 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 856 | switch (op_) { |
| 857 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 858 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 859 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 860 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 861 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 862 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 863 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 864 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 865 | } |
| 866 | break; |
| 867 | } |
| 868 | |
| 869 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 870 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 871 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 872 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 873 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 874 | } else { |
| 875 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 876 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 877 | } |
| 878 | break; |
| 879 | } |
| 880 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 881 | // For these operations there is no optimistic operation that needs to be |
| 882 | // reverted. |
| 883 | case Token::MUL: |
| 884 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 885 | case Token::BIT_OR: |
| 886 | case Token::BIT_XOR: |
| 887 | case Token::BIT_AND: { |
| 888 | if (reversed_) { |
| 889 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 890 | } else { |
| 891 | __ mov(r1, Operand(r0)); |
| 892 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 893 | } |
| 894 | break; |
| 895 | } |
| 896 | |
| 897 | case Token::SHL: |
| 898 | case Token::SHR: |
| 899 | case Token::SAR: { |
| 900 | if (!reversed_) { |
| 901 | __ mov(r1, Operand(r0)); |
| 902 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 903 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 904 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 905 | } |
| 906 | break; |
| 907 | } |
| 908 | |
| 909 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 910 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 911 | UNREACHABLE(); |
| 912 | break; |
| 913 | } |
| 914 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 915 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 916 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 920 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 921 | x &= x - 1; |
| 922 | return (x & (x - 1)) == 0; |
| 923 | } |
| 924 | |
| 925 | |
| 926 | // Returns the index of the lowest bit set. |
| 927 | static int BitPosition(unsigned x) { |
| 928 | int bit_posn = 0; |
| 929 | while ((x & 0xf) == 0) { |
| 930 | bit_posn += 4; |
| 931 | x >>= 4; |
| 932 | } |
| 933 | while ((x & 1) == 0) { |
| 934 | bit_posn++; |
| 935 | x >>= 1; |
| 936 | } |
| 937 | return bit_posn; |
| 938 | } |
| 939 | |
| 940 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 941 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 942 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 943 | bool reversed, |
| 944 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 945 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 946 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 947 | // some possible smi operations (like + and -) when (at least) one |
| 948 | // of the operands is a literal smi. With this optimization, the |
| 949 | // performance of the system is increased by ~15%, and the generated |
| 950 | // code size is increased by ~1% (measured on a combination of |
| 951 | // different benchmarks). |
| 952 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 953 | // sp[0] : operand |
| 954 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 955 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 956 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 957 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 958 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 959 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 960 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 961 | switch (op) { |
| 962 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 963 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 964 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 965 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 966 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 967 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 968 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 969 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 970 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 971 | break; |
| 972 | } |
| 973 | |
| 974 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 975 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 976 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 977 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 978 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 979 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 980 | } else { |
| 981 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 982 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 983 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 984 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 985 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 986 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 987 | break; |
| 988 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 989 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 990 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 991 | case Token::BIT_OR: |
| 992 | case Token::BIT_XOR: |
| 993 | case Token::BIT_AND: { |
| 994 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 995 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 996 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 997 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 998 | switch (op) { |
| 999 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 1000 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 1001 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 1002 | default: UNREACHABLE(); |
| 1003 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1004 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1005 | break; |
| 1006 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1007 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1008 | case Token::SHL: |
| 1009 | case Token::SHR: |
| 1010 | case Token::SAR: { |
| 1011 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 1012 | something_to_inline = false; |
| 1013 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1014 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 1015 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 1016 | DeferredCode* deferred = |
| 1017 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
| 1018 | __ tst(r0, Operand(kSmiTagMask)); |
| 1019 | deferred->Branch(ne); |
| 1020 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 1021 | switch (op) { |
| 1022 | case Token::SHL: { |
| 1023 | if (shift_value != 0) { |
| 1024 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 1025 | } |
| 1026 | // check that the *unsigned* result fits in a smi |
| 1027 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 1028 | deferred->Branch(mi); |
| 1029 | break; |
| 1030 | } |
| 1031 | case Token::SHR: { |
| 1032 | // LSR by immediate 0 means shifting 32 bits. |
| 1033 | if (shift_value != 0) { |
| 1034 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 1035 | } |
| 1036 | // check that the *unsigned* result fits in a smi |
| 1037 | // neither of the two high-order bits can be set: |
| 1038 | // - 0x80000000: high bit would be lost when smi tagging |
| 1039 | // - 0x40000000: this number would convert to negative when |
| 1040 | // smi tagging these two cases can only happen with shifts |
| 1041 | // by 0 or 1 when handed a valid smi |
| 1042 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 1043 | deferred->Branch(ne); |
| 1044 | break; |
| 1045 | } |
| 1046 | case Token::SAR: { |
| 1047 | if (shift_value != 0) { |
| 1048 | // ASR by immediate 0 means shifting 32 bits. |
| 1049 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 1050 | } |
| 1051 | break; |
| 1052 | } |
| 1053 | default: UNREACHABLE(); |
| 1054 | } |
| 1055 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 1056 | deferred->BindExit(); |
| 1057 | break; |
| 1058 | } |
| 1059 | |
| 1060 | case Token::MOD: { |
| 1061 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 1062 | something_to_inline = false; |
| 1063 | break; |
| 1064 | } |
| 1065 | DeferredCode* deferred = |
| 1066 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1067 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1068 | __ tst(r0, Operand(mask)); |
| 1069 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1070 | mask = (int_value << kSmiTagSize) - 1; |
| 1071 | __ and_(r0, r0, Operand(mask)); |
| 1072 | deferred->BindExit(); |
| 1073 | break; |
| 1074 | } |
| 1075 | |
| 1076 | case Token::MUL: { |
| 1077 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1078 | something_to_inline = false; |
| 1079 | break; |
| 1080 | } |
| 1081 | DeferredCode* deferred = |
| 1082 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 1083 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1084 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1085 | unsigned mask = 0x80000000u; |
| 1086 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1087 | mask |= mask >> 1; |
| 1088 | } |
| 1089 | mask |= kSmiTagMask; |
| 1090 | // This does a single mask that checks for a too high value in a |
| 1091 | // conservative way and for a non-Smi. It also filters out negative |
| 1092 | // numbers, unfortunately, but since this code is inline we prefer |
| 1093 | // brevity to comprehensiveness. |
| 1094 | __ tst(r0, Operand(mask)); |
| 1095 | deferred->Branch(ne); |
| 1096 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 1097 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | break; |
| 1099 | } |
| 1100 | |
| 1101 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 1102 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1103 | break; |
| 1104 | } |
| 1105 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 1106 | if (!something_to_inline) { |
| 1107 | if (!reversed) { |
| 1108 | frame_->EmitPush(r0); |
| 1109 | __ mov(r0, Operand(value)); |
| 1110 | frame_->EmitPush(r0); |
| 1111 | GenericBinaryOperation(op, mode, int_value); |
| 1112 | } else { |
| 1113 | __ mov(ip, Operand(value)); |
| 1114 | frame_->EmitPush(ip); |
| 1115 | frame_->EmitPush(r0); |
| 1116 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1117 | } |
| 1118 | } |
| 1119 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1120 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1124 | void CodeGenerator::Comparison(Condition cc, |
| 1125 | Expression* left, |
| 1126 | Expression* right, |
| 1127 | bool strict) { |
| 1128 | if (left != NULL) LoadAndSpill(left); |
| 1129 | if (right != NULL) LoadAndSpill(right); |
| 1130 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1131 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1132 | // sp[0] : y |
| 1133 | // sp[1] : x |
| 1134 | // result : cc register |
| 1135 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1136 | // Strict only makes sense for equality comparisons. |
| 1137 | ASSERT(!strict || cc == eq); |
| 1138 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1139 | JumpTarget exit; |
| 1140 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1141 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1142 | if (cc == gt || cc == le) { |
| 1143 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1144 | frame_->EmitPop(r1); |
| 1145 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1146 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1147 | frame_->EmitPop(r0); |
| 1148 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1149 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1150 | __ orr(r2, r0, Operand(r1)); |
| 1151 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1152 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1153 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1154 | // Perform non-smi comparison by stub. |
| 1155 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1156 | // We call with 0 args because there are 0 on the stack. |
| 1157 | CompareStub stub(cc, strict); |
| 1158 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1159 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1160 | Result result = allocator_->Allocate(r0); |
| 1161 | ASSERT(result.is_valid()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1162 | __ cmp(result.reg(), Operand(0)); |
| 1163 | result.Unuse(); |
| 1164 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1165 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1166 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1167 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1168 | __ cmp(r1, Operand(r0)); |
| 1169 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1170 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1171 | cc_reg_ = cc; |
| 1172 | } |
| 1173 | |
| 1174 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1175 | class CallFunctionStub: public CodeStub { |
| 1176 | public: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1177 | CallFunctionStub(int argc, InLoopFlag in_loop) |
| 1178 | : argc_(argc), in_loop_(in_loop) {} |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1179 | |
| 1180 | void Generate(MacroAssembler* masm); |
| 1181 | |
| 1182 | private: |
| 1183 | int argc_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1184 | InLoopFlag in_loop_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1185 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1186 | #if defined(DEBUG) |
| 1187 | void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); } |
| 1188 | #endif // defined(DEBUG) |
| 1189 | |
| 1190 | Major MajorKey() { return CallFunction; } |
| 1191 | int MinorKey() { return argc_; } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1192 | InLoopFlag InLoop() { return in_loop_; } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1193 | }; |
| 1194 | |
| 1195 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1196 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1197 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1198 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1199 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1200 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1201 | int arg_count = args->length(); |
| 1202 | for (int i = 0; i < arg_count; i++) { |
| 1203 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1204 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1205 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1206 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1207 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1208 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1209 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1210 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 1211 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1212 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1213 | |
| 1214 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1215 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1216 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1220 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1221 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1222 | ASSERT(has_cc()); |
| 1223 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1224 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1225 | cc_reg_ = al; |
| 1226 | } |
| 1227 | |
| 1228 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1229 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1230 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1231 | if (FLAG_check_stack) { |
| 1232 | Comment cmnt(masm_, "[ check stack"); |
| 1233 | StackCheckStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1234 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1239 | void CodeGenerator::VisitAndSpill(Statement* statement) { |
| 1240 | ASSERT(in_spilled_code()); |
| 1241 | set_in_spilled_code(false); |
| 1242 | Visit(statement); |
| 1243 | if (frame_ != NULL) { |
| 1244 | frame_->SpillAll(); |
| 1245 | } |
| 1246 | set_in_spilled_code(true); |
| 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) { |
| 1251 | ASSERT(in_spilled_code()); |
| 1252 | set_in_spilled_code(false); |
| 1253 | VisitStatements(statements); |
| 1254 | if (frame_ != NULL) { |
| 1255 | frame_->SpillAll(); |
| 1256 | } |
| 1257 | set_in_spilled_code(true); |
| 1258 | } |
| 1259 | |
| 1260 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1261 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1262 | #ifdef DEBUG |
| 1263 | int original_height = frame_->height(); |
| 1264 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1265 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1266 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1267 | VisitAndSpill(statements->at(i)); |
| 1268 | } |
| 1269 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1270 | } |
| 1271 | |
| 1272 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1273 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1274 | #ifdef DEBUG |
| 1275 | int original_height = frame_->height(); |
| 1276 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1277 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1278 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1279 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1280 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1281 | VisitStatementsAndSpill(node->statements()); |
| 1282 | if (node->break_target()->is_linked()) { |
| 1283 | node->break_target()->Bind(); |
| 1284 | } |
| 1285 | node->break_target()->Unuse(); |
| 1286 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1290 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1291 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1292 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1293 | frame_->EmitPush(r0); |
| 1294 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1295 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1296 | frame_->EmitPush(r0); |
| 1297 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1298 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1302 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1303 | #ifdef DEBUG |
| 1304 | int original_height = frame_->height(); |
| 1305 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1306 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1307 | Comment cmnt(masm_, "[ Declaration"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1308 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1309 | Variable* var = node->proxy()->var(); |
| 1310 | ASSERT(var != NULL); // must have been resolved |
| 1311 | Slot* slot = var->slot(); |
| 1312 | |
| 1313 | // If it was not possible to allocate the variable at compile time, |
| 1314 | // we need to "declare" it at runtime to make sure it actually |
| 1315 | // exists in the local context. |
| 1316 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1317 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1318 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1319 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1320 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1321 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1322 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1323 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1324 | // Declaration nodes are always declared in only two modes. |
| 1325 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1326 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1327 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1328 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1329 | // Push initial value, if any. |
| 1330 | // Note: For variables we must not push an initial value (such as |
| 1331 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1332 | // must not destroy the current value. |
| 1333 | if (node->mode() == Variable::CONST) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1334 | __ mov(r0, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1335 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1336 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1337 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1338 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1339 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1340 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1341 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1342 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1343 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1344 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1345 | return; |
| 1346 | } |
| 1347 | |
| 1348 | ASSERT(!var->is_global()); |
| 1349 | |
| 1350 | // If we have a function or a constant, we need to initialize the variable. |
| 1351 | Expression* val = NULL; |
| 1352 | if (node->mode() == Variable::CONST) { |
| 1353 | val = new Literal(Factory::the_hole_value()); |
| 1354 | } else { |
| 1355 | val = node->fun(); // NULL if we don't have a function |
| 1356 | } |
| 1357 | |
| 1358 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1359 | { |
| 1360 | // Set initial value. |
| 1361 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1362 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1363 | target.SetValue(NOT_CONST_INIT); |
| 1364 | // The reference is removed from the stack (preserving TOS) when |
| 1365 | // it goes out of scope. |
| 1366 | } |
| 1367 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1368 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1369 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1370 | ASSERT(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::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1375 | #ifdef DEBUG |
| 1376 | int original_height = frame_->height(); |
| 1377 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1378 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1379 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1380 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1381 | Expression* expression = node->expression(); |
| 1382 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1383 | LoadAndSpill(expression); |
| 1384 | frame_->Drop(); |
| 1385 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1389 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1390 | #ifdef DEBUG |
| 1391 | int original_height = frame_->height(); |
| 1392 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1393 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1394 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1395 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1396 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1397 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1401 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1402 | #ifdef DEBUG |
| 1403 | int original_height = frame_->height(); |
| 1404 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1405 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1406 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1407 | // Generate different code depending on which parts of the if statement |
| 1408 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1409 | bool has_then_stm = node->HasThenStatement(); |
| 1410 | bool has_else_stm = node->HasElseStatement(); |
| 1411 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1412 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1413 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1414 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1415 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1416 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1417 | JumpTarget then; |
| 1418 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1419 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1420 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1421 | &then, &else_, true); |
| 1422 | if (frame_ != NULL) { |
| 1423 | Branch(false, &else_); |
| 1424 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1425 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1426 | if (frame_ != NULL || then.is_linked()) { |
| 1427 | then.Bind(); |
| 1428 | VisitAndSpill(node->then_statement()); |
| 1429 | } |
| 1430 | if (frame_ != NULL) { |
| 1431 | exit.Jump(); |
| 1432 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1433 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1434 | if (else_.is_linked()) { |
| 1435 | else_.Bind(); |
| 1436 | VisitAndSpill(node->else_statement()); |
| 1437 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1438 | |
| 1439 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1440 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1441 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1442 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1443 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1444 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1445 | &then, &exit, true); |
| 1446 | if (frame_ != NULL) { |
| 1447 | Branch(false, &exit); |
| 1448 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1450 | if (frame_ != NULL || then.is_linked()) { |
| 1451 | then.Bind(); |
| 1452 | VisitAndSpill(node->then_statement()); |
| 1453 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1454 | |
| 1455 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1456 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1457 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1458 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1459 | // if (!cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1460 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1461 | &exit, &else_, true); |
| 1462 | if (frame_ != NULL) { |
| 1463 | Branch(true, &exit); |
| 1464 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1465 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1466 | if (frame_ != NULL || else_.is_linked()) { |
| 1467 | else_.Bind(); |
| 1468 | VisitAndSpill(node->else_statement()); |
| 1469 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1470 | |
| 1471 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1472 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1473 | ASSERT(!has_then_stm && !has_else_stm); |
| 1474 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1475 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1476 | &exit, &exit, false); |
| 1477 | if (frame_ != NULL) { |
| 1478 | if (has_cc()) { |
| 1479 | cc_reg_ = al; |
| 1480 | } else { |
| 1481 | frame_->Drop(); |
| 1482 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1487 | if (exit.is_linked()) { |
| 1488 | exit.Bind(); |
| 1489 | } |
| 1490 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1494 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1495 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1496 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1497 | CodeForStatementPosition(node); |
| 1498 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1502 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1503 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1504 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1505 | CodeForStatementPosition(node); |
| 1506 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1510 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1511 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1512 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1513 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1514 | if (function_return_is_shadowed_) { |
| 1515 | CodeForStatementPosition(node); |
| 1516 | LoadAndSpill(node->expression()); |
| 1517 | frame_->EmitPop(r0); |
| 1518 | function_return_.Jump(); |
| 1519 | } else { |
| 1520 | // Load the returned value. |
| 1521 | CodeForStatementPosition(node); |
| 1522 | LoadAndSpill(node->expression()); |
| 1523 | |
| 1524 | // Pop the result from the frame and prepare the frame for |
| 1525 | // returning thus making it easier to merge. |
| 1526 | frame_->EmitPop(r0); |
| 1527 | frame_->PrepareForReturn(); |
| 1528 | |
| 1529 | function_return_.Jump(); |
| 1530 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1534 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1535 | #ifdef DEBUG |
| 1536 | int original_height = frame_->height(); |
| 1537 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1538 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1539 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1540 | CodeForStatementPosition(node); |
| 1541 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1542 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1543 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1544 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1545 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1546 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1547 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1548 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1549 | __ cmp(r0, Operand(cp)); |
| 1550 | verified_true.Branch(eq); |
| 1551 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1552 | verified_true.Bind(); |
| 1553 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1554 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1555 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1556 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1560 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1561 | #ifdef DEBUG |
| 1562 | int original_height = frame_->height(); |
| 1563 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1564 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1565 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1566 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1567 | // Pop context. |
| 1568 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1569 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1570 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1571 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1575 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1576 | #ifdef DEBUG |
| 1577 | int original_height = frame_->height(); |
| 1578 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1579 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1580 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1581 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1582 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1583 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1584 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1585 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1586 | JumpTarget next_test; |
| 1587 | JumpTarget fall_through; |
| 1588 | JumpTarget default_entry; |
| 1589 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1590 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1591 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1592 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1593 | |
| 1594 | for (int i = 0; i < length; i++) { |
| 1595 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1596 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1597 | // Remember the default clause and compile it at the end. |
| 1598 | default_clause = clause; |
| 1599 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1602 | Comment cmnt(masm_, "[ Case clause"); |
| 1603 | // Compile the test. |
| 1604 | next_test.Bind(); |
| 1605 | next_test.Unuse(); |
| 1606 | // Duplicate TOS. |
| 1607 | __ ldr(r0, frame_->Top()); |
| 1608 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1609 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1610 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1611 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1612 | // Before entering the body from the test, remove the switch value from |
| 1613 | // the stack. |
| 1614 | frame_->Drop(); |
| 1615 | |
| 1616 | // Label the body so that fall through is enabled. |
| 1617 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1618 | default_exit.Bind(); |
| 1619 | } else { |
| 1620 | fall_through.Bind(); |
| 1621 | fall_through.Unuse(); |
| 1622 | } |
| 1623 | VisitStatementsAndSpill(clause->statements()); |
| 1624 | |
| 1625 | // If control flow can fall through from the body, jump to the next body |
| 1626 | // or the end of the statement. |
| 1627 | if (frame_ != NULL) { |
| 1628 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1629 | default_entry.Jump(); |
| 1630 | } else { |
| 1631 | fall_through.Jump(); |
| 1632 | } |
| 1633 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1636 | // The final "test" removes the switch value. |
| 1637 | next_test.Bind(); |
| 1638 | frame_->Drop(); |
| 1639 | |
| 1640 | // If there is a default clause, compile it. |
| 1641 | if (default_clause != NULL) { |
| 1642 | Comment cmnt(masm_, "[ Default clause"); |
| 1643 | default_entry.Bind(); |
| 1644 | VisitStatementsAndSpill(default_clause->statements()); |
| 1645 | // If control flow can fall out of the default and there is a case after |
| 1646 | // it, jup to that case's body. |
| 1647 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1648 | default_exit.Jump(); |
| 1649 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1650 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1651 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1652 | if (fall_through.is_linked()) { |
| 1653 | fall_through.Bind(); |
| 1654 | } |
| 1655 | |
| 1656 | if (node->break_target()->is_linked()) { |
| 1657 | node->break_target()->Bind(); |
| 1658 | } |
| 1659 | node->break_target()->Unuse(); |
| 1660 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1664 | void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1665 | #ifdef DEBUG |
| 1666 | int original_height = frame_->height(); |
| 1667 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1668 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1669 | Comment cmnt(masm_, "[ LoopStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1670 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1671 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1672 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1673 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 1674 | // known result for the test expression, with no side effects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1675 | enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
| 1676 | if (node->cond() == NULL) { |
| 1677 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1678 | info = ALWAYS_TRUE; |
| 1679 | } else { |
| 1680 | Literal* lit = node->cond()->AsLiteral(); |
| 1681 | if (lit != NULL) { |
| 1682 | if (lit->IsTrue()) { |
| 1683 | info = ALWAYS_TRUE; |
| 1684 | } else if (lit->IsFalse()) { |
| 1685 | info = ALWAYS_FALSE; |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1690 | switch (node->type()) { |
| 1691 | case LoopStatement::DO_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1692 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1693 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1694 | // Label the top of the loop for the backward CFG edge. If the test |
| 1695 | // is always true we can use the continue target, and if the test is |
| 1696 | // always false there is no need. |
| 1697 | if (info == ALWAYS_TRUE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1698 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1699 | node->continue_target()->Bind(); |
| 1700 | } else if (info == ALWAYS_FALSE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1701 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1702 | } else { |
| 1703 | ASSERT(info == DONT_KNOW); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1704 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1705 | body.Bind(); |
| 1706 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1707 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1708 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1709 | VisitAndSpill(node->body()); |
| 1710 | |
| 1711 | // Compile the test. |
| 1712 | if (info == ALWAYS_TRUE) { |
| 1713 | if (has_valid_frame()) { |
| 1714 | // If control can fall off the end of the body, jump back to the |
| 1715 | // top. |
| 1716 | node->continue_target()->Jump(); |
| 1717 | } |
| 1718 | } else if (info == ALWAYS_FALSE) { |
| 1719 | // If we have a continue in the body, we only have to bind its jump |
| 1720 | // target. |
| 1721 | if (node->continue_target()->is_linked()) { |
| 1722 | node->continue_target()->Bind(); |
| 1723 | } |
| 1724 | } else { |
| 1725 | ASSERT(info == DONT_KNOW); |
| 1726 | // We have to compile the test expression if it can be reached by |
| 1727 | // control flow falling out of the body or via continue. |
| 1728 | if (node->continue_target()->is_linked()) { |
| 1729 | node->continue_target()->Bind(); |
| 1730 | } |
| 1731 | if (has_valid_frame()) { |
| 1732 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1733 | &body, node->break_target(), true); |
| 1734 | if (has_valid_frame()) { |
| 1735 | // A invalid frame here indicates that control did not |
| 1736 | // fall out of the test expression. |
| 1737 | Branch(true, &body); |
| 1738 | } |
| 1739 | } |
| 1740 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1741 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | case LoopStatement::WHILE_LOOP: { |
| 1745 | // If the test is never true and has no side effects there is no need |
| 1746 | // to compile the test or body. |
| 1747 | if (info == ALWAYS_FALSE) break; |
| 1748 | |
| 1749 | // Label the top of the loop with the continue target for the backward |
| 1750 | // CFG edge. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1751 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1752 | node->continue_target()->Bind(); |
| 1753 | |
| 1754 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1755 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1756 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1757 | &body, node->break_target(), true); |
| 1758 | if (has_valid_frame()) { |
| 1759 | // A NULL frame indicates that control did not fall out of the |
| 1760 | // test expression. |
| 1761 | Branch(false, node->break_target()); |
| 1762 | } |
| 1763 | if (has_valid_frame() || body.is_linked()) { |
| 1764 | body.Bind(); |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | if (has_valid_frame()) { |
| 1769 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1770 | VisitAndSpill(node->body()); |
| 1771 | |
| 1772 | // If control flow can fall out of the body, jump back to the top. |
| 1773 | if (has_valid_frame()) { |
| 1774 | node->continue_target()->Jump(); |
| 1775 | } |
| 1776 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1777 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | case LoopStatement::FOR_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1781 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1782 | |
| 1783 | if (node->init() != NULL) { |
| 1784 | VisitAndSpill(node->init()); |
| 1785 | } |
| 1786 | |
| 1787 | // There is no need to compile the test or body. |
| 1788 | if (info == ALWAYS_FALSE) break; |
| 1789 | |
| 1790 | // If there is no update statement, label the top of the loop with the |
| 1791 | // continue target, otherwise with the loop target. |
| 1792 | if (node->next() == NULL) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1793 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1794 | node->continue_target()->Bind(); |
| 1795 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1796 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1797 | loop.Bind(); |
| 1798 | } |
| 1799 | |
| 1800 | // If the test is always true, there is no need to compile it. |
| 1801 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1802 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1803 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1804 | &body, node->break_target(), true); |
| 1805 | if (has_valid_frame()) { |
| 1806 | Branch(false, node->break_target()); |
| 1807 | } |
| 1808 | if (has_valid_frame() || body.is_linked()) { |
| 1809 | body.Bind(); |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | if (has_valid_frame()) { |
| 1814 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1815 | VisitAndSpill(node->body()); |
| 1816 | |
| 1817 | if (node->next() == NULL) { |
| 1818 | // If there is no update statement and control flow can fall out |
| 1819 | // of the loop, jump directly to the continue label. |
| 1820 | if (has_valid_frame()) { |
| 1821 | node->continue_target()->Jump(); |
| 1822 | } |
| 1823 | } else { |
| 1824 | // If there is an update statement and control flow can reach it |
| 1825 | // via falling out of the body of the loop or continuing, we |
| 1826 | // compile the update statement. |
| 1827 | if (node->continue_target()->is_linked()) { |
| 1828 | node->continue_target()->Bind(); |
| 1829 | } |
| 1830 | if (has_valid_frame()) { |
| 1831 | // Record source position of the statement as this code which is |
| 1832 | // after the code for the body actually belongs to the loop |
| 1833 | // statement and not the body. |
| 1834 | CodeForStatementPosition(node); |
| 1835 | VisitAndSpill(node->next()); |
| 1836 | loop.Jump(); |
| 1837 | } |
| 1838 | } |
| 1839 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1840 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1841 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1844 | if (node->break_target()->is_linked()) { |
| 1845 | node->break_target()->Bind(); |
| 1846 | } |
| 1847 | node->continue_target()->Unuse(); |
| 1848 | node->break_target()->Unuse(); |
| 1849 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1853 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1854 | #ifdef DEBUG |
| 1855 | int original_height = frame_->height(); |
| 1856 | #endif |
| 1857 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1858 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1860 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1861 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1862 | JumpTarget primitive; |
| 1863 | JumpTarget jsobject; |
| 1864 | JumpTarget fixed_array; |
| 1865 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1866 | JumpTarget end_del_check; |
| 1867 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | |
| 1869 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1870 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1871 | |
| 1872 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1873 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1874 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1875 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1876 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1877 | __ cmp(r0, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1878 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1879 | |
| 1880 | // Stack layout in body: |
| 1881 | // [iteration counter (Smi)] |
| 1882 | // [length of array] |
| 1883 | // [FixedArray] |
| 1884 | // [Map or 0] |
| 1885 | // [Object] |
| 1886 | |
| 1887 | // Check if enumerable is already a JSObject |
| 1888 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1889 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1890 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1891 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1892 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1893 | primitive.Bind(); |
| 1894 | frame_->EmitPush(r0); |
| 1895 | Result arg_count = allocator_->Allocate(r0); |
| 1896 | ASSERT(arg_count.is_valid()); |
| 1897 | __ mov(arg_count.reg(), Operand(0)); |
| 1898 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1899 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1900 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1901 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1902 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1903 | frame_->EmitPush(r0); |
| 1904 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1905 | |
| 1906 | // If we got a Map, we can do a fast modification check. |
| 1907 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1908 | __ mov(r2, Operand(r0)); |
| 1909 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1910 | __ cmp(r1, Operand(Factory::meta_map())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1911 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1912 | |
| 1913 | // Get enum cache |
| 1914 | __ mov(r1, Operand(r0)); |
| 1915 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1916 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1917 | __ ldr(r2, |
| 1918 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1919 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1920 | frame_->EmitPush(r0); // map |
| 1921 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1922 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1923 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1924 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1925 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1926 | frame_->EmitPush(r0); |
| 1927 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1928 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1929 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1930 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1931 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1932 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1933 | |
| 1934 | // 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] | 1935 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1936 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1937 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1938 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1939 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1940 | |
| 1941 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1942 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1943 | // sp[0] : index |
| 1944 | // sp[1] : array/enum cache length |
| 1945 | // sp[2] : array or enum cache |
| 1946 | // sp[3] : 0 or map |
| 1947 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1948 | // Grab the current frame's height for the break and continue |
| 1949 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1950 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1951 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1952 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1953 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1954 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1955 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1956 | node->break_target()->Branch(hs); |
| 1957 | |
| 1958 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1959 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1960 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1961 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1962 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1963 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1964 | |
| 1965 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1966 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1967 | // Check if this (still) matches the map of the enumerable. |
| 1968 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1969 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1970 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1971 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1972 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1973 | |
| 1974 | // 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] | 1975 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1976 | frame_->EmitPush(r0); |
| 1977 | frame_->EmitPush(r3); // push entry |
| 1978 | Result arg_count_register = allocator_->Allocate(r0); |
| 1979 | ASSERT(arg_count_register.is_valid()); |
| 1980 | __ mov(arg_count_register.reg(), Operand(1)); |
| 1981 | Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY, |
| 1982 | CALL_JS, |
| 1983 | &arg_count_register, |
| 1984 | 2); |
| 1985 | __ mov(r3, Operand(result.reg())); |
| 1986 | result.Unuse(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1987 | |
| 1988 | // If the property has been removed while iterating, we just skip it. |
| 1989 | __ cmp(r3, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1990 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1991 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1992 | end_del_check.Bind(); |
| 1993 | // Store the entry in the 'each' expression and take another spin in the |
| 1994 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1995 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1996 | { Reference each(this, node->each()); |
| 1997 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1998 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1999 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 2000 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2001 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2002 | // If the reference was to a slot we rely on the convenient property |
| 2003 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 2004 | // right on top of or right underneath a zero-sized reference. |
| 2005 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2006 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2007 | // It's safe to pop the value lying on top of the reference before |
| 2008 | // unloading the reference itself (which preserves the top of stack, |
| 2009 | // ie, now the topmost value of the non-zero sized reference), since |
| 2010 | // we will discard the top of stack after unloading the reference |
| 2011 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2012 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2013 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2014 | } |
| 2015 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2016 | // Discard the i'th entry pushed above or else the remainder of the |
| 2017 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2018 | frame_->Drop(); |
| 2019 | |
| 2020 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2021 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2022 | VisitAndSpill(node->body()); |
| 2023 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2024 | // Next. Reestablish a spilled frame in case we are coming here via |
| 2025 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2026 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2027 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2028 | frame_->EmitPop(r0); |
| 2029 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 2030 | frame_->EmitPush(r0); |
| 2031 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2032 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2033 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 2034 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2035 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2036 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2037 | |
| 2038 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2039 | exit.Bind(); |
| 2040 | node->continue_target()->Unuse(); |
| 2041 | node->break_target()->Unuse(); |
| 2042 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2046 | void CodeGenerator::VisitTryCatch(TryCatch* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2047 | #ifdef DEBUG |
| 2048 | int original_height = frame_->height(); |
| 2049 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2050 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | Comment cmnt(masm_, "[ TryCatch"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2052 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2053 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2054 | JumpTarget try_block; |
| 2055 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2056 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2057 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2058 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2059 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2060 | |
| 2061 | // Store the caught exception in the catch variable. |
| 2062 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2063 | ASSERT(ref.is_slot()); |
| 2064 | // Here we make use of the convenient property that it doesn't matter |
| 2065 | // whether a value is immediately on top of or underneath a zero-sized |
| 2066 | // reference. |
| 2067 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2071 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2072 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2073 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2074 | if (frame_ != NULL) { |
| 2075 | exit.Jump(); |
| 2076 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2077 | |
| 2078 | |
| 2079 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2080 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2081 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2082 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2083 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2084 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2085 | // Shadow the labels for all escapes from the try block, including |
| 2086 | // returns. During shadowing, the original label is hidden as the |
| 2087 | // LabelShadow and operations on the original actually affect the |
| 2088 | // shadowing label. |
| 2089 | // |
| 2090 | // We should probably try to unify the escaping labels and the return |
| 2091 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2092 | int nof_escapes = node->escaping_targets()->length(); |
| 2093 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2094 | |
| 2095 | // Add the shadow target for the function return. |
| 2096 | static const int kReturnShadowIndex = 0; |
| 2097 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2098 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2099 | function_return_is_shadowed_ = true; |
| 2100 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2101 | |
| 2102 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2103 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2104 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2108 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2109 | |
| 2110 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2111 | // After shadowing stops, the original labels are unshadowed and the |
| 2112 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2113 | bool has_unlinks = false; |
| 2114 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2115 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2116 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2117 | } |
| 2118 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2119 | |
| 2120 | // Get an external reference to the handler address. |
| 2121 | ExternalReference handler_address(Top::k_handler_address); |
| 2122 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2123 | // If we can fall off the end of the try block, unlink from try chain. |
| 2124 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2125 | // The next handler address is on top of the frame. Unlink from |
| 2126 | // the handler list and drop the rest of this handler from the |
| 2127 | // frame. |
| 2128 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2129 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2130 | __ mov(r3, Operand(handler_address)); |
| 2131 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2132 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2133 | if (has_unlinks) { |
| 2134 | exit.Jump(); |
| 2135 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2138 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2139 | // jumped to. Deallocate each shadow target. |
| 2140 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2141 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2142 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2143 | shadows[i]->Bind(); |
| 2144 | // Because we can be jumping here (to spilled code) from unspilled |
| 2145 | // code, we need to reestablish a spilled frame at this block. |
| 2146 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2147 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2148 | // Reload sp from the top handler, because some statements that we |
| 2149 | // 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] | 2150 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2151 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2152 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2153 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2154 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2155 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2156 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2157 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2158 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2159 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2160 | frame_->PrepareForReturn(); |
| 2161 | } |
| 2162 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2166 | exit.Bind(); |
| 2167 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2171 | void CodeGenerator::VisitTryFinally(TryFinally* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2172 | #ifdef DEBUG |
| 2173 | int original_height = frame_->height(); |
| 2174 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2175 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2176 | Comment cmnt(masm_, "[ TryFinally"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2177 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2178 | |
| 2179 | // State: Used to keep track of reason for entering the finally |
| 2180 | // block. Should probably be extended to hold information for |
| 2181 | // break/continue from within the try block. |
| 2182 | enum { FALLING, THROWING, JUMPING }; |
| 2183 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2184 | JumpTarget try_block; |
| 2185 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2186 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2187 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2188 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2189 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2190 | // In case of thrown exceptions, this is where we continue. |
| 2191 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2192 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2193 | |
| 2194 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2195 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2196 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2197 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2198 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2199 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2200 | // Shadow the labels for all escapes from the try block, including |
| 2201 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2202 | // operations on the original actually affect the shadowing label. |
| 2203 | // |
| 2204 | // We should probably try to unify the escaping labels and the return |
| 2205 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2206 | int nof_escapes = node->escaping_targets()->length(); |
| 2207 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2208 | |
| 2209 | // Add the shadow target for the function return. |
| 2210 | static const int kReturnShadowIndex = 0; |
| 2211 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2212 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2213 | function_return_is_shadowed_ = true; |
| 2214 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2215 | |
| 2216 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2217 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2218 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2222 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2223 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2224 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2225 | // After shadowing stops, the original labels are unshadowed and the |
| 2226 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2227 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2228 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2229 | shadows[i]->StopShadowing(); |
| 2230 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2231 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2232 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2233 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2234 | // Get an external reference to the handler address. |
| 2235 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2236 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2237 | // If we can fall off the end of the try block, unlink from the try |
| 2238 | // chain and set the state on the frame to FALLING. |
| 2239 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2240 | // The next handler address is on top of the frame. |
| 2241 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2242 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2243 | __ mov(r3, Operand(handler_address)); |
| 2244 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2245 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2246 | |
| 2247 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2248 | // state in r2, then jump around the unlink blocks if any. |
| 2249 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2250 | frame_->EmitPush(r0); |
| 2251 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2252 | if (nof_unlinks > 0) { |
| 2253 | finally_block.Jump(); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | // Generate code to unlink and set the state for the (formerly) |
| 2258 | // shadowing targets that have been jumped to. |
| 2259 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2260 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2261 | // If we have come from the shadowed return, the return value is |
| 2262 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2263 | // until it is pushed. |
| 2264 | // |
| 2265 | // Because we can be jumping here (to spilled code) from |
| 2266 | // unspilled code, we need to reestablish a spilled frame at |
| 2267 | // this block. |
| 2268 | shadows[i]->Bind(); |
| 2269 | frame_->SpillAll(); |
| 2270 | |
| 2271 | // Reload sp from the top handler, because some statements that |
| 2272 | // we break from (eg, for...in) may have left stuff on the |
| 2273 | // stack. |
| 2274 | __ mov(r3, Operand(handler_address)); |
| 2275 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2276 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2277 | |
| 2278 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2279 | // handler address is currently on top of the frame. |
| 2280 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2281 | frame_->EmitPop(r1); |
| 2282 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2283 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2284 | |
| 2285 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2286 | // If this label shadowed the function return, materialize the |
| 2287 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2288 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2289 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2290 | // Fake TOS for targets that shadowed breaks and continues. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2291 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2292 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2293 | } |
| 2294 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2295 | if (--nof_unlinks > 0) { |
| 2296 | // If this is not the last unlink block, jump around the next. |
| 2297 | finally_block.Jump(); |
| 2298 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2299 | } |
| 2300 | } |
| 2301 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2302 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2303 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2304 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2305 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2306 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2307 | |
| 2308 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2309 | // and the state - while evaluating the finally block. |
| 2310 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2311 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2312 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2313 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2314 | if (has_valid_frame()) { |
| 2315 | // Restore state and return value or faked TOS. |
| 2316 | frame_->EmitPop(r2); |
| 2317 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2320 | // Generate code to jump to the right destination for all used |
| 2321 | // formerly shadowing targets. Deallocate each shadow target. |
| 2322 | for (int i = 0; i < shadows.length(); i++) { |
| 2323 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2324 | JumpTarget* original = shadows[i]->other_target(); |
| 2325 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2326 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2327 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2328 | skip.Branch(ne); |
| 2329 | frame_->PrepareForReturn(); |
| 2330 | original->Jump(); |
| 2331 | skip.Bind(); |
| 2332 | } else { |
| 2333 | original->Branch(eq); |
| 2334 | } |
| 2335 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2336 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2337 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2338 | if (has_valid_frame()) { |
| 2339 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2340 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2341 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2342 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2343 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2344 | // Rethrow exception. |
| 2345 | frame_->EmitPush(r0); |
| 2346 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2347 | |
| 2348 | // Done. |
| 2349 | exit.Bind(); |
| 2350 | } |
| 2351 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2355 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2356 | #ifdef DEBUG |
| 2357 | int original_height = frame_->height(); |
| 2358 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2359 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2360 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2361 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2362 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2363 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2364 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2365 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2366 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2370 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2371 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2372 | ASSERT(boilerplate->IsBoilerplate()); |
| 2373 | |
| 2374 | // Push the boilerplate on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2375 | __ mov(r0, Operand(boilerplate)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2376 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2377 | |
| 2378 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2379 | frame_->EmitPush(cp); |
| 2380 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2381 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2385 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2386 | #ifdef DEBUG |
| 2387 | int original_height = frame_->height(); |
| 2388 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2389 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2390 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2391 | |
| 2392 | // Build the function boilerplate and instantiate it. |
| 2393 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2394 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2395 | if (HasStackOverflow()) { |
| 2396 | ASSERT(frame_->height() == original_height); |
| 2397 | return; |
| 2398 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2399 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2400 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2404 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2405 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2406 | #ifdef DEBUG |
| 2407 | int original_height = frame_->height(); |
| 2408 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2409 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2410 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2411 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2412 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2416 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2417 | #ifdef DEBUG |
| 2418 | int original_height = frame_->height(); |
| 2419 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2420 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2421 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2422 | JumpTarget then; |
| 2423 | JumpTarget else_; |
| 2424 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2425 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 2426 | &then, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2427 | Branch(false, &else_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2428 | then.Bind(); |
| 2429 | LoadAndSpill(node->then_expression(), typeof_state()); |
| 2430 | exit.Jump(); |
| 2431 | else_.Bind(); |
| 2432 | LoadAndSpill(node->else_expression(), typeof_state()); |
| 2433 | exit.Bind(); |
| 2434 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2438 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2439 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2440 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2441 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2442 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2443 | JumpTarget slow; |
| 2444 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2445 | |
| 2446 | // Generate fast-case code for variables that might be shadowed by |
| 2447 | // eval-introduced variables. Eval is used a lot without |
| 2448 | // introducing variables. In those cases, we do not want to |
| 2449 | // perform a runtime call for all variables in the scope |
| 2450 | // containing the eval. |
| 2451 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2452 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2453 | // If there was no control flow to slow, we can exit early. |
| 2454 | if (!slow.is_linked()) { |
| 2455 | frame_->EmitPush(r0); |
| 2456 | return; |
| 2457 | } |
| 2458 | |
| 2459 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2460 | |
| 2461 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2462 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2463 | // Only generate the fast case for locals that rewrite to slots. |
| 2464 | // This rules out argument loads. |
| 2465 | if (potential_slot != NULL) { |
| 2466 | __ ldr(r0, |
| 2467 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2468 | r1, |
| 2469 | r2, |
| 2470 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2471 | if (potential_slot->var()->mode() == Variable::CONST) { |
| 2472 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2473 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
| 2474 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2475 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2476 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2477 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2478 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2479 | } |
| 2480 | } |
| 2481 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2482 | slow.Bind(); |
| 2483 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2484 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2485 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2486 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2487 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2488 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2489 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2490 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2491 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2492 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2493 | done.Bind(); |
| 2494 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2495 | |
| 2496 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2497 | // Note: We would like to keep the assert below, but it fires because of |
| 2498 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2499 | // ASSERT(!slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2500 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2501 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2502 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2503 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2504 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2505 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2506 | // initialized yet) which needs to be converted into the 'undefined' |
| 2507 | // value. |
| 2508 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2509 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2510 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2511 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2512 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2513 | } |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2518 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2519 | TypeofState typeof_state, |
| 2520 | Register tmp, |
| 2521 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2522 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2523 | // Check that no extension objects have been created by calls to |
| 2524 | // eval from the current scope to the global scope. |
| 2525 | Register context = cp; |
| 2526 | Scope* s = scope(); |
| 2527 | while (s != NULL) { |
| 2528 | if (s->num_heap_slots() > 0) { |
| 2529 | if (s->calls_eval()) { |
| 2530 | // Check that extension is NULL. |
| 2531 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2532 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2533 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2534 | } |
| 2535 | // Load next context in chain. |
| 2536 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2537 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2538 | context = tmp; |
| 2539 | } |
| 2540 | // If no outer scope calls eval, we do not need to check more |
| 2541 | // context extensions. |
| 2542 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2543 | s = s->outer_scope(); |
| 2544 | } |
| 2545 | |
| 2546 | if (s->is_eval_scope()) { |
| 2547 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2548 | if (!context.is(tmp)) { |
| 2549 | __ mov(tmp, Operand(context)); |
| 2550 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2551 | __ bind(&next); |
| 2552 | // Terminate at global context. |
| 2553 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 2554 | __ cmp(tmp2, Operand(Factory::global_context_map())); |
| 2555 | __ b(eq, &fast); |
| 2556 | // Check that extension is NULL. |
| 2557 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2558 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2559 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2560 | // Load next context in chain. |
| 2561 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2562 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2563 | __ b(&next); |
| 2564 | __ bind(&fast); |
| 2565 | } |
| 2566 | |
| 2567 | // All extension objects were empty and it is safe to use a global |
| 2568 | // load IC call. |
| 2569 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2570 | // Load the global object. |
| 2571 | LoadGlobal(); |
| 2572 | // Setup the name register. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2573 | Result name = allocator_->Allocate(r2); |
| 2574 | ASSERT(name.is_valid()); // We are in spilled code. |
| 2575 | __ mov(name.reg(), Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2576 | // Call IC stub. |
| 2577 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2578 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2579 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2580 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2583 | // Drop the global object. The result is in r0. |
| 2584 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2588 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2589 | #ifdef DEBUG |
| 2590 | int original_height = frame_->height(); |
| 2591 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2592 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2593 | Comment cmnt(masm_, "[ Slot"); |
| 2594 | LoadFromSlot(node, typeof_state()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2595 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2596 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2597 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2598 | |
| 2599 | void CodeGenerator::VisitVariableProxy(VariableProxy* 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; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2604 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2605 | |
| 2606 | Variable* var = node->var(); |
| 2607 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2608 | if (expr != NULL) { |
| 2609 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2610 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2611 | ASSERT(var->is_global()); |
| 2612 | Reference ref(this, node); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2613 | ref.GetValueAndSpill(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2614 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2615 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2619 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2620 | #ifdef DEBUG |
| 2621 | int original_height = frame_->height(); |
| 2622 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2623 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2624 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2625 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2626 | frame_->EmitPush(r0); |
| 2627 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2631 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2632 | #ifdef DEBUG |
| 2633 | int original_height = frame_->height(); |
| 2634 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2635 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2636 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2637 | |
| 2638 | // Retrieve the literal array and check the allocated entry. |
| 2639 | |
| 2640 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2641 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2642 | |
| 2643 | // Load the literals array of the function. |
| 2644 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2645 | |
| 2646 | // Load the literal at the ast saved index. |
| 2647 | int literal_offset = |
| 2648 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2649 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2650 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2651 | JumpTarget done; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2652 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2653 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2654 | |
| 2655 | // If the entry is undefined we call the runtime system to computed |
| 2656 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2657 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2658 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2659 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2660 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2661 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2662 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2663 | frame_->EmitPush(r0); |
| 2664 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2665 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2666 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2667 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2668 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2669 | frame_->EmitPush(r2); |
| 2670 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | |
| 2674 | // This deferred code stub will be used for creating the boilerplate |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2675 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2676 | // Each created boilerplate is stored in the JSFunction and they are |
| 2677 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2678 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2679 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2680 | explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2681 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2682 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2683 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2684 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2685 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2686 | private: |
| 2687 | ObjectLiteral* node_; |
| 2688 | }; |
| 2689 | |
| 2690 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2691 | void DeferredObjectLiteral::Generate() { |
| 2692 | // Argument is passed in r1. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2693 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2694 | // 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] | 2695 | // the literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2696 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2697 | __ push(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2698 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2699 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2700 | __ push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2701 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2702 | __ mov(r0, Operand(node_->constant_properties())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2703 | __ push(r0); |
| 2704 | __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2705 | __ mov(r2, Operand(r0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2706 | // Result is returned in r2. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2710 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2711 | #ifdef DEBUG |
| 2712 | int original_height = frame_->height(); |
| 2713 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2714 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2715 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2716 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2717 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2718 | |
| 2719 | // Retrieve the literal array and check the allocated entry. |
| 2720 | |
| 2721 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2722 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2723 | |
| 2724 | // Load the literals array of the function. |
| 2725 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2726 | |
| 2727 | // Load the literal at the ast saved index. |
| 2728 | int literal_offset = |
| 2729 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2730 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2731 | |
| 2732 | // Check whether we need to materialize the object literal boilerplate. |
| 2733 | // If so, jump to the deferred code. |
| 2734 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2735 | deferred->Branch(eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2736 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2737 | |
| 2738 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2739 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2740 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2741 | // Clone the boilerplate object. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2742 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2743 | if (node->depth() == 1) { |
| 2744 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2745 | } |
| 2746 | frame_->CallRuntime(clone_function_id, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2747 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2748 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2749 | |
| 2750 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2751 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2752 | Literal* key = property->key(); |
| 2753 | Expression* value = property->value(); |
| 2754 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2755 | case ObjectLiteral::Property::CONSTANT: |
| 2756 | break; |
| 2757 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2758 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2759 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2760 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2761 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2762 | frame_->EmitPush(r0); // dup the result |
| 2763 | LoadAndSpill(key); |
| 2764 | LoadAndSpill(value); |
| 2765 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2766 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2767 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2768 | break; |
| 2769 | } |
| 2770 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2771 | frame_->EmitPush(r0); |
| 2772 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2773 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2774 | frame_->EmitPush(r0); |
| 2775 | LoadAndSpill(value); |
| 2776 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2777 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2778 | break; |
| 2779 | } |
| 2780 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2781 | frame_->EmitPush(r0); |
| 2782 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2783 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2784 | frame_->EmitPush(r0); |
| 2785 | LoadAndSpill(value); |
| 2786 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2787 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2788 | break; |
| 2789 | } |
| 2790 | } |
| 2791 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2792 | ASSERT(frame_->height() == original_height + 1); |
| 2793 | } |
| 2794 | |
| 2795 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2796 | // This deferred code stub will be used for creating the boilerplate |
| 2797 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2798 | // Each created boilerplate is stored in the JSFunction and they are |
| 2799 | // therefore context dependent. |
| 2800 | class DeferredArrayLiteral: public DeferredCode { |
| 2801 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2802 | explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2803 | set_comment("[ DeferredArrayLiteral"); |
| 2804 | } |
| 2805 | |
| 2806 | virtual void Generate(); |
| 2807 | |
| 2808 | private: |
| 2809 | ArrayLiteral* node_; |
| 2810 | }; |
| 2811 | |
| 2812 | |
| 2813 | void DeferredArrayLiteral::Generate() { |
| 2814 | // Argument is passed in r1. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2815 | |
| 2816 | // If the entry is undefined we call the runtime system to computed |
| 2817 | // the literal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2818 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2819 | __ push(r1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2820 | // Literal index (1). |
| 2821 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2822 | __ push(r0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2823 | // Constant properties (2). |
| 2824 | __ mov(r0, Operand(node_->literals())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2825 | __ push(r0); |
| 2826 | __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2827 | __ mov(r2, Operand(r0)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2828 | // Result is returned in r2. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2832 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2833 | #ifdef DEBUG |
| 2834 | int original_height = frame_->height(); |
| 2835 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2836 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2837 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2838 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2839 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2840 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2841 | // Retrieve the literal array and check the allocated entry. |
| 2842 | |
| 2843 | // Load the function of this activation. |
| 2844 | __ ldr(r1, frame_->Function()); |
| 2845 | |
| 2846 | // Load the literals array of the function. |
| 2847 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2848 | |
| 2849 | // Load the literal at the ast saved index. |
| 2850 | int literal_offset = |
| 2851 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2852 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2853 | |
| 2854 | // Check whether we need to materialize the object literal boilerplate. |
| 2855 | // If so, jump to the deferred code. |
| 2856 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2857 | deferred->Branch(eq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2858 | deferred->BindExit(); |
| 2859 | |
| 2860 | // Push the object literal boilerplate. |
| 2861 | frame_->EmitPush(r2); |
| 2862 | |
| 2863 | // Clone the boilerplate object. |
| 2864 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2865 | if (node->depth() == 1) { |
| 2866 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2867 | } |
| 2868 | frame_->CallRuntime(clone_function_id, 1); |
| 2869 | frame_->EmitPush(r0); // save the result |
| 2870 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2871 | |
| 2872 | // Generate code to set the elements in the array that are not |
| 2873 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2874 | for (int i = 0; i < node->values()->length(); i++) { |
| 2875 | Expression* value = node->values()->at(i); |
| 2876 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2877 | // If value is a literal the property value is already set in the |
| 2878 | // boilerplate object. |
| 2879 | if (value->AsLiteral() != NULL) continue; |
| 2880 | // If value is a materialized literal the property value is already set |
| 2881 | // in the boilerplate object if it is simple. |
| 2882 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2883 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2884 | // The property must be set by generated code. |
| 2885 | LoadAndSpill(value); |
| 2886 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2887 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2888 | // Fetch the object literal. |
| 2889 | __ ldr(r1, frame_->Top()); |
| 2890 | // Get the elements array. |
| 2891 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2892 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2893 | // Write to the indexed properties array. |
| 2894 | int offset = i * kPointerSize + Array::kHeaderSize; |
| 2895 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2896 | |
| 2897 | // Update the write barrier for the array address. |
| 2898 | __ mov(r3, Operand(offset)); |
| 2899 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2900 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2901 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2905 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2906 | #ifdef DEBUG |
| 2907 | int original_height = frame_->height(); |
| 2908 | #endif |
| 2909 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2910 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2911 | // Call runtime routine to allocate the catch extension object and |
| 2912 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2913 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2914 | LoadAndSpill(node->key()); |
| 2915 | LoadAndSpill(node->value()); |
| 2916 | Result result = |
| 2917 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2918 | frame_->EmitPush(result.reg()); |
| 2919 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2923 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2924 | #ifdef DEBUG |
| 2925 | int original_height = frame_->height(); |
| 2926 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2927 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2928 | Comment cmnt(masm_, "[ Assignment"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2929 | CodeForStatementPosition(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2930 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2931 | { Reference target(this, node->target()); |
| 2932 | if (target.is_illegal()) { |
| 2933 | // Fool the virtual frame into thinking that we left the assignment's |
| 2934 | // value on the frame. |
| 2935 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2936 | frame_->EmitPush(r0); |
| 2937 | ASSERT(frame_->height() == original_height + 1); |
| 2938 | return; |
| 2939 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2940 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2941 | if (node->op() == Token::ASSIGN || |
| 2942 | node->op() == Token::INIT_VAR || |
| 2943 | node->op() == Token::INIT_CONST) { |
| 2944 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2945 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2946 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2947 | // +=, *= and similar binary assignments. |
| 2948 | // Get the old value of the lhs. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2949 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 2950 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2951 | bool overwrite = |
| 2952 | (node->value()->AsBinaryOperation() != NULL && |
| 2953 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2954 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2955 | SmiOperation(node->binary_op(), |
| 2956 | literal->handle(), |
| 2957 | false, |
| 2958 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2959 | frame_->EmitPush(r0); |
| 2960 | |
| 2961 | } else { |
| 2962 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2963 | GenericBinaryOperation(node->binary_op(), |
| 2964 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2965 | frame_->EmitPush(r0); |
| 2966 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2967 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2968 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2969 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2970 | if (var != NULL && |
| 2971 | (var->mode() == Variable::CONST) && |
| 2972 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2973 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2974 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2975 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2976 | CodeForSourcePosition(node->position()); |
| 2977 | if (node->op() == Token::INIT_CONST) { |
| 2978 | // Dynamic constant initializations must use the function context |
| 2979 | // and initialize the actual constant declared. Dynamic variable |
| 2980 | // initializations are simply assignments and use SetValue. |
| 2981 | target.SetValue(CONST_INIT); |
| 2982 | } else { |
| 2983 | target.SetValue(NOT_CONST_INIT); |
| 2984 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2985 | } |
| 2986 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2987 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2991 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2992 | #ifdef DEBUG |
| 2993 | int original_height = frame_->height(); |
| 2994 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2995 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2996 | Comment cmnt(masm_, "[ Throw"); |
| 2997 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2998 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2999 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3000 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 3001 | frame_->EmitPush(r0); |
| 3002 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3006 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3007 | #ifdef DEBUG |
| 3008 | int original_height = frame_->height(); |
| 3009 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3010 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3011 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3012 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3013 | { Reference property(this, node); |
| 3014 | property.GetValueAndSpill(typeof_state()); |
| 3015 | } |
| 3016 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3020 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3021 | #ifdef DEBUG |
| 3022 | int original_height = frame_->height(); |
| 3023 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3024 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3025 | Comment cmnt(masm_, "[ Call"); |
| 3026 | |
| 3027 | ZoneList<Expression*>* args = node->arguments(); |
| 3028 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3029 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3030 | // Standard function call. |
| 3031 | |
| 3032 | // Check if the function is a variable or a property. |
| 3033 | Expression* function = node->expression(); |
| 3034 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 3035 | Property* property = function->AsProperty(); |
| 3036 | |
| 3037 | // ------------------------------------------------------------------------ |
| 3038 | // Fast-case: Use inline caching. |
| 3039 | // --- |
| 3040 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 3041 | // must be resolved after the arguments have been evaluated. The IC code |
| 3042 | // automatically handles this by loading the arguments before the function |
| 3043 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 3044 | // ------------------------------------------------------------------------ |
| 3045 | |
| 3046 | if (var != NULL && !var->is_this() && var->is_global()) { |
| 3047 | // ---------------------------------- |
| 3048 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3049 | // ---------------------------------- |
| 3050 | |
| 3051 | // 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] | 3052 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3053 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3054 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3055 | // Pass the global object as the receiver and let the IC stub |
| 3056 | // patch the stack to use the global proxy as 'this' in the |
| 3057 | // invoked function. |
| 3058 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3059 | |
| 3060 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3061 | int arg_count = args->length(); |
| 3062 | for (int i = 0; i < arg_count; i++) { |
| 3063 | LoadAndSpill(args->at(i)); |
| 3064 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3065 | |
| 3066 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3067 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3068 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3069 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3070 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3071 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3072 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3073 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3074 | frame_->Drop(); |
| 3075 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3076 | |
| 3077 | } else if (var != NULL && var->slot() != NULL && |
| 3078 | var->slot()->type() == Slot::LOOKUP) { |
| 3079 | // ---------------------------------- |
| 3080 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3081 | // ---------------------------------- |
| 3082 | |
| 3083 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3084 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3085 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3086 | frame_->EmitPush(r0); |
| 3087 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3088 | // r0: slot value; r1: receiver |
| 3089 | |
| 3090 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3091 | frame_->EmitPush(r0); // function |
| 3092 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3093 | |
| 3094 | // Call the function. |
| 3095 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3096 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3097 | |
| 3098 | } else if (property != NULL) { |
| 3099 | // Check if the key is a literal string. |
| 3100 | Literal* literal = property->key()->AsLiteral(); |
| 3101 | |
| 3102 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3103 | // ------------------------------------------------------------------ |
| 3104 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3105 | // ------------------------------------------------------------------ |
| 3106 | |
| 3107 | // 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] | 3108 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3109 | frame_->EmitPush(r0); |
| 3110 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3111 | |
| 3112 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3113 | int arg_count = args->length(); |
| 3114 | for (int i = 0; i < arg_count; i++) { |
| 3115 | LoadAndSpill(args->at(i)); |
| 3116 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3117 | |
| 3118 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3119 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3120 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3121 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3122 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3123 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3126 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3127 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3128 | 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] | 3129 | |
| 3130 | } else { |
| 3131 | // ------------------------------------------- |
| 3132 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3133 | // ------------------------------------------- |
| 3134 | |
| 3135 | // Load the function to call from the property through a reference. |
| 3136 | Reference ref(this, property); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3137 | ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3138 | |
| 3139 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3140 | if (property->is_synthetic()) { |
| 3141 | LoadGlobalReceiver(r0); |
| 3142 | } else { |
| 3143 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3144 | frame_->EmitPush(r0); |
| 3145 | } |
| 3146 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3147 | // Call the function. |
| 3148 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3149 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | } else { |
| 3153 | // ---------------------------------- |
| 3154 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3155 | // ---------------------------------- |
| 3156 | |
| 3157 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3158 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3159 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3160 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3161 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3162 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3163 | // Call the function. |
| 3164 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3165 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3166 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3167 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3171 | void CodeGenerator::VisitCallEval(CallEval* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3172 | #ifdef DEBUG |
| 3173 | int original_height = frame_->height(); |
| 3174 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3175 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3176 | Comment cmnt(masm_, "[ CallEval"); |
| 3177 | |
| 3178 | // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve |
| 3179 | // the function we need to call and the receiver of the call. |
| 3180 | // Then we call the resolved function using the given arguments. |
| 3181 | |
| 3182 | ZoneList<Expression*>* args = node->arguments(); |
| 3183 | Expression* function = node->expression(); |
| 3184 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3185 | CodeForStatementPosition(node); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3186 | |
| 3187 | // Prepare stack for call to resolved function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3188 | LoadAndSpill(function); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3189 | __ mov(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3190 | frame_->EmitPush(r2); // Slot for receiver |
| 3191 | int arg_count = args->length(); |
| 3192 | for (int i = 0; i < arg_count; i++) { |
| 3193 | LoadAndSpill(args->at(i)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | // Prepare stack for call to ResolvePossiblyDirectEval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3197 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3198 | frame_->EmitPush(r1); |
| 3199 | if (arg_count > 0) { |
| 3200 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3201 | frame_->EmitPush(r1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3202 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3203 | frame_->EmitPush(r2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | // Resolve the call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3207 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3208 | |
| 3209 | // Touch up stack with the right values for the function and the receiver. |
| 3210 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3211 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3212 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3213 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3214 | |
| 3215 | // Call the function. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3216 | CodeForSourcePosition(node->position()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3217 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3218 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3219 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3220 | frame_->CallStub(&call_function, arg_count + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3221 | |
| 3222 | __ ldr(cp, frame_->Context()); |
| 3223 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3224 | frame_->Drop(); |
| 3225 | frame_->EmitPush(r0); |
| 3226 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3230 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3231 | #ifdef DEBUG |
| 3232 | int original_height = frame_->height(); |
| 3233 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3234 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3235 | Comment cmnt(masm_, "[ CallNew"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3236 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3237 | |
| 3238 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3239 | // expression in new calls must be evaluated before the |
| 3240 | // arguments. This is different from ordinary calls, where the |
| 3241 | // actual function to call is resolved after the arguments have been |
| 3242 | // evaluated. |
| 3243 | |
| 3244 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3245 | // receiver. There is no need to use the global proxy here because |
| 3246 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3247 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3248 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3249 | |
| 3250 | // Push the arguments ("left-to-right") on the stack. |
| 3251 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3252 | int arg_count = args->length(); |
| 3253 | for (int i = 0; i < arg_count; i++) { |
| 3254 | LoadAndSpill(args->at(i)); |
| 3255 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3256 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3257 | // r0: the number of arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3258 | Result num_args = allocator_->Allocate(r0); |
| 3259 | ASSERT(num_args.is_valid()); |
| 3260 | __ mov(num_args.reg(), Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3261 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3262 | // Load the function into r1 as per calling convention. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3263 | Result function = allocator_->Allocate(r1); |
| 3264 | ASSERT(function.is_valid()); |
| 3265 | __ ldr(function.reg(), frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3266 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3267 | // Call the construct call builtin that handles allocation and |
| 3268 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3269 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3270 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
| 3271 | Result result = frame_->CallCodeObject(ic, |
| 3272 | RelocInfo::CONSTRUCT_CALL, |
| 3273 | &num_args, |
| 3274 | &function, |
| 3275 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3276 | |
| 3277 | // 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] | 3278 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3279 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3283 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3284 | VirtualFrame::SpilledScope spilled_scope; |
| 3285 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 3286 | JumpTarget leave, null, function, non_function_constructor; |
| 3287 | |
| 3288 | // Load the object into r0. |
| 3289 | LoadAndSpill(args->at(0)); |
| 3290 | frame_->EmitPop(r0); |
| 3291 | |
| 3292 | // If the object is a smi, we return null. |
| 3293 | __ tst(r0, Operand(kSmiTagMask)); |
| 3294 | null.Branch(eq); |
| 3295 | |
| 3296 | // Check that the object is a JS object but take special care of JS |
| 3297 | // functions to make sure they have 'Function' as their class. |
| 3298 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3299 | null.Branch(lt); |
| 3300 | |
| 3301 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3302 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3303 | // LAST_JS_OBJECT_TYPE. |
| 3304 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3305 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3306 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3307 | function.Branch(eq); |
| 3308 | |
| 3309 | // Check if the constructor in the map is a function. |
| 3310 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3311 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3312 | non_function_constructor.Branch(ne); |
| 3313 | |
| 3314 | // The r0 register now contains the constructor function. Grab the |
| 3315 | // instance class name from there. |
| 3316 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3317 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3318 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 3319 | leave.Jump(); |
| 3320 | |
| 3321 | // Functions have class 'Function'. |
| 3322 | function.Bind(); |
| 3323 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3324 | frame_->EmitPush(r0); |
| 3325 | leave.Jump(); |
| 3326 | |
| 3327 | // Objects with a non-function constructor have class 'Object'. |
| 3328 | non_function_constructor.Bind(); |
| 3329 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3330 | frame_->EmitPush(r0); |
| 3331 | leave.Jump(); |
| 3332 | |
| 3333 | // Non-JS objects have class null. |
| 3334 | null.Bind(); |
| 3335 | __ mov(r0, Operand(Factory::null_value())); |
| 3336 | frame_->EmitPush(r0); |
| 3337 | |
| 3338 | // All done. |
| 3339 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3343 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3344 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3345 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3346 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3347 | LoadAndSpill(args->at(0)); |
| 3348 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3349 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3350 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3351 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3352 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3353 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3354 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3355 | // Load the value. |
| 3356 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3357 | leave.Bind(); |
| 3358 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3362 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3363 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3364 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3365 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3366 | LoadAndSpill(args->at(0)); // Load the object. |
| 3367 | LoadAndSpill(args->at(1)); // Load the value. |
| 3368 | frame_->EmitPop(r0); // r0 contains value |
| 3369 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3370 | // if (object->IsSmi()) return object. |
| 3371 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3372 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3373 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3374 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3375 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3376 | // Store the value. |
| 3377 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3378 | // Update the write barrier. |
| 3379 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3380 | __ RecordWrite(r1, r2, r3); |
| 3381 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3382 | leave.Bind(); |
| 3383 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3387 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3388 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3389 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3390 | LoadAndSpill(args->at(0)); |
| 3391 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3392 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3393 | cc_reg_ = eq; |
| 3394 | } |
| 3395 | |
| 3396 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3397 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3398 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3399 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3400 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3401 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3402 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3403 | LoadAndSpill(args->at(1)); |
| 3404 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3405 | __ CallRuntime(Runtime::kLog, 2); |
| 3406 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3407 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3408 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3409 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3413 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3414 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3415 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3416 | LoadAndSpill(args->at(0)); |
| 3417 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 3418 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3419 | cc_reg_ = eq; |
| 3420 | } |
| 3421 | |
| 3422 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3423 | // This should generate code that performs a charCodeAt() call or returns |
| 3424 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3425 | // 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] | 3426 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3427 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3428 | ASSERT(args->length() == 2); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3429 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3430 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3434 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3435 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3436 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3437 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3438 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3439 | // We need the CC bits to come out as not_equal in the case where the |
| 3440 | // object is a smi. This can't be done with the usual test opcode so |
| 3441 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3442 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3443 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3444 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3445 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3446 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3447 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3448 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3449 | cc_reg_ = eq; |
| 3450 | } |
| 3451 | |
| 3452 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3453 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3454 | VirtualFrame::SpilledScope spilled_scope; |
| 3455 | ASSERT(args->length() == 0); |
| 3456 | frame_->CallRuntime(Runtime::kIsConstructCall, 0); |
| 3457 | frame_->EmitPush(r0); |
| 3458 | } |
| 3459 | |
| 3460 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3461 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3462 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3463 | ASSERT(args->length() == 0); |
| 3464 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3465 | // Seed the result with the formal parameters count, which will be used |
| 3466 | // 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] | 3467 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3468 | |
| 3469 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3470 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3471 | frame_->CallStub(&stub, 0); |
| 3472 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3476 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3477 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3478 | ASSERT(args->length() == 1); |
| 3479 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3480 | // Satisfy contract with ArgumentsAccessStub: |
| 3481 | // 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] | 3482 | LoadAndSpill(args->at(0)); |
| 3483 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3484 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3485 | |
| 3486 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3487 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3488 | frame_->CallStub(&stub, 0); |
| 3489 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3493 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3494 | VirtualFrame::SpilledScope spilled_scope; |
| 3495 | ASSERT(args->length() == 0); |
| 3496 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3497 | RelocInfo::RUNTIME_ENTRY); |
| 3498 | frame_->EmitPush(r0); |
| 3499 | } |
| 3500 | |
| 3501 | |
| 3502 | void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) { |
| 3503 | VirtualFrame::SpilledScope spilled_scope; |
| 3504 | LoadAndSpill(args->at(0)); |
| 3505 | switch (op) { |
| 3506 | case SIN: |
| 3507 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 3508 | break; |
| 3509 | case COS: |
| 3510 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 3511 | break; |
| 3512 | } |
| 3513 | frame_->EmitPush(r0); |
| 3514 | } |
| 3515 | |
| 3516 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3517 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3518 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3519 | ASSERT(args->length() == 2); |
| 3520 | |
| 3521 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3522 | LoadAndSpill(args->at(0)); |
| 3523 | LoadAndSpill(args->at(1)); |
| 3524 | frame_->EmitPop(r0); |
| 3525 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3526 | __ cmp(r0, Operand(r1)); |
| 3527 | cc_reg_ = eq; |
| 3528 | } |
| 3529 | |
| 3530 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3531 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3532 | #ifdef DEBUG |
| 3533 | int original_height = frame_->height(); |
| 3534 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3535 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3536 | if (CheckForInlineRuntimeCall(node)) { |
| 3537 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3538 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3539 | return; |
| 3540 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3541 | |
| 3542 | ZoneList<Expression*>* args = node->arguments(); |
| 3543 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3544 | Runtime::Function* function = node->function(); |
| 3545 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3546 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3547 | // Prepare stack for calling JS runtime function. |
| 3548 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3549 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3550 | // Push the builtins object found in the current global object. |
| 3551 | __ ldr(r1, GlobalObject()); |
| 3552 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3553 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3554 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3555 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3556 | // Push the arguments ("left-to-right"). |
| 3557 | int arg_count = args->length(); |
| 3558 | for (int i = 0; i < arg_count; i++) { |
| 3559 | LoadAndSpill(args->at(i)); |
| 3560 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3561 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3562 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3563 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3564 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3565 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3566 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3567 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3568 | frame_->Drop(); |
| 3569 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3570 | } else { |
| 3571 | // Call the C runtime function. |
| 3572 | frame_->CallRuntime(function, arg_count); |
| 3573 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3574 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3575 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3579 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3580 | #ifdef DEBUG |
| 3581 | int original_height = frame_->height(); |
| 3582 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3583 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3584 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3585 | |
| 3586 | Token::Value op = node->op(); |
| 3587 | |
| 3588 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3589 | LoadConditionAndSpill(node->expression(), |
| 3590 | NOT_INSIDE_TYPEOF, |
| 3591 | false_target(), |
| 3592 | true_target(), |
| 3593 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3594 | cc_reg_ = NegateCondition(cc_reg_); |
| 3595 | |
| 3596 | } else if (op == Token::DELETE) { |
| 3597 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3598 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3599 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3600 | LoadAndSpill(property->obj()); |
| 3601 | LoadAndSpill(property->key()); |
| 3602 | Result arg_count = allocator_->Allocate(r0); |
| 3603 | ASSERT(arg_count.is_valid()); |
| 3604 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3605 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3606 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3607 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3608 | Slot* slot = variable->slot(); |
| 3609 | if (variable->is_global()) { |
| 3610 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3611 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3612 | frame_->EmitPush(r0); |
| 3613 | Result arg_count = allocator_->Allocate(r0); |
| 3614 | ASSERT(arg_count.is_valid()); |
| 3615 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3616 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3617 | |
| 3618 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3619 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3620 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3621 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3622 | frame_->EmitPush(r0); |
| 3623 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3624 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3625 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3626 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3627 | frame_->EmitPush(r0); |
| 3628 | Result arg_count = allocator_->Allocate(r0); |
| 3629 | ASSERT(arg_count.is_valid()); |
| 3630 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3631 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3632 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3633 | } else { |
| 3634 | // Default: Result of deleting non-global, not dynamically |
| 3635 | // introduced variables is false. |
| 3636 | __ mov(r0, Operand(Factory::false_value())); |
| 3637 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3638 | |
| 3639 | } else { |
| 3640 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3641 | LoadAndSpill(node->expression()); // may have side-effects |
| 3642 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3643 | __ mov(r0, Operand(Factory::true_value())); |
| 3644 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3645 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3646 | |
| 3647 | } else if (op == Token::TYPEOF) { |
| 3648 | // Special case for loading the typeof expression; see comment on |
| 3649 | // LoadTypeofExpression(). |
| 3650 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3651 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3652 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3653 | |
| 3654 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3655 | LoadAndSpill(node->expression()); |
| 3656 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3657 | switch (op) { |
| 3658 | case Token::NOT: |
| 3659 | case Token::DELETE: |
| 3660 | case Token::TYPEOF: |
| 3661 | UNREACHABLE(); // handled above |
| 3662 | break; |
| 3663 | |
| 3664 | case Token::SUB: { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3665 | bool overwrite = |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3666 | (node->expression()->AsBinaryOperation() != NULL && |
| 3667 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3668 | UnarySubStub stub(overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3669 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3670 | break; |
| 3671 | } |
| 3672 | |
| 3673 | case Token::BIT_NOT: { |
| 3674 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3675 | JumpTarget smi_label; |
| 3676 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3677 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3678 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3679 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3680 | frame_->EmitPush(r0); |
| 3681 | Result arg_count = allocator_->Allocate(r0); |
| 3682 | ASSERT(arg_count.is_valid()); |
| 3683 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3684 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3685 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3686 | continue_label.Jump(); |
| 3687 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3688 | __ mvn(r0, Operand(r0)); |
| 3689 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3690 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3691 | break; |
| 3692 | } |
| 3693 | |
| 3694 | case Token::VOID: |
| 3695 | // since the stack top is cached in r0, popping and then |
| 3696 | // pushing a value can be done by just writing to r0. |
| 3697 | __ mov(r0, Operand(Factory::undefined_value())); |
| 3698 | break; |
| 3699 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3700 | case Token::ADD: { |
| 3701 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3702 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3703 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3704 | continue_label.Branch(eq); |
| 3705 | frame_->EmitPush(r0); |
| 3706 | Result arg_count = allocator_->Allocate(r0); |
| 3707 | ASSERT(arg_count.is_valid()); |
| 3708 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3709 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3710 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3711 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3712 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3713 | default: |
| 3714 | UNREACHABLE(); |
| 3715 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3716 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3717 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3718 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3719 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3723 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3724 | #ifdef DEBUG |
| 3725 | int original_height = frame_->height(); |
| 3726 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3727 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3728 | Comment cmnt(masm_, "[ CountOperation"); |
| 3729 | |
| 3730 | bool is_postfix = node->is_postfix(); |
| 3731 | bool is_increment = node->op() == Token::INC; |
| 3732 | |
| 3733 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3734 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3735 | |
| 3736 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3737 | if (is_postfix) { |
| 3738 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3739 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3740 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3741 | |
| 3742 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3743 | if (target.is_illegal()) { |
| 3744 | // Spoof the virtual frame to have the expected height (one higher |
| 3745 | // than on entry). |
| 3746 | if (!is_postfix) { |
| 3747 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3748 | frame_->EmitPush(r0); |
| 3749 | } |
| 3750 | ASSERT(frame_->height() == original_height + 1); |
| 3751 | return; |
| 3752 | } |
| 3753 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 3754 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3755 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3756 | JumpTarget slow; |
| 3757 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3758 | |
| 3759 | // Load the value (1) into register r1. |
| 3760 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3761 | |
| 3762 | // Check for smi operand. |
| 3763 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3764 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3765 | |
| 3766 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3767 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3768 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3769 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3770 | |
| 3771 | // Perform optimistic increment/decrement. |
| 3772 | if (is_increment) { |
| 3773 | __ add(r0, r0, Operand(r1), SetCC); |
| 3774 | } else { |
| 3775 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3776 | } |
| 3777 | |
| 3778 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3779 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3780 | |
| 3781 | // Revert optimistic increment/decrement. |
| 3782 | if (is_increment) { |
| 3783 | __ sub(r0, r0, Operand(r1)); |
| 3784 | } else { |
| 3785 | __ add(r0, r0, Operand(r1)); |
| 3786 | } |
| 3787 | |
| 3788 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3789 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3790 | { |
| 3791 | // Convert the operand to a number. |
| 3792 | frame_->EmitPush(r0); |
| 3793 | Result arg_count = allocator_->Allocate(r0); |
| 3794 | ASSERT(arg_count.is_valid()); |
| 3795 | __ mov(arg_count.reg(), Operand(0)); |
| 3796 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3797 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3798 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3799 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3800 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3803 | // Compute the new value. |
| 3804 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3805 | frame_->EmitPush(r0); |
| 3806 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3807 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3808 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3809 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3810 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3814 | exit.Bind(); |
| 3815 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3816 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3820 | if (is_postfix) frame_->EmitPop(r0); |
| 3821 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3825 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3826 | #ifdef DEBUG |
| 3827 | int original_height = frame_->height(); |
| 3828 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3829 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3830 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3831 | Token::Value op = node->op(); |
| 3832 | |
| 3833 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3834 | // operators must yield the result of one of the two expressions |
| 3835 | // before any ToBoolean() conversions. This means that the value |
| 3836 | // produced by a && or || operator is not necessarily a boolean. |
| 3837 | |
| 3838 | // NOTE: If the left hand side produces a materialized value (not in |
| 3839 | // the CC register), we force the right hand side to do the |
| 3840 | // same. This is necessary because we may have to branch to the exit |
| 3841 | // after evaluating the left hand side (due to the shortcut |
| 3842 | // semantics), but the compiler must (statically) know if the result |
| 3843 | // of compiling the binary operation is materialized or not. |
| 3844 | |
| 3845 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3846 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3847 | LoadConditionAndSpill(node->left(), |
| 3848 | NOT_INSIDE_TYPEOF, |
| 3849 | &is_true, |
| 3850 | false_target(), |
| 3851 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3852 | if (has_cc()) { |
| 3853 | Branch(false, false_target()); |
| 3854 | |
| 3855 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3856 | is_true.Bind(); |
| 3857 | LoadConditionAndSpill(node->right(), |
| 3858 | NOT_INSIDE_TYPEOF, |
| 3859 | true_target(), |
| 3860 | false_target(), |
| 3861 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3862 | |
| 3863 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3864 | JumpTarget pop_and_continue; |
| 3865 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3866 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3867 | __ ldr(r0, frame_->Top()); // dup the stack top |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3868 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3869 | // Avoid popping the result if it converts to 'false' using the |
| 3870 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3871 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3872 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3873 | Branch(false, &exit); |
| 3874 | |
| 3875 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3876 | pop_and_continue.Bind(); |
| 3877 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3878 | |
| 3879 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3880 | is_true.Bind(); |
| 3881 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3882 | |
| 3883 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3884 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3888 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3889 | LoadConditionAndSpill(node->left(), |
| 3890 | NOT_INSIDE_TYPEOF, |
| 3891 | true_target(), |
| 3892 | &is_false, |
| 3893 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3894 | if (has_cc()) { |
| 3895 | Branch(true, true_target()); |
| 3896 | |
| 3897 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3898 | is_false.Bind(); |
| 3899 | LoadConditionAndSpill(node->right(), |
| 3900 | NOT_INSIDE_TYPEOF, |
| 3901 | true_target(), |
| 3902 | false_target(), |
| 3903 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3904 | |
| 3905 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3906 | JumpTarget pop_and_continue; |
| 3907 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3908 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3909 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3910 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3911 | // Avoid popping the result if it converts to 'true' using the |
| 3912 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3913 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3914 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3915 | Branch(true, &exit); |
| 3916 | |
| 3917 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3918 | pop_and_continue.Bind(); |
| 3919 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3920 | |
| 3921 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3922 | is_false.Bind(); |
| 3923 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3924 | |
| 3925 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3926 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | } else { |
| 3930 | // Optimize for the case where (at least) one of the expressions |
| 3931 | // is a literal small integer. |
| 3932 | Literal* lliteral = node->left()->AsLiteral(); |
| 3933 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3934 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 3935 | // never return a constant/immutable object. |
| 3936 | bool overwrite_left = |
| 3937 | (node->left()->AsBinaryOperation() != NULL && |
| 3938 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3939 | bool overwrite_right = |
| 3940 | (node->right()->AsBinaryOperation() != NULL && |
| 3941 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3942 | |
| 3943 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3944 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3945 | SmiOperation(node->op(), |
| 3946 | rliteral->handle(), |
| 3947 | false, |
| 3948 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3949 | |
| 3950 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3951 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3952 | SmiOperation(node->op(), |
| 3953 | lliteral->handle(), |
| 3954 | true, |
| 3955 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3956 | |
| 3957 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3958 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 3959 | if (overwrite_left) { |
| 3960 | overwrite_mode = OVERWRITE_LEFT; |
| 3961 | } else if (overwrite_right) { |
| 3962 | overwrite_mode = OVERWRITE_RIGHT; |
| 3963 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3964 | LoadAndSpill(node->left()); |
| 3965 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3966 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3967 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3968 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3969 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3970 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3971 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3975 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3976 | #ifdef DEBUG |
| 3977 | int original_height = frame_->height(); |
| 3978 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3979 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3980 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3981 | frame_->EmitPush(r0); |
| 3982 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
| 3985 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3986 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3987 | #ifdef DEBUG |
| 3988 | int original_height = frame_->height(); |
| 3989 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3990 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3991 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3992 | |
| 3993 | // Get the expressions from the node. |
| 3994 | Expression* left = node->left(); |
| 3995 | Expression* right = node->right(); |
| 3996 | Token::Value op = node->op(); |
| 3997 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3998 | // To make null checks efficient, we check if either left or right is the |
| 3999 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 4000 | // instead of calling the (very) general runtime routine for checking |
| 4001 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4002 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4003 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4004 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4005 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4006 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4007 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4008 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4009 | LoadAndSpill(left_is_null ? right : left); |
| 4010 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4011 | __ cmp(r0, Operand(Factory::null_value())); |
| 4012 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4013 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4014 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4015 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4016 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4017 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4018 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4019 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4020 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4021 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4022 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4023 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4024 | // It can be an undetectable object. |
| 4025 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4026 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 4027 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 4028 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4031 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4032 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4033 | return; |
| 4034 | } |
| 4035 | } |
| 4036 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4037 | // To make typeof testing for natives implemented in JavaScript really |
| 4038 | // efficient, we generate special code for expressions of the form: |
| 4039 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4040 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 4041 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4042 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4043 | (right->AsLiteral() != NULL && |
| 4044 | right->AsLiteral()->handle()->IsString())) { |
| 4045 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4046 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4047 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4048 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4049 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4050 | |
| 4051 | if (check->Equals(Heap::number_symbol())) { |
| 4052 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4053 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4054 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4055 | __ cmp(r1, Operand(Factory::heap_number_map())); |
| 4056 | cc_reg_ = eq; |
| 4057 | |
| 4058 | } else if (check->Equals(Heap::string_symbol())) { |
| 4059 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4060 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4061 | |
| 4062 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4063 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4064 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4065 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4066 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4067 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4068 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4069 | |
| 4070 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 4071 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 4072 | cc_reg_ = lt; |
| 4073 | |
| 4074 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 4075 | __ cmp(r1, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4076 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4077 | __ cmp(r1, Operand(Factory::false_value())); |
| 4078 | cc_reg_ = eq; |
| 4079 | |
| 4080 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 4081 | __ cmp(r1, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4082 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4083 | |
| 4084 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4085 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4086 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4087 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4088 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4089 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4090 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4091 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4092 | |
| 4093 | cc_reg_ = eq; |
| 4094 | |
| 4095 | } else if (check->Equals(Heap::function_symbol())) { |
| 4096 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4097 | false_target()->Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4098 | __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4099 | cc_reg_ = eq; |
| 4100 | |
| 4101 | } else if (check->Equals(Heap::object_symbol())) { |
| 4102 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4103 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4104 | |
| 4105 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4106 | __ cmp(r1, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4107 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4108 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4109 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4110 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 4111 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4112 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4113 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4114 | |
| 4115 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4116 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4117 | false_target()->Branch(lt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4118 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 4119 | cc_reg_ = le; |
| 4120 | |
| 4121 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4122 | // Uncommon case: typeof testing against a string literal that is |
| 4123 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4124 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4125 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4126 | ASSERT(!has_valid_frame() || |
| 4127 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4128 | return; |
| 4129 | } |
| 4130 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4131 | switch (op) { |
| 4132 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4133 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4134 | break; |
| 4135 | |
| 4136 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4137 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4138 | break; |
| 4139 | |
| 4140 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4141 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4142 | break; |
| 4143 | |
| 4144 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4145 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4146 | break; |
| 4147 | |
| 4148 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4149 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4150 | break; |
| 4151 | |
| 4152 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4153 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4154 | break; |
| 4155 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4156 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4157 | LoadAndSpill(left); |
| 4158 | LoadAndSpill(right); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4159 | Result arg_count = allocator_->Allocate(r0); |
| 4160 | ASSERT(arg_count.is_valid()); |
| 4161 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 4162 | Result result = frame_->InvokeBuiltin(Builtins::IN, |
| 4163 | CALL_JS, |
| 4164 | &arg_count, |
| 4165 | 2); |
| 4166 | frame_->EmitPush(result.reg()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4167 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4168 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4169 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4170 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4171 | LoadAndSpill(left); |
| 4172 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4173 | InstanceofStub stub; |
| 4174 | Result result = frame_->CallStub(&stub, 2); |
| 4175 | // At this point if instanceof succeeded then r0 == 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4176 | __ tst(result.reg(), Operand(result.reg())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4177 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4178 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4179 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4180 | |
| 4181 | default: |
| 4182 | UNREACHABLE(); |
| 4183 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4184 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4185 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4189 | #ifdef DEBUG |
| 4190 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4191 | #endif |
| 4192 | |
| 4193 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4194 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4195 | #define __ ACCESS_MASM(masm) |
| 4196 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4197 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4198 | Handle<String> Reference::GetName() { |
| 4199 | ASSERT(type_ == NAMED); |
| 4200 | Property* property = expression_->AsProperty(); |
| 4201 | if (property == NULL) { |
| 4202 | // Global variable reference treated as a named property reference. |
| 4203 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4204 | ASSERT(proxy->AsVariable() != NULL); |
| 4205 | ASSERT(proxy->AsVariable()->is_global()); |
| 4206 | return proxy->name(); |
| 4207 | } else { |
| 4208 | Literal* raw_name = property->key()->AsLiteral(); |
| 4209 | ASSERT(raw_name != NULL); |
| 4210 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4211 | } |
| 4212 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4213 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4214 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4215 | void Reference::GetValueAndSpill(TypeofState typeof_state) { |
| 4216 | ASSERT(cgen_->in_spilled_code()); |
| 4217 | cgen_->set_in_spilled_code(false); |
| 4218 | GetValue(typeof_state); |
| 4219 | cgen_->frame()->SpillAll(); |
| 4220 | cgen_->set_in_spilled_code(true); |
| 4221 | } |
| 4222 | |
| 4223 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4224 | void Reference::GetValue(TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4225 | ASSERT(!cgen_->in_spilled_code()); |
| 4226 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4227 | ASSERT(!is_illegal()); |
| 4228 | ASSERT(!cgen_->has_cc()); |
| 4229 | MacroAssembler* masm = cgen_->masm(); |
| 4230 | Property* property = expression_->AsProperty(); |
| 4231 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4232 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | switch (type_) { |
| 4236 | case SLOT: { |
| 4237 | Comment cmnt(masm, "[ Load from Slot"); |
| 4238 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4239 | ASSERT(slot != NULL); |
| 4240 | cgen_->LoadFromSlot(slot, typeof_state); |
| 4241 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4244 | case NAMED: { |
| 4245 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4246 | // distinction between expressions in a typeof and not in a typeof. If |
| 4247 | // there is a chance that reference errors can be thrown below, we |
| 4248 | // must distinguish between the two kinds of loads (typeof expression |
| 4249 | // loads must not throw a reference error). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4250 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4251 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4252 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4253 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4254 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4255 | // Setup the name register. |
| 4256 | Result name_reg = cgen_->allocator()->Allocate(r2); |
| 4257 | ASSERT(name_reg.is_valid()); |
| 4258 | __ mov(name_reg.reg(), Operand(name)); |
| 4259 | ASSERT(var == NULL || var->is_global()); |
| 4260 | RelocInfo::Mode rmode = (var == NULL) |
| 4261 | ? RelocInfo::CODE_TARGET |
| 4262 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4263 | Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4264 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4265 | break; |
| 4266 | } |
| 4267 | |
| 4268 | case KEYED: { |
| 4269 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4270 | // 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] | 4271 | |
| 4272 | // TODO(181): Implement inlined version of array indexing once |
| 4273 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4274 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4275 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4276 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4277 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4278 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4279 | ASSERT(var == NULL || var->is_global()); |
| 4280 | RelocInfo::Mode rmode = (var == NULL) |
| 4281 | ? RelocInfo::CODE_TARGET |
| 4282 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4283 | Result answer = frame->CallCodeObject(ic, rmode, 0); |
| 4284 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4285 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | default: |
| 4289 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4290 | } |
| 4291 | } |
| 4292 | |
| 4293 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4294 | void Reference::SetValue(InitState init_state) { |
| 4295 | ASSERT(!is_illegal()); |
| 4296 | ASSERT(!cgen_->has_cc()); |
| 4297 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4298 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4299 | Property* property = expression_->AsProperty(); |
| 4300 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4301 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4302 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4303 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4304 | switch (type_) { |
| 4305 | case SLOT: { |
| 4306 | Comment cmnt(masm, "[ Store to Slot"); |
| 4307 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4308 | ASSERT(slot != NULL); |
| 4309 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4310 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4311 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4312 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4313 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4314 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4315 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4316 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4317 | if (init_state == CONST_INIT) { |
| 4318 | // Same as the case for a normal store, but ignores attribute |
| 4319 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4320 | // const properties (introduced via eval("const foo = (some |
| 4321 | // expr);")). Also, uses the current function context instead of |
| 4322 | // the top context. |
| 4323 | // |
| 4324 | // Note that we must declare the foo upon entry of eval(), via a |
| 4325 | // context slot declaration, but we cannot initialize it at the |
| 4326 | // same time, because the const declaration may be at the end of |
| 4327 | // the eval code (sigh...) and the const variable may have been |
| 4328 | // used before (where its value is 'undefined'). Thus, we can only |
| 4329 | // do the initialization when we actually encounter the expression |
| 4330 | // and when the expression operands are defined and valid, and |
| 4331 | // thus we need the split into 2 operations: declaration of the |
| 4332 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4333 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4334 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4335 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4336 | } |
| 4337 | // Storing a variable must keep the (new) value on the expression |
| 4338 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4339 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4340 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4341 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4342 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4343 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4344 | JumpTarget exit; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4345 | if (init_state == CONST_INIT) { |
| 4346 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4347 | // Only the first const initialization must be executed (the slot |
| 4348 | // still contains 'the hole' value). When the assignment is |
| 4349 | // executed, the code is identical to a normal store (see below). |
| 4350 | Comment cmnt(masm, "[ Init const"); |
| 4351 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
| 4352 | __ cmp(r2, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4353 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4354 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4355 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4356 | // We must execute the store. Storing a variable must keep the |
| 4357 | // (new) value on the stack. This is necessary for compiling |
| 4358 | // assignment expressions. |
| 4359 | // |
| 4360 | // Note: We will reach here even with slot->var()->mode() == |
| 4361 | // Variable::CONST because of const declarations which will |
| 4362 | // initialize consts to 'the hole' value and by doing so, end up |
| 4363 | // calling this code. r2 may be loaded with context; used below in |
| 4364 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4365 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4366 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4367 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4368 | if (slot->type() == Slot::CONTEXT) { |
| 4369 | // Skip write barrier if the written value is a smi. |
| 4370 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4371 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4372 | // r2 is loaded with context when calling SlotOperand above. |
| 4373 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4374 | __ mov(r3, Operand(offset)); |
| 4375 | __ RecordWrite(r2, r3, r1); |
| 4376 | } |
| 4377 | // If we definitely did not jump over the assignment, we do not need |
| 4378 | // to bind the exit label. Doing so can defeat peephole |
| 4379 | // optimization. |
| 4380 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4381 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4382 | } |
| 4383 | } |
| 4384 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4387 | case NAMED: { |
| 4388 | Comment cmnt(masm, "[ Store to named Property"); |
| 4389 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4390 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4391 | Handle<String> name(GetName()); |
| 4392 | |
| 4393 | Result value = cgen_->allocator()->Allocate(r0); |
| 4394 | ASSERT(value.is_valid()); |
| 4395 | frame->EmitPop(value.reg()); |
| 4396 | |
| 4397 | // Setup the name register. |
| 4398 | Result property_name = cgen_->allocator()->Allocate(r2); |
| 4399 | ASSERT(property_name.is_valid()); |
| 4400 | __ mov(property_name.reg(), Operand(name)); |
| 4401 | Result answer = frame->CallCodeObject(ic, |
| 4402 | RelocInfo::CODE_TARGET, |
| 4403 | &value, |
| 4404 | &property_name, |
| 4405 | 0); |
| 4406 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4407 | break; |
| 4408 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4409 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4410 | case KEYED: { |
| 4411 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4412 | Property* property = expression_->AsProperty(); |
| 4413 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4414 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4415 | |
| 4416 | // Call IC code. |
| 4417 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4418 | // TODO(1222589): Make the IC grab the values from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4419 | Result value = cgen_->allocator()->Allocate(r0); |
| 4420 | ASSERT(value.is_valid()); |
| 4421 | frame->EmitPop(value.reg()); // value |
| 4422 | Result result = |
| 4423 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4424 | frame->EmitPush(result.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4425 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4426 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4427 | |
| 4428 | default: |
| 4429 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4430 | } |
| 4431 | } |
| 4432 | |
| 4433 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4434 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4435 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4436 | // (31 instead of 32). |
| 4437 | static void CountLeadingZeros( |
| 4438 | MacroAssembler* masm, |
| 4439 | Register source, |
| 4440 | Register scratch, |
| 4441 | Register zeros) { |
| 4442 | #ifdef __ARM_ARCH_5__ |
| 4443 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4444 | #else |
| 4445 | __ mov(zeros, Operand(0)); |
| 4446 | __ mov(scratch, source); |
| 4447 | // Top 16. |
| 4448 | __ tst(scratch, Operand(0xffff0000)); |
| 4449 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4450 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4451 | // Top 8. |
| 4452 | __ tst(scratch, Operand(0xff000000)); |
| 4453 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4454 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4455 | // Top 4. |
| 4456 | __ tst(scratch, Operand(0xf0000000)); |
| 4457 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4458 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4459 | // Top 2. |
| 4460 | __ tst(scratch, Operand(0xc0000000)); |
| 4461 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4462 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4463 | // Top bit. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 4464 | __ tst(scratch, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4465 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4466 | #endif |
| 4467 | } |
| 4468 | |
| 4469 | |
| 4470 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4471 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4472 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4473 | // scratch register. Destroys the source register. No GC occurs during this |
| 4474 | // stub so you don't have to set up the frame. |
| 4475 | class ConvertToDoubleStub : public CodeStub { |
| 4476 | public: |
| 4477 | ConvertToDoubleStub(Register result_reg_1, |
| 4478 | Register result_reg_2, |
| 4479 | Register source_reg, |
| 4480 | Register scratch_reg) |
| 4481 | : result1_(result_reg_1), |
| 4482 | result2_(result_reg_2), |
| 4483 | source_(source_reg), |
| 4484 | zeros_(scratch_reg) { } |
| 4485 | |
| 4486 | private: |
| 4487 | Register result1_; |
| 4488 | Register result2_; |
| 4489 | Register source_; |
| 4490 | Register zeros_; |
| 4491 | |
| 4492 | // Minor key encoding in 16 bits. |
| 4493 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4494 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4495 | |
| 4496 | Major MajorKey() { return ConvertToDouble; } |
| 4497 | int MinorKey() { |
| 4498 | // Encode the parameters in a unique 16 bit value. |
| 4499 | return result1_.code() + |
| 4500 | (result2_.code() << 4) + |
| 4501 | (source_.code() << 8) + |
| 4502 | (zeros_.code() << 12); |
| 4503 | } |
| 4504 | |
| 4505 | void Generate(MacroAssembler* masm); |
| 4506 | |
| 4507 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4508 | |
| 4509 | #ifdef DEBUG |
| 4510 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4511 | #endif |
| 4512 | }; |
| 4513 | |
| 4514 | |
| 4515 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4516 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4517 | Register exponent = result1_; |
| 4518 | Register mantissa = result2_; |
| 4519 | #else |
| 4520 | Register exponent = result2_; |
| 4521 | Register mantissa = result1_; |
| 4522 | #endif |
| 4523 | Label not_special; |
| 4524 | // Convert from Smi to integer. |
| 4525 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4526 | // Move sign bit from source to destination. This works because the sign bit |
| 4527 | // in the exponent word of the double has the same position and polarity as |
| 4528 | // the 2's complement sign bit in a Smi. |
| 4529 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4530 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4531 | // Subtract from 0 if source was negative. |
| 4532 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4533 | __ cmp(source_, Operand(1)); |
| 4534 | __ b(gt, ¬_special); |
| 4535 | |
| 4536 | // We have -1, 0 or 1, which we treat specially. |
| 4537 | __ cmp(source_, Operand(0)); |
| 4538 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4539 | static const uint32_t exponent_word_for_1 = |
| 4540 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4541 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4542 | // 1, 0 and -1 all have 0 for the second word. |
| 4543 | __ mov(mantissa, Operand(0)); |
| 4544 | __ Ret(); |
| 4545 | |
| 4546 | __ bind(¬_special); |
| 4547 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4548 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4549 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4550 | // Compute exponent and or it into the exponent register. |
| 4551 | // We use result2 as a scratch register here. |
| 4552 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4553 | __ orr(exponent, |
| 4554 | exponent, |
| 4555 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4556 | // Shift up the source chopping the top bit off. |
| 4557 | __ add(zeros_, zeros_, Operand(1)); |
| 4558 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4559 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4560 | // Compute lower part of fraction (last 12 bits). |
| 4561 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4562 | // And the top (top 20 bits). |
| 4563 | __ orr(exponent, |
| 4564 | exponent, |
| 4565 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4566 | __ Ret(); |
| 4567 | } |
| 4568 | |
| 4569 | |
| 4570 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4571 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4572 | // so you don't have to set up the frame. |
| 4573 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4574 | public: |
| 4575 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4576 | Register the_heap_number, |
| 4577 | Register scratch) |
| 4578 | : the_int_(the_int), |
| 4579 | the_heap_number_(the_heap_number), |
| 4580 | scratch_(scratch) { } |
| 4581 | |
| 4582 | private: |
| 4583 | Register the_int_; |
| 4584 | Register the_heap_number_; |
| 4585 | Register scratch_; |
| 4586 | |
| 4587 | // Minor key encoding in 16 bits. |
| 4588 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4589 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4590 | |
| 4591 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4592 | int MinorKey() { |
| 4593 | // Encode the parameters in a unique 16 bit value. |
| 4594 | return the_int_.code() + |
| 4595 | (the_heap_number_.code() << 4) + |
| 4596 | (scratch_.code() << 8); |
| 4597 | } |
| 4598 | |
| 4599 | void Generate(MacroAssembler* masm); |
| 4600 | |
| 4601 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4602 | |
| 4603 | #ifdef DEBUG |
| 4604 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4605 | #endif |
| 4606 | }; |
| 4607 | |
| 4608 | |
| 4609 | // See comment for class. |
| 4610 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) { |
| 4611 | Label max_negative_int; |
| 4612 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4613 | // We test for the special value that has a different exponent. This test |
| 4614 | // has the neat side effect of setting the flags according to the sign. |
| 4615 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 4616 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4617 | __ b(eq, &max_negative_int); |
| 4618 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4619 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4620 | uint32_t non_smi_exponent = |
| 4621 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4622 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4623 | // Set the sign bit in scratch_ if the value was negative. |
| 4624 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4625 | // Subtract from 0 if the value was negative. |
| 4626 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4627 | // We should be masking the implict first digit of the mantissa away here, |
| 4628 | // but it just ends up combining harmlessly with the last digit of the |
| 4629 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4630 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4631 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4632 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4633 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4634 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4635 | HeapNumber::kExponentOffset)); |
| 4636 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4637 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4638 | HeapNumber::kMantissaOffset)); |
| 4639 | __ Ret(); |
| 4640 | |
| 4641 | __ bind(&max_negative_int); |
| 4642 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4643 | // a double because it uses a sign bit instead of using two's complement. |
| 4644 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4645 | // significant 1 bit is not stored. |
| 4646 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4647 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4648 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4649 | __ mov(ip, Operand(0)); |
| 4650 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4651 | __ Ret(); |
| 4652 | } |
| 4653 | |
| 4654 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4655 | // Handle the case where the lhs and rhs are the same object. |
| 4656 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4657 | // for "identity and not NaN". |
| 4658 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4659 | Label* slow, |
| 4660 | Condition cc) { |
| 4661 | Label not_identical; |
| 4662 | __ cmp(r0, Operand(r1)); |
| 4663 | __ b(ne, ¬_identical); |
| 4664 | |
| 4665 | Register exp_mask_reg = r5; |
| 4666 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4667 | |
| 4668 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4669 | // so we do the second best thing - test it ourselves. |
| 4670 | Label heap_number, return_equal; |
| 4671 | // They are both equal and they are not both Smis so both of them are not |
| 4672 | // Smis. If it's not a heap number, then return equal. |
| 4673 | if (cc == lt || cc == gt) { |
| 4674 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
| 4675 | __ b(ge, slow); |
| 4676 | } else { |
| 4677 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4678 | __ b(eq, &heap_number); |
| 4679 | // Comparing JS objects with <=, >= is complicated. |
| 4680 | if (cc != eq) { |
| 4681 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4682 | __ b(ge, slow); |
| 4683 | } |
| 4684 | } |
| 4685 | __ bind(&return_equal); |
| 4686 | if (cc == lt) { |
| 4687 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4688 | } else if (cc == gt) { |
| 4689 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4690 | } else { |
| 4691 | __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves. |
| 4692 | } |
| 4693 | __ mov(pc, Operand(lr)); // Return. |
| 4694 | |
| 4695 | // For less and greater we don't have to check for NaN since the result of |
| 4696 | // x < x is false regardless. For the others here is some code to check |
| 4697 | // for NaN. |
| 4698 | if (cc != lt && cc != gt) { |
| 4699 | __ bind(&heap_number); |
| 4700 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4701 | // not NaN. |
| 4702 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4703 | // and not all mantissa bits (0..51) clear. |
| 4704 | // Read top bits of double representation (second word of value). |
| 4705 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4706 | // Test that exponent bits are all set. |
| 4707 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4708 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4709 | __ b(ne, &return_equal); |
| 4710 | |
| 4711 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4712 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4713 | // Or with all low-bits of mantissa. |
| 4714 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4715 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4716 | // For equal we already have the right value in r0: Return zero (equal) |
| 4717 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if not |
| 4718 | // (it's a NaN). For <= and >= we need to load r0 with the failing value |
| 4719 | // if it's a NaN. |
| 4720 | if (cc != eq) { |
| 4721 | // All-zero means Infinity means equal. |
| 4722 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 4723 | if (cc == le) { |
| 4724 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 4725 | } else { |
| 4726 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 4727 | } |
| 4728 | } |
| 4729 | __ mov(pc, Operand(lr)); // Return. |
| 4730 | } |
| 4731 | // No fall through here. |
| 4732 | |
| 4733 | __ bind(¬_identical); |
| 4734 | } |
| 4735 | |
| 4736 | |
| 4737 | // See comment at call site. |
| 4738 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 4739 | Label* rhs_not_nan, |
| 4740 | Label* slow, |
| 4741 | bool strict) { |
| 4742 | Label lhs_is_smi; |
| 4743 | __ tst(r0, Operand(kSmiTagMask)); |
| 4744 | __ b(eq, &lhs_is_smi); |
| 4745 | |
| 4746 | // Rhs is a Smi. Check whether the non-smi is a heap number. |
| 4747 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4748 | if (strict) { |
| 4749 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4750 | // succeed. Return non-equal (r0 is already not zero) |
| 4751 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4752 | } else { |
| 4753 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4754 | // the runtime. |
| 4755 | __ b(ne, slow); |
| 4756 | } |
| 4757 | |
| 4758 | // Rhs is a smi, lhs is a number. |
| 4759 | __ push(lr); |
| 4760 | __ mov(r7, Operand(r1)); |
| 4761 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4762 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4763 | // r3 and r2 are rhs as double. |
| 4764 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4765 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4766 | // We now have both loaded as doubles but we can skip the lhs nan check |
| 4767 | // since it's a Smi. |
| 4768 | __ pop(lr); |
| 4769 | __ jmp(rhs_not_nan); |
| 4770 | |
| 4771 | __ bind(&lhs_is_smi); |
| 4772 | // Lhs is a Smi. Check whether the non-smi is a heap number. |
| 4773 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4774 | if (strict) { |
| 4775 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4776 | // succeed. Return non-equal. |
| 4777 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 4778 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4779 | } else { |
| 4780 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4781 | // the runtime. |
| 4782 | __ b(ne, slow); |
| 4783 | } |
| 4784 | |
| 4785 | // Lhs is a smi, rhs is a number. |
| 4786 | // r0 is Smi and r1 is heap number. |
| 4787 | __ push(lr); |
| 4788 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4789 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4790 | __ mov(r7, Operand(r0)); |
| 4791 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4792 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4793 | __ pop(lr); |
| 4794 | // Fall through to both_loaded_as_doubles. |
| 4795 | } |
| 4796 | |
| 4797 | |
| 4798 | void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) { |
| 4799 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4800 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4801 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4802 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4803 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4804 | Label one_is_nan, neither_is_nan; |
| 4805 | |
| 4806 | Register exp_mask_reg = r5; |
| 4807 | |
| 4808 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4809 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 4810 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4811 | __ b(ne, rhs_not_nan); |
| 4812 | __ mov(r4, |
| 4813 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4814 | SetCC); |
| 4815 | __ b(ne, &one_is_nan); |
| 4816 | __ cmp(rhs_mantissa, Operand(0)); |
| 4817 | __ b(ne, &one_is_nan); |
| 4818 | |
| 4819 | __ bind(rhs_not_nan); |
| 4820 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4821 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 4822 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4823 | __ b(ne, &neither_is_nan); |
| 4824 | __ mov(r4, |
| 4825 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4826 | SetCC); |
| 4827 | __ b(ne, &one_is_nan); |
| 4828 | __ cmp(lhs_mantissa, Operand(0)); |
| 4829 | __ b(eq, &neither_is_nan); |
| 4830 | |
| 4831 | __ bind(&one_is_nan); |
| 4832 | // NaN comparisons always fail. |
| 4833 | // Load whatever we need in r0 to make the comparison fail. |
| 4834 | if (cc == lt || cc == le) { |
| 4835 | __ mov(r0, Operand(GREATER)); |
| 4836 | } else { |
| 4837 | __ mov(r0, Operand(LESS)); |
| 4838 | } |
| 4839 | __ mov(pc, Operand(lr)); // Return. |
| 4840 | |
| 4841 | __ bind(&neither_is_nan); |
| 4842 | } |
| 4843 | |
| 4844 | |
| 4845 | // See comment at call site. |
| 4846 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 4847 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4848 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4849 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4850 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4851 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4852 | |
| 4853 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 4854 | if (cc == eq) { |
| 4855 | // Doubles are not equal unless they have the same bit pattern. |
| 4856 | // Exception: 0 and -0. |
| 4857 | __ cmp(lhs_mantissa, Operand(rhs_mantissa)); |
| 4858 | __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne); |
| 4859 | // Return non-zero if the numbers are unequal. |
| 4860 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 4861 | |
| 4862 | __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC); |
| 4863 | // If exponents are equal then return 0. |
| 4864 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 4865 | |
| 4866 | // Exponents are unequal. The only way we can return that the numbers |
| 4867 | // are equal is if one is -0 and the other is 0. We already dealt |
| 4868 | // with the case where both are -0 or both are 0. |
| 4869 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 4870 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 4871 | // equal. |
| 4872 | __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC); |
| 4873 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 4874 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 4875 | // Now they are equal if and only if the lhs exponent is zero in its |
| 4876 | // low 31 bits. |
| 4877 | __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize)); |
| 4878 | __ mov(pc, Operand(lr)); |
| 4879 | } else { |
| 4880 | // Call a native function to do a comparison between two non-NaNs. |
| 4881 | // Call C routine that may not cause GC or other trouble. |
| 4882 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 4883 | __ Jump(r5); // Tail call. |
| 4884 | } |
| 4885 | } |
| 4886 | |
| 4887 | |
| 4888 | // See comment at call site. |
| 4889 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 4890 | // If either operand is a JSObject or an oddball value, then they are |
| 4891 | // not equal since their pointers are different. |
| 4892 | // There is no test for undetectability in strict equality. |
| 4893 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 4894 | Label first_non_object; |
| 4895 | // Get the type of the first operand into r2 and compare it with |
| 4896 | // FIRST_JS_OBJECT_TYPE. |
| 4897 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 4898 | __ b(lt, &first_non_object); |
| 4899 | |
| 4900 | // Return non-zero (r0 is not zero) |
| 4901 | Label return_not_equal; |
| 4902 | __ bind(&return_not_equal); |
| 4903 | __ mov(pc, Operand(lr)); // Return. |
| 4904 | |
| 4905 | __ bind(&first_non_object); |
| 4906 | // Check for oddballs: true, false, null, undefined. |
| 4907 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 4908 | __ b(eq, &return_not_equal); |
| 4909 | |
| 4910 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 4911 | __ b(ge, &return_not_equal); |
| 4912 | |
| 4913 | // Check for oddballs: true, false, null, undefined. |
| 4914 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 4915 | __ b(eq, &return_not_equal); |
| 4916 | } |
| 4917 | |
| 4918 | |
| 4919 | // See comment at call site. |
| 4920 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 4921 | Label* both_loaded_as_doubles, |
| 4922 | Label* not_heap_numbers, |
| 4923 | Label* slow) { |
| 4924 | __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE); |
| 4925 | __ b(ne, not_heap_numbers); |
| 4926 | __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE); |
| 4927 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 4928 | |
| 4929 | // Both are heap numbers. Load them up then jump to the code we have |
| 4930 | // for that. |
| 4931 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4932 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4933 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4934 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4935 | __ jmp(both_loaded_as_doubles); |
| 4936 | } |
| 4937 | |
| 4938 | |
| 4939 | // Fast negative check for symbol-to-symbol equality. |
| 4940 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 4941 | // r2 is object type of r0. |
| 4942 | __ tst(r2, Operand(kIsNotStringMask)); |
| 4943 | __ b(ne, slow); |
| 4944 | __ tst(r2, Operand(kIsSymbolMask)); |
| 4945 | __ b(eq, slow); |
| 4946 | __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE); |
| 4947 | __ b(ge, slow); |
| 4948 | __ tst(r3, Operand(kIsSymbolMask)); |
| 4949 | __ b(eq, slow); |
| 4950 | |
| 4951 | // Both are symbols. We already checked they weren't the same pointer |
| 4952 | // so they are not equal. |
| 4953 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 4954 | __ mov(pc, Operand(lr)); // Return. |
| 4955 | } |
| 4956 | |
| 4957 | |
| 4958 | // On entry r0 and r1 are the things to be compared. On exit r0 is 0, |
| 4959 | // positive or negative to indicate the result of the comparison. |
| 4960 | void CompareStub::Generate(MacroAssembler* masm) { |
| 4961 | Label slow; // Call builtin. |
| 4962 | Label not_smis, both_loaded_as_doubles, rhs_not_nan; |
| 4963 | |
| 4964 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 4965 | // it is certain that at least one operand isn't a smi. |
| 4966 | |
| 4967 | // Handle the case where the objects are identical. Either returns the answer |
| 4968 | // or goes to slow. Only falls through if the objects were not identical. |
| 4969 | EmitIdenticalObjectComparison(masm, &slow, cc_); |
| 4970 | |
| 4971 | // If either is a Smi (we know that not both are), then they can only |
| 4972 | // be strictly equal if the other is a HeapNumber. |
| 4973 | ASSERT_EQ(0, kSmiTag); |
| 4974 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 4975 | __ and_(r2, r0, Operand(r1)); |
| 4976 | __ tst(r2, Operand(kSmiTagMask)); |
| 4977 | __ b(ne, ¬_smis); |
| 4978 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 4979 | // 1) Return the answer. |
| 4980 | // 2) Go to slow. |
| 4981 | // 3) Fall through to both_loaded_as_doubles. |
| 4982 | // 4) Jump to rhs_not_nan. |
| 4983 | // In cases 3 and 4 we have found out we were dealing with a number-number |
| 4984 | // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles. |
| 4985 | EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_); |
| 4986 | |
| 4987 | __ bind(&both_loaded_as_doubles); |
| 4988 | // r0, r1, r2, r3 are the double representations of the left hand side |
| 4989 | // and the right hand side. |
| 4990 | |
| 4991 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 4992 | // fall through if neither is a NaN. Also binds rhs_not_nan. |
| 4993 | EmitNanCheck(masm, &rhs_not_nan, cc_); |
| 4994 | |
| 4995 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 4996 | // answer. Never falls through. |
| 4997 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 4998 | |
| 4999 | __ bind(¬_smis); |
| 5000 | // At this point we know we are dealing with two different objects, |
| 5001 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 5002 | if (strict_) { |
| 5003 | // This returns non-equal for some object types, or falls through if it |
| 5004 | // was not lucky. |
| 5005 | EmitStrictTwoHeapObjectCompare(masm); |
| 5006 | } |
| 5007 | |
| 5008 | Label check_for_symbols; |
| 5009 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 5010 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 5011 | // that case. If the inputs are not doubles then jumps to check_for_symbols. |
| 5012 | // In this case r2 will contain the type of r0. |
| 5013 | EmitCheckForTwoHeapNumbers(masm, |
| 5014 | &both_loaded_as_doubles, |
| 5015 | &check_for_symbols, |
| 5016 | &slow); |
| 5017 | |
| 5018 | __ bind(&check_for_symbols); |
| 5019 | if (cc_ == eq) { |
| 5020 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5021 | // of r0 on entry. |
| 5022 | EmitCheckForSymbols(masm, &slow); |
| 5023 | } |
| 5024 | |
| 5025 | __ bind(&slow); |
| 5026 | __ push(lr); |
| 5027 | __ push(r1); |
| 5028 | __ push(r0); |
| 5029 | // Figure out which native to call and setup the arguments. |
| 5030 | Builtins::JavaScript native; |
| 5031 | int arg_count = 1; // Not counting receiver. |
| 5032 | if (cc_ == eq) { |
| 5033 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 5034 | } else { |
| 5035 | native = Builtins::COMPARE; |
| 5036 | int ncr; // NaN compare result |
| 5037 | if (cc_ == lt || cc_ == le) { |
| 5038 | ncr = GREATER; |
| 5039 | } else { |
| 5040 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 5041 | ncr = LESS; |
| 5042 | } |
| 5043 | arg_count++; |
| 5044 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 5045 | __ push(r0); |
| 5046 | } |
| 5047 | |
| 5048 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 5049 | // tagged as a small integer. |
| 5050 | __ mov(r0, Operand(arg_count)); |
| 5051 | __ InvokeBuiltin(native, CALL_JS); |
| 5052 | __ cmp(r0, Operand(0)); |
| 5053 | __ pop(pc); |
| 5054 | } |
| 5055 | |
| 5056 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5057 | // Allocates a heap number or jumps to the label if the young space is full and |
| 5058 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5059 | static void AllocateHeapNumber( |
| 5060 | MacroAssembler* masm, |
| 5061 | Label* need_gc, // Jump here if young space is full. |
| 5062 | Register result_reg, // The tagged address of the new heap number. |
| 5063 | Register allocation_top_addr_reg, // A scratch register. |
| 5064 | Register scratch2) { // Another scratch register. |
| 5065 | ExternalReference allocation_top = |
| 5066 | ExternalReference::new_space_allocation_top_address(); |
| 5067 | ExternalReference allocation_limit = |
| 5068 | ExternalReference::new_space_allocation_limit_address(); |
| 5069 | |
| 5070 | // allocat := the address of the allocation top variable. |
| 5071 | __ mov(allocation_top_addr_reg, Operand(allocation_top)); |
| 5072 | // result_reg := the old allocation top. |
| 5073 | __ ldr(result_reg, MemOperand(allocation_top_addr_reg)); |
| 5074 | // scratch2 := the address of the allocation limit. |
| 5075 | __ mov(scratch2, Operand(allocation_limit)); |
| 5076 | // scratch2 := the allocation limit. |
| 5077 | __ ldr(scratch2, MemOperand(scratch2)); |
| 5078 | // result_reg := the new allocation top. |
| 5079 | __ add(result_reg, result_reg, Operand(HeapNumber::kSize)); |
| 5080 | // Compare new new allocation top and limit. |
| 5081 | __ cmp(result_reg, Operand(scratch2)); |
| 5082 | // Branch if out of space in young generation. |
| 5083 | __ b(hi, need_gc); |
| 5084 | // Store new allocation top. |
| 5085 | __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top |
| 5086 | // Tag and adjust back to start of new object. |
| 5087 | __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag)); |
| 5088 | // Get heap number map into scratch2. |
| 5089 | __ mov(scratch2, Operand(Factory::heap_number_map())); |
| 5090 | // Store heap number map in new object. |
| 5091 | __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset)); |
| 5092 | } |
| 5093 | |
| 5094 | |
| 5095 | // We fall into this code if the operands were Smis, but the result was |
| 5096 | // 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] | 5097 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 5098 | // to call the C-implemented binary fp operation routines we need to end up |
| 5099 | // with the double precision floating point operands in r0 and r1 (for the |
| 5100 | // 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] | 5101 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 5102 | Label* not_smi, |
| 5103 | const Builtins::JavaScript& builtin, |
| 5104 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5105 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5106 | Label slow, slow_pop_2_first, do_the_call; |
| 5107 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 5108 | // Smi-smi case (overflow). |
| 5109 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 5110 | // The new heap number is in r5. r6 and r7 are scratch. |
| 5111 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5112 | // 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] | 5113 | __ mov(r7, Operand(r0)); |
| 5114 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5115 | __ push(lr); |
| 5116 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 5117 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 5118 | __ mov(r7, Operand(r1)); |
| 5119 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5120 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 5121 | __ pop(lr); |
| 5122 | __ jmp(&do_the_call); // Tail call. No return. |
| 5123 | |
| 5124 | // We jump to here if something goes wrong (one param is not a number of any |
| 5125 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5126 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5127 | __ push(r1); |
| 5128 | __ push(r0); |
| 5129 | __ mov(r0, Operand(1)); // Set number of arguments. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5130 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5131 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5132 | // 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] | 5133 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5134 | if (mode == NO_OVERWRITE) { |
| 5135 | // In the case where there is no chance of an overwritable float we may as |
| 5136 | // well do the allocation immediately while r0 and r1 are untouched. |
| 5137 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5138 | } |
| 5139 | |
| 5140 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5141 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5142 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5143 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5144 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5145 | if (mode == OVERWRITE_RIGHT) { |
| 5146 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5147 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5148 | // Calling convention says that second double is in r2 and r3. |
| 5149 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5150 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5151 | __ jmp(&finished_loading_r0); |
| 5152 | __ bind(&r0_is_smi); |
| 5153 | if (mode == OVERWRITE_RIGHT) { |
| 5154 | // 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] | 5155 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5156 | } |
| 5157 | // Write Smi from r0 to r3 and r2 in double format. |
| 5158 | __ mov(r7, Operand(r0)); |
| 5159 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5160 | __ push(lr); |
| 5161 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5162 | __ pop(lr); |
| 5163 | __ bind(&finished_loading_r0); |
| 5164 | |
| 5165 | // Move r1 to a double in r0-r1. |
| 5166 | __ tst(r1, Operand(kSmiTagMask)); |
| 5167 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5168 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5169 | __ b(ne, &slow); |
| 5170 | if (mode == OVERWRITE_LEFT) { |
| 5171 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5172 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5173 | // Calling convention says that first double is in r0 and r1. |
| 5174 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5175 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5176 | __ jmp(&finished_loading_r1); |
| 5177 | __ bind(&r1_is_smi); |
| 5178 | if (mode == OVERWRITE_LEFT) { |
| 5179 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5180 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5181 | } |
| 5182 | // Write Smi from r1 to r1 and r0 in double format. |
| 5183 | __ mov(r7, Operand(r1)); |
| 5184 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5185 | __ push(lr); |
| 5186 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5187 | __ pop(lr); |
| 5188 | __ bind(&finished_loading_r1); |
| 5189 | |
| 5190 | __ bind(&do_the_call); |
| 5191 | // r0: Left value (least significant part of mantissa). |
| 5192 | // r1: Left value (sign, exponent, top of mantissa). |
| 5193 | // r2: Right value (least significant part of mantissa). |
| 5194 | // r3: Right value (sign, exponent, top of mantissa). |
| 5195 | // r5: Address of heap number for result. |
| 5196 | __ push(lr); // For later. |
| 5197 | __ push(r5); // Address of heap number that is answer. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5198 | // Call C routine that may not cause GC or other trouble. |
| 5199 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5200 | __ Call(r5); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5201 | // Store answer in the overwritable heap number. |
| 5202 | __ pop(r4); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5203 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5204 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5205 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5206 | // substract the tag from r4. |
| 5207 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5208 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5209 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5210 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5211 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5212 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5213 | #endif |
| 5214 | __ mov(r0, Operand(r4)); |
| 5215 | // And we are done. |
| 5216 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5220 | // 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] | 5221 | // 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] | 5222 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5223 | // 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] | 5224 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5225 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5226 | static void GetInt32(MacroAssembler* masm, |
| 5227 | Register source, |
| 5228 | Register dest, |
| 5229 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5230 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5231 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5232 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5233 | // Get exponent word. |
| 5234 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5235 | // Get exponent alone in scratch2. |
| 5236 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5237 | // Load dest with zero. We use this either for the final shift or |
| 5238 | // for the answer. |
| 5239 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5240 | // 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] | 5241 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5242 | // the exponent that we are fastest at and also the highest exponent we can |
| 5243 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5244 | const uint32_t non_smi_exponent = |
| 5245 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5246 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5247 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5248 | __ b(eq, &right_exponent); |
| 5249 | // If the exponent is higher than that then go to slow case. This catches |
| 5250 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5251 | __ b(gt, slow); |
| 5252 | |
| 5253 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5254 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5255 | // it rounds to zero. |
| 5256 | const uint32_t zero_exponent = |
| 5257 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5258 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5259 | // Dest already has a Smi zero. |
| 5260 | __ b(lt, &done); |
| 5261 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5262 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5263 | // We now have the exponent in dest. Subtract from 30 to get |
| 5264 | // how much to shift down. |
| 5265 | __ rsb(dest, dest, Operand(30)); |
| 5266 | |
| 5267 | __ bind(&right_exponent); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5268 | // Get the top bits of the mantissa. |
| 5269 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5270 | // Put back the implicit 1. |
| 5271 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5272 | // Shift up the mantissa bits to take up the space the exponent used to take. |
| 5273 | // We just orred in the implicit bit so that took care of one and we want to |
| 5274 | // leave the sign bit 0 so we subtract 2 bits from the shift distance. |
| 5275 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5276 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5277 | // Put sign in zero flag. |
| 5278 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5279 | // Get the second half of the double. For some exponents we don't actually |
| 5280 | // need this because the bits get shifted out again, but it's probably slower |
| 5281 | // to test than just to do it. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5282 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5283 | // Shift down 22 bits to get the last 10 bits. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5284 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5285 | // Move down according to the exponent. |
| 5286 | __ mov(dest, Operand(scratch, LSR, dest)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5287 | // Fix sign if sign bit was set. |
| 5288 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5289 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | |
| 5293 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5294 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5295 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5296 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5297 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5298 | // write the answer into (either by allocating or by overwriting). |
| 5299 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5300 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5301 | Label slow, result_not_a_smi; |
| 5302 | Label r0_is_smi, r1_is_smi; |
| 5303 | Label done_checking_r0, done_checking_r1; |
| 5304 | |
| 5305 | __ tst(r1, Operand(kSmiTagMask)); |
| 5306 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5307 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5308 | __ b(ne, &slow); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5309 | GetInt32(masm, r1, r3, r4, r5, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5310 | __ jmp(&done_checking_r1); |
| 5311 | __ bind(&r1_is_smi); |
| 5312 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5313 | __ bind(&done_checking_r1); |
| 5314 | |
| 5315 | __ tst(r0, Operand(kSmiTagMask)); |
| 5316 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5317 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5318 | __ b(ne, &slow); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5319 | GetInt32(masm, r0, r2, r4, r5, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5320 | __ jmp(&done_checking_r0); |
| 5321 | __ bind(&r0_is_smi); |
| 5322 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5323 | __ bind(&done_checking_r0); |
| 5324 | |
| 5325 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5326 | // r2 and r3: Signed int32 operands. |
| 5327 | switch (op_) { |
| 5328 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5329 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5330 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5331 | case Token::SAR: |
| 5332 | // Use only the 5 least significant bits of the shift count. |
| 5333 | __ and_(r2, r2, Operand(0x1f)); |
| 5334 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5335 | break; |
| 5336 | case Token::SHR: |
| 5337 | // Use only the 5 least significant bits of the shift count. |
| 5338 | __ and_(r2, r2, Operand(0x1f)); |
| 5339 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5340 | // SHR is special because it is required to produce a positive answer. |
| 5341 | // The code below for writing into heap numbers isn't capable of writing |
| 5342 | // the register as an unsigned int so we go to slow case if we hit this |
| 5343 | // case. |
| 5344 | __ b(mi, &slow); |
| 5345 | break; |
| 5346 | case Token::SHL: |
| 5347 | // Use only the 5 least significant bits of the shift count. |
| 5348 | __ and_(r2, r2, Operand(0x1f)); |
| 5349 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5350 | break; |
| 5351 | default: UNREACHABLE(); |
| 5352 | } |
| 5353 | // check that the *signed* result fits in a smi |
| 5354 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5355 | __ b(mi, &result_not_a_smi); |
| 5356 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5357 | __ Ret(); |
| 5358 | |
| 5359 | Label have_to_allocate, got_a_heap_number; |
| 5360 | __ bind(&result_not_a_smi); |
| 5361 | switch (mode_) { |
| 5362 | case OVERWRITE_RIGHT: { |
| 5363 | __ tst(r0, Operand(kSmiTagMask)); |
| 5364 | __ b(eq, &have_to_allocate); |
| 5365 | __ mov(r5, Operand(r0)); |
| 5366 | break; |
| 5367 | } |
| 5368 | case OVERWRITE_LEFT: { |
| 5369 | __ tst(r1, Operand(kSmiTagMask)); |
| 5370 | __ b(eq, &have_to_allocate); |
| 5371 | __ mov(r5, Operand(r1)); |
| 5372 | break; |
| 5373 | } |
| 5374 | case NO_OVERWRITE: { |
| 5375 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5376 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5377 | } |
| 5378 | default: break; |
| 5379 | } |
| 5380 | __ bind(&got_a_heap_number); |
| 5381 | // r2: Answer as signed int32. |
| 5382 | // r5: Heap number to write answer into. |
| 5383 | |
| 5384 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5385 | // result. |
| 5386 | __ mov(r0, Operand(r5)); |
| 5387 | |
| 5388 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5389 | // r3 as scratch. r0 is preserved and returned. |
| 5390 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5391 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5392 | |
| 5393 | if (mode_ != NO_OVERWRITE) { |
| 5394 | __ bind(&have_to_allocate); |
| 5395 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5396 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5397 | __ jmp(&got_a_heap_number); |
| 5398 | } |
| 5399 | |
| 5400 | // If all else failed then we go to the runtime system. |
| 5401 | __ bind(&slow); |
| 5402 | __ push(r1); // restore stack |
| 5403 | __ push(r0); |
| 5404 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 5405 | switch (op_) { |
| 5406 | case Token::BIT_OR: |
| 5407 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5408 | break; |
| 5409 | case Token::BIT_AND: |
| 5410 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5411 | break; |
| 5412 | case Token::BIT_XOR: |
| 5413 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5414 | break; |
| 5415 | case Token::SAR: |
| 5416 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5417 | break; |
| 5418 | case Token::SHR: |
| 5419 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5420 | break; |
| 5421 | case Token::SHL: |
| 5422 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5423 | break; |
| 5424 | default: |
| 5425 | UNREACHABLE(); |
| 5426 | } |
| 5427 | } |
| 5428 | |
| 5429 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 5430 | // Can we multiply by x with max two shifts and an add. |
| 5431 | // This answers yes to all integers from 2 to 10. |
| 5432 | static bool IsEasyToMultiplyBy(int x) { |
| 5433 | if (x < 2) return false; // Avoid special cases. |
| 5434 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 5435 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 5436 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 5437 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 5438 | return false; |
| 5439 | } |
| 5440 | |
| 5441 | |
| 5442 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 5443 | // Source and destination may be the same register. This routine does |
| 5444 | // not set carry and overflow the way a mul instruction would. |
| 5445 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 5446 | Register source, |
| 5447 | Register destination, |
| 5448 | int known_int) { |
| 5449 | if (IsPowerOf2(known_int)) { |
| 5450 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 5451 | } else if (PopCountLessThanEqual2(known_int)) { |
| 5452 | int first_bit = BitPosition(known_int); |
| 5453 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 5454 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 5455 | if (first_bit != 0) { |
| 5456 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 5457 | } |
| 5458 | } else { |
| 5459 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 5460 | int the_bit = BitPosition(known_int + 1); |
| 5461 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 5462 | } |
| 5463 | } |
| 5464 | |
| 5465 | |
| 5466 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 5467 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 5468 | // a result that needs shifting. |
| 5469 | static void MultiplyByKnownInt2( |
| 5470 | MacroAssembler* masm, |
| 5471 | Register result, |
| 5472 | Register source, |
| 5473 | Register known_int_register, // Smi tagged. |
| 5474 | int known_int, |
| 5475 | int* required_shift) { // Including Smi tag shift |
| 5476 | switch (known_int) { |
| 5477 | case 3: |
| 5478 | __ add(result, source, Operand(source, LSL, 1)); |
| 5479 | *required_shift = 1; |
| 5480 | break; |
| 5481 | case 5: |
| 5482 | __ add(result, source, Operand(source, LSL, 2)); |
| 5483 | *required_shift = 1; |
| 5484 | break; |
| 5485 | case 6: |
| 5486 | __ add(result, source, Operand(source, LSL, 1)); |
| 5487 | *required_shift = 2; |
| 5488 | break; |
| 5489 | case 7: |
| 5490 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 5491 | *required_shift = 1; |
| 5492 | break; |
| 5493 | case 9: |
| 5494 | __ add(result, source, Operand(source, LSL, 3)); |
| 5495 | *required_shift = 1; |
| 5496 | break; |
| 5497 | case 10: |
| 5498 | __ add(result, source, Operand(source, LSL, 2)); |
| 5499 | *required_shift = 2; |
| 5500 | break; |
| 5501 | default: |
| 5502 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 5503 | __ mul(result, source, known_int_register); |
| 5504 | *required_shift = 0; |
| 5505 | } |
| 5506 | } |
| 5507 | |
| 5508 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5509 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5510 | // r1 : x |
| 5511 | // r0 : y |
| 5512 | // result : r0 |
| 5513 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5514 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 5515 | // tell us that. |
| 5516 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 5517 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5518 | switch (op_) { |
| 5519 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5520 | Label not_smi; |
| 5521 | // Fast path. |
| 5522 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5523 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5524 | __ b(ne, ¬_smi); |
| 5525 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 5526 | // Return if no overflow. |
| 5527 | __ Ret(vc); |
| 5528 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 5529 | |
| 5530 | HandleBinaryOpSlowCases(masm, |
| 5531 | ¬_smi, |
| 5532 | Builtins::ADD, |
| 5533 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5534 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5535 | break; |
| 5536 | } |
| 5537 | |
| 5538 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5539 | Label not_smi; |
| 5540 | // Fast path. |
| 5541 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5542 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5543 | __ b(ne, ¬_smi); |
| 5544 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 5545 | // Return if no overflow. |
| 5546 | __ Ret(vc); |
| 5547 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 5548 | |
| 5549 | HandleBinaryOpSlowCases(masm, |
| 5550 | ¬_smi, |
| 5551 | Builtins::SUB, |
| 5552 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5553 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5554 | break; |
| 5555 | } |
| 5556 | |
| 5557 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5558 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5559 | ASSERT(kSmiTag == 0); // adjust code below |
| 5560 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5561 | __ b(ne, ¬_smi); |
| 5562 | // 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] | 5563 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5564 | // Do multiplication |
| 5565 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 5566 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5567 | __ mov(ip, Operand(r3, ASR, 31)); |
| 5568 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 5569 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5570 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5571 | __ tst(r3, Operand(r3)); |
| 5572 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5573 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5574 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 5575 | // We know one of them was zero. |
| 5576 | __ add(r2, r0, Operand(r1), SetCC); |
| 5577 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 5578 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 5579 | // Slow case. We fall through here if we multiplied a negative number |
| 5580 | // with 0, because that would mean we should produce -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5581 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5582 | |
| 5583 | HandleBinaryOpSlowCases(masm, |
| 5584 | ¬_smi, |
| 5585 | Builtins::MUL, |
| 5586 | Token::MUL, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 5587 | mode_); |
| 5588 | break; |
| 5589 | } |
| 5590 | |
| 5591 | case Token::DIV: |
| 5592 | case Token::MOD: { |
| 5593 | Label not_smi; |
| 5594 | if (specialized_on_rhs_) { |
| 5595 | Label smi_is_unsuitable; |
| 5596 | __ BranchOnNotSmi(r1, ¬_smi); |
| 5597 | if (IsPowerOf2(constant_rhs_)) { |
| 5598 | if (op_ == Token::MOD) { |
| 5599 | __ and_(r0, |
| 5600 | r1, |
| 5601 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 5602 | SetCC); |
| 5603 | // We now have the answer, but if the input was negative we also |
| 5604 | // have the sign bit. Our work is done if the result is |
| 5605 | // positive or zero: |
| 5606 | __ Ret(pl); |
| 5607 | // A mod of a negative left hand side must return a negative number. |
| 5608 | // Unfortunately if the answer is 0 then we must return -0. And we |
| 5609 | // already optimistically trashed r0 so we may need to restore it. |
| 5610 | __ eor(r0, r0, Operand(0x80000000u), SetCC); |
| 5611 | // Next two instructions are conditional on the answer being -0. |
| 5612 | __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
| 5613 | __ b(eq, &smi_is_unsuitable); |
| 5614 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
| 5615 | __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_))); |
| 5616 | } else { |
| 5617 | ASSERT(op_ == Token::DIV); |
| 5618 | __ tst(r1, |
| 5619 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 5620 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 5621 | int shift = 0; |
| 5622 | int d = constant_rhs_; |
| 5623 | while ((d & 1) == 0) { |
| 5624 | d >>= 1; |
| 5625 | shift++; |
| 5626 | } |
| 5627 | __ mov(r0, Operand(r1, LSR, shift)); |
| 5628 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 5629 | } |
| 5630 | } else { |
| 5631 | // Not a power of 2. |
| 5632 | __ tst(r1, Operand(0x80000000u)); |
| 5633 | __ b(ne, &smi_is_unsuitable); |
| 5634 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 5635 | // multiplying. |
| 5636 | double divisor = 1.0 / constant_rhs_; |
| 5637 | int shift = 32; |
| 5638 | double scale = 4294967296.0; // 1 << 32. |
| 5639 | uint32_t mul; |
| 5640 | // Maximise the precision of the fixed point reciprocal. |
| 5641 | while (true) { |
| 5642 | mul = static_cast<uint32_t>(scale * divisor); |
| 5643 | if (mul >= 0x7fffffff) break; |
| 5644 | scale *= 2.0; |
| 5645 | shift++; |
| 5646 | } |
| 5647 | mul++; |
| 5648 | __ mov(r2, Operand(mul)); |
| 5649 | __ umull(r3, r2, r2, r1); |
| 5650 | __ mov(r2, Operand(r2, LSR, shift - 31)); |
| 5651 | // r2 is r1 / rhs. r2 is not Smi tagged. |
| 5652 | // r0 is still the known rhs. r0 is Smi tagged. |
| 5653 | // r1 is still the unkown lhs. r1 is Smi tagged. |
| 5654 | int required_r4_shift = 0; // Including the Smi tag shift of 1. |
| 5655 | // r4 = r2 * r0. |
| 5656 | MultiplyByKnownInt2(masm, |
| 5657 | r4, |
| 5658 | r2, |
| 5659 | r0, |
| 5660 | constant_rhs_, |
| 5661 | &required_r4_shift); |
| 5662 | // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs). |
| 5663 | if (op_ == Token::DIV) { |
| 5664 | __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC); |
| 5665 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
| 5666 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5667 | } else { |
| 5668 | ASSERT(op_ == Token::MOD); |
| 5669 | __ sub(r0, r1, Operand(r4, LSL, required_r4_shift)); |
| 5670 | } |
| 5671 | } |
| 5672 | __ Ret(); |
| 5673 | __ bind(&smi_is_unsuitable); |
| 5674 | } else { |
| 5675 | __ jmp(¬_smi); |
| 5676 | } |
| 5677 | HandleBinaryOpSlowCases(masm, |
| 5678 | ¬_smi, |
| 5679 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV, |
| 5680 | op_, |
| 5681 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5682 | break; |
| 5683 | } |
| 5684 | |
| 5685 | case Token::BIT_OR: |
| 5686 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5687 | case Token::BIT_XOR: |
| 5688 | case Token::SAR: |
| 5689 | case Token::SHR: |
| 5690 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5691 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5692 | ASSERT(kSmiTag == 0); // adjust code below |
| 5693 | __ tst(r2, Operand(kSmiTagMask)); |
| 5694 | __ b(ne, &slow); |
| 5695 | switch (op_) { |
| 5696 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 5697 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 5698 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5699 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5700 | // Remove tags from right operand. |
| 5701 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5702 | // Use only the 5 least significant bits of the shift count. |
| 5703 | __ and_(r2, r2, Operand(0x1f)); |
| 5704 | __ mov(r0, Operand(r1, ASR, r2)); |
| 5705 | // Smi tag result. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 5706 | __ bic(r0, r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5707 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5708 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5709 | // Remove tags from operands. We can't do this on a 31 bit number |
| 5710 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 5711 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5712 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5713 | // Use only the 5 least significant bits of the shift count. |
| 5714 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5715 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5716 | // Unsigned shift is not allowed to produce a negative number, so |
| 5717 | // check the sign bit and the sign bit after Smi tagging. |
| 5718 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5719 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5720 | // Smi tag result. |
| 5721 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5722 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5723 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5724 | // Remove tags from operands. |
| 5725 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5726 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5727 | // Use only the 5 least significant bits of the shift count. |
| 5728 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5729 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5730 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5731 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 5732 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5733 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5734 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5735 | default: UNREACHABLE(); |
| 5736 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5737 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5738 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5739 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5740 | break; |
| 5741 | } |
| 5742 | |
| 5743 | default: UNREACHABLE(); |
| 5744 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5745 | // This code should be unreachable. |
| 5746 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5747 | } |
| 5748 | |
| 5749 | |
| 5750 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 5751 | Label within_limit; |
| 5752 | __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit())); |
| 5753 | __ ldr(ip, MemOperand(ip)); |
| 5754 | __ cmp(sp, Operand(ip)); |
| 5755 | __ b(hs, &within_limit); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5756 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 5757 | // argument, so give it a Smi. |
| 5758 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5759 | __ push(r0); |
| 5760 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1); |
| 5761 | __ bind(&within_limit); |
| 5762 | |
| 5763 | __ StubReturn(1); |
| 5764 | } |
| 5765 | |
| 5766 | |
| 5767 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 5768 | Label undo; |
| 5769 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5770 | Label not_smi; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5771 | |
| 5772 | // Enter runtime system if the value is not a smi. |
| 5773 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5774 | __ b(ne, ¬_smi); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5775 | |
| 5776 | // Enter runtime system if the value of the expression is zero |
| 5777 | // to make sure that we switch between 0 and -0. |
| 5778 | __ cmp(r0, Operand(0)); |
| 5779 | __ b(eq, &slow); |
| 5780 | |
| 5781 | // The value of the expression is a smi that is not zero. Try |
| 5782 | // optimistic subtraction '0 - value'. |
| 5783 | __ rsb(r1, r0, Operand(0), SetCC); |
| 5784 | __ b(vs, &slow); |
| 5785 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5786 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 5787 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5788 | |
| 5789 | // Enter runtime system. |
| 5790 | __ bind(&slow); |
| 5791 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5792 | __ mov(r0, Operand(0)); // Set number of arguments. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5793 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 5794 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5795 | __ bind(¬_smi); |
| 5796 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 5797 | __ b(ne, &slow); |
| 5798 | // r0 is a heap number. Get a new heap number in r1. |
| 5799 | if (overwrite_) { |
| 5800 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5801 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5802 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5803 | } else { |
| 5804 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
| 5805 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 5806 | __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 5807 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5808 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5809 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 5810 | __ mov(r0, Operand(r1)); |
| 5811 | } |
| 5812 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5816 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5817 | // r0 holds the exception. |
| 5818 | |
| 5819 | // Adjust this code if not the case. |
| 5820 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5821 | |
| 5822 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5823 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 5824 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5825 | |
| 5826 | // Restore the next handler and frame pointer, discard handler state. |
| 5827 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5828 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5829 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5830 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5831 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 5832 | |
| 5833 | // Before returning we restore the context from the frame pointer if |
| 5834 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5835 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5836 | __ cmp(fp, Operand(0)); |
| 5837 | // Set cp to NULL if fp is NULL. |
| 5838 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5839 | // Restore cp otherwise. |
| 5840 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5841 | #ifdef DEBUG |
| 5842 | if (FLAG_debug_code) { |
| 5843 | __ mov(lr, Operand(pc)); |
| 5844 | } |
| 5845 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5846 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5847 | __ pop(pc); |
| 5848 | } |
| 5849 | |
| 5850 | |
| 5851 | void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5852 | // Adjust this code if not the case. |
| 5853 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5854 | |
| 5855 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5856 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5857 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5858 | |
| 5859 | // Unwind the handlers until the ENTRY handler is found. |
| 5860 | Label loop, done; |
| 5861 | __ bind(&loop); |
| 5862 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5863 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 5864 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5865 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 5866 | __ b(eq, &done); |
| 5867 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5868 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 5869 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5870 | __ jmp(&loop); |
| 5871 | __ bind(&done); |
| 5872 | |
| 5873 | // 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] | 5874 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5875 | __ pop(r0); |
| 5876 | __ str(r0, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5877 | |
| 5878 | // Set external caught exception to false. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5879 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5880 | __ mov(r0, Operand(false)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5881 | __ mov(r2, Operand(external_caught)); |
| 5882 | __ str(r0, MemOperand(r2)); |
| 5883 | |
| 5884 | // Set pending exception and r0 to out of memory exception. |
| 5885 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5886 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5887 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 5888 | __ str(r0, MemOperand(r2)); |
| 5889 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5890 | // Stack layout at this point. See also StackHandlerConstants. |
| 5891 | // sp -> state (ENTRY) |
| 5892 | // fp |
| 5893 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5894 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5895 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 5896 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5897 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 5898 | // Before returning we restore the context from the frame pointer if |
| 5899 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5900 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5901 | __ cmp(fp, Operand(0)); |
| 5902 | // Set cp to NULL if fp is NULL. |
| 5903 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5904 | // Restore cp otherwise. |
| 5905 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5906 | #ifdef DEBUG |
| 5907 | if (FLAG_debug_code) { |
| 5908 | __ mov(lr, Operand(pc)); |
| 5909 | } |
| 5910 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5911 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5912 | __ pop(pc); |
| 5913 | } |
| 5914 | |
| 5915 | |
| 5916 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 5917 | Label* throw_normal_exception, |
| 5918 | Label* throw_out_of_memory_exception, |
| 5919 | StackFrame::Type frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5920 | bool do_gc, |
| 5921 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5922 | // r0: result parameter for PerformGC, if any |
| 5923 | // r4: number of arguments including receiver (C callee-saved) |
| 5924 | // r5: pointer to builtin function (C callee-saved) |
| 5925 | // r6: pointer to the first argument (C callee-saved) |
| 5926 | |
| 5927 | if (do_gc) { |
| 5928 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5929 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 5930 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5931 | } |
| 5932 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5933 | ExternalReference scope_depth = |
| 5934 | ExternalReference::heap_always_allocate_scope_depth(); |
| 5935 | if (always_allocate) { |
| 5936 | __ mov(r0, Operand(scope_depth)); |
| 5937 | __ ldr(r1, MemOperand(r0)); |
| 5938 | __ add(r1, r1, Operand(1)); |
| 5939 | __ str(r1, MemOperand(r0)); |
| 5940 | } |
| 5941 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5942 | // Call C built-in. |
| 5943 | // r0 = argc, r1 = argv |
| 5944 | __ mov(r0, Operand(r4)); |
| 5945 | __ mov(r1, Operand(r6)); |
| 5946 | |
| 5947 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 5948 | // frames, we need to know where the return address is. Right now, |
| 5949 | // we push it on the stack to be able to find it again, but we never |
| 5950 | // restore from it in case of changes, which makes it impossible to |
| 5951 | // support moving the C entry code stub. This should be fixed, but currently |
| 5952 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 5953 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5954 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 5955 | masm->push(lr); |
| 5956 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5957 | |
| 5958 | if (always_allocate) { |
| 5959 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 5960 | // though (contain the result). |
| 5961 | __ mov(r2, Operand(scope_depth)); |
| 5962 | __ ldr(r3, MemOperand(r2)); |
| 5963 | __ sub(r3, r3, Operand(1)); |
| 5964 | __ str(r3, MemOperand(r2)); |
| 5965 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5966 | |
| 5967 | // check for failure result |
| 5968 | Label failure_returned; |
| 5969 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 5970 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 5971 | __ add(r2, r0, Operand(1)); |
| 5972 | __ tst(r2, Operand(kFailureTagMask)); |
| 5973 | __ b(eq, &failure_returned); |
| 5974 | |
| 5975 | // Exit C frame and return. |
| 5976 | // r0:r1: result |
| 5977 | // sp: stack pointer |
| 5978 | // fp: frame pointer |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5979 | __ LeaveExitFrame(frame_type); |
| 5980 | |
| 5981 | // check if we should retry or throw exception |
| 5982 | Label retry; |
| 5983 | __ bind(&failure_returned); |
| 5984 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 5985 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 5986 | __ b(eq, &retry); |
| 5987 | |
| 5988 | Label continue_exception; |
| 5989 | // If the returned failure is EXCEPTION then promote Top::pending_exception(). |
| 5990 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 5991 | __ b(ne, &continue_exception); |
| 5992 | |
| 5993 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5994 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5995 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5996 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5997 | __ ldr(r0, MemOperand(ip)); |
| 5998 | __ str(r3, MemOperand(ip)); |
| 5999 | |
| 6000 | __ bind(&continue_exception); |
| 6001 | // Special handling of out of memory exception. |
| 6002 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6003 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6004 | __ b(eq, throw_out_of_memory_exception); |
| 6005 | |
| 6006 | // Handle normal exception. |
| 6007 | __ jmp(throw_normal_exception); |
| 6008 | |
| 6009 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 6010 | } |
| 6011 | |
| 6012 | |
| 6013 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 6014 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 6015 | // r0: number of arguments including receiver |
| 6016 | // r1: pointer to builtin function |
| 6017 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6018 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6019 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6020 | |
| 6021 | // NOTE: Invocations of builtins may return failure objects |
| 6022 | // instead of a proper result. The builtin entry handles |
| 6023 | // this by performing a garbage collection and retrying the |
| 6024 | // builtin once. |
| 6025 | |
| 6026 | StackFrame::Type frame_type = is_debug_break |
| 6027 | ? StackFrame::EXIT_DEBUG |
| 6028 | : StackFrame::EXIT; |
| 6029 | |
| 6030 | // Enter the exit frame that transitions from JavaScript to C++. |
| 6031 | __ EnterExitFrame(frame_type); |
| 6032 | |
| 6033 | // r4: number of arguments (C callee-saved) |
| 6034 | // r5: pointer to builtin function (C callee-saved) |
| 6035 | // r6: pointer to first argument (C callee-saved) |
| 6036 | |
| 6037 | Label throw_out_of_memory_exception; |
| 6038 | Label throw_normal_exception; |
| 6039 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6040 | // Call into the runtime system. Collect garbage before the call if |
| 6041 | // running with --gc-greedy set. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6042 | if (FLAG_gc_greedy) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6043 | Failure* failure = Failure::RetryAfterGC(0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6044 | __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure))); |
| 6045 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6046 | GenerateCore(masm, &throw_normal_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6047 | &throw_out_of_memory_exception, |
| 6048 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6049 | FLAG_gc_greedy, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6050 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6051 | |
| 6052 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6053 | GenerateCore(masm, |
| 6054 | &throw_normal_exception, |
| 6055 | &throw_out_of_memory_exception, |
| 6056 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6057 | true, |
| 6058 | false); |
| 6059 | |
| 6060 | // Do full GC and retry runtime call one final time. |
| 6061 | Failure* failure = Failure::InternalError(); |
| 6062 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 6063 | GenerateCore(masm, |
| 6064 | &throw_normal_exception, |
| 6065 | &throw_out_of_memory_exception, |
| 6066 | frame_type, |
| 6067 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6068 | true); |
| 6069 | |
| 6070 | __ bind(&throw_out_of_memory_exception); |
| 6071 | GenerateThrowOutOfMemory(masm); |
| 6072 | // control flow for generated will not return. |
| 6073 | |
| 6074 | __ bind(&throw_normal_exception); |
| 6075 | GenerateThrowTOS(masm); |
| 6076 | } |
| 6077 | |
| 6078 | |
| 6079 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 6080 | // r0: code entry |
| 6081 | // r1: function |
| 6082 | // r2: receiver |
| 6083 | // r3: argc |
| 6084 | // [sp+0]: argv |
| 6085 | |
| 6086 | Label invoke, exit; |
| 6087 | |
| 6088 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 6089 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6090 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6091 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 6092 | |
| 6093 | // Get address of argv, see stm above. |
| 6094 | // r0: code entry |
| 6095 | // r1: function |
| 6096 | // r2: receiver |
| 6097 | // r3: argc |
| 6098 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 6099 | __ ldr(r4, MemOperand(r4)); // argv |
| 6100 | |
| 6101 | // Push a frame with special values setup to mark it as an entry frame. |
| 6102 | // r0: code entry |
| 6103 | // r1: function |
| 6104 | // r2: receiver |
| 6105 | // r3: argc |
| 6106 | // r4: argv |
| 6107 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 6108 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 6109 | __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL)); |
| 6110 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 6111 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6112 | __ ldr(r5, MemOperand(r5)); |
| 6113 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 6114 | |
| 6115 | // Setup frame pointer for the frame to be pushed. |
| 6116 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6117 | |
| 6118 | // Call a faked try-block that does the invoke. |
| 6119 | __ bl(&invoke); |
| 6120 | |
| 6121 | // Caught exception: Store result (exception) in the pending |
| 6122 | // exception field in the JSEnv and return a failure sentinel. |
| 6123 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 6124 | // 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] | 6125 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6126 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6127 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6128 | __ b(&exit); |
| 6129 | |
| 6130 | // Invoke: Link this frame into the handler chain. |
| 6131 | __ bind(&invoke); |
| 6132 | // Must preserve r0-r4, r5-r7 are available. |
| 6133 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6134 | // If an exception not caught by another handler occurs, this handler |
| 6135 | // returns control to the code after the bl(&invoke) above, which |
| 6136 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 6137 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6138 | |
| 6139 | // Clear any pending exceptions. |
| 6140 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6141 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6142 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6143 | __ str(r5, MemOperand(ip)); |
| 6144 | |
| 6145 | // Invoke the function by calling through JS entry trampoline builtin. |
| 6146 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6147 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6148 | |
| 6149 | // Expected registers by Builtins::JSEntryTrampoline |
| 6150 | // r0: code entry |
| 6151 | // r1: function |
| 6152 | // r2: receiver |
| 6153 | // r3: argc |
| 6154 | // r4: argv |
| 6155 | if (is_construct) { |
| 6156 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6157 | __ mov(ip, Operand(construct_entry)); |
| 6158 | } else { |
| 6159 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6160 | __ mov(ip, Operand(entry)); |
| 6161 | } |
| 6162 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6163 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6164 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6165 | // macro for the add instruction because we don't want the coverage tool |
| 6166 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6167 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6168 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6169 | |
| 6170 | // Unlink this frame from the handler chain. When reading the |
| 6171 | // address of the next handler, there is no need to use the address |
| 6172 | // displacement since the current stack pointer (sp) points directly |
| 6173 | // to the stack handler. |
| 6174 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6175 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6176 | __ str(r3, MemOperand(ip)); |
| 6177 | // No need to restore registers |
| 6178 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6179 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6180 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6181 | __ bind(&exit); // r0 holds result |
| 6182 | // Restore the top frame descriptors from the stack. |
| 6183 | __ pop(r3); |
| 6184 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6185 | __ str(r3, MemOperand(ip)); |
| 6186 | |
| 6187 | // Reset the stack to the callee saved registers. |
| 6188 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6189 | |
| 6190 | // Restore callee-saved registers and return. |
| 6191 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6192 | if (FLAG_debug_code) { |
| 6193 | __ mov(lr, Operand(pc)); |
| 6194 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6195 | #endif |
| 6196 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6197 | } |
| 6198 | |
| 6199 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6200 | // This stub performs an instanceof, calling the builtin function if |
| 6201 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6202 | // be an instance of (these are fetched from the stack). |
| 6203 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6204 | // Get the object - slow case for smis (we may need to throw an exception |
| 6205 | // depending on the rhs). |
| 6206 | Label slow, loop, is_instance, is_not_instance; |
| 6207 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6208 | __ BranchOnSmi(r0, &slow); |
| 6209 | |
| 6210 | // Check that the left hand is a JS object and put map in r3. |
| 6211 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6212 | __ b(lt, &slow); |
| 6213 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6214 | __ b(gt, &slow); |
| 6215 | |
| 6216 | // Get the prototype of the function (r4 is result, r2 is scratch). |
| 6217 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 6218 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6219 | |
| 6220 | // Check that the function prototype is a JS object. |
| 6221 | __ BranchOnSmi(r4, &slow); |
| 6222 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6223 | __ b(lt, &slow); |
| 6224 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6225 | __ b(gt, &slow); |
| 6226 | |
| 6227 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6228 | // Get prototype of object into r2. |
| 6229 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6230 | |
| 6231 | // Loop through the prototype chain looking for the function prototype. |
| 6232 | __ bind(&loop); |
| 6233 | __ cmp(r2, Operand(r4)); |
| 6234 | __ b(eq, &is_instance); |
| 6235 | __ cmp(r2, Operand(Factory::null_value())); |
| 6236 | __ b(eq, &is_not_instance); |
| 6237 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6238 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6239 | __ jmp(&loop); |
| 6240 | |
| 6241 | __ bind(&is_instance); |
| 6242 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6243 | __ pop(); |
| 6244 | __ pop(); |
| 6245 | __ mov(pc, Operand(lr)); // Return. |
| 6246 | |
| 6247 | __ bind(&is_not_instance); |
| 6248 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6249 | __ pop(); |
| 6250 | __ pop(); |
| 6251 | __ mov(pc, Operand(lr)); // Return. |
| 6252 | |
| 6253 | // Slow-case. Tail call builtin. |
| 6254 | __ bind(&slow); |
| 6255 | __ mov(r0, Operand(1)); // Arg count without receiver. |
| 6256 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6257 | } |
| 6258 | |
| 6259 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6260 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6261 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6262 | Label adaptor; |
| 6263 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6264 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6265 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6266 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6267 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6268 | // Nothing to do: The formal number of parameters has already been |
| 6269 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6270 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6271 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6272 | // Arguments adaptor case: Read the arguments length from the |
| 6273 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6274 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6275 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6276 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
| 6279 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6280 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6281 | // The displacement is the offset of the last parameter (if any) |
| 6282 | // relative to the frame pointer. |
| 6283 | static const int kDisplacement = |
| 6284 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6285 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6286 | // Check that the key is a smi. |
| 6287 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6288 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6289 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6290 | // Check if the calling frame is an arguments adaptor frame. |
| 6291 | Label adaptor; |
| 6292 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6293 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6294 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 6295 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6296 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6297 | // Check index against formal parameters count limit passed in |
| 6298 | // through register eax. Use unsigned comparison to get negative |
| 6299 | // check for free. |
| 6300 | __ cmp(r1, r0); |
| 6301 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6302 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6303 | // Read the argument from the stack and return it. |
| 6304 | __ sub(r3, r0, r1); |
| 6305 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6306 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6307 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6308 | |
| 6309 | // Arguments adaptor case: Check index against actual arguments |
| 6310 | // limit found in the arguments adaptor frame. Use unsigned |
| 6311 | // comparison to get negative check for free. |
| 6312 | __ bind(&adaptor); |
| 6313 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6314 | __ cmp(r1, r0); |
| 6315 | __ b(cs, &slow); |
| 6316 | |
| 6317 | // Read the argument from the adaptor frame and return it. |
| 6318 | __ sub(r3, r0, r1); |
| 6319 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6320 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6321 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6322 | |
| 6323 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6324 | // by calling the runtime system. |
| 6325 | __ bind(&slow); |
| 6326 | __ push(r1); |
| 6327 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1); |
| 6328 | } |
| 6329 | |
| 6330 | |
| 6331 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 6332 | // Check if the calling frame is an arguments adaptor frame. |
| 6333 | Label runtime; |
| 6334 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6335 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6336 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 6337 | __ b(ne, &runtime); |
| 6338 | |
| 6339 | // Patch the arguments.length and the parameters pointer. |
| 6340 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6341 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 6342 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6343 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6344 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6345 | |
| 6346 | // Do the runtime call to allocate the arguments object. |
| 6347 | __ bind(&runtime); |
| 6348 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | |
| 6352 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6353 | Label slow; |
| 6354 | // Get the function to call from the stack. |
| 6355 | // function, receiver [, arguments] |
| 6356 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6357 | |
| 6358 | // Check that the function is really a JavaScript function. |
| 6359 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6360 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6361 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6362 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6363 | __ b(ne, &slow); |
| 6364 | |
| 6365 | // Fast-case: Invoke the function now. |
| 6366 | // r1: pushed function |
| 6367 | ParameterCount actual(argc_); |
| 6368 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6369 | |
| 6370 | // Slow-case: Non-function called. |
| 6371 | __ bind(&slow); |
| 6372 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6373 | __ mov(r2, Operand(0)); |
| 6374 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6375 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6376 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6377 | } |
| 6378 | |
| 6379 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6380 | int CompareStub::MinorKey() { |
| 6381 | // Encode the two parameters in a unique 16 bit value. |
| 6382 | ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15)); |
| 6383 | return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0); |
| 6384 | } |
| 6385 | |
| 6386 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6387 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6388 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6389 | } } // namespace v8::internal |