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); |
| 53 | |
| 54 | |
| 55 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 56 | // ------------------------------------------------------------------------- |
| 57 | // Platform-specific DeferredCode functions. |
| 58 | |
| 59 | void DeferredCode::SaveRegisters() { |
| 60 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 61 | int action = registers_[i]; |
| 62 | if (action == kPush) { |
| 63 | __ push(RegisterAllocator::ToRegister(i)); |
| 64 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 65 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void DeferredCode::RestoreRegisters() { |
| 72 | // Restore registers in reverse order due to the stack. |
| 73 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 74 | int action = registers_[i]; |
| 75 | if (action == kPush) { |
| 76 | __ pop(RegisterAllocator::ToRegister(i)); |
| 77 | } else if (action != kIgnore) { |
| 78 | action &= ~kSyncedFlag; |
| 79 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 84 | |
| 85 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 86 | // CodeGenState implementation. |
| 87 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 88 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 89 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 90 | typeof_state_(NOT_INSIDE_TYPEOF), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 91 | true_target_(NULL), |
| 92 | false_target_(NULL), |
| 93 | previous_(NULL) { |
| 94 | owner_->set_state(this); |
| 95 | } |
| 96 | |
| 97 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 98 | CodeGenState::CodeGenState(CodeGenerator* owner, |
| 99 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 100 | JumpTarget* true_target, |
| 101 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 102 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 103 | typeof_state_(typeof_state), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 104 | true_target_(true_target), |
| 105 | false_target_(false_target), |
| 106 | previous_(owner->state()) { |
| 107 | owner_->set_state(this); |
| 108 | } |
| 109 | |
| 110 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 111 | CodeGenState::~CodeGenState() { |
| 112 | ASSERT(owner_->state() == this); |
| 113 | owner_->set_state(previous_); |
| 114 | } |
| 115 | |
| 116 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 117 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 118 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 120 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 121 | bool is_eval) |
| 122 | : is_eval_(is_eval), |
| 123 | script_(script), |
| 124 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 126 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 127 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 128 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | cc_reg_(al), |
| 130 | state_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 131 | function_return_is_shadowed_(false), |
| 132 | in_spilled_code_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
| 136 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 137 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 139 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | // cp: callee's context |
| 141 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 142 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | ZoneList<Statement*>* body = fun->body(); |
| 144 | |
| 145 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 146 | ASSERT(scope_ == NULL); |
| 147 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 148 | ASSERT(allocator_ == NULL); |
| 149 | RegisterAllocator register_allocator(this); |
| 150 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 151 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 152 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 153 | cc_reg_ = al; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 154 | set_in_spilled_code(false); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 155 | { |
| 156 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 157 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 158 | // Entry: |
| 159 | // Stack: receiver, arguments |
| 160 | // lr: return address |
| 161 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 163 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 165 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 166 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | // tos: code slot |
| 168 | #ifdef DEBUG |
| 169 | if (strlen(FLAG_stop_at) > 0 && |
| 170 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 171 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 172 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | } |
| 174 | #endif |
| 175 | |
| 176 | // Allocate space for locals and initialize them. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 177 | frame_->AllocateStackSlots(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 178 | // Initialize the function return target after the locals are set |
| 179 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 180 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 181 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 182 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 183 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 184 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 185 | // Allocate local context. |
| 186 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 187 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 188 | frame_->EmitPush(r0); |
| 189 | frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 190 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 191 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 192 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 193 | __ cmp(r0, Operand(cp)); |
| 194 | verified_true.Branch(eq); |
| 195 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 196 | verified_true.Bind(); |
| 197 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 198 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 199 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 200 | } |
| 201 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 202 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | // 1) only needed if we have a context |
| 204 | // 2) no need to recompute context ptr every single time |
| 205 | // 3) don't copy parameter operand code from SlotOperand! |
| 206 | { |
| 207 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 208 | |
| 209 | // Note that iteration order is relevant here! If we have the same |
| 210 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 211 | // needs to be copied into the context, it must be the last argument |
| 212 | // passed to the parameter that needs to be copied. This is a rare |
| 213 | // case so we don't check for it, instead we rely on the copying |
| 214 | // order: such a parameter is copied repeatedly into the same |
| 215 | // context location and thus the last value is what is seen inside |
| 216 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 217 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 218 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | Slot* slot = par->slot(); |
| 220 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 221 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 222 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | // Loads r2 with context; used below in RecordWrite. |
| 224 | __ str(r1, SlotOperand(slot, r2)); |
| 225 | // Load the offset into r3. |
| 226 | int slot_offset = |
| 227 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 228 | __ mov(r3, Operand(slot_offset)); |
| 229 | __ RecordWrite(r2, r3, r1); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 234 | // Store the arguments object. This must happen after context |
| 235 | // initialization because the arguments object may be stored in the |
| 236 | // context. |
| 237 | if (scope_->arguments() != NULL) { |
| 238 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 240 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 241 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 242 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 243 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 244 | // The receiver is below the arguments, the return address, |
| 245 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 246 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 247 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 248 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 249 | frame_->Adjust(3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 250 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 251 | frame_->CallStub(&stub, 3); |
| 252 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 253 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 254 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 255 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 256 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 257 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 258 | } |
| 259 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 260 | // Generate code to 'execute' declarations and initialize functions |
| 261 | // (source elements). In case of an illegal redeclaration we need to |
| 262 | // handle that instead of processing the declarations. |
| 263 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 264 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 265 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 266 | } else { |
| 267 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 268 | ProcessDeclarations(scope_->declarations()); |
| 269 | // Bail out if a stack-overflow exception occurred when processing |
| 270 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 271 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | } |
| 273 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 274 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 275 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 276 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 277 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | CheckStack(); |
| 279 | |
| 280 | // Compile the body of the function in a vanilla state. Don't |
| 281 | // bother compiling all the code if the scope has an illegal |
| 282 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 283 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | Comment cmnt(masm_, "[ function body"); |
| 285 | #ifdef DEBUG |
| 286 | bool is_builtin = Bootstrapper::IsActive(); |
| 287 | bool should_trace = |
| 288 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 289 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 290 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 291 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 292 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 294 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 295 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | } |
| 297 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 298 | // Generate the return sequence if necessary. |
| 299 | if (frame_ != NULL || function_return_.is_linked()) { |
| 300 | // exit |
| 301 | // r0: result |
| 302 | // sp: stack pointer |
| 303 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 304 | // cp: callee's context |
| 305 | __ mov(r0, Operand(Factory::undefined_value())); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 306 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 307 | function_return_.Bind(); |
| 308 | if (FLAG_trace) { |
| 309 | // Push the return value on the stack as the parameter. |
| 310 | // Runtime::TraceExit returns the parameter as it is. |
| 311 | frame_->EmitPush(r0); |
| 312 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 313 | } |
| 314 | |
| 315 | // Tear down the frame which will restore the caller's frame pointer and |
| 316 | // the link register. |
| 317 | frame_->Exit(); |
| 318 | |
| 319 | __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 320 | __ Jump(lr); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 321 | } |
| 322 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 323 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | ASSERT(!has_cc()); |
| 325 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 326 | ASSERT(!function_return_is_shadowed_); |
| 327 | function_return_.Unuse(); |
| 328 | DeleteFrame(); |
| 329 | |
| 330 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 331 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 332 | ProcessDeferred(); |
| 333 | } |
| 334 | |
| 335 | allocator_ = NULL; |
| 336 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 340 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 341 | // Currently, this assertion will fail if we try to assign to |
| 342 | // a constant variable that is constant because it is read-only |
| 343 | // (such as the variable referring to a named function expression). |
| 344 | // We need to implement assignments to read-only variables. |
| 345 | // Ideally, we should do this during AST generation (by converting |
| 346 | // such assignments into expression statements); however, in general |
| 347 | // we may not be able to make the decision until past AST generation, |
| 348 | // that is when the entire program is known. |
| 349 | ASSERT(slot != NULL); |
| 350 | int index = slot->index(); |
| 351 | switch (slot->type()) { |
| 352 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 353 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 354 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 355 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 356 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 357 | |
| 358 | case Slot::CONTEXT: { |
| 359 | // Follow the context chain if necessary. |
| 360 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 361 | Register context = cp; |
| 362 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 363 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 364 | // Load the closure. |
| 365 | // (All contexts, even 'with' contexts, have a closure, |
| 366 | // and it is the same for all contexts inside a function. |
| 367 | // There is no need to go to the function context first.) |
| 368 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 369 | // Load the function context (which is the incoming, outer context). |
| 370 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 371 | context = tmp; |
| 372 | } |
| 373 | // We may have a 'with' context now. Get the function context. |
| 374 | // (In fact this mov may never be the needed, since the scope analysis |
| 375 | // may not permit a direct context access in this case and thus we are |
| 376 | // always at a function context. However it is safe to dereference be- |
| 377 | // cause the function context of a function context is itself. Before |
| 378 | // deleting this mov we should try to create a counter-example first, |
| 379 | // though...) |
| 380 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 381 | return ContextOperand(tmp, index); |
| 382 | } |
| 383 | |
| 384 | default: |
| 385 | UNREACHABLE(); |
| 386 | return MemOperand(r0, 0); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 391 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 392 | Slot* slot, |
| 393 | Register tmp, |
| 394 | Register tmp2, |
| 395 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 396 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 397 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 398 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 399 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 400 | if (s->num_heap_slots() > 0) { |
| 401 | if (s->calls_eval()) { |
| 402 | // Check that extension is NULL. |
| 403 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 404 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 405 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 406 | } |
| 407 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 408 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 409 | context = tmp; |
| 410 | } |
| 411 | } |
| 412 | // Check that last extension is NULL. |
| 413 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 414 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 415 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 416 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 417 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 421 | void CodeGenerator::LoadConditionAndSpill(Expression* expression, |
| 422 | TypeofState typeof_state, |
| 423 | JumpTarget* true_target, |
| 424 | JumpTarget* false_target, |
| 425 | bool force_control) { |
| 426 | ASSERT(in_spilled_code()); |
| 427 | set_in_spilled_code(false); |
| 428 | LoadCondition(expression, typeof_state, true_target, false_target, |
| 429 | force_control); |
| 430 | if (frame_ != NULL) { |
| 431 | frame_->SpillAll(); |
| 432 | } |
| 433 | set_in_spilled_code(true); |
| 434 | } |
| 435 | |
| 436 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 437 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 438 | // (partially) translated into branches, or it may have set the condition |
| 439 | // code register. If force_cc is set, the value is forced to set the |
| 440 | // condition code register and no value is pushed. If the condition code |
| 441 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 442 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 443 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 444 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 445 | JumpTarget* true_target, |
| 446 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 447 | bool force_cc) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 448 | ASSERT(!in_spilled_code()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 449 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 450 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 451 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 452 | { CodeGenState new_state(this, typeof_state, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 453 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 454 | |
| 455 | // If we hit a stack overflow, we may not have actually visited |
| 456 | // the expression. In that case, we ensure that we have a |
| 457 | // valid-looking frame state because we will continue to generate |
| 458 | // code as we unwind the C++ stack. |
| 459 | // |
| 460 | // It's possible to have both a stack overflow and a valid frame |
| 461 | // state (eg, a subexpression overflowed, visiting it returned |
| 462 | // with a dummied frame state, and visiting this expression |
| 463 | // returned with a normal-looking state). |
| 464 | if (HasStackOverflow() && |
| 465 | has_valid_frame() && |
| 466 | !has_cc() && |
| 467 | frame_->height() == original_height) { |
| 468 | true_target->Jump(); |
| 469 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 470 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 471 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 472 | // Convert the TOS value to a boolean in the condition code register. |
| 473 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 474 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 475 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 476 | ASSERT(!has_valid_frame() || |
| 477 | (has_cc() && frame_->height() == original_height) || |
| 478 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 482 | void CodeGenerator::LoadAndSpill(Expression* expression, |
| 483 | TypeofState typeof_state) { |
| 484 | ASSERT(in_spilled_code()); |
| 485 | set_in_spilled_code(false); |
| 486 | Load(expression, typeof_state); |
| 487 | frame_->SpillAll(); |
| 488 | set_in_spilled_code(true); |
| 489 | } |
| 490 | |
| 491 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 492 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 493 | #ifdef DEBUG |
| 494 | int original_height = frame_->height(); |
| 495 | #endif |
| 496 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 497 | JumpTarget true_target; |
| 498 | JumpTarget false_target; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 499 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 500 | |
| 501 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 502 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 503 | JumpTarget loaded; |
| 504 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 505 | materialize_true.Branch(cc_reg_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 506 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 507 | frame_->EmitPush(r0); |
| 508 | loaded.Jump(); |
| 509 | materialize_true.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 510 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 511 | frame_->EmitPush(r0); |
| 512 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 513 | cc_reg_ = al; |
| 514 | } |
| 515 | |
| 516 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 517 | // We have at least one condition value that has been "translated" |
| 518 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 519 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 520 | if (frame_ != NULL) { |
| 521 | loaded.Jump(); // Don't lose the current TOS. |
| 522 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 523 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 524 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 525 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 526 | true_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 527 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 528 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 529 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 530 | // If both "true" and "false" need to be loaded jump across the code for |
| 531 | // "false". |
| 532 | if (both) { |
| 533 | loaded.Jump(); |
| 534 | } |
| 535 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 536 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 537 | false_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 538 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 539 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 541 | // A value is loaded on all paths reaching this point. |
| 542 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 544 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 546 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 550 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 551 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 552 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 553 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 557 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 558 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 559 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 560 | __ ldr(scratch, |
| 561 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 562 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 566 | // 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] | 567 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 568 | // variables w/o reference errors elsewhere. |
| 569 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 570 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 572 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 573 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 574 | // the variable as if through '<global>.<variable>' to make sure we |
| 575 | // do not get reference errors. |
| 576 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 577 | Literal key(variable->name()); |
| 578 | // TODO(1241834): Fetch the position from the variable instead of using |
| 579 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 580 | Property property(&global, &key, RelocInfo::kNoPosition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 581 | LoadAndSpill(&property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 582 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 583 | LoadAndSpill(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 588 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 589 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 590 | cgen->LoadReference(this); |
| 591 | } |
| 592 | |
| 593 | |
| 594 | Reference::~Reference() { |
| 595 | cgen_->UnloadReference(this); |
| 596 | } |
| 597 | |
| 598 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 599 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 600 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 601 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 602 | Expression* e = ref->expression(); |
| 603 | Property* property = e->AsProperty(); |
| 604 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 605 | |
| 606 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 607 | // The expression is either a property or a variable proxy that rewrites |
| 608 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 609 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 610 | // We use a named reference if the key is a literal symbol, unless it is |
| 611 | // a string that can be legally parsed as an integer. This is because |
| 612 | // otherwise we will not get into the slow case code that handles [] on |
| 613 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 614 | Literal* literal = property->key()->AsLiteral(); |
| 615 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 616 | if (literal != NULL && |
| 617 | literal->handle()->IsSymbol() && |
| 618 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 619 | ref->set_type(Reference::NAMED); |
| 620 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 621 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 622 | ref->set_type(Reference::KEYED); |
| 623 | } |
| 624 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 625 | // The expression is a variable proxy that does not rewrite to a |
| 626 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | LoadGlobal(); |
| 629 | ref->set_type(Reference::NAMED); |
| 630 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 631 | ASSERT(var->slot() != NULL); |
| 632 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 633 | } |
| 634 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 635 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 636 | LoadAndSpill(e); |
| 637 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
| 641 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 642 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 643 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 644 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 645 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 646 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 647 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 648 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 649 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 650 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 655 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 656 | // register to a boolean in the condition code register. The code |
| 657 | // 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] | 658 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 659 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 660 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 661 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 662 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 663 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 664 | |
| 665 | // Fast case checks |
| 666 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 667 | // Check if the value is 'false'. |
| 668 | __ cmp(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 669 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 671 | // Check if the value is 'true'. |
| 672 | __ cmp(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 673 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 674 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 675 | // Check if the value is 'undefined'. |
| 676 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 677 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 679 | // Check if the value is a smi. |
| 680 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 681 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 682 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 683 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 684 | |
| 685 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 686 | frame_->EmitPush(r0); |
| 687 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 688 | // Convert the result (r0) to a condition code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 689 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 690 | |
| 691 | cc_reg_ = ne; |
| 692 | } |
| 693 | |
| 694 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 695 | class GenericBinaryOpStub : public CodeStub { |
| 696 | public: |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 697 | GenericBinaryOpStub(Token::Value op, |
| 698 | OverwriteMode mode) |
| 699 | : op_(op), mode_(mode) { } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 700 | |
| 701 | private: |
| 702 | Token::Value op_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 703 | OverwriteMode mode_; |
| 704 | |
| 705 | // Minor key encoding in 16 bits. |
| 706 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 707 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 708 | |
| 709 | Major MajorKey() { return GenericBinaryOp; } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 710 | int MinorKey() { |
| 711 | // Encode the parameters in a unique 16 bit value. |
| 712 | return OpBits::encode(op_) |
| 713 | | ModeBits::encode(mode_); |
| 714 | } |
| 715 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 716 | void Generate(MacroAssembler* masm); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 717 | void HandleNonSmiBitwiseOp(MacroAssembler* masm); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 718 | |
| 719 | const char* GetName() { |
| 720 | switch (op_) { |
| 721 | case Token::ADD: return "GenericBinaryOpStub_ADD"; |
| 722 | case Token::SUB: return "GenericBinaryOpStub_SUB"; |
| 723 | case Token::MUL: return "GenericBinaryOpStub_MUL"; |
| 724 | case Token::DIV: return "GenericBinaryOpStub_DIV"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 725 | case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR"; |
| 726 | case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND"; |
| 727 | case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR"; |
| 728 | case Token::SAR: return "GenericBinaryOpStub_SAR"; |
| 729 | case Token::SHL: return "GenericBinaryOpStub_SHL"; |
| 730 | case Token::SHR: return "GenericBinaryOpStub_SHR"; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 731 | default: return "GenericBinaryOpStub"; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | #ifdef DEBUG |
| 736 | void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); } |
| 737 | #endif |
| 738 | }; |
| 739 | |
| 740 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 741 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
| 742 | OverwriteMode overwrite_mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 743 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 744 | // sp[0] : y |
| 745 | // sp[1] : x |
| 746 | // result : r0 |
| 747 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 748 | // Stub is entered with a call: 'return address' is in lr. |
| 749 | switch (op) { |
| 750 | case Token::ADD: // fall through. |
| 751 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 752 | case Token::MUL: |
| 753 | case Token::BIT_OR: |
| 754 | case Token::BIT_AND: |
| 755 | case Token::BIT_XOR: |
| 756 | case Token::SHL: |
| 757 | case Token::SHR: |
| 758 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 759 | frame_->EmitPop(r0); // r0 : y |
| 760 | frame_->EmitPop(r1); // r1 : x |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 761 | GenericBinaryOpStub stub(op, overwrite_mode); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 762 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 763 | break; |
| 764 | } |
| 765 | |
| 766 | case Token::DIV: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 767 | Result arg_count = allocator_->Allocate(r0); |
| 768 | ASSERT(arg_count.is_valid()); |
| 769 | __ mov(arg_count.reg(), Operand(1)); |
| 770 | frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 771 | break; |
| 772 | } |
| 773 | |
| 774 | case Token::MOD: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 775 | Result arg_count = allocator_->Allocate(r0); |
| 776 | ASSERT(arg_count.is_valid()); |
| 777 | __ mov(arg_count.reg(), Operand(1)); |
| 778 | frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 779 | break; |
| 780 | } |
| 781 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 782 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 783 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 784 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 785 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 786 | break; |
| 787 | |
| 788 | default: |
| 789 | // Other cases should have been handled before this point. |
| 790 | UNREACHABLE(); |
| 791 | break; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 796 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 797 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 798 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 799 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 800 | bool reversed, |
| 801 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 802 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 803 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 804 | reversed_(reversed), |
| 805 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 806 | set_comment("[ DeferredInlinedSmiOperation"); |
| 807 | } |
| 808 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 809 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 810 | |
| 811 | private: |
| 812 | Token::Value op_; |
| 813 | int value_; |
| 814 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 815 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 816 | }; |
| 817 | |
| 818 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 819 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 820 | switch (op_) { |
| 821 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 822 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 823 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 824 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 825 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 826 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 827 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 828 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 829 | } |
| 830 | break; |
| 831 | } |
| 832 | |
| 833 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 834 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 835 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 836 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 837 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 838 | } else { |
| 839 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 840 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 841 | } |
| 842 | break; |
| 843 | } |
| 844 | |
| 845 | case Token::BIT_OR: |
| 846 | case Token::BIT_XOR: |
| 847 | case Token::BIT_AND: { |
| 848 | if (reversed_) { |
| 849 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 850 | } else { |
| 851 | __ mov(r1, Operand(r0)); |
| 852 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 853 | } |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | case Token::SHL: |
| 858 | case Token::SHR: |
| 859 | case Token::SAR: { |
| 860 | if (!reversed_) { |
| 861 | __ mov(r1, Operand(r0)); |
| 862 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 863 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 864 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 865 | } |
| 866 | break; |
| 867 | } |
| 868 | |
| 869 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 870 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 871 | UNREACHABLE(); |
| 872 | break; |
| 873 | } |
| 874 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 875 | GenericBinaryOpStub stub(op_, overwrite_mode_); |
| 876 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 880 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 881 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 882 | bool reversed, |
| 883 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 884 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 885 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 886 | // some possible smi operations (like + and -) when (at least) one |
| 887 | // of the operands is a literal smi. With this optimization, the |
| 888 | // performance of the system is increased by ~15%, and the generated |
| 889 | // code size is increased by ~1% (measured on a combination of |
| 890 | // different benchmarks). |
| 891 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 892 | // sp[0] : operand |
| 893 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 894 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 895 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 896 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 897 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 898 | |
| 899 | switch (op) { |
| 900 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 901 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 902 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 903 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 904 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 905 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 906 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 907 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 908 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 909 | break; |
| 910 | } |
| 911 | |
| 912 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 913 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 914 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 915 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 916 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 917 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 918 | } else { |
| 919 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 920 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 921 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 922 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 923 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 924 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 925 | break; |
| 926 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 927 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 928 | case Token::BIT_OR: |
| 929 | case Token::BIT_XOR: |
| 930 | case Token::BIT_AND: { |
| 931 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 932 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 933 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 934 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 935 | switch (op) { |
| 936 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 937 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 938 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 939 | default: UNREACHABLE(); |
| 940 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 941 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 942 | break; |
| 943 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 944 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 945 | case Token::SHL: |
| 946 | case Token::SHR: |
| 947 | case Token::SAR: { |
| 948 | if (reversed) { |
| 949 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 950 | frame_->EmitPush(ip); |
| 951 | frame_->EmitPush(r0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 952 | GenericBinaryOperation(op, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 953 | |
| 954 | } else { |
| 955 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 956 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 957 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 958 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 959 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 960 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 961 | switch (op) { |
| 962 | case Token::SHL: { |
| 963 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 964 | // check that the *unsigned* result fits in a smi |
| 965 | __ add(r3, r2, Operand(0x40000000), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 966 | deferred->Branch(mi); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 967 | break; |
| 968 | } |
| 969 | case Token::SHR: { |
| 970 | // LSR by immediate 0 means shifting 32 bits. |
| 971 | if (shift_value != 0) { |
| 972 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 973 | } |
| 974 | // check that the *unsigned* result fits in a smi |
| 975 | // neither of the two high-order bits can be set: |
| 976 | // - 0x80000000: high bit would be lost when smi tagging |
| 977 | // - 0x40000000: this number would convert to negative when |
| 978 | // smi tagging these two cases can only happen with shifts |
| 979 | // by 0 or 1 when handed a valid smi |
| 980 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 981 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 982 | break; |
| 983 | } |
| 984 | case Token::SAR: { |
| 985 | if (shift_value != 0) { |
| 986 | // ASR by immediate 0 means shifting 32 bits. |
| 987 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 988 | } |
| 989 | break; |
| 990 | } |
| 991 | default: UNREACHABLE(); |
| 992 | } |
| 993 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 994 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 995 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 996 | break; |
| 997 | } |
| 998 | |
| 999 | default: |
| 1000 | if (!reversed) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1001 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1002 | __ mov(r0, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1003 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1004 | } else { |
| 1005 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1006 | frame_->EmitPush(ip); |
| 1007 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1008 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1009 | GenericBinaryOperation(op, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1010 | break; |
| 1011 | } |
| 1012 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1013 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 1017 | void CodeGenerator::Comparison(Condition cc, |
| 1018 | Expression* left, |
| 1019 | Expression* right, |
| 1020 | bool strict) { |
| 1021 | if (left != NULL) LoadAndSpill(left); |
| 1022 | if (right != NULL) LoadAndSpill(right); |
| 1023 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1024 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1025 | // sp[0] : y |
| 1026 | // sp[1] : x |
| 1027 | // result : cc register |
| 1028 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1029 | // Strict only makes sense for equality comparisons. |
| 1030 | ASSERT(!strict || cc == eq); |
| 1031 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1032 | JumpTarget exit; |
| 1033 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1034 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1035 | if (cc == gt || cc == le) { |
| 1036 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1037 | frame_->EmitPop(r1); |
| 1038 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1039 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1040 | frame_->EmitPop(r0); |
| 1041 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1042 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1043 | __ orr(r2, r0, Operand(r1)); |
| 1044 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1045 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1046 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 1047 | // Perform non-smi comparison by stub. |
| 1048 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1049 | // We call with 0 args because there are 0 on the stack. |
| 1050 | CompareStub stub(cc, strict); |
| 1051 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1052 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 1053 | Result result = allocator_->Allocate(r0); |
| 1054 | ASSERT(result.is_valid()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1055 | __ cmp(result.reg(), Operand(0)); |
| 1056 | result.Unuse(); |
| 1057 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1058 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 1059 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1060 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1061 | __ cmp(r1, Operand(r0)); |
| 1062 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1063 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1064 | cc_reg_ = cc; |
| 1065 | } |
| 1066 | |
| 1067 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1068 | class CallFunctionStub: public CodeStub { |
| 1069 | public: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1070 | CallFunctionStub(int argc, InLoopFlag in_loop) |
| 1071 | : argc_(argc), in_loop_(in_loop) {} |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1072 | |
| 1073 | void Generate(MacroAssembler* masm); |
| 1074 | |
| 1075 | private: |
| 1076 | int argc_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1077 | InLoopFlag in_loop_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1078 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1079 | #if defined(DEBUG) |
| 1080 | void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); } |
| 1081 | #endif // defined(DEBUG) |
| 1082 | |
| 1083 | Major MajorKey() { return CallFunction; } |
| 1084 | int MinorKey() { return argc_; } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1085 | InLoopFlag InLoop() { return in_loop_; } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1086 | }; |
| 1087 | |
| 1088 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1089 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1090 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1091 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1092 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1093 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1094 | int arg_count = args->length(); |
| 1095 | for (int i = 0; i < arg_count; i++) { |
| 1096 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1097 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1099 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1100 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1101 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1102 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1103 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 1104 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1105 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1108 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1109 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1113 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1114 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1115 | ASSERT(has_cc()); |
| 1116 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1117 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1118 | cc_reg_ = al; |
| 1119 | } |
| 1120 | |
| 1121 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1122 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1123 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1124 | if (FLAG_check_stack) { |
| 1125 | Comment cmnt(masm_, "[ check stack"); |
| 1126 | StackCheckStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1127 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1132 | void CodeGenerator::VisitAndSpill(Statement* statement) { |
| 1133 | ASSERT(in_spilled_code()); |
| 1134 | set_in_spilled_code(false); |
| 1135 | Visit(statement); |
| 1136 | if (frame_ != NULL) { |
| 1137 | frame_->SpillAll(); |
| 1138 | } |
| 1139 | set_in_spilled_code(true); |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) { |
| 1144 | ASSERT(in_spilled_code()); |
| 1145 | set_in_spilled_code(false); |
| 1146 | VisitStatements(statements); |
| 1147 | if (frame_ != NULL) { |
| 1148 | frame_->SpillAll(); |
| 1149 | } |
| 1150 | set_in_spilled_code(true); |
| 1151 | } |
| 1152 | |
| 1153 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1154 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1155 | #ifdef DEBUG |
| 1156 | int original_height = frame_->height(); |
| 1157 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1158 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1159 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1160 | VisitAndSpill(statements->at(i)); |
| 1161 | } |
| 1162 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1163 | } |
| 1164 | |
| 1165 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1166 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1167 | #ifdef DEBUG |
| 1168 | int original_height = frame_->height(); |
| 1169 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1170 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1171 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1172 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1173 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1174 | VisitStatementsAndSpill(node->statements()); |
| 1175 | if (node->break_target()->is_linked()) { |
| 1176 | node->break_target()->Bind(); |
| 1177 | } |
| 1178 | node->break_target()->Unuse(); |
| 1179 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1183 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1184 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1185 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1186 | frame_->EmitPush(r0); |
| 1187 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1188 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1189 | frame_->EmitPush(r0); |
| 1190 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1191 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1195 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1196 | #ifdef DEBUG |
| 1197 | int original_height = frame_->height(); |
| 1198 | #endif |
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 | Comment cmnt(masm_, "[ Declaration"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1201 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1202 | Variable* var = node->proxy()->var(); |
| 1203 | ASSERT(var != NULL); // must have been resolved |
| 1204 | Slot* slot = var->slot(); |
| 1205 | |
| 1206 | // If it was not possible to allocate the variable at compile time, |
| 1207 | // we need to "declare" it at runtime to make sure it actually |
| 1208 | // exists in the local context. |
| 1209 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1210 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1211 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1212 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1213 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1214 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1215 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1216 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1217 | // Declaration nodes are always declared in only two modes. |
| 1218 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1219 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1220 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1221 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1222 | // Push initial value, if any. |
| 1223 | // Note: For variables we must not push an initial value (such as |
| 1224 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1225 | // must not destroy the current value. |
| 1226 | if (node->mode() == Variable::CONST) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1227 | __ mov(r0, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1228 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1229 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1230 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1231 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1232 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1233 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1234 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1235 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1236 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1237 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1238 | return; |
| 1239 | } |
| 1240 | |
| 1241 | ASSERT(!var->is_global()); |
| 1242 | |
| 1243 | // If we have a function or a constant, we need to initialize the variable. |
| 1244 | Expression* val = NULL; |
| 1245 | if (node->mode() == Variable::CONST) { |
| 1246 | val = new Literal(Factory::the_hole_value()); |
| 1247 | } else { |
| 1248 | val = node->fun(); // NULL if we don't have a function |
| 1249 | } |
| 1250 | |
| 1251 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1252 | { |
| 1253 | // Set initial value. |
| 1254 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1255 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1256 | target.SetValue(NOT_CONST_INIT); |
| 1257 | // The reference is removed from the stack (preserving TOS) when |
| 1258 | // it goes out of scope. |
| 1259 | } |
| 1260 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1261 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1262 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1263 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1267 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1268 | #ifdef DEBUG |
| 1269 | int original_height = frame_->height(); |
| 1270 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1271 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1272 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1273 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1274 | Expression* expression = node->expression(); |
| 1275 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1276 | LoadAndSpill(expression); |
| 1277 | frame_->Drop(); |
| 1278 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1282 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1283 | #ifdef DEBUG |
| 1284 | int original_height = frame_->height(); |
| 1285 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1286 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1288 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1289 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1290 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1294 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1295 | #ifdef DEBUG |
| 1296 | int original_height = frame_->height(); |
| 1297 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1298 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1299 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1300 | // Generate different code depending on which parts of the if statement |
| 1301 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1302 | bool has_then_stm = node->HasThenStatement(); |
| 1303 | bool has_else_stm = node->HasElseStatement(); |
| 1304 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1305 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1306 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1307 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1309 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1310 | JumpTarget then; |
| 1311 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1312 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1313 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1314 | &then, &else_, true); |
| 1315 | if (frame_ != NULL) { |
| 1316 | Branch(false, &else_); |
| 1317 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1318 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1319 | if (frame_ != NULL || then.is_linked()) { |
| 1320 | then.Bind(); |
| 1321 | VisitAndSpill(node->then_statement()); |
| 1322 | } |
| 1323 | if (frame_ != NULL) { |
| 1324 | exit.Jump(); |
| 1325 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1326 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1327 | if (else_.is_linked()) { |
| 1328 | else_.Bind(); |
| 1329 | VisitAndSpill(node->else_statement()); |
| 1330 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1331 | |
| 1332 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1333 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1334 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1335 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1336 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1337 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1338 | &then, &exit, true); |
| 1339 | if (frame_ != NULL) { |
| 1340 | Branch(false, &exit); |
| 1341 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1342 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1343 | if (frame_ != NULL || then.is_linked()) { |
| 1344 | then.Bind(); |
| 1345 | VisitAndSpill(node->then_statement()); |
| 1346 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1347 | |
| 1348 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1349 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1350 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1351 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1352 | // if (!cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1353 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1354 | &exit, &else_, true); |
| 1355 | if (frame_ != NULL) { |
| 1356 | Branch(true, &exit); |
| 1357 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1358 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1359 | if (frame_ != NULL || else_.is_linked()) { |
| 1360 | else_.Bind(); |
| 1361 | VisitAndSpill(node->else_statement()); |
| 1362 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1363 | |
| 1364 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1365 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1366 | ASSERT(!has_then_stm && !has_else_stm); |
| 1367 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1368 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1369 | &exit, &exit, false); |
| 1370 | if (frame_ != NULL) { |
| 1371 | if (has_cc()) { |
| 1372 | cc_reg_ = al; |
| 1373 | } else { |
| 1374 | frame_->Drop(); |
| 1375 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1380 | if (exit.is_linked()) { |
| 1381 | exit.Bind(); |
| 1382 | } |
| 1383 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1387 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1388 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1389 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1390 | CodeForStatementPosition(node); |
| 1391 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1395 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1396 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1397 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1398 | CodeForStatementPosition(node); |
| 1399 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1403 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1404 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1405 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1406 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1407 | if (function_return_is_shadowed_) { |
| 1408 | CodeForStatementPosition(node); |
| 1409 | LoadAndSpill(node->expression()); |
| 1410 | frame_->EmitPop(r0); |
| 1411 | function_return_.Jump(); |
| 1412 | } else { |
| 1413 | // Load the returned value. |
| 1414 | CodeForStatementPosition(node); |
| 1415 | LoadAndSpill(node->expression()); |
| 1416 | |
| 1417 | // Pop the result from the frame and prepare the frame for |
| 1418 | // returning thus making it easier to merge. |
| 1419 | frame_->EmitPop(r0); |
| 1420 | frame_->PrepareForReturn(); |
| 1421 | |
| 1422 | function_return_.Jump(); |
| 1423 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1427 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1428 | #ifdef DEBUG |
| 1429 | int original_height = frame_->height(); |
| 1430 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1431 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1432 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1433 | CodeForStatementPosition(node); |
| 1434 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1435 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1436 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1437 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1438 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1439 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1440 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1441 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1442 | __ cmp(r0, Operand(cp)); |
| 1443 | verified_true.Branch(eq); |
| 1444 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1445 | verified_true.Bind(); |
| 1446 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1447 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1448 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1449 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1453 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1454 | #ifdef DEBUG |
| 1455 | int original_height = frame_->height(); |
| 1456 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1457 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1458 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1459 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1460 | // Pop context. |
| 1461 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1462 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1463 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1464 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1468 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1469 | #ifdef DEBUG |
| 1470 | int original_height = frame_->height(); |
| 1471 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1472 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1473 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1474 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1475 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1476 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1477 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1478 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1479 | JumpTarget next_test; |
| 1480 | JumpTarget fall_through; |
| 1481 | JumpTarget default_entry; |
| 1482 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1483 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1484 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1485 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1486 | |
| 1487 | for (int i = 0; i < length; i++) { |
| 1488 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1489 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1490 | // Remember the default clause and compile it at the end. |
| 1491 | default_clause = clause; |
| 1492 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1495 | Comment cmnt(masm_, "[ Case clause"); |
| 1496 | // Compile the test. |
| 1497 | next_test.Bind(); |
| 1498 | next_test.Unuse(); |
| 1499 | // Duplicate TOS. |
| 1500 | __ ldr(r0, frame_->Top()); |
| 1501 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 1502 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1503 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1504 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1505 | // Before entering the body from the test, remove the switch value from |
| 1506 | // the stack. |
| 1507 | frame_->Drop(); |
| 1508 | |
| 1509 | // Label the body so that fall through is enabled. |
| 1510 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1511 | default_exit.Bind(); |
| 1512 | } else { |
| 1513 | fall_through.Bind(); |
| 1514 | fall_through.Unuse(); |
| 1515 | } |
| 1516 | VisitStatementsAndSpill(clause->statements()); |
| 1517 | |
| 1518 | // If control flow can fall through from the body, jump to the next body |
| 1519 | // or the end of the statement. |
| 1520 | if (frame_ != NULL) { |
| 1521 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1522 | default_entry.Jump(); |
| 1523 | } else { |
| 1524 | fall_through.Jump(); |
| 1525 | } |
| 1526 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1529 | // The final "test" removes the switch value. |
| 1530 | next_test.Bind(); |
| 1531 | frame_->Drop(); |
| 1532 | |
| 1533 | // If there is a default clause, compile it. |
| 1534 | if (default_clause != NULL) { |
| 1535 | Comment cmnt(masm_, "[ Default clause"); |
| 1536 | default_entry.Bind(); |
| 1537 | VisitStatementsAndSpill(default_clause->statements()); |
| 1538 | // If control flow can fall out of the default and there is a case after |
| 1539 | // it, jup to that case's body. |
| 1540 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1541 | default_exit.Jump(); |
| 1542 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1543 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1544 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1545 | if (fall_through.is_linked()) { |
| 1546 | fall_through.Bind(); |
| 1547 | } |
| 1548 | |
| 1549 | if (node->break_target()->is_linked()) { |
| 1550 | node->break_target()->Bind(); |
| 1551 | } |
| 1552 | node->break_target()->Unuse(); |
| 1553 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1557 | void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1558 | #ifdef DEBUG |
| 1559 | int original_height = frame_->height(); |
| 1560 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1561 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1562 | Comment cmnt(masm_, "[ LoopStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1563 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1564 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1565 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1566 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 1567 | // known result for the test expression, with no side effects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1568 | enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
| 1569 | if (node->cond() == NULL) { |
| 1570 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1571 | info = ALWAYS_TRUE; |
| 1572 | } else { |
| 1573 | Literal* lit = node->cond()->AsLiteral(); |
| 1574 | if (lit != NULL) { |
| 1575 | if (lit->IsTrue()) { |
| 1576 | info = ALWAYS_TRUE; |
| 1577 | } else if (lit->IsFalse()) { |
| 1578 | info = ALWAYS_FALSE; |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1583 | switch (node->type()) { |
| 1584 | case LoopStatement::DO_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1585 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1586 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1587 | // Label the top of the loop for the backward CFG edge. If the test |
| 1588 | // is always true we can use the continue target, and if the test is |
| 1589 | // always false there is no need. |
| 1590 | if (info == ALWAYS_TRUE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1591 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1592 | node->continue_target()->Bind(); |
| 1593 | } else if (info == ALWAYS_FALSE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1594 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1595 | } else { |
| 1596 | ASSERT(info == DONT_KNOW); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1597 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1598 | body.Bind(); |
| 1599 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1600 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1601 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1602 | VisitAndSpill(node->body()); |
| 1603 | |
| 1604 | // Compile the test. |
| 1605 | if (info == ALWAYS_TRUE) { |
| 1606 | if (has_valid_frame()) { |
| 1607 | // If control can fall off the end of the body, jump back to the |
| 1608 | // top. |
| 1609 | node->continue_target()->Jump(); |
| 1610 | } |
| 1611 | } else if (info == ALWAYS_FALSE) { |
| 1612 | // If we have a continue in the body, we only have to bind its jump |
| 1613 | // target. |
| 1614 | if (node->continue_target()->is_linked()) { |
| 1615 | node->continue_target()->Bind(); |
| 1616 | } |
| 1617 | } else { |
| 1618 | ASSERT(info == DONT_KNOW); |
| 1619 | // We have to compile the test expression if it can be reached by |
| 1620 | // control flow falling out of the body or via continue. |
| 1621 | if (node->continue_target()->is_linked()) { |
| 1622 | node->continue_target()->Bind(); |
| 1623 | } |
| 1624 | if (has_valid_frame()) { |
| 1625 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1626 | &body, node->break_target(), true); |
| 1627 | if (has_valid_frame()) { |
| 1628 | // A invalid frame here indicates that control did not |
| 1629 | // fall out of the test expression. |
| 1630 | Branch(true, &body); |
| 1631 | } |
| 1632 | } |
| 1633 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1634 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | case LoopStatement::WHILE_LOOP: { |
| 1638 | // If the test is never true and has no side effects there is no need |
| 1639 | // to compile the test or body. |
| 1640 | if (info == ALWAYS_FALSE) break; |
| 1641 | |
| 1642 | // Label the top of the loop with the continue target for the backward |
| 1643 | // CFG edge. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1644 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1645 | node->continue_target()->Bind(); |
| 1646 | |
| 1647 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1648 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1649 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1650 | &body, node->break_target(), true); |
| 1651 | if (has_valid_frame()) { |
| 1652 | // A NULL frame indicates that control did not fall out of the |
| 1653 | // test expression. |
| 1654 | Branch(false, node->break_target()); |
| 1655 | } |
| 1656 | if (has_valid_frame() || body.is_linked()) { |
| 1657 | body.Bind(); |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | if (has_valid_frame()) { |
| 1662 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1663 | VisitAndSpill(node->body()); |
| 1664 | |
| 1665 | // If control flow can fall out of the body, jump back to the top. |
| 1666 | if (has_valid_frame()) { |
| 1667 | node->continue_target()->Jump(); |
| 1668 | } |
| 1669 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1670 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | case LoopStatement::FOR_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1674 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1675 | |
| 1676 | if (node->init() != NULL) { |
| 1677 | VisitAndSpill(node->init()); |
| 1678 | } |
| 1679 | |
| 1680 | // There is no need to compile the test or body. |
| 1681 | if (info == ALWAYS_FALSE) break; |
| 1682 | |
| 1683 | // If there is no update statement, label the top of the loop with the |
| 1684 | // continue target, otherwise with the loop target. |
| 1685 | if (node->next() == NULL) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1686 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1687 | node->continue_target()->Bind(); |
| 1688 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1689 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1690 | loop.Bind(); |
| 1691 | } |
| 1692 | |
| 1693 | // If the test is always true, there is no need to compile it. |
| 1694 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1695 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1696 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1697 | &body, node->break_target(), true); |
| 1698 | if (has_valid_frame()) { |
| 1699 | Branch(false, node->break_target()); |
| 1700 | } |
| 1701 | if (has_valid_frame() || body.is_linked()) { |
| 1702 | body.Bind(); |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | if (has_valid_frame()) { |
| 1707 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1708 | VisitAndSpill(node->body()); |
| 1709 | |
| 1710 | if (node->next() == NULL) { |
| 1711 | // If there is no update statement and control flow can fall out |
| 1712 | // of the loop, jump directly to the continue label. |
| 1713 | if (has_valid_frame()) { |
| 1714 | node->continue_target()->Jump(); |
| 1715 | } |
| 1716 | } else { |
| 1717 | // If there is an update statement and control flow can reach it |
| 1718 | // via falling out of the body of the loop or continuing, we |
| 1719 | // compile the update statement. |
| 1720 | if (node->continue_target()->is_linked()) { |
| 1721 | node->continue_target()->Bind(); |
| 1722 | } |
| 1723 | if (has_valid_frame()) { |
| 1724 | // Record source position of the statement as this code which is |
| 1725 | // after the code for the body actually belongs to the loop |
| 1726 | // statement and not the body. |
| 1727 | CodeForStatementPosition(node); |
| 1728 | VisitAndSpill(node->next()); |
| 1729 | loop.Jump(); |
| 1730 | } |
| 1731 | } |
| 1732 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1733 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1734 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1737 | if (node->break_target()->is_linked()) { |
| 1738 | node->break_target()->Bind(); |
| 1739 | } |
| 1740 | node->continue_target()->Unuse(); |
| 1741 | node->break_target()->Unuse(); |
| 1742 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1746 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1747 | #ifdef DEBUG |
| 1748 | int original_height = frame_->height(); |
| 1749 | #endif |
| 1750 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1751 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1752 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1753 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1754 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1755 | JumpTarget primitive; |
| 1756 | JumpTarget jsobject; |
| 1757 | JumpTarget fixed_array; |
| 1758 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1759 | JumpTarget end_del_check; |
| 1760 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1761 | |
| 1762 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1763 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1764 | |
| 1765 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1766 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1767 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1768 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1769 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1770 | __ cmp(r0, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1771 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Stack layout in body: |
| 1774 | // [iteration counter (Smi)] |
| 1775 | // [length of array] |
| 1776 | // [FixedArray] |
| 1777 | // [Map or 0] |
| 1778 | // [Object] |
| 1779 | |
| 1780 | // Check if enumerable is already a JSObject |
| 1781 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1782 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1783 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1784 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1785 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1786 | primitive.Bind(); |
| 1787 | frame_->EmitPush(r0); |
| 1788 | Result arg_count = allocator_->Allocate(r0); |
| 1789 | ASSERT(arg_count.is_valid()); |
| 1790 | __ mov(arg_count.reg(), Operand(0)); |
| 1791 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1792 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1793 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1794 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1795 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1796 | frame_->EmitPush(r0); |
| 1797 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1798 | |
| 1799 | // If we got a Map, we can do a fast modification check. |
| 1800 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1801 | __ mov(r2, Operand(r0)); |
| 1802 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1803 | __ cmp(r1, Operand(Factory::meta_map())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1804 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1805 | |
| 1806 | // Get enum cache |
| 1807 | __ mov(r1, Operand(r0)); |
| 1808 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1809 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1810 | __ ldr(r2, |
| 1811 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1812 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1813 | frame_->EmitPush(r0); // map |
| 1814 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1815 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1816 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1817 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1818 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1819 | frame_->EmitPush(r0); |
| 1820 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1821 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1822 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1823 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1824 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1825 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1826 | |
| 1827 | // 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] | 1828 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1829 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1830 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1831 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1832 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1833 | |
| 1834 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1835 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1836 | // sp[0] : index |
| 1837 | // sp[1] : array/enum cache length |
| 1838 | // sp[2] : array or enum cache |
| 1839 | // sp[3] : 0 or map |
| 1840 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1841 | // Grab the current frame's height for the break and continue |
| 1842 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1843 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1844 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1845 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1846 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1847 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1848 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1849 | node->break_target()->Branch(hs); |
| 1850 | |
| 1851 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1852 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1853 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1854 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1855 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1856 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1857 | |
| 1858 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1859 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1860 | // Check if this (still) matches the map of the enumerable. |
| 1861 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1862 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1863 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1864 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1865 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1866 | |
| 1867 | // 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] | 1868 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1869 | frame_->EmitPush(r0); |
| 1870 | frame_->EmitPush(r3); // push entry |
| 1871 | Result arg_count_register = allocator_->Allocate(r0); |
| 1872 | ASSERT(arg_count_register.is_valid()); |
| 1873 | __ mov(arg_count_register.reg(), Operand(1)); |
| 1874 | Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY, |
| 1875 | CALL_JS, |
| 1876 | &arg_count_register, |
| 1877 | 2); |
| 1878 | __ mov(r3, Operand(result.reg())); |
| 1879 | result.Unuse(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1880 | |
| 1881 | // If the property has been removed while iterating, we just skip it. |
| 1882 | __ cmp(r3, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1883 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1885 | end_del_check.Bind(); |
| 1886 | // Store the entry in the 'each' expression and take another spin in the |
| 1887 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1888 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1889 | { Reference each(this, node->each()); |
| 1890 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1891 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1892 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1893 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1894 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1895 | // If the reference was to a slot we rely on the convenient property |
| 1896 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1897 | // right on top of or right underneath a zero-sized reference. |
| 1898 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1899 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1900 | // It's safe to pop the value lying on top of the reference before |
| 1901 | // unloading the reference itself (which preserves the top of stack, |
| 1902 | // ie, now the topmost value of the non-zero sized reference), since |
| 1903 | // we will discard the top of stack after unloading the reference |
| 1904 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1905 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1906 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1907 | } |
| 1908 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1909 | // Discard the i'th entry pushed above or else the remainder of the |
| 1910 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1911 | frame_->Drop(); |
| 1912 | |
| 1913 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1914 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1915 | VisitAndSpill(node->body()); |
| 1916 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1917 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1918 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1919 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1920 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1921 | frame_->EmitPop(r0); |
| 1922 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1923 | frame_->EmitPush(r0); |
| 1924 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1925 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1926 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1927 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1928 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1929 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1930 | |
| 1931 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1932 | exit.Bind(); |
| 1933 | node->continue_target()->Unuse(); |
| 1934 | node->break_target()->Unuse(); |
| 1935 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1939 | void CodeGenerator::VisitTryCatch(TryCatch* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1940 | #ifdef DEBUG |
| 1941 | int original_height = frame_->height(); |
| 1942 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1943 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1944 | Comment cmnt(masm_, "[ TryCatch"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1945 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1946 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1947 | JumpTarget try_block; |
| 1948 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1949 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1950 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1951 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1952 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Store the caught exception in the catch variable. |
| 1955 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1956 | ASSERT(ref.is_slot()); |
| 1957 | // Here we make use of the convenient property that it doesn't matter |
| 1958 | // whether a value is immediately on top of or underneath a zero-sized |
| 1959 | // reference. |
| 1960 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1964 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1965 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1966 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 1967 | if (frame_ != NULL) { |
| 1968 | exit.Jump(); |
| 1969 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1970 | |
| 1971 | |
| 1972 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1973 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1974 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1975 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 1976 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1977 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1978 | // Shadow the labels for all escapes from the try block, including |
| 1979 | // returns. During shadowing, the original label is hidden as the |
| 1980 | // LabelShadow and operations on the original actually affect the |
| 1981 | // shadowing label. |
| 1982 | // |
| 1983 | // We should probably try to unify the escaping labels and the return |
| 1984 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1985 | int nof_escapes = node->escaping_targets()->length(); |
| 1986 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 1987 | |
| 1988 | // Add the shadow target for the function return. |
| 1989 | static const int kReturnShadowIndex = 0; |
| 1990 | shadows.Add(new ShadowTarget(&function_return_)); |
| 1991 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 1992 | function_return_is_shadowed_ = true; |
| 1993 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 1994 | |
| 1995 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1996 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1997 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2001 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2002 | |
| 2003 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2004 | // After shadowing stops, the original labels are unshadowed and the |
| 2005 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2006 | bool has_unlinks = false; |
| 2007 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2008 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2009 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2010 | } |
| 2011 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2012 | |
| 2013 | // Get an external reference to the handler address. |
| 2014 | ExternalReference handler_address(Top::k_handler_address); |
| 2015 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2016 | // If we can fall off the end of the try block, unlink from try chain. |
| 2017 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2018 | // The next handler address is on top of the frame. Unlink from |
| 2019 | // the handler list and drop the rest of this handler from the |
| 2020 | // frame. |
| 2021 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2022 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2023 | __ mov(r3, Operand(handler_address)); |
| 2024 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2025 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2026 | if (has_unlinks) { |
| 2027 | exit.Jump(); |
| 2028 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2031 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2032 | // jumped to. Deallocate each shadow target. |
| 2033 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2034 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2035 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2036 | shadows[i]->Bind(); |
| 2037 | // Because we can be jumping here (to spilled code) from unspilled |
| 2038 | // code, we need to reestablish a spilled frame at this block. |
| 2039 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2040 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2041 | // Reload sp from the top handler, because some statements that we |
| 2042 | // 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] | 2043 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2044 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2045 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2046 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2047 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2048 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2049 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2050 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2052 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2053 | frame_->PrepareForReturn(); |
| 2054 | } |
| 2055 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2059 | exit.Bind(); |
| 2060 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2064 | void CodeGenerator::VisitTryFinally(TryFinally* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2065 | #ifdef DEBUG |
| 2066 | int original_height = frame_->height(); |
| 2067 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2068 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2069 | Comment cmnt(masm_, "[ TryFinally"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2070 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2071 | |
| 2072 | // State: Used to keep track of reason for entering the finally |
| 2073 | // block. Should probably be extended to hold information for |
| 2074 | // break/continue from within the try block. |
| 2075 | enum { FALLING, THROWING, JUMPING }; |
| 2076 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2077 | JumpTarget try_block; |
| 2078 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2079 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2080 | try_block.Call(); |
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_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2083 | // In case of thrown exceptions, this is where we continue. |
| 2084 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2085 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2086 | |
| 2087 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2088 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2089 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2090 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2091 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2093 | // Shadow the labels for all escapes from the try block, including |
| 2094 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2095 | // operations on the original actually affect the shadowing label. |
| 2096 | // |
| 2097 | // We should probably try to unify the escaping labels and the return |
| 2098 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2099 | int nof_escapes = node->escaping_targets()->length(); |
| 2100 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2101 | |
| 2102 | // Add the shadow target for the function return. |
| 2103 | static const int kReturnShadowIndex = 0; |
| 2104 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2105 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2106 | function_return_is_shadowed_ = true; |
| 2107 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2108 | |
| 2109 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2110 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2111 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2115 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2116 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2117 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2118 | // After shadowing stops, the original labels are unshadowed and the |
| 2119 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2120 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2121 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2122 | shadows[i]->StopShadowing(); |
| 2123 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2124 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2125 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2126 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2127 | // Get an external reference to the handler address. |
| 2128 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2129 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2130 | // If we can fall off the end of the try block, unlink from the try |
| 2131 | // chain and set the state on the frame to FALLING. |
| 2132 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2133 | // The next handler address is on top of the frame. |
| 2134 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2135 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2136 | __ mov(r3, Operand(handler_address)); |
| 2137 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2138 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2139 | |
| 2140 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2141 | // state in r2, then jump around the unlink blocks if any. |
| 2142 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2143 | frame_->EmitPush(r0); |
| 2144 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2145 | if (nof_unlinks > 0) { |
| 2146 | finally_block.Jump(); |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | // Generate code to unlink and set the state for the (formerly) |
| 2151 | // shadowing targets that have been jumped to. |
| 2152 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2153 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2154 | // If we have come from the shadowed return, the return value is |
| 2155 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2156 | // until it is pushed. |
| 2157 | // |
| 2158 | // Because we can be jumping here (to spilled code) from |
| 2159 | // unspilled code, we need to reestablish a spilled frame at |
| 2160 | // this block. |
| 2161 | shadows[i]->Bind(); |
| 2162 | frame_->SpillAll(); |
| 2163 | |
| 2164 | // Reload sp from the top handler, because some statements that |
| 2165 | // we break from (eg, for...in) may have left stuff on the |
| 2166 | // stack. |
| 2167 | __ mov(r3, Operand(handler_address)); |
| 2168 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2169 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2170 | |
| 2171 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2172 | // handler address is currently on top of the frame. |
| 2173 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2174 | frame_->EmitPop(r1); |
| 2175 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2176 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2177 | |
| 2178 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2179 | // If this label shadowed the function return, materialize the |
| 2180 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2181 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2182 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2183 | // Fake TOS for targets that shadowed breaks and continues. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2184 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2185 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2186 | } |
| 2187 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2188 | if (--nof_unlinks > 0) { |
| 2189 | // If this is not the last unlink block, jump around the next. |
| 2190 | finally_block.Jump(); |
| 2191 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2192 | } |
| 2193 | } |
| 2194 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2195 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2196 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2197 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2198 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2199 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2200 | |
| 2201 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2202 | // and the state - while evaluating the finally block. |
| 2203 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2204 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2205 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2206 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2207 | if (has_valid_frame()) { |
| 2208 | // Restore state and return value or faked TOS. |
| 2209 | frame_->EmitPop(r2); |
| 2210 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2213 | // Generate code to jump to the right destination for all used |
| 2214 | // formerly shadowing targets. Deallocate each shadow target. |
| 2215 | for (int i = 0; i < shadows.length(); i++) { |
| 2216 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2217 | JumpTarget* original = shadows[i]->other_target(); |
| 2218 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2219 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2220 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2221 | skip.Branch(ne); |
| 2222 | frame_->PrepareForReturn(); |
| 2223 | original->Jump(); |
| 2224 | skip.Bind(); |
| 2225 | } else { |
| 2226 | original->Branch(eq); |
| 2227 | } |
| 2228 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2229 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2230 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2231 | if (has_valid_frame()) { |
| 2232 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2233 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2234 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2235 | exit.Branch(ne); |
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 | // Rethrow exception. |
| 2238 | frame_->EmitPush(r0); |
| 2239 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2240 | |
| 2241 | // Done. |
| 2242 | exit.Bind(); |
| 2243 | } |
| 2244 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2248 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2249 | #ifdef DEBUG |
| 2250 | int original_height = frame_->height(); |
| 2251 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2252 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2253 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2254 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2255 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2256 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2257 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2258 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2259 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2263 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2264 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2265 | ASSERT(boilerplate->IsBoilerplate()); |
| 2266 | |
| 2267 | // Push the boilerplate on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2268 | __ mov(r0, Operand(boilerplate)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2269 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2270 | |
| 2271 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2272 | frame_->EmitPush(cp); |
| 2273 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2274 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2278 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2279 | #ifdef DEBUG |
| 2280 | int original_height = frame_->height(); |
| 2281 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2282 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2283 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2284 | |
| 2285 | // Build the function boilerplate and instantiate it. |
| 2286 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2287 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2288 | if (HasStackOverflow()) { |
| 2289 | ASSERT(frame_->height() == original_height); |
| 2290 | return; |
| 2291 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2292 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2293 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2297 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2298 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2299 | #ifdef DEBUG |
| 2300 | int original_height = frame_->height(); |
| 2301 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2302 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2303 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2304 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2305 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2309 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2310 | #ifdef DEBUG |
| 2311 | int original_height = frame_->height(); |
| 2312 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2313 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2314 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2315 | JumpTarget then; |
| 2316 | JumpTarget else_; |
| 2317 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2318 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 2319 | &then, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2320 | Branch(false, &else_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2321 | then.Bind(); |
| 2322 | LoadAndSpill(node->then_expression(), typeof_state()); |
| 2323 | exit.Jump(); |
| 2324 | else_.Bind(); |
| 2325 | LoadAndSpill(node->else_expression(), typeof_state()); |
| 2326 | exit.Bind(); |
| 2327 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2331 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2332 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2333 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2334 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2335 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2336 | JumpTarget slow; |
| 2337 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2338 | |
| 2339 | // Generate fast-case code for variables that might be shadowed by |
| 2340 | // eval-introduced variables. Eval is used a lot without |
| 2341 | // introducing variables. In those cases, we do not want to |
| 2342 | // perform a runtime call for all variables in the scope |
| 2343 | // containing the eval. |
| 2344 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2345 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2346 | // If there was no control flow to slow, we can exit early. |
| 2347 | if (!slow.is_linked()) { |
| 2348 | frame_->EmitPush(r0); |
| 2349 | return; |
| 2350 | } |
| 2351 | |
| 2352 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2353 | |
| 2354 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2355 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2356 | // Only generate the fast case for locals that rewrite to slots. |
| 2357 | // This rules out argument loads. |
| 2358 | if (potential_slot != NULL) { |
| 2359 | __ ldr(r0, |
| 2360 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2361 | r1, |
| 2362 | r2, |
| 2363 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2364 | if (potential_slot->var()->mode() == Variable::CONST) { |
| 2365 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2366 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
| 2367 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2368 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2369 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2370 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2371 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2372 | } |
| 2373 | } |
| 2374 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2375 | slow.Bind(); |
| 2376 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2377 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2378 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2379 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2380 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2381 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2382 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2383 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2384 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2385 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2386 | done.Bind(); |
| 2387 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2388 | |
| 2389 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2390 | // Note: We would like to keep the assert below, but it fires because of |
| 2391 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2392 | // ASSERT(!slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2393 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2394 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2395 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2396 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2397 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2398 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2399 | // initialized yet) which needs to be converted into the 'undefined' |
| 2400 | // value. |
| 2401 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2402 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2403 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2404 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2405 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2411 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2412 | TypeofState typeof_state, |
| 2413 | Register tmp, |
| 2414 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2415 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2416 | // Check that no extension objects have been created by calls to |
| 2417 | // eval from the current scope to the global scope. |
| 2418 | Register context = cp; |
| 2419 | Scope* s = scope(); |
| 2420 | while (s != NULL) { |
| 2421 | if (s->num_heap_slots() > 0) { |
| 2422 | if (s->calls_eval()) { |
| 2423 | // Check that extension is NULL. |
| 2424 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2425 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2426 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2427 | } |
| 2428 | // Load next context in chain. |
| 2429 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2430 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2431 | context = tmp; |
| 2432 | } |
| 2433 | // If no outer scope calls eval, we do not need to check more |
| 2434 | // context extensions. |
| 2435 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2436 | s = s->outer_scope(); |
| 2437 | } |
| 2438 | |
| 2439 | if (s->is_eval_scope()) { |
| 2440 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2441 | if (!context.is(tmp)) { |
| 2442 | __ mov(tmp, Operand(context)); |
| 2443 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2444 | __ bind(&next); |
| 2445 | // Terminate at global context. |
| 2446 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 2447 | __ cmp(tmp2, Operand(Factory::global_context_map())); |
| 2448 | __ b(eq, &fast); |
| 2449 | // Check that extension is NULL. |
| 2450 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2451 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2452 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2453 | // Load next context in chain. |
| 2454 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2455 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2456 | __ b(&next); |
| 2457 | __ bind(&fast); |
| 2458 | } |
| 2459 | |
| 2460 | // All extension objects were empty and it is safe to use a global |
| 2461 | // load IC call. |
| 2462 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2463 | // Load the global object. |
| 2464 | LoadGlobal(); |
| 2465 | // Setup the name register. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2466 | Result name = allocator_->Allocate(r2); |
| 2467 | ASSERT(name.is_valid()); // We are in spilled code. |
| 2468 | __ mov(name.reg(), Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2469 | // Call IC stub. |
| 2470 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2471 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2472 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2473 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2476 | // Drop the global object. The result is in r0. |
| 2477 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2481 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2482 | #ifdef DEBUG |
| 2483 | int original_height = frame_->height(); |
| 2484 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2485 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2486 | Comment cmnt(masm_, "[ Slot"); |
| 2487 | LoadFromSlot(node, typeof_state()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2488 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2489 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2490 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2491 | |
| 2492 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2493 | #ifdef DEBUG |
| 2494 | int original_height = frame_->height(); |
| 2495 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2496 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2497 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2498 | |
| 2499 | Variable* var = node->var(); |
| 2500 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2501 | if (expr != NULL) { |
| 2502 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2503 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2504 | ASSERT(var->is_global()); |
| 2505 | Reference ref(this, node); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2506 | ref.GetValueAndSpill(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2507 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2508 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2512 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2513 | #ifdef DEBUG |
| 2514 | int original_height = frame_->height(); |
| 2515 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2516 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2517 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2518 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2519 | frame_->EmitPush(r0); |
| 2520 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2524 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2525 | #ifdef DEBUG |
| 2526 | int original_height = frame_->height(); |
| 2527 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2528 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2529 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2530 | |
| 2531 | // Retrieve the literal array and check the allocated entry. |
| 2532 | |
| 2533 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2534 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2535 | |
| 2536 | // Load the literals array of the function. |
| 2537 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2538 | |
| 2539 | // Load the literal at the ast saved index. |
| 2540 | int literal_offset = |
| 2541 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2542 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2543 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2544 | JumpTarget done; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2545 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2546 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2547 | |
| 2548 | // If the entry is undefined we call the runtime system to computed |
| 2549 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2550 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2551 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2552 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2553 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2554 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2555 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2556 | frame_->EmitPush(r0); |
| 2557 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2558 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2559 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2560 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2561 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2562 | frame_->EmitPush(r2); |
| 2563 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
| 2566 | |
| 2567 | // This deferred code stub will be used for creating the boilerplate |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2568 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2569 | // Each created boilerplate is stored in the JSFunction and they are |
| 2570 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2571 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2572 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2573 | explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2574 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2575 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2576 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2577 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2578 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2579 | private: |
| 2580 | ObjectLiteral* node_; |
| 2581 | }; |
| 2582 | |
| 2583 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2584 | void DeferredObjectLiteral::Generate() { |
| 2585 | // Argument is passed in r1. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2586 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2587 | // 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] | 2588 | // the literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2589 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2590 | __ push(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2591 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2592 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2593 | __ push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2594 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2595 | __ mov(r0, Operand(node_->constant_properties())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2596 | __ push(r0); |
| 2597 | __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2598 | __ mov(r2, Operand(r0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2599 | // Result is returned in r2. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2603 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2604 | #ifdef DEBUG |
| 2605 | int original_height = frame_->height(); |
| 2606 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2607 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2608 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2609 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2610 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2611 | |
| 2612 | // Retrieve the literal array and check the allocated entry. |
| 2613 | |
| 2614 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2615 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2616 | |
| 2617 | // Load the literals array of the function. |
| 2618 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2619 | |
| 2620 | // Load the literal at the ast saved index. |
| 2621 | int literal_offset = |
| 2622 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2623 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2624 | |
| 2625 | // Check whether we need to materialize the object literal boilerplate. |
| 2626 | // If so, jump to the deferred code. |
| 2627 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2628 | deferred->Branch(eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2629 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2630 | |
| 2631 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2632 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2633 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2634 | // Clone the boilerplate object. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2635 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2636 | if (node->depth() == 1) { |
| 2637 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2638 | } |
| 2639 | frame_->CallRuntime(clone_function_id, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2640 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2641 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2642 | |
| 2643 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2644 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2645 | Literal* key = property->key(); |
| 2646 | Expression* value = property->value(); |
| 2647 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2648 | case ObjectLiteral::Property::CONSTANT: |
| 2649 | break; |
| 2650 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2651 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2652 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2653 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2654 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2655 | frame_->EmitPush(r0); // dup the result |
| 2656 | LoadAndSpill(key); |
| 2657 | LoadAndSpill(value); |
| 2658 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2659 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2660 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2661 | break; |
| 2662 | } |
| 2663 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2664 | frame_->EmitPush(r0); |
| 2665 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2666 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2667 | frame_->EmitPush(r0); |
| 2668 | LoadAndSpill(value); |
| 2669 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2670 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2671 | break; |
| 2672 | } |
| 2673 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2674 | frame_->EmitPush(r0); |
| 2675 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2676 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2677 | frame_->EmitPush(r0); |
| 2678 | LoadAndSpill(value); |
| 2679 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2680 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2681 | break; |
| 2682 | } |
| 2683 | } |
| 2684 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2685 | ASSERT(frame_->height() == original_height + 1); |
| 2686 | } |
| 2687 | |
| 2688 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2689 | // This deferred code stub will be used for creating the boilerplate |
| 2690 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2691 | // Each created boilerplate is stored in the JSFunction and they are |
| 2692 | // therefore context dependent. |
| 2693 | class DeferredArrayLiteral: public DeferredCode { |
| 2694 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2695 | explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2696 | set_comment("[ DeferredArrayLiteral"); |
| 2697 | } |
| 2698 | |
| 2699 | virtual void Generate(); |
| 2700 | |
| 2701 | private: |
| 2702 | ArrayLiteral* node_; |
| 2703 | }; |
| 2704 | |
| 2705 | |
| 2706 | void DeferredArrayLiteral::Generate() { |
| 2707 | // Argument is passed in r1. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2708 | |
| 2709 | // If the entry is undefined we call the runtime system to computed |
| 2710 | // the literal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2711 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2712 | __ push(r1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2713 | // Literal index (1). |
| 2714 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2715 | __ push(r0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2716 | // Constant properties (2). |
| 2717 | __ mov(r0, Operand(node_->literals())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2718 | __ push(r0); |
| 2719 | __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2720 | __ mov(r2, Operand(r0)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2721 | // Result is returned in r2. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2725 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2726 | #ifdef DEBUG |
| 2727 | int original_height = frame_->height(); |
| 2728 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2729 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2730 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2731 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2732 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2733 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2734 | // Retrieve the literal array and check the allocated entry. |
| 2735 | |
| 2736 | // Load the function of this activation. |
| 2737 | __ ldr(r1, frame_->Function()); |
| 2738 | |
| 2739 | // Load the literals array of the function. |
| 2740 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2741 | |
| 2742 | // Load the literal at the ast saved index. |
| 2743 | int literal_offset = |
| 2744 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2745 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2746 | |
| 2747 | // Check whether we need to materialize the object literal boilerplate. |
| 2748 | // If so, jump to the deferred code. |
| 2749 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2750 | deferred->Branch(eq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2751 | deferred->BindExit(); |
| 2752 | |
| 2753 | // Push the object literal boilerplate. |
| 2754 | frame_->EmitPush(r2); |
| 2755 | |
| 2756 | // Clone the boilerplate object. |
| 2757 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2758 | if (node->depth() == 1) { |
| 2759 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2760 | } |
| 2761 | frame_->CallRuntime(clone_function_id, 1); |
| 2762 | frame_->EmitPush(r0); // save the result |
| 2763 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2764 | |
| 2765 | // Generate code to set the elements in the array that are not |
| 2766 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2767 | for (int i = 0; i < node->values()->length(); i++) { |
| 2768 | Expression* value = node->values()->at(i); |
| 2769 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2770 | // If value is a literal the property value is already set in the |
| 2771 | // boilerplate object. |
| 2772 | if (value->AsLiteral() != NULL) continue; |
| 2773 | // If value is a materialized literal the property value is already set |
| 2774 | // in the boilerplate object if it is simple. |
| 2775 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2776 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2777 | // The property must be set by generated code. |
| 2778 | LoadAndSpill(value); |
| 2779 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2780 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2781 | // Fetch the object literal. |
| 2782 | __ ldr(r1, frame_->Top()); |
| 2783 | // Get the elements array. |
| 2784 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2785 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2786 | // Write to the indexed properties array. |
| 2787 | int offset = i * kPointerSize + Array::kHeaderSize; |
| 2788 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2789 | |
| 2790 | // Update the write barrier for the array address. |
| 2791 | __ mov(r3, Operand(offset)); |
| 2792 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2793 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2794 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2798 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2799 | #ifdef DEBUG |
| 2800 | int original_height = frame_->height(); |
| 2801 | #endif |
| 2802 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2803 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2804 | // Call runtime routine to allocate the catch extension object and |
| 2805 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2806 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2807 | LoadAndSpill(node->key()); |
| 2808 | LoadAndSpill(node->value()); |
| 2809 | Result result = |
| 2810 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2811 | frame_->EmitPush(result.reg()); |
| 2812 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2816 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2817 | #ifdef DEBUG |
| 2818 | int original_height = frame_->height(); |
| 2819 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2820 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2821 | Comment cmnt(masm_, "[ Assignment"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2822 | CodeForStatementPosition(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2823 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2824 | { Reference target(this, node->target()); |
| 2825 | if (target.is_illegal()) { |
| 2826 | // Fool the virtual frame into thinking that we left the assignment's |
| 2827 | // value on the frame. |
| 2828 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2829 | frame_->EmitPush(r0); |
| 2830 | ASSERT(frame_->height() == original_height + 1); |
| 2831 | return; |
| 2832 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2833 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2834 | if (node->op() == Token::ASSIGN || |
| 2835 | node->op() == Token::INIT_VAR || |
| 2836 | node->op() == Token::INIT_CONST) { |
| 2837 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2838 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2839 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2840 | // +=, *= and similar binary assignments. |
| 2841 | // Get the old value of the lhs. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2842 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 2843 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2844 | bool overwrite = |
| 2845 | (node->value()->AsBinaryOperation() != NULL && |
| 2846 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2847 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2848 | SmiOperation(node->binary_op(), |
| 2849 | literal->handle(), |
| 2850 | false, |
| 2851 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2852 | frame_->EmitPush(r0); |
| 2853 | |
| 2854 | } else { |
| 2855 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2856 | GenericBinaryOperation(node->binary_op(), |
| 2857 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2858 | frame_->EmitPush(r0); |
| 2859 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2860 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2861 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2862 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2863 | if (var != NULL && |
| 2864 | (var->mode() == Variable::CONST) && |
| 2865 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2866 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2867 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2868 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2869 | CodeForSourcePosition(node->position()); |
| 2870 | if (node->op() == Token::INIT_CONST) { |
| 2871 | // Dynamic constant initializations must use the function context |
| 2872 | // and initialize the actual constant declared. Dynamic variable |
| 2873 | // initializations are simply assignments and use SetValue. |
| 2874 | target.SetValue(CONST_INIT); |
| 2875 | } else { |
| 2876 | target.SetValue(NOT_CONST_INIT); |
| 2877 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2878 | } |
| 2879 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2880 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2884 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2885 | #ifdef DEBUG |
| 2886 | int original_height = frame_->height(); |
| 2887 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2888 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2889 | Comment cmnt(masm_, "[ Throw"); |
| 2890 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2891 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2892 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2893 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2894 | frame_->EmitPush(r0); |
| 2895 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2899 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2900 | #ifdef DEBUG |
| 2901 | int original_height = frame_->height(); |
| 2902 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2903 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2904 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2905 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2906 | { Reference property(this, node); |
| 2907 | property.GetValueAndSpill(typeof_state()); |
| 2908 | } |
| 2909 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2913 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2914 | #ifdef DEBUG |
| 2915 | int original_height = frame_->height(); |
| 2916 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2917 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2918 | Comment cmnt(masm_, "[ Call"); |
| 2919 | |
| 2920 | ZoneList<Expression*>* args = node->arguments(); |
| 2921 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2922 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2923 | // Standard function call. |
| 2924 | |
| 2925 | // Check if the function is a variable or a property. |
| 2926 | Expression* function = node->expression(); |
| 2927 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2928 | Property* property = function->AsProperty(); |
| 2929 | |
| 2930 | // ------------------------------------------------------------------------ |
| 2931 | // Fast-case: Use inline caching. |
| 2932 | // --- |
| 2933 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2934 | // must be resolved after the arguments have been evaluated. The IC code |
| 2935 | // automatically handles this by loading the arguments before the function |
| 2936 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2937 | // ------------------------------------------------------------------------ |
| 2938 | |
| 2939 | if (var != NULL && !var->is_this() && var->is_global()) { |
| 2940 | // ---------------------------------- |
| 2941 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 2942 | // ---------------------------------- |
| 2943 | |
| 2944 | // 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] | 2945 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2946 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2947 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2948 | // Pass the global object as the receiver and let the IC stub |
| 2949 | // patch the stack to use the global proxy as 'this' in the |
| 2950 | // invoked function. |
| 2951 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2952 | |
| 2953 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2954 | int arg_count = args->length(); |
| 2955 | for (int i = 0; i < arg_count; i++) { |
| 2956 | LoadAndSpill(args->at(i)); |
| 2957 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2958 | |
| 2959 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2960 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 2961 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2962 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2963 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 2964 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2965 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2966 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2967 | frame_->Drop(); |
| 2968 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2969 | |
| 2970 | } else if (var != NULL && var->slot() != NULL && |
| 2971 | var->slot()->type() == Slot::LOOKUP) { |
| 2972 | // ---------------------------------- |
| 2973 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 2974 | // ---------------------------------- |
| 2975 | |
| 2976 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2977 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2978 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2979 | frame_->EmitPush(r0); |
| 2980 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2981 | // r0: slot value; r1: receiver |
| 2982 | |
| 2983 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2984 | frame_->EmitPush(r0); // function |
| 2985 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2986 | |
| 2987 | // Call the function. |
| 2988 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2989 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2990 | |
| 2991 | } else if (property != NULL) { |
| 2992 | // Check if the key is a literal string. |
| 2993 | Literal* literal = property->key()->AsLiteral(); |
| 2994 | |
| 2995 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 2996 | // ------------------------------------------------------------------ |
| 2997 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 2998 | // ------------------------------------------------------------------ |
| 2999 | |
| 3000 | // 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] | 3001 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3002 | frame_->EmitPush(r0); |
| 3003 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3004 | |
| 3005 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3006 | int arg_count = args->length(); |
| 3007 | for (int i = 0; i < arg_count; i++) { |
| 3008 | LoadAndSpill(args->at(i)); |
| 3009 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3010 | |
| 3011 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3012 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3013 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3014 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3015 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3016 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3017 | |
| 3018 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3019 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3020 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3021 | 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] | 3022 | |
| 3023 | } else { |
| 3024 | // ------------------------------------------- |
| 3025 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3026 | // ------------------------------------------- |
| 3027 | |
| 3028 | // Load the function to call from the property through a reference. |
| 3029 | Reference ref(this, property); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3030 | ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3031 | |
| 3032 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3033 | if (property->is_synthetic()) { |
| 3034 | LoadGlobalReceiver(r0); |
| 3035 | } else { |
| 3036 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3037 | frame_->EmitPush(r0); |
| 3038 | } |
| 3039 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3040 | // Call the function. |
| 3041 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3042 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | } else { |
| 3046 | // ---------------------------------- |
| 3047 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3048 | // ---------------------------------- |
| 3049 | |
| 3050 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3051 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3052 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3053 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3054 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3055 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3056 | // Call the function. |
| 3057 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3058 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3059 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3060 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
| 3063 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3064 | void CodeGenerator::VisitCallEval(CallEval* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3065 | #ifdef DEBUG |
| 3066 | int original_height = frame_->height(); |
| 3067 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3068 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3069 | Comment cmnt(masm_, "[ CallEval"); |
| 3070 | |
| 3071 | // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve |
| 3072 | // the function we need to call and the receiver of the call. |
| 3073 | // Then we call the resolved function using the given arguments. |
| 3074 | |
| 3075 | ZoneList<Expression*>* args = node->arguments(); |
| 3076 | Expression* function = node->expression(); |
| 3077 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3078 | CodeForStatementPosition(node); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3079 | |
| 3080 | // Prepare stack for call to resolved function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3081 | LoadAndSpill(function); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3082 | __ mov(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3083 | frame_->EmitPush(r2); // Slot for receiver |
| 3084 | int arg_count = args->length(); |
| 3085 | for (int i = 0; i < arg_count; i++) { |
| 3086 | LoadAndSpill(args->at(i)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | // Prepare stack for call to ResolvePossiblyDirectEval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3090 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3091 | frame_->EmitPush(r1); |
| 3092 | if (arg_count > 0) { |
| 3093 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3094 | frame_->EmitPush(r1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3095 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3096 | frame_->EmitPush(r2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | // Resolve the call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3100 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3101 | |
| 3102 | // Touch up stack with the right values for the function and the receiver. |
| 3103 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3104 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3105 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3106 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3107 | |
| 3108 | // Call the function. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3109 | CodeForSourcePosition(node->position()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3110 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3111 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3112 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3113 | frame_->CallStub(&call_function, arg_count + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3114 | |
| 3115 | __ ldr(cp, frame_->Context()); |
| 3116 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3117 | frame_->Drop(); |
| 3118 | frame_->EmitPush(r0); |
| 3119 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3123 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3124 | #ifdef DEBUG |
| 3125 | int original_height = frame_->height(); |
| 3126 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3127 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3128 | Comment cmnt(masm_, "[ CallNew"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3129 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3130 | |
| 3131 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3132 | // expression in new calls must be evaluated before the |
| 3133 | // arguments. This is different from ordinary calls, where the |
| 3134 | // actual function to call is resolved after the arguments have been |
| 3135 | // evaluated. |
| 3136 | |
| 3137 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3138 | // receiver. There is no need to use the global proxy here because |
| 3139 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3140 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3141 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3142 | |
| 3143 | // Push the arguments ("left-to-right") on the stack. |
| 3144 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3145 | int arg_count = args->length(); |
| 3146 | for (int i = 0; i < arg_count; i++) { |
| 3147 | LoadAndSpill(args->at(i)); |
| 3148 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3149 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3150 | // r0: the number of arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3151 | Result num_args = allocator_->Allocate(r0); |
| 3152 | ASSERT(num_args.is_valid()); |
| 3153 | __ mov(num_args.reg(), Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3154 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3155 | // Load the function into r1 as per calling convention. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3156 | Result function = allocator_->Allocate(r1); |
| 3157 | ASSERT(function.is_valid()); |
| 3158 | __ ldr(function.reg(), frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3159 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3160 | // Call the construct call builtin that handles allocation and |
| 3161 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3162 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3163 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
| 3164 | Result result = frame_->CallCodeObject(ic, |
| 3165 | RelocInfo::CONSTRUCT_CALL, |
| 3166 | &num_args, |
| 3167 | &function, |
| 3168 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3169 | |
| 3170 | // 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] | 3171 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3172 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3176 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3177 | VirtualFrame::SpilledScope spilled_scope; |
| 3178 | ASSERT(args->length() == 1); |
| 3179 | LoadAndSpill(args->at(0)); // Load the object. |
| 3180 | frame_->CallRuntime(Runtime::kClassOf, 1); |
| 3181 | frame_->EmitPush(r0); |
| 3182 | } |
| 3183 | |
| 3184 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3185 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3186 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3187 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3188 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3189 | LoadAndSpill(args->at(0)); |
| 3190 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3191 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3192 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3193 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3194 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3195 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3196 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3197 | // Load the value. |
| 3198 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3199 | leave.Bind(); |
| 3200 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3204 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3205 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3206 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3207 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3208 | LoadAndSpill(args->at(0)); // Load the object. |
| 3209 | LoadAndSpill(args->at(1)); // Load the value. |
| 3210 | frame_->EmitPop(r0); // r0 contains value |
| 3211 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3212 | // if (object->IsSmi()) return object. |
| 3213 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3214 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3215 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3216 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3217 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3218 | // Store the value. |
| 3219 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3220 | // Update the write barrier. |
| 3221 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3222 | __ RecordWrite(r1, r2, r3); |
| 3223 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3224 | leave.Bind(); |
| 3225 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3229 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3230 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3231 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3232 | LoadAndSpill(args->at(0)); |
| 3233 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3234 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3235 | cc_reg_ = eq; |
| 3236 | } |
| 3237 | |
| 3238 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3239 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3240 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3241 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3242 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3243 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3244 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3245 | LoadAndSpill(args->at(1)); |
| 3246 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3247 | __ CallRuntime(Runtime::kLog, 2); |
| 3248 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3249 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3250 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3251 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3255 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3256 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3257 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3258 | LoadAndSpill(args->at(0)); |
| 3259 | frame_->EmitPop(r0); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3260 | __ tst(r0, Operand(kSmiTagMask | 0x80000000)); |
| 3261 | cc_reg_ = eq; |
| 3262 | } |
| 3263 | |
| 3264 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3265 | // This should generate code that performs a charCodeAt() call or returns |
| 3266 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3267 | // 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] | 3268 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3269 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3270 | ASSERT(args->length() == 2); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3271 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3272 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3276 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3277 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3278 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3279 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3280 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3281 | // We need the CC bits to come out as not_equal in the case where the |
| 3282 | // object is a smi. This can't be done with the usual test opcode so |
| 3283 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3284 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3285 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3286 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3287 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3288 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3289 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3290 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3291 | cc_reg_ = eq; |
| 3292 | } |
| 3293 | |
| 3294 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3295 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3296 | VirtualFrame::SpilledScope spilled_scope; |
| 3297 | ASSERT(args->length() == 0); |
| 3298 | frame_->CallRuntime(Runtime::kIsConstructCall, 0); |
| 3299 | frame_->EmitPush(r0); |
| 3300 | } |
| 3301 | |
| 3302 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3303 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3304 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3305 | ASSERT(args->length() == 0); |
| 3306 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3307 | // Seed the result with the formal parameters count, which will be used |
| 3308 | // 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] | 3309 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3310 | |
| 3311 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3312 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3313 | frame_->CallStub(&stub, 0); |
| 3314 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3318 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3319 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3320 | ASSERT(args->length() == 1); |
| 3321 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3322 | // Satisfy contract with ArgumentsAccessStub: |
| 3323 | // 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] | 3324 | LoadAndSpill(args->at(0)); |
| 3325 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3326 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3327 | |
| 3328 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3329 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3330 | frame_->CallStub(&stub, 0); |
| 3331 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3335 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3336 | VirtualFrame::SpilledScope spilled_scope; |
| 3337 | ASSERT(args->length() == 0); |
| 3338 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3339 | RelocInfo::RUNTIME_ENTRY); |
| 3340 | frame_->EmitPush(r0); |
| 3341 | } |
| 3342 | |
| 3343 | |
| 3344 | void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) { |
| 3345 | VirtualFrame::SpilledScope spilled_scope; |
| 3346 | LoadAndSpill(args->at(0)); |
| 3347 | switch (op) { |
| 3348 | case SIN: |
| 3349 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 3350 | break; |
| 3351 | case COS: |
| 3352 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 3353 | break; |
| 3354 | } |
| 3355 | frame_->EmitPush(r0); |
| 3356 | } |
| 3357 | |
| 3358 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3359 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3360 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3361 | ASSERT(args->length() == 2); |
| 3362 | |
| 3363 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3364 | LoadAndSpill(args->at(0)); |
| 3365 | LoadAndSpill(args->at(1)); |
| 3366 | frame_->EmitPop(r0); |
| 3367 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3368 | __ cmp(r0, Operand(r1)); |
| 3369 | cc_reg_ = eq; |
| 3370 | } |
| 3371 | |
| 3372 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3373 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3374 | #ifdef DEBUG |
| 3375 | int original_height = frame_->height(); |
| 3376 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3377 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3378 | if (CheckForInlineRuntimeCall(node)) { |
| 3379 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3380 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3381 | return; |
| 3382 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3383 | |
| 3384 | ZoneList<Expression*>* args = node->arguments(); |
| 3385 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3386 | Runtime::Function* function = node->function(); |
| 3387 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3388 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3389 | // Prepare stack for calling JS runtime function. |
| 3390 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3391 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3392 | // Push the builtins object found in the current global object. |
| 3393 | __ ldr(r1, GlobalObject()); |
| 3394 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3395 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3396 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3397 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3398 | // Push the arguments ("left-to-right"). |
| 3399 | int arg_count = args->length(); |
| 3400 | for (int i = 0; i < arg_count; i++) { |
| 3401 | LoadAndSpill(args->at(i)); |
| 3402 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3403 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3404 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3405 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3406 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3407 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3408 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3409 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3410 | frame_->Drop(); |
| 3411 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3412 | } else { |
| 3413 | // Call the C runtime function. |
| 3414 | frame_->CallRuntime(function, arg_count); |
| 3415 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3416 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3417 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3421 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3422 | #ifdef DEBUG |
| 3423 | int original_height = frame_->height(); |
| 3424 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3425 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3426 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3427 | |
| 3428 | Token::Value op = node->op(); |
| 3429 | |
| 3430 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3431 | LoadConditionAndSpill(node->expression(), |
| 3432 | NOT_INSIDE_TYPEOF, |
| 3433 | false_target(), |
| 3434 | true_target(), |
| 3435 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3436 | cc_reg_ = NegateCondition(cc_reg_); |
| 3437 | |
| 3438 | } else if (op == Token::DELETE) { |
| 3439 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3440 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3441 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3442 | LoadAndSpill(property->obj()); |
| 3443 | LoadAndSpill(property->key()); |
| 3444 | Result arg_count = allocator_->Allocate(r0); |
| 3445 | ASSERT(arg_count.is_valid()); |
| 3446 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3447 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3448 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3449 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3450 | Slot* slot = variable->slot(); |
| 3451 | if (variable->is_global()) { |
| 3452 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3453 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3454 | frame_->EmitPush(r0); |
| 3455 | Result arg_count = allocator_->Allocate(r0); |
| 3456 | ASSERT(arg_count.is_valid()); |
| 3457 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3458 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3459 | |
| 3460 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3461 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3462 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3463 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3464 | frame_->EmitPush(r0); |
| 3465 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3466 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3467 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3468 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3469 | frame_->EmitPush(r0); |
| 3470 | Result arg_count = allocator_->Allocate(r0); |
| 3471 | ASSERT(arg_count.is_valid()); |
| 3472 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3473 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3474 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3475 | } else { |
| 3476 | // Default: Result of deleting non-global, not dynamically |
| 3477 | // introduced variables is false. |
| 3478 | __ mov(r0, Operand(Factory::false_value())); |
| 3479 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3480 | |
| 3481 | } else { |
| 3482 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3483 | LoadAndSpill(node->expression()); // may have side-effects |
| 3484 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3485 | __ mov(r0, Operand(Factory::true_value())); |
| 3486 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3487 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3488 | |
| 3489 | } else if (op == Token::TYPEOF) { |
| 3490 | // Special case for loading the typeof expression; see comment on |
| 3491 | // LoadTypeofExpression(). |
| 3492 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3493 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3494 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3495 | |
| 3496 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3497 | LoadAndSpill(node->expression()); |
| 3498 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3499 | switch (op) { |
| 3500 | case Token::NOT: |
| 3501 | case Token::DELETE: |
| 3502 | case Token::TYPEOF: |
| 3503 | UNREACHABLE(); // handled above |
| 3504 | break; |
| 3505 | |
| 3506 | case Token::SUB: { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3507 | bool overwrite = |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3508 | (node->expression()->AsBinaryOperation() != NULL && |
| 3509 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3510 | UnarySubStub stub(overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3511 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3512 | break; |
| 3513 | } |
| 3514 | |
| 3515 | case Token::BIT_NOT: { |
| 3516 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3517 | JumpTarget smi_label; |
| 3518 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3519 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3520 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3521 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3522 | frame_->EmitPush(r0); |
| 3523 | Result arg_count = allocator_->Allocate(r0); |
| 3524 | ASSERT(arg_count.is_valid()); |
| 3525 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3526 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3527 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3528 | continue_label.Jump(); |
| 3529 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3530 | __ mvn(r0, Operand(r0)); |
| 3531 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3532 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3533 | break; |
| 3534 | } |
| 3535 | |
| 3536 | case Token::VOID: |
| 3537 | // since the stack top is cached in r0, popping and then |
| 3538 | // pushing a value can be done by just writing to r0. |
| 3539 | __ mov(r0, Operand(Factory::undefined_value())); |
| 3540 | break; |
| 3541 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3542 | case Token::ADD: { |
| 3543 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3544 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3545 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3546 | continue_label.Branch(eq); |
| 3547 | frame_->EmitPush(r0); |
| 3548 | Result arg_count = allocator_->Allocate(r0); |
| 3549 | ASSERT(arg_count.is_valid()); |
| 3550 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3551 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3552 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3553 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3554 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3555 | default: |
| 3556 | UNREACHABLE(); |
| 3557 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3558 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3559 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3560 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3561 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3565 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3566 | #ifdef DEBUG |
| 3567 | int original_height = frame_->height(); |
| 3568 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3569 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3570 | Comment cmnt(masm_, "[ CountOperation"); |
| 3571 | |
| 3572 | bool is_postfix = node->is_postfix(); |
| 3573 | bool is_increment = node->op() == Token::INC; |
| 3574 | |
| 3575 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3576 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3577 | |
| 3578 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3579 | if (is_postfix) { |
| 3580 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3581 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3582 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3583 | |
| 3584 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3585 | if (target.is_illegal()) { |
| 3586 | // Spoof the virtual frame to have the expected height (one higher |
| 3587 | // than on entry). |
| 3588 | if (!is_postfix) { |
| 3589 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3590 | frame_->EmitPush(r0); |
| 3591 | } |
| 3592 | ASSERT(frame_->height() == original_height + 1); |
| 3593 | return; |
| 3594 | } |
| 3595 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 3596 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3597 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3598 | JumpTarget slow; |
| 3599 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3600 | |
| 3601 | // Load the value (1) into register r1. |
| 3602 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3603 | |
| 3604 | // Check for smi operand. |
| 3605 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3606 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3607 | |
| 3608 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3609 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3610 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3611 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3612 | |
| 3613 | // Perform optimistic increment/decrement. |
| 3614 | if (is_increment) { |
| 3615 | __ add(r0, r0, Operand(r1), SetCC); |
| 3616 | } else { |
| 3617 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3618 | } |
| 3619 | |
| 3620 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3621 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3622 | |
| 3623 | // Revert optimistic increment/decrement. |
| 3624 | if (is_increment) { |
| 3625 | __ sub(r0, r0, Operand(r1)); |
| 3626 | } else { |
| 3627 | __ add(r0, r0, Operand(r1)); |
| 3628 | } |
| 3629 | |
| 3630 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3631 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3632 | { |
| 3633 | // Convert the operand to a number. |
| 3634 | frame_->EmitPush(r0); |
| 3635 | Result arg_count = allocator_->Allocate(r0); |
| 3636 | ASSERT(arg_count.is_valid()); |
| 3637 | __ mov(arg_count.reg(), Operand(0)); |
| 3638 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3639 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3640 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3641 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3642 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3645 | // Compute the new value. |
| 3646 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3647 | frame_->EmitPush(r0); |
| 3648 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3649 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3650 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3651 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3652 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3656 | exit.Bind(); |
| 3657 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3658 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3662 | if (is_postfix) frame_->EmitPop(r0); |
| 3663 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3667 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3668 | #ifdef DEBUG |
| 3669 | int original_height = frame_->height(); |
| 3670 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3671 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3672 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3673 | Token::Value op = node->op(); |
| 3674 | |
| 3675 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3676 | // operators must yield the result of one of the two expressions |
| 3677 | // before any ToBoolean() conversions. This means that the value |
| 3678 | // produced by a && or || operator is not necessarily a boolean. |
| 3679 | |
| 3680 | // NOTE: If the left hand side produces a materialized value (not in |
| 3681 | // the CC register), we force the right hand side to do the |
| 3682 | // same. This is necessary because we may have to branch to the exit |
| 3683 | // after evaluating the left hand side (due to the shortcut |
| 3684 | // semantics), but the compiler must (statically) know if the result |
| 3685 | // of compiling the binary operation is materialized or not. |
| 3686 | |
| 3687 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3688 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3689 | LoadConditionAndSpill(node->left(), |
| 3690 | NOT_INSIDE_TYPEOF, |
| 3691 | &is_true, |
| 3692 | false_target(), |
| 3693 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3694 | if (has_cc()) { |
| 3695 | Branch(false, false_target()); |
| 3696 | |
| 3697 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3698 | is_true.Bind(); |
| 3699 | LoadConditionAndSpill(node->right(), |
| 3700 | NOT_INSIDE_TYPEOF, |
| 3701 | true_target(), |
| 3702 | false_target(), |
| 3703 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3704 | |
| 3705 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3706 | JumpTarget pop_and_continue; |
| 3707 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3708 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3709 | __ ldr(r0, frame_->Top()); // dup the stack top |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3710 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3711 | // Avoid popping the result if it converts to 'false' using the |
| 3712 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3713 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3714 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3715 | Branch(false, &exit); |
| 3716 | |
| 3717 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3718 | pop_and_continue.Bind(); |
| 3719 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3720 | |
| 3721 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3722 | is_true.Bind(); |
| 3723 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3724 | |
| 3725 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3726 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3730 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3731 | LoadConditionAndSpill(node->left(), |
| 3732 | NOT_INSIDE_TYPEOF, |
| 3733 | true_target(), |
| 3734 | &is_false, |
| 3735 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3736 | if (has_cc()) { |
| 3737 | Branch(true, true_target()); |
| 3738 | |
| 3739 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3740 | is_false.Bind(); |
| 3741 | LoadConditionAndSpill(node->right(), |
| 3742 | NOT_INSIDE_TYPEOF, |
| 3743 | true_target(), |
| 3744 | false_target(), |
| 3745 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3746 | |
| 3747 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3748 | JumpTarget pop_and_continue; |
| 3749 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3750 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3751 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3752 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3753 | // Avoid popping the result if it converts to 'true' using the |
| 3754 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3755 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3756 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3757 | Branch(true, &exit); |
| 3758 | |
| 3759 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3760 | pop_and_continue.Bind(); |
| 3761 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3762 | |
| 3763 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3764 | is_false.Bind(); |
| 3765 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3766 | |
| 3767 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3768 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | } else { |
| 3772 | // Optimize for the case where (at least) one of the expressions |
| 3773 | // is a literal small integer. |
| 3774 | Literal* lliteral = node->left()->AsLiteral(); |
| 3775 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3776 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 3777 | // never return a constant/immutable object. |
| 3778 | bool overwrite_left = |
| 3779 | (node->left()->AsBinaryOperation() != NULL && |
| 3780 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3781 | bool overwrite_right = |
| 3782 | (node->right()->AsBinaryOperation() != NULL && |
| 3783 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3784 | |
| 3785 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3786 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3787 | SmiOperation(node->op(), |
| 3788 | rliteral->handle(), |
| 3789 | false, |
| 3790 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3791 | |
| 3792 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3793 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3794 | SmiOperation(node->op(), |
| 3795 | lliteral->handle(), |
| 3796 | true, |
| 3797 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3798 | |
| 3799 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3800 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 3801 | if (overwrite_left) { |
| 3802 | overwrite_mode = OVERWRITE_LEFT; |
| 3803 | } else if (overwrite_right) { |
| 3804 | overwrite_mode = OVERWRITE_RIGHT; |
| 3805 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3806 | LoadAndSpill(node->left()); |
| 3807 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3808 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3809 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3810 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3811 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3812 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3813 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3817 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3818 | #ifdef DEBUG |
| 3819 | int original_height = frame_->height(); |
| 3820 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3821 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3822 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3823 | frame_->EmitPush(r0); |
| 3824 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3828 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3829 | #ifdef DEBUG |
| 3830 | int original_height = frame_->height(); |
| 3831 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3832 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3833 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3834 | |
| 3835 | // Get the expressions from the node. |
| 3836 | Expression* left = node->left(); |
| 3837 | Expression* right = node->right(); |
| 3838 | Token::Value op = node->op(); |
| 3839 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3840 | // To make null checks efficient, we check if either left or right is the |
| 3841 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3842 | // instead of calling the (very) general runtime routine for checking |
| 3843 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3844 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3845 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3846 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3847 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3848 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 3849 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3850 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3851 | LoadAndSpill(left_is_null ? right : left); |
| 3852 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3853 | __ cmp(r0, Operand(Factory::null_value())); |
| 3854 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3855 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 3856 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3857 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3858 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3859 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3860 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3861 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3862 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3863 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3864 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3865 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3866 | // It can be an undetectable object. |
| 3867 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3868 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 3869 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 3870 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3873 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3874 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3875 | return; |
| 3876 | } |
| 3877 | } |
| 3878 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3879 | // To make typeof testing for natives implemented in JavaScript really |
| 3880 | // efficient, we generate special code for expressions of the form: |
| 3881 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3882 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 3883 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 3884 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 3885 | (right->AsLiteral() != NULL && |
| 3886 | right->AsLiteral()->handle()->IsString())) { |
| 3887 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 3888 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3889 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3890 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3891 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3892 | |
| 3893 | if (check->Equals(Heap::number_symbol())) { |
| 3894 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3895 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3896 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3897 | __ cmp(r1, Operand(Factory::heap_number_map())); |
| 3898 | cc_reg_ = eq; |
| 3899 | |
| 3900 | } else if (check->Equals(Heap::string_symbol())) { |
| 3901 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3902 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3903 | |
| 3904 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3905 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3906 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3907 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3908 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3909 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3910 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3911 | |
| 3912 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3913 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 3914 | cc_reg_ = lt; |
| 3915 | |
| 3916 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 3917 | __ cmp(r1, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3918 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3919 | __ cmp(r1, Operand(Factory::false_value())); |
| 3920 | cc_reg_ = eq; |
| 3921 | |
| 3922 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 3923 | __ cmp(r1, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3924 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3925 | |
| 3926 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3927 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3928 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3929 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3930 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3931 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3932 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3933 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 3934 | |
| 3935 | cc_reg_ = eq; |
| 3936 | |
| 3937 | } else if (check->Equals(Heap::function_symbol())) { |
| 3938 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3939 | false_target()->Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3940 | __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3941 | cc_reg_ = eq; |
| 3942 | |
| 3943 | } else if (check->Equals(Heap::object_symbol())) { |
| 3944 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3945 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3946 | |
| 3947 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3948 | __ cmp(r1, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3949 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3950 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3951 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3952 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 3953 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3954 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3955 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3956 | |
| 3957 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3958 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3959 | false_target()->Branch(lt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3960 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 3961 | cc_reg_ = le; |
| 3962 | |
| 3963 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3964 | // Uncommon case: typeof testing against a string literal that is |
| 3965 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3966 | false_target()->Jump(); |
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 | ASSERT(!has_valid_frame() || |
| 3969 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3970 | return; |
| 3971 | } |
| 3972 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3973 | switch (op) { |
| 3974 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3975 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3976 | break; |
| 3977 | |
| 3978 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3979 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3980 | break; |
| 3981 | |
| 3982 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3983 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3984 | break; |
| 3985 | |
| 3986 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3987 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3988 | break; |
| 3989 | |
| 3990 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3991 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3992 | break; |
| 3993 | |
| 3994 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3995 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3996 | break; |
| 3997 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3998 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 3999 | LoadAndSpill(left); |
| 4000 | LoadAndSpill(right); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4001 | Result arg_count = allocator_->Allocate(r0); |
| 4002 | ASSERT(arg_count.is_valid()); |
| 4003 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 4004 | Result result = frame_->InvokeBuiltin(Builtins::IN, |
| 4005 | CALL_JS, |
| 4006 | &arg_count, |
| 4007 | 2); |
| 4008 | frame_->EmitPush(result.reg()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4009 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4010 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4011 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4012 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 4013 | LoadAndSpill(left); |
| 4014 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4015 | InstanceofStub stub; |
| 4016 | Result result = frame_->CallStub(&stub, 2); |
| 4017 | // At this point if instanceof succeeded then r0 == 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4018 | __ tst(result.reg(), Operand(result.reg())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4019 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4020 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4021 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4022 | |
| 4023 | default: |
| 4024 | UNREACHABLE(); |
| 4025 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4026 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4027 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4031 | #ifdef DEBUG |
| 4032 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4033 | #endif |
| 4034 | |
| 4035 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4036 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4037 | #define __ ACCESS_MASM(masm) |
| 4038 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4039 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4040 | Handle<String> Reference::GetName() { |
| 4041 | ASSERT(type_ == NAMED); |
| 4042 | Property* property = expression_->AsProperty(); |
| 4043 | if (property == NULL) { |
| 4044 | // Global variable reference treated as a named property reference. |
| 4045 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4046 | ASSERT(proxy->AsVariable() != NULL); |
| 4047 | ASSERT(proxy->AsVariable()->is_global()); |
| 4048 | return proxy->name(); |
| 4049 | } else { |
| 4050 | Literal* raw_name = property->key()->AsLiteral(); |
| 4051 | ASSERT(raw_name != NULL); |
| 4052 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4053 | } |
| 4054 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4055 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4056 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4057 | void Reference::GetValueAndSpill(TypeofState typeof_state) { |
| 4058 | ASSERT(cgen_->in_spilled_code()); |
| 4059 | cgen_->set_in_spilled_code(false); |
| 4060 | GetValue(typeof_state); |
| 4061 | cgen_->frame()->SpillAll(); |
| 4062 | cgen_->set_in_spilled_code(true); |
| 4063 | } |
| 4064 | |
| 4065 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4066 | void Reference::GetValue(TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4067 | ASSERT(!cgen_->in_spilled_code()); |
| 4068 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4069 | ASSERT(!is_illegal()); |
| 4070 | ASSERT(!cgen_->has_cc()); |
| 4071 | MacroAssembler* masm = cgen_->masm(); |
| 4072 | Property* property = expression_->AsProperty(); |
| 4073 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4074 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | switch (type_) { |
| 4078 | case SLOT: { |
| 4079 | Comment cmnt(masm, "[ Load from Slot"); |
| 4080 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4081 | ASSERT(slot != NULL); |
| 4082 | cgen_->LoadFromSlot(slot, typeof_state); |
| 4083 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4086 | case NAMED: { |
| 4087 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4088 | // distinction between expressions in a typeof and not in a typeof. If |
| 4089 | // there is a chance that reference errors can be thrown below, we |
| 4090 | // must distinguish between the two kinds of loads (typeof expression |
| 4091 | // loads must not throw a reference error). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4092 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4093 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4094 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4095 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4096 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4097 | // Setup the name register. |
| 4098 | Result name_reg = cgen_->allocator()->Allocate(r2); |
| 4099 | ASSERT(name_reg.is_valid()); |
| 4100 | __ mov(name_reg.reg(), Operand(name)); |
| 4101 | ASSERT(var == NULL || var->is_global()); |
| 4102 | RelocInfo::Mode rmode = (var == NULL) |
| 4103 | ? RelocInfo::CODE_TARGET |
| 4104 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4105 | Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4106 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4107 | break; |
| 4108 | } |
| 4109 | |
| 4110 | case KEYED: { |
| 4111 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4112 | // 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] | 4113 | |
| 4114 | // TODO(181): Implement inlined version of array indexing once |
| 4115 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4116 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4117 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4118 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4119 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4120 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4121 | ASSERT(var == NULL || var->is_global()); |
| 4122 | RelocInfo::Mode rmode = (var == NULL) |
| 4123 | ? RelocInfo::CODE_TARGET |
| 4124 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4125 | Result answer = frame->CallCodeObject(ic, rmode, 0); |
| 4126 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4127 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | default: |
| 4131 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4132 | } |
| 4133 | } |
| 4134 | |
| 4135 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4136 | void Reference::SetValue(InitState init_state) { |
| 4137 | ASSERT(!is_illegal()); |
| 4138 | ASSERT(!cgen_->has_cc()); |
| 4139 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4140 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4141 | Property* property = expression_->AsProperty(); |
| 4142 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4143 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4144 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4145 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4146 | switch (type_) { |
| 4147 | case SLOT: { |
| 4148 | Comment cmnt(masm, "[ Store to Slot"); |
| 4149 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4150 | ASSERT(slot != NULL); |
| 4151 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4152 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4153 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4154 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4155 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4156 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4157 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4158 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4159 | if (init_state == CONST_INIT) { |
| 4160 | // Same as the case for a normal store, but ignores attribute |
| 4161 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4162 | // const properties (introduced via eval("const foo = (some |
| 4163 | // expr);")). Also, uses the current function context instead of |
| 4164 | // the top context. |
| 4165 | // |
| 4166 | // Note that we must declare the foo upon entry of eval(), via a |
| 4167 | // context slot declaration, but we cannot initialize it at the |
| 4168 | // same time, because the const declaration may be at the end of |
| 4169 | // the eval code (sigh...) and the const variable may have been |
| 4170 | // used before (where its value is 'undefined'). Thus, we can only |
| 4171 | // do the initialization when we actually encounter the expression |
| 4172 | // and when the expression operands are defined and valid, and |
| 4173 | // thus we need the split into 2 operations: declaration of the |
| 4174 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4175 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4176 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4177 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4178 | } |
| 4179 | // Storing a variable must keep the (new) value on the expression |
| 4180 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4181 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4182 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4183 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4184 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4185 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4186 | JumpTarget exit; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4187 | if (init_state == CONST_INIT) { |
| 4188 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4189 | // Only the first const initialization must be executed (the slot |
| 4190 | // still contains 'the hole' value). When the assignment is |
| 4191 | // executed, the code is identical to a normal store (see below). |
| 4192 | Comment cmnt(masm, "[ Init const"); |
| 4193 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
| 4194 | __ cmp(r2, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4195 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4196 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4197 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4198 | // We must execute the store. Storing a variable must keep the |
| 4199 | // (new) value on the stack. This is necessary for compiling |
| 4200 | // assignment expressions. |
| 4201 | // |
| 4202 | // Note: We will reach here even with slot->var()->mode() == |
| 4203 | // Variable::CONST because of const declarations which will |
| 4204 | // initialize consts to 'the hole' value and by doing so, end up |
| 4205 | // calling this code. r2 may be loaded with context; used below in |
| 4206 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4207 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4208 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4209 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4210 | if (slot->type() == Slot::CONTEXT) { |
| 4211 | // Skip write barrier if the written value is a smi. |
| 4212 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4213 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4214 | // r2 is loaded with context when calling SlotOperand above. |
| 4215 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4216 | __ mov(r3, Operand(offset)); |
| 4217 | __ RecordWrite(r2, r3, r1); |
| 4218 | } |
| 4219 | // If we definitely did not jump over the assignment, we do not need |
| 4220 | // to bind the exit label. Doing so can defeat peephole |
| 4221 | // optimization. |
| 4222 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4223 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4224 | } |
| 4225 | } |
| 4226 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4229 | case NAMED: { |
| 4230 | Comment cmnt(masm, "[ Store to named Property"); |
| 4231 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4232 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4233 | Handle<String> name(GetName()); |
| 4234 | |
| 4235 | Result value = cgen_->allocator()->Allocate(r0); |
| 4236 | ASSERT(value.is_valid()); |
| 4237 | frame->EmitPop(value.reg()); |
| 4238 | |
| 4239 | // Setup the name register. |
| 4240 | Result property_name = cgen_->allocator()->Allocate(r2); |
| 4241 | ASSERT(property_name.is_valid()); |
| 4242 | __ mov(property_name.reg(), Operand(name)); |
| 4243 | Result answer = frame->CallCodeObject(ic, |
| 4244 | RelocInfo::CODE_TARGET, |
| 4245 | &value, |
| 4246 | &property_name, |
| 4247 | 0); |
| 4248 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4249 | break; |
| 4250 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4251 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4252 | case KEYED: { |
| 4253 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4254 | Property* property = expression_->AsProperty(); |
| 4255 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4256 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4257 | |
| 4258 | // Call IC code. |
| 4259 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4260 | // TODO(1222589): Make the IC grab the values from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4261 | Result value = cgen_->allocator()->Allocate(r0); |
| 4262 | ASSERT(value.is_valid()); |
| 4263 | frame->EmitPop(value.reg()); // value |
| 4264 | Result result = |
| 4265 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4266 | frame->EmitPush(result.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4267 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4268 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4269 | |
| 4270 | default: |
| 4271 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4276 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4277 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4278 | // (31 instead of 32). |
| 4279 | static void CountLeadingZeros( |
| 4280 | MacroAssembler* masm, |
| 4281 | Register source, |
| 4282 | Register scratch, |
| 4283 | Register zeros) { |
| 4284 | #ifdef __ARM_ARCH_5__ |
| 4285 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4286 | #else |
| 4287 | __ mov(zeros, Operand(0)); |
| 4288 | __ mov(scratch, source); |
| 4289 | // Top 16. |
| 4290 | __ tst(scratch, Operand(0xffff0000)); |
| 4291 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4292 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4293 | // Top 8. |
| 4294 | __ tst(scratch, Operand(0xff000000)); |
| 4295 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4296 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4297 | // Top 4. |
| 4298 | __ tst(scratch, Operand(0xf0000000)); |
| 4299 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4300 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4301 | // Top 2. |
| 4302 | __ tst(scratch, Operand(0xc0000000)); |
| 4303 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4304 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4305 | // Top bit. |
| 4306 | __ tst(scratch, Operand(0x80000000)); |
| 4307 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4308 | #endif |
| 4309 | } |
| 4310 | |
| 4311 | |
| 4312 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4313 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4314 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4315 | // scratch register. Destroys the source register. No GC occurs during this |
| 4316 | // stub so you don't have to set up the frame. |
| 4317 | class ConvertToDoubleStub : public CodeStub { |
| 4318 | public: |
| 4319 | ConvertToDoubleStub(Register result_reg_1, |
| 4320 | Register result_reg_2, |
| 4321 | Register source_reg, |
| 4322 | Register scratch_reg) |
| 4323 | : result1_(result_reg_1), |
| 4324 | result2_(result_reg_2), |
| 4325 | source_(source_reg), |
| 4326 | zeros_(scratch_reg) { } |
| 4327 | |
| 4328 | private: |
| 4329 | Register result1_; |
| 4330 | Register result2_; |
| 4331 | Register source_; |
| 4332 | Register zeros_; |
| 4333 | |
| 4334 | // Minor key encoding in 16 bits. |
| 4335 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4336 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4337 | |
| 4338 | Major MajorKey() { return ConvertToDouble; } |
| 4339 | int MinorKey() { |
| 4340 | // Encode the parameters in a unique 16 bit value. |
| 4341 | return result1_.code() + |
| 4342 | (result2_.code() << 4) + |
| 4343 | (source_.code() << 8) + |
| 4344 | (zeros_.code() << 12); |
| 4345 | } |
| 4346 | |
| 4347 | void Generate(MacroAssembler* masm); |
| 4348 | |
| 4349 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4350 | |
| 4351 | #ifdef DEBUG |
| 4352 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4353 | #endif |
| 4354 | }; |
| 4355 | |
| 4356 | |
| 4357 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4358 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4359 | Register exponent = result1_; |
| 4360 | Register mantissa = result2_; |
| 4361 | #else |
| 4362 | Register exponent = result2_; |
| 4363 | Register mantissa = result1_; |
| 4364 | #endif |
| 4365 | Label not_special; |
| 4366 | // Convert from Smi to integer. |
| 4367 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4368 | // Move sign bit from source to destination. This works because the sign bit |
| 4369 | // in the exponent word of the double has the same position and polarity as |
| 4370 | // the 2's complement sign bit in a Smi. |
| 4371 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4372 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4373 | // Subtract from 0 if source was negative. |
| 4374 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4375 | __ cmp(source_, Operand(1)); |
| 4376 | __ b(gt, ¬_special); |
| 4377 | |
| 4378 | // We have -1, 0 or 1, which we treat specially. |
| 4379 | __ cmp(source_, Operand(0)); |
| 4380 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4381 | static const uint32_t exponent_word_for_1 = |
| 4382 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4383 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4384 | // 1, 0 and -1 all have 0 for the second word. |
| 4385 | __ mov(mantissa, Operand(0)); |
| 4386 | __ Ret(); |
| 4387 | |
| 4388 | __ bind(¬_special); |
| 4389 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4390 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4391 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4392 | // Compute exponent and or it into the exponent register. |
| 4393 | // We use result2 as a scratch register here. |
| 4394 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4395 | __ orr(exponent, |
| 4396 | exponent, |
| 4397 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4398 | // Shift up the source chopping the top bit off. |
| 4399 | __ add(zeros_, zeros_, Operand(1)); |
| 4400 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4401 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4402 | // Compute lower part of fraction (last 12 bits). |
| 4403 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4404 | // And the top (top 20 bits). |
| 4405 | __ orr(exponent, |
| 4406 | exponent, |
| 4407 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4408 | __ Ret(); |
| 4409 | } |
| 4410 | |
| 4411 | |
| 4412 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4413 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4414 | // so you don't have to set up the frame. |
| 4415 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4416 | public: |
| 4417 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4418 | Register the_heap_number, |
| 4419 | Register scratch) |
| 4420 | : the_int_(the_int), |
| 4421 | the_heap_number_(the_heap_number), |
| 4422 | scratch_(scratch) { } |
| 4423 | |
| 4424 | private: |
| 4425 | Register the_int_; |
| 4426 | Register the_heap_number_; |
| 4427 | Register scratch_; |
| 4428 | |
| 4429 | // Minor key encoding in 16 bits. |
| 4430 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4431 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4432 | |
| 4433 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4434 | int MinorKey() { |
| 4435 | // Encode the parameters in a unique 16 bit value. |
| 4436 | return the_int_.code() + |
| 4437 | (the_heap_number_.code() << 4) + |
| 4438 | (scratch_.code() << 8); |
| 4439 | } |
| 4440 | |
| 4441 | void Generate(MacroAssembler* masm); |
| 4442 | |
| 4443 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4444 | |
| 4445 | #ifdef DEBUG |
| 4446 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4447 | #endif |
| 4448 | }; |
| 4449 | |
| 4450 | |
| 4451 | // See comment for class. |
| 4452 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) { |
| 4453 | Label max_negative_int; |
| 4454 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4455 | // We test for the special value that has a different exponent. This test |
| 4456 | // has the neat side effect of setting the flags according to the sign. |
| 4457 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4458 | __ cmp(the_int_, Operand(0x80000000)); |
| 4459 | __ b(eq, &max_negative_int); |
| 4460 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4461 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4462 | uint32_t non_smi_exponent = |
| 4463 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4464 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4465 | // Set the sign bit in scratch_ if the value was negative. |
| 4466 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4467 | // Subtract from 0 if the value was negative. |
| 4468 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4469 | // We should be masking the implict first digit of the mantissa away here, |
| 4470 | // but it just ends up combining harmlessly with the last digit of the |
| 4471 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4472 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4473 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4474 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4475 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4476 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4477 | HeapNumber::kExponentOffset)); |
| 4478 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4479 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4480 | HeapNumber::kMantissaOffset)); |
| 4481 | __ Ret(); |
| 4482 | |
| 4483 | __ bind(&max_negative_int); |
| 4484 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4485 | // a double because it uses a sign bit instead of using two's complement. |
| 4486 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4487 | // significant 1 bit is not stored. |
| 4488 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4489 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4490 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4491 | __ mov(ip, Operand(0)); |
| 4492 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4493 | __ Ret(); |
| 4494 | } |
| 4495 | |
| 4496 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 4497 | // Handle the case where the lhs and rhs are the same object. |
| 4498 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4499 | // for "identity and not NaN". |
| 4500 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4501 | Label* slow, |
| 4502 | Condition cc) { |
| 4503 | Label not_identical; |
| 4504 | __ cmp(r0, Operand(r1)); |
| 4505 | __ b(ne, ¬_identical); |
| 4506 | |
| 4507 | Register exp_mask_reg = r5; |
| 4508 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4509 | |
| 4510 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4511 | // so we do the second best thing - test it ourselves. |
| 4512 | Label heap_number, return_equal; |
| 4513 | // They are both equal and they are not both Smis so both of them are not |
| 4514 | // Smis. If it's not a heap number, then return equal. |
| 4515 | if (cc == lt || cc == gt) { |
| 4516 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
| 4517 | __ b(ge, slow); |
| 4518 | } else { |
| 4519 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4520 | __ b(eq, &heap_number); |
| 4521 | // Comparing JS objects with <=, >= is complicated. |
| 4522 | if (cc != eq) { |
| 4523 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4524 | __ b(ge, slow); |
| 4525 | } |
| 4526 | } |
| 4527 | __ bind(&return_equal); |
| 4528 | if (cc == lt) { |
| 4529 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4530 | } else if (cc == gt) { |
| 4531 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4532 | } else { |
| 4533 | __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves. |
| 4534 | } |
| 4535 | __ mov(pc, Operand(lr)); // Return. |
| 4536 | |
| 4537 | // For less and greater we don't have to check for NaN since the result of |
| 4538 | // x < x is false regardless. For the others here is some code to check |
| 4539 | // for NaN. |
| 4540 | if (cc != lt && cc != gt) { |
| 4541 | __ bind(&heap_number); |
| 4542 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4543 | // not NaN. |
| 4544 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4545 | // and not all mantissa bits (0..51) clear. |
| 4546 | // Read top bits of double representation (second word of value). |
| 4547 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4548 | // Test that exponent bits are all set. |
| 4549 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4550 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4551 | __ b(ne, &return_equal); |
| 4552 | |
| 4553 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4554 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4555 | // Or with all low-bits of mantissa. |
| 4556 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4557 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4558 | // For equal we already have the right value in r0: Return zero (equal) |
| 4559 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if not |
| 4560 | // (it's a NaN). For <= and >= we need to load r0 with the failing value |
| 4561 | // if it's a NaN. |
| 4562 | if (cc != eq) { |
| 4563 | // All-zero means Infinity means equal. |
| 4564 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 4565 | if (cc == le) { |
| 4566 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 4567 | } else { |
| 4568 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 4569 | } |
| 4570 | } |
| 4571 | __ mov(pc, Operand(lr)); // Return. |
| 4572 | } |
| 4573 | // No fall through here. |
| 4574 | |
| 4575 | __ bind(¬_identical); |
| 4576 | } |
| 4577 | |
| 4578 | |
| 4579 | // See comment at call site. |
| 4580 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 4581 | Label* rhs_not_nan, |
| 4582 | Label* slow, |
| 4583 | bool strict) { |
| 4584 | Label lhs_is_smi; |
| 4585 | __ tst(r0, Operand(kSmiTagMask)); |
| 4586 | __ b(eq, &lhs_is_smi); |
| 4587 | |
| 4588 | // Rhs is a Smi. Check whether the non-smi is a heap number. |
| 4589 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4590 | if (strict) { |
| 4591 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4592 | // succeed. Return non-equal (r0 is already not zero) |
| 4593 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4594 | } else { |
| 4595 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4596 | // the runtime. |
| 4597 | __ b(ne, slow); |
| 4598 | } |
| 4599 | |
| 4600 | // Rhs is a smi, lhs is a number. |
| 4601 | __ push(lr); |
| 4602 | __ mov(r7, Operand(r1)); |
| 4603 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4604 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4605 | // r3 and r2 are rhs as double. |
| 4606 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4607 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4608 | // We now have both loaded as doubles but we can skip the lhs nan check |
| 4609 | // since it's a Smi. |
| 4610 | __ pop(lr); |
| 4611 | __ jmp(rhs_not_nan); |
| 4612 | |
| 4613 | __ bind(&lhs_is_smi); |
| 4614 | // Lhs is a Smi. Check whether the non-smi is a heap number. |
| 4615 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4616 | if (strict) { |
| 4617 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4618 | // succeed. Return non-equal. |
| 4619 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 4620 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4621 | } else { |
| 4622 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4623 | // the runtime. |
| 4624 | __ b(ne, slow); |
| 4625 | } |
| 4626 | |
| 4627 | // Lhs is a smi, rhs is a number. |
| 4628 | // r0 is Smi and r1 is heap number. |
| 4629 | __ push(lr); |
| 4630 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4631 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4632 | __ mov(r7, Operand(r0)); |
| 4633 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4634 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4635 | __ pop(lr); |
| 4636 | // Fall through to both_loaded_as_doubles. |
| 4637 | } |
| 4638 | |
| 4639 | |
| 4640 | void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) { |
| 4641 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4642 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4643 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4644 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4645 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4646 | Label one_is_nan, neither_is_nan; |
| 4647 | |
| 4648 | Register exp_mask_reg = r5; |
| 4649 | |
| 4650 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4651 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 4652 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4653 | __ b(ne, rhs_not_nan); |
| 4654 | __ mov(r4, |
| 4655 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4656 | SetCC); |
| 4657 | __ b(ne, &one_is_nan); |
| 4658 | __ cmp(rhs_mantissa, Operand(0)); |
| 4659 | __ b(ne, &one_is_nan); |
| 4660 | |
| 4661 | __ bind(rhs_not_nan); |
| 4662 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4663 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 4664 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4665 | __ b(ne, &neither_is_nan); |
| 4666 | __ mov(r4, |
| 4667 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4668 | SetCC); |
| 4669 | __ b(ne, &one_is_nan); |
| 4670 | __ cmp(lhs_mantissa, Operand(0)); |
| 4671 | __ b(eq, &neither_is_nan); |
| 4672 | |
| 4673 | __ bind(&one_is_nan); |
| 4674 | // NaN comparisons always fail. |
| 4675 | // Load whatever we need in r0 to make the comparison fail. |
| 4676 | if (cc == lt || cc == le) { |
| 4677 | __ mov(r0, Operand(GREATER)); |
| 4678 | } else { |
| 4679 | __ mov(r0, Operand(LESS)); |
| 4680 | } |
| 4681 | __ mov(pc, Operand(lr)); // Return. |
| 4682 | |
| 4683 | __ bind(&neither_is_nan); |
| 4684 | } |
| 4685 | |
| 4686 | |
| 4687 | // See comment at call site. |
| 4688 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 4689 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4690 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4691 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4692 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4693 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4694 | |
| 4695 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 4696 | if (cc == eq) { |
| 4697 | // Doubles are not equal unless they have the same bit pattern. |
| 4698 | // Exception: 0 and -0. |
| 4699 | __ cmp(lhs_mantissa, Operand(rhs_mantissa)); |
| 4700 | __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne); |
| 4701 | // Return non-zero if the numbers are unequal. |
| 4702 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 4703 | |
| 4704 | __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC); |
| 4705 | // If exponents are equal then return 0. |
| 4706 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 4707 | |
| 4708 | // Exponents are unequal. The only way we can return that the numbers |
| 4709 | // are equal is if one is -0 and the other is 0. We already dealt |
| 4710 | // with the case where both are -0 or both are 0. |
| 4711 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 4712 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 4713 | // equal. |
| 4714 | __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC); |
| 4715 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 4716 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 4717 | // Now they are equal if and only if the lhs exponent is zero in its |
| 4718 | // low 31 bits. |
| 4719 | __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize)); |
| 4720 | __ mov(pc, Operand(lr)); |
| 4721 | } else { |
| 4722 | // Call a native function to do a comparison between two non-NaNs. |
| 4723 | // Call C routine that may not cause GC or other trouble. |
| 4724 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 4725 | __ Jump(r5); // Tail call. |
| 4726 | } |
| 4727 | } |
| 4728 | |
| 4729 | |
| 4730 | // See comment at call site. |
| 4731 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 4732 | // If either operand is a JSObject or an oddball value, then they are |
| 4733 | // not equal since their pointers are different. |
| 4734 | // There is no test for undetectability in strict equality. |
| 4735 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 4736 | Label first_non_object; |
| 4737 | // Get the type of the first operand into r2 and compare it with |
| 4738 | // FIRST_JS_OBJECT_TYPE. |
| 4739 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 4740 | __ b(lt, &first_non_object); |
| 4741 | |
| 4742 | // Return non-zero (r0 is not zero) |
| 4743 | Label return_not_equal; |
| 4744 | __ bind(&return_not_equal); |
| 4745 | __ mov(pc, Operand(lr)); // Return. |
| 4746 | |
| 4747 | __ bind(&first_non_object); |
| 4748 | // Check for oddballs: true, false, null, undefined. |
| 4749 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 4750 | __ b(eq, &return_not_equal); |
| 4751 | |
| 4752 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 4753 | __ b(ge, &return_not_equal); |
| 4754 | |
| 4755 | // Check for oddballs: true, false, null, undefined. |
| 4756 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 4757 | __ b(eq, &return_not_equal); |
| 4758 | } |
| 4759 | |
| 4760 | |
| 4761 | // See comment at call site. |
| 4762 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 4763 | Label* both_loaded_as_doubles, |
| 4764 | Label* not_heap_numbers, |
| 4765 | Label* slow) { |
| 4766 | __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE); |
| 4767 | __ b(ne, not_heap_numbers); |
| 4768 | __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE); |
| 4769 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 4770 | |
| 4771 | // Both are heap numbers. Load them up then jump to the code we have |
| 4772 | // for that. |
| 4773 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4774 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4775 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4776 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4777 | __ jmp(both_loaded_as_doubles); |
| 4778 | } |
| 4779 | |
| 4780 | |
| 4781 | // Fast negative check for symbol-to-symbol equality. |
| 4782 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 4783 | // r2 is object type of r0. |
| 4784 | __ tst(r2, Operand(kIsNotStringMask)); |
| 4785 | __ b(ne, slow); |
| 4786 | __ tst(r2, Operand(kIsSymbolMask)); |
| 4787 | __ b(eq, slow); |
| 4788 | __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE); |
| 4789 | __ b(ge, slow); |
| 4790 | __ tst(r3, Operand(kIsSymbolMask)); |
| 4791 | __ b(eq, slow); |
| 4792 | |
| 4793 | // Both are symbols. We already checked they weren't the same pointer |
| 4794 | // so they are not equal. |
| 4795 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 4796 | __ mov(pc, Operand(lr)); // Return. |
| 4797 | } |
| 4798 | |
| 4799 | |
| 4800 | // On entry r0 and r1 are the things to be compared. On exit r0 is 0, |
| 4801 | // positive or negative to indicate the result of the comparison. |
| 4802 | void CompareStub::Generate(MacroAssembler* masm) { |
| 4803 | Label slow; // Call builtin. |
| 4804 | Label not_smis, both_loaded_as_doubles, rhs_not_nan; |
| 4805 | |
| 4806 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 4807 | // it is certain that at least one operand isn't a smi. |
| 4808 | |
| 4809 | // Handle the case where the objects are identical. Either returns the answer |
| 4810 | // or goes to slow. Only falls through if the objects were not identical. |
| 4811 | EmitIdenticalObjectComparison(masm, &slow, cc_); |
| 4812 | |
| 4813 | // If either is a Smi (we know that not both are), then they can only |
| 4814 | // be strictly equal if the other is a HeapNumber. |
| 4815 | ASSERT_EQ(0, kSmiTag); |
| 4816 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 4817 | __ and_(r2, r0, Operand(r1)); |
| 4818 | __ tst(r2, Operand(kSmiTagMask)); |
| 4819 | __ b(ne, ¬_smis); |
| 4820 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 4821 | // 1) Return the answer. |
| 4822 | // 2) Go to slow. |
| 4823 | // 3) Fall through to both_loaded_as_doubles. |
| 4824 | // 4) Jump to rhs_not_nan. |
| 4825 | // In cases 3 and 4 we have found out we were dealing with a number-number |
| 4826 | // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles. |
| 4827 | EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_); |
| 4828 | |
| 4829 | __ bind(&both_loaded_as_doubles); |
| 4830 | // r0, r1, r2, r3 are the double representations of the left hand side |
| 4831 | // and the right hand side. |
| 4832 | |
| 4833 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 4834 | // fall through if neither is a NaN. Also binds rhs_not_nan. |
| 4835 | EmitNanCheck(masm, &rhs_not_nan, cc_); |
| 4836 | |
| 4837 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 4838 | // answer. Never falls through. |
| 4839 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 4840 | |
| 4841 | __ bind(¬_smis); |
| 4842 | // At this point we know we are dealing with two different objects, |
| 4843 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 4844 | if (strict_) { |
| 4845 | // This returns non-equal for some object types, or falls through if it |
| 4846 | // was not lucky. |
| 4847 | EmitStrictTwoHeapObjectCompare(masm); |
| 4848 | } |
| 4849 | |
| 4850 | Label check_for_symbols; |
| 4851 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 4852 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 4853 | // that case. If the inputs are not doubles then jumps to check_for_symbols. |
| 4854 | // In this case r2 will contain the type of r0. |
| 4855 | EmitCheckForTwoHeapNumbers(masm, |
| 4856 | &both_loaded_as_doubles, |
| 4857 | &check_for_symbols, |
| 4858 | &slow); |
| 4859 | |
| 4860 | __ bind(&check_for_symbols); |
| 4861 | if (cc_ == eq) { |
| 4862 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 4863 | // of r0 on entry. |
| 4864 | EmitCheckForSymbols(masm, &slow); |
| 4865 | } |
| 4866 | |
| 4867 | __ bind(&slow); |
| 4868 | __ push(lr); |
| 4869 | __ push(r1); |
| 4870 | __ push(r0); |
| 4871 | // Figure out which native to call and setup the arguments. |
| 4872 | Builtins::JavaScript native; |
| 4873 | int arg_count = 1; // Not counting receiver. |
| 4874 | if (cc_ == eq) { |
| 4875 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 4876 | } else { |
| 4877 | native = Builtins::COMPARE; |
| 4878 | int ncr; // NaN compare result |
| 4879 | if (cc_ == lt || cc_ == le) { |
| 4880 | ncr = GREATER; |
| 4881 | } else { |
| 4882 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 4883 | ncr = LESS; |
| 4884 | } |
| 4885 | arg_count++; |
| 4886 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 4887 | __ push(r0); |
| 4888 | } |
| 4889 | |
| 4890 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 4891 | // tagged as a small integer. |
| 4892 | __ mov(r0, Operand(arg_count)); |
| 4893 | __ InvokeBuiltin(native, CALL_JS); |
| 4894 | __ cmp(r0, Operand(0)); |
| 4895 | __ pop(pc); |
| 4896 | } |
| 4897 | |
| 4898 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4899 | // Allocates a heap number or jumps to the label if the young space is full and |
| 4900 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4901 | static void AllocateHeapNumber( |
| 4902 | MacroAssembler* masm, |
| 4903 | Label* need_gc, // Jump here if young space is full. |
| 4904 | Register result_reg, // The tagged address of the new heap number. |
| 4905 | Register allocation_top_addr_reg, // A scratch register. |
| 4906 | Register scratch2) { // Another scratch register. |
| 4907 | ExternalReference allocation_top = |
| 4908 | ExternalReference::new_space_allocation_top_address(); |
| 4909 | ExternalReference allocation_limit = |
| 4910 | ExternalReference::new_space_allocation_limit_address(); |
| 4911 | |
| 4912 | // allocat := the address of the allocation top variable. |
| 4913 | __ mov(allocation_top_addr_reg, Operand(allocation_top)); |
| 4914 | // result_reg := the old allocation top. |
| 4915 | __ ldr(result_reg, MemOperand(allocation_top_addr_reg)); |
| 4916 | // scratch2 := the address of the allocation limit. |
| 4917 | __ mov(scratch2, Operand(allocation_limit)); |
| 4918 | // scratch2 := the allocation limit. |
| 4919 | __ ldr(scratch2, MemOperand(scratch2)); |
| 4920 | // result_reg := the new allocation top. |
| 4921 | __ add(result_reg, result_reg, Operand(HeapNumber::kSize)); |
| 4922 | // Compare new new allocation top and limit. |
| 4923 | __ cmp(result_reg, Operand(scratch2)); |
| 4924 | // Branch if out of space in young generation. |
| 4925 | __ b(hi, need_gc); |
| 4926 | // Store new allocation top. |
| 4927 | __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top |
| 4928 | // Tag and adjust back to start of new object. |
| 4929 | __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag)); |
| 4930 | // Get heap number map into scratch2. |
| 4931 | __ mov(scratch2, Operand(Factory::heap_number_map())); |
| 4932 | // Store heap number map in new object. |
| 4933 | __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset)); |
| 4934 | } |
| 4935 | |
| 4936 | |
| 4937 | // We fall into this code if the operands were Smis, but the result was |
| 4938 | // 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] | 4939 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 4940 | // to call the C-implemented binary fp operation routines we need to end up |
| 4941 | // with the double precision floating point operands in r0 and r1 (for the |
| 4942 | // 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] | 4943 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 4944 | Label* not_smi, |
| 4945 | const Builtins::JavaScript& builtin, |
| 4946 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4947 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4948 | Label slow, slow_pop_2_first, do_the_call; |
| 4949 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 4950 | // Smi-smi case (overflow). |
| 4951 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 4952 | // The new heap number is in r5. r6 and r7 are scratch. |
| 4953 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4954 | // 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^] | 4955 | __ mov(r7, Operand(r0)); |
| 4956 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4957 | __ push(lr); |
| 4958 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4959 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 4960 | __ mov(r7, Operand(r1)); |
| 4961 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4962 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4963 | __ pop(lr); |
| 4964 | __ jmp(&do_the_call); // Tail call. No return. |
| 4965 | |
| 4966 | // We jump to here if something goes wrong (one param is not a number of any |
| 4967 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4968 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4969 | __ push(r1); |
| 4970 | __ push(r0); |
| 4971 | __ mov(r0, Operand(1)); // Set number of arguments. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4972 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4973 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4974 | // 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] | 4975 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4976 | if (mode == NO_OVERWRITE) { |
| 4977 | // In the case where there is no chance of an overwritable float we may as |
| 4978 | // well do the allocation immediately while r0 and r1 are untouched. |
| 4979 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4980 | } |
| 4981 | |
| 4982 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4983 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4984 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 4985 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4986 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4987 | if (mode == OVERWRITE_RIGHT) { |
| 4988 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 4989 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4990 | // Calling convention says that second double is in r2 and r3. |
| 4991 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4992 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 4993 | __ jmp(&finished_loading_r0); |
| 4994 | __ bind(&r0_is_smi); |
| 4995 | if (mode == OVERWRITE_RIGHT) { |
| 4996 | // 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] | 4997 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4998 | } |
| 4999 | // Write Smi from r0 to r3 and r2 in double format. |
| 5000 | __ mov(r7, Operand(r0)); |
| 5001 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5002 | __ push(lr); |
| 5003 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5004 | __ pop(lr); |
| 5005 | __ bind(&finished_loading_r0); |
| 5006 | |
| 5007 | // Move r1 to a double in r0-r1. |
| 5008 | __ tst(r1, Operand(kSmiTagMask)); |
| 5009 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5010 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5011 | __ b(ne, &slow); |
| 5012 | if (mode == OVERWRITE_LEFT) { |
| 5013 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5014 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5015 | // Calling convention says that first double is in r0 and r1. |
| 5016 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5017 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5018 | __ jmp(&finished_loading_r1); |
| 5019 | __ bind(&r1_is_smi); |
| 5020 | if (mode == OVERWRITE_LEFT) { |
| 5021 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5022 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5023 | } |
| 5024 | // Write Smi from r1 to r1 and r0 in double format. |
| 5025 | __ mov(r7, Operand(r1)); |
| 5026 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5027 | __ push(lr); |
| 5028 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5029 | __ pop(lr); |
| 5030 | __ bind(&finished_loading_r1); |
| 5031 | |
| 5032 | __ bind(&do_the_call); |
| 5033 | // r0: Left value (least significant part of mantissa). |
| 5034 | // r1: Left value (sign, exponent, top of mantissa). |
| 5035 | // r2: Right value (least significant part of mantissa). |
| 5036 | // r3: Right value (sign, exponent, top of mantissa). |
| 5037 | // r5: Address of heap number for result. |
| 5038 | __ push(lr); // For later. |
| 5039 | __ push(r5); // Address of heap number that is answer. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5040 | // Call C routine that may not cause GC or other trouble. |
| 5041 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5042 | __ Call(r5); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5043 | // Store answer in the overwritable heap number. |
| 5044 | __ pop(r4); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5045 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5046 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5047 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5048 | // substract the tag from r4. |
| 5049 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5050 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5051 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5052 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5053 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5054 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5055 | #endif |
| 5056 | __ mov(r0, Operand(r4)); |
| 5057 | // And we are done. |
| 5058 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5062 | // 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] | 5063 | // 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] | 5064 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5065 | // 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] | 5066 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5067 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5068 | static void GetInt32(MacroAssembler* masm, |
| 5069 | Register source, |
| 5070 | Register dest, |
| 5071 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5072 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5073 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5074 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5075 | // Get exponent word. |
| 5076 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5077 | // Get exponent alone in scratch2. |
| 5078 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5079 | // Load dest with zero. We use this either for the final shift or |
| 5080 | // for the answer. |
| 5081 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5082 | // 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] | 5083 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5084 | // the exponent that we are fastest at and also the highest exponent we can |
| 5085 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5086 | const uint32_t non_smi_exponent = |
| 5087 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5088 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5089 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5090 | __ b(eq, &right_exponent); |
| 5091 | // If the exponent is higher than that then go to slow case. This catches |
| 5092 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5093 | __ b(gt, slow); |
| 5094 | |
| 5095 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5096 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5097 | // it rounds to zero. |
| 5098 | const uint32_t zero_exponent = |
| 5099 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5100 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5101 | // Dest already has a Smi zero. |
| 5102 | __ b(lt, &done); |
| 5103 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5104 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5105 | // We now have the exponent in dest. Subtract from 30 to get |
| 5106 | // how much to shift down. |
| 5107 | __ rsb(dest, dest, Operand(30)); |
| 5108 | |
| 5109 | __ bind(&right_exponent); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5110 | // Get the top bits of the mantissa. |
| 5111 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5112 | // Put back the implicit 1. |
| 5113 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5114 | // Shift up the mantissa bits to take up the space the exponent used to take. |
| 5115 | // We just orred in the implicit bit so that took care of one and we want to |
| 5116 | // leave the sign bit 0 so we subtract 2 bits from the shift distance. |
| 5117 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5118 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5119 | // Put sign in zero flag. |
| 5120 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5121 | // Get the second half of the double. For some exponents we don't actually |
| 5122 | // need this because the bits get shifted out again, but it's probably slower |
| 5123 | // to test than just to do it. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5124 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5125 | // Shift down 22 bits to get the last 10 bits. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5126 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5127 | // Move down according to the exponent. |
| 5128 | __ mov(dest, Operand(scratch, LSR, dest)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5129 | // Fix sign if sign bit was set. |
| 5130 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5131 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | |
| 5135 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5136 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5137 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5138 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5139 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5140 | // write the answer into (either by allocating or by overwriting). |
| 5141 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5142 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5143 | Label slow, result_not_a_smi; |
| 5144 | Label r0_is_smi, r1_is_smi; |
| 5145 | Label done_checking_r0, done_checking_r1; |
| 5146 | |
| 5147 | __ tst(r1, Operand(kSmiTagMask)); |
| 5148 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5149 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5150 | __ b(ne, &slow); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5151 | GetInt32(masm, r1, r3, r4, r5, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5152 | __ jmp(&done_checking_r1); |
| 5153 | __ bind(&r1_is_smi); |
| 5154 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5155 | __ bind(&done_checking_r1); |
| 5156 | |
| 5157 | __ tst(r0, Operand(kSmiTagMask)); |
| 5158 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5159 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5160 | __ b(ne, &slow); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5161 | GetInt32(masm, r0, r2, r4, r5, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5162 | __ jmp(&done_checking_r0); |
| 5163 | __ bind(&r0_is_smi); |
| 5164 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5165 | __ bind(&done_checking_r0); |
| 5166 | |
| 5167 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5168 | // r2 and r3: Signed int32 operands. |
| 5169 | switch (op_) { |
| 5170 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5171 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5172 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5173 | case Token::SAR: |
| 5174 | // Use only the 5 least significant bits of the shift count. |
| 5175 | __ and_(r2, r2, Operand(0x1f)); |
| 5176 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5177 | break; |
| 5178 | case Token::SHR: |
| 5179 | // Use only the 5 least significant bits of the shift count. |
| 5180 | __ and_(r2, r2, Operand(0x1f)); |
| 5181 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5182 | // SHR is special because it is required to produce a positive answer. |
| 5183 | // The code below for writing into heap numbers isn't capable of writing |
| 5184 | // the register as an unsigned int so we go to slow case if we hit this |
| 5185 | // case. |
| 5186 | __ b(mi, &slow); |
| 5187 | break; |
| 5188 | case Token::SHL: |
| 5189 | // Use only the 5 least significant bits of the shift count. |
| 5190 | __ and_(r2, r2, Operand(0x1f)); |
| 5191 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5192 | break; |
| 5193 | default: UNREACHABLE(); |
| 5194 | } |
| 5195 | // check that the *signed* result fits in a smi |
| 5196 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5197 | __ b(mi, &result_not_a_smi); |
| 5198 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5199 | __ Ret(); |
| 5200 | |
| 5201 | Label have_to_allocate, got_a_heap_number; |
| 5202 | __ bind(&result_not_a_smi); |
| 5203 | switch (mode_) { |
| 5204 | case OVERWRITE_RIGHT: { |
| 5205 | __ tst(r0, Operand(kSmiTagMask)); |
| 5206 | __ b(eq, &have_to_allocate); |
| 5207 | __ mov(r5, Operand(r0)); |
| 5208 | break; |
| 5209 | } |
| 5210 | case OVERWRITE_LEFT: { |
| 5211 | __ tst(r1, Operand(kSmiTagMask)); |
| 5212 | __ b(eq, &have_to_allocate); |
| 5213 | __ mov(r5, Operand(r1)); |
| 5214 | break; |
| 5215 | } |
| 5216 | case NO_OVERWRITE: { |
| 5217 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5218 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5219 | } |
| 5220 | default: break; |
| 5221 | } |
| 5222 | __ bind(&got_a_heap_number); |
| 5223 | // r2: Answer as signed int32. |
| 5224 | // r5: Heap number to write answer into. |
| 5225 | |
| 5226 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5227 | // result. |
| 5228 | __ mov(r0, Operand(r5)); |
| 5229 | |
| 5230 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5231 | // r3 as scratch. r0 is preserved and returned. |
| 5232 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5233 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5234 | |
| 5235 | if (mode_ != NO_OVERWRITE) { |
| 5236 | __ bind(&have_to_allocate); |
| 5237 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5238 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5239 | __ jmp(&got_a_heap_number); |
| 5240 | } |
| 5241 | |
| 5242 | // If all else failed then we go to the runtime system. |
| 5243 | __ bind(&slow); |
| 5244 | __ push(r1); // restore stack |
| 5245 | __ push(r0); |
| 5246 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 5247 | switch (op_) { |
| 5248 | case Token::BIT_OR: |
| 5249 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5250 | break; |
| 5251 | case Token::BIT_AND: |
| 5252 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5253 | break; |
| 5254 | case Token::BIT_XOR: |
| 5255 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5256 | break; |
| 5257 | case Token::SAR: |
| 5258 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5259 | break; |
| 5260 | case Token::SHR: |
| 5261 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5262 | break; |
| 5263 | case Token::SHL: |
| 5264 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5265 | break; |
| 5266 | default: |
| 5267 | UNREACHABLE(); |
| 5268 | } |
| 5269 | } |
| 5270 | |
| 5271 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5272 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5273 | // r1 : x |
| 5274 | // r0 : y |
| 5275 | // result : r0 |
| 5276 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5277 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 5278 | // tell us that. |
| 5279 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 5280 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5281 | switch (op_) { |
| 5282 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5283 | Label not_smi; |
| 5284 | // Fast path. |
| 5285 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5286 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5287 | __ b(ne, ¬_smi); |
| 5288 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 5289 | // Return if no overflow. |
| 5290 | __ Ret(vc); |
| 5291 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 5292 | |
| 5293 | HandleBinaryOpSlowCases(masm, |
| 5294 | ¬_smi, |
| 5295 | Builtins::ADD, |
| 5296 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5297 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5298 | break; |
| 5299 | } |
| 5300 | |
| 5301 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5302 | Label not_smi; |
| 5303 | // Fast path. |
| 5304 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5305 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5306 | __ b(ne, ¬_smi); |
| 5307 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 5308 | // Return if no overflow. |
| 5309 | __ Ret(vc); |
| 5310 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 5311 | |
| 5312 | HandleBinaryOpSlowCases(masm, |
| 5313 | ¬_smi, |
| 5314 | Builtins::SUB, |
| 5315 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5316 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5317 | break; |
| 5318 | } |
| 5319 | |
| 5320 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5321 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5322 | ASSERT(kSmiTag == 0); // adjust code below |
| 5323 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5324 | __ b(ne, ¬_smi); |
| 5325 | // 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] | 5326 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5327 | // Do multiplication |
| 5328 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 5329 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5330 | __ mov(ip, Operand(r3, ASR, 31)); |
| 5331 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 5332 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5333 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5334 | __ tst(r3, Operand(r3)); |
| 5335 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5336 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 5337 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 5338 | // We know one of them was zero. |
| 5339 | __ add(r2, r0, Operand(r1), SetCC); |
| 5340 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 5341 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 5342 | // Slow case. We fall through here if we multiplied a negative number |
| 5343 | // with 0, because that would mean we should produce -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5344 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5345 | |
| 5346 | HandleBinaryOpSlowCases(masm, |
| 5347 | ¬_smi, |
| 5348 | Builtins::MUL, |
| 5349 | Token::MUL, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5350 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5351 | break; |
| 5352 | } |
| 5353 | |
| 5354 | case Token::BIT_OR: |
| 5355 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5356 | case Token::BIT_XOR: |
| 5357 | case Token::SAR: |
| 5358 | case Token::SHR: |
| 5359 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5360 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5361 | ASSERT(kSmiTag == 0); // adjust code below |
| 5362 | __ tst(r2, Operand(kSmiTagMask)); |
| 5363 | __ b(ne, &slow); |
| 5364 | switch (op_) { |
| 5365 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 5366 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 5367 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5368 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5369 | // Remove tags from right operand. |
| 5370 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5371 | // Use only the 5 least significant bits of the shift count. |
| 5372 | __ and_(r2, r2, Operand(0x1f)); |
| 5373 | __ mov(r0, Operand(r1, ASR, r2)); |
| 5374 | // Smi tag result. |
| 5375 | __ and_(r0, r0, Operand(~kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5376 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5377 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5378 | // Remove tags from operands. We can't do this on a 31 bit number |
| 5379 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 5380 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5381 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5382 | // Use only the 5 least significant bits of the shift count. |
| 5383 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5384 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5385 | // Unsigned shift is not allowed to produce a negative number, so |
| 5386 | // check the sign bit and the sign bit after Smi tagging. |
| 5387 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5388 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5389 | // Smi tag result. |
| 5390 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5391 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5392 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5393 | // Remove tags from operands. |
| 5394 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5395 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5396 | // Use only the 5 least significant bits of the shift count. |
| 5397 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5398 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5399 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5400 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 5401 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5402 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5403 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5404 | default: UNREACHABLE(); |
| 5405 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5406 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5407 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5408 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5409 | break; |
| 5410 | } |
| 5411 | |
| 5412 | default: UNREACHABLE(); |
| 5413 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5414 | // This code should be unreachable. |
| 5415 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5416 | } |
| 5417 | |
| 5418 | |
| 5419 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 5420 | Label within_limit; |
| 5421 | __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit())); |
| 5422 | __ ldr(ip, MemOperand(ip)); |
| 5423 | __ cmp(sp, Operand(ip)); |
| 5424 | __ b(hs, &within_limit); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5425 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 5426 | // argument, so give it a Smi. |
| 5427 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5428 | __ push(r0); |
| 5429 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1); |
| 5430 | __ bind(&within_limit); |
| 5431 | |
| 5432 | __ StubReturn(1); |
| 5433 | } |
| 5434 | |
| 5435 | |
| 5436 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 5437 | Label undo; |
| 5438 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5439 | Label not_smi; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5440 | |
| 5441 | // Enter runtime system if the value is not a smi. |
| 5442 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5443 | __ b(ne, ¬_smi); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5444 | |
| 5445 | // Enter runtime system if the value of the expression is zero |
| 5446 | // to make sure that we switch between 0 and -0. |
| 5447 | __ cmp(r0, Operand(0)); |
| 5448 | __ b(eq, &slow); |
| 5449 | |
| 5450 | // The value of the expression is a smi that is not zero. Try |
| 5451 | // optimistic subtraction '0 - value'. |
| 5452 | __ rsb(r1, r0, Operand(0), SetCC); |
| 5453 | __ b(vs, &slow); |
| 5454 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5455 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 5456 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5457 | |
| 5458 | // Enter runtime system. |
| 5459 | __ bind(&slow); |
| 5460 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5461 | __ mov(r0, Operand(0)); // Set number of arguments. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5462 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 5463 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5464 | __ bind(¬_smi); |
| 5465 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 5466 | __ b(ne, &slow); |
| 5467 | // r0 is a heap number. Get a new heap number in r1. |
| 5468 | if (overwrite_) { |
| 5469 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5470 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5471 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5472 | } else { |
| 5473 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
| 5474 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 5475 | __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 5476 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5477 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5478 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 5479 | __ mov(r0, Operand(r1)); |
| 5480 | } |
| 5481 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5485 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5486 | // r0 holds the exception. |
| 5487 | |
| 5488 | // Adjust this code if not the case. |
| 5489 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5490 | |
| 5491 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5492 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 5493 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5494 | |
| 5495 | // Restore the next handler and frame pointer, discard handler state. |
| 5496 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5497 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5498 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5499 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5500 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 5501 | |
| 5502 | // Before returning we restore the context from the frame pointer if |
| 5503 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5504 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5505 | __ cmp(fp, Operand(0)); |
| 5506 | // Set cp to NULL if fp is NULL. |
| 5507 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5508 | // Restore cp otherwise. |
| 5509 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5510 | #ifdef DEBUG |
| 5511 | if (FLAG_debug_code) { |
| 5512 | __ mov(lr, Operand(pc)); |
| 5513 | } |
| 5514 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5515 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5516 | __ pop(pc); |
| 5517 | } |
| 5518 | |
| 5519 | |
| 5520 | void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5521 | // Adjust this code if not the case. |
| 5522 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5523 | |
| 5524 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5525 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5526 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5527 | |
| 5528 | // Unwind the handlers until the ENTRY handler is found. |
| 5529 | Label loop, done; |
| 5530 | __ bind(&loop); |
| 5531 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5532 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 5533 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5534 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 5535 | __ b(eq, &done); |
| 5536 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5537 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 5538 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5539 | __ jmp(&loop); |
| 5540 | __ bind(&done); |
| 5541 | |
| 5542 | // 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] | 5543 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5544 | __ pop(r0); |
| 5545 | __ str(r0, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5546 | |
| 5547 | // Set external caught exception to false. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5548 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5549 | __ mov(r0, Operand(false)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5550 | __ mov(r2, Operand(external_caught)); |
| 5551 | __ str(r0, MemOperand(r2)); |
| 5552 | |
| 5553 | // Set pending exception and r0 to out of memory exception. |
| 5554 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5555 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5556 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 5557 | __ str(r0, MemOperand(r2)); |
| 5558 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5559 | // Stack layout at this point. See also StackHandlerConstants. |
| 5560 | // sp -> state (ENTRY) |
| 5561 | // fp |
| 5562 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5563 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5564 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 5565 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5566 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 5567 | // Before returning we restore the context from the frame pointer if |
| 5568 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5569 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5570 | __ cmp(fp, Operand(0)); |
| 5571 | // Set cp to NULL if fp is NULL. |
| 5572 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5573 | // Restore cp otherwise. |
| 5574 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5575 | #ifdef DEBUG |
| 5576 | if (FLAG_debug_code) { |
| 5577 | __ mov(lr, Operand(pc)); |
| 5578 | } |
| 5579 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5580 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5581 | __ pop(pc); |
| 5582 | } |
| 5583 | |
| 5584 | |
| 5585 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 5586 | Label* throw_normal_exception, |
| 5587 | Label* throw_out_of_memory_exception, |
| 5588 | StackFrame::Type frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5589 | bool do_gc, |
| 5590 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5591 | // r0: result parameter for PerformGC, if any |
| 5592 | // r4: number of arguments including receiver (C callee-saved) |
| 5593 | // r5: pointer to builtin function (C callee-saved) |
| 5594 | // r6: pointer to the first argument (C callee-saved) |
| 5595 | |
| 5596 | if (do_gc) { |
| 5597 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5598 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 5599 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5602 | ExternalReference scope_depth = |
| 5603 | ExternalReference::heap_always_allocate_scope_depth(); |
| 5604 | if (always_allocate) { |
| 5605 | __ mov(r0, Operand(scope_depth)); |
| 5606 | __ ldr(r1, MemOperand(r0)); |
| 5607 | __ add(r1, r1, Operand(1)); |
| 5608 | __ str(r1, MemOperand(r0)); |
| 5609 | } |
| 5610 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5611 | // Call C built-in. |
| 5612 | // r0 = argc, r1 = argv |
| 5613 | __ mov(r0, Operand(r4)); |
| 5614 | __ mov(r1, Operand(r6)); |
| 5615 | |
| 5616 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 5617 | // frames, we need to know where the return address is. Right now, |
| 5618 | // we push it on the stack to be able to find it again, but we never |
| 5619 | // restore from it in case of changes, which makes it impossible to |
| 5620 | // support moving the C entry code stub. This should be fixed, but currently |
| 5621 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 5622 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 5623 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 5624 | masm->push(lr); |
| 5625 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5626 | |
| 5627 | if (always_allocate) { |
| 5628 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 5629 | // though (contain the result). |
| 5630 | __ mov(r2, Operand(scope_depth)); |
| 5631 | __ ldr(r3, MemOperand(r2)); |
| 5632 | __ sub(r3, r3, Operand(1)); |
| 5633 | __ str(r3, MemOperand(r2)); |
| 5634 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5635 | |
| 5636 | // check for failure result |
| 5637 | Label failure_returned; |
| 5638 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 5639 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 5640 | __ add(r2, r0, Operand(1)); |
| 5641 | __ tst(r2, Operand(kFailureTagMask)); |
| 5642 | __ b(eq, &failure_returned); |
| 5643 | |
| 5644 | // Exit C frame and return. |
| 5645 | // r0:r1: result |
| 5646 | // sp: stack pointer |
| 5647 | // fp: frame pointer |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5648 | __ LeaveExitFrame(frame_type); |
| 5649 | |
| 5650 | // check if we should retry or throw exception |
| 5651 | Label retry; |
| 5652 | __ bind(&failure_returned); |
| 5653 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 5654 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 5655 | __ b(eq, &retry); |
| 5656 | |
| 5657 | Label continue_exception; |
| 5658 | // If the returned failure is EXCEPTION then promote Top::pending_exception(). |
| 5659 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 5660 | __ b(ne, &continue_exception); |
| 5661 | |
| 5662 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5663 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5664 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5665 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5666 | __ ldr(r0, MemOperand(ip)); |
| 5667 | __ str(r3, MemOperand(ip)); |
| 5668 | |
| 5669 | __ bind(&continue_exception); |
| 5670 | // Special handling of out of memory exception. |
| 5671 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5672 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5673 | __ b(eq, throw_out_of_memory_exception); |
| 5674 | |
| 5675 | // Handle normal exception. |
| 5676 | __ jmp(throw_normal_exception); |
| 5677 | |
| 5678 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 5679 | } |
| 5680 | |
| 5681 | |
| 5682 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 5683 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 5684 | // r0: number of arguments including receiver |
| 5685 | // r1: pointer to builtin function |
| 5686 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5687 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5688 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5689 | |
| 5690 | // NOTE: Invocations of builtins may return failure objects |
| 5691 | // instead of a proper result. The builtin entry handles |
| 5692 | // this by performing a garbage collection and retrying the |
| 5693 | // builtin once. |
| 5694 | |
| 5695 | StackFrame::Type frame_type = is_debug_break |
| 5696 | ? StackFrame::EXIT_DEBUG |
| 5697 | : StackFrame::EXIT; |
| 5698 | |
| 5699 | // Enter the exit frame that transitions from JavaScript to C++. |
| 5700 | __ EnterExitFrame(frame_type); |
| 5701 | |
| 5702 | // r4: number of arguments (C callee-saved) |
| 5703 | // r5: pointer to builtin function (C callee-saved) |
| 5704 | // r6: pointer to first argument (C callee-saved) |
| 5705 | |
| 5706 | Label throw_out_of_memory_exception; |
| 5707 | Label throw_normal_exception; |
| 5708 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5709 | // Call into the runtime system. Collect garbage before the call if |
| 5710 | // running with --gc-greedy set. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5711 | if (FLAG_gc_greedy) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5712 | Failure* failure = Failure::RetryAfterGC(0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5713 | __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure))); |
| 5714 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5715 | GenerateCore(masm, &throw_normal_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5716 | &throw_out_of_memory_exception, |
| 5717 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5718 | FLAG_gc_greedy, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5719 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5720 | |
| 5721 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5722 | GenerateCore(masm, |
| 5723 | &throw_normal_exception, |
| 5724 | &throw_out_of_memory_exception, |
| 5725 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5726 | true, |
| 5727 | false); |
| 5728 | |
| 5729 | // Do full GC and retry runtime call one final time. |
| 5730 | Failure* failure = Failure::InternalError(); |
| 5731 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 5732 | GenerateCore(masm, |
| 5733 | &throw_normal_exception, |
| 5734 | &throw_out_of_memory_exception, |
| 5735 | frame_type, |
| 5736 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5737 | true); |
| 5738 | |
| 5739 | __ bind(&throw_out_of_memory_exception); |
| 5740 | GenerateThrowOutOfMemory(masm); |
| 5741 | // control flow for generated will not return. |
| 5742 | |
| 5743 | __ bind(&throw_normal_exception); |
| 5744 | GenerateThrowTOS(masm); |
| 5745 | } |
| 5746 | |
| 5747 | |
| 5748 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 5749 | // r0: code entry |
| 5750 | // r1: function |
| 5751 | // r2: receiver |
| 5752 | // r3: argc |
| 5753 | // [sp+0]: argv |
| 5754 | |
| 5755 | Label invoke, exit; |
| 5756 | |
| 5757 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 5758 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5759 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5760 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 5761 | |
| 5762 | // Get address of argv, see stm above. |
| 5763 | // r0: code entry |
| 5764 | // r1: function |
| 5765 | // r2: receiver |
| 5766 | // r3: argc |
| 5767 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 5768 | __ ldr(r4, MemOperand(r4)); // argv |
| 5769 | |
| 5770 | // Push a frame with special values setup to mark it as an entry frame. |
| 5771 | // r0: code entry |
| 5772 | // r1: function |
| 5773 | // r2: receiver |
| 5774 | // r3: argc |
| 5775 | // r4: argv |
| 5776 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 5777 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 5778 | __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL)); |
| 5779 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 5780 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5781 | __ ldr(r5, MemOperand(r5)); |
| 5782 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 5783 | |
| 5784 | // Setup frame pointer for the frame to be pushed. |
| 5785 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5786 | |
| 5787 | // Call a faked try-block that does the invoke. |
| 5788 | __ bl(&invoke); |
| 5789 | |
| 5790 | // Caught exception: Store result (exception) in the pending |
| 5791 | // exception field in the JSEnv and return a failure sentinel. |
| 5792 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 5793 | // 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] | 5794 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5795 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5796 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5797 | __ b(&exit); |
| 5798 | |
| 5799 | // Invoke: Link this frame into the handler chain. |
| 5800 | __ bind(&invoke); |
| 5801 | // Must preserve r0-r4, r5-r7 are available. |
| 5802 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5803 | // If an exception not caught by another handler occurs, this handler |
| 5804 | // returns control to the code after the bl(&invoke) above, which |
| 5805 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 5806 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5807 | |
| 5808 | // Clear any pending exceptions. |
| 5809 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 5810 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5811 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5812 | __ str(r5, MemOperand(ip)); |
| 5813 | |
| 5814 | // Invoke the function by calling through JS entry trampoline builtin. |
| 5815 | // Notice that we cannot store a reference to the trampoline code directly in |
| 5816 | // this stub, because runtime stubs are not traversed when doing GC. |
| 5817 | |
| 5818 | // Expected registers by Builtins::JSEntryTrampoline |
| 5819 | // r0: code entry |
| 5820 | // r1: function |
| 5821 | // r2: receiver |
| 5822 | // r3: argc |
| 5823 | // r4: argv |
| 5824 | if (is_construct) { |
| 5825 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 5826 | __ mov(ip, Operand(construct_entry)); |
| 5827 | } else { |
| 5828 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 5829 | __ mov(ip, Operand(entry)); |
| 5830 | } |
| 5831 | __ ldr(ip, MemOperand(ip)); // deref address |
| 5832 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5833 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 5834 | // macro for the add instruction because we don't want the coverage tool |
| 5835 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5836 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5837 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5838 | |
| 5839 | // Unlink this frame from the handler chain. When reading the |
| 5840 | // address of the next handler, there is no need to use the address |
| 5841 | // displacement since the current stack pointer (sp) points directly |
| 5842 | // to the stack handler. |
| 5843 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 5844 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 5845 | __ str(r3, MemOperand(ip)); |
| 5846 | // No need to restore registers |
| 5847 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 5848 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5849 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5850 | __ bind(&exit); // r0 holds result |
| 5851 | // Restore the top frame descriptors from the stack. |
| 5852 | __ pop(r3); |
| 5853 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5854 | __ str(r3, MemOperand(ip)); |
| 5855 | |
| 5856 | // Reset the stack to the callee saved registers. |
| 5857 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5858 | |
| 5859 | // Restore callee-saved registers and return. |
| 5860 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5861 | if (FLAG_debug_code) { |
| 5862 | __ mov(lr, Operand(pc)); |
| 5863 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5864 | #endif |
| 5865 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 5866 | } |
| 5867 | |
| 5868 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5869 | // This stub performs an instanceof, calling the builtin function if |
| 5870 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 5871 | // be an instance of (these are fetched from the stack). |
| 5872 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 5873 | // Get the object - slow case for smis (we may need to throw an exception |
| 5874 | // depending on the rhs). |
| 5875 | Label slow, loop, is_instance, is_not_instance; |
| 5876 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 5877 | __ BranchOnSmi(r0, &slow); |
| 5878 | |
| 5879 | // Check that the left hand is a JS object and put map in r3. |
| 5880 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 5881 | __ b(lt, &slow); |
| 5882 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 5883 | __ b(gt, &slow); |
| 5884 | |
| 5885 | // Get the prototype of the function (r4 is result, r2 is scratch). |
| 5886 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 5887 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 5888 | |
| 5889 | // Check that the function prototype is a JS object. |
| 5890 | __ BranchOnSmi(r4, &slow); |
| 5891 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 5892 | __ b(lt, &slow); |
| 5893 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 5894 | __ b(gt, &slow); |
| 5895 | |
| 5896 | // Register mapping: r3 is object map and r4 is function prototype. |
| 5897 | // Get prototype of object into r2. |
| 5898 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 5899 | |
| 5900 | // Loop through the prototype chain looking for the function prototype. |
| 5901 | __ bind(&loop); |
| 5902 | __ cmp(r2, Operand(r4)); |
| 5903 | __ b(eq, &is_instance); |
| 5904 | __ cmp(r2, Operand(Factory::null_value())); |
| 5905 | __ b(eq, &is_not_instance); |
| 5906 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 5907 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 5908 | __ jmp(&loop); |
| 5909 | |
| 5910 | __ bind(&is_instance); |
| 5911 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 5912 | __ pop(); |
| 5913 | __ pop(); |
| 5914 | __ mov(pc, Operand(lr)); // Return. |
| 5915 | |
| 5916 | __ bind(&is_not_instance); |
| 5917 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 5918 | __ pop(); |
| 5919 | __ pop(); |
| 5920 | __ mov(pc, Operand(lr)); // Return. |
| 5921 | |
| 5922 | // Slow-case. Tail call builtin. |
| 5923 | __ bind(&slow); |
| 5924 | __ mov(r0, Operand(1)); // Arg count without receiver. |
| 5925 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 5926 | } |
| 5927 | |
| 5928 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5929 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5930 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5931 | Label adaptor; |
| 5932 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5933 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5934 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5935 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5936 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5937 | // Nothing to do: The formal number of parameters has already been |
| 5938 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5939 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5940 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5941 | // Arguments adaptor case: Read the arguments length from the |
| 5942 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5943 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5944 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5945 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
| 5948 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5949 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 5950 | // The displacement is the offset of the last parameter (if any) |
| 5951 | // relative to the frame pointer. |
| 5952 | static const int kDisplacement = |
| 5953 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5954 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5955 | // Check that the key is a smi. |
| 5956 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5957 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5958 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5959 | // Check if the calling frame is an arguments adaptor frame. |
| 5960 | Label adaptor; |
| 5961 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5962 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5963 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5964 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5965 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5966 | // Check index against formal parameters count limit passed in |
| 5967 | // through register eax. Use unsigned comparison to get negative |
| 5968 | // check for free. |
| 5969 | __ cmp(r1, r0); |
| 5970 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5971 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5972 | // Read the argument from the stack and return it. |
| 5973 | __ sub(r3, r0, r1); |
| 5974 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5975 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5976 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5977 | |
| 5978 | // Arguments adaptor case: Check index against actual arguments |
| 5979 | // limit found in the arguments adaptor frame. Use unsigned |
| 5980 | // comparison to get negative check for free. |
| 5981 | __ bind(&adaptor); |
| 5982 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5983 | __ cmp(r1, r0); |
| 5984 | __ b(cs, &slow); |
| 5985 | |
| 5986 | // Read the argument from the adaptor frame and return it. |
| 5987 | __ sub(r3, r0, r1); |
| 5988 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5989 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5990 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5991 | |
| 5992 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 5993 | // by calling the runtime system. |
| 5994 | __ bind(&slow); |
| 5995 | __ push(r1); |
| 5996 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1); |
| 5997 | } |
| 5998 | |
| 5999 | |
| 6000 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 6001 | // Check if the calling frame is an arguments adaptor frame. |
| 6002 | Label runtime; |
| 6003 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6004 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6005 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 6006 | __ b(ne, &runtime); |
| 6007 | |
| 6008 | // Patch the arguments.length and the parameters pointer. |
| 6009 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6010 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 6011 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6012 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6013 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6014 | |
| 6015 | // Do the runtime call to allocate the arguments object. |
| 6016 | __ bind(&runtime); |
| 6017 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6018 | } |
| 6019 | |
| 6020 | |
| 6021 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6022 | Label slow; |
| 6023 | // Get the function to call from the stack. |
| 6024 | // function, receiver [, arguments] |
| 6025 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6026 | |
| 6027 | // Check that the function is really a JavaScript function. |
| 6028 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6029 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6030 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6031 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6032 | __ b(ne, &slow); |
| 6033 | |
| 6034 | // Fast-case: Invoke the function now. |
| 6035 | // r1: pushed function |
| 6036 | ParameterCount actual(argc_); |
| 6037 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6038 | |
| 6039 | // Slow-case: Non-function called. |
| 6040 | __ bind(&slow); |
| 6041 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6042 | __ mov(r2, Operand(0)); |
| 6043 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6044 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6045 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6046 | } |
| 6047 | |
| 6048 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 6049 | int CompareStub::MinorKey() { |
| 6050 | // Encode the two parameters in a unique 16 bit value. |
| 6051 | ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15)); |
| 6052 | return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0); |
| 6053 | } |
| 6054 | |
| 6055 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6056 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6057 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6058 | } } // namespace v8::internal |