ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "debug.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 34 | #include "parser.h" |
| 35 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 37 | #include "scopes.h" |
| 38 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 43 | #define __ ACCESS_MASM(masm_) |
| 44 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 45 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 46 | Label* slow, |
| 47 | Condition cc); |
| 48 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 49 | Label* rhs_not_nan, |
| 50 | Label* slow, |
| 51 | bool strict); |
| 52 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
| 53 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 54 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 55 | Register source, |
| 56 | Register destination, |
| 57 | int known_int); |
| 58 | static bool IsEasyToMultiplyBy(int x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 62 | // ------------------------------------------------------------------------- |
| 63 | // Platform-specific DeferredCode functions. |
| 64 | |
| 65 | void DeferredCode::SaveRegisters() { |
| 66 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 67 | int action = registers_[i]; |
| 68 | if (action == kPush) { |
| 69 | __ push(RegisterAllocator::ToRegister(i)); |
| 70 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 71 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void DeferredCode::RestoreRegisters() { |
| 78 | // Restore registers in reverse order due to the stack. |
| 79 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 80 | int action = registers_[i]; |
| 81 | if (action == kPush) { |
| 82 | __ pop(RegisterAllocator::ToRegister(i)); |
| 83 | } else if (action != kIgnore) { |
| 84 | action &= ~kSyncedFlag; |
| 85 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 90 | |
| 91 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 92 | // CodeGenState implementation. |
| 93 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 94 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 95 | : owner_(owner), |
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, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 104 | JumpTarget* true_target, |
| 105 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 106 | : owner_(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 107 | true_target_(true_target), |
| 108 | false_target_(false_target), |
| 109 | previous_(owner->state()) { |
| 110 | owner_->set_state(this); |
| 111 | } |
| 112 | |
| 113 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 114 | CodeGenState::~CodeGenState() { |
| 115 | ASSERT(owner_->state() == this); |
| 116 | owner_->set_state(previous_); |
| 117 | } |
| 118 | |
| 119 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 120 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 121 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 123 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 124 | bool is_eval) |
| 125 | : is_eval_(is_eval), |
| 126 | script_(script), |
| 127 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 129 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 130 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 131 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 132 | cc_reg_(al), |
| 133 | state_(NULL), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 134 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
| 138 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 139 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 141 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | // cp: callee's context |
| 143 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 144 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 145 | // Record the position for debugging purposes. |
| 146 | CodeForFunctionPosition(fun); |
| 147 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | ZoneList<Statement*>* body = fun->body(); |
| 149 | |
| 150 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 151 | ASSERT(scope_ == NULL); |
| 152 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 153 | ASSERT(allocator_ == NULL); |
| 154 | RegisterAllocator register_allocator(this); |
| 155 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 156 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 157 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 158 | cc_reg_ = al; |
| 159 | { |
| 160 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 161 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 162 | // Entry: |
| 163 | // Stack: receiver, arguments |
| 164 | // lr: return address |
| 165 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 166 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 167 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 169 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 170 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 171 | // tos: code slot |
| 172 | #ifdef DEBUG |
| 173 | if (strlen(FLAG_stop_at) > 0 && |
| 174 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 175 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 176 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 177 | } |
| 178 | #endif |
| 179 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 180 | // Allocate space for locals and initialize them. This also checks |
| 181 | // for stack overflow. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 182 | frame_->AllocateStackSlots(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 183 | // Initialize the function return target after the locals are set |
| 184 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 185 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 186 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 188 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 189 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 190 | // Allocate local context. |
| 191 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 192 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 193 | frame_->EmitPush(r0); |
| 194 | frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 195 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 196 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 197 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 198 | __ cmp(r0, Operand(cp)); |
| 199 | verified_true.Branch(eq); |
| 200 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 201 | verified_true.Bind(); |
| 202 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 204 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 205 | } |
| 206 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 207 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 208 | // 1) only needed if we have a context |
| 209 | // 2) no need to recompute context ptr every single time |
| 210 | // 3) don't copy parameter operand code from SlotOperand! |
| 211 | { |
| 212 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 213 | |
| 214 | // Note that iteration order is relevant here! If we have the same |
| 215 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 216 | // needs to be copied into the context, it must be the last argument |
| 217 | // passed to the parameter that needs to be copied. This is a rare |
| 218 | // case so we don't check for it, instead we rely on the copying |
| 219 | // order: such a parameter is copied repeatedly into the same |
| 220 | // context location and thus the last value is what is seen inside |
| 221 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 222 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 223 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | Slot* slot = par->slot(); |
| 225 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 226 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 227 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | // Loads r2 with context; used below in RecordWrite. |
| 229 | __ str(r1, SlotOperand(slot, r2)); |
| 230 | // Load the offset into r3. |
| 231 | int slot_offset = |
| 232 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 233 | __ mov(r3, Operand(slot_offset)); |
| 234 | __ RecordWrite(r2, r3, r1); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 239 | // Store the arguments object. This must happen after context |
| 240 | // initialization because the arguments object may be stored in the |
| 241 | // context. |
| 242 | if (scope_->arguments() != NULL) { |
| 243 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 245 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 246 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 247 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 248 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 249 | // The receiver is below the arguments, the return address, |
| 250 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 251 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 252 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 253 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 254 | frame_->Adjust(3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 255 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 256 | frame_->CallStub(&stub, 3); |
| 257 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 258 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 259 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 260 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 261 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 262 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 263 | } |
| 264 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 265 | // Generate code to 'execute' declarations and initialize functions |
| 266 | // (source elements). In case of an illegal redeclaration we need to |
| 267 | // handle that instead of processing the declarations. |
| 268 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 270 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | } else { |
| 272 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 273 | ProcessDeclarations(scope_->declarations()); |
| 274 | // Bail out if a stack-overflow exception occurred when processing |
| 275 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 276 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | } |
| 278 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 279 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 280 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 281 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 282 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | |
| 284 | // Compile the body of the function in a vanilla state. Don't |
| 285 | // bother compiling all the code if the scope has an illegal |
| 286 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 287 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 288 | Comment cmnt(masm_, "[ function body"); |
| 289 | #ifdef DEBUG |
| 290 | bool is_builtin = Bootstrapper::IsActive(); |
| 291 | bool should_trace = |
| 292 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 293 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 294 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 295 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 296 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 298 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | } |
| 301 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 302 | // Generate the return sequence if necessary. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 303 | if (has_valid_frame() || function_return_.is_linked()) { |
| 304 | if (!function_return_.is_linked()) { |
| 305 | CodeForReturnPosition(fun); |
| 306 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 307 | // exit |
| 308 | // r0: result |
| 309 | // sp: stack pointer |
| 310 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 311 | // cp: callee's context |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 312 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 313 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 314 | function_return_.Bind(); |
| 315 | if (FLAG_trace) { |
| 316 | // Push the return value on the stack as the parameter. |
| 317 | // Runtime::TraceExit returns the parameter as it is. |
| 318 | frame_->EmitPush(r0); |
| 319 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 320 | } |
| 321 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 322 | // Add a label for checking the size of the code used for returning. |
| 323 | Label check_exit_codesize; |
| 324 | masm_->bind(&check_exit_codesize); |
| 325 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 326 | // Calculate the exact length of the return sequence and make sure that |
| 327 | // the constant pool is not emitted inside of the return sequence. |
| 328 | int32_t sp_delta = (scope_->num_parameters() + 1) * kPointerSize; |
| 329 | int return_sequence_length = Debug::kARMJSReturnSequenceLength; |
| 330 | if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) { |
| 331 | // Additional mov instruction generated. |
| 332 | return_sequence_length++; |
| 333 | } |
| 334 | masm_->BlockConstPoolFor(return_sequence_length); |
| 335 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 336 | // Tear down the frame which will restore the caller's frame pointer and |
| 337 | // the link register. |
| 338 | frame_->Exit(); |
| 339 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 340 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 341 | // tool from instrumenting as we rely on the code size here. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 342 | masm_->add(sp, sp, Operand(sp_delta)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 343 | masm_->Jump(lr); |
| 344 | |
| 345 | // Check that the size of the code used for returning matches what is |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 346 | // expected by the debugger. The add instruction above is an addressing |
| 347 | // mode 1 instruction where there are restrictions on which immediate values |
| 348 | // can be encoded in the instruction and which immediate values requires |
| 349 | // use of an additional instruction for moving the immediate to a temporary |
| 350 | // register. |
| 351 | ASSERT_EQ(return_sequence_length, |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 352 | masm_->InstructionsGeneratedSince(&check_exit_codesize)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 353 | } |
| 354 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 355 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 356 | ASSERT(!has_cc()); |
| 357 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 358 | ASSERT(!function_return_is_shadowed_); |
| 359 | function_return_.Unuse(); |
| 360 | DeleteFrame(); |
| 361 | |
| 362 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 363 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 364 | ProcessDeferred(); |
| 365 | } |
| 366 | |
| 367 | allocator_ = NULL; |
| 368 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 372 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 373 | // Currently, this assertion will fail if we try to assign to |
| 374 | // a constant variable that is constant because it is read-only |
| 375 | // (such as the variable referring to a named function expression). |
| 376 | // We need to implement assignments to read-only variables. |
| 377 | // Ideally, we should do this during AST generation (by converting |
| 378 | // such assignments into expression statements); however, in general |
| 379 | // we may not be able to make the decision until past AST generation, |
| 380 | // that is when the entire program is known. |
| 381 | ASSERT(slot != NULL); |
| 382 | int index = slot->index(); |
| 383 | switch (slot->type()) { |
| 384 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 385 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 386 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 387 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 388 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 389 | |
| 390 | case Slot::CONTEXT: { |
| 391 | // Follow the context chain if necessary. |
| 392 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 393 | Register context = cp; |
| 394 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 395 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 396 | // Load the closure. |
| 397 | // (All contexts, even 'with' contexts, have a closure, |
| 398 | // and it is the same for all contexts inside a function. |
| 399 | // There is no need to go to the function context first.) |
| 400 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 401 | // Load the function context (which is the incoming, outer context). |
| 402 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 403 | context = tmp; |
| 404 | } |
| 405 | // We may have a 'with' context now. Get the function context. |
| 406 | // (In fact this mov may never be the needed, since the scope analysis |
| 407 | // may not permit a direct context access in this case and thus we are |
| 408 | // always at a function context. However it is safe to dereference be- |
| 409 | // cause the function context of a function context is itself. Before |
| 410 | // deleting this mov we should try to create a counter-example first, |
| 411 | // though...) |
| 412 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 413 | return ContextOperand(tmp, index); |
| 414 | } |
| 415 | |
| 416 | default: |
| 417 | UNREACHABLE(); |
| 418 | return MemOperand(r0, 0); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 423 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 424 | Slot* slot, |
| 425 | Register tmp, |
| 426 | Register tmp2, |
| 427 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 428 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 429 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 430 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 431 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 432 | if (s->num_heap_slots() > 0) { |
| 433 | if (s->calls_eval()) { |
| 434 | // Check that extension is NULL. |
| 435 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 436 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 437 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 438 | } |
| 439 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 440 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 441 | context = tmp; |
| 442 | } |
| 443 | } |
| 444 | // Check that last extension is NULL. |
| 445 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 446 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 447 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 448 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 449 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 453 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 454 | // (partially) translated into branches, or it may have set the condition |
| 455 | // code register. If force_cc is set, the value is forced to set the |
| 456 | // condition code register and no value is pushed. If the condition code |
| 457 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 458 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 459 | void CodeGenerator::LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 460 | JumpTarget* true_target, |
| 461 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 462 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 463 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 464 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 465 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 466 | { CodeGenState new_state(this, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 467 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 468 | |
| 469 | // If we hit a stack overflow, we may not have actually visited |
| 470 | // the expression. In that case, we ensure that we have a |
| 471 | // valid-looking frame state because we will continue to generate |
| 472 | // code as we unwind the C++ stack. |
| 473 | // |
| 474 | // It's possible to have both a stack overflow and a valid frame |
| 475 | // state (eg, a subexpression overflowed, visiting it returned |
| 476 | // with a dummied frame state, and visiting this expression |
| 477 | // returned with a normal-looking state). |
| 478 | if (HasStackOverflow() && |
| 479 | has_valid_frame() && |
| 480 | !has_cc() && |
| 481 | frame_->height() == original_height) { |
| 482 | true_target->Jump(); |
| 483 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 484 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 485 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 486 | // Convert the TOS value to a boolean in the condition code register. |
| 487 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 488 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 489 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 490 | ASSERT(!has_valid_frame() || |
| 491 | (has_cc() && frame_->height() == original_height) || |
| 492 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 496 | void CodeGenerator::Load(Expression* expr) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 497 | #ifdef DEBUG |
| 498 | int original_height = frame_->height(); |
| 499 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 500 | JumpTarget true_target; |
| 501 | JumpTarget false_target; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 502 | LoadCondition(expr, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 503 | |
| 504 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 505 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 506 | JumpTarget loaded; |
| 507 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 508 | materialize_true.Branch(cc_reg_); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 509 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 510 | frame_->EmitPush(r0); |
| 511 | loaded.Jump(); |
| 512 | materialize_true.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 513 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 514 | frame_->EmitPush(r0); |
| 515 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 516 | cc_reg_ = al; |
| 517 | } |
| 518 | |
| 519 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 520 | // We have at least one condition value that has been "translated" |
| 521 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 522 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 523 | if (frame_ != NULL) { |
| 524 | loaded.Jump(); // Don't lose the current TOS. |
| 525 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 526 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 527 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 528 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 529 | true_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 530 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 531 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 533 | // If both "true" and "false" need to be loaded jump across the code for |
| 534 | // "false". |
| 535 | if (both) { |
| 536 | loaded.Jump(); |
| 537 | } |
| 538 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 540 | false_target.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 541 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 542 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 544 | // A value is loaded on all paths reaching this point. |
| 545 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 546 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 547 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 548 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 549 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 553 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 554 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 555 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 556 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 560 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 561 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 562 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 563 | __ ldr(scratch, |
| 564 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 565 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 569 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 570 | // Special handling of identifiers as subexpressions of typeof. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 571 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 572 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 573 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 574 | // For a global variable we build the property reference |
| 575 | // <global>.<variable> and perform a (regular non-contextual) property |
| 576 | // load to make sure we do not get reference errors. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 577 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 578 | Literal key(variable->name()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 579 | Property property(&global, &key, RelocInfo::kNoPosition); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 580 | Reference ref(this, &property); |
| 581 | ref.GetValueAndSpill(); |
| 582 | } else if (variable != NULL && variable->slot() != NULL) { |
| 583 | // For a variable that rewrites to a slot, we signal it is the immediate |
| 584 | // subexpression of a typeof. |
| 585 | LoadFromSlot(variable->slot(), INSIDE_TYPEOF); |
| 586 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 587 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 588 | // Anything else can be handled normally. |
| 589 | LoadAndSpill(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 594 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 595 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 596 | cgen->LoadReference(this); |
| 597 | } |
| 598 | |
| 599 | |
| 600 | Reference::~Reference() { |
| 601 | cgen_->UnloadReference(this); |
| 602 | } |
| 603 | |
| 604 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 605 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 606 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 607 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 608 | Expression* e = ref->expression(); |
| 609 | Property* property = e->AsProperty(); |
| 610 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 611 | |
| 612 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 613 | // The expression is either a property or a variable proxy that rewrites |
| 614 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 615 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 616 | // We use a named reference if the key is a literal symbol, unless it is |
| 617 | // a string that can be legally parsed as an integer. This is because |
| 618 | // otherwise we will not get into the slow case code that handles [] on |
| 619 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | Literal* literal = property->key()->AsLiteral(); |
| 621 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 622 | if (literal != NULL && |
| 623 | literal->handle()->IsSymbol() && |
| 624 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | ref->set_type(Reference::NAMED); |
| 626 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 627 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | ref->set_type(Reference::KEYED); |
| 629 | } |
| 630 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 631 | // The expression is a variable proxy that does not rewrite to a |
| 632 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 633 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 634 | LoadGlobal(); |
| 635 | ref->set_type(Reference::NAMED); |
| 636 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 637 | ASSERT(var->slot() != NULL); |
| 638 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 639 | } |
| 640 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 641 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 642 | LoadAndSpill(e); |
| 643 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
| 647 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 648 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 649 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 650 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 651 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 652 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 653 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 654 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 655 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 656 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 661 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 662 | // register to a boolean in the condition code register. The code |
| 663 | // 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] | 664 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 665 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 666 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 667 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 668 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 669 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | |
| 671 | // Fast case checks |
| 672 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 673 | // Check if the value is 'false'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 674 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 675 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 676 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 677 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 678 | // Check if the value is 'true'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 679 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 680 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 681 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 682 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 683 | // Check if the value is 'undefined'. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 684 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 685 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 686 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 687 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 688 | // Check if the value is a smi. |
| 689 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 690 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 691 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 692 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 693 | |
| 694 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 695 | frame_->EmitPush(r0); |
| 696 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 697 | // Convert the result (r0) to a condition code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 698 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 699 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 700 | |
| 701 | cc_reg_ = ne; |
| 702 | } |
| 703 | |
| 704 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 705 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 706 | OverwriteMode overwrite_mode, |
| 707 | int constant_rhs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 708 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 709 | // sp[0] : y |
| 710 | // sp[1] : x |
| 711 | // result : r0 |
| 712 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 713 | // Stub is entered with a call: 'return address' is in lr. |
| 714 | switch (op) { |
| 715 | case Token::ADD: // fall through. |
| 716 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 717 | case Token::MUL: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 718 | case Token::DIV: |
| 719 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 720 | case Token::BIT_OR: |
| 721 | case Token::BIT_AND: |
| 722 | case Token::BIT_XOR: |
| 723 | case Token::SHL: |
| 724 | case Token::SHR: |
| 725 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 726 | frame_->EmitPop(r0); // r0 : y |
| 727 | frame_->EmitPop(r1); // r1 : x |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 728 | GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 729 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 730 | break; |
| 731 | } |
| 732 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 733 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 734 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 735 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 736 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 737 | break; |
| 738 | |
| 739 | default: |
| 740 | // Other cases should have been handled before this point. |
| 741 | UNREACHABLE(); |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 747 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 748 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 749 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 750 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 751 | bool reversed, |
| 752 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 753 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 754 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 755 | reversed_(reversed), |
| 756 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 757 | set_comment("[ DeferredInlinedSmiOperation"); |
| 758 | } |
| 759 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 760 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 761 | |
| 762 | private: |
| 763 | Token::Value op_; |
| 764 | int value_; |
| 765 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 766 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 770 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 771 | switch (op_) { |
| 772 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 773 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 774 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 775 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 776 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 777 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 778 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 779 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 780 | } |
| 781 | break; |
| 782 | } |
| 783 | |
| 784 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 785 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 786 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 787 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 788 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 789 | } else { |
| 790 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 791 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 792 | } |
| 793 | break; |
| 794 | } |
| 795 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 796 | // For these operations there is no optimistic operation that needs to be |
| 797 | // reverted. |
| 798 | case Token::MUL: |
| 799 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 800 | case Token::BIT_OR: |
| 801 | case Token::BIT_XOR: |
| 802 | case Token::BIT_AND: { |
| 803 | if (reversed_) { |
| 804 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 805 | } else { |
| 806 | __ mov(r1, Operand(r0)); |
| 807 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 808 | } |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | case Token::SHL: |
| 813 | case Token::SHR: |
| 814 | case Token::SAR: { |
| 815 | if (!reversed_) { |
| 816 | __ mov(r1, Operand(r0)); |
| 817 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 818 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 819 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 820 | } |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 825 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 826 | UNREACHABLE(); |
| 827 | break; |
| 828 | } |
| 829 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 830 | GenericBinaryOpStub stub(op_, overwrite_mode_, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 831 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 835 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 836 | x &= x - 1; |
| 837 | return (x & (x - 1)) == 0; |
| 838 | } |
| 839 | |
| 840 | |
| 841 | // Returns the index of the lowest bit set. |
| 842 | static int BitPosition(unsigned x) { |
| 843 | int bit_posn = 0; |
| 844 | while ((x & 0xf) == 0) { |
| 845 | bit_posn += 4; |
| 846 | x >>= 4; |
| 847 | } |
| 848 | while ((x & 1) == 0) { |
| 849 | bit_posn++; |
| 850 | x >>= 1; |
| 851 | } |
| 852 | return bit_posn; |
| 853 | } |
| 854 | |
| 855 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 856 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 857 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 858 | bool reversed, |
| 859 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 860 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 861 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 862 | // some possible smi operations (like + and -) when (at least) one |
| 863 | // of the operands is a literal smi. With this optimization, the |
| 864 | // performance of the system is increased by ~15%, and the generated |
| 865 | // code size is increased by ~1% (measured on a combination of |
| 866 | // different benchmarks). |
| 867 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 868 | // sp[0] : operand |
| 869 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 870 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 871 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 872 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 873 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 874 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 875 | bool something_to_inline = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 876 | switch (op) { |
| 877 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 878 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 879 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 880 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 881 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 882 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 883 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 884 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 885 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 886 | break; |
| 887 | } |
| 888 | |
| 889 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 890 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 891 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 892 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 893 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 894 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 895 | } else { |
| 896 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 897 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 898 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 899 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 900 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 901 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 902 | break; |
| 903 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 904 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 905 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 906 | case Token::BIT_OR: |
| 907 | case Token::BIT_XOR: |
| 908 | case Token::BIT_AND: { |
| 909 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 910 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 911 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 912 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 913 | switch (op) { |
| 914 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 915 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 916 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 917 | default: UNREACHABLE(); |
| 918 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 919 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 920 | break; |
| 921 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 922 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 923 | case Token::SHL: |
| 924 | case Token::SHR: |
| 925 | case Token::SAR: { |
| 926 | if (reversed) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 927 | something_to_inline = false; |
| 928 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 929 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 930 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 931 | DeferredCode* deferred = |
| 932 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
| 933 | __ tst(r0, Operand(kSmiTagMask)); |
| 934 | deferred->Branch(ne); |
| 935 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 936 | switch (op) { |
| 937 | case Token::SHL: { |
| 938 | if (shift_value != 0) { |
| 939 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 940 | } |
| 941 | // check that the *unsigned* result fits in a smi |
| 942 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 943 | deferred->Branch(mi); |
| 944 | break; |
| 945 | } |
| 946 | case Token::SHR: { |
| 947 | // LSR by immediate 0 means shifting 32 bits. |
| 948 | if (shift_value != 0) { |
| 949 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 950 | } |
| 951 | // check that the *unsigned* result fits in a smi |
| 952 | // neither of the two high-order bits can be set: |
| 953 | // - 0x80000000: high bit would be lost when smi tagging |
| 954 | // - 0x40000000: this number would convert to negative when |
| 955 | // smi tagging these two cases can only happen with shifts |
| 956 | // by 0 or 1 when handed a valid smi |
| 957 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 958 | deferred->Branch(ne); |
| 959 | break; |
| 960 | } |
| 961 | case Token::SAR: { |
| 962 | if (shift_value != 0) { |
| 963 | // ASR by immediate 0 means shifting 32 bits. |
| 964 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 965 | } |
| 966 | break; |
| 967 | } |
| 968 | default: UNREACHABLE(); |
| 969 | } |
| 970 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 971 | deferred->BindExit(); |
| 972 | break; |
| 973 | } |
| 974 | |
| 975 | case Token::MOD: { |
| 976 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 977 | something_to_inline = false; |
| 978 | break; |
| 979 | } |
| 980 | DeferredCode* deferred = |
| 981 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 982 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 983 | __ tst(r0, Operand(mask)); |
| 984 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 985 | mask = (int_value << kSmiTagSize) - 1; |
| 986 | __ and_(r0, r0, Operand(mask)); |
| 987 | deferred->BindExit(); |
| 988 | break; |
| 989 | } |
| 990 | |
| 991 | case Token::MUL: { |
| 992 | if (!IsEasyToMultiplyBy(int_value)) { |
| 993 | something_to_inline = false; |
| 994 | break; |
| 995 | } |
| 996 | DeferredCode* deferred = |
| 997 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
| 998 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 999 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1000 | unsigned mask = 0x80000000u; |
| 1001 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1002 | mask |= mask >> 1; |
| 1003 | } |
| 1004 | mask |= kSmiTagMask; |
| 1005 | // This does a single mask that checks for a too high value in a |
| 1006 | // conservative way and for a non-Smi. It also filters out negative |
| 1007 | // numbers, unfortunately, but since this code is inline we prefer |
| 1008 | // brevity to comprehensiveness. |
| 1009 | __ tst(r0, Operand(mask)); |
| 1010 | deferred->Branch(ne); |
| 1011 | MultiplyByKnownInt(masm_, r0, r0, int_value); |
| 1012 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1017 | something_to_inline = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1018 | break; |
| 1019 | } |
| 1020 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1021 | if (!something_to_inline) { |
| 1022 | if (!reversed) { |
| 1023 | frame_->EmitPush(r0); |
| 1024 | __ mov(r0, Operand(value)); |
| 1025 | frame_->EmitPush(r0); |
| 1026 | GenericBinaryOperation(op, mode, int_value); |
| 1027 | } else { |
| 1028 | __ mov(ip, Operand(value)); |
| 1029 | frame_->EmitPush(ip); |
| 1030 | frame_->EmitPush(r0); |
| 1031 | GenericBinaryOperation(op, mode, kUnknownIntValue); |
| 1032 | } |
| 1033 | } |
| 1034 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1035 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1039 | void CodeGenerator::Comparison(Condition cc, |
| 1040 | Expression* left, |
| 1041 | Expression* right, |
| 1042 | bool strict) { |
| 1043 | if (left != NULL) LoadAndSpill(left); |
| 1044 | if (right != NULL) LoadAndSpill(right); |
| 1045 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1046 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1047 | // sp[0] : y |
| 1048 | // sp[1] : x |
| 1049 | // result : cc register |
| 1050 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1051 | // Strict only makes sense for equality comparisons. |
| 1052 | ASSERT(!strict || cc == eq); |
| 1053 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1054 | JumpTarget exit; |
| 1055 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1056 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1057 | if (cc == gt || cc == le) { |
| 1058 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1059 | frame_->EmitPop(r1); |
| 1060 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1061 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1062 | frame_->EmitPop(r0); |
| 1063 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1064 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1065 | __ orr(r2, r0, Operand(r1)); |
| 1066 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1067 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1068 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1069 | // Perform non-smi comparison by stub. |
| 1070 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1071 | // We call with 0 args because there are 0 on the stack. |
| 1072 | CompareStub stub(cc, strict); |
| 1073 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1074 | __ cmp(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1075 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1076 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1077 | // Do smi comparisons by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1078 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1079 | __ cmp(r1, Operand(r0)); |
| 1080 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1081 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1082 | cc_reg_ = cc; |
| 1083 | } |
| 1084 | |
| 1085 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1086 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1087 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1088 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1089 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1090 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1091 | int arg_count = args->length(); |
| 1092 | for (int i = 0; i < arg_count; i++) { |
| 1093 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1094 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1095 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1096 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1097 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1099 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1100 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 1101 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1102 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1103 | |
| 1104 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1105 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1106 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1110 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1111 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1112 | ASSERT(has_cc()); |
| 1113 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1114 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1115 | cc_reg_ = al; |
| 1116 | } |
| 1117 | |
| 1118 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1119 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1120 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1121 | Comment cmnt(masm_, "[ check stack"); |
| 1122 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1123 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1124 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1125 | // gives a return address 12 bytes down. |
| 1126 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1127 | masm_->cmp(sp, Operand(ip)); |
| 1128 | StackCheckStub stub; |
| 1129 | // Call the stub if lower. |
| 1130 | masm_->mov(pc, |
| 1131 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1132 | RelocInfo::CODE_TARGET), |
| 1133 | LeaveCC, |
| 1134 | lo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1138 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1139 | #ifdef DEBUG |
| 1140 | int original_height = frame_->height(); |
| 1141 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1142 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1143 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1144 | VisitAndSpill(statements->at(i)); |
| 1145 | } |
| 1146 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1147 | } |
| 1148 | |
| 1149 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1150 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1151 | #ifdef DEBUG |
| 1152 | int original_height = frame_->height(); |
| 1153 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1154 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1155 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1156 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1157 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1158 | VisitStatementsAndSpill(node->statements()); |
| 1159 | if (node->break_target()->is_linked()) { |
| 1160 | node->break_target()->Bind(); |
| 1161 | } |
| 1162 | node->break_target()->Unuse(); |
| 1163 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1167 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1168 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1169 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1170 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1171 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1172 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1173 | frame_->EmitPush(r0); |
| 1174 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1175 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1179 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1180 | #ifdef DEBUG |
| 1181 | int original_height = frame_->height(); |
| 1182 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1183 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1184 | Comment cmnt(masm_, "[ Declaration"); |
| 1185 | Variable* var = node->proxy()->var(); |
| 1186 | ASSERT(var != NULL); // must have been resolved |
| 1187 | Slot* slot = var->slot(); |
| 1188 | |
| 1189 | // If it was not possible to allocate the variable at compile time, |
| 1190 | // we need to "declare" it at runtime to make sure it actually |
| 1191 | // exists in the local context. |
| 1192 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1193 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1194 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1195 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1196 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1197 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1198 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1199 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1200 | // Declaration nodes are always declared in only two modes. |
| 1201 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1202 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1203 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1204 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1205 | // Push initial value, if any. |
| 1206 | // Note: For variables we must not push an initial value (such as |
| 1207 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1208 | // must not destroy the current value. |
| 1209 | if (node->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1210 | __ LoadRoot(r0, Heap::kTheHoleValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1211 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1212 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1213 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1214 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1215 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1216 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1217 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1218 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1219 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1220 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | return; |
| 1222 | } |
| 1223 | |
| 1224 | ASSERT(!var->is_global()); |
| 1225 | |
| 1226 | // If we have a function or a constant, we need to initialize the variable. |
| 1227 | Expression* val = NULL; |
| 1228 | if (node->mode() == Variable::CONST) { |
| 1229 | val = new Literal(Factory::the_hole_value()); |
| 1230 | } else { |
| 1231 | val = node->fun(); // NULL if we don't have a function |
| 1232 | } |
| 1233 | |
| 1234 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1235 | { |
| 1236 | // Set initial value. |
| 1237 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1238 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1239 | target.SetValue(NOT_CONST_INIT); |
| 1240 | // The reference is removed from the stack (preserving TOS) when |
| 1241 | // it goes out of scope. |
| 1242 | } |
| 1243 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1244 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1245 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1246 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1250 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1251 | #ifdef DEBUG |
| 1252 | int original_height = frame_->height(); |
| 1253 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1254 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1255 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1256 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1257 | Expression* expression = node->expression(); |
| 1258 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1259 | LoadAndSpill(expression); |
| 1260 | frame_->Drop(); |
| 1261 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1265 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1266 | #ifdef DEBUG |
| 1267 | int original_height = frame_->height(); |
| 1268 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1269 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1270 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1271 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1272 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1273 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1277 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1278 | #ifdef DEBUG |
| 1279 | int original_height = frame_->height(); |
| 1280 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1281 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1282 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1283 | // Generate different code depending on which parts of the if statement |
| 1284 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1285 | bool has_then_stm = node->HasThenStatement(); |
| 1286 | bool has_else_stm = node->HasElseStatement(); |
| 1287 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1288 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1289 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1290 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1291 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1292 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1293 | JumpTarget then; |
| 1294 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1296 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1297 | if (frame_ != NULL) { |
| 1298 | Branch(false, &else_); |
| 1299 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1300 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1301 | if (frame_ != NULL || then.is_linked()) { |
| 1302 | then.Bind(); |
| 1303 | VisitAndSpill(node->then_statement()); |
| 1304 | } |
| 1305 | if (frame_ != NULL) { |
| 1306 | exit.Jump(); |
| 1307 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1309 | if (else_.is_linked()) { |
| 1310 | else_.Bind(); |
| 1311 | VisitAndSpill(node->else_statement()); |
| 1312 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1313 | |
| 1314 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1315 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1316 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1317 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1318 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1319 | LoadConditionAndSpill(node->condition(), &then, &exit, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1320 | if (frame_ != NULL) { |
| 1321 | Branch(false, &exit); |
| 1322 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1323 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1324 | if (frame_ != NULL || then.is_linked()) { |
| 1325 | then.Bind(); |
| 1326 | VisitAndSpill(node->then_statement()); |
| 1327 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1328 | |
| 1329 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1330 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1331 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1332 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1333 | // if (!cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1334 | LoadConditionAndSpill(node->condition(), &exit, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1335 | if (frame_ != NULL) { |
| 1336 | Branch(true, &exit); |
| 1337 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1338 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1339 | if (frame_ != NULL || else_.is_linked()) { |
| 1340 | else_.Bind(); |
| 1341 | VisitAndSpill(node->else_statement()); |
| 1342 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1343 | |
| 1344 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1345 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1346 | ASSERT(!has_then_stm && !has_else_stm); |
| 1347 | // if (cond) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1348 | LoadConditionAndSpill(node->condition(), &exit, &exit, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1349 | if (frame_ != NULL) { |
| 1350 | if (has_cc()) { |
| 1351 | cc_reg_ = al; |
| 1352 | } else { |
| 1353 | frame_->Drop(); |
| 1354 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1359 | if (exit.is_linked()) { |
| 1360 | exit.Bind(); |
| 1361 | } |
| 1362 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1366 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1367 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1368 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1369 | CodeForStatementPosition(node); |
| 1370 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1374 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1375 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1376 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1377 | CodeForStatementPosition(node); |
| 1378 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1382 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1383 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1384 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1385 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1386 | CodeForStatementPosition(node); |
| 1387 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1388 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1389 | frame_->EmitPop(r0); |
| 1390 | function_return_.Jump(); |
| 1391 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1392 | // Pop the result from the frame and prepare the frame for |
| 1393 | // returning thus making it easier to merge. |
| 1394 | frame_->EmitPop(r0); |
| 1395 | frame_->PrepareForReturn(); |
| 1396 | |
| 1397 | function_return_.Jump(); |
| 1398 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1402 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1403 | #ifdef DEBUG |
| 1404 | int original_height = frame_->height(); |
| 1405 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1406 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1407 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1408 | CodeForStatementPosition(node); |
| 1409 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1410 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1411 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1412 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1413 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1414 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1415 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1416 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1417 | __ cmp(r0, Operand(cp)); |
| 1418 | verified_true.Branch(eq); |
| 1419 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1420 | verified_true.Bind(); |
| 1421 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1422 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1423 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1424 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1428 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1429 | #ifdef DEBUG |
| 1430 | int original_height = frame_->height(); |
| 1431 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1432 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1433 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1434 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1435 | // Pop context. |
| 1436 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1437 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1438 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1439 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1443 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1444 | #ifdef DEBUG |
| 1445 | int original_height = frame_->height(); |
| 1446 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1447 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1448 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1449 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1450 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1451 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1452 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1453 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1454 | JumpTarget next_test; |
| 1455 | JumpTarget fall_through; |
| 1456 | JumpTarget default_entry; |
| 1457 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1458 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1459 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1460 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1461 | |
| 1462 | for (int i = 0; i < length; i++) { |
| 1463 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1464 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1465 | // Remember the default clause and compile it at the end. |
| 1466 | default_clause = clause; |
| 1467 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1470 | Comment cmnt(masm_, "[ Case clause"); |
| 1471 | // Compile the test. |
| 1472 | next_test.Bind(); |
| 1473 | next_test.Unuse(); |
| 1474 | // Duplicate TOS. |
| 1475 | __ ldr(r0, frame_->Top()); |
| 1476 | frame_->EmitPush(r0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1477 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1478 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1479 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1480 | // Before entering the body from the test, remove the switch value from |
| 1481 | // the stack. |
| 1482 | frame_->Drop(); |
| 1483 | |
| 1484 | // Label the body so that fall through is enabled. |
| 1485 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1486 | default_exit.Bind(); |
| 1487 | } else { |
| 1488 | fall_through.Bind(); |
| 1489 | fall_through.Unuse(); |
| 1490 | } |
| 1491 | VisitStatementsAndSpill(clause->statements()); |
| 1492 | |
| 1493 | // If control flow can fall through from the body, jump to the next body |
| 1494 | // or the end of the statement. |
| 1495 | if (frame_ != NULL) { |
| 1496 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1497 | default_entry.Jump(); |
| 1498 | } else { |
| 1499 | fall_through.Jump(); |
| 1500 | } |
| 1501 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1504 | // The final "test" removes the switch value. |
| 1505 | next_test.Bind(); |
| 1506 | frame_->Drop(); |
| 1507 | |
| 1508 | // If there is a default clause, compile it. |
| 1509 | if (default_clause != NULL) { |
| 1510 | Comment cmnt(masm_, "[ Default clause"); |
| 1511 | default_entry.Bind(); |
| 1512 | VisitStatementsAndSpill(default_clause->statements()); |
| 1513 | // If control flow can fall out of the default and there is a case after |
| 1514 | // it, jup to that case's body. |
| 1515 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1516 | default_exit.Jump(); |
| 1517 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1518 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1519 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1520 | if (fall_through.is_linked()) { |
| 1521 | fall_through.Bind(); |
| 1522 | } |
| 1523 | |
| 1524 | if (node->break_target()->is_linked()) { |
| 1525 | node->break_target()->Bind(); |
| 1526 | } |
| 1527 | node->break_target()->Unuse(); |
| 1528 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1532 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1533 | #ifdef DEBUG |
| 1534 | int original_height = frame_->height(); |
| 1535 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1536 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1537 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1538 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1539 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1540 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1541 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1542 | // Label the top of the loop for the backward CFG edge. If the test |
| 1543 | // is always true we can use the continue target, and if the test is |
| 1544 | // always false there is no need. |
| 1545 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1546 | switch (info) { |
| 1547 | case ALWAYS_TRUE: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1548 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1549 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1550 | break; |
| 1551 | case ALWAYS_FALSE: |
| 1552 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1553 | break; |
| 1554 | case DONT_KNOW: |
| 1555 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1556 | body.Bind(); |
| 1557 | break; |
| 1558 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1559 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1560 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1561 | VisitAndSpill(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1562 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1563 | // Compile the test. |
| 1564 | switch (info) { |
| 1565 | case ALWAYS_TRUE: |
| 1566 | // If control can fall off the end of the body, jump back to the |
| 1567 | // top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1568 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1569 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1570 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1571 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1572 | case ALWAYS_FALSE: |
| 1573 | // If we have a continue in the body, we only have to bind its |
| 1574 | // jump target. |
| 1575 | if (node->continue_target()->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1576 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1577 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1578 | break; |
| 1579 | case 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()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1586 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 1587 | CodeForDoWhileConditionPosition(node); |
| 1588 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1589 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1590 | // A invalid frame here indicates that control did not |
| 1591 | // fall out of the test expression. |
| 1592 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1593 | } |
| 1594 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1595 | break; |
| 1596 | } |
| 1597 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1598 | if (node->break_target()->is_linked()) { |
| 1599 | node->break_target()->Bind(); |
| 1600 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1601 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 1606 | #ifdef DEBUG |
| 1607 | int original_height = frame_->height(); |
| 1608 | #endif |
| 1609 | VirtualFrame::SpilledScope spilled_scope; |
| 1610 | Comment cmnt(masm_, "[ WhileStatement"); |
| 1611 | CodeForStatementPosition(node); |
| 1612 | |
| 1613 | // If the test is never true and has no side effects there is no need |
| 1614 | // to compile the test or body. |
| 1615 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1616 | if (info == ALWAYS_FALSE) return; |
| 1617 | |
| 1618 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1619 | |
| 1620 | // Label the top of the loop with the continue target for the backward |
| 1621 | // CFG edge. |
| 1622 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1623 | node->continue_target()->Bind(); |
| 1624 | |
| 1625 | if (info == DONT_KNOW) { |
| 1626 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1627 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1628 | if (has_valid_frame()) { |
| 1629 | // A NULL frame indicates that control did not fall out of the |
| 1630 | // test expression. |
| 1631 | Branch(false, node->break_target()); |
| 1632 | } |
| 1633 | if (has_valid_frame() || body.is_linked()) { |
| 1634 | body.Bind(); |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | if (has_valid_frame()) { |
| 1639 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1640 | VisitAndSpill(node->body()); |
| 1641 | |
| 1642 | // If control flow can fall out of the body, jump back to the top. |
| 1643 | if (has_valid_frame()) { |
| 1644 | node->continue_target()->Jump(); |
| 1645 | } |
| 1646 | } |
| 1647 | if (node->break_target()->is_linked()) { |
| 1648 | node->break_target()->Bind(); |
| 1649 | } |
| 1650 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1651 | } |
| 1652 | |
| 1653 | |
| 1654 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 1655 | #ifdef DEBUG |
| 1656 | int original_height = frame_->height(); |
| 1657 | #endif |
| 1658 | VirtualFrame::SpilledScope spilled_scope; |
| 1659 | Comment cmnt(masm_, "[ ForStatement"); |
| 1660 | CodeForStatementPosition(node); |
| 1661 | if (node->init() != NULL) { |
| 1662 | VisitAndSpill(node->init()); |
| 1663 | } |
| 1664 | |
| 1665 | // If the test is never true there is no need to compile the test or |
| 1666 | // body. |
| 1667 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 1668 | if (info == ALWAYS_FALSE) return; |
| 1669 | |
| 1670 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1671 | |
| 1672 | // If there is no update statement, label the top of the loop with the |
| 1673 | // continue target, otherwise with the loop target. |
| 1674 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 1675 | if (node->next() == NULL) { |
| 1676 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
| 1677 | node->continue_target()->Bind(); |
| 1678 | } else { |
| 1679 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1680 | loop.Bind(); |
| 1681 | } |
| 1682 | |
| 1683 | // If the test is always true, there is no need to compile it. |
| 1684 | if (info == DONT_KNOW) { |
| 1685 | JumpTarget body; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1686 | LoadConditionAndSpill(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1687 | if (has_valid_frame()) { |
| 1688 | Branch(false, node->break_target()); |
| 1689 | } |
| 1690 | if (has_valid_frame() || body.is_linked()) { |
| 1691 | body.Bind(); |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | if (has_valid_frame()) { |
| 1696 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1697 | VisitAndSpill(node->body()); |
| 1698 | |
| 1699 | if (node->next() == NULL) { |
| 1700 | // If there is no update statement and control flow can fall out |
| 1701 | // of the loop, jump directly to the continue label. |
| 1702 | if (has_valid_frame()) { |
| 1703 | node->continue_target()->Jump(); |
| 1704 | } |
| 1705 | } else { |
| 1706 | // If there is an update statement and control flow can reach it |
| 1707 | // via falling out of the body of the loop or continuing, we |
| 1708 | // compile the update statement. |
| 1709 | if (node->continue_target()->is_linked()) { |
| 1710 | node->continue_target()->Bind(); |
| 1711 | } |
| 1712 | if (has_valid_frame()) { |
| 1713 | // Record source position of the statement as this code which is |
| 1714 | // after the code for the body actually belongs to the loop |
| 1715 | // statement and not the body. |
| 1716 | CodeForStatementPosition(node); |
| 1717 | VisitAndSpill(node->next()); |
| 1718 | loop.Jump(); |
| 1719 | } |
| 1720 | } |
| 1721 | } |
| 1722 | if (node->break_target()->is_linked()) { |
| 1723 | node->break_target()->Bind(); |
| 1724 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1725 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1729 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1730 | #ifdef DEBUG |
| 1731 | int original_height = frame_->height(); |
| 1732 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1733 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1734 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1735 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1736 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1737 | JumpTarget primitive; |
| 1738 | JumpTarget jsobject; |
| 1739 | JumpTarget fixed_array; |
| 1740 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1741 | JumpTarget end_del_check; |
| 1742 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1743 | |
| 1744 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1745 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1746 | |
| 1747 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1748 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1749 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1750 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 1751 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1752 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1753 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1754 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1755 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1756 | |
| 1757 | // Stack layout in body: |
| 1758 | // [iteration counter (Smi)] |
| 1759 | // [length of array] |
| 1760 | // [FixedArray] |
| 1761 | // [Map or 0] |
| 1762 | // [Object] |
| 1763 | |
| 1764 | // Check if enumerable is already a JSObject |
| 1765 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1766 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1767 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1768 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1769 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1770 | primitive.Bind(); |
| 1771 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1772 | Result arg_count(r0); |
| 1773 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1774 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1775 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1776 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1777 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1778 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1779 | frame_->EmitPush(r0); |
| 1780 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1781 | |
| 1782 | // If we got a Map, we can do a fast modification check. |
| 1783 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1784 | __ mov(r2, Operand(r0)); |
| 1785 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1786 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 1787 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1788 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1789 | |
| 1790 | // Get enum cache |
| 1791 | __ mov(r1, Operand(r0)); |
| 1792 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1793 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1794 | __ ldr(r2, |
| 1795 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1796 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1797 | frame_->EmitPush(r0); // map |
| 1798 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1799 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1800 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1801 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1802 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1803 | frame_->EmitPush(r0); |
| 1804 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1805 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1806 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1807 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1808 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1809 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1810 | |
| 1811 | // 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] | 1812 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1813 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1814 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1815 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1816 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1817 | |
| 1818 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1819 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1820 | // sp[0] : index |
| 1821 | // sp[1] : array/enum cache length |
| 1822 | // sp[2] : array or enum cache |
| 1823 | // sp[3] : 0 or map |
| 1824 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1825 | // Grab the current frame's height for the break and continue |
| 1826 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1827 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1828 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1829 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1830 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1831 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1832 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1833 | node->break_target()->Branch(hs); |
| 1834 | |
| 1835 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1836 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1837 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1838 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1839 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1840 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1841 | |
| 1842 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1843 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1844 | // Check if this (still) matches the map of the enumerable. |
| 1845 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1846 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1847 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1848 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1849 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1850 | |
| 1851 | // 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] | 1852 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1853 | frame_->EmitPush(r0); |
| 1854 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1855 | Result arg_count_reg(r0); |
| 1856 | __ mov(r0, Operand(1)); |
| 1857 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, &arg_count_reg, 2); |
| 1858 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | |
| 1860 | // If the property has been removed while iterating, we just skip it. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1861 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 1862 | __ cmp(r3, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1863 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1864 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1865 | end_del_check.Bind(); |
| 1866 | // Store the entry in the 'each' expression and take another spin in the |
| 1867 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1868 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1869 | { Reference each(this, node->each()); |
| 1870 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1871 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1872 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1873 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1874 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1875 | // If the reference was to a slot we rely on the convenient property |
| 1876 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1877 | // right on top of or right underneath a zero-sized reference. |
| 1878 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1879 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1880 | // It's safe to pop the value lying on top of the reference before |
| 1881 | // unloading the reference itself (which preserves the top of stack, |
| 1882 | // ie, now the topmost value of the non-zero sized reference), since |
| 1883 | // we will discard the top of stack after unloading the reference |
| 1884 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1885 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1886 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1887 | } |
| 1888 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1889 | // Discard the i'th entry pushed above or else the remainder of the |
| 1890 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1891 | frame_->Drop(); |
| 1892 | |
| 1893 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1894 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1895 | VisitAndSpill(node->body()); |
| 1896 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1897 | // Next. Reestablish a spilled frame in case we are coming here via |
| 1898 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1899 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1900 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1901 | frame_->EmitPop(r0); |
| 1902 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1903 | frame_->EmitPush(r0); |
| 1904 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1905 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1906 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 1907 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1908 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1909 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1910 | |
| 1911 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1912 | exit.Bind(); |
| 1913 | node->continue_target()->Unuse(); |
| 1914 | node->break_target()->Unuse(); |
| 1915 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1919 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1920 | #ifdef DEBUG |
| 1921 | int original_height = frame_->height(); |
| 1922 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1923 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1924 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1925 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1926 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1927 | JumpTarget try_block; |
| 1928 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1929 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1930 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1931 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1932 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1933 | |
| 1934 | // Store the caught exception in the catch variable. |
| 1935 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1936 | ASSERT(ref.is_slot()); |
| 1937 | // Here we make use of the convenient property that it doesn't matter |
| 1938 | // whether a value is immediately on top of or underneath a zero-sized |
| 1939 | // reference. |
| 1940 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1944 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1945 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1946 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 1947 | if (frame_ != NULL) { |
| 1948 | exit.Jump(); |
| 1949 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | |
| 1951 | |
| 1952 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1953 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1954 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1955 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 1956 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1957 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1958 | // Shadow the labels for all escapes from the try block, including |
| 1959 | // returns. During shadowing, the original label is hidden as the |
| 1960 | // LabelShadow and operations on the original actually affect the |
| 1961 | // shadowing label. |
| 1962 | // |
| 1963 | // We should probably try to unify the escaping labels and the return |
| 1964 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1965 | int nof_escapes = node->escaping_targets()->length(); |
| 1966 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 1967 | |
| 1968 | // Add the shadow target for the function return. |
| 1969 | static const int kReturnShadowIndex = 0; |
| 1970 | shadows.Add(new ShadowTarget(&function_return_)); |
| 1971 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 1972 | function_return_is_shadowed_ = true; |
| 1973 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 1974 | |
| 1975 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1976 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1977 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1981 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1982 | |
| 1983 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1984 | // After shadowing stops, the original labels are unshadowed and the |
| 1985 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1986 | bool has_unlinks = false; |
| 1987 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1988 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1989 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 1990 | } |
| 1991 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 1992 | |
| 1993 | // Get an external reference to the handler address. |
| 1994 | ExternalReference handler_address(Top::k_handler_address); |
| 1995 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1996 | // If we can fall off the end of the try block, unlink from try chain. |
| 1997 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1998 | // The next handler address is on top of the frame. Unlink from |
| 1999 | // the handler list and drop the rest of this handler from the |
| 2000 | // frame. |
| 2001 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2002 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2003 | __ mov(r3, Operand(handler_address)); |
| 2004 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2005 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2006 | if (has_unlinks) { |
| 2007 | exit.Jump(); |
| 2008 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2011 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2012 | // jumped to. Deallocate each shadow target. |
| 2013 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2014 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2015 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2016 | shadows[i]->Bind(); |
| 2017 | // Because we can be jumping here (to spilled code) from unspilled |
| 2018 | // code, we need to reestablish a spilled frame at this block. |
| 2019 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2020 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2021 | // Reload sp from the top handler, because some statements that we |
| 2022 | // 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] | 2023 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2024 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2025 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2026 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2027 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2028 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2029 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2030 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2031 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2032 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2033 | frame_->PrepareForReturn(); |
| 2034 | } |
| 2035 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2039 | exit.Bind(); |
| 2040 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2044 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2045 | #ifdef DEBUG |
| 2046 | int original_height = frame_->height(); |
| 2047 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2048 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2049 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2050 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | |
| 2052 | // State: Used to keep track of reason for entering the finally |
| 2053 | // block. Should probably be extended to hold information for |
| 2054 | // break/continue from within the try block. |
| 2055 | enum { FALLING, THROWING, JUMPING }; |
| 2056 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2057 | JumpTarget try_block; |
| 2058 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2059 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2060 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2061 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2062 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2063 | // In case of thrown exceptions, this is where we continue. |
| 2064 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2065 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2066 | |
| 2067 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2068 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2069 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2070 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2071 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2072 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2073 | // Shadow the labels for all escapes from the try block, including |
| 2074 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2075 | // operations on the original actually affect the shadowing label. |
| 2076 | // |
| 2077 | // We should probably try to unify the escaping labels and the return |
| 2078 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2079 | int nof_escapes = node->escaping_targets()->length(); |
| 2080 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2081 | |
| 2082 | // Add the shadow target for the function return. |
| 2083 | static const int kReturnShadowIndex = 0; |
| 2084 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2085 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2086 | function_return_is_shadowed_ = true; |
| 2087 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2088 | |
| 2089 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2090 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2091 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2095 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2096 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2097 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2098 | // After shadowing stops, the original labels are unshadowed and the |
| 2099 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2100 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2101 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2102 | shadows[i]->StopShadowing(); |
| 2103 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2104 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2105 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2106 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2107 | // Get an external reference to the handler address. |
| 2108 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2109 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2110 | // If we can fall off the end of the try block, unlink from the try |
| 2111 | // chain and set the state on the frame to FALLING. |
| 2112 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2113 | // The next handler address is on top of the frame. |
| 2114 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2115 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2116 | __ mov(r3, Operand(handler_address)); |
| 2117 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2118 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2119 | |
| 2120 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2121 | // state in r2, then jump around the unlink blocks if any. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2122 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2123 | frame_->EmitPush(r0); |
| 2124 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2125 | if (nof_unlinks > 0) { |
| 2126 | finally_block.Jump(); |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | // Generate code to unlink and set the state for the (formerly) |
| 2131 | // shadowing targets that have been jumped to. |
| 2132 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2133 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2134 | // If we have come from the shadowed return, the return value is |
| 2135 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2136 | // until it is pushed. |
| 2137 | // |
| 2138 | // Because we can be jumping here (to spilled code) from |
| 2139 | // unspilled code, we need to reestablish a spilled frame at |
| 2140 | // this block. |
| 2141 | shadows[i]->Bind(); |
| 2142 | frame_->SpillAll(); |
| 2143 | |
| 2144 | // Reload sp from the top handler, because some statements that |
| 2145 | // we break from (eg, for...in) may have left stuff on the |
| 2146 | // stack. |
| 2147 | __ mov(r3, Operand(handler_address)); |
| 2148 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2149 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2150 | |
| 2151 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2152 | // handler address is currently on top of the frame. |
| 2153 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2154 | frame_->EmitPop(r1); |
| 2155 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2156 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2157 | |
| 2158 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2159 | // If this label shadowed the function return, materialize the |
| 2160 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2161 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2162 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2163 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2164 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2165 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2166 | } |
| 2167 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2168 | if (--nof_unlinks > 0) { |
| 2169 | // If this is not the last unlink block, jump around the next. |
| 2170 | finally_block.Jump(); |
| 2171 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2172 | } |
| 2173 | } |
| 2174 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2175 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2176 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2177 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2178 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2179 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2180 | |
| 2181 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2182 | // and the state - while evaluating the finally block. |
| 2183 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2184 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2185 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2186 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2187 | if (has_valid_frame()) { |
| 2188 | // Restore state and return value or faked TOS. |
| 2189 | frame_->EmitPop(r2); |
| 2190 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2193 | // Generate code to jump to the right destination for all used |
| 2194 | // formerly shadowing targets. Deallocate each shadow target. |
| 2195 | for (int i = 0; i < shadows.length(); i++) { |
| 2196 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2197 | JumpTarget* original = shadows[i]->other_target(); |
| 2198 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2199 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2200 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2201 | skip.Branch(ne); |
| 2202 | frame_->PrepareForReturn(); |
| 2203 | original->Jump(); |
| 2204 | skip.Bind(); |
| 2205 | } else { |
| 2206 | original->Branch(eq); |
| 2207 | } |
| 2208 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2209 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2210 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2211 | if (has_valid_frame()) { |
| 2212 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2213 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2214 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2215 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2216 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2217 | // Rethrow exception. |
| 2218 | frame_->EmitPush(r0); |
| 2219 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2220 | |
| 2221 | // Done. |
| 2222 | exit.Bind(); |
| 2223 | } |
| 2224 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2228 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2229 | #ifdef DEBUG |
| 2230 | int original_height = frame_->height(); |
| 2231 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2232 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2233 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2234 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2235 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2236 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2237 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2238 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2239 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2243 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2244 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2245 | ASSERT(boilerplate->IsBoilerplate()); |
| 2246 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2247 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2248 | frame_->EmitPush(cp); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2249 | __ mov(r0, Operand(boilerplate)); |
| 2250 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2251 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2252 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2256 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2257 | #ifdef DEBUG |
| 2258 | int original_height = frame_->height(); |
| 2259 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2260 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2261 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2262 | |
| 2263 | // Build the function boilerplate and instantiate it. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2264 | Handle<JSFunction> boilerplate = |
| 2265 | Compiler::BuildBoilerplate(node, script_, this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2266 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2267 | if (HasStackOverflow()) { |
| 2268 | ASSERT(frame_->height() == original_height); |
| 2269 | return; |
| 2270 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2271 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2272 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2276 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2277 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2278 | #ifdef DEBUG |
| 2279 | int original_height = frame_->height(); |
| 2280 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2281 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2282 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2283 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2284 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2288 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2289 | #ifdef DEBUG |
| 2290 | int original_height = frame_->height(); |
| 2291 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2292 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2293 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2294 | JumpTarget then; |
| 2295 | JumpTarget else_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2296 | LoadConditionAndSpill(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2297 | if (has_valid_frame()) { |
| 2298 | Branch(false, &else_); |
| 2299 | } |
| 2300 | if (has_valid_frame() || then.is_linked()) { |
| 2301 | then.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2302 | LoadAndSpill(node->then_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2303 | } |
| 2304 | if (else_.is_linked()) { |
| 2305 | JumpTarget exit; |
| 2306 | if (has_valid_frame()) exit.Jump(); |
| 2307 | else_.Bind(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2308 | LoadAndSpill(node->else_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2309 | if (exit.is_linked()) exit.Bind(); |
| 2310 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2311 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2315 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2316 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2317 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2318 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2319 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2320 | JumpTarget slow; |
| 2321 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2322 | |
| 2323 | // Generate fast-case code for variables that might be shadowed by |
| 2324 | // eval-introduced variables. Eval is used a lot without |
| 2325 | // introducing variables. In those cases, we do not want to |
| 2326 | // perform a runtime call for all variables in the scope |
| 2327 | // containing the eval. |
| 2328 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2329 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2330 | // If there was no control flow to slow, we can exit early. |
| 2331 | if (!slow.is_linked()) { |
| 2332 | frame_->EmitPush(r0); |
| 2333 | return; |
| 2334 | } |
| 2335 | |
| 2336 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2337 | |
| 2338 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2339 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2340 | // Only generate the fast case for locals that rewrite to slots. |
| 2341 | // This rules out argument loads. |
| 2342 | if (potential_slot != NULL) { |
| 2343 | __ ldr(r0, |
| 2344 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2345 | r1, |
| 2346 | r2, |
| 2347 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2348 | if (potential_slot->var()->mode() == Variable::CONST) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2349 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2350 | __ cmp(r0, ip); |
| 2351 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2352 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2353 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2354 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2355 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2356 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2357 | } |
| 2358 | } |
| 2359 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2360 | slow.Bind(); |
| 2361 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2362 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2363 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2364 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2365 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2366 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2367 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2368 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2369 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2370 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2371 | done.Bind(); |
| 2372 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2373 | |
| 2374 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2375 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2376 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2377 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2378 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2379 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2380 | // initialized yet) which needs to be converted into the 'undefined' |
| 2381 | // value. |
| 2382 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2383 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2384 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 2385 | __ cmp(r0, ip); |
| 2386 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2387 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2388 | } |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2393 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2394 | TypeofState typeof_state, |
| 2395 | Register tmp, |
| 2396 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2397 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2398 | // Check that no extension objects have been created by calls to |
| 2399 | // eval from the current scope to the global scope. |
| 2400 | Register context = cp; |
| 2401 | Scope* s = scope(); |
| 2402 | while (s != NULL) { |
| 2403 | if (s->num_heap_slots() > 0) { |
| 2404 | if (s->calls_eval()) { |
| 2405 | // Check that extension is NULL. |
| 2406 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2407 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2408 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2409 | } |
| 2410 | // Load next context in chain. |
| 2411 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2412 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2413 | context = tmp; |
| 2414 | } |
| 2415 | // If no outer scope calls eval, we do not need to check more |
| 2416 | // context extensions. |
| 2417 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2418 | s = s->outer_scope(); |
| 2419 | } |
| 2420 | |
| 2421 | if (s->is_eval_scope()) { |
| 2422 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2423 | if (!context.is(tmp)) { |
| 2424 | __ mov(tmp, Operand(context)); |
| 2425 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2426 | __ bind(&next); |
| 2427 | // Terminate at global context. |
| 2428 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2429 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 2430 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2431 | __ b(eq, &fast); |
| 2432 | // Check that extension is NULL. |
| 2433 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2434 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2435 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2436 | // Load next context in chain. |
| 2437 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2438 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2439 | __ b(&next); |
| 2440 | __ bind(&fast); |
| 2441 | } |
| 2442 | |
| 2443 | // All extension objects were empty and it is safe to use a global |
| 2444 | // load IC call. |
| 2445 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2446 | // Load the global object. |
| 2447 | LoadGlobal(); |
| 2448 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2449 | Result name(r2); |
| 2450 | __ mov(r2, Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2451 | // Call IC stub. |
| 2452 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2453 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2454 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2455 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2458 | // Drop the global object. The result is in r0. |
| 2459 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2463 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2464 | #ifdef DEBUG |
| 2465 | int original_height = frame_->height(); |
| 2466 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2467 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2468 | Comment cmnt(masm_, "[ Slot"); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2469 | LoadFromSlot(node, NOT_INSIDE_TYPEOF); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2470 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2471 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2472 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2473 | |
| 2474 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2475 | #ifdef DEBUG |
| 2476 | int original_height = frame_->height(); |
| 2477 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2478 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2479 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2480 | |
| 2481 | Variable* var = node->var(); |
| 2482 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2483 | if (expr != NULL) { |
| 2484 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2485 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2486 | ASSERT(var->is_global()); |
| 2487 | Reference ref(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2488 | ref.GetValueAndSpill(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2489 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2490 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2494 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2495 | #ifdef DEBUG |
| 2496 | int original_height = frame_->height(); |
| 2497 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2498 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2499 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2500 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2501 | frame_->EmitPush(r0); |
| 2502 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2506 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2507 | #ifdef DEBUG |
| 2508 | int original_height = frame_->height(); |
| 2509 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2510 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2511 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2512 | |
| 2513 | // Retrieve the literal array and check the allocated entry. |
| 2514 | |
| 2515 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2516 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2517 | |
| 2518 | // Load the literals array of the function. |
| 2519 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2520 | |
| 2521 | // Load the literal at the ast saved index. |
| 2522 | int literal_offset = |
| 2523 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2524 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2525 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2526 | JumpTarget done; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2527 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2528 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2529 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2530 | |
| 2531 | // If the entry is undefined we call the runtime system to computed |
| 2532 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2533 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2534 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2535 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2536 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2537 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2538 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2539 | frame_->EmitPush(r0); |
| 2540 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2541 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2542 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2543 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2544 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2545 | frame_->EmitPush(r2); |
| 2546 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | |
| 2550 | // This deferred code stub will be used for creating the boilerplate |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2551 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2552 | // Each created boilerplate is stored in the JSFunction and they are |
| 2553 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2554 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2555 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2556 | explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2557 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2558 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2559 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2560 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2561 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2562 | private: |
| 2563 | ObjectLiteral* node_; |
| 2564 | }; |
| 2565 | |
| 2566 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2567 | void DeferredObjectLiteral::Generate() { |
| 2568 | // Argument is passed in r1. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2569 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2570 | // 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] | 2571 | // the literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2572 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2573 | __ push(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2574 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2575 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2576 | __ push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2577 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2578 | __ mov(r0, Operand(node_->constant_properties())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2579 | __ push(r0); |
| 2580 | __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2581 | __ mov(r2, Operand(r0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2582 | // Result is returned in r2. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2586 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2587 | #ifdef DEBUG |
| 2588 | int original_height = frame_->height(); |
| 2589 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2590 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2591 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2592 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2593 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2594 | |
| 2595 | // Retrieve the literal array and check the allocated entry. |
| 2596 | |
| 2597 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2598 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2599 | |
| 2600 | // Load the literals array of the function. |
| 2601 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2602 | |
| 2603 | // Load the literal at the ast saved index. |
| 2604 | int literal_offset = |
| 2605 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2606 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2607 | |
| 2608 | // Check whether we need to materialize the object literal boilerplate. |
| 2609 | // If so, jump to the deferred code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2610 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2611 | __ cmp(r2, Operand(ip)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2612 | deferred->Branch(eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2613 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2614 | |
| 2615 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2616 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2617 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2618 | // Clone the boilerplate object. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2619 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2620 | if (node->depth() == 1) { |
| 2621 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2622 | } |
| 2623 | frame_->CallRuntime(clone_function_id, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2624 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2625 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2626 | |
| 2627 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2628 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2629 | Literal* key = property->key(); |
| 2630 | Expression* value = property->value(); |
| 2631 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2632 | case ObjectLiteral::Property::CONSTANT: |
| 2633 | break; |
| 2634 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2635 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2636 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2637 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2638 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2639 | frame_->EmitPush(r0); // dup the result |
| 2640 | LoadAndSpill(key); |
| 2641 | LoadAndSpill(value); |
| 2642 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2643 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2644 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2645 | break; |
| 2646 | } |
| 2647 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2648 | frame_->EmitPush(r0); |
| 2649 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2650 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2651 | frame_->EmitPush(r0); |
| 2652 | LoadAndSpill(value); |
| 2653 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2654 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2655 | break; |
| 2656 | } |
| 2657 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2658 | frame_->EmitPush(r0); |
| 2659 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2660 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2661 | frame_->EmitPush(r0); |
| 2662 | LoadAndSpill(value); |
| 2663 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2664 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2665 | break; |
| 2666 | } |
| 2667 | } |
| 2668 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2669 | ASSERT(frame_->height() == original_height + 1); |
| 2670 | } |
| 2671 | |
| 2672 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2673 | // This deferred code stub will be used for creating the boilerplate |
| 2674 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2675 | // Each created boilerplate is stored in the JSFunction and they are |
| 2676 | // therefore context dependent. |
| 2677 | class DeferredArrayLiteral: public DeferredCode { |
| 2678 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2679 | explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2680 | set_comment("[ DeferredArrayLiteral"); |
| 2681 | } |
| 2682 | |
| 2683 | virtual void Generate(); |
| 2684 | |
| 2685 | private: |
| 2686 | ArrayLiteral* node_; |
| 2687 | }; |
| 2688 | |
| 2689 | |
| 2690 | void DeferredArrayLiteral::Generate() { |
| 2691 | // Argument is passed in r1. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2692 | |
| 2693 | // If the entry is undefined we call the runtime system to computed |
| 2694 | // the literal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2695 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2696 | __ push(r1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2697 | // Literal index (1). |
| 2698 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2699 | __ push(r0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2700 | // Constant properties (2). |
| 2701 | __ mov(r0, Operand(node_->literals())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2702 | __ push(r0); |
| 2703 | __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2704 | __ mov(r2, Operand(r0)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2705 | // Result is returned in r2. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2709 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2710 | #ifdef DEBUG |
| 2711 | int original_height = frame_->height(); |
| 2712 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2713 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2714 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2715 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2716 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2717 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2718 | // Retrieve the literal array and check the allocated entry. |
| 2719 | |
| 2720 | // Load the function of this activation. |
| 2721 | __ ldr(r1, frame_->Function()); |
| 2722 | |
| 2723 | // Load the literals array of the function. |
| 2724 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2725 | |
| 2726 | // Load the literal at the ast saved index. |
| 2727 | int literal_offset = |
| 2728 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2729 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2730 | |
| 2731 | // Check whether we need to materialize the object literal boilerplate. |
| 2732 | // If so, jump to the deferred code. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2733 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2734 | __ cmp(r2, Operand(ip)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2735 | deferred->Branch(eq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2736 | deferred->BindExit(); |
| 2737 | |
| 2738 | // Push the object literal boilerplate. |
| 2739 | frame_->EmitPush(r2); |
| 2740 | |
| 2741 | // Clone the boilerplate object. |
| 2742 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2743 | if (node->depth() == 1) { |
| 2744 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2745 | } |
| 2746 | frame_->CallRuntime(clone_function_id, 1); |
| 2747 | frame_->EmitPush(r0); // save the result |
| 2748 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2749 | |
| 2750 | // Generate code to set the elements in the array that are not |
| 2751 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2752 | for (int i = 0; i < node->values()->length(); i++) { |
| 2753 | Expression* value = node->values()->at(i); |
| 2754 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2755 | // If value is a literal the property value is already set in the |
| 2756 | // boilerplate object. |
| 2757 | if (value->AsLiteral() != NULL) continue; |
| 2758 | // If value is a materialized literal the property value is already set |
| 2759 | // in the boilerplate object if it is simple. |
| 2760 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2761 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2762 | // The property must be set by generated code. |
| 2763 | LoadAndSpill(value); |
| 2764 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2765 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2766 | // Fetch the object literal. |
| 2767 | __ ldr(r1, frame_->Top()); |
| 2768 | // Get the elements array. |
| 2769 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2770 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2771 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2772 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2773 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2774 | |
| 2775 | // Update the write barrier for the array address. |
| 2776 | __ mov(r3, Operand(offset)); |
| 2777 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2778 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2779 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2783 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2784 | #ifdef DEBUG |
| 2785 | int original_height = frame_->height(); |
| 2786 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2787 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2788 | // Call runtime routine to allocate the catch extension object and |
| 2789 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2790 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2791 | LoadAndSpill(node->key()); |
| 2792 | LoadAndSpill(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 2793 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2794 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2795 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2799 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2800 | #ifdef DEBUG |
| 2801 | int original_height = frame_->height(); |
| 2802 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2803 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2804 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2805 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2806 | { Reference target(this, node->target()); |
| 2807 | if (target.is_illegal()) { |
| 2808 | // Fool the virtual frame into thinking that we left the assignment's |
| 2809 | // value on the frame. |
| 2810 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2811 | frame_->EmitPush(r0); |
| 2812 | ASSERT(frame_->height() == original_height + 1); |
| 2813 | return; |
| 2814 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2815 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2816 | if (node->op() == Token::ASSIGN || |
| 2817 | node->op() == Token::INIT_VAR || |
| 2818 | node->op() == Token::INIT_CONST) { |
| 2819 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2820 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2821 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2822 | // +=, *= and similar binary assignments. |
| 2823 | // Get the old value of the lhs. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2824 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2825 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2826 | bool overwrite = |
| 2827 | (node->value()->AsBinaryOperation() != NULL && |
| 2828 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2829 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2830 | SmiOperation(node->binary_op(), |
| 2831 | literal->handle(), |
| 2832 | false, |
| 2833 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2834 | frame_->EmitPush(r0); |
| 2835 | |
| 2836 | } else { |
| 2837 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2838 | GenericBinaryOperation(node->binary_op(), |
| 2839 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2840 | frame_->EmitPush(r0); |
| 2841 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2842 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2843 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2844 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2845 | if (var != NULL && |
| 2846 | (var->mode() == Variable::CONST) && |
| 2847 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2848 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2849 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2850 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2851 | CodeForSourcePosition(node->position()); |
| 2852 | if (node->op() == Token::INIT_CONST) { |
| 2853 | // Dynamic constant initializations must use the function context |
| 2854 | // and initialize the actual constant declared. Dynamic variable |
| 2855 | // initializations are simply assignments and use SetValue. |
| 2856 | target.SetValue(CONST_INIT); |
| 2857 | } else { |
| 2858 | target.SetValue(NOT_CONST_INIT); |
| 2859 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2860 | } |
| 2861 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2862 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2866 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2867 | #ifdef DEBUG |
| 2868 | int original_height = frame_->height(); |
| 2869 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2870 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2871 | Comment cmnt(masm_, "[ Throw"); |
| 2872 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2873 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2874 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2875 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2876 | frame_->EmitPush(r0); |
| 2877 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2881 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2882 | #ifdef DEBUG |
| 2883 | int original_height = frame_->height(); |
| 2884 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2885 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2886 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2887 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2888 | { Reference property(this, node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2889 | property.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2890 | } |
| 2891 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2895 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2896 | #ifdef DEBUG |
| 2897 | int original_height = frame_->height(); |
| 2898 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2899 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2900 | Comment cmnt(masm_, "[ Call"); |
| 2901 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2902 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2903 | ZoneList<Expression*>* args = node->arguments(); |
| 2904 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2905 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2906 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2907 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2908 | Property* property = function->AsProperty(); |
| 2909 | |
| 2910 | // ------------------------------------------------------------------------ |
| 2911 | // Fast-case: Use inline caching. |
| 2912 | // --- |
| 2913 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2914 | // must be resolved after the arguments have been evaluated. The IC code |
| 2915 | // automatically handles this by loading the arguments before the function |
| 2916 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2917 | // ------------------------------------------------------------------------ |
| 2918 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2919 | if (var != NULL && var->is_possibly_eval()) { |
| 2920 | // ---------------------------------- |
| 2921 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 2922 | // ---------------------------------- |
| 2923 | |
| 2924 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 2925 | // resolve the function we need to call and the receiver of the |
| 2926 | // call. Then we call the resolved function using the given |
| 2927 | // arguments. |
| 2928 | // Prepare stack for call to resolved function. |
| 2929 | LoadAndSpill(function); |
| 2930 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 2931 | frame_->EmitPush(r2); // Slot for receiver |
| 2932 | int arg_count = args->length(); |
| 2933 | for (int i = 0; i < arg_count; i++) { |
| 2934 | LoadAndSpill(args->at(i)); |
| 2935 | } |
| 2936 | |
| 2937 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 2938 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 2939 | frame_->EmitPush(r1); |
| 2940 | if (arg_count > 0) { |
| 2941 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 2942 | frame_->EmitPush(r1); |
| 2943 | } else { |
| 2944 | frame_->EmitPush(r2); |
| 2945 | } |
| 2946 | |
| 2947 | // Resolve the call. |
| 2948 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
| 2949 | |
| 2950 | // Touch up stack with the right values for the function and the receiver. |
| 2951 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
| 2952 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
| 2953 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
| 2954 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 2955 | |
| 2956 | // Call the function. |
| 2957 | CodeForSourcePosition(node->position()); |
| 2958 | |
| 2959 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 2960 | CallFunctionStub call_function(arg_count, in_loop); |
| 2961 | frame_->CallStub(&call_function, arg_count + 1); |
| 2962 | |
| 2963 | __ ldr(cp, frame_->Context()); |
| 2964 | // Remove the function from the stack. |
| 2965 | frame_->Drop(); |
| 2966 | frame_->EmitPush(r0); |
| 2967 | |
| 2968 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2969 | // ---------------------------------- |
| 2970 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 2971 | // ---------------------------------- |
| 2972 | |
| 2973 | // 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] | 2974 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2975 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2976 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2977 | // Pass the global object as the receiver and let the IC stub |
| 2978 | // patch the stack to use the global proxy as 'this' in the |
| 2979 | // invoked function. |
| 2980 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2981 | |
| 2982 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2983 | int arg_count = args->length(); |
| 2984 | for (int i = 0; i < arg_count; i++) { |
| 2985 | LoadAndSpill(args->at(i)); |
| 2986 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2987 | |
| 2988 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2989 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 2990 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2991 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2992 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 2993 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2994 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2995 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2996 | frame_->Drop(); |
| 2997 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2998 | |
| 2999 | } else if (var != NULL && var->slot() != NULL && |
| 3000 | var->slot()->type() == Slot::LOOKUP) { |
| 3001 | // ---------------------------------- |
| 3002 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3003 | // ---------------------------------- |
| 3004 | |
| 3005 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3006 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3007 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3008 | frame_->EmitPush(r0); |
| 3009 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3010 | // r0: slot value; r1: receiver |
| 3011 | |
| 3012 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3013 | frame_->EmitPush(r0); // function |
| 3014 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3015 | |
| 3016 | // Call the function. |
| 3017 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3018 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3019 | |
| 3020 | } else if (property != NULL) { |
| 3021 | // Check if the key is a literal string. |
| 3022 | Literal* literal = property->key()->AsLiteral(); |
| 3023 | |
| 3024 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3025 | // ------------------------------------------------------------------ |
| 3026 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3027 | // ------------------------------------------------------------------ |
| 3028 | |
| 3029 | // 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] | 3030 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3031 | frame_->EmitPush(r0); |
| 3032 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3033 | |
| 3034 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3035 | int arg_count = args->length(); |
| 3036 | for (int i = 0; i < arg_count; i++) { |
| 3037 | LoadAndSpill(args->at(i)); |
| 3038 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3039 | |
| 3040 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3041 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3042 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3043 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3044 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3045 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3046 | |
| 3047 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3048 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3049 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3050 | 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] | 3051 | |
| 3052 | } else { |
| 3053 | // ------------------------------------------- |
| 3054 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3055 | // ------------------------------------------- |
| 3056 | |
| 3057 | // Load the function to call from the property through a reference. |
| 3058 | Reference ref(this, property); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3059 | ref.GetValueAndSpill(); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3060 | |
| 3061 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3062 | if (property->is_synthetic()) { |
| 3063 | LoadGlobalReceiver(r0); |
| 3064 | } else { |
| 3065 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3066 | frame_->EmitPush(r0); |
| 3067 | } |
| 3068 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3069 | // Call the function. |
| 3070 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3071 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | } else { |
| 3075 | // ---------------------------------- |
| 3076 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3077 | // ---------------------------------- |
| 3078 | |
| 3079 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3080 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3081 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3082 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3083 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3084 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3085 | // Call the function. |
| 3086 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3087 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3088 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3089 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3093 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3094 | #ifdef DEBUG |
| 3095 | int original_height = frame_->height(); |
| 3096 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3097 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3098 | Comment cmnt(masm_, "[ CallNew"); |
| 3099 | |
| 3100 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3101 | // expression in new calls must be evaluated before the |
| 3102 | // arguments. This is different from ordinary calls, where the |
| 3103 | // actual function to call is resolved after the arguments have been |
| 3104 | // evaluated. |
| 3105 | |
| 3106 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3107 | // receiver. There is no need to use the global proxy here because |
| 3108 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3109 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3110 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3111 | |
| 3112 | // Push the arguments ("left-to-right") on the stack. |
| 3113 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3114 | int arg_count = args->length(); |
| 3115 | for (int i = 0; i < arg_count; i++) { |
| 3116 | LoadAndSpill(args->at(i)); |
| 3117 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3118 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3119 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3120 | Result num_args(r0); |
| 3121 | __ mov(r0, Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3122 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3123 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3124 | Result function(r1); |
| 3125 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3126 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3127 | // Call the construct call builtin that handles allocation and |
| 3128 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3129 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3130 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3131 | frame_->CallCodeObject(ic, |
| 3132 | RelocInfo::CONSTRUCT_CALL, |
| 3133 | &num_args, |
| 3134 | &function, |
| 3135 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3136 | |
| 3137 | // 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] | 3138 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3139 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3143 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
| 3144 | VirtualFrame::SpilledScope spilled_scope; |
| 3145 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3146 | JumpTarget leave, null, function, non_function_constructor; |
| 3147 | |
| 3148 | // Load the object into r0. |
| 3149 | LoadAndSpill(args->at(0)); |
| 3150 | frame_->EmitPop(r0); |
| 3151 | |
| 3152 | // If the object is a smi, we return null. |
| 3153 | __ tst(r0, Operand(kSmiTagMask)); |
| 3154 | null.Branch(eq); |
| 3155 | |
| 3156 | // Check that the object is a JS object but take special care of JS |
| 3157 | // functions to make sure they have 'Function' as their class. |
| 3158 | __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE); |
| 3159 | null.Branch(lt); |
| 3160 | |
| 3161 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 3162 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 3163 | // LAST_JS_OBJECT_TYPE. |
| 3164 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 3165 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 3166 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3167 | function.Branch(eq); |
| 3168 | |
| 3169 | // Check if the constructor in the map is a function. |
| 3170 | __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset)); |
| 3171 | __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE); |
| 3172 | non_function_constructor.Branch(ne); |
| 3173 | |
| 3174 | // The r0 register now contains the constructor function. Grab the |
| 3175 | // instance class name from there. |
| 3176 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 3177 | __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3178 | frame_->EmitPush(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3179 | leave.Jump(); |
| 3180 | |
| 3181 | // Functions have class 'Function'. |
| 3182 | function.Bind(); |
| 3183 | __ mov(r0, Operand(Factory::function_class_symbol())); |
| 3184 | frame_->EmitPush(r0); |
| 3185 | leave.Jump(); |
| 3186 | |
| 3187 | // Objects with a non-function constructor have class 'Object'. |
| 3188 | non_function_constructor.Bind(); |
| 3189 | __ mov(r0, Operand(Factory::Object_symbol())); |
| 3190 | frame_->EmitPush(r0); |
| 3191 | leave.Jump(); |
| 3192 | |
| 3193 | // Non-JS objects have class null. |
| 3194 | null.Bind(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3195 | __ LoadRoot(r0, Heap::kNullValueRootIndex); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3196 | frame_->EmitPush(r0); |
| 3197 | |
| 3198 | // All done. |
| 3199 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3203 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3204 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3205 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3206 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3207 | LoadAndSpill(args->at(0)); |
| 3208 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3209 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3210 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3211 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3212 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3213 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3214 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3215 | // Load the value. |
| 3216 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3217 | leave.Bind(); |
| 3218 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3222 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3223 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3224 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3225 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3226 | LoadAndSpill(args->at(0)); // Load the object. |
| 3227 | LoadAndSpill(args->at(1)); // Load the value. |
| 3228 | frame_->EmitPop(r0); // r0 contains value |
| 3229 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3230 | // if (object->IsSmi()) return object. |
| 3231 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3232 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3233 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3234 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3235 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3236 | // Store the value. |
| 3237 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3238 | // Update the write barrier. |
| 3239 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3240 | __ RecordWrite(r1, r2, r3); |
| 3241 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3242 | leave.Bind(); |
| 3243 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3247 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3248 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3249 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3250 | LoadAndSpill(args->at(0)); |
| 3251 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3252 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3253 | cc_reg_ = eq; |
| 3254 | } |
| 3255 | |
| 3256 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3257 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3258 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3259 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3260 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3261 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3262 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3263 | LoadAndSpill(args->at(1)); |
| 3264 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3265 | __ CallRuntime(Runtime::kLog, 2); |
| 3266 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3267 | #endif |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3268 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3269 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3273 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3274 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3275 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3276 | LoadAndSpill(args->at(0)); |
| 3277 | frame_->EmitPop(r0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3278 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3279 | cc_reg_ = eq; |
| 3280 | } |
| 3281 | |
| 3282 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3283 | // This should generate code that performs a charCodeAt() call or returns |
| 3284 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3285 | // 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] | 3286 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3287 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3288 | ASSERT(args->length() == 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3289 | Comment(masm_, "[ GenerateFastCharCodeAt"); |
| 3290 | |
| 3291 | LoadAndSpill(args->at(0)); |
| 3292 | LoadAndSpill(args->at(1)); |
| 3293 | frame_->EmitPop(r0); // Index. |
| 3294 | frame_->EmitPop(r1); // String. |
| 3295 | |
| 3296 | Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string; |
| 3297 | |
| 3298 | __ tst(r1, Operand(kSmiTagMask)); |
| 3299 | __ b(eq, &slow); // The 'string' was a Smi. |
| 3300 | |
| 3301 | ASSERT(kSmiTag == 0); |
| 3302 | __ tst(r0, Operand(kSmiTagMask | 0x80000000u)); |
| 3303 | __ b(ne, &slow); // The index was negative or not a Smi. |
| 3304 | |
| 3305 | __ bind(&try_again_with_new_string); |
| 3306 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 3307 | __ b(ge, &slow); |
| 3308 | |
| 3309 | // Now r2 has the string type. |
| 3310 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
| 3311 | __ and_(r4, r2, Operand(kStringSizeMask)); |
| 3312 | __ add(r4, r4, Operand(String::kLongLengthShift)); |
| 3313 | __ mov(r3, Operand(r3, LSR, r4)); |
| 3314 | // Now r3 has the length of the string. Compare with the index. |
| 3315 | __ cmp(r3, Operand(r0, LSR, kSmiTagSize)); |
| 3316 | __ b(le, &slow); |
| 3317 | |
| 3318 | // Here we know the index is in range. Check that string is sequential. |
| 3319 | ASSERT_EQ(0, kSeqStringTag); |
| 3320 | __ tst(r2, Operand(kStringRepresentationMask)); |
| 3321 | __ b(ne, ¬_a_flat_string); |
| 3322 | |
| 3323 | // Check whether it is an ASCII string. |
| 3324 | ASSERT_EQ(0, kTwoByteStringTag); |
| 3325 | __ tst(r2, Operand(kStringEncodingMask)); |
| 3326 | __ b(ne, &ascii_string); |
| 3327 | |
| 3328 | // 2-byte string. We can add without shifting since the Smi tag size is the |
| 3329 | // log2 of the number of bytes in a two-byte character. |
| 3330 | ASSERT_EQ(1, kSmiTagSize); |
| 3331 | ASSERT_EQ(0, kSmiShiftSize); |
| 3332 | __ add(r1, r1, Operand(r0)); |
| 3333 | __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize)); |
| 3334 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3335 | __ jmp(&end); |
| 3336 | |
| 3337 | __ bind(&ascii_string); |
| 3338 | __ add(r1, r1, Operand(r0, LSR, kSmiTagSize)); |
| 3339 | __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 3340 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
| 3341 | __ jmp(&end); |
| 3342 | |
| 3343 | __ bind(¬_a_flat_string); |
| 3344 | __ and_(r2, r2, Operand(kStringRepresentationMask)); |
| 3345 | __ cmp(r2, Operand(kConsStringTag)); |
| 3346 | __ b(ne, &slow); |
| 3347 | |
| 3348 | // ConsString. |
| 3349 | // Check that the right hand side is the empty string (ie if this is really a |
| 3350 | // flat string in a cons string). If that is not the case we would rather go |
| 3351 | // to the runtime system now, to flatten the string. |
| 3352 | __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset)); |
| 3353 | __ LoadRoot(r3, Heap::kEmptyStringRootIndex); |
| 3354 | __ cmp(r2, Operand(r3)); |
| 3355 | __ b(ne, &slow); |
| 3356 | |
| 3357 | // Get the first of the two strings. |
| 3358 | __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset)); |
| 3359 | __ jmp(&try_again_with_new_string); |
| 3360 | |
| 3361 | __ bind(&slow); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3362 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3363 | |
| 3364 | __ bind(&end); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3365 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3369 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3370 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3371 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3372 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3373 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3374 | // We need the CC bits to come out as not_equal in the case where the |
| 3375 | // object is a smi. This can't be done with the usual test opcode so |
| 3376 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3377 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3378 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3379 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3380 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3381 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3382 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3383 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3384 | cc_reg_ = eq; |
| 3385 | } |
| 3386 | |
| 3387 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame^] | 3388 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 3389 | // This generates a fast version of: |
| 3390 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
| 3391 | VirtualFrame::SpilledScope spilled_scope; |
| 3392 | ASSERT(args->length() == 1); |
| 3393 | LoadAndSpill(args->at(0)); |
| 3394 | frame_->EmitPop(r1); |
| 3395 | __ tst(r1, Operand(kSmiTagMask)); |
| 3396 | false_target()->Branch(eq); |
| 3397 | |
| 3398 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 3399 | __ cmp(r1, ip); |
| 3400 | true_target()->Branch(eq); |
| 3401 | |
| 3402 | Register map_reg = r2; |
| 3403 | __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3404 | // Undetectable objects behave like undefined when tested with typeof. |
| 3405 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 3406 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3407 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 3408 | false_target()->Branch(eq); |
| 3409 | |
| 3410 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 3411 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3412 | false_target()->Branch(lt); |
| 3413 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
| 3414 | cc_reg_ = le; |
| 3415 | } |
| 3416 | |
| 3417 | |
| 3418 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 3419 | // This generates a fast version of: |
| 3420 | // (%_ClassOf(arg) === 'Function') |
| 3421 | VirtualFrame::SpilledScope spilled_scope; |
| 3422 | ASSERT(args->length() == 1); |
| 3423 | LoadAndSpill(args->at(0)); |
| 3424 | frame_->EmitPop(r0); |
| 3425 | __ tst(r0, Operand(kSmiTagMask)); |
| 3426 | false_target()->Branch(eq); |
| 3427 | Register map_reg = r2; |
| 3428 | __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE); |
| 3429 | cc_reg_ = eq; |
| 3430 | } |
| 3431 | |
| 3432 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3433 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
| 3434 | VirtualFrame::SpilledScope spilled_scope; |
| 3435 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3436 | |
| 3437 | // Get the frame pointer for the calling frame. |
| 3438 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 3439 | |
| 3440 | // Skip the arguments adaptor frame if it exists. |
| 3441 | Label check_frame_marker; |
| 3442 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3443 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3444 | __ b(ne, &check_frame_marker); |
| 3445 | __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset)); |
| 3446 | |
| 3447 | // Check the marker in the calling frame. |
| 3448 | __ bind(&check_frame_marker); |
| 3449 | __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset)); |
| 3450 | __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3451 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3455 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3456 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3457 | ASSERT(args->length() == 0); |
| 3458 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3459 | // Seed the result with the formal parameters count, which will be used |
| 3460 | // 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] | 3461 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3462 | |
| 3463 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3464 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3465 | frame_->CallStub(&stub, 0); |
| 3466 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3470 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3471 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3472 | ASSERT(args->length() == 1); |
| 3473 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3474 | // Satisfy contract with ArgumentsAccessStub: |
| 3475 | // 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] | 3476 | LoadAndSpill(args->at(0)); |
| 3477 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3478 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3479 | |
| 3480 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3481 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3482 | frame_->CallStub(&stub, 0); |
| 3483 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3487 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3488 | VirtualFrame::SpilledScope spilled_scope; |
| 3489 | ASSERT(args->length() == 0); |
| 3490 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3491 | RelocInfo::RUNTIME_ENTRY); |
| 3492 | frame_->EmitPush(r0); |
| 3493 | } |
| 3494 | |
| 3495 | |
| 3496 | void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) { |
| 3497 | VirtualFrame::SpilledScope spilled_scope; |
| 3498 | LoadAndSpill(args->at(0)); |
| 3499 | switch (op) { |
| 3500 | case SIN: |
| 3501 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 3502 | break; |
| 3503 | case COS: |
| 3504 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 3505 | break; |
| 3506 | } |
| 3507 | frame_->EmitPush(r0); |
| 3508 | } |
| 3509 | |
| 3510 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3511 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3512 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3513 | ASSERT(args->length() == 2); |
| 3514 | |
| 3515 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3516 | LoadAndSpill(args->at(0)); |
| 3517 | LoadAndSpill(args->at(1)); |
| 3518 | frame_->EmitPop(r0); |
| 3519 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3520 | __ cmp(r0, Operand(r1)); |
| 3521 | cc_reg_ = eq; |
| 3522 | } |
| 3523 | |
| 3524 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3525 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3526 | #ifdef DEBUG |
| 3527 | int original_height = frame_->height(); |
| 3528 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3529 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3530 | if (CheckForInlineRuntimeCall(node)) { |
| 3531 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3532 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3533 | return; |
| 3534 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3535 | |
| 3536 | ZoneList<Expression*>* args = node->arguments(); |
| 3537 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3538 | Runtime::Function* function = node->function(); |
| 3539 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3540 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3541 | // Prepare stack for calling JS runtime function. |
| 3542 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3543 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3544 | // Push the builtins object found in the current global object. |
| 3545 | __ ldr(r1, GlobalObject()); |
| 3546 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3547 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3548 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3549 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3550 | // Push the arguments ("left-to-right"). |
| 3551 | int arg_count = args->length(); |
| 3552 | for (int i = 0; i < arg_count; i++) { |
| 3553 | LoadAndSpill(args->at(i)); |
| 3554 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3555 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3556 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3557 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3558 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3559 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3560 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3561 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3562 | frame_->Drop(); |
| 3563 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3564 | } else { |
| 3565 | // Call the C runtime function. |
| 3566 | frame_->CallRuntime(function, arg_count); |
| 3567 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3568 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3569 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3573 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3574 | #ifdef DEBUG |
| 3575 | int original_height = frame_->height(); |
| 3576 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3577 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3578 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3579 | |
| 3580 | Token::Value op = node->op(); |
| 3581 | |
| 3582 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3583 | LoadConditionAndSpill(node->expression(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3584 | false_target(), |
| 3585 | true_target(), |
| 3586 | true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3587 | // LoadCondition may (and usually does) leave a test and branch to |
| 3588 | // be emitted by the caller. In that case, negate the condition. |
| 3589 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3590 | |
| 3591 | } else if (op == Token::DELETE) { |
| 3592 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3593 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3594 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3595 | LoadAndSpill(property->obj()); |
| 3596 | LoadAndSpill(property->key()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3597 | Result arg_count(r0); |
| 3598 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3599 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3600 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3601 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3602 | Slot* slot = variable->slot(); |
| 3603 | if (variable->is_global()) { |
| 3604 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3605 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3606 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3607 | Result arg_count(r0); |
| 3608 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3609 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3610 | |
| 3611 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3612 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3613 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3614 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3615 | frame_->EmitPush(r0); |
| 3616 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3617 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3618 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3619 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3620 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3621 | Result arg_count(r0); |
| 3622 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3623 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3624 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3625 | } else { |
| 3626 | // Default: Result of deleting non-global, not dynamically |
| 3627 | // introduced variables is false. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3628 | __ LoadRoot(r0, Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3629 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3630 | |
| 3631 | } else { |
| 3632 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3633 | LoadAndSpill(node->expression()); // may have side-effects |
| 3634 | frame_->Drop(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3635 | __ LoadRoot(r0, Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3636 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3637 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3638 | |
| 3639 | } else if (op == Token::TYPEOF) { |
| 3640 | // Special case for loading the typeof expression; see comment on |
| 3641 | // LoadTypeofExpression(). |
| 3642 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3643 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3644 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3645 | |
| 3646 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3647 | LoadAndSpill(node->expression()); |
| 3648 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3649 | switch (op) { |
| 3650 | case Token::NOT: |
| 3651 | case Token::DELETE: |
| 3652 | case Token::TYPEOF: |
| 3653 | UNREACHABLE(); // handled above |
| 3654 | break; |
| 3655 | |
| 3656 | case Token::SUB: { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3657 | bool overwrite = |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3658 | (node->expression()->AsBinaryOperation() != NULL && |
| 3659 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3660 | UnarySubStub stub(overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3661 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3662 | break; |
| 3663 | } |
| 3664 | |
| 3665 | case Token::BIT_NOT: { |
| 3666 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3667 | JumpTarget smi_label; |
| 3668 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3669 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3670 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3671 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3672 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3673 | Result arg_count(r0); |
| 3674 | __ mov(r0, Operand(0)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3675 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3676 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3677 | continue_label.Jump(); |
| 3678 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3679 | __ mvn(r0, Operand(r0)); |
| 3680 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3681 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3682 | break; |
| 3683 | } |
| 3684 | |
| 3685 | case Token::VOID: |
| 3686 | // since the stack top is cached in r0, popping and then |
| 3687 | // pushing a value can be done by just writing to r0. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3688 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3689 | break; |
| 3690 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3691 | case Token::ADD: { |
| 3692 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3693 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3694 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3695 | continue_label.Branch(eq); |
| 3696 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3697 | Result arg_count(r0); |
| 3698 | __ mov(r0, Operand(0)); // not counting receiver |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3699 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3700 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3701 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3702 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3703 | default: |
| 3704 | UNREACHABLE(); |
| 3705 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3706 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3707 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3708 | ASSERT(!has_valid_frame() || |
| 3709 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3710 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3714 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3715 | #ifdef DEBUG |
| 3716 | int original_height = frame_->height(); |
| 3717 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3718 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3719 | Comment cmnt(masm_, "[ CountOperation"); |
| 3720 | |
| 3721 | bool is_postfix = node->is_postfix(); |
| 3722 | bool is_increment = node->op() == Token::INC; |
| 3723 | |
| 3724 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3725 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3726 | |
| 3727 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3728 | if (is_postfix) { |
| 3729 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3730 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3731 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3732 | |
| 3733 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3734 | if (target.is_illegal()) { |
| 3735 | // Spoof the virtual frame to have the expected height (one higher |
| 3736 | // than on entry). |
| 3737 | if (!is_postfix) { |
| 3738 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3739 | frame_->EmitPush(r0); |
| 3740 | } |
| 3741 | ASSERT(frame_->height() == original_height + 1); |
| 3742 | return; |
| 3743 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3744 | target.GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3745 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3746 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3747 | JumpTarget slow; |
| 3748 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3749 | |
| 3750 | // Load the value (1) into register r1. |
| 3751 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3752 | |
| 3753 | // Check for smi operand. |
| 3754 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3755 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3756 | |
| 3757 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3758 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3759 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3760 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3761 | |
| 3762 | // Perform optimistic increment/decrement. |
| 3763 | if (is_increment) { |
| 3764 | __ add(r0, r0, Operand(r1), SetCC); |
| 3765 | } else { |
| 3766 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3767 | } |
| 3768 | |
| 3769 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3770 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3771 | |
| 3772 | // Revert optimistic increment/decrement. |
| 3773 | if (is_increment) { |
| 3774 | __ sub(r0, r0, Operand(r1)); |
| 3775 | } else { |
| 3776 | __ add(r0, r0, Operand(r1)); |
| 3777 | } |
| 3778 | |
| 3779 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3780 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3781 | { |
| 3782 | // Convert the operand to a number. |
| 3783 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3784 | Result arg_count(r0); |
| 3785 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3786 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3787 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3788 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3789 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3790 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3793 | // Compute the new value. |
| 3794 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3795 | frame_->EmitPush(r0); |
| 3796 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3797 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3798 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3799 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3800 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3804 | exit.Bind(); |
| 3805 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3806 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3810 | if (is_postfix) frame_->EmitPop(r0); |
| 3811 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3815 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3816 | #ifdef DEBUG |
| 3817 | int original_height = frame_->height(); |
| 3818 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3819 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3820 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3821 | Token::Value op = node->op(); |
| 3822 | |
| 3823 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3824 | // operators must yield the result of one of the two expressions |
| 3825 | // before any ToBoolean() conversions. This means that the value |
| 3826 | // produced by a && or || operator is not necessarily a boolean. |
| 3827 | |
| 3828 | // NOTE: If the left hand side produces a materialized value (not in |
| 3829 | // the CC register), we force the right hand side to do the |
| 3830 | // same. This is necessary because we may have to branch to the exit |
| 3831 | // after evaluating the left hand side (due to the shortcut |
| 3832 | // semantics), but the compiler must (statically) know if the result |
| 3833 | // of compiling the binary operation is materialized or not. |
| 3834 | |
| 3835 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3836 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3837 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3838 | &is_true, |
| 3839 | false_target(), |
| 3840 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3841 | if (has_valid_frame() && !has_cc()) { |
| 3842 | // 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] | 3843 | JumpTarget pop_and_continue; |
| 3844 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3845 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3846 | __ ldr(r0, frame_->Top()); // Duplicate the stack top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3847 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3848 | // Avoid popping the result if it converts to 'false' using the |
| 3849 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3850 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3851 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3852 | Branch(false, &exit); |
| 3853 | |
| 3854 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3855 | pop_and_continue.Bind(); |
| 3856 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3857 | |
| 3858 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3859 | is_true.Bind(); |
| 3860 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3861 | |
| 3862 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3863 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3864 | } else if (has_cc() || is_true.is_linked()) { |
| 3865 | // The left-hand side is either (a) partially compiled to |
| 3866 | // control flow with a final branch left to emit or (b) fully |
| 3867 | // compiled to control flow and possibly true. |
| 3868 | if (has_cc()) { |
| 3869 | Branch(false, false_target()); |
| 3870 | } |
| 3871 | is_true.Bind(); |
| 3872 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3873 | true_target(), |
| 3874 | false_target(), |
| 3875 | false); |
| 3876 | } else { |
| 3877 | // Nothing to do. |
| 3878 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3882 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3883 | LoadConditionAndSpill(node->left(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3884 | true_target(), |
| 3885 | &is_false, |
| 3886 | false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3887 | if (has_valid_frame() && !has_cc()) { |
| 3888 | // 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] | 3889 | JumpTarget pop_and_continue; |
| 3890 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3891 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3892 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3893 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3894 | // Avoid popping the result if it converts to 'true' using the |
| 3895 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3896 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3897 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3898 | Branch(true, &exit); |
| 3899 | |
| 3900 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3901 | pop_and_continue.Bind(); |
| 3902 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3903 | |
| 3904 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3905 | is_false.Bind(); |
| 3906 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3907 | |
| 3908 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3909 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3910 | } else if (has_cc() || is_false.is_linked()) { |
| 3911 | // The left-hand side is either (a) partially compiled to |
| 3912 | // control flow with a final branch left to emit or (b) fully |
| 3913 | // compiled to control flow and possibly false. |
| 3914 | if (has_cc()) { |
| 3915 | Branch(true, true_target()); |
| 3916 | } |
| 3917 | is_false.Bind(); |
| 3918 | LoadConditionAndSpill(node->right(), |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3919 | true_target(), |
| 3920 | false_target(), |
| 3921 | false); |
| 3922 | } else { |
| 3923 | // Nothing to do. |
| 3924 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | } else { |
| 3928 | // Optimize for the case where (at least) one of the expressions |
| 3929 | // is a literal small integer. |
| 3930 | Literal* lliteral = node->left()->AsLiteral(); |
| 3931 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3932 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 3933 | // never return a constant/immutable object. |
| 3934 | bool overwrite_left = |
| 3935 | (node->left()->AsBinaryOperation() != NULL && |
| 3936 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3937 | bool overwrite_right = |
| 3938 | (node->right()->AsBinaryOperation() != NULL && |
| 3939 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3940 | |
| 3941 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3942 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3943 | SmiOperation(node->op(), |
| 3944 | rliteral->handle(), |
| 3945 | false, |
| 3946 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3947 | |
| 3948 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3949 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3950 | SmiOperation(node->op(), |
| 3951 | lliteral->handle(), |
| 3952 | true, |
| 3953 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3954 | |
| 3955 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3956 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 3957 | if (overwrite_left) { |
| 3958 | overwrite_mode = OVERWRITE_LEFT; |
| 3959 | } else if (overwrite_right) { |
| 3960 | overwrite_mode = OVERWRITE_RIGHT; |
| 3961 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3962 | LoadAndSpill(node->left()); |
| 3963 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3964 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3965 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3966 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3967 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3968 | ASSERT(!has_valid_frame() || |
| 3969 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3970 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3974 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3975 | #ifdef DEBUG |
| 3976 | int original_height = frame_->height(); |
| 3977 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3978 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3979 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3980 | frame_->EmitPush(r0); |
| 3981 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3985 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3986 | #ifdef DEBUG |
| 3987 | int original_height = frame_->height(); |
| 3988 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3989 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3990 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3991 | |
| 3992 | // Get the expressions from the node. |
| 3993 | Expression* left = node->left(); |
| 3994 | Expression* right = node->right(); |
| 3995 | Token::Value op = node->op(); |
| 3996 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3997 | // To make null checks efficient, we check if either left or right is the |
| 3998 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3999 | // instead of calling the (very) general runtime routine for checking |
| 4000 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4001 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4002 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4003 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4004 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4005 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 4006 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4007 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4008 | LoadAndSpill(left_is_null ? right : left); |
| 4009 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4010 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4011 | __ cmp(r0, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4012 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4013 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 4014 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4015 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4016 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4017 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4018 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4019 | __ cmp(r0, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4020 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4021 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4022 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4023 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4024 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4025 | // It can be an undetectable object. |
| 4026 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4027 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 4028 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 4029 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4032 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4033 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4034 | return; |
| 4035 | } |
| 4036 | } |
| 4037 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4038 | // To make typeof testing for natives implemented in JavaScript really |
| 4039 | // efficient, we generate special code for expressions of the form: |
| 4040 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4041 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 4042 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4043 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4044 | (right->AsLiteral() != NULL && |
| 4045 | right->AsLiteral()->handle()->IsString())) { |
| 4046 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4047 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4048 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4049 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4050 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4051 | |
| 4052 | if (check->Equals(Heap::number_symbol())) { |
| 4053 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4054 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4055 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4056 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 4057 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4058 | cc_reg_ = eq; |
| 4059 | |
| 4060 | } else if (check->Equals(Heap::string_symbol())) { |
| 4061 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4062 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4063 | |
| 4064 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4065 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4066 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4067 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4068 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4069 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4070 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4071 | |
| 4072 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 4073 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 4074 | cc_reg_ = lt; |
| 4075 | |
| 4076 | } else if (check->Equals(Heap::boolean_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4077 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 4078 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4079 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4080 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 4081 | __ cmp(r1, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4082 | cc_reg_ = eq; |
| 4083 | |
| 4084 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4085 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 4086 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4087 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4088 | |
| 4089 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4090 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4091 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4092 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4093 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4094 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4095 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4096 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4097 | |
| 4098 | cc_reg_ = eq; |
| 4099 | |
| 4100 | } else if (check->Equals(Heap::function_symbol())) { |
| 4101 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4102 | false_target()->Branch(eq); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4103 | Register map_reg = r2; |
| 4104 | __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE); |
| 4105 | true_target()->Branch(eq); |
| 4106 | // Regular expressions are callable so typeof == 'function'. |
| 4107 | __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4108 | cc_reg_ = eq; |
| 4109 | |
| 4110 | } else if (check->Equals(Heap::object_symbol())) { |
| 4111 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4112 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4113 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4114 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 4115 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4116 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4117 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4118 | Register map_reg = r2; |
| 4119 | __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE); |
| 4120 | false_target()->Branch(eq); |
| 4121 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4122 | // It can be an undetectable object. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4123 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4124 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4125 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4126 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4127 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4128 | __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4129 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4130 | false_target()->Branch(lt); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4131 | __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4132 | cc_reg_ = le; |
| 4133 | |
| 4134 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4135 | // Uncommon case: typeof testing against a string literal that is |
| 4136 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4137 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4138 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4139 | ASSERT(!has_valid_frame() || |
| 4140 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4141 | return; |
| 4142 | } |
| 4143 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4144 | switch (op) { |
| 4145 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4146 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4147 | break; |
| 4148 | |
| 4149 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4150 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4151 | break; |
| 4152 | |
| 4153 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4154 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4155 | break; |
| 4156 | |
| 4157 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4158 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4159 | break; |
| 4160 | |
| 4161 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4162 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4163 | break; |
| 4164 | |
| 4165 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4166 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4167 | break; |
| 4168 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4169 | case Token::IN: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4170 | LoadAndSpill(left); |
| 4171 | LoadAndSpill(right); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4172 | Result arg_count(r0); |
| 4173 | __ mov(r0, Operand(1)); // not counting receiver |
| 4174 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, &arg_count, 2); |
| 4175 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4176 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4177 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4178 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4179 | case Token::INSTANCEOF: { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4180 | LoadAndSpill(left); |
| 4181 | LoadAndSpill(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4182 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4183 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4184 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4185 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4186 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4187 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4188 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4189 | |
| 4190 | default: |
| 4191 | UNREACHABLE(); |
| 4192 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4193 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4194 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4198 | #ifdef DEBUG |
| 4199 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4200 | #endif |
| 4201 | |
| 4202 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4203 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4204 | #define __ ACCESS_MASM(masm) |
| 4205 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4206 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4207 | Handle<String> Reference::GetName() { |
| 4208 | ASSERT(type_ == NAMED); |
| 4209 | Property* property = expression_->AsProperty(); |
| 4210 | if (property == NULL) { |
| 4211 | // Global variable reference treated as a named property reference. |
| 4212 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4213 | ASSERT(proxy->AsVariable() != NULL); |
| 4214 | ASSERT(proxy->AsVariable()->is_global()); |
| 4215 | return proxy->name(); |
| 4216 | } else { |
| 4217 | Literal* raw_name = property->key()->AsLiteral(); |
| 4218 | ASSERT(raw_name != NULL); |
| 4219 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4220 | } |
| 4221 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4222 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4223 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4224 | void Reference::GetValue() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4225 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4226 | ASSERT(!is_illegal()); |
| 4227 | ASSERT(!cgen_->has_cc()); |
| 4228 | MacroAssembler* masm = cgen_->masm(); |
| 4229 | Property* property = expression_->AsProperty(); |
| 4230 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4231 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | switch (type_) { |
| 4235 | case SLOT: { |
| 4236 | Comment cmnt(masm, "[ Load from Slot"); |
| 4237 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4238 | ASSERT(slot != NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4239 | cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4240 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4243 | case NAMED: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4244 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4245 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4246 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4247 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4248 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4249 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4250 | Result name_reg(r2); |
| 4251 | __ mov(r2, Operand(name)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4252 | ASSERT(var == NULL || var->is_global()); |
| 4253 | RelocInfo::Mode rmode = (var == NULL) |
| 4254 | ? RelocInfo::CODE_TARGET |
| 4255 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4256 | frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4257 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4258 | break; |
| 4259 | } |
| 4260 | |
| 4261 | case KEYED: { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4262 | // TODO(181): Implement inlined version of array indexing once |
| 4263 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4264 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4265 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4266 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4267 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4268 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4269 | ASSERT(var == NULL || var->is_global()); |
| 4270 | RelocInfo::Mode rmode = (var == NULL) |
| 4271 | ? RelocInfo::CODE_TARGET |
| 4272 | : RelocInfo::CODE_TARGET_CONTEXT; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4273 | frame->CallCodeObject(ic, rmode, 0); |
| 4274 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4275 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | default: |
| 4279 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4280 | } |
| 4281 | } |
| 4282 | |
| 4283 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4284 | void Reference::SetValue(InitState init_state) { |
| 4285 | ASSERT(!is_illegal()); |
| 4286 | ASSERT(!cgen_->has_cc()); |
| 4287 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4288 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4289 | Property* property = expression_->AsProperty(); |
| 4290 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4291 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4292 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4293 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4294 | switch (type_) { |
| 4295 | case SLOT: { |
| 4296 | Comment cmnt(masm, "[ Store to Slot"); |
| 4297 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4298 | ASSERT(slot != NULL); |
| 4299 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4300 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4301 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4302 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4303 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4304 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4305 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4306 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4307 | if (init_state == CONST_INIT) { |
| 4308 | // Same as the case for a normal store, but ignores attribute |
| 4309 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4310 | // const properties (introduced via eval("const foo = (some |
| 4311 | // expr);")). Also, uses the current function context instead of |
| 4312 | // the top context. |
| 4313 | // |
| 4314 | // Note that we must declare the foo upon entry of eval(), via a |
| 4315 | // context slot declaration, but we cannot initialize it at the |
| 4316 | // same time, because the const declaration may be at the end of |
| 4317 | // the eval code (sigh...) and the const variable may have been |
| 4318 | // used before (where its value is 'undefined'). Thus, we can only |
| 4319 | // do the initialization when we actually encounter the expression |
| 4320 | // and when the expression operands are defined and valid, and |
| 4321 | // thus we need the split into 2 operations: declaration of the |
| 4322 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4323 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4324 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4325 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4326 | } |
| 4327 | // Storing a variable must keep the (new) value on the expression |
| 4328 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4329 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4330 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4331 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4332 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4333 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4334 | JumpTarget exit; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4335 | if (init_state == CONST_INIT) { |
| 4336 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4337 | // Only the first const initialization must be executed (the slot |
| 4338 | // still contains 'the hole' value). When the assignment is |
| 4339 | // executed, the code is identical to a normal store (see below). |
| 4340 | Comment cmnt(masm, "[ Init const"); |
| 4341 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 4342 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 4343 | __ cmp(r2, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4344 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4345 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4346 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4347 | // We must execute the store. Storing a variable must keep the |
| 4348 | // (new) value on the stack. This is necessary for compiling |
| 4349 | // assignment expressions. |
| 4350 | // |
| 4351 | // Note: We will reach here even with slot->var()->mode() == |
| 4352 | // Variable::CONST because of const declarations which will |
| 4353 | // initialize consts to 'the hole' value and by doing so, end up |
| 4354 | // calling this code. r2 may be loaded with context; used below in |
| 4355 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4356 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4357 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4358 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4359 | if (slot->type() == Slot::CONTEXT) { |
| 4360 | // Skip write barrier if the written value is a smi. |
| 4361 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4362 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4363 | // r2 is loaded with context when calling SlotOperand above. |
| 4364 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4365 | __ mov(r3, Operand(offset)); |
| 4366 | __ RecordWrite(r2, r3, r1); |
| 4367 | } |
| 4368 | // If we definitely did not jump over the assignment, we do not need |
| 4369 | // to bind the exit label. Doing so can defeat peephole |
| 4370 | // optimization. |
| 4371 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4372 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4373 | } |
| 4374 | } |
| 4375 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4378 | case NAMED: { |
| 4379 | Comment cmnt(masm, "[ Store to named Property"); |
| 4380 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4381 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4382 | Handle<String> name(GetName()); |
| 4383 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4384 | Result value(r0); |
| 4385 | frame->EmitPop(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4386 | |
| 4387 | // Setup the name register. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4388 | Result property_name(r2); |
| 4389 | __ mov(r2, Operand(name)); |
| 4390 | frame->CallCodeObject(ic, |
| 4391 | RelocInfo::CODE_TARGET, |
| 4392 | &value, |
| 4393 | &property_name, |
| 4394 | 0); |
| 4395 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4396 | break; |
| 4397 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4398 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4399 | case KEYED: { |
| 4400 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4401 | Property* property = expression_->AsProperty(); |
| 4402 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4403 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4404 | |
| 4405 | // Call IC code. |
| 4406 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4407 | // 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] | 4408 | Result value(r0); |
| 4409 | frame->EmitPop(r0); // value |
| 4410 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4411 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4412 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4413 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4414 | |
| 4415 | default: |
| 4416 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4421 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4422 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4423 | // (31 instead of 32). |
| 4424 | static void CountLeadingZeros( |
| 4425 | MacroAssembler* masm, |
| 4426 | Register source, |
| 4427 | Register scratch, |
| 4428 | Register zeros) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4429 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4430 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4431 | #else |
| 4432 | __ mov(zeros, Operand(0)); |
| 4433 | __ mov(scratch, source); |
| 4434 | // Top 16. |
| 4435 | __ tst(scratch, Operand(0xffff0000)); |
| 4436 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4437 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4438 | // Top 8. |
| 4439 | __ tst(scratch, Operand(0xff000000)); |
| 4440 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4441 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4442 | // Top 4. |
| 4443 | __ tst(scratch, Operand(0xf0000000)); |
| 4444 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4445 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4446 | // Top 2. |
| 4447 | __ tst(scratch, Operand(0xc0000000)); |
| 4448 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4449 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4450 | // Top bit. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4451 | __ tst(scratch, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4452 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4453 | #endif |
| 4454 | } |
| 4455 | |
| 4456 | |
| 4457 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4458 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4459 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4460 | // scratch register. Destroys the source register. No GC occurs during this |
| 4461 | // stub so you don't have to set up the frame. |
| 4462 | class ConvertToDoubleStub : public CodeStub { |
| 4463 | public: |
| 4464 | ConvertToDoubleStub(Register result_reg_1, |
| 4465 | Register result_reg_2, |
| 4466 | Register source_reg, |
| 4467 | Register scratch_reg) |
| 4468 | : result1_(result_reg_1), |
| 4469 | result2_(result_reg_2), |
| 4470 | source_(source_reg), |
| 4471 | zeros_(scratch_reg) { } |
| 4472 | |
| 4473 | private: |
| 4474 | Register result1_; |
| 4475 | Register result2_; |
| 4476 | Register source_; |
| 4477 | Register zeros_; |
| 4478 | |
| 4479 | // Minor key encoding in 16 bits. |
| 4480 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4481 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4482 | |
| 4483 | Major MajorKey() { return ConvertToDouble; } |
| 4484 | int MinorKey() { |
| 4485 | // Encode the parameters in a unique 16 bit value. |
| 4486 | return result1_.code() + |
| 4487 | (result2_.code() << 4) + |
| 4488 | (source_.code() << 8) + |
| 4489 | (zeros_.code() << 12); |
| 4490 | } |
| 4491 | |
| 4492 | void Generate(MacroAssembler* masm); |
| 4493 | |
| 4494 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4495 | |
| 4496 | #ifdef DEBUG |
| 4497 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4498 | #endif |
| 4499 | }; |
| 4500 | |
| 4501 | |
| 4502 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4503 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4504 | Register exponent = result1_; |
| 4505 | Register mantissa = result2_; |
| 4506 | #else |
| 4507 | Register exponent = result2_; |
| 4508 | Register mantissa = result1_; |
| 4509 | #endif |
| 4510 | Label not_special; |
| 4511 | // Convert from Smi to integer. |
| 4512 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4513 | // Move sign bit from source to destination. This works because the sign bit |
| 4514 | // in the exponent word of the double has the same position and polarity as |
| 4515 | // the 2's complement sign bit in a Smi. |
| 4516 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4517 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4518 | // Subtract from 0 if source was negative. |
| 4519 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4520 | __ cmp(source_, Operand(1)); |
| 4521 | __ b(gt, ¬_special); |
| 4522 | |
| 4523 | // We have -1, 0 or 1, which we treat specially. |
| 4524 | __ cmp(source_, Operand(0)); |
| 4525 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4526 | static const uint32_t exponent_word_for_1 = |
| 4527 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4528 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4529 | // 1, 0 and -1 all have 0 for the second word. |
| 4530 | __ mov(mantissa, Operand(0)); |
| 4531 | __ Ret(); |
| 4532 | |
| 4533 | __ bind(¬_special); |
| 4534 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4535 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4536 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4537 | // Compute exponent and or it into the exponent register. |
| 4538 | // We use result2 as a scratch register here. |
| 4539 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4540 | __ orr(exponent, |
| 4541 | exponent, |
| 4542 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4543 | // Shift up the source chopping the top bit off. |
| 4544 | __ add(zeros_, zeros_, Operand(1)); |
| 4545 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4546 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4547 | // Compute lower part of fraction (last 12 bits). |
| 4548 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4549 | // And the top (top 20 bits). |
| 4550 | __ orr(exponent, |
| 4551 | exponent, |
| 4552 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4553 | __ Ret(); |
| 4554 | } |
| 4555 | |
| 4556 | |
| 4557 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4558 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4559 | // so you don't have to set up the frame. |
| 4560 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4561 | public: |
| 4562 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4563 | Register the_heap_number, |
| 4564 | Register scratch) |
| 4565 | : the_int_(the_int), |
| 4566 | the_heap_number_(the_heap_number), |
| 4567 | scratch_(scratch) { } |
| 4568 | |
| 4569 | private: |
| 4570 | Register the_int_; |
| 4571 | Register the_heap_number_; |
| 4572 | Register scratch_; |
| 4573 | |
| 4574 | // Minor key encoding in 16 bits. |
| 4575 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4576 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4577 | |
| 4578 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4579 | int MinorKey() { |
| 4580 | // Encode the parameters in a unique 16 bit value. |
| 4581 | return the_int_.code() + |
| 4582 | (the_heap_number_.code() << 4) + |
| 4583 | (scratch_.code() << 8); |
| 4584 | } |
| 4585 | |
| 4586 | void Generate(MacroAssembler* masm); |
| 4587 | |
| 4588 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4589 | |
| 4590 | #ifdef DEBUG |
| 4591 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4592 | #endif |
| 4593 | }; |
| 4594 | |
| 4595 | |
| 4596 | // See comment for class. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4597 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4598 | Label max_negative_int; |
| 4599 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4600 | // We test for the special value that has a different exponent. This test |
| 4601 | // has the neat side effect of setting the flags according to the sign. |
| 4602 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4603 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4604 | __ b(eq, &max_negative_int); |
| 4605 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4606 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4607 | uint32_t non_smi_exponent = |
| 4608 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4609 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4610 | // Set the sign bit in scratch_ if the value was negative. |
| 4611 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4612 | // Subtract from 0 if the value was negative. |
| 4613 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4614 | // We should be masking the implict first digit of the mantissa away here, |
| 4615 | // but it just ends up combining harmlessly with the last digit of the |
| 4616 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4617 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4618 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4619 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4620 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4621 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4622 | HeapNumber::kExponentOffset)); |
| 4623 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4624 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4625 | HeapNumber::kMantissaOffset)); |
| 4626 | __ Ret(); |
| 4627 | |
| 4628 | __ bind(&max_negative_int); |
| 4629 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4630 | // a double because it uses a sign bit instead of using two's complement. |
| 4631 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4632 | // significant 1 bit is not stored. |
| 4633 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4634 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4635 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4636 | __ mov(ip, Operand(0)); |
| 4637 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4638 | __ Ret(); |
| 4639 | } |
| 4640 | |
| 4641 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4642 | // Handle the case where the lhs and rhs are the same object. |
| 4643 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 4644 | // for "identity and not NaN". |
| 4645 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 4646 | Label* slow, |
| 4647 | Condition cc) { |
| 4648 | Label not_identical; |
| 4649 | __ cmp(r0, Operand(r1)); |
| 4650 | __ b(ne, ¬_identical); |
| 4651 | |
| 4652 | Register exp_mask_reg = r5; |
| 4653 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4654 | |
| 4655 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 4656 | // so we do the second best thing - test it ourselves. |
| 4657 | Label heap_number, return_equal; |
| 4658 | // They are both equal and they are not both Smis so both of them are not |
| 4659 | // Smis. If it's not a heap number, then return equal. |
| 4660 | if (cc == lt || cc == gt) { |
| 4661 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
| 4662 | __ b(ge, slow); |
| 4663 | } else { |
| 4664 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4665 | __ b(eq, &heap_number); |
| 4666 | // Comparing JS objects with <=, >= is complicated. |
| 4667 | if (cc != eq) { |
| 4668 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4669 | __ b(ge, slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4670 | // Normally here we fall through to return_equal, but undefined is |
| 4671 | // special: (undefined == undefined) == true, but (undefined <= undefined) |
| 4672 | // == false! See ECMAScript 11.8.5. |
| 4673 | if (cc == le || cc == ge) { |
| 4674 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 4675 | __ b(ne, &return_equal); |
| 4676 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
| 4677 | __ cmp(r0, Operand(r2)); |
| 4678 | __ b(ne, &return_equal); |
| 4679 | if (cc == le) { |
| 4680 | __ mov(r0, Operand(GREATER)); // undefined <= undefined should fail. |
| 4681 | } else { |
| 4682 | __ mov(r0, Operand(LESS)); // undefined >= undefined should fail. |
| 4683 | } |
| 4684 | __ mov(pc, Operand(lr)); // Return. |
| 4685 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4686 | } |
| 4687 | } |
| 4688 | __ bind(&return_equal); |
| 4689 | if (cc == lt) { |
| 4690 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 4691 | } else if (cc == gt) { |
| 4692 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 4693 | } else { |
| 4694 | __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves. |
| 4695 | } |
| 4696 | __ mov(pc, Operand(lr)); // Return. |
| 4697 | |
| 4698 | // For less and greater we don't have to check for NaN since the result of |
| 4699 | // x < x is false regardless. For the others here is some code to check |
| 4700 | // for NaN. |
| 4701 | if (cc != lt && cc != gt) { |
| 4702 | __ bind(&heap_number); |
| 4703 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 4704 | // not NaN. |
| 4705 | // The representation of NaN values has all exponent bits (52..62) set, |
| 4706 | // and not all mantissa bits (0..51) clear. |
| 4707 | // Read top bits of double representation (second word of value). |
| 4708 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 4709 | // Test that exponent bits are all set. |
| 4710 | __ and_(r3, r2, Operand(exp_mask_reg)); |
| 4711 | __ cmp(r3, Operand(exp_mask_reg)); |
| 4712 | __ b(ne, &return_equal); |
| 4713 | |
| 4714 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 4715 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 4716 | // Or with all low-bits of mantissa. |
| 4717 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 4718 | __ orr(r0, r3, Operand(r2), SetCC); |
| 4719 | // For equal we already have the right value in r0: Return zero (equal) |
| 4720 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if not |
| 4721 | // (it's a NaN). For <= and >= we need to load r0 with the failing value |
| 4722 | // if it's a NaN. |
| 4723 | if (cc != eq) { |
| 4724 | // All-zero means Infinity means equal. |
| 4725 | __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal |
| 4726 | if (cc == le) { |
| 4727 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 4728 | } else { |
| 4729 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 4730 | } |
| 4731 | } |
| 4732 | __ mov(pc, Operand(lr)); // Return. |
| 4733 | } |
| 4734 | // No fall through here. |
| 4735 | |
| 4736 | __ bind(¬_identical); |
| 4737 | } |
| 4738 | |
| 4739 | |
| 4740 | // See comment at call site. |
| 4741 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
| 4742 | Label* rhs_not_nan, |
| 4743 | Label* slow, |
| 4744 | bool strict) { |
| 4745 | Label lhs_is_smi; |
| 4746 | __ tst(r0, Operand(kSmiTagMask)); |
| 4747 | __ b(eq, &lhs_is_smi); |
| 4748 | |
| 4749 | // Rhs is a Smi. Check whether the non-smi is a heap number. |
| 4750 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4751 | if (strict) { |
| 4752 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4753 | // succeed. Return non-equal (r0 is already not zero) |
| 4754 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4755 | } else { |
| 4756 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4757 | // the runtime. |
| 4758 | __ b(ne, slow); |
| 4759 | } |
| 4760 | |
| 4761 | // Rhs is a smi, lhs is a number. |
| 4762 | __ push(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4763 | |
| 4764 | if (CpuFeatures::IsSupported(VFP3)) { |
| 4765 | CpuFeatures::Scope scope(VFP3); |
| 4766 | __ IntegerToDoubleConversionWithVFP3(r1, r3, r2); |
| 4767 | } else { |
| 4768 | __ mov(r7, Operand(r1)); |
| 4769 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 4770 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4771 | } |
| 4772 | |
| 4773 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4774 | // r3 and r2 are rhs as double. |
| 4775 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4776 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4777 | // We now have both loaded as doubles but we can skip the lhs nan check |
| 4778 | // since it's a Smi. |
| 4779 | __ pop(lr); |
| 4780 | __ jmp(rhs_not_nan); |
| 4781 | |
| 4782 | __ bind(&lhs_is_smi); |
| 4783 | // Lhs is a Smi. Check whether the non-smi is a heap number. |
| 4784 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4785 | if (strict) { |
| 4786 | // If lhs was not a number and rhs was a Smi then strict equality cannot |
| 4787 | // succeed. Return non-equal. |
| 4788 | __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal. |
| 4789 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return. |
| 4790 | } else { |
| 4791 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 4792 | // the runtime. |
| 4793 | __ b(ne, slow); |
| 4794 | } |
| 4795 | |
| 4796 | // Lhs is a smi, rhs is a number. |
| 4797 | // r0 is Smi and r1 is heap number. |
| 4798 | __ push(lr); |
| 4799 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4800 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4801 | |
| 4802 | if (CpuFeatures::IsSupported(VFP3)) { |
| 4803 | CpuFeatures::Scope scope(VFP3); |
| 4804 | __ IntegerToDoubleConversionWithVFP3(r0, r1, r0); |
| 4805 | } else { |
| 4806 | __ mov(r7, Operand(r0)); |
| 4807 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4808 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4809 | } |
| 4810 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4811 | __ pop(lr); |
| 4812 | // Fall through to both_loaded_as_doubles. |
| 4813 | } |
| 4814 | |
| 4815 | |
| 4816 | void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) { |
| 4817 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4818 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4819 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4820 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4821 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4822 | Label one_is_nan, neither_is_nan; |
| 4823 | |
| 4824 | Register exp_mask_reg = r5; |
| 4825 | |
| 4826 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4827 | __ and_(r4, rhs_exponent, Operand(exp_mask_reg)); |
| 4828 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4829 | __ b(ne, rhs_not_nan); |
| 4830 | __ mov(r4, |
| 4831 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4832 | SetCC); |
| 4833 | __ b(ne, &one_is_nan); |
| 4834 | __ cmp(rhs_mantissa, Operand(0)); |
| 4835 | __ b(ne, &one_is_nan); |
| 4836 | |
| 4837 | __ bind(rhs_not_nan); |
| 4838 | __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask)); |
| 4839 | __ and_(r4, lhs_exponent, Operand(exp_mask_reg)); |
| 4840 | __ cmp(r4, Operand(exp_mask_reg)); |
| 4841 | __ b(ne, &neither_is_nan); |
| 4842 | __ mov(r4, |
| 4843 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 4844 | SetCC); |
| 4845 | __ b(ne, &one_is_nan); |
| 4846 | __ cmp(lhs_mantissa, Operand(0)); |
| 4847 | __ b(eq, &neither_is_nan); |
| 4848 | |
| 4849 | __ bind(&one_is_nan); |
| 4850 | // NaN comparisons always fail. |
| 4851 | // Load whatever we need in r0 to make the comparison fail. |
| 4852 | if (cc == lt || cc == le) { |
| 4853 | __ mov(r0, Operand(GREATER)); |
| 4854 | } else { |
| 4855 | __ mov(r0, Operand(LESS)); |
| 4856 | } |
| 4857 | __ mov(pc, Operand(lr)); // Return. |
| 4858 | |
| 4859 | __ bind(&neither_is_nan); |
| 4860 | } |
| 4861 | |
| 4862 | |
| 4863 | // See comment at call site. |
| 4864 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 4865 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
| 4866 | Register lhs_exponent = exp_first ? r0 : r1; |
| 4867 | Register rhs_exponent = exp_first ? r2 : r3; |
| 4868 | Register lhs_mantissa = exp_first ? r1 : r0; |
| 4869 | Register rhs_mantissa = exp_first ? r3 : r2; |
| 4870 | |
| 4871 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 4872 | if (cc == eq) { |
| 4873 | // Doubles are not equal unless they have the same bit pattern. |
| 4874 | // Exception: 0 and -0. |
| 4875 | __ cmp(lhs_mantissa, Operand(rhs_mantissa)); |
| 4876 | __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne); |
| 4877 | // Return non-zero if the numbers are unequal. |
| 4878 | __ mov(pc, Operand(lr), LeaveCC, ne); |
| 4879 | |
| 4880 | __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC); |
| 4881 | // If exponents are equal then return 0. |
| 4882 | __ mov(pc, Operand(lr), LeaveCC, eq); |
| 4883 | |
| 4884 | // Exponents are unequal. The only way we can return that the numbers |
| 4885 | // are equal is if one is -0 and the other is 0. We already dealt |
| 4886 | // with the case where both are -0 or both are 0. |
| 4887 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 4888 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 4889 | // equal. |
| 4890 | __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC); |
| 4891 | __ mov(r0, Operand(r4), LeaveCC, ne); |
| 4892 | __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally. |
| 4893 | // Now they are equal if and only if the lhs exponent is zero in its |
| 4894 | // low 31 bits. |
| 4895 | __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize)); |
| 4896 | __ mov(pc, Operand(lr)); |
| 4897 | } else { |
| 4898 | // Call a native function to do a comparison between two non-NaNs. |
| 4899 | // Call C routine that may not cause GC or other trouble. |
| 4900 | __ mov(r5, Operand(ExternalReference::compare_doubles())); |
| 4901 | __ Jump(r5); // Tail call. |
| 4902 | } |
| 4903 | } |
| 4904 | |
| 4905 | |
| 4906 | // See comment at call site. |
| 4907 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) { |
| 4908 | // If either operand is a JSObject or an oddball value, then they are |
| 4909 | // not equal since their pointers are different. |
| 4910 | // There is no test for undetectability in strict equality. |
| 4911 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 4912 | Label first_non_object; |
| 4913 | // Get the type of the first operand into r2 and compare it with |
| 4914 | // FIRST_JS_OBJECT_TYPE. |
| 4915 | __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 4916 | __ b(lt, &first_non_object); |
| 4917 | |
| 4918 | // Return non-zero (r0 is not zero) |
| 4919 | Label return_not_equal; |
| 4920 | __ bind(&return_not_equal); |
| 4921 | __ mov(pc, Operand(lr)); // Return. |
| 4922 | |
| 4923 | __ bind(&first_non_object); |
| 4924 | // Check for oddballs: true, false, null, undefined. |
| 4925 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 4926 | __ b(eq, &return_not_equal); |
| 4927 | |
| 4928 | __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE); |
| 4929 | __ b(ge, &return_not_equal); |
| 4930 | |
| 4931 | // Check for oddballs: true, false, null, undefined. |
| 4932 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 4933 | __ b(eq, &return_not_equal); |
| 4934 | } |
| 4935 | |
| 4936 | |
| 4937 | // See comment at call site. |
| 4938 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
| 4939 | Label* both_loaded_as_doubles, |
| 4940 | Label* not_heap_numbers, |
| 4941 | Label* slow) { |
| 4942 | __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE); |
| 4943 | __ b(ne, not_heap_numbers); |
| 4944 | __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE); |
| 4945 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 4946 | |
| 4947 | // Both are heap numbers. Load them up then jump to the code we have |
| 4948 | // for that. |
| 4949 | __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4950 | __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4951 | __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4952 | __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4953 | __ jmp(both_loaded_as_doubles); |
| 4954 | } |
| 4955 | |
| 4956 | |
| 4957 | // Fast negative check for symbol-to-symbol equality. |
| 4958 | static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) { |
| 4959 | // r2 is object type of r0. |
| 4960 | __ tst(r2, Operand(kIsNotStringMask)); |
| 4961 | __ b(ne, slow); |
| 4962 | __ tst(r2, Operand(kIsSymbolMask)); |
| 4963 | __ b(eq, slow); |
| 4964 | __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE); |
| 4965 | __ b(ge, slow); |
| 4966 | __ tst(r3, Operand(kIsSymbolMask)); |
| 4967 | __ b(eq, slow); |
| 4968 | |
| 4969 | // Both are symbols. We already checked they weren't the same pointer |
| 4970 | // so they are not equal. |
| 4971 | __ mov(r0, Operand(1)); // Non-zero indicates not equal. |
| 4972 | __ mov(pc, Operand(lr)); // Return. |
| 4973 | } |
| 4974 | |
| 4975 | |
| 4976 | // On entry r0 and r1 are the things to be compared. On exit r0 is 0, |
| 4977 | // positive or negative to indicate the result of the comparison. |
| 4978 | void CompareStub::Generate(MacroAssembler* masm) { |
| 4979 | Label slow; // Call builtin. |
| 4980 | Label not_smis, both_loaded_as_doubles, rhs_not_nan; |
| 4981 | |
| 4982 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 4983 | // it is certain that at least one operand isn't a smi. |
| 4984 | |
| 4985 | // Handle the case where the objects are identical. Either returns the answer |
| 4986 | // or goes to slow. Only falls through if the objects were not identical. |
| 4987 | EmitIdenticalObjectComparison(masm, &slow, cc_); |
| 4988 | |
| 4989 | // If either is a Smi (we know that not both are), then they can only |
| 4990 | // be strictly equal if the other is a HeapNumber. |
| 4991 | ASSERT_EQ(0, kSmiTag); |
| 4992 | ASSERT_EQ(0, Smi::FromInt(0)); |
| 4993 | __ and_(r2, r0, Operand(r1)); |
| 4994 | __ tst(r2, Operand(kSmiTagMask)); |
| 4995 | __ b(ne, ¬_smis); |
| 4996 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 4997 | // 1) Return the answer. |
| 4998 | // 2) Go to slow. |
| 4999 | // 3) Fall through to both_loaded_as_doubles. |
| 5000 | // 4) Jump to rhs_not_nan. |
| 5001 | // In cases 3 and 4 we have found out we were dealing with a number-number |
| 5002 | // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles. |
| 5003 | EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_); |
| 5004 | |
| 5005 | __ bind(&both_loaded_as_doubles); |
| 5006 | // r0, r1, r2, r3 are the double representations of the left hand side |
| 5007 | // and the right hand side. |
| 5008 | |
| 5009 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 5010 | // fall through if neither is a NaN. Also binds rhs_not_nan. |
| 5011 | EmitNanCheck(masm, &rhs_not_nan, cc_); |
| 5012 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5013 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5014 | CpuFeatures::Scope scope(VFP3); |
| 5015 | // ARMv7 VFP3 instructions to implement double precision comparison. |
| 5016 | __ fmdrr(d6, r0, r1); |
| 5017 | __ fmdrr(d7, r2, r3); |
| 5018 | |
| 5019 | __ fcmp(d6, d7); |
| 5020 | __ vmrs(pc); |
| 5021 | __ mov(r0, Operand(0), LeaveCC, eq); |
| 5022 | __ mov(r0, Operand(1), LeaveCC, lt); |
| 5023 | __ mvn(r0, Operand(0), LeaveCC, gt); |
| 5024 | __ mov(pc, Operand(lr)); |
| 5025 | } else { |
| 5026 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 5027 | // answer. Never falls through. |
| 5028 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 5029 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5030 | |
| 5031 | __ bind(¬_smis); |
| 5032 | // At this point we know we are dealing with two different objects, |
| 5033 | // and neither of them is a Smi. The objects are in r0 and r1. |
| 5034 | if (strict_) { |
| 5035 | // This returns non-equal for some object types, or falls through if it |
| 5036 | // was not lucky. |
| 5037 | EmitStrictTwoHeapObjectCompare(masm); |
| 5038 | } |
| 5039 | |
| 5040 | Label check_for_symbols; |
| 5041 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 5042 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 5043 | // that case. If the inputs are not doubles then jumps to check_for_symbols. |
| 5044 | // In this case r2 will contain the type of r0. |
| 5045 | EmitCheckForTwoHeapNumbers(masm, |
| 5046 | &both_loaded_as_doubles, |
| 5047 | &check_for_symbols, |
| 5048 | &slow); |
| 5049 | |
| 5050 | __ bind(&check_for_symbols); |
| 5051 | if (cc_ == eq) { |
| 5052 | // Either jumps to slow or returns the answer. Assumes that r2 is the type |
| 5053 | // of r0 on entry. |
| 5054 | EmitCheckForSymbols(masm, &slow); |
| 5055 | } |
| 5056 | |
| 5057 | __ bind(&slow); |
| 5058 | __ push(lr); |
| 5059 | __ push(r1); |
| 5060 | __ push(r0); |
| 5061 | // Figure out which native to call and setup the arguments. |
| 5062 | Builtins::JavaScript native; |
| 5063 | int arg_count = 1; // Not counting receiver. |
| 5064 | if (cc_ == eq) { |
| 5065 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 5066 | } else { |
| 5067 | native = Builtins::COMPARE; |
| 5068 | int ncr; // NaN compare result |
| 5069 | if (cc_ == lt || cc_ == le) { |
| 5070 | ncr = GREATER; |
| 5071 | } else { |
| 5072 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 5073 | ncr = LESS; |
| 5074 | } |
| 5075 | arg_count++; |
| 5076 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 5077 | __ push(r0); |
| 5078 | } |
| 5079 | |
| 5080 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 5081 | // tagged as a small integer. |
| 5082 | __ mov(r0, Operand(arg_count)); |
| 5083 | __ InvokeBuiltin(native, CALL_JS); |
| 5084 | __ cmp(r0, Operand(0)); |
| 5085 | __ pop(pc); |
| 5086 | } |
| 5087 | |
| 5088 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5089 | // Allocates a heap number or jumps to the label if the young space is full and |
| 5090 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5091 | static void AllocateHeapNumber( |
| 5092 | MacroAssembler* masm, |
| 5093 | Label* need_gc, // Jump here if young space is full. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5094 | Register result, // The tagged address of the new heap number. |
| 5095 | Register scratch1, // A scratch register. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5096 | Register scratch2) { // Another scratch register. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5097 | // Allocate an object in the heap for the heap number and tag it as a heap |
| 5098 | // object. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5099 | __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize, |
| 5100 | result, |
| 5101 | scratch1, |
| 5102 | scratch2, |
| 5103 | need_gc, |
| 5104 | TAG_OBJECT); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5105 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5106 | // Get heap number map and store it in the allocated object. |
| 5107 | __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex); |
| 5108 | __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | |
| 5112 | // We fall into this code if the operands were Smis, but the result was |
| 5113 | // 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] | 5114 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 5115 | // to call the C-implemented binary fp operation routines we need to end up |
| 5116 | // with the double precision floating point operands in r0 and r1 (for the |
| 5117 | // 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] | 5118 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 5119 | Label* not_smi, |
| 5120 | const Builtins::JavaScript& builtin, |
| 5121 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5122 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5123 | Label slow, slow_pop_2_first, do_the_call; |
| 5124 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 5125 | // Smi-smi case (overflow). |
| 5126 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 5127 | // The new heap number is in r5. r6 and r7 are scratch. |
| 5128 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5129 | |
| 5130 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5131 | CpuFeatures::Scope scope(VFP3); |
| 5132 | __ IntegerToDoubleConversionWithVFP3(r0, r3, r2); |
| 5133 | __ IntegerToDoubleConversionWithVFP3(r1, r1, r0); |
| 5134 | } else { |
| 5135 | // Write Smi from r0 to r3 and r2 in double format. r6 is scratch. |
| 5136 | __ mov(r7, Operand(r0)); |
| 5137 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 5138 | __ push(lr); |
| 5139 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 5140 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 5141 | __ mov(r7, Operand(r1)); |
| 5142 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 5143 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 5144 | __ pop(lr); |
| 5145 | } |
| 5146 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5147 | __ jmp(&do_the_call); // Tail call. No return. |
| 5148 | |
| 5149 | // We jump to here if something goes wrong (one param is not a number of any |
| 5150 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5151 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5152 | __ push(r1); |
| 5153 | __ push(r0); |
| 5154 | __ mov(r0, Operand(1)); // Set number of arguments. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5155 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5156 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5157 | // 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] | 5158 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5159 | if (mode == NO_OVERWRITE) { |
| 5160 | // In the case where there is no chance of an overwritable float we may as |
| 5161 | // well do the allocation immediately while r0 and r1 are untouched. |
| 5162 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5163 | } |
| 5164 | |
| 5165 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5166 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5167 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5168 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5169 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5170 | if (mode == OVERWRITE_RIGHT) { |
| 5171 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 5172 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5173 | // Calling convention says that second double is in r2 and r3. |
| 5174 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5175 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 5176 | __ jmp(&finished_loading_r0); |
| 5177 | __ bind(&r0_is_smi); |
| 5178 | if (mode == OVERWRITE_RIGHT) { |
| 5179 | // 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] | 5180 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5181 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5182 | |
| 5183 | |
| 5184 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5185 | CpuFeatures::Scope scope(VFP3); |
| 5186 | __ IntegerToDoubleConversionWithVFP3(r0, r3, r2); |
| 5187 | } else { |
| 5188 | // Write Smi from r0 to r3 and r2 in double format. |
| 5189 | __ mov(r7, Operand(r0)); |
| 5190 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 5191 | __ push(lr); |
| 5192 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 5193 | __ pop(lr); |
| 5194 | } |
| 5195 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5196 | __ bind(&finished_loading_r0); |
| 5197 | |
| 5198 | // Move r1 to a double in r0-r1. |
| 5199 | __ tst(r1, Operand(kSmiTagMask)); |
| 5200 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5201 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5202 | __ b(ne, &slow); |
| 5203 | if (mode == OVERWRITE_LEFT) { |
| 5204 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5205 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5206 | // Calling convention says that first double is in r0 and r1. |
| 5207 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5208 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 5209 | __ jmp(&finished_loading_r1); |
| 5210 | __ bind(&r1_is_smi); |
| 5211 | if (mode == OVERWRITE_LEFT) { |
| 5212 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 5213 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5214 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5215 | |
| 5216 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5217 | CpuFeatures::Scope scope(VFP3); |
| 5218 | __ IntegerToDoubleConversionWithVFP3(r1, r1, r0); |
| 5219 | } else { |
| 5220 | // Write Smi from r1 to r1 and r0 in double format. |
| 5221 | __ mov(r7, Operand(r1)); |
| 5222 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 5223 | __ push(lr); |
| 5224 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 5225 | __ pop(lr); |
| 5226 | } |
| 5227 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5228 | __ bind(&finished_loading_r1); |
| 5229 | |
| 5230 | __ bind(&do_the_call); |
| 5231 | // r0: Left value (least significant part of mantissa). |
| 5232 | // r1: Left value (sign, exponent, top of mantissa). |
| 5233 | // r2: Right value (least significant part of mantissa). |
| 5234 | // r3: Right value (sign, exponent, top of mantissa). |
| 5235 | // r5: Address of heap number for result. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5236 | |
| 5237 | if (CpuFeatures::IsSupported(VFP3) && |
| 5238 | ((Token::MUL == operation) || |
| 5239 | (Token::DIV == operation) || |
| 5240 | (Token::ADD == operation) || |
| 5241 | (Token::SUB == operation))) { |
| 5242 | CpuFeatures::Scope scope(VFP3); |
| 5243 | // ARMv7 VFP3 instructions to implement |
| 5244 | // double precision, add, subtract, multiply, divide. |
| 5245 | __ fmdrr(d6, r0, r1); |
| 5246 | __ fmdrr(d7, r2, r3); |
| 5247 | |
| 5248 | if (Token::MUL == operation) { |
| 5249 | __ fmuld(d5, d6, d7); |
| 5250 | } else if (Token::DIV == operation) { |
| 5251 | __ fdivd(d5, d6, d7); |
| 5252 | } else if (Token::ADD == operation) { |
| 5253 | __ faddd(d5, d6, d7); |
| 5254 | } else if (Token::SUB == operation) { |
| 5255 | __ fsubd(d5, d6, d7); |
| 5256 | } else { |
| 5257 | UNREACHABLE(); |
| 5258 | } |
| 5259 | |
| 5260 | __ fmrrd(r0, r1, d5); |
| 5261 | |
| 5262 | __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset)); |
| 5263 | __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4)); |
| 5264 | __ mov(r0, Operand(r5)); |
| 5265 | __ mov(pc, lr); |
| 5266 | return; |
| 5267 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5268 | __ push(lr); // For later. |
| 5269 | __ push(r5); // Address of heap number that is answer. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5270 | __ AlignStack(0); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5271 | // Call C routine that may not cause GC or other trouble. |
| 5272 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5273 | __ Call(r5); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5274 | __ pop(r4); // Address of heap number. |
| 5275 | __ cmp(r4, Operand(Smi::FromInt(0))); |
| 5276 | __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5277 | // Store answer in the overwritable heap number. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5278 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5279 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 5280 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 5281 | // substract the tag from r4. |
| 5282 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 5283 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 5284 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5285 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5286 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5287 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 5288 | #endif |
| 5289 | __ mov(r0, Operand(r4)); |
| 5290 | // And we are done. |
| 5291 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
| 5294 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5295 | // 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] | 5296 | // 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] | 5297 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 5298 | // 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] | 5299 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 5300 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5301 | static void GetInt32(MacroAssembler* masm, |
| 5302 | Register source, |
| 5303 | Register dest, |
| 5304 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5305 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5306 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5307 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5308 | // Get exponent word. |
| 5309 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 5310 | // Get exponent alone in scratch2. |
| 5311 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5312 | // Load dest with zero. We use this either for the final shift or |
| 5313 | // for the answer. |
| 5314 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5315 | // 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] | 5316 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 5317 | // the exponent that we are fastest at and also the highest exponent we can |
| 5318 | // handle here. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5319 | const uint32_t non_smi_exponent = |
| 5320 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 5321 | __ cmp(scratch2, Operand(non_smi_exponent)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5322 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 5323 | __ b(eq, &right_exponent); |
| 5324 | // If the exponent is higher than that then go to slow case. This catches |
| 5325 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 5326 | __ b(gt, slow); |
| 5327 | |
| 5328 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 5329 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 5330 | // it rounds to zero. |
| 5331 | const uint32_t zero_exponent = |
| 5332 | (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift; |
| 5333 | __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC); |
| 5334 | // Dest already has a Smi zero. |
| 5335 | __ b(lt, &done); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5336 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 5337 | // We have a shifted exponent between 0 and 30 in scratch2. |
| 5338 | __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift)); |
| 5339 | // We now have the exponent in dest. Subtract from 30 to get |
| 5340 | // how much to shift down. |
| 5341 | __ rsb(dest, dest, Operand(30)); |
| 5342 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5343 | __ bind(&right_exponent); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5344 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5345 | CpuFeatures::Scope scope(VFP3); |
| 5346 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 5347 | // conversion using round to zero. |
| 5348 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5349 | __ fmdrr(d7, scratch2, scratch); |
| 5350 | __ ftosid(s15, d7); |
| 5351 | __ fmrs(dest, s15); |
| 5352 | } else { |
| 5353 | // Get the top bits of the mantissa. |
| 5354 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 5355 | // Put back the implicit 1. |
| 5356 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 5357 | // Shift up the mantissa bits to take up the space the exponent used to |
| 5358 | // take. We just orred in the implicit bit so that took care of one and |
| 5359 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 5360 | // distance. |
| 5361 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 5362 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 5363 | // Put sign in zero flag. |
| 5364 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 5365 | // Get the second half of the double. For some exponents we don't |
| 5366 | // actually need this because the bits get shifted out again, but |
| 5367 | // it's probably slower to test than just to do it. |
| 5368 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 5369 | // Shift down 22 bits to get the last 10 bits. |
| 5370 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 5371 | // Move down according to the exponent. |
| 5372 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 5373 | // Fix sign if sign bit was set. |
| 5374 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 5375 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5376 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5379 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 5380 | // whether both inputs are either Smis or at least heap numbers that can be |
| 5381 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 5382 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 5383 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 5384 | // write the answer into (either by allocating or by overwriting). |
| 5385 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 5386 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 5387 | Label slow, result_not_a_smi; |
| 5388 | Label r0_is_smi, r1_is_smi; |
| 5389 | Label done_checking_r0, done_checking_r1; |
| 5390 | |
| 5391 | __ tst(r1, Operand(kSmiTagMask)); |
| 5392 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 5393 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 5394 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5395 | GetInt32(masm, r1, r3, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5396 | __ jmp(&done_checking_r1); |
| 5397 | __ bind(&r1_is_smi); |
| 5398 | __ mov(r3, Operand(r1, ASR, 1)); |
| 5399 | __ bind(&done_checking_r1); |
| 5400 | |
| 5401 | __ tst(r0, Operand(kSmiTagMask)); |
| 5402 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 5403 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 5404 | __ b(ne, &slow); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5405 | GetInt32(masm, r0, r2, r5, r4, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5406 | __ jmp(&done_checking_r0); |
| 5407 | __ bind(&r0_is_smi); |
| 5408 | __ mov(r2, Operand(r0, ASR, 1)); |
| 5409 | __ bind(&done_checking_r0); |
| 5410 | |
| 5411 | // r0 and r1: Original operands (Smi or heap numbers). |
| 5412 | // r2 and r3: Signed int32 operands. |
| 5413 | switch (op_) { |
| 5414 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 5415 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 5416 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 5417 | case Token::SAR: |
| 5418 | // Use only the 5 least significant bits of the shift count. |
| 5419 | __ and_(r2, r2, Operand(0x1f)); |
| 5420 | __ mov(r2, Operand(r3, ASR, r2)); |
| 5421 | break; |
| 5422 | case Token::SHR: |
| 5423 | // Use only the 5 least significant bits of the shift count. |
| 5424 | __ and_(r2, r2, Operand(0x1f)); |
| 5425 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 5426 | // SHR is special because it is required to produce a positive answer. |
| 5427 | // The code below for writing into heap numbers isn't capable of writing |
| 5428 | // the register as an unsigned int so we go to slow case if we hit this |
| 5429 | // case. |
| 5430 | __ b(mi, &slow); |
| 5431 | break; |
| 5432 | case Token::SHL: |
| 5433 | // Use only the 5 least significant bits of the shift count. |
| 5434 | __ and_(r2, r2, Operand(0x1f)); |
| 5435 | __ mov(r2, Operand(r3, LSL, r2)); |
| 5436 | break; |
| 5437 | default: UNREACHABLE(); |
| 5438 | } |
| 5439 | // check that the *signed* result fits in a smi |
| 5440 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 5441 | __ b(mi, &result_not_a_smi); |
| 5442 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5443 | __ Ret(); |
| 5444 | |
| 5445 | Label have_to_allocate, got_a_heap_number; |
| 5446 | __ bind(&result_not_a_smi); |
| 5447 | switch (mode_) { |
| 5448 | case OVERWRITE_RIGHT: { |
| 5449 | __ tst(r0, Operand(kSmiTagMask)); |
| 5450 | __ b(eq, &have_to_allocate); |
| 5451 | __ mov(r5, Operand(r0)); |
| 5452 | break; |
| 5453 | } |
| 5454 | case OVERWRITE_LEFT: { |
| 5455 | __ tst(r1, Operand(kSmiTagMask)); |
| 5456 | __ b(eq, &have_to_allocate); |
| 5457 | __ mov(r5, Operand(r1)); |
| 5458 | break; |
| 5459 | } |
| 5460 | case NO_OVERWRITE: { |
| 5461 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5462 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5463 | } |
| 5464 | default: break; |
| 5465 | } |
| 5466 | __ bind(&got_a_heap_number); |
| 5467 | // r2: Answer as signed int32. |
| 5468 | // r5: Heap number to write answer into. |
| 5469 | |
| 5470 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 5471 | // result. |
| 5472 | __ mov(r0, Operand(r5)); |
| 5473 | |
| 5474 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 5475 | // r3 as scratch. r0 is preserved and returned. |
| 5476 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 5477 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 5478 | |
| 5479 | if (mode_ != NO_OVERWRITE) { |
| 5480 | __ bind(&have_to_allocate); |
| 5481 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 5482 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 5483 | __ jmp(&got_a_heap_number); |
| 5484 | } |
| 5485 | |
| 5486 | // If all else failed then we go to the runtime system. |
| 5487 | __ bind(&slow); |
| 5488 | __ push(r1); // restore stack |
| 5489 | __ push(r0); |
| 5490 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 5491 | switch (op_) { |
| 5492 | case Token::BIT_OR: |
| 5493 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 5494 | break; |
| 5495 | case Token::BIT_AND: |
| 5496 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 5497 | break; |
| 5498 | case Token::BIT_XOR: |
| 5499 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 5500 | break; |
| 5501 | case Token::SAR: |
| 5502 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 5503 | break; |
| 5504 | case Token::SHR: |
| 5505 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 5506 | break; |
| 5507 | case Token::SHL: |
| 5508 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 5509 | break; |
| 5510 | default: |
| 5511 | UNREACHABLE(); |
| 5512 | } |
| 5513 | } |
| 5514 | |
| 5515 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5516 | // Can we multiply by x with max two shifts and an add. |
| 5517 | // This answers yes to all integers from 2 to 10. |
| 5518 | static bool IsEasyToMultiplyBy(int x) { |
| 5519 | if (x < 2) return false; // Avoid special cases. |
| 5520 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 5521 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 5522 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 5523 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 5524 | return false; |
| 5525 | } |
| 5526 | |
| 5527 | |
| 5528 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 5529 | // Source and destination may be the same register. This routine does |
| 5530 | // not set carry and overflow the way a mul instruction would. |
| 5531 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 5532 | Register source, |
| 5533 | Register destination, |
| 5534 | int known_int) { |
| 5535 | if (IsPowerOf2(known_int)) { |
| 5536 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 5537 | } else if (PopCountLessThanEqual2(known_int)) { |
| 5538 | int first_bit = BitPosition(known_int); |
| 5539 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 5540 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 5541 | if (first_bit != 0) { |
| 5542 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 5543 | } |
| 5544 | } else { |
| 5545 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 5546 | int the_bit = BitPosition(known_int + 1); |
| 5547 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 5548 | } |
| 5549 | } |
| 5550 | |
| 5551 | |
| 5552 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 5553 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 5554 | // a result that needs shifting. |
| 5555 | static void MultiplyByKnownInt2( |
| 5556 | MacroAssembler* masm, |
| 5557 | Register result, |
| 5558 | Register source, |
| 5559 | Register known_int_register, // Smi tagged. |
| 5560 | int known_int, |
| 5561 | int* required_shift) { // Including Smi tag shift |
| 5562 | switch (known_int) { |
| 5563 | case 3: |
| 5564 | __ add(result, source, Operand(source, LSL, 1)); |
| 5565 | *required_shift = 1; |
| 5566 | break; |
| 5567 | case 5: |
| 5568 | __ add(result, source, Operand(source, LSL, 2)); |
| 5569 | *required_shift = 1; |
| 5570 | break; |
| 5571 | case 6: |
| 5572 | __ add(result, source, Operand(source, LSL, 1)); |
| 5573 | *required_shift = 2; |
| 5574 | break; |
| 5575 | case 7: |
| 5576 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 5577 | *required_shift = 1; |
| 5578 | break; |
| 5579 | case 9: |
| 5580 | __ add(result, source, Operand(source, LSL, 3)); |
| 5581 | *required_shift = 1; |
| 5582 | break; |
| 5583 | case 10: |
| 5584 | __ add(result, source, Operand(source, LSL, 2)); |
| 5585 | *required_shift = 2; |
| 5586 | break; |
| 5587 | default: |
| 5588 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 5589 | __ mul(result, source, known_int_register); |
| 5590 | *required_shift = 0; |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5595 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 5596 | // r1 : x |
| 5597 | // r0 : y |
| 5598 | // result : r0 |
| 5599 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5600 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 5601 | // tell us that. |
| 5602 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 5603 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5604 | switch (op_) { |
| 5605 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5606 | Label not_smi; |
| 5607 | // Fast path. |
| 5608 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5609 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5610 | __ b(ne, ¬_smi); |
| 5611 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 5612 | // Return if no overflow. |
| 5613 | __ Ret(vc); |
| 5614 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 5615 | |
| 5616 | HandleBinaryOpSlowCases(masm, |
| 5617 | ¬_smi, |
| 5618 | Builtins::ADD, |
| 5619 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5620 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5621 | break; |
| 5622 | } |
| 5623 | |
| 5624 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5625 | Label not_smi; |
| 5626 | // Fast path. |
| 5627 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5628 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5629 | __ b(ne, ¬_smi); |
| 5630 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 5631 | // Return if no overflow. |
| 5632 | __ Ret(vc); |
| 5633 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 5634 | |
| 5635 | HandleBinaryOpSlowCases(masm, |
| 5636 | ¬_smi, |
| 5637 | Builtins::SUB, |
| 5638 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5639 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5640 | break; |
| 5641 | } |
| 5642 | |
| 5643 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5644 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5645 | ASSERT(kSmiTag == 0); // adjust code below |
| 5646 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5647 | __ b(ne, ¬_smi); |
| 5648 | // 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] | 5649 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5650 | // Do multiplication |
| 5651 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 5652 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5653 | __ mov(ip, Operand(r3, ASR, 31)); |
| 5654 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 5655 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5656 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5657 | __ tst(r3, Operand(r3)); |
| 5658 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5659 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5660 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 5661 | // We know one of them was zero. |
| 5662 | __ add(r2, r0, Operand(r1), SetCC); |
| 5663 | __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 5664 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 5665 | // Slow case. We fall through here if we multiplied a negative number |
| 5666 | // with 0, because that would mean we should produce -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5667 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5668 | |
| 5669 | HandleBinaryOpSlowCases(masm, |
| 5670 | ¬_smi, |
| 5671 | Builtins::MUL, |
| 5672 | Token::MUL, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5673 | mode_); |
| 5674 | break; |
| 5675 | } |
| 5676 | |
| 5677 | case Token::DIV: |
| 5678 | case Token::MOD: { |
| 5679 | Label not_smi; |
| 5680 | if (specialized_on_rhs_) { |
| 5681 | Label smi_is_unsuitable; |
| 5682 | __ BranchOnNotSmi(r1, ¬_smi); |
| 5683 | if (IsPowerOf2(constant_rhs_)) { |
| 5684 | if (op_ == Token::MOD) { |
| 5685 | __ and_(r0, |
| 5686 | r1, |
| 5687 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 5688 | SetCC); |
| 5689 | // We now have the answer, but if the input was negative we also |
| 5690 | // have the sign bit. Our work is done if the result is |
| 5691 | // positive or zero: |
| 5692 | __ Ret(pl); |
| 5693 | // A mod of a negative left hand side must return a negative number. |
| 5694 | // Unfortunately if the answer is 0 then we must return -0. And we |
| 5695 | // already optimistically trashed r0 so we may need to restore it. |
| 5696 | __ eor(r0, r0, Operand(0x80000000u), SetCC); |
| 5697 | // Next two instructions are conditional on the answer being -0. |
| 5698 | __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
| 5699 | __ b(eq, &smi_is_unsuitable); |
| 5700 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
| 5701 | __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_))); |
| 5702 | } else { |
| 5703 | ASSERT(op_ == Token::DIV); |
| 5704 | __ tst(r1, |
| 5705 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
| 5706 | __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder. |
| 5707 | int shift = 0; |
| 5708 | int d = constant_rhs_; |
| 5709 | while ((d & 1) == 0) { |
| 5710 | d >>= 1; |
| 5711 | shift++; |
| 5712 | } |
| 5713 | __ mov(r0, Operand(r1, LSR, shift)); |
| 5714 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 5715 | } |
| 5716 | } else { |
| 5717 | // Not a power of 2. |
| 5718 | __ tst(r1, Operand(0x80000000u)); |
| 5719 | __ b(ne, &smi_is_unsuitable); |
| 5720 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 5721 | // multiplying. |
| 5722 | double divisor = 1.0 / constant_rhs_; |
| 5723 | int shift = 32; |
| 5724 | double scale = 4294967296.0; // 1 << 32. |
| 5725 | uint32_t mul; |
| 5726 | // Maximise the precision of the fixed point reciprocal. |
| 5727 | while (true) { |
| 5728 | mul = static_cast<uint32_t>(scale * divisor); |
| 5729 | if (mul >= 0x7fffffff) break; |
| 5730 | scale *= 2.0; |
| 5731 | shift++; |
| 5732 | } |
| 5733 | mul++; |
| 5734 | __ mov(r2, Operand(mul)); |
| 5735 | __ umull(r3, r2, r2, r1); |
| 5736 | __ mov(r2, Operand(r2, LSR, shift - 31)); |
| 5737 | // r2 is r1 / rhs. r2 is not Smi tagged. |
| 5738 | // r0 is still the known rhs. r0 is Smi tagged. |
| 5739 | // r1 is still the unkown lhs. r1 is Smi tagged. |
| 5740 | int required_r4_shift = 0; // Including the Smi tag shift of 1. |
| 5741 | // r4 = r2 * r0. |
| 5742 | MultiplyByKnownInt2(masm, |
| 5743 | r4, |
| 5744 | r2, |
| 5745 | r0, |
| 5746 | constant_rhs_, |
| 5747 | &required_r4_shift); |
| 5748 | // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs). |
| 5749 | if (op_ == Token::DIV) { |
| 5750 | __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC); |
| 5751 | __ b(ne, &smi_is_unsuitable); // There was a remainder. |
| 5752 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 5753 | } else { |
| 5754 | ASSERT(op_ == Token::MOD); |
| 5755 | __ sub(r0, r1, Operand(r4, LSL, required_r4_shift)); |
| 5756 | } |
| 5757 | } |
| 5758 | __ Ret(); |
| 5759 | __ bind(&smi_is_unsuitable); |
| 5760 | } else { |
| 5761 | __ jmp(¬_smi); |
| 5762 | } |
| 5763 | HandleBinaryOpSlowCases(masm, |
| 5764 | ¬_smi, |
| 5765 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV, |
| 5766 | op_, |
| 5767 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5768 | break; |
| 5769 | } |
| 5770 | |
| 5771 | case Token::BIT_OR: |
| 5772 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5773 | case Token::BIT_XOR: |
| 5774 | case Token::SAR: |
| 5775 | case Token::SHR: |
| 5776 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5777 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5778 | ASSERT(kSmiTag == 0); // adjust code below |
| 5779 | __ tst(r2, Operand(kSmiTagMask)); |
| 5780 | __ b(ne, &slow); |
| 5781 | switch (op_) { |
| 5782 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 5783 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 5784 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5785 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5786 | // Remove tags from right operand. |
| 5787 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5788 | // Use only the 5 least significant bits of the shift count. |
| 5789 | __ and_(r2, r2, Operand(0x1f)); |
| 5790 | __ mov(r0, Operand(r1, ASR, r2)); |
| 5791 | // Smi tag result. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5792 | __ bic(r0, r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5793 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5794 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5795 | // Remove tags from operands. We can't do this on a 31 bit number |
| 5796 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 5797 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5798 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5799 | // Use only the 5 least significant bits of the shift count. |
| 5800 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5801 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5802 | // Unsigned shift is not allowed to produce a negative number, so |
| 5803 | // check the sign bit and the sign bit after Smi tagging. |
| 5804 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5805 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5806 | // Smi tag result. |
| 5807 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5808 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5809 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5810 | // Remove tags from operands. |
| 5811 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5812 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5813 | // Use only the 5 least significant bits of the shift count. |
| 5814 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5815 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5816 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5817 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 5818 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5819 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5820 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5821 | default: UNREACHABLE(); |
| 5822 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5823 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5824 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5825 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5826 | break; |
| 5827 | } |
| 5828 | |
| 5829 | default: UNREACHABLE(); |
| 5830 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5831 | // This code should be unreachable. |
| 5832 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5833 | } |
| 5834 | |
| 5835 | |
| 5836 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5837 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 5838 | // argument, so give it a Smi. |
| 5839 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5840 | __ push(r0); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5841 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5842 | |
| 5843 | __ StubReturn(1); |
| 5844 | } |
| 5845 | |
| 5846 | |
| 5847 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 5848 | Label undo; |
| 5849 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5850 | Label not_smi; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5851 | |
| 5852 | // Enter runtime system if the value is not a smi. |
| 5853 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5854 | __ b(ne, ¬_smi); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5855 | |
| 5856 | // Enter runtime system if the value of the expression is zero |
| 5857 | // to make sure that we switch between 0 and -0. |
| 5858 | __ cmp(r0, Operand(0)); |
| 5859 | __ b(eq, &slow); |
| 5860 | |
| 5861 | // The value of the expression is a smi that is not zero. Try |
| 5862 | // optimistic subtraction '0 - value'. |
| 5863 | __ rsb(r1, r0, Operand(0), SetCC); |
| 5864 | __ b(vs, &slow); |
| 5865 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5866 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 5867 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5868 | |
| 5869 | // Enter runtime system. |
| 5870 | __ bind(&slow); |
| 5871 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5872 | __ mov(r0, Operand(0)); // Set number of arguments. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5873 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 5874 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5875 | __ bind(¬_smi); |
| 5876 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 5877 | __ b(ne, &slow); |
| 5878 | // r0 is a heap number. Get a new heap number in r1. |
| 5879 | if (overwrite_) { |
| 5880 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5881 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5882 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5883 | } else { |
| 5884 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5885 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5886 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5887 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5888 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5889 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 5890 | __ mov(r0, Operand(r1)); |
| 5891 | } |
| 5892 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 5896 | int CEntryStub::MinorKey() { |
| 5897 | ASSERT(result_size_ <= 2); |
| 5898 | // Result returned in r0 or r0+r1 by default. |
| 5899 | return 0; |
| 5900 | } |
| 5901 | |
| 5902 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5903 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5904 | // r0 holds the exception. |
| 5905 | |
| 5906 | // Adjust this code if not the case. |
| 5907 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5908 | |
| 5909 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5910 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 5911 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5912 | |
| 5913 | // Restore the next handler and frame pointer, discard handler state. |
| 5914 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5915 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5916 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5917 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5918 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 5919 | |
| 5920 | // Before returning we restore the context from the frame pointer if |
| 5921 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5922 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5923 | __ cmp(fp, Operand(0)); |
| 5924 | // Set cp to NULL if fp is NULL. |
| 5925 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5926 | // Restore cp otherwise. |
| 5927 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5928 | #ifdef DEBUG |
| 5929 | if (FLAG_debug_code) { |
| 5930 | __ mov(lr, Operand(pc)); |
| 5931 | } |
| 5932 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5933 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5934 | __ pop(pc); |
| 5935 | } |
| 5936 | |
| 5937 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5938 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 5939 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5940 | // Adjust this code if not the case. |
| 5941 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5942 | |
| 5943 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5944 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5945 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5946 | |
| 5947 | // Unwind the handlers until the ENTRY handler is found. |
| 5948 | Label loop, done; |
| 5949 | __ bind(&loop); |
| 5950 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5951 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 5952 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5953 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 5954 | __ b(eq, &done); |
| 5955 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5956 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 5957 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5958 | __ jmp(&loop); |
| 5959 | __ bind(&done); |
| 5960 | |
| 5961 | // 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] | 5962 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5963 | __ pop(r2); |
| 5964 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5965 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5966 | if (type == OUT_OF_MEMORY) { |
| 5967 | // Set external caught exception to false. |
| 5968 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 5969 | __ mov(r0, Operand(false)); |
| 5970 | __ mov(r2, Operand(external_caught)); |
| 5971 | __ str(r0, MemOperand(r2)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5972 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5973 | // Set pending exception and r0 to out of memory exception. |
| 5974 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5975 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5976 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 5977 | __ str(r0, MemOperand(r2)); |
| 5978 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5979 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5980 | // Stack layout at this point. See also StackHandlerConstants. |
| 5981 | // sp -> state (ENTRY) |
| 5982 | // fp |
| 5983 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5984 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5985 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 5986 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5987 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 5988 | // Before returning we restore the context from the frame pointer if |
| 5989 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5990 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5991 | __ cmp(fp, Operand(0)); |
| 5992 | // Set cp to NULL if fp is NULL. |
| 5993 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5994 | // Restore cp otherwise. |
| 5995 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5996 | #ifdef DEBUG |
| 5997 | if (FLAG_debug_code) { |
| 5998 | __ mov(lr, Operand(pc)); |
| 5999 | } |
| 6000 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6001 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6002 | __ pop(pc); |
| 6003 | } |
| 6004 | |
| 6005 | |
| 6006 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 6007 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6008 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6009 | Label* throw_out_of_memory_exception, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6010 | ExitFrame::Mode mode, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6011 | bool do_gc, |
| 6012 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6013 | // r0: result parameter for PerformGC, if any |
| 6014 | // r4: number of arguments including receiver (C callee-saved) |
| 6015 | // r5: pointer to builtin function (C callee-saved) |
| 6016 | // r6: pointer to the first argument (C callee-saved) |
| 6017 | |
| 6018 | if (do_gc) { |
| 6019 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6020 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 6021 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6022 | } |
| 6023 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6024 | ExternalReference scope_depth = |
| 6025 | ExternalReference::heap_always_allocate_scope_depth(); |
| 6026 | if (always_allocate) { |
| 6027 | __ mov(r0, Operand(scope_depth)); |
| 6028 | __ ldr(r1, MemOperand(r0)); |
| 6029 | __ add(r1, r1, Operand(1)); |
| 6030 | __ str(r1, MemOperand(r0)); |
| 6031 | } |
| 6032 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6033 | // Call C built-in. |
| 6034 | // r0 = argc, r1 = argv |
| 6035 | __ mov(r0, Operand(r4)); |
| 6036 | __ mov(r1, Operand(r6)); |
| 6037 | |
| 6038 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 6039 | // frames, we need to know where the return address is. Right now, |
| 6040 | // we push it on the stack to be able to find it again, but we never |
| 6041 | // restore from it in case of changes, which makes it impossible to |
| 6042 | // support moving the C entry code stub. This should be fixed, but currently |
| 6043 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 6044 | // sequence that it is not moving ever. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6045 | masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 6046 | masm->push(lr); |
| 6047 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6048 | |
| 6049 | if (always_allocate) { |
| 6050 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 6051 | // though (contain the result). |
| 6052 | __ mov(r2, Operand(scope_depth)); |
| 6053 | __ ldr(r3, MemOperand(r2)); |
| 6054 | __ sub(r3, r3, Operand(1)); |
| 6055 | __ str(r3, MemOperand(r2)); |
| 6056 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6057 | |
| 6058 | // check for failure result |
| 6059 | Label failure_returned; |
| 6060 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 6061 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 6062 | __ add(r2, r0, Operand(1)); |
| 6063 | __ tst(r2, Operand(kFailureTagMask)); |
| 6064 | __ b(eq, &failure_returned); |
| 6065 | |
| 6066 | // Exit C frame and return. |
| 6067 | // r0:r1: result |
| 6068 | // sp: stack pointer |
| 6069 | // fp: frame pointer |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6070 | __ LeaveExitFrame(mode); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6071 | |
| 6072 | // check if we should retry or throw exception |
| 6073 | Label retry; |
| 6074 | __ bind(&failure_returned); |
| 6075 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 6076 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 6077 | __ b(eq, &retry); |
| 6078 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6079 | // Special handling of out of memory exceptions. |
| 6080 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 6081 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 6082 | __ b(eq, throw_out_of_memory_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6083 | |
| 6084 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6085 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6086 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6087 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6088 | __ ldr(r0, MemOperand(ip)); |
| 6089 | __ str(r3, MemOperand(ip)); |
| 6090 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6091 | // Special handling of termination exceptions which are uncatchable |
| 6092 | // by javascript code. |
| 6093 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 6094 | __ b(eq, throw_termination_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6095 | |
| 6096 | // Handle normal exception. |
| 6097 | __ jmp(throw_normal_exception); |
| 6098 | |
| 6099 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 6100 | } |
| 6101 | |
| 6102 | |
| 6103 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 6104 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 6105 | // r0: number of arguments including receiver |
| 6106 | // r1: pointer to builtin function |
| 6107 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6108 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6109 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6110 | |
| 6111 | // NOTE: Invocations of builtins may return failure objects |
| 6112 | // instead of a proper result. The builtin entry handles |
| 6113 | // this by performing a garbage collection and retrying the |
| 6114 | // builtin once. |
| 6115 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6116 | ExitFrame::Mode mode = is_debug_break |
| 6117 | ? ExitFrame::MODE_DEBUG |
| 6118 | : ExitFrame::MODE_NORMAL; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6119 | |
| 6120 | // Enter the exit frame that transitions from JavaScript to C++. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6121 | __ EnterExitFrame(mode); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6122 | |
| 6123 | // r4: number of arguments (C callee-saved) |
| 6124 | // r5: pointer to builtin function (C callee-saved) |
| 6125 | // r6: pointer to first argument (C callee-saved) |
| 6126 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6127 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6128 | Label throw_termination_exception; |
| 6129 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6130 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6131 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6132 | GenerateCore(masm, |
| 6133 | &throw_normal_exception, |
| 6134 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6135 | &throw_out_of_memory_exception, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6136 | mode, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6137 | false, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6138 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6139 | |
| 6140 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6141 | GenerateCore(masm, |
| 6142 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6143 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6144 | &throw_out_of_memory_exception, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6145 | mode, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6146 | true, |
| 6147 | false); |
| 6148 | |
| 6149 | // Do full GC and retry runtime call one final time. |
| 6150 | Failure* failure = Failure::InternalError(); |
| 6151 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 6152 | GenerateCore(masm, |
| 6153 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6154 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6155 | &throw_out_of_memory_exception, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6156 | mode, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 6157 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6158 | true); |
| 6159 | |
| 6160 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6161 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 6162 | |
| 6163 | __ bind(&throw_termination_exception); |
| 6164 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6165 | |
| 6166 | __ bind(&throw_normal_exception); |
| 6167 | GenerateThrowTOS(masm); |
| 6168 | } |
| 6169 | |
| 6170 | |
| 6171 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 6172 | // r0: code entry |
| 6173 | // r1: function |
| 6174 | // r2: receiver |
| 6175 | // r3: argc |
| 6176 | // [sp+0]: argv |
| 6177 | |
| 6178 | Label invoke, exit; |
| 6179 | |
| 6180 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 6181 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6182 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6183 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 6184 | |
| 6185 | // Get address of argv, see stm above. |
| 6186 | // r0: code entry |
| 6187 | // r1: function |
| 6188 | // r2: receiver |
| 6189 | // r3: argc |
| 6190 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 6191 | __ ldr(r4, MemOperand(r4)); // argv |
| 6192 | |
| 6193 | // Push a frame with special values setup to mark it as an entry frame. |
| 6194 | // r0: code entry |
| 6195 | // r1: function |
| 6196 | // r2: receiver |
| 6197 | // r3: argc |
| 6198 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6199 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6200 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 6201 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6202 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 6203 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6204 | __ ldr(r5, MemOperand(r5)); |
| 6205 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 6206 | |
| 6207 | // Setup frame pointer for the frame to be pushed. |
| 6208 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6209 | |
| 6210 | // Call a faked try-block that does the invoke. |
| 6211 | __ bl(&invoke); |
| 6212 | |
| 6213 | // Caught exception: Store result (exception) in the pending |
| 6214 | // exception field in the JSEnv and return a failure sentinel. |
| 6215 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 6216 | // 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] | 6217 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6218 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6219 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6220 | __ b(&exit); |
| 6221 | |
| 6222 | // Invoke: Link this frame into the handler chain. |
| 6223 | __ bind(&invoke); |
| 6224 | // Must preserve r0-r4, r5-r7 are available. |
| 6225 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6226 | // If an exception not caught by another handler occurs, this handler |
| 6227 | // returns control to the code after the bl(&invoke) above, which |
| 6228 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 6229 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6230 | |
| 6231 | // Clear any pending exceptions. |
| 6232 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 6233 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6234 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6235 | __ str(r5, MemOperand(ip)); |
| 6236 | |
| 6237 | // Invoke the function by calling through JS entry trampoline builtin. |
| 6238 | // Notice that we cannot store a reference to the trampoline code directly in |
| 6239 | // this stub, because runtime stubs are not traversed when doing GC. |
| 6240 | |
| 6241 | // Expected registers by Builtins::JSEntryTrampoline |
| 6242 | // r0: code entry |
| 6243 | // r1: function |
| 6244 | // r2: receiver |
| 6245 | // r3: argc |
| 6246 | // r4: argv |
| 6247 | if (is_construct) { |
| 6248 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 6249 | __ mov(ip, Operand(construct_entry)); |
| 6250 | } else { |
| 6251 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 6252 | __ mov(ip, Operand(entry)); |
| 6253 | } |
| 6254 | __ ldr(ip, MemOperand(ip)); // deref address |
| 6255 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6256 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 6257 | // macro for the add instruction because we don't want the coverage tool |
| 6258 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6259 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6260 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6261 | |
| 6262 | // Unlink this frame from the handler chain. When reading the |
| 6263 | // address of the next handler, there is no need to use the address |
| 6264 | // displacement since the current stack pointer (sp) points directly |
| 6265 | // to the stack handler. |
| 6266 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 6267 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 6268 | __ str(r3, MemOperand(ip)); |
| 6269 | // No need to restore registers |
| 6270 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 6271 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6272 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6273 | __ bind(&exit); // r0 holds result |
| 6274 | // Restore the top frame descriptors from the stack. |
| 6275 | __ pop(r3); |
| 6276 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 6277 | __ str(r3, MemOperand(ip)); |
| 6278 | |
| 6279 | // Reset the stack to the callee saved registers. |
| 6280 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 6281 | |
| 6282 | // Restore callee-saved registers and return. |
| 6283 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6284 | if (FLAG_debug_code) { |
| 6285 | __ mov(lr, Operand(pc)); |
| 6286 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6287 | #endif |
| 6288 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 6289 | } |
| 6290 | |
| 6291 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6292 | // This stub performs an instanceof, calling the builtin function if |
| 6293 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 6294 | // be an instance of (these are fetched from the stack). |
| 6295 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 6296 | // Get the object - slow case for smis (we may need to throw an exception |
| 6297 | // depending on the rhs). |
| 6298 | Label slow, loop, is_instance, is_not_instance; |
| 6299 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 6300 | __ BranchOnSmi(r0, &slow); |
| 6301 | |
| 6302 | // Check that the left hand is a JS object and put map in r3. |
| 6303 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 6304 | __ b(lt, &slow); |
| 6305 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 6306 | __ b(gt, &slow); |
| 6307 | |
| 6308 | // Get the prototype of the function (r4 is result, r2 is scratch). |
| 6309 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 6310 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 6311 | |
| 6312 | // Check that the function prototype is a JS object. |
| 6313 | __ BranchOnSmi(r4, &slow); |
| 6314 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 6315 | __ b(lt, &slow); |
| 6316 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 6317 | __ b(gt, &slow); |
| 6318 | |
| 6319 | // Register mapping: r3 is object map and r4 is function prototype. |
| 6320 | // Get prototype of object into r2. |
| 6321 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 6322 | |
| 6323 | // Loop through the prototype chain looking for the function prototype. |
| 6324 | __ bind(&loop); |
| 6325 | __ cmp(r2, Operand(r4)); |
| 6326 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6327 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 6328 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6329 | __ b(eq, &is_not_instance); |
| 6330 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 6331 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 6332 | __ jmp(&loop); |
| 6333 | |
| 6334 | __ bind(&is_instance); |
| 6335 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 6336 | __ pop(); |
| 6337 | __ pop(); |
| 6338 | __ mov(pc, Operand(lr)); // Return. |
| 6339 | |
| 6340 | __ bind(&is_not_instance); |
| 6341 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 6342 | __ pop(); |
| 6343 | __ pop(); |
| 6344 | __ mov(pc, Operand(lr)); // Return. |
| 6345 | |
| 6346 | // Slow-case. Tail call builtin. |
| 6347 | __ bind(&slow); |
| 6348 | __ mov(r0, Operand(1)); // Arg count without receiver. |
| 6349 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 6350 | } |
| 6351 | |
| 6352 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6353 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6354 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6355 | Label adaptor; |
| 6356 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6357 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6358 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6359 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6360 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6361 | // Nothing to do: The formal number of parameters has already been |
| 6362 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6363 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6364 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6365 | // Arguments adaptor case: Read the arguments length from the |
| 6366 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6367 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6368 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6369 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6370 | } |
| 6371 | |
| 6372 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6373 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 6374 | // The displacement is the offset of the last parameter (if any) |
| 6375 | // relative to the frame pointer. |
| 6376 | static const int kDisplacement = |
| 6377 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6378 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6379 | // Check that the key is a smi. |
| 6380 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6381 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6382 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6383 | // Check if the calling frame is an arguments adaptor frame. |
| 6384 | Label adaptor; |
| 6385 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6386 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6387 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6388 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6389 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6390 | // Check index against formal parameters count limit passed in |
| 6391 | // through register eax. Use unsigned comparison to get negative |
| 6392 | // check for free. |
| 6393 | __ cmp(r1, r0); |
| 6394 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6395 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6396 | // Read the argument from the stack and return it. |
| 6397 | __ sub(r3, r0, r1); |
| 6398 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6399 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6400 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6401 | |
| 6402 | // Arguments adaptor case: Check index against actual arguments |
| 6403 | // limit found in the arguments adaptor frame. Use unsigned |
| 6404 | // comparison to get negative check for free. |
| 6405 | __ bind(&adaptor); |
| 6406 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6407 | __ cmp(r1, r0); |
| 6408 | __ b(cs, &slow); |
| 6409 | |
| 6410 | // Read the argument from the adaptor frame and return it. |
| 6411 | __ sub(r3, r0, r1); |
| 6412 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6413 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 6414 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6415 | |
| 6416 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 6417 | // by calling the runtime system. |
| 6418 | __ bind(&slow); |
| 6419 | __ push(r1); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6420 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
| 6423 | |
| 6424 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 6425 | // Check if the calling frame is an arguments adaptor frame. |
| 6426 | Label runtime; |
| 6427 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 6428 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6429 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6430 | __ b(ne, &runtime); |
| 6431 | |
| 6432 | // Patch the arguments.length and the parameters pointer. |
| 6433 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 6434 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 6435 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 6436 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 6437 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 6438 | |
| 6439 | // Do the runtime call to allocate the arguments object. |
| 6440 | __ bind(&runtime); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 6441 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6442 | } |
| 6443 | |
| 6444 | |
| 6445 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 6446 | Label slow; |
| 6447 | // Get the function to call from the stack. |
| 6448 | // function, receiver [, arguments] |
| 6449 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 6450 | |
| 6451 | // Check that the function is really a JavaScript function. |
| 6452 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6453 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6454 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6455 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6456 | __ b(ne, &slow); |
| 6457 | |
| 6458 | // Fast-case: Invoke the function now. |
| 6459 | // r1: pushed function |
| 6460 | ParameterCount actual(argc_); |
| 6461 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 6462 | |
| 6463 | // Slow-case: Non-function called. |
| 6464 | __ bind(&slow); |
| 6465 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6466 | __ mov(r2, Operand(0)); |
| 6467 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 6468 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 6469 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6470 | } |
| 6471 | |
| 6472 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6473 | int CompareStub::MinorKey() { |
| 6474 | // Encode the two parameters in a unique 16 bit value. |
| 6475 | ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15)); |
| 6476 | return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0); |
| 6477 | } |
| 6478 | |
| 6479 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6480 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6481 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6482 | } } // namespace v8::internal |