ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
| 32 | #include "debug.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 33 | #include "parser.h" |
| 34 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 36 | #include "scopes.h" |
| 37 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 42 | #define __ ACCESS_MASM(masm_) |
| 43 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 44 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 45 | Label* slow, |
| 46 | Condition cc); |
| 47 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 48 | Label* rhs_not_nan, |
| 49 | Label* slow, |
| 50 | bool strict); |
| 51 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
| 52 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 53 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 54 | Register source, |
| 55 | Register destination, |
| 56 | int known_int); |
| 57 | static bool IsEasyToMultiplyBy(int x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 61 | // ------------------------------------------------------------------------- |
| 62 | // Platform-specific DeferredCode functions. |
| 63 | |
| 64 | void DeferredCode::SaveRegisters() { |
| 65 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 66 | int action = registers_[i]; |
| 67 | if (action == kPush) { |
| 68 | __ push(RegisterAllocator::ToRegister(i)); |
| 69 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 70 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | void DeferredCode::RestoreRegisters() { |
| 77 | // Restore registers in reverse order due to the stack. |
| 78 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 79 | int action = registers_[i]; |
| 80 | if (action == kPush) { |
| 81 | __ pop(RegisterAllocator::ToRegister(i)); |
| 82 | } else if (action != kIgnore) { |
| 83 | action &= ~kSyncedFlag; |
| 84 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 89 | |
| 90 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 91 | // CodeGenState implementation. |
| 92 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 93 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 94 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 95 | typeof_state_(NOT_INSIDE_TYPEOF), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 96 | true_target_(NULL), |
| 97 | false_target_(NULL), |
| 98 | previous_(NULL) { |
| 99 | owner_->set_state(this); |
| 100 | } |
| 101 | |
| 102 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 103 | CodeGenState::CodeGenState(CodeGenerator* owner, |
| 104 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 105 | JumpTarget* true_target, |
| 106 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 107 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 108 | typeof_state_(typeof_state), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 109 | true_target_(true_target), |
| 110 | false_target_(false_target), |
| 111 | previous_(owner->state()) { |
| 112 | owner_->set_state(this); |
| 113 | } |
| 114 | |
| 115 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 116 | CodeGenState::~CodeGenState() { |
| 117 | ASSERT(owner_->state() == this); |
| 118 | owner_->set_state(previous_); |
| 119 | } |
| 120 | |
| 121 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 122 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 123 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 125 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 126 | bool is_eval) |
| 127 | : is_eval_(is_eval), |
| 128 | script_(script), |
| 129 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 131 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 132 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 133 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | cc_reg_(al), |
| 135 | state_(NULL), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 136 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 141 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 143 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | // cp: callee's context |
| 145 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 146 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 147 | ZoneList<Statement*>* body = fun->body(); |
| 148 | |
| 149 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 150 | ASSERT(scope_ == NULL); |
| 151 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 152 | ASSERT(allocator_ == NULL); |
| 153 | RegisterAllocator register_allocator(this); |
| 154 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 155 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 156 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 157 | cc_reg_ = al; |
| 158 | { |
| 159 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 160 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 161 | // Entry: |
| 162 | // Stack: receiver, arguments |
| 163 | // lr: return address |
| 164 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 166 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 168 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 169 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | // tos: code slot |
| 171 | #ifdef DEBUG |
| 172 | if (strlen(FLAG_stop_at) > 0 && |
| 173 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 174 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 175 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 176 | } |
| 177 | #endif |
| 178 | |
| 179 | // Allocate space for locals and initialize them. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 180 | frame_->AllocateStackSlots(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 181 | // Initialize the function return target after the locals are set |
| 182 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 183 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 184 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 185 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 186 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 187 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | // Allocate local context. |
| 189 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 190 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 191 | frame_->EmitPush(r0); |
| 192 | frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 193 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 194 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 195 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 196 | __ cmp(r0, Operand(cp)); |
| 197 | verified_true.Branch(eq); |
| 198 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 199 | verified_true.Bind(); |
| 200 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 201 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 202 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | } |
| 204 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 205 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 206 | // 1) only needed if we have a context |
| 207 | // 2) no need to recompute context ptr every single time |
| 208 | // 3) don't copy parameter operand code from SlotOperand! |
| 209 | { |
| 210 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 211 | |
| 212 | // Note that iteration order is relevant here! If we have the same |
| 213 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 214 | // needs to be copied into the context, it must be the last argument |
| 215 | // passed to the parameter that needs to be copied. This is a rare |
| 216 | // case so we don't check for it, instead we rely on the copying |
| 217 | // order: such a parameter is copied repeatedly into the same |
| 218 | // context location and thus the last value is what is seen inside |
| 219 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 220 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 221 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | Slot* slot = par->slot(); |
| 223 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 224 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 225 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | // Loads r2 with context; used below in RecordWrite. |
| 227 | __ str(r1, SlotOperand(slot, r2)); |
| 228 | // Load the offset into r3. |
| 229 | int slot_offset = |
| 230 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 231 | __ mov(r3, Operand(slot_offset)); |
| 232 | __ RecordWrite(r2, r3, r1); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 237 | // Store the arguments object. This must happen after context |
| 238 | // initialization because the arguments object may be stored in the |
| 239 | // context. |
| 240 | if (scope_->arguments() != NULL) { |
| 241 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 243 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 244 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 245 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 246 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 247 | // The receiver is below the arguments, the return address, |
| 248 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 249 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 250 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 251 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 252 | frame_->Adjust(3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 253 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 254 | frame_->CallStub(&stub, 3); |
| 255 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 256 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 257 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 258 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 259 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 260 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 261 | } |
| 262 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 263 | // Generate code to 'execute' declarations and initialize functions |
| 264 | // (source elements). In case of an illegal redeclaration we need to |
| 265 | // handle that instead of processing the declarations. |
| 266 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 267 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 268 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | } else { |
| 270 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 271 | ProcessDeclarations(scope_->declarations()); |
| 272 | // Bail out if a stack-overflow exception occurred when processing |
| 273 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 274 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 275 | } |
| 276 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 277 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 278 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 279 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 280 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | CheckStack(); |
| 282 | |
| 283 | // Compile the body of the function in a vanilla state. Don't |
| 284 | // bother compiling all the code if the scope has an illegal |
| 285 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 286 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | Comment cmnt(masm_, "[ function body"); |
| 288 | #ifdef DEBUG |
| 289 | bool is_builtin = Bootstrapper::IsActive(); |
| 290 | bool should_trace = |
| 291 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 292 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 293 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 294 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 295 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 297 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | } |
| 300 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 301 | // Generate the return sequence if necessary. |
| 302 | if (frame_ != NULL || function_return_.is_linked()) { |
| 303 | // exit |
| 304 | // r0: result |
| 305 | // sp: stack pointer |
| 306 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 307 | // cp: callee's context |
| 308 | __ mov(r0, Operand(Factory::undefined_value())); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 309 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 310 | function_return_.Bind(); |
| 311 | if (FLAG_trace) { |
| 312 | // Push the return value on the stack as the parameter. |
| 313 | // Runtime::TraceExit returns the parameter as it is. |
| 314 | frame_->EmitPush(r0); |
| 315 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 316 | } |
| 317 | |
| 318 | // Tear down the frame which will restore the caller's frame pointer and |
| 319 | // the link register. |
| 320 | frame_->Exit(); |
| 321 | |
| 322 | __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 323 | __ Jump(lr); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 324 | } |
| 325 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | ASSERT(!has_cc()); |
| 328 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 329 | ASSERT(!function_return_is_shadowed_); |
| 330 | function_return_.Unuse(); |
| 331 | DeleteFrame(); |
| 332 | |
| 333 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 334 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 335 | ProcessDeferred(); |
| 336 | } |
| 337 | |
| 338 | allocator_ = NULL; |
| 339 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 343 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 344 | // Currently, this assertion will fail if we try to assign to |
| 345 | // a constant variable that is constant because it is read-only |
| 346 | // (such as the variable referring to a named function expression). |
| 347 | // We need to implement assignments to read-only variables. |
| 348 | // Ideally, we should do this during AST generation (by converting |
| 349 | // such assignments into expression statements); however, in general |
| 350 | // we may not be able to make the decision until past AST generation, |
| 351 | // that is when the entire program is known. |
| 352 | ASSERT(slot != NULL); |
| 353 | int index = slot->index(); |
| 354 | switch (slot->type()) { |
| 355 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 356 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 357 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 358 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 359 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 360 | |
| 361 | case Slot::CONTEXT: { |
| 362 | // Follow the context chain if necessary. |
| 363 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 364 | Register context = cp; |
| 365 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 366 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 367 | // Load the closure. |
| 368 | // (All contexts, even 'with' contexts, have a closure, |
| 369 | // and it is the same for all contexts inside a function. |
| 370 | // There is no need to go to the function context first.) |
| 371 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 372 | // Load the function context (which is the incoming, outer context). |
| 373 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 374 | context = tmp; |
| 375 | } |
| 376 | // We may have a 'with' context now. Get the function context. |
| 377 | // (In fact this mov may never be the needed, since the scope analysis |
| 378 | // may not permit a direct context access in this case and thus we are |
| 379 | // always at a function context. However it is safe to dereference be- |
| 380 | // cause the function context of a function context is itself. Before |
| 381 | // deleting this mov we should try to create a counter-example first, |
| 382 | // though...) |
| 383 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 384 | return ContextOperand(tmp, index); |
| 385 | } |
| 386 | |
| 387 | default: |
| 388 | UNREACHABLE(); |
| 389 | return MemOperand(r0, 0); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 394 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 395 | Slot* slot, |
| 396 | Register tmp, |
| 397 | Register tmp2, |
| 398 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 399 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 400 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 401 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 402 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 403 | if (s->num_heap_slots() > 0) { |
| 404 | if (s->calls_eval()) { |
| 405 | // Check that extension is NULL. |
| 406 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 407 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 408 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 409 | } |
| 410 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 411 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 412 | context = tmp; |
| 413 | } |
| 414 | } |
| 415 | // Check that last extension is NULL. |
| 416 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 417 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 418 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 419 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 420 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 424 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 425 | // (partially) translated into branches, or it may have set the condition |
| 426 | // code register. If force_cc is set, the value is forced to set the |
| 427 | // condition code register and no value is pushed. If the condition code |
| 428 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 429 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 430 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 431 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 432 | JumpTarget* true_target, |
| 433 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 434 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 435 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 436 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 437 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 438 | { CodeGenState new_state(this, typeof_state, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 439 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 440 | |
| 441 | // If we hit a stack overflow, we may not have actually visited |
| 442 | // the expression. In that case, we ensure that we have a |
| 443 | // valid-looking frame state because we will continue to generate |
| 444 | // code as we unwind the C++ stack. |
| 445 | // |
| 446 | // It's possible to have both a stack overflow and a valid frame |
| 447 | // state (eg, a subexpression overflowed, visiting it returned |
| 448 | // with a dummied frame state, and visiting this expression |
| 449 | // returned with a normal-looking state). |
| 450 | if (HasStackOverflow() && |
| 451 | has_valid_frame() && |
| 452 | !has_cc() && |
| 453 | frame_->height() == original_height) { |
| 454 | true_target->Jump(); |
| 455 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 456 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 457 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 458 | // Convert the TOS value to a boolean in the condition code register. |
| 459 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 460 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 461 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 462 | ASSERT(!has_valid_frame() || |
| 463 | (has_cc() && frame_->height() == original_height) || |
| 464 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 468 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 469 | #ifdef DEBUG |
| 470 | int original_height = frame_->height(); |
| 471 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 472 | JumpTarget true_target; |
| 473 | JumpTarget false_target; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 474 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 475 | |
| 476 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 477 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 478 | JumpTarget loaded; |
| 479 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 480 | materialize_true.Branch(cc_reg_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 481 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 482 | frame_->EmitPush(r0); |
| 483 | loaded.Jump(); |
| 484 | materialize_true.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 485 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 486 | frame_->EmitPush(r0); |
| 487 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 488 | cc_reg_ = al; |
| 489 | } |
| 490 | |
| 491 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 492 | // We have at least one condition value that has been "translated" |
| 493 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 494 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 495 | if (frame_ != NULL) { |
| 496 | loaded.Jump(); // Don't lose the current TOS. |
| 497 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 498 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 499 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 500 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 501 | true_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 502 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 503 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 505 | // If both "true" and "false" need to be loaded jump across the code for |
| 506 | // "false". |
| 507 | if (both) { |
| 508 | loaded.Jump(); |
| 509 | } |
| 510 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 511 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 512 | false_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 513 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 514 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 515 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 516 | // A value is loaded on all paths reaching this point. |
| 517 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 519 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 520 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 521 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 525 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 526 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 527 | __ ldr(r0, GlobalObject()); |
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 | } |
| 530 | |
| 531 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 532 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 533 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 534 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 535 | __ ldr(scratch, |
| 536 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 537 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 541 | // 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] | 542 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 543 | // variables w/o reference errors elsewhere. |
| 544 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 545 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 546 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 547 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 548 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 549 | // the variable as if through '<global>.<variable>' to make sure we |
| 550 | // do not get reference errors. |
| 551 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 552 | Literal key(variable->name()); |
| 553 | // TODO(1241834): Fetch the position from the variable instead of using |
| 554 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 555 | Property property(&global, &key, RelocInfo::kNoPosition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 556 | LoadAndSpill(&property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 557 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 558 | LoadAndSpill(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
| 562 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 563 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 564 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 565 | cgen->LoadReference(this); |
| 566 | } |
| 567 | |
| 568 | |
| 569 | Reference::~Reference() { |
| 570 | cgen_->UnloadReference(this); |
| 571 | } |
| 572 | |
| 573 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 574 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 575 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 576 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 577 | Expression* e = ref->expression(); |
| 578 | Property* property = e->AsProperty(); |
| 579 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 580 | |
| 581 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 582 | // The expression is either a property or a variable proxy that rewrites |
| 583 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 584 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 585 | // We use a named reference if the key is a literal symbol, unless it is |
| 586 | // a string that can be legally parsed as an integer. This is because |
| 587 | // otherwise we will not get into the slow case code that handles [] on |
| 588 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 589 | Literal* literal = property->key()->AsLiteral(); |
| 590 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 591 | if (literal != NULL && |
| 592 | literal->handle()->IsSymbol() && |
| 593 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 594 | ref->set_type(Reference::NAMED); |
| 595 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 596 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | ref->set_type(Reference::KEYED); |
| 598 | } |
| 599 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 600 | // The expression is a variable proxy that does not rewrite to a |
| 601 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 602 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | LoadGlobal(); |
| 604 | ref->set_type(Reference::NAMED); |
| 605 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 606 | ASSERT(var->slot() != NULL); |
| 607 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 608 | } |
| 609 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 610 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 611 | LoadAndSpill(e); |
| 612 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
| 616 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 617 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 618 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 619 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 620 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 621 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 622 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 623 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 624 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 625 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 630 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 631 | // register to a boolean in the condition code register. The code |
| 632 | // 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] | 633 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 634 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 635 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 636 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 637 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 638 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 639 | |
| 640 | // Fast case checks |
| 641 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 642 | // Check if the value is 'false'. |
| 643 | __ cmp(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 644 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 645 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 646 | // Check if the value is 'true'. |
| 647 | __ cmp(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 648 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 649 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 650 | // Check if the value is 'undefined'. |
| 651 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 652 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 653 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 654 | // Check if the value is a smi. |
| 655 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 656 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 657 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 658 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 659 | |
| 660 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 661 | frame_->EmitPush(r0); |
| 662 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 663 | // Convert the result (r0) to a condition code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 664 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 665 | |
| 666 | cc_reg_ = ne; |
| 667 | } |
| 668 | |
| 669 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 670 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 671 | OverwriteMode overwrite_mode, |
| 672 | int constant_rhs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 673 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 674 | // sp[0] : y |
| 675 | // sp[1] : x |
| 676 | // result : r0 |
| 677 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | // Stub is entered with a call: 'return address' is in lr. |
| 679 | switch (op) { |
| 680 | case Token::ADD: // fall through. |
| 681 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 682 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 683 | case Token::DIV: |
| 684 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 685 | case Token::BIT_OR: |
| 686 | case Token::BIT_AND: |
| 687 | case Token::BIT_XOR: |
| 688 | case Token::SHL: |
| 689 | case Token::SHR: |
| 690 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 691 | frame_->EmitPop(r0); // r0 : y |
| 692 | frame_->EmitPop(r1); // r1 : x |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 693 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 694 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 695 | break; |
| 696 | } |
| 697 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 699 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 700 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 701 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | break; |
| 703 | |
| 704 | default: |
| 705 | // Other cases should have been handled before this point. |
| 706 | UNREACHABLE(); |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 712 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 713 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 714 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 715 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 716 | bool reversed, |
| 717 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 718 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 719 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 720 | reversed_(reversed), |
| 721 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 722 | set_comment("[ DeferredInlinedSmiOperation"); |
| 723 | } |
| 724 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 725 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 726 | |
| 727 | private: |
| 728 | Token::Value op_; |
| 729 | int value_; |
| 730 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 731 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 732 | }; |
| 733 | |
| 734 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 735 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 736 | switch (op_) { |
| 737 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 738 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 739 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 740 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 741 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 742 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 743 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 744 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 745 | } |
| 746 | break; |
| 747 | } |
| 748 | |
| 749 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 750 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 751 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 752 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 753 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 754 | } else { |
| 755 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 756 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 757 | } |
| 758 | break; |
| 759 | } |
| 760 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 761 | // For these operations there is no optimistic operation that needs to be |
| 762 | // reverted. |
| 763 | case Token::MUL: |
| 764 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 765 | case Token::BIT_OR: |
| 766 | case Token::BIT_XOR: |
| 767 | case Token::BIT_AND: { |
| 768 | if (reversed_) { |
| 769 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 770 | } else { |
| 771 | __ mov(r1, Operand(r0)); |
| 772 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 773 | } |
| 774 | break; |
| 775 | } |
| 776 | |
| 777 | case Token::SHL: |
| 778 | case Token::SHR: |
| 779 | case Token::SAR: { |
| 780 | if (!reversed_) { |
| 781 | __ mov(r1, Operand(r0)); |
| 782 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 783 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 784 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 785 | } |
| 786 | break; |
| 787 | } |
| 788 | |
| 789 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 790 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 791 | UNREACHABLE(); |
| 792 | break; |
| 793 | } |
| 794 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 795 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 796 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 800 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 801 | x &= x - 1; |
| 802 | return (x & (x - 1)) == 0; |
| 803 | } |
| 804 | |
| 805 | |
| 806 | // Returns the index of the lowest bit set. |
| 807 | static int BitPosition(unsigned x) { |
| 808 | int bit_posn = 0; |
| 809 | while ((x & 0xf) == 0) { |
| 810 | bit_posn += 4; |
| 811 | x >>= 4; |
| 812 | } |
| 813 | while ((x & 1) == 0) { |
| 814 | bit_posn++; |
| 815 | x >>= 1; |
| 816 | } |
| 817 | return bit_posn; |
| 818 | } |
| 819 | |
| 820 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 821 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 822 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 823 | bool reversed, |
| 824 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 825 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 826 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 827 | // some possible smi operations (like + and -) when (at least) one |
| 828 | // of the operands is a literal smi. With this optimization, the |
| 829 | // performance of the system is increased by ~15%, and the generated |
| 830 | // code size is increased by ~1% (measured on a combination of |
| 831 | // different benchmarks). |
| 832 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 833 | // sp[0] : operand |
| 834 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 835 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 836 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 837 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 838 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 839 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 840 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 841 | switch (op) { |
| 842 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 843 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 844 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 845 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 846 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 847 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 848 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 849 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 850 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 851 | break; |
| 852 | } |
| 853 | |
| 854 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 855 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 856 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 858 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 859 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 860 | } else { |
| 861 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 862 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 863 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 864 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 865 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 866 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 867 | break; |
| 868 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 869 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 870 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 871 | case Token::BIT_OR: |
| 872 | case Token::BIT_XOR: |
| 873 | case Token::BIT_AND: { |
| 874 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 875 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 876 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 877 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 878 | switch (op) { |
| 879 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 880 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 881 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 882 | default: UNREACHABLE(); |
| 883 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 884 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 885 | break; |
| 886 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 887 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 888 | case Token::SHL: |
| 889 | case Token::SHR: |
| 890 | case Token::SAR: { |
| 891 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 892 | something_to_inline = false; |
| 893 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 894 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 895 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 896 | DeferredCode* deferred = |
| 897 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
| 898 | __ tst(r0, Operand(kSmiTagMask)); |
| 899 | deferred->Branch(ne); |
| 900 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 901 | switch (op) { |
| 902 | case Token::SHL: { |
| 903 | if (shift_value != 0) { |
| 904 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 905 | } |
| 906 | // check that the *unsigned* result fits in a smi |
| 907 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 908 | deferred->Branch(mi); |
| 909 | break; |
| 910 | } |
| 911 | case Token::SHR: { |
| 912 | // LSR by immediate 0 means shifting 32 bits. |
| 913 | if (shift_value != 0) { |
| 914 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 915 | } |
| 916 | // check that the *unsigned* result fits in a smi |
| 917 | // neither of the two high-order bits can be set: |
| 918 | // - 0x80000000: high bit would be lost when smi tagging |
| 919 | // - 0x40000000: this number would convert to negative when |
| 920 | // smi tagging these two cases can only happen with shifts |
| 921 | // by 0 or 1 when handed a valid smi |
| 922 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 923 | deferred->Branch(ne); |
| 924 | break; |
| 925 | } |
| 926 | case Token::SAR: { |
| 927 | if (shift_value != 0) { |
| 928 | // ASR by immediate 0 means shifting 32 bits. |
| 929 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 930 | } |
| 931 | break; |
| 932 | } |
| 933 | default: UNREACHABLE(); |
| 934 | } |
| 935 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 936 | deferred->BindExit(); |
| 937 | break; |
| 938 | } |
| 939 | |
| 940 | case Token::MOD: { |
| 941 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 942 | something_to_inline = false; |
| 943 | break; |
| 944 | } |
| 945 | DeferredCode* deferred = |
| 946 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 947 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 948 | __ tst(r0, Operand(mask)); |
| 949 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 950 | mask = (int_value << kSmiTagSize) - 1; |
| 951 | __ and_(r0, r0, Operand(mask)); |
| 952 | deferred->BindExit(); |
| 953 | break; |
| 954 | } |
| 955 | |
| 956 | case Token::MUL: { |
| 957 | if (!IsEasyToMultiplyBy(int_value)) { |
| 958 | something_to_inline = false; |
| 959 | break; |
| 960 | } |
| 961 | DeferredCode* deferred = |
| 962 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 963 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 964 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 965 | unsigned mask = 0x80000000u; |
| 966 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 967 | mask |= mask >> 1; |
| 968 | } |
| 969 | mask |= kSmiTagMask; |
| 970 | // This does a single mask that checks for a too high value in a |
| 971 | // conservative way and for a non-Smi. It also filters out negative |
| 972 | // numbers, unfortunately, but since this code is inline we prefer |
| 973 | // brevity to comprehensiveness. |
| 974 | __ tst(r0, Operand(mask)); |
| 975 | deferred->Branch(ne); |
| 976 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 977 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 978 | break; |
| 979 | } |
| 980 | |
| 981 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 982 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 983 | break; |
| 984 | } |
| 985 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 986 | if (!something_to_inline) { |
| 987 | if (!reversed) { |
| 988 | frame_->EmitPush(r0); |
| 989 | __ mov(r0, Operand(value)); |
| 990 | frame_->EmitPush(r0); |
| 991 | GenericBinaryOperation(op, mode, int_value); |
| 992 | } else { |
| 993 | __ mov(ip, Operand(value)); |
| 994 | frame_->EmitPush(ip); |
| 995 | frame_->EmitPush(r0); |
| 996 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 997 | } |
| 998 | } |
| 999 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1000 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1004 | void CodeGenerator::Comparison(Condition cc, |
| 1005 | Expression* left, |
| 1006 | Expression* right, |
| 1007 | bool strict) { |
| 1008 | if (left != NULL) LoadAndSpill(left); |
| 1009 | if (right != NULL) LoadAndSpill(right); |
| 1010 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1011 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1012 | // sp[0] : y |
| 1013 | // sp[1] : x |
| 1014 | // result : cc register |
| 1015 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | // Strict only makes sense for equality comparisons. |
| 1017 | ASSERT(!strict || cc == eq); |
| 1018 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1019 | JumpTarget exit; |
| 1020 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1021 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1022 | if (cc == gt || cc == le) { |
| 1023 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1024 | frame_->EmitPop(r1); |
| 1025 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1026 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1027 | frame_->EmitPop(r0); |
| 1028 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1029 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1030 | __ orr(r2, r0, Operand(r1)); |
| 1031 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1032 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1033 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1034 | // Perform non-smi comparison by stub. |
| 1035 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1036 | // We call with 0 args because there are 0 on the stack. |
| 1037 | CompareStub stub(cc, strict); |
| 1038 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 1039 | __ cmp(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1040 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1041 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1042 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1043 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1044 | __ cmp(r1, Operand(r0)); |
| 1045 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1046 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1047 | cc_reg_ = cc; |
| 1048 | } |
| 1049 | |
| 1050 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1051 | class CallFunctionStub: public CodeStub { |
| 1052 | public: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1053 | CallFunctionStub(int argc, InLoopFlag in_loop) |
| 1054 | : argc_(argc), in_loop_(in_loop) {} |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1055 | |
| 1056 | void Generate(MacroAssembler* masm); |
| 1057 | |
| 1058 | private: |
| 1059 | int argc_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1060 | InLoopFlag in_loop_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1061 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1062 | #if defined(DEBUG) |
| 1063 | void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); } |
| 1064 | #endif // defined(DEBUG) |
| 1065 | |
| 1066 | Major MajorKey() { return CallFunction; } |
| 1067 | int MinorKey() { return argc_; } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1068 | InLoopFlag InLoop() { return in_loop_; } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1069 | }; |
| 1070 | |
| 1071 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1072 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1073 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1074 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1075 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1076 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1077 | int arg_count = args->length(); |
| 1078 | for (int i = 0; i < arg_count; i++) { |
| 1079 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1080 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1081 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1082 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1083 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1084 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1085 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1086 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 1087 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1088 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1089 | |
| 1090 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1091 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1092 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1096 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1097 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | ASSERT(has_cc()); |
| 1099 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1100 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1101 | cc_reg_ = al; |
| 1102 | } |
| 1103 | |
| 1104 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1105 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1106 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1107 | if (FLAG_check_stack) { |
| 1108 | Comment cmnt(masm_, "[ check stack"); |
| 1109 | StackCheckStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1110 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1115 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1116 | #ifdef DEBUG |
| 1117 | int original_height = frame_->height(); |
| 1118 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1119 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1120 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1121 | VisitAndSpill(statements->at(i)); |
| 1122 | } |
| 1123 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1124 | } |
| 1125 | |
| 1126 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1127 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1128 | #ifdef DEBUG |
| 1129 | int original_height = frame_->height(); |
| 1130 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1131 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1132 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1133 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1134 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1135 | VisitStatementsAndSpill(node->statements()); |
| 1136 | if (node->break_target()->is_linked()) { |
| 1137 | node->break_target()->Bind(); |
| 1138 | } |
| 1139 | node->break_target()->Unuse(); |
| 1140 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1144 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1145 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1146 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1147 | frame_->EmitPush(r0); |
| 1148 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1149 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1150 | frame_->EmitPush(r0); |
| 1151 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1152 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1156 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1157 | #ifdef DEBUG |
| 1158 | int original_height = frame_->height(); |
| 1159 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1160 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1161 | Comment cmnt(masm_, "[ Declaration"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1162 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1163 | Variable* var = node->proxy()->var(); |
| 1164 | ASSERT(var != NULL); // must have been resolved |
| 1165 | Slot* slot = var->slot(); |
| 1166 | |
| 1167 | // If it was not possible to allocate the variable at compile time, |
| 1168 | // we need to "declare" it at runtime to make sure it actually |
| 1169 | // exists in the local context. |
| 1170 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1171 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1172 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1173 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1174 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1175 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1176 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1177 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1178 | // Declaration nodes are always declared in only two modes. |
| 1179 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1180 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1181 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1182 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1183 | // Push initial value, if any. |
| 1184 | // Note: For variables we must not push an initial value (such as |
| 1185 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1186 | // must not destroy the current value. |
| 1187 | if (node->mode() == Variable::CONST) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1188 | __ mov(r0, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1189 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1190 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1191 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1192 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1193 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1194 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1195 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1196 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1197 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1198 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1199 | return; |
| 1200 | } |
| 1201 | |
| 1202 | ASSERT(!var->is_global()); |
| 1203 | |
| 1204 | // If we have a function or a constant, we need to initialize the variable. |
| 1205 | Expression* val = NULL; |
| 1206 | if (node->mode() == Variable::CONST) { |
| 1207 | val = new Literal(Factory::the_hole_value()); |
| 1208 | } else { |
| 1209 | val = node->fun(); // NULL if we don't have a function |
| 1210 | } |
| 1211 | |
| 1212 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1213 | { |
| 1214 | // Set initial value. |
| 1215 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1216 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1217 | target.SetValue(NOT_CONST_INIT); |
| 1218 | // The reference is removed from the stack (preserving TOS) when |
| 1219 | // it goes out of scope. |
| 1220 | } |
| 1221 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1222 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1223 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1224 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1228 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1229 | #ifdef DEBUG |
| 1230 | int original_height = frame_->height(); |
| 1231 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1232 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1233 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1234 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1235 | Expression* expression = node->expression(); |
| 1236 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1237 | LoadAndSpill(expression); |
| 1238 | frame_->Drop(); |
| 1239 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1243 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1244 | #ifdef DEBUG |
| 1245 | int original_height = frame_->height(); |
| 1246 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1247 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1248 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1249 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1250 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1251 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1255 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1256 | #ifdef DEBUG |
| 1257 | int original_height = frame_->height(); |
| 1258 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1259 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1260 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1261 | // Generate different code depending on which parts of the if statement |
| 1262 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1263 | bool has_then_stm = node->HasThenStatement(); |
| 1264 | bool has_else_stm = node->HasElseStatement(); |
| 1265 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1266 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1267 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1268 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1269 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1270 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1271 | JumpTarget then; |
| 1272 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1273 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1274 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1275 | &then, &else_, true); |
| 1276 | if (frame_ != NULL) { |
| 1277 | Branch(false, &else_); |
| 1278 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1279 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1280 | if (frame_ != NULL || then.is_linked()) { |
| 1281 | then.Bind(); |
| 1282 | VisitAndSpill(node->then_statement()); |
| 1283 | } |
| 1284 | if (frame_ != NULL) { |
| 1285 | exit.Jump(); |
| 1286 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1288 | if (else_.is_linked()) { |
| 1289 | else_.Bind(); |
| 1290 | VisitAndSpill(node->else_statement()); |
| 1291 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1292 | |
| 1293 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1294 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1296 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1297 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1298 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1299 | &then, &exit, true); |
| 1300 | if (frame_ != NULL) { |
| 1301 | Branch(false, &exit); |
| 1302 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1303 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1304 | if (frame_ != NULL || then.is_linked()) { |
| 1305 | then.Bind(); |
| 1306 | VisitAndSpill(node->then_statement()); |
| 1307 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | |
| 1309 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1310 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1311 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1312 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1313 | // if (!cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1314 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1315 | &exit, &else_, true); |
| 1316 | if (frame_ != NULL) { |
| 1317 | Branch(true, &exit); |
| 1318 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1319 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1320 | if (frame_ != NULL || else_.is_linked()) { |
| 1321 | else_.Bind(); |
| 1322 | VisitAndSpill(node->else_statement()); |
| 1323 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1324 | |
| 1325 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1326 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1327 | ASSERT(!has_then_stm && !has_else_stm); |
| 1328 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1329 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1330 | &exit, &exit, false); |
| 1331 | if (frame_ != NULL) { |
| 1332 | if (has_cc()) { |
| 1333 | cc_reg_ = al; |
| 1334 | } else { |
| 1335 | frame_->Drop(); |
| 1336 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1341 | if (exit.is_linked()) { |
| 1342 | exit.Bind(); |
| 1343 | } |
| 1344 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1348 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1349 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1350 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1351 | CodeForStatementPosition(node); |
| 1352 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1356 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1357 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1358 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1359 | CodeForStatementPosition(node); |
| 1360 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1364 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1365 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1366 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1367 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1368 | if (function_return_is_shadowed_) { |
| 1369 | CodeForStatementPosition(node); |
| 1370 | LoadAndSpill(node->expression()); |
| 1371 | frame_->EmitPop(r0); |
| 1372 | function_return_.Jump(); |
| 1373 | } else { |
| 1374 | // Load the returned value. |
| 1375 | CodeForStatementPosition(node); |
| 1376 | LoadAndSpill(node->expression()); |
| 1377 | |
| 1378 | // Pop the result from the frame and prepare the frame for |
| 1379 | // returning thus making it easier to merge. |
| 1380 | frame_->EmitPop(r0); |
| 1381 | frame_->PrepareForReturn(); |
| 1382 | |
| 1383 | function_return_.Jump(); |
| 1384 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1388 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1389 | #ifdef DEBUG |
| 1390 | int original_height = frame_->height(); |
| 1391 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1392 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1393 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1394 | CodeForStatementPosition(node); |
| 1395 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1396 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1397 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1398 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1399 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1400 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1401 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1402 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1403 | __ cmp(r0, Operand(cp)); |
| 1404 | verified_true.Branch(eq); |
| 1405 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1406 | verified_true.Bind(); |
| 1407 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1408 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1409 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1410 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1414 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1415 | #ifdef DEBUG |
| 1416 | int original_height = frame_->height(); |
| 1417 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1418 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1419 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1420 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1421 | // Pop context. |
| 1422 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1423 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1424 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1425 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1429 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1430 | #ifdef DEBUG |
| 1431 | int original_height = frame_->height(); |
| 1432 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1433 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1434 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1435 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1436 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1437 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1438 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1439 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1440 | JumpTarget next_test; |
| 1441 | JumpTarget fall_through; |
| 1442 | JumpTarget default_entry; |
| 1443 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1444 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1445 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1446 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1447 | |
| 1448 | for (int i = 0; i < length; i++) { |
| 1449 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1450 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1451 | // Remember the default clause and compile it at the end. |
| 1452 | default_clause = clause; |
| 1453 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1456 | Comment cmnt(masm_, "[ Case clause"); |
| 1457 | // Compile the test. |
| 1458 | next_test.Bind(); |
| 1459 | next_test.Unuse(); |
| 1460 | // Duplicate TOS. |
| 1461 | __ ldr(r0, frame_->Top()); |
| 1462 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1463 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1464 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1465 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1466 | // Before entering the body from the test, remove the switch value from |
| 1467 | // the stack. |
| 1468 | frame_->Drop(); |
| 1469 | |
| 1470 | // Label the body so that fall through is enabled. |
| 1471 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1472 | default_exit.Bind(); |
| 1473 | } else { |
| 1474 | fall_through.Bind(); |
| 1475 | fall_through.Unuse(); |
| 1476 | } |
| 1477 | VisitStatementsAndSpill(clause->statements()); |
| 1478 | |
| 1479 | // If control flow can fall through from the body, jump to the next body |
| 1480 | // or the end of the statement. |
| 1481 | if (frame_ != NULL) { |
| 1482 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1483 | default_entry.Jump(); |
| 1484 | } else { |
| 1485 | fall_through.Jump(); |
| 1486 | } |
| 1487 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1490 | // The final "test" removes the switch value. |
| 1491 | next_test.Bind(); |
| 1492 | frame_->Drop(); |
| 1493 | |
| 1494 | // If there is a default clause, compile it. |
| 1495 | if (default_clause != NULL) { |
| 1496 | Comment cmnt(masm_, "[ Default clause"); |
| 1497 | default_entry.Bind(); |
| 1498 | VisitStatementsAndSpill(default_clause->statements()); |
| 1499 | // If control flow can fall out of the default and there is a case after |
| 1500 | // it, jup to that case's body. |
| 1501 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1502 | default_exit.Jump(); |
| 1503 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1504 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1505 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1506 | if (fall_through.is_linked()) { |
| 1507 | fall_through.Bind(); |
| 1508 | } |
| 1509 | |
| 1510 | if (node->break_target()->is_linked()) { |
| 1511 | node->break_target()->Bind(); |
| 1512 | } |
| 1513 | node->break_target()->Unuse(); |
| 1514 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1518 | void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1519 | #ifdef DEBUG |
| 1520 | int original_height = frame_->height(); |
| 1521 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1522 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1523 | Comment cmnt(masm_, "[ LoopStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1524 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1525 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1526 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1527 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 1528 | // known result for the test expression, with no side effects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1529 | enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
| 1530 | if (node->cond() == NULL) { |
| 1531 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1532 | info = ALWAYS_TRUE; |
| 1533 | } else { |
| 1534 | Literal* lit = node->cond()->AsLiteral(); |
| 1535 | if (lit != NULL) { |
| 1536 | if (lit->IsTrue()) { |
| 1537 | info = ALWAYS_TRUE; |
| 1538 | } else if (lit->IsFalse()) { |
| 1539 | info = ALWAYS_FALSE; |
| 1540 | } |
| 1541 | } |
| 1542 | } |
| 1543 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1544 | switch (node->type()) { |
| 1545 | case LoopStatement::DO_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1546 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1547 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1548 | // Label the top of the loop for the backward CFG edge. If the test |
| 1549 | // is always true we can use the continue target, and if the test is |
| 1550 | // always false there is no need. |
| 1551 | if (info == ALWAYS_TRUE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1552 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1553 | node->continue_target()->Bind(); |
| 1554 | } else if (info == ALWAYS_FALSE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1555 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1556 | } else { |
| 1557 | ASSERT(info == DONT_KNOW); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1558 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1559 | body.Bind(); |
| 1560 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1561 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1562 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1563 | VisitAndSpill(node->body()); |
| 1564 | |
| 1565 | // Compile the test. |
| 1566 | if (info == ALWAYS_TRUE) { |
| 1567 | if (has_valid_frame()) { |
| 1568 | // If control can fall off the end of the body, jump back to the |
| 1569 | // top. |
| 1570 | node->continue_target()->Jump(); |
| 1571 | } |
| 1572 | } else if (info == ALWAYS_FALSE) { |
| 1573 | // If we have a continue in the body, we only have to bind its jump |
| 1574 | // target. |
| 1575 | if (node->continue_target()->is_linked()) { |
| 1576 | node->continue_target()->Bind(); |
| 1577 | } |
| 1578 | } else { |
| 1579 | ASSERT(info == DONT_KNOW); |
| 1580 | // We have to compile the test expression if it can be reached by |
| 1581 | // control flow falling out of the body or via continue. |
| 1582 | if (node->continue_target()->is_linked()) { |
| 1583 | node->continue_target()->Bind(); |
| 1584 | } |
| 1585 | if (has_valid_frame()) { |
| 1586 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1587 | &body, node->break_target(), true); |
| 1588 | if (has_valid_frame()) { |
| 1589 | // A invalid frame here indicates that control did not |
| 1590 | // fall out of the test expression. |
| 1591 | Branch(true, &body); |
| 1592 | } |
| 1593 | } |
| 1594 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1595 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | case LoopStatement::WHILE_LOOP: { |
| 1599 | // If the test is never true and has no side effects there is no need |
| 1600 | // to compile the test or body. |
| 1601 | if (info == ALWAYS_FALSE) break; |
| 1602 | |
| 1603 | // Label the top of the loop with the continue target for the backward |
| 1604 | // CFG edge. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1605 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1606 | node->continue_target()->Bind(); |
| 1607 | |
| 1608 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1609 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1610 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1611 | &body, node->break_target(), true); |
| 1612 | if (has_valid_frame()) { |
| 1613 | // A NULL frame indicates that control did not fall out of the |
| 1614 | // test expression. |
| 1615 | Branch(false, node->break_target()); |
| 1616 | } |
| 1617 | if (has_valid_frame() || body.is_linked()) { |
| 1618 | body.Bind(); |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | if (has_valid_frame()) { |
| 1623 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1624 | VisitAndSpill(node->body()); |
| 1625 | |
| 1626 | // If control flow can fall out of the body, jump back to the top. |
| 1627 | if (has_valid_frame()) { |
| 1628 | node->continue_target()->Jump(); |
| 1629 | } |
| 1630 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1631 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | case LoopStatement::FOR_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1635 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1636 | |
| 1637 | if (node->init() != NULL) { |
| 1638 | VisitAndSpill(node->init()); |
| 1639 | } |
| 1640 | |
| 1641 | // There is no need to compile the test or body. |
| 1642 | if (info == ALWAYS_FALSE) break; |
| 1643 | |
| 1644 | // If there is no update statement, label the top of the loop with the |
| 1645 | // continue target, otherwise with the loop target. |
| 1646 | if (node->next() == NULL) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1647 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1648 | node->continue_target()->Bind(); |
| 1649 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1650 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1651 | loop.Bind(); |
| 1652 | } |
| 1653 | |
| 1654 | // If the test is always true, there is no need to compile it. |
| 1655 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1656 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1657 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1658 | &body, node->break_target(), true); |
| 1659 | if (has_valid_frame()) { |
| 1660 | Branch(false, node->break_target()); |
| 1661 | } |
| 1662 | if (has_valid_frame() || body.is_linked()) { |
| 1663 | body.Bind(); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | if (has_valid_frame()) { |
| 1668 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1669 | VisitAndSpill(node->body()); |
| 1670 | |
| 1671 | if (node->next() == NULL) { |
| 1672 | // If there is no update statement and control flow can fall out |
| 1673 | // of the loop, jump directly to the continue label. |
| 1674 | if (has_valid_frame()) { |
| 1675 | node->continue_target()->Jump(); |
| 1676 | } |
| 1677 | } else { |
| 1678 | // If there is an update statement and control flow can reach it |
| 1679 | // via falling out of the body of the loop or continuing, we |
| 1680 | // compile the update statement. |
| 1681 | if (node->continue_target()->is_linked()) { |
| 1682 | node->continue_target()->Bind(); |
| 1683 | } |
| 1684 | if (has_valid_frame()) { |
| 1685 | // Record source position of the statement as this code which is |
| 1686 | // after the code for the body actually belongs to the loop |
| 1687 | // statement and not the body. |
| 1688 | CodeForStatementPosition(node); |
| 1689 | VisitAndSpill(node->next()); |
| 1690 | loop.Jump(); |
| 1691 | } |
| 1692 | } |
| 1693 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1694 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1695 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1698 | if (node->break_target()->is_linked()) { |
| 1699 | node->break_target()->Bind(); |
| 1700 | } |
| 1701 | node->continue_target()->Unuse(); |
| 1702 | node->break_target()->Unuse(); |
| 1703 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1707 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1708 | #ifdef DEBUG |
| 1709 | int original_height = frame_->height(); |
| 1710 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1711 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1712 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1713 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1714 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1715 | JumpTarget primitive; |
| 1716 | JumpTarget jsobject; |
| 1717 | JumpTarget fixed_array; |
| 1718 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1719 | JumpTarget end_del_check; |
| 1720 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1721 | |
| 1722 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1723 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1724 | |
| 1725 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1726 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1727 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1728 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1729 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1730 | __ cmp(r0, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1731 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1732 | |
| 1733 | // Stack layout in body: |
| 1734 | // [iteration counter (Smi)] |
| 1735 | // [length of array] |
| 1736 | // [FixedArray] |
| 1737 | // [Map or 0] |
| 1738 | // [Object] |
| 1739 | |
| 1740 | // Check if enumerable is already a JSObject |
| 1741 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1742 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1743 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1744 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1745 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1746 | primitive.Bind(); |
| 1747 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 1748 | Result arg_count(r0); |
| 1749 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1750 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1751 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1752 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1753 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1754 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1755 | frame_->EmitPush(r0); |
| 1756 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1757 | |
| 1758 | // If we got a Map, we can do a fast modification check. |
| 1759 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1760 | __ mov(r2, Operand(r0)); |
| 1761 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1762 | __ cmp(r1, Operand(Factory::meta_map())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1763 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1764 | |
| 1765 | // Get enum cache |
| 1766 | __ mov(r1, Operand(r0)); |
| 1767 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1768 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1769 | __ ldr(r2, |
| 1770 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1771 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1772 | frame_->EmitPush(r0); // map |
| 1773 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1774 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1775 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1776 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1777 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1778 | frame_->EmitPush(r0); |
| 1779 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1780 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1781 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1782 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1783 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1784 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1785 | |
| 1786 | // 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] | 1787 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1788 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1789 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1790 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1791 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1792 | |
| 1793 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1794 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1795 | // sp[0] : index |
| 1796 | // sp[1] : array/enum cache length |
| 1797 | // sp[2] : array or enum cache |
| 1798 | // sp[3] : 0 or map |
| 1799 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1800 | // Grab the current frame's height for the break and continue |
| 1801 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1802 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1803 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1804 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1805 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1806 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1807 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1808 | node->break_target()->Branch(hs); |
| 1809 | |
| 1810 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1811 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1812 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1813 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1814 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1815 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1816 | |
| 1817 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1818 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1819 | // Check if this (still) matches the map of the enumerable. |
| 1820 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1821 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1822 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1823 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1824 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1825 | |
| 1826 | // 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] | 1827 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1828 | frame_->EmitPush(r0); |
| 1829 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 1830 | Result arg_count_reg(r0); |
| 1831 | __ mov(r0, Operand(1)); |
| 1832 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, &arg_count_reg, 2); |
| 1833 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1834 | |
| 1835 | // If the property has been removed while iterating, we just skip it. |
| 1836 | __ cmp(r3, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1837 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1838 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1839 | end_del_check.Bind(); |
| 1840 | // Store the entry in the 'each' expression and take another spin in the |
| 1841 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1842 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1843 | { Reference each(this, node->each()); |
| 1844 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1845 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1846 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1847 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1848 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1849 | // If the reference was to a slot we rely on the convenient property |
| 1850 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1851 | // right on top of or right underneath a zero-sized reference. |
| 1852 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1853 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1854 | // It's safe to pop the value lying on top of the reference before |
| 1855 | // unloading the reference itself (which preserves the top of stack, |
| 1856 | // ie, now the topmost value of the non-zero sized reference), since |
| 1857 | // we will discard the top of stack after unloading the reference |
| 1858 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1859 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1860 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1861 | } |
| 1862 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1863 | // Discard the i'th entry pushed above or else the remainder of the |
| 1864 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1865 | frame_->Drop(); |
| 1866 | |
| 1867 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1869 | VisitAndSpill(node->body()); |
| 1870 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1871 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1872 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1873 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1874 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1875 | frame_->EmitPop(r0); |
| 1876 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1877 | frame_->EmitPush(r0); |
| 1878 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1879 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1880 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1881 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1882 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1883 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | |
| 1885 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1886 | exit.Bind(); |
| 1887 | node->continue_target()->Unuse(); |
| 1888 | node->break_target()->Unuse(); |
| 1889 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1893 | void CodeGenerator::VisitTryCatch(TryCatch* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1894 | #ifdef DEBUG |
| 1895 | int original_height = frame_->height(); |
| 1896 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1897 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1898 | Comment cmnt(masm_, "[ TryCatch"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1899 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1900 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1901 | JumpTarget try_block; |
| 1902 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1903 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1904 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1905 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1906 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1907 | |
| 1908 | // Store the caught exception in the catch variable. |
| 1909 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1910 | ASSERT(ref.is_slot()); |
| 1911 | // Here we make use of the convenient property that it doesn't matter |
| 1912 | // whether a value is immediately on top of or underneath a zero-sized |
| 1913 | // reference. |
| 1914 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1918 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1919 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1920 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 1921 | if (frame_ != NULL) { |
| 1922 | exit.Jump(); |
| 1923 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1924 | |
| 1925 | |
| 1926 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1927 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1928 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1929 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 1930 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1931 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1932 | // Shadow the labels for all escapes from the try block, including |
| 1933 | // returns. During shadowing, the original label is hidden as the |
| 1934 | // LabelShadow and operations on the original actually affect the |
| 1935 | // shadowing label. |
| 1936 | // |
| 1937 | // We should probably try to unify the escaping labels and the return |
| 1938 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1939 | int nof_escapes = node->escaping_targets()->length(); |
| 1940 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 1941 | |
| 1942 | // Add the shadow target for the function return. |
| 1943 | static const int kReturnShadowIndex = 0; |
| 1944 | shadows.Add(new ShadowTarget(&function_return_)); |
| 1945 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 1946 | function_return_is_shadowed_ = true; |
| 1947 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 1948 | |
| 1949 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1951 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1955 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1956 | |
| 1957 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1958 | // After shadowing stops, the original labels are unshadowed and the |
| 1959 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1960 | bool has_unlinks = false; |
| 1961 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1962 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1963 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 1964 | } |
| 1965 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 1966 | |
| 1967 | // Get an external reference to the handler address. |
| 1968 | ExternalReference handler_address(Top::k_handler_address); |
| 1969 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1970 | // If we can fall off the end of the try block, unlink from try chain. |
| 1971 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1972 | // The next handler address is on top of the frame. Unlink from |
| 1973 | // the handler list and drop the rest of this handler from the |
| 1974 | // frame. |
| 1975 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 1976 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1977 | __ mov(r3, Operand(handler_address)); |
| 1978 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1979 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1980 | if (has_unlinks) { |
| 1981 | exit.Jump(); |
| 1982 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1985 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1986 | // jumped to. Deallocate each shadow target. |
| 1987 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1988 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1989 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1990 | shadows[i]->Bind(); |
| 1991 | // Because we can be jumping here (to spilled code) from unspilled |
| 1992 | // code, we need to reestablish a spilled frame at this block. |
| 1993 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1994 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1995 | // Reload sp from the top handler, because some statements that we |
| 1996 | // 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] | 1997 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1998 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1999 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2000 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2001 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2002 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2003 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2004 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2005 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2006 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2007 | frame_->PrepareForReturn(); |
| 2008 | } |
| 2009 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } |
| 2012 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2013 | exit.Bind(); |
| 2014 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2018 | void CodeGenerator::VisitTryFinally(TryFinally* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2019 | #ifdef DEBUG |
| 2020 | int original_height = frame_->height(); |
| 2021 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2022 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2023 | Comment cmnt(masm_, "[ TryFinally"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2024 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2025 | |
| 2026 | // State: Used to keep track of reason for entering the finally |
| 2027 | // block. Should probably be extended to hold information for |
| 2028 | // break/continue from within the try block. |
| 2029 | enum { FALLING, THROWING, JUMPING }; |
| 2030 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2031 | JumpTarget try_block; |
| 2032 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2033 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2034 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2035 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2036 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2037 | // In case of thrown exceptions, this is where we continue. |
| 2038 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2039 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2040 | |
| 2041 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2042 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2043 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2044 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2045 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2046 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2047 | // Shadow the labels for all escapes from the try block, including |
| 2048 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2049 | // operations on the original actually affect the shadowing label. |
| 2050 | // |
| 2051 | // We should probably try to unify the escaping labels and the return |
| 2052 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2053 | int nof_escapes = node->escaping_targets()->length(); |
| 2054 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2055 | |
| 2056 | // Add the shadow target for the function return. |
| 2057 | static const int kReturnShadowIndex = 0; |
| 2058 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2059 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2060 | function_return_is_shadowed_ = true; |
| 2061 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2062 | |
| 2063 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2064 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2065 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2069 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2070 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2071 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2072 | // After shadowing stops, the original labels are unshadowed and the |
| 2073 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2074 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2075 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2076 | shadows[i]->StopShadowing(); |
| 2077 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2078 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2079 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2080 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2081 | // Get an external reference to the handler address. |
| 2082 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2083 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2084 | // If we can fall off the end of the try block, unlink from the try |
| 2085 | // chain and set the state on the frame to FALLING. |
| 2086 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2087 | // The next handler address is on top of the frame. |
| 2088 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2089 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2090 | __ mov(r3, Operand(handler_address)); |
| 2091 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2092 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2093 | |
| 2094 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2095 | // state in r2, then jump around the unlink blocks if any. |
| 2096 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2097 | frame_->EmitPush(r0); |
| 2098 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2099 | if (nof_unlinks > 0) { |
| 2100 | finally_block.Jump(); |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | // Generate code to unlink and set the state for the (formerly) |
| 2105 | // shadowing targets that have been jumped to. |
| 2106 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2107 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2108 | // If we have come from the shadowed return, the return value is |
| 2109 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2110 | // until it is pushed. |
| 2111 | // |
| 2112 | // Because we can be jumping here (to spilled code) from |
| 2113 | // unspilled code, we need to reestablish a spilled frame at |
| 2114 | // this block. |
| 2115 | shadows[i]->Bind(); |
| 2116 | frame_->SpillAll(); |
| 2117 | |
| 2118 | // Reload sp from the top handler, because some statements that |
| 2119 | // we break from (eg, for...in) may have left stuff on the |
| 2120 | // stack. |
| 2121 | __ mov(r3, Operand(handler_address)); |
| 2122 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2123 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2124 | |
| 2125 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2126 | // handler address is currently on top of the frame. |
| 2127 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2128 | frame_->EmitPop(r1); |
| 2129 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2130 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2131 | |
| 2132 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2133 | // If this label shadowed the function return, materialize the |
| 2134 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2135 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2136 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2137 | // Fake TOS for targets that shadowed breaks and continues. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2138 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2139 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2140 | } |
| 2141 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2142 | if (--nof_unlinks > 0) { |
| 2143 | // If this is not the last unlink block, jump around the next. |
| 2144 | finally_block.Jump(); |
| 2145 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2149 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2150 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2151 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2152 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2153 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2154 | |
| 2155 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2156 | // and the state - while evaluating the finally block. |
| 2157 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2158 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2159 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2160 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2161 | if (has_valid_frame()) { |
| 2162 | // Restore state and return value or faked TOS. |
| 2163 | frame_->EmitPop(r2); |
| 2164 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2167 | // Generate code to jump to the right destination for all used |
| 2168 | // formerly shadowing targets. Deallocate each shadow target. |
| 2169 | for (int i = 0; i < shadows.length(); i++) { |
| 2170 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2171 | JumpTarget* original = shadows[i]->other_target(); |
| 2172 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2173 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2174 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2175 | skip.Branch(ne); |
| 2176 | frame_->PrepareForReturn(); |
| 2177 | original->Jump(); |
| 2178 | skip.Bind(); |
| 2179 | } else { |
| 2180 | original->Branch(eq); |
| 2181 | } |
| 2182 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2183 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2184 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2185 | if (has_valid_frame()) { |
| 2186 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2187 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2188 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2189 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2190 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2191 | // Rethrow exception. |
| 2192 | frame_->EmitPush(r0); |
| 2193 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2194 | |
| 2195 | // Done. |
| 2196 | exit.Bind(); |
| 2197 | } |
| 2198 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2202 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2203 | #ifdef DEBUG |
| 2204 | int original_height = frame_->height(); |
| 2205 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2206 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2207 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2208 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2209 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2210 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2211 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2212 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2213 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2217 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2218 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2219 | ASSERT(boilerplate->IsBoilerplate()); |
| 2220 | |
| 2221 | // Push the boilerplate on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2222 | __ mov(r0, Operand(boilerplate)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2223 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2224 | |
| 2225 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2226 | frame_->EmitPush(cp); |
| 2227 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2228 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2232 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2233 | #ifdef DEBUG |
| 2234 | int original_height = frame_->height(); |
| 2235 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2236 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2237 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2238 | |
| 2239 | // Build the function boilerplate and instantiate it. |
| 2240 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2241 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2242 | if (HasStackOverflow()) { |
| 2243 | ASSERT(frame_->height() == original_height); |
| 2244 | return; |
| 2245 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2246 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2247 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2251 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2252 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2253 | #ifdef DEBUG |
| 2254 | int original_height = frame_->height(); |
| 2255 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2256 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2257 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2258 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2259 | ASSERT(frame_->height() == original_height + 1); |
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::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2264 | #ifdef DEBUG |
| 2265 | int original_height = frame_->height(); |
| 2266 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2267 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2268 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2269 | JumpTarget then; |
| 2270 | JumpTarget else_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2271 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 2272 | &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2273 | if (has_valid_frame()) { |
| 2274 | Branch(false, &else_); |
| 2275 | } |
| 2276 | if (has_valid_frame() || then.is_linked()) { |
| 2277 | then.Bind(); |
| 2278 | LoadAndSpill(node->then_expression(), typeof_state()); |
| 2279 | } |
| 2280 | if (else_.is_linked()) { |
| 2281 | JumpTarget exit; |
| 2282 | if (has_valid_frame()) exit.Jump(); |
| 2283 | else_.Bind(); |
| 2284 | LoadAndSpill(node->else_expression(), typeof_state()); |
| 2285 | if (exit.is_linked()) exit.Bind(); |
| 2286 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2287 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2291 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2292 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2293 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2294 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2295 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2296 | JumpTarget slow; |
| 2297 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2298 | |
| 2299 | // Generate fast-case code for variables that might be shadowed by |
| 2300 | // eval-introduced variables. Eval is used a lot without |
| 2301 | // introducing variables. In those cases, we do not want to |
| 2302 | // perform a runtime call for all variables in the scope |
| 2303 | // containing the eval. |
| 2304 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2305 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2306 | // If there was no control flow to slow, we can exit early. |
| 2307 | if (!slow.is_linked()) { |
| 2308 | frame_->EmitPush(r0); |
| 2309 | return; |
| 2310 | } |
| 2311 | |
| 2312 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2313 | |
| 2314 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2315 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2316 | // Only generate the fast case for locals that rewrite to slots. |
| 2317 | // This rules out argument loads. |
| 2318 | if (potential_slot != NULL) { |
| 2319 | __ ldr(r0, |
| 2320 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2321 | r1, |
| 2322 | r2, |
| 2323 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2324 | if (potential_slot->var()->mode() == Variable::CONST) { |
| 2325 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2326 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
| 2327 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2328 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2329 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2330 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2331 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2335 | slow.Bind(); |
| 2336 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2337 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2338 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2339 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2340 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2341 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2342 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2343 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2344 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2345 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2346 | done.Bind(); |
| 2347 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2348 | |
| 2349 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2350 | // Note: We would like to keep the assert below, but it fires because of |
| 2351 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2352 | // ASSERT(!slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2353 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2354 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2355 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2356 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2357 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2358 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2359 | // initialized yet) which needs to be converted into the 'undefined' |
| 2360 | // value. |
| 2361 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2362 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2363 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2364 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2365 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2366 | } |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2371 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2372 | TypeofState typeof_state, |
| 2373 | Register tmp, |
| 2374 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2375 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2376 | // Check that no extension objects have been created by calls to |
| 2377 | // eval from the current scope to the global scope. |
| 2378 | Register context = cp; |
| 2379 | Scope* s = scope(); |
| 2380 | while (s != NULL) { |
| 2381 | if (s->num_heap_slots() > 0) { |
| 2382 | if (s->calls_eval()) { |
| 2383 | // Check that extension is NULL. |
| 2384 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2385 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2386 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2387 | } |
| 2388 | // Load next context in chain. |
| 2389 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2390 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2391 | context = tmp; |
| 2392 | } |
| 2393 | // If no outer scope calls eval, we do not need to check more |
| 2394 | // context extensions. |
| 2395 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2396 | s = s->outer_scope(); |
| 2397 | } |
| 2398 | |
| 2399 | if (s->is_eval_scope()) { |
| 2400 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2401 | if (!context.is(tmp)) { |
| 2402 | __ mov(tmp, Operand(context)); |
| 2403 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2404 | __ bind(&next); |
| 2405 | // Terminate at global context. |
| 2406 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 2407 | __ cmp(tmp2, Operand(Factory::global_context_map())); |
| 2408 | __ b(eq, &fast); |
| 2409 | // Check that extension is NULL. |
| 2410 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2411 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2412 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2413 | // Load next context in chain. |
| 2414 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2415 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2416 | __ b(&next); |
| 2417 | __ bind(&fast); |
| 2418 | } |
| 2419 | |
| 2420 | // All extension objects were empty and it is safe to use a global |
| 2421 | // load IC call. |
| 2422 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2423 | // Load the global object. |
| 2424 | LoadGlobal(); |
| 2425 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 2426 | Result name(r2); |
| 2427 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2428 | // Call IC stub. |
| 2429 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2430 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2431 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2432 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2435 | // Drop the global object. The result is in r0. |
| 2436 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2440 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2441 | #ifdef DEBUG |
| 2442 | int original_height = frame_->height(); |
| 2443 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2444 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2445 | Comment cmnt(masm_, "[ Slot"); |
| 2446 | LoadFromSlot(node, typeof_state()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2447 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2448 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2449 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2450 | |
| 2451 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2452 | #ifdef DEBUG |
| 2453 | int original_height = frame_->height(); |
| 2454 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2455 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2456 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2457 | |
| 2458 | Variable* var = node->var(); |
| 2459 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2460 | if (expr != NULL) { |
| 2461 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2462 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2463 | ASSERT(var->is_global()); |
| 2464 | Reference ref(this, node); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2465 | ref.GetValueAndSpill(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2466 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2467 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2471 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2472 | #ifdef DEBUG |
| 2473 | int original_height = frame_->height(); |
| 2474 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2475 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2476 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2477 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2478 | frame_->EmitPush(r0); |
| 2479 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2483 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2484 | #ifdef DEBUG |
| 2485 | int original_height = frame_->height(); |
| 2486 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2487 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2488 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2489 | |
| 2490 | // Retrieve the literal array and check the allocated entry. |
| 2491 | |
| 2492 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2493 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2494 | |
| 2495 | // Load the literals array of the function. |
| 2496 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2497 | |
| 2498 | // Load the literal at the ast saved index. |
| 2499 | int literal_offset = |
| 2500 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2501 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2502 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2503 | JumpTarget done; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2504 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2505 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2506 | |
| 2507 | // If the entry is undefined we call the runtime system to computed |
| 2508 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2509 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2510 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2511 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2512 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2513 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2514 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2515 | frame_->EmitPush(r0); |
| 2516 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2517 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2518 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2519 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2520 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2521 | frame_->EmitPush(r2); |
| 2522 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | |
| 2526 | // This deferred code stub will be used for creating the boilerplate |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2527 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2528 | // Each created boilerplate is stored in the JSFunction and they are |
| 2529 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2530 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2531 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2532 | explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2533 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2534 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2535 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2536 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2537 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2538 | private: |
| 2539 | ObjectLiteral* node_; |
| 2540 | }; |
| 2541 | |
| 2542 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2543 | void DeferredObjectLiteral::Generate() { |
| 2544 | // Argument is passed in r1. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2545 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2546 | // 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] | 2547 | // the literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2548 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2549 | __ push(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2550 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2551 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2552 | __ push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2553 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2554 | __ mov(r0, Operand(node_->constant_properties())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2555 | __ push(r0); |
| 2556 | __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2557 | __ mov(r2, Operand(r0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2558 | // Result is returned in r2. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2562 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2563 | #ifdef DEBUG |
| 2564 | int original_height = frame_->height(); |
| 2565 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2566 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2567 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2568 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2569 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2570 | |
| 2571 | // Retrieve the literal array and check the allocated entry. |
| 2572 | |
| 2573 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2574 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2575 | |
| 2576 | // Load the literals array of the function. |
| 2577 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2578 | |
| 2579 | // Load the literal at the ast saved index. |
| 2580 | int literal_offset = |
| 2581 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2582 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2583 | |
| 2584 | // Check whether we need to materialize the object literal boilerplate. |
| 2585 | // If so, jump to the deferred code. |
| 2586 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2587 | deferred->Branch(eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2588 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2589 | |
| 2590 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2591 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2592 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2593 | // Clone the boilerplate object. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2594 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2595 | if (node->depth() == 1) { |
| 2596 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2597 | } |
| 2598 | frame_->CallRuntime(clone_function_id, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2599 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2600 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2601 | |
| 2602 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2603 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2604 | Literal* key = property->key(); |
| 2605 | Expression* value = property->value(); |
| 2606 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2607 | case ObjectLiteral::Property::CONSTANT: |
| 2608 | break; |
| 2609 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2610 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2611 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2612 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2613 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2614 | frame_->EmitPush(r0); // dup the result |
| 2615 | LoadAndSpill(key); |
| 2616 | LoadAndSpill(value); |
| 2617 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2618 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2619 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2620 | break; |
| 2621 | } |
| 2622 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2623 | frame_->EmitPush(r0); |
| 2624 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2625 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2626 | frame_->EmitPush(r0); |
| 2627 | LoadAndSpill(value); |
| 2628 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2629 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2630 | break; |
| 2631 | } |
| 2632 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2633 | frame_->EmitPush(r0); |
| 2634 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2635 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2636 | frame_->EmitPush(r0); |
| 2637 | LoadAndSpill(value); |
| 2638 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2639 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2640 | break; |
| 2641 | } |
| 2642 | } |
| 2643 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2644 | ASSERT(frame_->height() == original_height + 1); |
| 2645 | } |
| 2646 | |
| 2647 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2648 | // This deferred code stub will be used for creating the boilerplate |
| 2649 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2650 | // Each created boilerplate is stored in the JSFunction and they are |
| 2651 | // therefore context dependent. |
| 2652 | class DeferredArrayLiteral: public DeferredCode { |
| 2653 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2654 | explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2655 | set_comment("[ DeferredArrayLiteral"); |
| 2656 | } |
| 2657 | |
| 2658 | virtual void Generate(); |
| 2659 | |
| 2660 | private: |
| 2661 | ArrayLiteral* node_; |
| 2662 | }; |
| 2663 | |
| 2664 | |
| 2665 | void DeferredArrayLiteral::Generate() { |
| 2666 | // Argument is passed in r1. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2667 | |
| 2668 | // If the entry is undefined we call the runtime system to computed |
| 2669 | // the literal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2670 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2671 | __ push(r1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2672 | // Literal index (1). |
| 2673 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2674 | __ push(r0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2675 | // Constant properties (2). |
| 2676 | __ mov(r0, Operand(node_->literals())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2677 | __ push(r0); |
| 2678 | __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2679 | __ mov(r2, Operand(r0)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2680 | // Result is returned in r2. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2684 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2685 | #ifdef DEBUG |
| 2686 | int original_height = frame_->height(); |
| 2687 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2688 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2689 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2690 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2691 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2692 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2693 | // Retrieve the literal array and check the allocated entry. |
| 2694 | |
| 2695 | // Load the function of this activation. |
| 2696 | __ ldr(r1, frame_->Function()); |
| 2697 | |
| 2698 | // Load the literals array of the function. |
| 2699 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2700 | |
| 2701 | // Load the literal at the ast saved index. |
| 2702 | int literal_offset = |
| 2703 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2704 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2705 | |
| 2706 | // Check whether we need to materialize the object literal boilerplate. |
| 2707 | // If so, jump to the deferred code. |
| 2708 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2709 | deferred->Branch(eq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2710 | deferred->BindExit(); |
| 2711 | |
| 2712 | // Push the object literal boilerplate. |
| 2713 | frame_->EmitPush(r2); |
| 2714 | |
| 2715 | // Clone the boilerplate object. |
| 2716 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2717 | if (node->depth() == 1) { |
| 2718 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2719 | } |
| 2720 | frame_->CallRuntime(clone_function_id, 1); |
| 2721 | frame_->EmitPush(r0); // save the result |
| 2722 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2723 | |
| 2724 | // Generate code to set the elements in the array that are not |
| 2725 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2726 | for (int i = 0; i < node->values()->length(); i++) { |
| 2727 | Expression* value = node->values()->at(i); |
| 2728 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2729 | // If value is a literal the property value is already set in the |
| 2730 | // boilerplate object. |
| 2731 | if (value->AsLiteral() != NULL) continue; |
| 2732 | // If value is a materialized literal the property value is already set |
| 2733 | // in the boilerplate object if it is simple. |
| 2734 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2735 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2736 | // The property must be set by generated code. |
| 2737 | LoadAndSpill(value); |
| 2738 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2739 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2740 | // Fetch the object literal. |
| 2741 | __ ldr(r1, frame_->Top()); |
| 2742 | // Get the elements array. |
| 2743 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2744 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2745 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2746 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2747 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2748 | |
| 2749 | // Update the write barrier for the array address. |
| 2750 | __ mov(r3, Operand(offset)); |
| 2751 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2752 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2753 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2757 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2758 | #ifdef DEBUG |
| 2759 | int original_height = frame_->height(); |
| 2760 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2761 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2762 | // Call runtime routine to allocate the catch extension object and |
| 2763 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2764 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2765 | LoadAndSpill(node->key()); |
| 2766 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 2767 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2768 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2769 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2773 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2774 | #ifdef DEBUG |
| 2775 | int original_height = frame_->height(); |
| 2776 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2777 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2778 | Comment cmnt(masm_, "[ Assignment"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2779 | CodeForStatementPosition(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2780 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2781 | { Reference target(this, node->target()); |
| 2782 | if (target.is_illegal()) { |
| 2783 | // Fool the virtual frame into thinking that we left the assignment's |
| 2784 | // value on the frame. |
| 2785 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2786 | frame_->EmitPush(r0); |
| 2787 | ASSERT(frame_->height() == original_height + 1); |
| 2788 | return; |
| 2789 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2790 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2791 | if (node->op() == Token::ASSIGN || |
| 2792 | node->op() == Token::INIT_VAR || |
| 2793 | node->op() == Token::INIT_CONST) { |
| 2794 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2795 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2796 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2797 | // +=, *= and similar binary assignments. |
| 2798 | // Get the old value of the lhs. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2799 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 2800 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2801 | bool overwrite = |
| 2802 | (node->value()->AsBinaryOperation() != NULL && |
| 2803 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2804 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2805 | SmiOperation(node->binary_op(), |
| 2806 | literal->handle(), |
| 2807 | false, |
| 2808 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2809 | frame_->EmitPush(r0); |
| 2810 | |
| 2811 | } else { |
| 2812 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2813 | GenericBinaryOperation(node->binary_op(), |
| 2814 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2815 | frame_->EmitPush(r0); |
| 2816 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2817 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2818 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2819 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2820 | if (var != NULL && |
| 2821 | (var->mode() == Variable::CONST) && |
| 2822 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2823 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2824 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2825 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2826 | CodeForSourcePosition(node->position()); |
| 2827 | if (node->op() == Token::INIT_CONST) { |
| 2828 | // Dynamic constant initializations must use the function context |
| 2829 | // and initialize the actual constant declared. Dynamic variable |
| 2830 | // initializations are simply assignments and use SetValue. |
| 2831 | target.SetValue(CONST_INIT); |
| 2832 | } else { |
| 2833 | target.SetValue(NOT_CONST_INIT); |
| 2834 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2835 | } |
| 2836 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2837 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2841 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2842 | #ifdef DEBUG |
| 2843 | int original_height = frame_->height(); |
| 2844 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2845 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2846 | Comment cmnt(masm_, "[ Throw"); |
| 2847 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2848 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2849 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2850 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2851 | frame_->EmitPush(r0); |
| 2852 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2856 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2857 | #ifdef DEBUG |
| 2858 | int original_height = frame_->height(); |
| 2859 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2860 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2861 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2862 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2863 | { Reference property(this, node); |
| 2864 | property.GetValueAndSpill(typeof_state()); |
| 2865 | } |
| 2866 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2870 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2871 | #ifdef DEBUG |
| 2872 | int original_height = frame_->height(); |
| 2873 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2874 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2875 | Comment cmnt(masm_, "[ Call"); |
| 2876 | |
| 2877 | ZoneList<Expression*>* args = node->arguments(); |
| 2878 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2879 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2880 | // Standard function call. |
| 2881 | |
| 2882 | // Check if the function is a variable or a property. |
| 2883 | Expression* function = node->expression(); |
| 2884 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2885 | Property* property = function->AsProperty(); |
| 2886 | |
| 2887 | // ------------------------------------------------------------------------ |
| 2888 | // Fast-case: Use inline caching. |
| 2889 | // --- |
| 2890 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2891 | // must be resolved after the arguments have been evaluated. The IC code |
| 2892 | // automatically handles this by loading the arguments before the function |
| 2893 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2894 | // ------------------------------------------------------------------------ |
| 2895 | |
| 2896 | if (var != NULL && !var->is_this() && var->is_global()) { |
| 2897 | // ---------------------------------- |
| 2898 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 2899 | // ---------------------------------- |
| 2900 | |
| 2901 | // 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] | 2902 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2903 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2904 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2905 | // Pass the global object as the receiver and let the IC stub |
| 2906 | // patch the stack to use the global proxy as 'this' in the |
| 2907 | // invoked function. |
| 2908 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2909 | |
| 2910 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2911 | int arg_count = args->length(); |
| 2912 | for (int i = 0; i < arg_count; i++) { |
| 2913 | LoadAndSpill(args->at(i)); |
| 2914 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2915 | |
| 2916 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2917 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 2918 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2919 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2920 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 2921 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2922 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2923 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2924 | frame_->Drop(); |
| 2925 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2926 | |
| 2927 | } else if (var != NULL && var->slot() != NULL && |
| 2928 | var->slot()->type() == Slot::LOOKUP) { |
| 2929 | // ---------------------------------- |
| 2930 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 2931 | // ---------------------------------- |
| 2932 | |
| 2933 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2934 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2935 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2936 | frame_->EmitPush(r0); |
| 2937 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2938 | // r0: slot value; r1: receiver |
| 2939 | |
| 2940 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2941 | frame_->EmitPush(r0); // function |
| 2942 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2943 | |
| 2944 | // Call the function. |
| 2945 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2946 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2947 | |
| 2948 | } else if (property != NULL) { |
| 2949 | // Check if the key is a literal string. |
| 2950 | Literal* literal = property->key()->AsLiteral(); |
| 2951 | |
| 2952 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 2953 | // ------------------------------------------------------------------ |
| 2954 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 2955 | // ------------------------------------------------------------------ |
| 2956 | |
| 2957 | // 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] | 2958 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2959 | frame_->EmitPush(r0); |
| 2960 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2961 | |
| 2962 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2963 | int arg_count = args->length(); |
| 2964 | for (int i = 0; i < arg_count; i++) { |
| 2965 | LoadAndSpill(args->at(i)); |
| 2966 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2967 | |
| 2968 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2969 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 2970 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2971 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2972 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2973 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2974 | |
| 2975 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2976 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2977 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2978 | 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] | 2979 | |
| 2980 | } else { |
| 2981 | // ------------------------------------------- |
| 2982 | // JavaScript example: 'array[index](1, 2, 3)' |
| 2983 | // ------------------------------------------- |
| 2984 | |
| 2985 | // Load the function to call from the property through a reference. |
| 2986 | Reference ref(this, property); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2987 | ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2988 | |
| 2989 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2990 | if (property->is_synthetic()) { |
| 2991 | LoadGlobalReceiver(r0); |
| 2992 | } else { |
| 2993 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 2994 | frame_->EmitPush(r0); |
| 2995 | } |
| 2996 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2997 | // Call the function. |
| 2998 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2999 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | } else { |
| 3003 | // ---------------------------------- |
| 3004 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3005 | // ---------------------------------- |
| 3006 | |
| 3007 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3008 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3009 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3010 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3011 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3012 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3013 | // Call the function. |
| 3014 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3015 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3016 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3017 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3021 | void CodeGenerator::VisitCallEval(CallEval* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3022 | #ifdef DEBUG |
| 3023 | int original_height = frame_->height(); |
| 3024 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3025 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3026 | Comment cmnt(masm_, "[ CallEval"); |
| 3027 | |
| 3028 | // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve |
| 3029 | // the function we need to call and the receiver of the call. |
| 3030 | // Then we call the resolved function using the given arguments. |
| 3031 | |
| 3032 | ZoneList<Expression*>* args = node->arguments(); |
| 3033 | Expression* function = node->expression(); |
| 3034 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3035 | CodeForStatementPosition(node); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3036 | |
| 3037 | // Prepare stack for call to resolved function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3038 | LoadAndSpill(function); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3039 | __ mov(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3040 | frame_->EmitPush(r2); // Slot for receiver |
| 3041 | int arg_count = args->length(); |
| 3042 | for (int i = 0; i < arg_count; i++) { |
| 3043 | LoadAndSpill(args->at(i)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | // Prepare stack for call to ResolvePossiblyDirectEval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3047 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3048 | frame_->EmitPush(r1); |
| 3049 | if (arg_count > 0) { |
| 3050 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3051 | frame_->EmitPush(r1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3052 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3053 | frame_->EmitPush(r2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | // Resolve the call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3057 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3058 | |
| 3059 | // Touch up stack with the right values for the function and the receiver. |
| 3060 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3061 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3062 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3063 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3064 | |
| 3065 | // Call the function. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3066 | CodeForSourcePosition(node->position()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3067 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3068 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3069 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3070 | frame_->CallStub(&call_function, arg_count + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3071 | |
| 3072 | __ ldr(cp, frame_->Context()); |
| 3073 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3074 | frame_->Drop(); |
| 3075 | frame_->EmitPush(r0); |
| 3076 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3080 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3081 | #ifdef DEBUG |
| 3082 | int original_height = frame_->height(); |
| 3083 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3084 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3085 | Comment cmnt(masm_, "[ CallNew"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3086 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3087 | |
| 3088 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3089 | // expression in new calls must be evaluated before the |
| 3090 | // arguments. This is different from ordinary calls, where the |
| 3091 | // actual function to call is resolved after the arguments have been |
| 3092 | // evaluated. |
| 3093 | |
| 3094 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3095 | // receiver. There is no need to use the global proxy here because |
| 3096 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3097 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3098 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3099 | |
| 3100 | // Push the arguments ("left-to-right") on the stack. |
| 3101 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3102 | int arg_count = args->length(); |
| 3103 | for (int i = 0; i < arg_count; i++) { |
| 3104 | LoadAndSpill(args->at(i)); |
| 3105 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3106 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3107 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 3108 | Result num_args(r0); |
| 3109 | __ mov(r0, Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3110 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3111 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 3112 | Result function(r1); |
| 3113 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3114 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3115 | // Call the construct call builtin that handles allocation and |
| 3116 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3117 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3118 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 3119 | frame_->CallCodeObject(ic, |
| 3120 | RelocInfo::CONSTRUCT_CALL, |
| 3121 | &num_args, |
| 3122 | &function, |
| 3123 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3124 | |
| 3125 | // 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] | 3126 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3127 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3131 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3132 | VirtualFrame::SpilledScope spilled_scope; |
| 3133 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3134 | JumpTarget leave, null, function, non_function_constructor; |
| 3135 | |
| 3136 | // Load the object into r0. |
| 3137 | LoadAndSpill(args->at(0)); |
| 3138 | frame_->EmitPop(r0); |
| 3139 | |
| 3140 | // If the object is a smi, we return null. |
| 3141 | __ tst(r0, Operand(kSmiTagMask)); |
| 3142 | null.Branch(eq); |
| 3143 | |
| 3144 | // Check that the object is a JS object but take special care of JS |
| 3145 | // functions to make sure they have 'Function' as their class. |
| 3146 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3147 | null.Branch(lt); |
| 3148 | |
| 3149 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3150 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3151 | // LAST_JS_OBJECT_TYPE. |
| 3152 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3153 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3154 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3155 | function.Branch(eq); |
| 3156 | |
| 3157 | // Check if the constructor in the map is a function. |
| 3158 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3159 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3160 | non_function_constructor.Branch(ne); |
| 3161 | |
| 3162 | // The r0 register now contains the constructor function. Grab the |
| 3163 | // instance class name from there. |
| 3164 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3165 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3166 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3167 | leave.Jump(); |
| 3168 | |
| 3169 | // Functions have class 'Function'. |
| 3170 | function.Bind(); |
| 3171 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3172 | frame_->EmitPush(r0); |
| 3173 | leave.Jump(); |
| 3174 | |
| 3175 | // Objects with a non-function constructor have class 'Object'. |
| 3176 | non_function_constructor.Bind(); |
| 3177 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3178 | frame_->EmitPush(r0); |
| 3179 | leave.Jump(); |
| 3180 | |
| 3181 | // Non-JS objects have class null. |
| 3182 | null.Bind(); |
| 3183 | __ mov(r0, Operand(Factory::null_value())); |
| 3184 | frame_->EmitPush(r0); |
| 3185 | |
| 3186 | // All done. |
| 3187 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3191 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3192 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3193 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3194 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3195 | LoadAndSpill(args->at(0)); |
| 3196 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3197 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3198 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3199 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3200 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3201 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3202 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3203 | // Load the value. |
| 3204 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3205 | leave.Bind(); |
| 3206 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3210 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3211 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3212 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3213 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3214 | LoadAndSpill(args->at(0)); // Load the object. |
| 3215 | LoadAndSpill(args->at(1)); // Load the value. |
| 3216 | frame_->EmitPop(r0); // r0 contains value |
| 3217 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3218 | // if (object->IsSmi()) return object. |
| 3219 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3220 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3221 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3222 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3223 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3224 | // Store the value. |
| 3225 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3226 | // Update the write barrier. |
| 3227 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3228 | __ RecordWrite(r1, r2, r3); |
| 3229 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3230 | leave.Bind(); |
| 3231 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3235 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3236 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3237 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3238 | LoadAndSpill(args->at(0)); |
| 3239 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3240 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3241 | cc_reg_ = eq; |
| 3242 | } |
| 3243 | |
| 3244 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3245 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3246 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3247 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3248 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3249 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3250 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3251 | LoadAndSpill(args->at(1)); |
| 3252 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3253 | __ CallRuntime(Runtime::kLog, 2); |
| 3254 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3255 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3256 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3257 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3261 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3262 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3263 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3264 | LoadAndSpill(args->at(0)); |
| 3265 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3266 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3267 | cc_reg_ = eq; |
| 3268 | } |
| 3269 | |
| 3270 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3271 | // This should generate code that performs a charCodeAt() call or returns |
| 3272 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3273 | // 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] | 3274 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3275 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3276 | ASSERT(args->length() == 2); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3277 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3278 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3282 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3283 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3284 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3285 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3286 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3287 | // We need the CC bits to come out as not_equal in the case where the |
| 3288 | // object is a smi. This can't be done with the usual test opcode so |
| 3289 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3290 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3291 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3292 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3293 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3294 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3295 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3296 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3297 | cc_reg_ = eq; |
| 3298 | } |
| 3299 | |
| 3300 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3301 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3302 | VirtualFrame::SpilledScope spilled_scope; |
| 3303 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3304 | |
| 3305 | // Get the frame pointer for the calling frame. |
| 3306 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3307 | |
| 3308 | // Skip the arguments adaptor frame if it exists. |
| 3309 | Label check_frame_marker; |
| 3310 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 3311 | __ cmp(r1, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 3312 | __ b(ne, &check_frame_marker); |
| 3313 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3314 | |
| 3315 | // Check the marker in the calling frame. |
| 3316 | __ bind(&check_frame_marker); |
| 3317 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3318 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3319 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3323 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3324 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3325 | ASSERT(args->length() == 0); |
| 3326 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3327 | // Seed the result with the formal parameters count, which will be used |
| 3328 | // 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] | 3329 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3330 | |
| 3331 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3332 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3333 | frame_->CallStub(&stub, 0); |
| 3334 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3338 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3339 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3340 | ASSERT(args->length() == 1); |
| 3341 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3342 | // Satisfy contract with ArgumentsAccessStub: |
| 3343 | // 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] | 3344 | LoadAndSpill(args->at(0)); |
| 3345 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3346 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3347 | |
| 3348 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3349 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3350 | frame_->CallStub(&stub, 0); |
| 3351 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3355 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3356 | VirtualFrame::SpilledScope spilled_scope; |
| 3357 | ASSERT(args->length() == 0); |
| 3358 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3359 | RelocInfo::RUNTIME_ENTRY); |
| 3360 | frame_->EmitPush(r0); |
| 3361 | } |
| 3362 | |
| 3363 | |
| 3364 | void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) { |
| 3365 | VirtualFrame::SpilledScope spilled_scope; |
| 3366 | LoadAndSpill(args->at(0)); |
| 3367 | switch (op) { |
| 3368 | case SIN: |
| 3369 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 3370 | break; |
| 3371 | case COS: |
| 3372 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 3373 | break; |
| 3374 | } |
| 3375 | frame_->EmitPush(r0); |
| 3376 | } |
| 3377 | |
| 3378 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3379 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3380 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3381 | ASSERT(args->length() == 2); |
| 3382 | |
| 3383 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3384 | LoadAndSpill(args->at(0)); |
| 3385 | LoadAndSpill(args->at(1)); |
| 3386 | frame_->EmitPop(r0); |
| 3387 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3388 | __ cmp(r0, Operand(r1)); |
| 3389 | cc_reg_ = eq; |
| 3390 | } |
| 3391 | |
| 3392 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3393 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3394 | #ifdef DEBUG |
| 3395 | int original_height = frame_->height(); |
| 3396 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3397 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3398 | if (CheckForInlineRuntimeCall(node)) { |
| 3399 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3400 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3401 | return; |
| 3402 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3403 | |
| 3404 | ZoneList<Expression*>* args = node->arguments(); |
| 3405 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3406 | Runtime::Function* function = node->function(); |
| 3407 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3408 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3409 | // Prepare stack for calling JS runtime function. |
| 3410 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3411 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3412 | // Push the builtins object found in the current global object. |
| 3413 | __ ldr(r1, GlobalObject()); |
| 3414 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3415 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3416 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3417 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3418 | // Push the arguments ("left-to-right"). |
| 3419 | int arg_count = args->length(); |
| 3420 | for (int i = 0; i < arg_count; i++) { |
| 3421 | LoadAndSpill(args->at(i)); |
| 3422 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3423 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3424 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3425 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3426 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3427 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3428 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3429 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3430 | frame_->Drop(); |
| 3431 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3432 | } else { |
| 3433 | // Call the C runtime function. |
| 3434 | frame_->CallRuntime(function, arg_count); |
| 3435 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3436 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3437 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3441 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3442 | #ifdef DEBUG |
| 3443 | int original_height = frame_->height(); |
| 3444 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3445 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3446 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3447 | |
| 3448 | Token::Value op = node->op(); |
| 3449 | |
| 3450 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3451 | LoadConditionAndSpill(node->expression(), |
| 3452 | NOT_INSIDE_TYPEOF, |
| 3453 | false_target(), |
| 3454 | true_target(), |
| 3455 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3456 | // LoadCondition may (and usually does) leave a test and branch to |
| 3457 | // be emitted by the caller. In that case, negate the condition. |
| 3458 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3459 | |
| 3460 | } else if (op == Token::DELETE) { |
| 3461 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3462 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3463 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3464 | LoadAndSpill(property->obj()); |
| 3465 | LoadAndSpill(property->key()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 3466 | Result arg_count(r0); |
| 3467 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3468 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3469 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3470 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3471 | Slot* slot = variable->slot(); |
| 3472 | if (variable->is_global()) { |
| 3473 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3474 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3475 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 3476 | Result arg_count(r0); |
| 3477 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3478 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3479 | |
| 3480 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3481 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3482 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3483 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3484 | frame_->EmitPush(r0); |
| 3485 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3486 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3487 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3488 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3489 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 3490 | Result arg_count(r0); |
| 3491 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3492 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3493 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3494 | } else { |
| 3495 | // Default: Result of deleting non-global, not dynamically |
| 3496 | // introduced variables is false. |
| 3497 | __ mov(r0, Operand(Factory::false_value())); |
| 3498 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3499 | |
| 3500 | } else { |
| 3501 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3502 | LoadAndSpill(node->expression()); // may have side-effects |
| 3503 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3504 | __ mov(r0, Operand(Factory::true_value())); |
| 3505 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3506 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3507 | |
| 3508 | } else if (op == Token::TYPEOF) { |
| 3509 | // Special case for loading the typeof expression; see comment on |
| 3510 | // LoadTypeofExpression(). |
| 3511 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3512 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3513 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3514 | |
| 3515 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3516 | LoadAndSpill(node->expression()); |
| 3517 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3518 | switch (op) { |
| 3519 | case Token::NOT: |
| 3520 | case Token::DELETE: |
| 3521 | case Token::TYPEOF: |
| 3522 | UNREACHABLE(); // handled above |
| 3523 | break; |
| 3524 | |
| 3525 | case Token::SUB: { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3526 | bool overwrite = |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3527 | (node->expression()->AsBinaryOperation() != NULL && |
| 3528 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3529 | UnarySubStub stub(overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3530 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3531 | break; |
| 3532 | } |
| 3533 | |
| 3534 | case Token::BIT_NOT: { |
| 3535 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3536 | JumpTarget smi_label; |
| 3537 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3538 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3539 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3540 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3541 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 3542 | Result arg_count(r0); |
| 3543 | __ mov(r0, Operand(0)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3544 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3545 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3546 | continue_label.Jump(); |
| 3547 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3548 | __ mvn(r0, Operand(r0)); |
| 3549 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3550 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3551 | break; |
| 3552 | } |
| 3553 | |
| 3554 | case Token::VOID: |
| 3555 | // since the stack top is cached in r0, popping and then |
| 3556 | // pushing a value can be done by just writing to r0. |
| 3557 | __ mov(r0, Operand(Factory::undefined_value())); |
| 3558 | break; |
| 3559 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3560 | case Token::ADD: { |
| 3561 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3562 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3563 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3564 | continue_label.Branch(eq); |
| 3565 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 3566 | Result arg_count(r0); |
| 3567 | __ mov(r0, Operand(0)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3568 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3569 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3570 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3571 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3572 | default: |
| 3573 | UNREACHABLE(); |
| 3574 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3575 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3576 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3577 | ASSERT(!has_valid_frame() || |
| 3578 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3579 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3583 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3584 | #ifdef DEBUG |
| 3585 | int original_height = frame_->height(); |
| 3586 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3587 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3588 | Comment cmnt(masm_, "[ CountOperation"); |
| 3589 | |
| 3590 | bool is_postfix = node->is_postfix(); |
| 3591 | bool is_increment = node->op() == Token::INC; |
| 3592 | |
| 3593 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3594 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3595 | |
| 3596 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3597 | if (is_postfix) { |
| 3598 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3599 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3600 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3601 | |
| 3602 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3603 | if (target.is_illegal()) { |
| 3604 | // Spoof the virtual frame to have the expected height (one higher |
| 3605 | // than on entry). |
| 3606 | if (!is_postfix) { |
| 3607 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3608 | frame_->EmitPush(r0); |
| 3609 | } |
| 3610 | ASSERT(frame_->height() == original_height + 1); |
| 3611 | return; |
| 3612 | } |
| 3613 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 3614 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3615 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3616 | JumpTarget slow; |
| 3617 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3618 | |
| 3619 | // Load the value (1) into register r1. |
| 3620 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3621 | |
| 3622 | // Check for smi operand. |
| 3623 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3624 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3625 | |
| 3626 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3627 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3628 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3629 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3630 | |
| 3631 | // Perform optimistic increment/decrement. |
| 3632 | if (is_increment) { |
| 3633 | __ add(r0, r0, Operand(r1), SetCC); |
| 3634 | } else { |
| 3635 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3636 | } |
| 3637 | |
| 3638 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3639 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3640 | |
| 3641 | // Revert optimistic increment/decrement. |
| 3642 | if (is_increment) { |
| 3643 | __ sub(r0, r0, Operand(r1)); |
| 3644 | } else { |
| 3645 | __ add(r0, r0, Operand(r1)); |
| 3646 | } |
| 3647 | |
| 3648 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3649 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3650 | { |
| 3651 | // Convert the operand to a number. |
| 3652 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 3653 | Result arg_count(r0); |
| 3654 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3655 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3656 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3657 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3658 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3659 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3662 | // Compute the new value. |
| 3663 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3664 | frame_->EmitPush(r0); |
| 3665 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3666 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3667 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3668 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3669 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3673 | exit.Bind(); |
| 3674 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3675 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3679 | if (is_postfix) frame_->EmitPop(r0); |
| 3680 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3684 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3685 | #ifdef DEBUG |
| 3686 | int original_height = frame_->height(); |
| 3687 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3688 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3689 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3690 | Token::Value op = node->op(); |
| 3691 | |
| 3692 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3693 | // operators must yield the result of one of the two expressions |
| 3694 | // before any ToBoolean() conversions. This means that the value |
| 3695 | // produced by a && or || operator is not necessarily a boolean. |
| 3696 | |
| 3697 | // NOTE: If the left hand side produces a materialized value (not in |
| 3698 | // the CC register), we force the right hand side to do the |
| 3699 | // same. This is necessary because we may have to branch to the exit |
| 3700 | // after evaluating the left hand side (due to the shortcut |
| 3701 | // semantics), but the compiler must (statically) know if the result |
| 3702 | // of compiling the binary operation is materialized or not. |
| 3703 | |
| 3704 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3705 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3706 | LoadConditionAndSpill(node->left(), |
| 3707 | NOT_INSIDE_TYPEOF, |
| 3708 | &is_true, |
| 3709 | false_target(), |
| 3710 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3711 | if (has_valid_frame() && !has_cc()) { |
| 3712 | // The left-hand side result is on top of the virtual frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3713 | JumpTarget pop_and_continue; |
| 3714 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3715 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3716 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3717 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3718 | // Avoid popping the result if it converts to 'false' using the |
| 3719 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3720 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3721 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3722 | Branch(false, &exit); |
| 3723 | |
| 3724 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3725 | pop_and_continue.Bind(); |
| 3726 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3727 | |
| 3728 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3729 | is_true.Bind(); |
| 3730 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3731 | |
| 3732 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3733 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3734 | } else if (has_cc() || is_true.is_linked()) { |
| 3735 | // The left-hand side is either (a) partially compiled to |
| 3736 | // control flow with a final branch left to emit or (b) fully |
| 3737 | // compiled to control flow and possibly true. |
| 3738 | if (has_cc()) { |
| 3739 | Branch(false, false_target()); |
| 3740 | } |
| 3741 | is_true.Bind(); |
| 3742 | LoadConditionAndSpill(node->right(), |
| 3743 | NOT_INSIDE_TYPEOF, |
| 3744 | true_target(), |
| 3745 | false_target(), |
| 3746 | false); |
| 3747 | } else { |
| 3748 | // Nothing to do. |
| 3749 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3753 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3754 | LoadConditionAndSpill(node->left(), |
| 3755 | NOT_INSIDE_TYPEOF, |
| 3756 | true_target(), |
| 3757 | &is_false, |
| 3758 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3759 | if (has_valid_frame() && !has_cc()) { |
| 3760 | // The left-hand side result is on top of the virtual frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3761 | JumpTarget pop_and_continue; |
| 3762 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3763 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3764 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3765 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3766 | // Avoid popping the result if it converts to 'true' using the |
| 3767 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3768 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3769 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3770 | Branch(true, &exit); |
| 3771 | |
| 3772 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3773 | pop_and_continue.Bind(); |
| 3774 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3775 | |
| 3776 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3777 | is_false.Bind(); |
| 3778 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3779 | |
| 3780 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3781 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3782 | } else if (has_cc() || is_false.is_linked()) { |
| 3783 | // The left-hand side is either (a) partially compiled to |
| 3784 | // control flow with a final branch left to emit or (b) fully |
| 3785 | // compiled to control flow and possibly false. |
| 3786 | if (has_cc()) { |
| 3787 | Branch(true, true_target()); |
| 3788 | } |
| 3789 | is_false.Bind(); |
| 3790 | LoadConditionAndSpill(node->right(), |
| 3791 | NOT_INSIDE_TYPEOF, |
| 3792 | true_target(), |
| 3793 | false_target(), |
| 3794 | false); |
| 3795 | } else { |
| 3796 | // Nothing to do. |
| 3797 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | } else { |
| 3801 | // Optimize for the case where (at least) one of the expressions |
| 3802 | // is a literal small integer. |
| 3803 | Literal* lliteral = node->left()->AsLiteral(); |
| 3804 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3805 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 3806 | // never return a constant/immutable object. |
| 3807 | bool overwrite_left = |
| 3808 | (node->left()->AsBinaryOperation() != NULL && |
| 3809 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3810 | bool overwrite_right = |
| 3811 | (node->right()->AsBinaryOperation() != NULL && |
| 3812 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3813 | |
| 3814 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3815 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3816 | SmiOperation(node->op(), |
| 3817 | rliteral->handle(), |
| 3818 | false, |
| 3819 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3820 | |
| 3821 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3822 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3823 | SmiOperation(node->op(), |
| 3824 | lliteral->handle(), |
| 3825 | true, |
| 3826 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3827 | |
| 3828 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3829 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 3830 | if (overwrite_left) { |
| 3831 | overwrite_mode = OVERWRITE_LEFT; |
| 3832 | } else if (overwrite_right) { |
| 3833 | overwrite_mode = OVERWRITE_RIGHT; |
| 3834 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3835 | LoadAndSpill(node->left()); |
| 3836 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3837 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3838 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3839 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3840 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3841 | ASSERT(!has_valid_frame() || |
| 3842 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3843 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3847 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3848 | #ifdef DEBUG |
| 3849 | int original_height = frame_->height(); |
| 3850 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3851 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3852 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3853 | frame_->EmitPush(r0); |
| 3854 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3858 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3859 | #ifdef DEBUG |
| 3860 | int original_height = frame_->height(); |
| 3861 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3862 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3863 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3864 | |
| 3865 | // Get the expressions from the node. |
| 3866 | Expression* left = node->left(); |
| 3867 | Expression* right = node->right(); |
| 3868 | Token::Value op = node->op(); |
| 3869 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3870 | // To make null checks efficient, we check if either left or right is the |
| 3871 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3872 | // instead of calling the (very) general runtime routine for checking |
| 3873 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3874 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3875 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3876 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3877 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3878 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 3879 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3880 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3881 | LoadAndSpill(left_is_null ? right : left); |
| 3882 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3883 | __ cmp(r0, Operand(Factory::null_value())); |
| 3884 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3885 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 3886 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3887 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3888 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3889 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3890 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3891 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3892 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3893 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3894 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3895 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3896 | // It can be an undetectable object. |
| 3897 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3898 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 3899 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 3900 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3903 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3904 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3905 | return; |
| 3906 | } |
| 3907 | } |
| 3908 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3909 | // To make typeof testing for natives implemented in JavaScript really |
| 3910 | // efficient, we generate special code for expressions of the form: |
| 3911 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3912 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 3913 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 3914 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 3915 | (right->AsLiteral() != NULL && |
| 3916 | right->AsLiteral()->handle()->IsString())) { |
| 3917 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 3918 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3919 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3920 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3921 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3922 | |
| 3923 | if (check->Equals(Heap::number_symbol())) { |
| 3924 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3925 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3926 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3927 | __ cmp(r1, Operand(Factory::heap_number_map())); |
| 3928 | cc_reg_ = eq; |
| 3929 | |
| 3930 | } else if (check->Equals(Heap::string_symbol())) { |
| 3931 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3932 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3933 | |
| 3934 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3935 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3936 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3937 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3938 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3939 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3940 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3941 | |
| 3942 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3943 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 3944 | cc_reg_ = lt; |
| 3945 | |
| 3946 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 3947 | __ cmp(r1, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3948 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3949 | __ cmp(r1, Operand(Factory::false_value())); |
| 3950 | cc_reg_ = eq; |
| 3951 | |
| 3952 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 3953 | __ cmp(r1, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3954 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3955 | |
| 3956 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3957 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3958 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3959 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3960 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3961 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3962 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3963 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 3964 | |
| 3965 | cc_reg_ = eq; |
| 3966 | |
| 3967 | } else if (check->Equals(Heap::function_symbol())) { |
| 3968 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3969 | false_target()->Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3970 | __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3971 | cc_reg_ = eq; |
| 3972 | |
| 3973 | } else if (check->Equals(Heap::object_symbol())) { |
| 3974 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3975 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3976 | |
| 3977 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3978 | __ cmp(r1, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3979 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3980 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3981 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3982 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 3983 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3984 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3985 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3986 | |
| 3987 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3988 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3989 | false_target()->Branch(lt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3990 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 3991 | cc_reg_ = le; |
| 3992 | |
| 3993 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3994 | // Uncommon case: typeof testing against a string literal that is |
| 3995 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3996 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3997 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3998 | ASSERT(!has_valid_frame() || |
| 3999 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4000 | return; |
| 4001 | } |
| 4002 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4003 | switch (op) { |
| 4004 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4005 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4006 | break; |
| 4007 | |
| 4008 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4009 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4010 | break; |
| 4011 | |
| 4012 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4013 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4014 | break; |
| 4015 | |
| 4016 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4017 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4018 | break; |
| 4019 | |
| 4020 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4021 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4022 | break; |
| 4023 | |
| 4024 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4025 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4026 | break; |
| 4027 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4028 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4029 | LoadAndSpill(left); |
| 4030 | LoadAndSpill(right); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 4031 | Result arg_count(r0); |
| 4032 | __ mov(r0, Operand(1)); // not counting receiver |
| 4033 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, &arg_count, 2); |
| 4034 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4035 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4036 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4037 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4038 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4039 | LoadAndSpill(left); |
| 4040 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4041 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 4042 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4043 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 4044 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4045 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4046 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4047 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4048 | |
| 4049 | default: |
| 4050 | UNREACHABLE(); |
| 4051 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4052 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4053 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4057 | #ifdef DEBUG |
| 4058 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4059 | #endif |
| 4060 | |
| 4061 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4062 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4063 | #define __ ACCESS_MASM(masm) |
| 4064 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4065 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4066 | Handle<String> Reference::GetName() { |
| 4067 | ASSERT(type_ == NAMED); |
| 4068 | Property* property = expression_->AsProperty(); |
| 4069 | if (property == NULL) { |
| 4070 | // Global variable reference treated as a named property reference. |
| 4071 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4072 | ASSERT(proxy->AsVariable() != NULL); |
| 4073 | ASSERT(proxy->AsVariable()->is_global()); |
| 4074 | return proxy->name(); |
| 4075 | } else { |
| 4076 | Literal* raw_name = property->key()->AsLiteral(); |
| 4077 | ASSERT(raw_name != NULL); |
| 4078 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4079 | } |
| 4080 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4081 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4082 | |
| 4083 | void Reference::GetValue(TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4084 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4085 | ASSERT(!is_illegal()); |
| 4086 | ASSERT(!cgen_->has_cc()); |
| 4087 | MacroAssembler* masm = cgen_->masm(); |
| 4088 | Property* property = expression_->AsProperty(); |
| 4089 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4090 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | switch (type_) { |
| 4094 | case SLOT: { |
| 4095 | Comment cmnt(masm, "[ Load from Slot"); |
| 4096 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4097 | ASSERT(slot != NULL); |
| 4098 | cgen_->LoadFromSlot(slot, typeof_state); |
| 4099 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4102 | case NAMED: { |
| 4103 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4104 | // distinction between expressions in a typeof and not in a typeof. If |
| 4105 | // there is a chance that reference errors can be thrown below, we |
| 4106 | // must distinguish between the two kinds of loads (typeof expression |
| 4107 | // loads must not throw a reference error). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4108 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4109 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4110 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4111 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4112 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4113 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 4114 | Result name_reg(r2); |
| 4115 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4116 | ASSERT(var == NULL || var->is_global()); |
| 4117 | RelocInfo::Mode rmode = (var == NULL) |
| 4118 | ? RelocInfo::CODE_TARGET |
| 4119 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 4120 | frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4121 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4122 | break; |
| 4123 | } |
| 4124 | |
| 4125 | case KEYED: { |
| 4126 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4127 | // 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] | 4128 | |
| 4129 | // TODO(181): Implement inlined version of array indexing once |
| 4130 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4131 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4132 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4133 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4134 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4135 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4136 | ASSERT(var == NULL || var->is_global()); |
| 4137 | RelocInfo::Mode rmode = (var == NULL) |
| 4138 | ? RelocInfo::CODE_TARGET |
| 4139 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 4140 | frame->CallCodeObject(ic, rmode, 0); |
| 4141 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4142 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | default: |
| 4146 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4147 | } |
| 4148 | } |
| 4149 | |
| 4150 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4151 | void Reference::SetValue(InitState init_state) { |
| 4152 | ASSERT(!is_illegal()); |
| 4153 | ASSERT(!cgen_->has_cc()); |
| 4154 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4155 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4156 | Property* property = expression_->AsProperty(); |
| 4157 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4158 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4159 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4160 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4161 | switch (type_) { |
| 4162 | case SLOT: { |
| 4163 | Comment cmnt(masm, "[ Store to Slot"); |
| 4164 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4165 | ASSERT(slot != NULL); |
| 4166 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4167 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4168 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4169 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4170 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4171 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4172 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4173 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4174 | if (init_state == CONST_INIT) { |
| 4175 | // Same as the case for a normal store, but ignores attribute |
| 4176 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4177 | // const properties (introduced via eval("const foo = (some |
| 4178 | // expr);")). Also, uses the current function context instead of |
| 4179 | // the top context. |
| 4180 | // |
| 4181 | // Note that we must declare the foo upon entry of eval(), via a |
| 4182 | // context slot declaration, but we cannot initialize it at the |
| 4183 | // same time, because the const declaration may be at the end of |
| 4184 | // the eval code (sigh...) and the const variable may have been |
| 4185 | // used before (where its value is 'undefined'). Thus, we can only |
| 4186 | // do the initialization when we actually encounter the expression |
| 4187 | // and when the expression operands are defined and valid, and |
| 4188 | // thus we need the split into 2 operations: declaration of the |
| 4189 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4190 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4191 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4192 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4193 | } |
| 4194 | // Storing a variable must keep the (new) value on the expression |
| 4195 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4196 | frame->EmitPush(r0); |
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 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4199 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4200 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4201 | JumpTarget exit; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4202 | if (init_state == CONST_INIT) { |
| 4203 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4204 | // Only the first const initialization must be executed (the slot |
| 4205 | // still contains 'the hole' value). When the assignment is |
| 4206 | // executed, the code is identical to a normal store (see below). |
| 4207 | Comment cmnt(masm, "[ Init const"); |
| 4208 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
| 4209 | __ cmp(r2, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4210 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4211 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4212 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4213 | // We must execute the store. Storing a variable must keep the |
| 4214 | // (new) value on the stack. This is necessary for compiling |
| 4215 | // assignment expressions. |
| 4216 | // |
| 4217 | // Note: We will reach here even with slot->var()->mode() == |
| 4218 | // Variable::CONST because of const declarations which will |
| 4219 | // initialize consts to 'the hole' value and by doing so, end up |
| 4220 | // calling this code. r2 may be loaded with context; used below in |
| 4221 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4222 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4223 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4224 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4225 | if (slot->type() == Slot::CONTEXT) { |
| 4226 | // Skip write barrier if the written value is a smi. |
| 4227 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4228 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4229 | // r2 is loaded with context when calling SlotOperand above. |
| 4230 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4231 | __ mov(r3, Operand(offset)); |
| 4232 | __ RecordWrite(r2, r3, r1); |
| 4233 | } |
| 4234 | // If we definitely did not jump over the assignment, we do not need |
| 4235 | // to bind the exit label. Doing so can defeat peephole |
| 4236 | // optimization. |
| 4237 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4238 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4239 | } |
| 4240 | } |
| 4241 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4244 | case NAMED: { |
| 4245 | Comment cmnt(masm, "[ Store to named Property"); |
| 4246 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4247 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4248 | Handle<String> name(GetName()); |
| 4249 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 4250 | Result value(r0); |
| 4251 | frame->EmitPop(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4252 | |
| 4253 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 4254 | Result property_name(r2); |
| 4255 | __ mov(r2, Operand(name)); |
| 4256 | frame->CallCodeObject(ic, |
| 4257 | RelocInfo::CODE_TARGET, |
| 4258 | &value, |
| 4259 | &property_name, |
| 4260 | 0); |
| 4261 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4262 | break; |
| 4263 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4264 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4265 | case KEYED: { |
| 4266 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4267 | Property* property = expression_->AsProperty(); |
| 4268 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4269 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4270 | |
| 4271 | // Call IC code. |
| 4272 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4273 | // TODO(1222589): Make the IC grab the values from the stack. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame^] | 4274 | Result value(r0); |
| 4275 | frame->EmitPop(r0); // value |
| 4276 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4277 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4278 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4279 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4280 | |
| 4281 | default: |
| 4282 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4283 | } |
| 4284 | } |
| 4285 | |
| 4286 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4287 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4288 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4289 | // (31 instead of 32). |
| 4290 | static void CountLeadingZeros( |
| 4291 | MacroAssembler* masm, |
| 4292 | Register source, |
| 4293 | Register scratch, |
| 4294 | Register zeros) { |
| 4295 | #ifdef __ARM_ARCH_5__ |
| 4296 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4297 | #else |
| 4298 | __ mov(zeros, Operand(0)); |
| 4299 | __ mov(scratch, source); |
| 4300 | // Top 16. |
| 4301 | __ tst(scratch, Operand(0xffff0000)); |
| 4302 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4303 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4304 | // Top 8. |
| 4305 | __ tst(scratch, Operand(0xff000000)); |
| 4306 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4307 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4308 | // Top 4. |
| 4309 | __ tst(scratch, Operand(0xf0000000)); |
| 4310 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4311 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4312 | // Top 2. |
| 4313 | __ tst(scratch, Operand(0xc0000000)); |
| 4314 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4315 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4316 | // Top bit. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4317 | __ tst(scratch, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4318 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4319 | #endif |
| 4320 | } |
| 4321 | |
| 4322 | |
| 4323 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4324 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4325 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4326 | // scratch register. Destroys the source register. No GC occurs during this |
| 4327 | // stub so you don't have to set up the frame. |
| 4328 | class ConvertToDoubleStub : public CodeStub { |
| 4329 | public: |
| 4330 | ConvertToDoubleStub(Register result_reg_1, |
| 4331 | Register result_reg_2, |
| 4332 | Register source_reg, |
| 4333 | Register scratch_reg) |
| 4334 | : result1_(result_reg_1), |
| 4335 | result2_(result_reg_2), |
| 4336 | source_(source_reg), |
| 4337 | zeros_(scratch_reg) { } |
| 4338 | |
| 4339 | private: |
| 4340 | Register result1_; |
| 4341 | Register result2_; |
| 4342 | Register source_; |
| 4343 | Register zeros_; |
| 4344 | |
| 4345 | // Minor key encoding in 16 bits. |
| 4346 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4347 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4348 | |
| 4349 | Major MajorKey() { return ConvertToDouble; } |
| 4350 | int MinorKey() { |
| 4351 | // Encode the parameters in a unique 16 bit value. |
| 4352 | return result1_.code() + |
| 4353 | (result2_.code() << 4) + |
| 4354 | (source_.code() << 8) + |
| 4355 | (zeros_.code() << 12); |
| 4356 | } |
| 4357 | |
| 4358 | void Generate(MacroAssembler* masm); |
| 4359 | |
| 4360 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4361 | |
| 4362 | #ifdef DEBUG |
| 4363 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4364 | #endif |
| 4365 | }; |
| 4366 | |
| 4367 | |
| 4368 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4369 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4370 | Register exponent = result1_; |
| 4371 | Register mantissa = result2_; |
| 4372 | #else |
| 4373 | Register exponent = result2_; |
| 4374 | Register mantissa = result1_; |
| 4375 | #endif |
| 4376 | Label not_special; |
| 4377 | // Convert from Smi to integer. |
| 4378 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4379 | // Move sign bit from source to destination. This works because the sign bit |
| 4380 | // in the exponent word of the double has the same position and polarity as |
| 4381 | // the 2's complement sign bit in a Smi. |
| 4382 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4383 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4384 | // Subtract from 0 if source was negative. |
| 4385 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4386 | __ cmp(source_, Operand(1)); |
| 4387 | __ b(gt, ¬_special); |
| 4388 | |
| 4389 | // We have -1, 0 or 1, which we treat specially. |
| 4390 | __ cmp(source_, Operand(0)); |
| 4391 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4392 | static const uint32_t exponent_word_for_1 = |
| 4393 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4394 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4395 | // 1, 0 and -1 all have 0 for the second word. |
| 4396 | __ mov(mantissa, Operand(0)); |
| 4397 | __ Ret(); |
| 4398 | |
| 4399 | __ bind(¬_special); |
| 4400 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4401 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4402 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4403 | // Compute exponent and or it into the exponent register. |
| 4404 | // We use result2 as a scratch register here. |
| 4405 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4406 | __ orr(exponent, |
| 4407 | exponent, |
| 4408 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4409 | // Shift up the source chopping the top bit off. |
| 4410 | __ add(zeros_, zeros_, Operand(1)); |
| 4411 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4412 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4413 | // Compute lower part of fraction (last 12 bits). |
| 4414 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4415 | // And the top (top 20 bits). |
| 4416 | __ orr(exponent, |
| 4417 | exponent, |
| 4418 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4419 | __ Ret(); |
| 4420 | } |
| 4421 | |
| 4422 | |
| 4423 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4424 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4425 | // so you don't have to set up the frame. |
| 4426 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4427 | public: |
| 4428 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4429 | Register the_heap_number, |
| 4430 | Register scratch) |
| 4431 | : the_int_(the_int), |
| 4432 | the_heap_number_(the_heap_number), |
| 4433 | scratch_(scratch) { } |
| 4434 | |
| 4435 | private: |
| 4436 | Register the_int_; |
| 4437 | Register the_heap_number_; |
| 4438 | Register scratch_; |
| 4439 | |
| 4440 | // Minor key encoding in 16 bits. |
| 4441 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4442 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4443 | |
| 4444 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4445 | int MinorKey() { |
| 4446 | // Encode the parameters in a unique 16 bit value. |
| 4447 | return the_int_.code() + |
| 4448 | (the_heap_number_.code() << 4) + |
| 4449 | (scratch_.code() << 8); |
| 4450 | } |
| 4451 | |
| 4452 | void Generate(MacroAssembler* masm); |
| 4453 | |
| 4454 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4455 | |
| 4456 | #ifdef DEBUG |
| 4457 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4458 | #endif |
| 4459 | }; |
| 4460 | |
| 4461 | |
| 4462 | // See comment for class. |
| 4463 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) { |
| 4464 | Label max_negative_int; |
| 4465 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4466 | // We test for the special value that has a different exponent. This test |
| 4467 | // has the neat side effect of setting the flags according to the sign. |
| 4468 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4469 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4470 | __ b(eq, &max_negative_int); |
| 4471 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4472 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4473 | uint32_t non_smi_exponent = |
| 4474 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4475 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4476 | // Set the sign bit in scratch_ if the value was negative. |
| 4477 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4478 | // Subtract from 0 if the value was negative. |
| 4479 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4480 | // We should be masking the implict first digit of the mantissa away here, |
| 4481 | // but it just ends up combining harmlessly with the last digit of the |
| 4482 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4483 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4484 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4485 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4486 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4487 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4488 | HeapNumber::kExponentOffset)); |
| 4489 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4490 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4491 | HeapNumber::kMantissaOffset)); |
| 4492 | __ Ret(); |
| 4493 | |
| 4494 | __ bind(&max_negative_int); |
| 4495 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4496 | // a double because it uses a sign bit instead of using two's complement. |
| 4497 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4498 | // significant 1 bit is not stored. |
| 4499 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4500 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4501 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4502 | __ mov(ip, Operand(0)); |
| 4503 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4504 | __ Ret(); |
| 4505 | } |
| 4506 | |
| 4507 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4508 | // Handle the case where the lhs and rhs are the same object. |
| 4509 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4510 | // for "identity and not NaN". |
| 4511 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4512 | Label* slow, |
| 4513 | Condition cc) { |
| 4514 | Label not_identical; |
| 4515 | __ cmp(r0, Operand(r1)); |
| 4516 | __ b(ne, ¬_identical); |
| 4517 | |
| 4518 | Register exp_mask_reg = r5; |
| 4519 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4520 | |
| 4521 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4522 | // so we do the second best thing - test it ourselves. |
| 4523 | Label heap_number, return_equal; |
| 4524 | // They are both equal and they are not both Smis so both of them are not |
| 4525 | // Smis. If it's not a heap number, then return equal. |
| 4526 | if (cc == lt || cc == gt) { |
| 4527 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
| 4528 | __ b(ge, slow); |
| 4529 | } else { |
| 4530 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4531 | __ b(eq, &heap_number); |
| 4532 | // Comparing JS objects with <=, >= is complicated. |
| 4533 | if (cc != eq) { |
| 4534 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4535 | __ b(ge, slow); |
| 4536 | } |
| 4537 | } |
| 4538 | __ bind(&return_equal); |
| 4539 | if (cc == lt) { |
| 4540 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4541 | } else if (cc == gt) { |
| 4542 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4543 | } else { |
| 4544 | __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves. |
| 4545 | } |
| 4546 | __ mov(pc, Operand(lr)); // Return. |
| 4547 | |
| 4548 | // For less and greater we don't have to check for NaN since the result of |
| 4549 | // x < x is false regardless. For the others here is some code to check |
| 4550 | // for NaN. |
| 4551 | if (cc != lt && cc != gt) { |
| 4552 | __ bind(&heap_number); |
| 4553 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4554 | // not NaN. |
| 4555 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4556 | // and not all mantissa bits (0..51) clear. |
| 4557 | // Read top bits of double representation (second word of value). |
| 4558 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4559 | // Test that exponent bits are all set. |
| 4560 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4561 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4562 | __ b(ne, &return_equal); |
| 4563 | |
| 4564 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4565 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4566 | // Or with all low-bits of mantissa. |
| 4567 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4568 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4569 | // For equal we already have the right value in r0: Return zero (equal) |
| 4570 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if not |
| 4571 | // (it's a NaN). For <= and >= we need to load r0 with the failing value |
| 4572 | // if it's a NaN. |
| 4573 | if (cc != eq) { |
| 4574 | // All-zero means Infinity means equal. |
| 4575 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 4576 | if (cc == le) { |
| 4577 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 4578 | } else { |
| 4579 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 4580 | } |
| 4581 | } |
| 4582 | __ mov(pc, Operand(lr)); // Return. |
| 4583 | } |
| 4584 | // No fall through here. |
| 4585 | |
| 4586 | __ bind(¬_identical); |
| 4587 | } |
| 4588 | |
| 4589 | |
| 4590 | // See comment at call site. |
| 4591 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 4592 | Label* rhs_not_nan, |
| 4593 | Label* slow, |
| 4594 | bool strict) { |
| 4595 | Label lhs_is_smi; |
| 4596 | __ tst(r0, Operand(kSmiTagMask)); |
| 4597 | __ b(eq, &lhs_is_smi); |
| 4598 | |
| 4599 | // Rhs is a Smi. Check whether the non-smi is a heap number. |
| 4600 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4601 | if (strict) { |
| 4602 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4603 | // succeed. Return non-equal (r0 is already not zero) |
| 4604 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4605 | } else { |
| 4606 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4607 | // the runtime. |
| 4608 | __ b(ne, slow); |
| 4609 | } |
| 4610 | |
| 4611 | // Rhs is a smi, lhs is a number. |
| 4612 | __ push(lr); |
| 4613 | __ mov(r7, Operand(r1)); |
| 4614 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4615 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4616 | // r3 and r2 are rhs as double. |
| 4617 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4618 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4619 | // We now have both loaded as doubles but we can skip the lhs nan check |
| 4620 | // since it's a Smi. |
| 4621 | __ pop(lr); |
| 4622 | __ jmp(rhs_not_nan); |
| 4623 | |
| 4624 | __ bind(&lhs_is_smi); |
| 4625 | // Lhs is a Smi. Check whether the non-smi is a heap number. |
| 4626 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4627 | if (strict) { |
| 4628 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4629 | // succeed. Return non-equal. |
| 4630 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 4631 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4632 | } else { |
| 4633 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4634 | // the runtime. |
| 4635 | __ b(ne, slow); |
| 4636 | } |
| 4637 | |
| 4638 | // Lhs is a smi, rhs is a number. |
| 4639 | // r0 is Smi and r1 is heap number. |
| 4640 | __ push(lr); |
| 4641 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4642 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4643 | __ mov(r7, Operand(r0)); |
| 4644 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4645 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4646 | __ pop(lr); |
| 4647 | // Fall through to both_loaded_as_doubles. |
| 4648 | } |
| 4649 | |
| 4650 | |
| 4651 | void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) { |
| 4652 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4653 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4654 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4655 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4656 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4657 | Label one_is_nan, neither_is_nan; |
| 4658 | |
| 4659 | Register exp_mask_reg = r5; |
| 4660 | |
| 4661 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4662 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 4663 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4664 | __ b(ne, rhs_not_nan); |
| 4665 | __ mov(r4, |
| 4666 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4667 | SetCC); |
| 4668 | __ b(ne, &one_is_nan); |
| 4669 | __ cmp(rhs_mantissa, Operand(0)); |
| 4670 | __ b(ne, &one_is_nan); |
| 4671 | |
| 4672 | __ bind(rhs_not_nan); |
| 4673 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4674 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 4675 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4676 | __ b(ne, &neither_is_nan); |
| 4677 | __ mov(r4, |
| 4678 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4679 | SetCC); |
| 4680 | __ b(ne, &one_is_nan); |
| 4681 | __ cmp(lhs_mantissa, Operand(0)); |
| 4682 | __ b(eq, &neither_is_nan); |
| 4683 | |
| 4684 | __ bind(&one_is_nan); |
| 4685 | // NaN comparisons always fail. |
| 4686 | // Load whatever we need in r0 to make the comparison fail. |
| 4687 | if (cc == lt || cc == le) { |
| 4688 | __ mov(r0, Operand(GREATER)); |
| 4689 | } else { |
| 4690 | __ mov(r0, Operand(LESS)); |
| 4691 | } |
| 4692 | __ mov(pc, Operand(lr)); // Return. |
| 4693 | |
| 4694 | __ bind(&neither_is_nan); |
| 4695 | } |
| 4696 | |
| 4697 | |
| 4698 | // See comment at call site. |
| 4699 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 4700 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4701 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4702 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4703 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4704 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4705 | |
| 4706 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 4707 | if (cc == eq) { |
| 4708 | // Doubles are not equal unless they have the same bit pattern. |
| 4709 | // Exception: 0 and -0. |
| 4710 | __ cmp(lhs_mantissa, Operand(rhs_mantissa)); |
| 4711 | __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne); |
| 4712 | // Return non-zero if the numbers are unequal. |
| 4713 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 4714 | |
| 4715 | __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC); |
| 4716 | // If exponents are equal then return 0. |
| 4717 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 4718 | |
| 4719 | // Exponents are unequal. The only way we can return that the numbers |
| 4720 | // are equal is if one is -0 and the other is 0. We already dealt |
| 4721 | // with the case where both are -0 or both are 0. |
| 4722 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 4723 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 4724 | // equal. |
| 4725 | __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC); |
| 4726 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 4727 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 4728 | // Now they are equal if and only if the lhs exponent is zero in its |
| 4729 | // low 31 bits. |
| 4730 | __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize)); |
| 4731 | __ mov(pc, Operand(lr)); |
| 4732 | } else { |
| 4733 | // Call a native function to do a comparison between two non-NaNs. |
| 4734 | // Call C routine that may not cause GC or other trouble. |
| 4735 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 4736 | __ Jump(r5); // Tail call. |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | |
| 4741 | // See comment at call site. |
| 4742 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 4743 | // If either operand is a JSObject or an oddball value, then they are |
| 4744 | // not equal since their pointers are different. |
| 4745 | // There is no test for undetectability in strict equality. |
| 4746 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 4747 | Label first_non_object; |
| 4748 | // Get the type of the first operand into r2 and compare it with |
| 4749 | // FIRST_JS_OBJECT_TYPE. |
| 4750 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 4751 | __ b(lt, &first_non_object); |
| 4752 | |
| 4753 | // Return non-zero (r0 is not zero) |
| 4754 | Label return_not_equal; |
| 4755 | __ bind(&return_not_equal); |
| 4756 | __ mov(pc, Operand(lr)); // Return. |
| 4757 | |
| 4758 | __ bind(&first_non_object); |
| 4759 | // Check for oddballs: true, false, null, undefined. |
| 4760 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 4761 | __ b(eq, &return_not_equal); |
| 4762 | |
| 4763 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 4764 | __ b(ge, &return_not_equal); |
| 4765 | |
| 4766 | // Check for oddballs: true, false, null, undefined. |
| 4767 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 4768 | __ b(eq, &return_not_equal); |
| 4769 | } |
| 4770 | |
| 4771 | |
| 4772 | // See comment at call site. |
| 4773 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 4774 | Label* both_loaded_as_doubles, |
| 4775 | Label* not_heap_numbers, |
| 4776 | Label* slow) { |
| 4777 | __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE); |
| 4778 | __ b(ne, not_heap_numbers); |
| 4779 | __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE); |
| 4780 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 4781 | |
| 4782 | // Both are heap numbers. Load them up then jump to the code we have |
| 4783 | // for that. |
| 4784 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4785 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4786 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4787 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4788 | __ jmp(both_loaded_as_doubles); |
| 4789 | } |
| 4790 | |
| 4791 | |
| 4792 | // Fast negative check for symbol-to-symbol equality. |
| 4793 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 4794 | // r2 is object type of r0. |
| 4795 | __ tst(r2, Operand(kIsNotStringMask)); |
| 4796 | __ b(ne, slow); |
| 4797 | __ tst(r2, Operand(kIsSymbolMask)); |
| 4798 | __ b(eq, slow); |
| 4799 | __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE); |
| 4800 | __ b(ge, slow); |
| 4801 | __ tst(r3, Operand(kIsSymbolMask)); |
| 4802 | __ b(eq, slow); |
| 4803 | |
| 4804 | // Both are symbols. We already checked they weren't the same pointer |
| 4805 | // so they are not equal. |
| 4806 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 4807 | __ mov(pc, Operand(lr)); // Return. |
| 4808 | } |
| 4809 | |
| 4810 | |
| 4811 | // On entry r0 and r1 are the things to be compared. On exit r0 is 0, |
| 4812 | // positive or negative to indicate the result of the comparison. |
| 4813 | void CompareStub::Generate(MacroAssembler* masm) { |
| 4814 | Label slow; // Call builtin. |
| 4815 | Label not_smis, both_loaded_as_doubles, rhs_not_nan; |
| 4816 | |
| 4817 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 4818 | // it is certain that at least one operand isn't a smi. |
| 4819 | |
| 4820 | // Handle the case where the objects are identical. Either returns the answer |
| 4821 | // or goes to slow. Only falls through if the objects were not identical. |
| 4822 | EmitIdenticalObjectComparison(masm, &slow, cc_); |
| 4823 | |
| 4824 | // If either is a Smi (we know that not both are), then they can only |
| 4825 | // be strictly equal if the other is a HeapNumber. |
| 4826 | ASSERT_EQ(0, kSmiTag); |
| 4827 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 4828 | __ and_(r2, r0, Operand(r1)); |
| 4829 | __ tst(r2, Operand(kSmiTagMask)); |
| 4830 | __ b(ne, ¬_smis); |
| 4831 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 4832 | // 1) Return the answer. |
| 4833 | // 2) Go to slow. |
| 4834 | // 3) Fall through to both_loaded_as_doubles. |
| 4835 | // 4) Jump to rhs_not_nan. |
| 4836 | // In cases 3 and 4 we have found out we were dealing with a number-number |
| 4837 | // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles. |
| 4838 | EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_); |
| 4839 | |
| 4840 | __ bind(&both_loaded_as_doubles); |
| 4841 | // r0, r1, r2, r3 are the double representations of the left hand side |
| 4842 | // and the right hand side. |
| 4843 | |
| 4844 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 4845 | // fall through if neither is a NaN. Also binds rhs_not_nan. |
| 4846 | EmitNanCheck(masm, &rhs_not_nan, cc_); |
| 4847 | |
| 4848 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 4849 | // answer. Never falls through. |
| 4850 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 4851 | |
| 4852 | __ bind(¬_smis); |
| 4853 | // At this point we know we are dealing with two different objects, |
| 4854 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 4855 | if (strict_) { |
| 4856 | // This returns non-equal for some object types, or falls through if it |
| 4857 | // was not lucky. |
| 4858 | EmitStrictTwoHeapObjectCompare(masm); |
| 4859 | } |
| 4860 | |
| 4861 | Label check_for_symbols; |
| 4862 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 4863 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 4864 | // that case. If the inputs are not doubles then jumps to check_for_symbols. |
| 4865 | // In this case r2 will contain the type of r0. |
| 4866 | EmitCheckForTwoHeapNumbers(masm, |
| 4867 | &both_loaded_as_doubles, |
| 4868 | &check_for_symbols, |
| 4869 | &slow); |
| 4870 | |
| 4871 | __ bind(&check_for_symbols); |
| 4872 | if (cc_ == eq) { |
| 4873 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 4874 | // of r0 on entry. |
| 4875 | EmitCheckForSymbols(masm, &slow); |
| 4876 | } |
| 4877 | |
| 4878 | __ bind(&slow); |
| 4879 | __ push(lr); |
| 4880 | __ push(r1); |
| 4881 | __ push(r0); |
| 4882 | // Figure out which native to call and setup the arguments. |
| 4883 | Builtins::JavaScript native; |
| 4884 | int arg_count = 1; // Not counting receiver. |
| 4885 | if (cc_ == eq) { |
| 4886 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 4887 | } else { |
| 4888 | native = Builtins::COMPARE; |
| 4889 | int ncr; // NaN compare result |
| 4890 | if (cc_ == lt || cc_ == le) { |
| 4891 | ncr = GREATER; |
| 4892 | } else { |
| 4893 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 4894 | ncr = LESS; |
| 4895 | } |
| 4896 | arg_count++; |
| 4897 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 4898 | __ push(r0); |
| 4899 | } |
| 4900 | |
| 4901 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 4902 | // tagged as a small integer. |
| 4903 | __ mov(r0, Operand(arg_count)); |
| 4904 | __ InvokeBuiltin(native, CALL_JS); |
| 4905 | __ cmp(r0, Operand(0)); |
| 4906 | __ pop(pc); |
| 4907 | } |
| 4908 | |
| 4909 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4910 | // Allocates a heap number or jumps to the label if the young space is full and |
| 4911 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4912 | static void AllocateHeapNumber( |
| 4913 | MacroAssembler* masm, |
| 4914 | Label* need_gc, // Jump here if young space is full. |
| 4915 | Register result_reg, // The tagged address of the new heap number. |
| 4916 | Register allocation_top_addr_reg, // A scratch register. |
| 4917 | Register scratch2) { // Another scratch register. |
| 4918 | ExternalReference allocation_top = |
| 4919 | ExternalReference::new_space_allocation_top_address(); |
| 4920 | ExternalReference allocation_limit = |
| 4921 | ExternalReference::new_space_allocation_limit_address(); |
| 4922 | |
| 4923 | // allocat := the address of the allocation top variable. |
| 4924 | __ mov(allocation_top_addr_reg, Operand(allocation_top)); |
| 4925 | // result_reg := the old allocation top. |
| 4926 | __ ldr(result_reg, MemOperand(allocation_top_addr_reg)); |
| 4927 | // scratch2 := the address of the allocation limit. |
| 4928 | __ mov(scratch2, Operand(allocation_limit)); |
| 4929 | // scratch2 := the allocation limit. |
| 4930 | __ ldr(scratch2, MemOperand(scratch2)); |
| 4931 | // result_reg := the new allocation top. |
| 4932 | __ add(result_reg, result_reg, Operand(HeapNumber::kSize)); |
| 4933 | // Compare new new allocation top and limit. |
| 4934 | __ cmp(result_reg, Operand(scratch2)); |
| 4935 | // Branch if out of space in young generation. |
| 4936 | __ b(hi, need_gc); |
| 4937 | // Store new allocation top. |
| 4938 | __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top |
| 4939 | // Tag and adjust back to start of new object. |
| 4940 | __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag)); |
| 4941 | // Get heap number map into scratch2. |
| 4942 | __ mov(scratch2, Operand(Factory::heap_number_map())); |
| 4943 | // Store heap number map in new object. |
| 4944 | __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset)); |
| 4945 | } |
| 4946 | |
| 4947 | |
| 4948 | // We fall into this code if the operands were Smis, but the result was |
| 4949 | // 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] | 4950 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 4951 | // to call the C-implemented binary fp operation routines we need to end up |
| 4952 | // with the double precision floating point operands in r0 and r1 (for the |
| 4953 | // 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] | 4954 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 4955 | Label* not_smi, |
| 4956 | const Builtins::JavaScript& builtin, |
| 4957 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4958 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4959 | Label slow, slow_pop_2_first, do_the_call; |
| 4960 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 4961 | // Smi-smi case (overflow). |
| 4962 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 4963 | // The new heap number is in r5. r6 and r7 are scratch. |
| 4964 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4965 | // 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] | 4966 | __ mov(r7, Operand(r0)); |
| 4967 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4968 | __ push(lr); |
| 4969 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4970 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 4971 | __ mov(r7, Operand(r1)); |
| 4972 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4973 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4974 | __ pop(lr); |
| 4975 | __ jmp(&do_the_call); // Tail call. No return. |
| 4976 | |
| 4977 | // We jump to here if something goes wrong (one param is not a number of any |
| 4978 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4979 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4980 | __ push(r1); |
| 4981 | __ push(r0); |
| 4982 | __ mov(r0, Operand(1)); // Set number of arguments. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4983 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4984 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4985 | // 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] | 4986 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4987 | if (mode == NO_OVERWRITE) { |
| 4988 | // In the case where there is no chance of an overwritable float we may as |
| 4989 | // well do the allocation immediately while r0 and r1 are untouched. |
| 4990 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4991 | } |
| 4992 | |
| 4993 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4994 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4995 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 4996 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4997 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4998 | if (mode == OVERWRITE_RIGHT) { |
| 4999 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5000 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5001 | // Calling convention says that second double is in r2 and r3. |
| 5002 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5003 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5004 | __ jmp(&finished_loading_r0); |
| 5005 | __ bind(&r0_is_smi); |
| 5006 | if (mode == OVERWRITE_RIGHT) { |
| 5007 | // 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] | 5008 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5009 | } |
| 5010 | // Write Smi from r0 to r3 and r2 in double format. |
| 5011 | __ mov(r7, Operand(r0)); |
| 5012 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5013 | __ push(lr); |
| 5014 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5015 | __ pop(lr); |
| 5016 | __ bind(&finished_loading_r0); |
| 5017 | |
| 5018 | // Move r1 to a double in r0-r1. |
| 5019 | __ tst(r1, Operand(kSmiTagMask)); |
| 5020 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5021 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5022 | __ b(ne, &slow); |
| 5023 | if (mode == OVERWRITE_LEFT) { |
| 5024 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5025 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5026 | // Calling convention says that first double is in r0 and r1. |
| 5027 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5028 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5029 | __ jmp(&finished_loading_r1); |
| 5030 | __ bind(&r1_is_smi); |
| 5031 | if (mode == OVERWRITE_LEFT) { |
| 5032 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5033 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5034 | } |
| 5035 | // Write Smi from r1 to r1 and r0 in double format. |
| 5036 | __ mov(r7, Operand(r1)); |
| 5037 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5038 | __ push(lr); |
| 5039 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5040 | __ pop(lr); |
| 5041 | __ bind(&finished_loading_r1); |
| 5042 | |
| 5043 | __ bind(&do_the_call); |
| 5044 | // r0: Left value (least significant part of mantissa). |
| 5045 | // r1: Left value (sign, exponent, top of mantissa). |
| 5046 | // r2: Right value (least significant part of mantissa). |
| 5047 | // r3: Right value (sign, exponent, top of mantissa). |
| 5048 | // r5: Address of heap number for result. |
| 5049 | __ push(lr); // For later. |
| 5050 | __ push(r5); // Address of heap number that is answer. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5051 | // Call C routine that may not cause GC or other trouble. |
| 5052 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5053 | __ Call(r5); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5054 | // Store answer in the overwritable heap number. |
| 5055 | __ pop(r4); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5056 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5057 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5058 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5059 | // substract the tag from r4. |
| 5060 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5061 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5062 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5063 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5064 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5065 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5066 | #endif |
| 5067 | __ mov(r0, Operand(r4)); |
| 5068 | // And we are done. |
| 5069 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5073 | // 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] | 5074 | // 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] | 5075 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5076 | // 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] | 5077 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5078 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5079 | static void GetInt32(MacroAssembler* masm, |
| 5080 | Register source, |
| 5081 | Register dest, |
| 5082 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5083 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5084 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5085 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5086 | // Get exponent word. |
| 5087 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5088 | // Get exponent alone in scratch2. |
| 5089 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5090 | // Load dest with zero. We use this either for the final shift or |
| 5091 | // for the answer. |
| 5092 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5093 | // 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] | 5094 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5095 | // the exponent that we are fastest at and also the highest exponent we can |
| 5096 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5097 | const uint32_t non_smi_exponent = |
| 5098 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5099 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5100 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5101 | __ b(eq, &right_exponent); |
| 5102 | // If the exponent is higher than that then go to slow case. This catches |
| 5103 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5104 | __ b(gt, slow); |
| 5105 | |
| 5106 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5107 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5108 | // it rounds to zero. |
| 5109 | const uint32_t zero_exponent = |
| 5110 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5111 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5112 | // Dest already has a Smi zero. |
| 5113 | __ b(lt, &done); |
| 5114 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5115 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5116 | // We now have the exponent in dest. Subtract from 30 to get |
| 5117 | // how much to shift down. |
| 5118 | __ rsb(dest, dest, Operand(30)); |
| 5119 | |
| 5120 | __ bind(&right_exponent); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5121 | // Get the top bits of the mantissa. |
| 5122 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5123 | // Put back the implicit 1. |
| 5124 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5125 | // Shift up the mantissa bits to take up the space the exponent used to take. |
| 5126 | // We just orred in the implicit bit so that took care of one and we want to |
| 5127 | // leave the sign bit 0 so we subtract 2 bits from the shift distance. |
| 5128 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5129 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5130 | // Put sign in zero flag. |
| 5131 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5132 | // Get the second half of the double. For some exponents we don't actually |
| 5133 | // need this because the bits get shifted out again, but it's probably slower |
| 5134 | // to test than just to do it. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5135 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5136 | // Shift down 22 bits to get the last 10 bits. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5137 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5138 | // Move down according to the exponent. |
| 5139 | __ mov(dest, Operand(scratch, LSR, dest)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5140 | // Fix sign if sign bit was set. |
| 5141 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5142 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | |
| 5146 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5147 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5148 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5149 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5150 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5151 | // write the answer into (either by allocating or by overwriting). |
| 5152 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5153 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5154 | Label slow, result_not_a_smi; |
| 5155 | Label r0_is_smi, r1_is_smi; |
| 5156 | Label done_checking_r0, done_checking_r1; |
| 5157 | |
| 5158 | __ tst(r1, Operand(kSmiTagMask)); |
| 5159 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5160 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5161 | __ b(ne, &slow); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5162 | GetInt32(masm, r1, r3, r4, r5, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5163 | __ jmp(&done_checking_r1); |
| 5164 | __ bind(&r1_is_smi); |
| 5165 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5166 | __ bind(&done_checking_r1); |
| 5167 | |
| 5168 | __ tst(r0, Operand(kSmiTagMask)); |
| 5169 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5170 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5171 | __ b(ne, &slow); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5172 | GetInt32(masm, r0, r2, r4, r5, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5173 | __ jmp(&done_checking_r0); |
| 5174 | __ bind(&r0_is_smi); |
| 5175 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5176 | __ bind(&done_checking_r0); |
| 5177 | |
| 5178 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5179 | // r2 and r3: Signed int32 operands. |
| 5180 | switch (op_) { |
| 5181 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5182 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5183 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5184 | case Token::SAR: |
| 5185 | // Use only the 5 least significant bits of the shift count. |
| 5186 | __ and_(r2, r2, Operand(0x1f)); |
| 5187 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5188 | break; |
| 5189 | case Token::SHR: |
| 5190 | // Use only the 5 least significant bits of the shift count. |
| 5191 | __ and_(r2, r2, Operand(0x1f)); |
| 5192 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5193 | // SHR is special because it is required to produce a positive answer. |
| 5194 | // The code below for writing into heap numbers isn't capable of writing |
| 5195 | // the register as an unsigned int so we go to slow case if we hit this |
| 5196 | // case. |
| 5197 | __ b(mi, &slow); |
| 5198 | break; |
| 5199 | case Token::SHL: |
| 5200 | // Use only the 5 least significant bits of the shift count. |
| 5201 | __ and_(r2, r2, Operand(0x1f)); |
| 5202 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5203 | break; |
| 5204 | default: UNREACHABLE(); |
| 5205 | } |
| 5206 | // check that the *signed* result fits in a smi |
| 5207 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5208 | __ b(mi, &result_not_a_smi); |
| 5209 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5210 | __ Ret(); |
| 5211 | |
| 5212 | Label have_to_allocate, got_a_heap_number; |
| 5213 | __ bind(&result_not_a_smi); |
| 5214 | switch (mode_) { |
| 5215 | case OVERWRITE_RIGHT: { |
| 5216 | __ tst(r0, Operand(kSmiTagMask)); |
| 5217 | __ b(eq, &have_to_allocate); |
| 5218 | __ mov(r5, Operand(r0)); |
| 5219 | break; |
| 5220 | } |
| 5221 | case OVERWRITE_LEFT: { |
| 5222 | __ tst(r1, Operand(kSmiTagMask)); |
| 5223 | __ b(eq, &have_to_allocate); |
| 5224 | __ mov(r5, Operand(r1)); |
| 5225 | break; |
| 5226 | } |
| 5227 | case NO_OVERWRITE: { |
| 5228 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5229 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5230 | } |
| 5231 | default: break; |
| 5232 | } |
| 5233 | __ bind(&got_a_heap_number); |
| 5234 | // r2: Answer as signed int32. |
| 5235 | // r5: Heap number to write answer into. |
| 5236 | |
| 5237 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5238 | // result. |
| 5239 | __ mov(r0, Operand(r5)); |
| 5240 | |
| 5241 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5242 | // r3 as scratch. r0 is preserved and returned. |
| 5243 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5244 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5245 | |
| 5246 | if (mode_ != NO_OVERWRITE) { |
| 5247 | __ bind(&have_to_allocate); |
| 5248 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5249 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5250 | __ jmp(&got_a_heap_number); |
| 5251 | } |
| 5252 | |
| 5253 | // If all else failed then we go to the runtime system. |
| 5254 | __ bind(&slow); |
| 5255 | __ push(r1); // restore stack |
| 5256 | __ push(r0); |
| 5257 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 5258 | switch (op_) { |
| 5259 | case Token::BIT_OR: |
| 5260 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5261 | break; |
| 5262 | case Token::BIT_AND: |
| 5263 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5264 | break; |
| 5265 | case Token::BIT_XOR: |
| 5266 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5267 | break; |
| 5268 | case Token::SAR: |
| 5269 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5270 | break; |
| 5271 | case Token::SHR: |
| 5272 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5273 | break; |
| 5274 | case Token::SHL: |
| 5275 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5276 | break; |
| 5277 | default: |
| 5278 | UNREACHABLE(); |
| 5279 | } |
| 5280 | } |
| 5281 | |
| 5282 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5283 | // Can we multiply by x with max two shifts and an add. |
| 5284 | // This answers yes to all integers from 2 to 10. |
| 5285 | static bool IsEasyToMultiplyBy(int x) { |
| 5286 | if (x < 2) return false; // Avoid special cases. |
| 5287 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 5288 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 5289 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 5290 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 5291 | return false; |
| 5292 | } |
| 5293 | |
| 5294 | |
| 5295 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 5296 | // Source and destination may be the same register. This routine does |
| 5297 | // not set carry and overflow the way a mul instruction would. |
| 5298 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 5299 | Register source, |
| 5300 | Register destination, |
| 5301 | int known_int) { |
| 5302 | if (IsPowerOf2(known_int)) { |
| 5303 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 5304 | } else if (PopCountLessThanEqual2(known_int)) { |
| 5305 | int first_bit = BitPosition(known_int); |
| 5306 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 5307 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 5308 | if (first_bit != 0) { |
| 5309 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 5310 | } |
| 5311 | } else { |
| 5312 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 5313 | int the_bit = BitPosition(known_int + 1); |
| 5314 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 5315 | } |
| 5316 | } |
| 5317 | |
| 5318 | |
| 5319 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 5320 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 5321 | // a result that needs shifting. |
| 5322 | static void MultiplyByKnownInt2( |
| 5323 | MacroAssembler* masm, |
| 5324 | Register result, |
| 5325 | Register source, |
| 5326 | Register known_int_register, // Smi tagged. |
| 5327 | int known_int, |
| 5328 | int* required_shift) { // Including Smi tag shift |
| 5329 | switch (known_int) { |
| 5330 | case 3: |
| 5331 | __ add(result, source, Operand(source, LSL, 1)); |
| 5332 | *required_shift = 1; |
| 5333 | break; |
| 5334 | case 5: |
| 5335 | __ add(result, source, Operand(source, LSL, 2)); |
| 5336 | *required_shift = 1; |
| 5337 | break; |
| 5338 | case 6: |
| 5339 | __ add(result, source, Operand(source, LSL, 1)); |
| 5340 | *required_shift = 2; |
| 5341 | break; |
| 5342 | case 7: |
| 5343 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 5344 | *required_shift = 1; |
| 5345 | break; |
| 5346 | case 9: |
| 5347 | __ add(result, source, Operand(source, LSL, 3)); |
| 5348 | *required_shift = 1; |
| 5349 | break; |
| 5350 | case 10: |
| 5351 | __ add(result, source, Operand(source, LSL, 2)); |
| 5352 | *required_shift = 2; |
| 5353 | break; |
| 5354 | default: |
| 5355 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 5356 | __ mul(result, source, known_int_register); |
| 5357 | *required_shift = 0; |
| 5358 | } |
| 5359 | } |
| 5360 | |
| 5361 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5362 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5363 | // r1 : x |
| 5364 | // r0 : y |
| 5365 | // result : r0 |
| 5366 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5367 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 5368 | // tell us that. |
| 5369 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 5370 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5371 | switch (op_) { |
| 5372 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5373 | Label not_smi; |
| 5374 | // Fast path. |
| 5375 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5376 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5377 | __ b(ne, ¬_smi); |
| 5378 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 5379 | // Return if no overflow. |
| 5380 | __ Ret(vc); |
| 5381 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 5382 | |
| 5383 | HandleBinaryOpSlowCases(masm, |
| 5384 | ¬_smi, |
| 5385 | Builtins::ADD, |
| 5386 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5387 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5388 | break; |
| 5389 | } |
| 5390 | |
| 5391 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5392 | Label not_smi; |
| 5393 | // Fast path. |
| 5394 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5395 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5396 | __ b(ne, ¬_smi); |
| 5397 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 5398 | // Return if no overflow. |
| 5399 | __ Ret(vc); |
| 5400 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 5401 | |
| 5402 | HandleBinaryOpSlowCases(masm, |
| 5403 | ¬_smi, |
| 5404 | Builtins::SUB, |
| 5405 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5406 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5407 | break; |
| 5408 | } |
| 5409 | |
| 5410 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5411 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5412 | ASSERT(kSmiTag == 0); // adjust code below |
| 5413 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5414 | __ b(ne, ¬_smi); |
| 5415 | // 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] | 5416 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5417 | // Do multiplication |
| 5418 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 5419 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5420 | __ mov(ip, Operand(r3, ASR, 31)); |
| 5421 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 5422 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5423 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5424 | __ tst(r3, Operand(r3)); |
| 5425 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5426 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5427 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 5428 | // We know one of them was zero. |
| 5429 | __ add(r2, r0, Operand(r1), SetCC); |
| 5430 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 5431 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 5432 | // Slow case. We fall through here if we multiplied a negative number |
| 5433 | // with 0, because that would mean we should produce -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5434 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5435 | |
| 5436 | HandleBinaryOpSlowCases(masm, |
| 5437 | ¬_smi, |
| 5438 | Builtins::MUL, |
| 5439 | Token::MUL, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5440 | mode_); |
| 5441 | break; |
| 5442 | } |
| 5443 | |
| 5444 | case Token::DIV: |
| 5445 | case Token::MOD: { |
| 5446 | Label not_smi; |
| 5447 | if (specialized_on_rhs_) { |
| 5448 | Label smi_is_unsuitable; |
| 5449 | __ BranchOnNotSmi(r1, ¬_smi); |
| 5450 | if (IsPowerOf2(constant_rhs_)) { |
| 5451 | if (op_ == Token::MOD) { |
| 5452 | __ and_(r0, |
| 5453 | r1, |
| 5454 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 5455 | SetCC); |
| 5456 | // We now have the answer, but if the input was negative we also |
| 5457 | // have the sign bit. Our work is done if the result is |
| 5458 | // positive or zero: |
| 5459 | __ Ret(pl); |
| 5460 | // A mod of a negative left hand side must return a negative number. |
| 5461 | // Unfortunately if the answer is 0 then we must return -0. And we |
| 5462 | // already optimistically trashed r0 so we may need to restore it. |
| 5463 | __ eor(r0, r0, Operand(0x80000000u), SetCC); |
| 5464 | // Next two instructions are conditional on the answer being -0. |
| 5465 | __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
| 5466 | __ b(eq, &smi_is_unsuitable); |
| 5467 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
| 5468 | __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_))); |
| 5469 | } else { |
| 5470 | ASSERT(op_ == Token::DIV); |
| 5471 | __ tst(r1, |
| 5472 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 5473 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 5474 | int shift = 0; |
| 5475 | int d = constant_rhs_; |
| 5476 | while ((d & 1) == 0) { |
| 5477 | d >>= 1; |
| 5478 | shift++; |
| 5479 | } |
| 5480 | __ mov(r0, Operand(r1, LSR, shift)); |
| 5481 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 5482 | } |
| 5483 | } else { |
| 5484 | // Not a power of 2. |
| 5485 | __ tst(r1, Operand(0x80000000u)); |
| 5486 | __ b(ne, &smi_is_unsuitable); |
| 5487 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 5488 | // multiplying. |
| 5489 | double divisor = 1.0 / constant_rhs_; |
| 5490 | int shift = 32; |
| 5491 | double scale = 4294967296.0; // 1 << 32. |
| 5492 | uint32_t mul; |
| 5493 | // Maximise the precision of the fixed point reciprocal. |
| 5494 | while (true) { |
| 5495 | mul = static_cast<uint32_t>(scale * divisor); |
| 5496 | if (mul >= 0x7fffffff) break; |
| 5497 | scale *= 2.0; |
| 5498 | shift++; |
| 5499 | } |
| 5500 | mul++; |
| 5501 | __ mov(r2, Operand(mul)); |
| 5502 | __ umull(r3, r2, r2, r1); |
| 5503 | __ mov(r2, Operand(r2, LSR, shift - 31)); |
| 5504 | // r2 is r1 / rhs. r2 is not Smi tagged. |
| 5505 | // r0 is still the known rhs. r0 is Smi tagged. |
| 5506 | // r1 is still the unkown lhs. r1 is Smi tagged. |
| 5507 | int required_r4_shift = 0; // Including the Smi tag shift of 1. |
| 5508 | // r4 = r2 * r0. |
| 5509 | MultiplyByKnownInt2(masm, |
| 5510 | r4, |
| 5511 | r2, |
| 5512 | r0, |
| 5513 | constant_rhs_, |
| 5514 | &required_r4_shift); |
| 5515 | // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs). |
| 5516 | if (op_ == Token::DIV) { |
| 5517 | __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC); |
| 5518 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
| 5519 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5520 | } else { |
| 5521 | ASSERT(op_ == Token::MOD); |
| 5522 | __ sub(r0, r1, Operand(r4, LSL, required_r4_shift)); |
| 5523 | } |
| 5524 | } |
| 5525 | __ Ret(); |
| 5526 | __ bind(&smi_is_unsuitable); |
| 5527 | } else { |
| 5528 | __ jmp(¬_smi); |
| 5529 | } |
| 5530 | HandleBinaryOpSlowCases(masm, |
| 5531 | ¬_smi, |
| 5532 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV, |
| 5533 | op_, |
| 5534 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5535 | break; |
| 5536 | } |
| 5537 | |
| 5538 | case Token::BIT_OR: |
| 5539 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5540 | case Token::BIT_XOR: |
| 5541 | case Token::SAR: |
| 5542 | case Token::SHR: |
| 5543 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5544 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5545 | ASSERT(kSmiTag == 0); // adjust code below |
| 5546 | __ tst(r2, Operand(kSmiTagMask)); |
| 5547 | __ b(ne, &slow); |
| 5548 | switch (op_) { |
| 5549 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 5550 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 5551 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5552 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5553 | // Remove tags from right operand. |
| 5554 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5555 | // Use only the 5 least significant bits of the shift count. |
| 5556 | __ and_(r2, r2, Operand(0x1f)); |
| 5557 | __ mov(r0, Operand(r1, ASR, r2)); |
| 5558 | // Smi tag result. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5559 | __ bic(r0, r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5560 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5561 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5562 | // Remove tags from operands. We can't do this on a 31 bit number |
| 5563 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 5564 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5565 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5566 | // Use only the 5 least significant bits of the shift count. |
| 5567 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5568 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5569 | // Unsigned shift is not allowed to produce a negative number, so |
| 5570 | // check the sign bit and the sign bit after Smi tagging. |
| 5571 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5572 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5573 | // Smi tag result. |
| 5574 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5575 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5576 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5577 | // Remove tags from operands. |
| 5578 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5579 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5580 | // Use only the 5 least significant bits of the shift count. |
| 5581 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5582 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5583 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5584 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 5585 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5586 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5587 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5588 | default: UNREACHABLE(); |
| 5589 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5590 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5591 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5592 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5593 | break; |
| 5594 | } |
| 5595 | |
| 5596 | default: UNREACHABLE(); |
| 5597 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5598 | // This code should be unreachable. |
| 5599 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
| 5602 | |
| 5603 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 5604 | Label within_limit; |
| 5605 | __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit())); |
| 5606 | __ ldr(ip, MemOperand(ip)); |
| 5607 | __ cmp(sp, Operand(ip)); |
| 5608 | __ b(hs, &within_limit); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5609 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 5610 | // argument, so give it a Smi. |
| 5611 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5612 | __ push(r0); |
| 5613 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1); |
| 5614 | __ bind(&within_limit); |
| 5615 | |
| 5616 | __ StubReturn(1); |
| 5617 | } |
| 5618 | |
| 5619 | |
| 5620 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 5621 | Label undo; |
| 5622 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5623 | Label not_smi; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5624 | |
| 5625 | // Enter runtime system if the value is not a smi. |
| 5626 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5627 | __ b(ne, ¬_smi); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5628 | |
| 5629 | // Enter runtime system if the value of the expression is zero |
| 5630 | // to make sure that we switch between 0 and -0. |
| 5631 | __ cmp(r0, Operand(0)); |
| 5632 | __ b(eq, &slow); |
| 5633 | |
| 5634 | // The value of the expression is a smi that is not zero. Try |
| 5635 | // optimistic subtraction '0 - value'. |
| 5636 | __ rsb(r1, r0, Operand(0), SetCC); |
| 5637 | __ b(vs, &slow); |
| 5638 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5639 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 5640 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5641 | |
| 5642 | // Enter runtime system. |
| 5643 | __ bind(&slow); |
| 5644 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5645 | __ mov(r0, Operand(0)); // Set number of arguments. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5646 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 5647 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5648 | __ bind(¬_smi); |
| 5649 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 5650 | __ b(ne, &slow); |
| 5651 | // r0 is a heap number. Get a new heap number in r1. |
| 5652 | if (overwrite_) { |
| 5653 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5654 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5655 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5656 | } else { |
| 5657 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
| 5658 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 5659 | __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 5660 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5661 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5662 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 5663 | __ mov(r0, Operand(r1)); |
| 5664 | } |
| 5665 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5666 | } |
| 5667 | |
| 5668 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5669 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5670 | // r0 holds the exception. |
| 5671 | |
| 5672 | // Adjust this code if not the case. |
| 5673 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5674 | |
| 5675 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5676 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 5677 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5678 | |
| 5679 | // Restore the next handler and frame pointer, discard handler state. |
| 5680 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5681 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5682 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5683 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5684 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 5685 | |
| 5686 | // Before returning we restore the context from the frame pointer if |
| 5687 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5688 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5689 | __ cmp(fp, Operand(0)); |
| 5690 | // Set cp to NULL if fp is NULL. |
| 5691 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5692 | // Restore cp otherwise. |
| 5693 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5694 | #ifdef DEBUG |
| 5695 | if (FLAG_debug_code) { |
| 5696 | __ mov(lr, Operand(pc)); |
| 5697 | } |
| 5698 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5699 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5700 | __ pop(pc); |
| 5701 | } |
| 5702 | |
| 5703 | |
| 5704 | void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5705 | // Adjust this code if not the case. |
| 5706 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5707 | |
| 5708 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5709 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5710 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5711 | |
| 5712 | // Unwind the handlers until the ENTRY handler is found. |
| 5713 | Label loop, done; |
| 5714 | __ bind(&loop); |
| 5715 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5716 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 5717 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5718 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 5719 | __ b(eq, &done); |
| 5720 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5721 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 5722 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5723 | __ jmp(&loop); |
| 5724 | __ bind(&done); |
| 5725 | |
| 5726 | // 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] | 5727 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5728 | __ pop(r0); |
| 5729 | __ str(r0, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5730 | |
| 5731 | // Set external caught exception to false. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5732 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5733 | __ mov(r0, Operand(false)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5734 | __ mov(r2, Operand(external_caught)); |
| 5735 | __ str(r0, MemOperand(r2)); |
| 5736 | |
| 5737 | // Set pending exception and r0 to out of memory exception. |
| 5738 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5739 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5740 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 5741 | __ str(r0, MemOperand(r2)); |
| 5742 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5743 | // Stack layout at this point. See also StackHandlerConstants. |
| 5744 | // sp -> state (ENTRY) |
| 5745 | // fp |
| 5746 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5747 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5748 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 5749 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5750 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 5751 | // Before returning we restore the context from the frame pointer if |
| 5752 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5753 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5754 | __ cmp(fp, Operand(0)); |
| 5755 | // Set cp to NULL if fp is NULL. |
| 5756 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5757 | // Restore cp otherwise. |
| 5758 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5759 | #ifdef DEBUG |
| 5760 | if (FLAG_debug_code) { |
| 5761 | __ mov(lr, Operand(pc)); |
| 5762 | } |
| 5763 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5764 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5765 | __ pop(pc); |
| 5766 | } |
| 5767 | |
| 5768 | |
| 5769 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 5770 | Label* throw_normal_exception, |
| 5771 | Label* throw_out_of_memory_exception, |
| 5772 | StackFrame::Type frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5773 | bool do_gc, |
| 5774 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5775 | // r0: result parameter for PerformGC, if any |
| 5776 | // r4: number of arguments including receiver (C callee-saved) |
| 5777 | // r5: pointer to builtin function (C callee-saved) |
| 5778 | // r6: pointer to the first argument (C callee-saved) |
| 5779 | |
| 5780 | if (do_gc) { |
| 5781 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5782 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 5783 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5784 | } |
| 5785 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5786 | ExternalReference scope_depth = |
| 5787 | ExternalReference::heap_always_allocate_scope_depth(); |
| 5788 | if (always_allocate) { |
| 5789 | __ mov(r0, Operand(scope_depth)); |
| 5790 | __ ldr(r1, MemOperand(r0)); |
| 5791 | __ add(r1, r1, Operand(1)); |
| 5792 | __ str(r1, MemOperand(r0)); |
| 5793 | } |
| 5794 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5795 | // Call C built-in. |
| 5796 | // r0 = argc, r1 = argv |
| 5797 | __ mov(r0, Operand(r4)); |
| 5798 | __ mov(r1, Operand(r6)); |
| 5799 | |
| 5800 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 5801 | // frames, we need to know where the return address is. Right now, |
| 5802 | // we push it on the stack to be able to find it again, but we never |
| 5803 | // restore from it in case of changes, which makes it impossible to |
| 5804 | // support moving the C entry code stub. This should be fixed, but currently |
| 5805 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 5806 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5807 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 5808 | masm->push(lr); |
| 5809 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5810 | |
| 5811 | if (always_allocate) { |
| 5812 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 5813 | // though (contain the result). |
| 5814 | __ mov(r2, Operand(scope_depth)); |
| 5815 | __ ldr(r3, MemOperand(r2)); |
| 5816 | __ sub(r3, r3, Operand(1)); |
| 5817 | __ str(r3, MemOperand(r2)); |
| 5818 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5819 | |
| 5820 | // check for failure result |
| 5821 | Label failure_returned; |
| 5822 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 5823 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 5824 | __ add(r2, r0, Operand(1)); |
| 5825 | __ tst(r2, Operand(kFailureTagMask)); |
| 5826 | __ b(eq, &failure_returned); |
| 5827 | |
| 5828 | // Exit C frame and return. |
| 5829 | // r0:r1: result |
| 5830 | // sp: stack pointer |
| 5831 | // fp: frame pointer |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5832 | __ LeaveExitFrame(frame_type); |
| 5833 | |
| 5834 | // check if we should retry or throw exception |
| 5835 | Label retry; |
| 5836 | __ bind(&failure_returned); |
| 5837 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 5838 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 5839 | __ b(eq, &retry); |
| 5840 | |
| 5841 | Label continue_exception; |
| 5842 | // If the returned failure is EXCEPTION then promote Top::pending_exception(). |
| 5843 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 5844 | __ b(ne, &continue_exception); |
| 5845 | |
| 5846 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5847 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5848 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5849 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5850 | __ ldr(r0, MemOperand(ip)); |
| 5851 | __ str(r3, MemOperand(ip)); |
| 5852 | |
| 5853 | __ bind(&continue_exception); |
| 5854 | // Special handling of out of memory exception. |
| 5855 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5856 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5857 | __ b(eq, throw_out_of_memory_exception); |
| 5858 | |
| 5859 | // Handle normal exception. |
| 5860 | __ jmp(throw_normal_exception); |
| 5861 | |
| 5862 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 5863 | } |
| 5864 | |
| 5865 | |
| 5866 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 5867 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 5868 | // r0: number of arguments including receiver |
| 5869 | // r1: pointer to builtin function |
| 5870 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5871 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5872 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5873 | |
| 5874 | // NOTE: Invocations of builtins may return failure objects |
| 5875 | // instead of a proper result. The builtin entry handles |
| 5876 | // this by performing a garbage collection and retrying the |
| 5877 | // builtin once. |
| 5878 | |
| 5879 | StackFrame::Type frame_type = is_debug_break |
| 5880 | ? StackFrame::EXIT_DEBUG |
| 5881 | : StackFrame::EXIT; |
| 5882 | |
| 5883 | // Enter the exit frame that transitions from JavaScript to C++. |
| 5884 | __ EnterExitFrame(frame_type); |
| 5885 | |
| 5886 | // r4: number of arguments (C callee-saved) |
| 5887 | // r5: pointer to builtin function (C callee-saved) |
| 5888 | // r6: pointer to first argument (C callee-saved) |
| 5889 | |
| 5890 | Label throw_out_of_memory_exception; |
| 5891 | Label throw_normal_exception; |
| 5892 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5893 | // Call into the runtime system. Collect garbage before the call if |
| 5894 | // running with --gc-greedy set. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5895 | if (FLAG_gc_greedy) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5896 | Failure* failure = Failure::RetryAfterGC(0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5897 | __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure))); |
| 5898 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5899 | GenerateCore(masm, &throw_normal_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5900 | &throw_out_of_memory_exception, |
| 5901 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5902 | FLAG_gc_greedy, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5903 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5904 | |
| 5905 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5906 | GenerateCore(masm, |
| 5907 | &throw_normal_exception, |
| 5908 | &throw_out_of_memory_exception, |
| 5909 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5910 | true, |
| 5911 | false); |
| 5912 | |
| 5913 | // Do full GC and retry runtime call one final time. |
| 5914 | Failure* failure = Failure::InternalError(); |
| 5915 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 5916 | GenerateCore(masm, |
| 5917 | &throw_normal_exception, |
| 5918 | &throw_out_of_memory_exception, |
| 5919 | frame_type, |
| 5920 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5921 | true); |
| 5922 | |
| 5923 | __ bind(&throw_out_of_memory_exception); |
| 5924 | GenerateThrowOutOfMemory(masm); |
| 5925 | // control flow for generated will not return. |
| 5926 | |
| 5927 | __ bind(&throw_normal_exception); |
| 5928 | GenerateThrowTOS(masm); |
| 5929 | } |
| 5930 | |
| 5931 | |
| 5932 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 5933 | // r0: code entry |
| 5934 | // r1: function |
| 5935 | // r2: receiver |
| 5936 | // r3: argc |
| 5937 | // [sp+0]: argv |
| 5938 | |
| 5939 | Label invoke, exit; |
| 5940 | |
| 5941 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 5942 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5943 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5944 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 5945 | |
| 5946 | // Get address of argv, see stm above. |
| 5947 | // r0: code entry |
| 5948 | // r1: function |
| 5949 | // r2: receiver |
| 5950 | // r3: argc |
| 5951 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 5952 | __ ldr(r4, MemOperand(r4)); // argv |
| 5953 | |
| 5954 | // Push a frame with special values setup to mark it as an entry frame. |
| 5955 | // r0: code entry |
| 5956 | // r1: function |
| 5957 | // r2: receiver |
| 5958 | // r3: argc |
| 5959 | // r4: argv |
| 5960 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 5961 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 5962 | __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL)); |
| 5963 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 5964 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5965 | __ ldr(r5, MemOperand(r5)); |
| 5966 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 5967 | |
| 5968 | // Setup frame pointer for the frame to be pushed. |
| 5969 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5970 | |
| 5971 | // Call a faked try-block that does the invoke. |
| 5972 | __ bl(&invoke); |
| 5973 | |
| 5974 | // Caught exception: Store result (exception) in the pending |
| 5975 | // exception field in the JSEnv and return a failure sentinel. |
| 5976 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 5977 | // 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] | 5978 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5979 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5980 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5981 | __ b(&exit); |
| 5982 | |
| 5983 | // Invoke: Link this frame into the handler chain. |
| 5984 | __ bind(&invoke); |
| 5985 | // Must preserve r0-r4, r5-r7 are available. |
| 5986 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5987 | // If an exception not caught by another handler occurs, this handler |
| 5988 | // returns control to the code after the bl(&invoke) above, which |
| 5989 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 5990 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5991 | |
| 5992 | // Clear any pending exceptions. |
| 5993 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 5994 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5995 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5996 | __ str(r5, MemOperand(ip)); |
| 5997 | |
| 5998 | // Invoke the function by calling through JS entry trampoline builtin. |
| 5999 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6000 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6001 | |
| 6002 | // Expected registers by Builtins::JSEntryTrampoline |
| 6003 | // r0: code entry |
| 6004 | // r1: function |
| 6005 | // r2: receiver |
| 6006 | // r3: argc |
| 6007 | // r4: argv |
| 6008 | if (is_construct) { |
| 6009 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6010 | __ mov(ip, Operand(construct_entry)); |
| 6011 | } else { |
| 6012 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6013 | __ mov(ip, Operand(entry)); |
| 6014 | } |
| 6015 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6016 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6017 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6018 | // macro for the add instruction because we don't want the coverage tool |
| 6019 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6020 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6021 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6022 | |
| 6023 | // Unlink this frame from the handler chain. When reading the |
| 6024 | // address of the next handler, there is no need to use the address |
| 6025 | // displacement since the current stack pointer (sp) points directly |
| 6026 | // to the stack handler. |
| 6027 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6028 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6029 | __ str(r3, MemOperand(ip)); |
| 6030 | // No need to restore registers |
| 6031 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6032 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6033 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6034 | __ bind(&exit); // r0 holds result |
| 6035 | // Restore the top frame descriptors from the stack. |
| 6036 | __ pop(r3); |
| 6037 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6038 | __ str(r3, MemOperand(ip)); |
| 6039 | |
| 6040 | // Reset the stack to the callee saved registers. |
| 6041 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6042 | |
| 6043 | // Restore callee-saved registers and return. |
| 6044 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6045 | if (FLAG_debug_code) { |
| 6046 | __ mov(lr, Operand(pc)); |
| 6047 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6048 | #endif |
| 6049 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6050 | } |
| 6051 | |
| 6052 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6053 | // This stub performs an instanceof, calling the builtin function if |
| 6054 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6055 | // be an instance of (these are fetched from the stack). |
| 6056 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6057 | // Get the object - slow case for smis (we may need to throw an exception |
| 6058 | // depending on the rhs). |
| 6059 | Label slow, loop, is_instance, is_not_instance; |
| 6060 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6061 | __ BranchOnSmi(r0, &slow); |
| 6062 | |
| 6063 | // Check that the left hand is a JS object and put map in r3. |
| 6064 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6065 | __ b(lt, &slow); |
| 6066 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6067 | __ b(gt, &slow); |
| 6068 | |
| 6069 | // Get the prototype of the function (r4 is result, r2 is scratch). |
| 6070 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 6071 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6072 | |
| 6073 | // Check that the function prototype is a JS object. |
| 6074 | __ BranchOnSmi(r4, &slow); |
| 6075 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6076 | __ b(lt, &slow); |
| 6077 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6078 | __ b(gt, &slow); |
| 6079 | |
| 6080 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6081 | // Get prototype of object into r2. |
| 6082 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6083 | |
| 6084 | // Loop through the prototype chain looking for the function prototype. |
| 6085 | __ bind(&loop); |
| 6086 | __ cmp(r2, Operand(r4)); |
| 6087 | __ b(eq, &is_instance); |
| 6088 | __ cmp(r2, Operand(Factory::null_value())); |
| 6089 | __ b(eq, &is_not_instance); |
| 6090 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6091 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6092 | __ jmp(&loop); |
| 6093 | |
| 6094 | __ bind(&is_instance); |
| 6095 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6096 | __ pop(); |
| 6097 | __ pop(); |
| 6098 | __ mov(pc, Operand(lr)); // Return. |
| 6099 | |
| 6100 | __ bind(&is_not_instance); |
| 6101 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6102 | __ pop(); |
| 6103 | __ pop(); |
| 6104 | __ mov(pc, Operand(lr)); // Return. |
| 6105 | |
| 6106 | // Slow-case. Tail call builtin. |
| 6107 | __ bind(&slow); |
| 6108 | __ mov(r0, Operand(1)); // Arg count without receiver. |
| 6109 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6110 | } |
| 6111 | |
| 6112 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6113 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6114 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6115 | Label adaptor; |
| 6116 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6117 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6118 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6119 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6120 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6121 | // Nothing to do: The formal number of parameters has already been |
| 6122 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6123 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6124 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6125 | // Arguments adaptor case: Read the arguments length from the |
| 6126 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6127 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6128 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6129 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6130 | } |
| 6131 | |
| 6132 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6133 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6134 | // The displacement is the offset of the last parameter (if any) |
| 6135 | // relative to the frame pointer. |
| 6136 | static const int kDisplacement = |
| 6137 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6138 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6139 | // Check that the key is a smi. |
| 6140 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6141 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6142 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6143 | // Check if the calling frame is an arguments adaptor frame. |
| 6144 | Label adaptor; |
| 6145 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6146 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6147 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 6148 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6149 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6150 | // Check index against formal parameters count limit passed in |
| 6151 | // through register eax. Use unsigned comparison to get negative |
| 6152 | // check for free. |
| 6153 | __ cmp(r1, r0); |
| 6154 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6155 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6156 | // Read the argument from the stack and return it. |
| 6157 | __ sub(r3, r0, r1); |
| 6158 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6159 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6160 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6161 | |
| 6162 | // Arguments adaptor case: Check index against actual arguments |
| 6163 | // limit found in the arguments adaptor frame. Use unsigned |
| 6164 | // comparison to get negative check for free. |
| 6165 | __ bind(&adaptor); |
| 6166 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6167 | __ cmp(r1, r0); |
| 6168 | __ b(cs, &slow); |
| 6169 | |
| 6170 | // Read the argument from the adaptor frame and return it. |
| 6171 | __ sub(r3, r0, r1); |
| 6172 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6173 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6174 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6175 | |
| 6176 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6177 | // by calling the runtime system. |
| 6178 | __ bind(&slow); |
| 6179 | __ push(r1); |
| 6180 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1); |
| 6181 | } |
| 6182 | |
| 6183 | |
| 6184 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 6185 | // Check if the calling frame is an arguments adaptor frame. |
| 6186 | Label runtime; |
| 6187 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6188 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 6189 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 6190 | __ b(ne, &runtime); |
| 6191 | |
| 6192 | // Patch the arguments.length and the parameters pointer. |
| 6193 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6194 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 6195 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6196 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6197 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6198 | |
| 6199 | // Do the runtime call to allocate the arguments object. |
| 6200 | __ bind(&runtime); |
| 6201 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6202 | } |
| 6203 | |
| 6204 | |
| 6205 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6206 | Label slow; |
| 6207 | // Get the function to call from the stack. |
| 6208 | // function, receiver [, arguments] |
| 6209 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6210 | |
| 6211 | // Check that the function is really a JavaScript function. |
| 6212 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6213 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6214 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6215 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6216 | __ b(ne, &slow); |
| 6217 | |
| 6218 | // Fast-case: Invoke the function now. |
| 6219 | // r1: pushed function |
| 6220 | ParameterCount actual(argc_); |
| 6221 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6222 | |
| 6223 | // Slow-case: Non-function called. |
| 6224 | __ bind(&slow); |
| 6225 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6226 | __ mov(r2, Operand(0)); |
| 6227 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6228 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6229 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6233 | int CompareStub::MinorKey() { |
| 6234 | // Encode the two parameters in a unique 16 bit value. |
| 6235 | ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15)); |
| 6236 | return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0); |
| 6237 | } |
| 6238 | |
| 6239 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6240 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6241 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6242 | } } // namespace v8::internal |