ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
| 32 | #include "debug.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 33 | #include "parser.h" |
| 34 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 36 | #include "scopes.h" |
| 37 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 42 | #define __ ACCESS_MASM(masm_) |
| 43 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 44 | // ------------------------------------------------------------------------- |
| 45 | // Platform-specific DeferredCode functions. |
| 46 | |
| 47 | void DeferredCode::SaveRegisters() { |
| 48 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 49 | int action = registers_[i]; |
| 50 | if (action == kPush) { |
| 51 | __ push(RegisterAllocator::ToRegister(i)); |
| 52 | } else if (action != kIgnore && (action & kSyncedFlag) == 0) { |
| 53 | __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | void DeferredCode::RestoreRegisters() { |
| 60 | // Restore registers in reverse order due to the stack. |
| 61 | for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) { |
| 62 | int action = registers_[i]; |
| 63 | if (action == kPush) { |
| 64 | __ pop(RegisterAllocator::ToRegister(i)); |
| 65 | } else if (action != kIgnore) { |
| 66 | action &= ~kSyncedFlag; |
| 67 | __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action)); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 72 | |
| 73 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 74 | // CodeGenState implementation. |
| 75 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 76 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 77 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 78 | typeof_state_(NOT_INSIDE_TYPEOF), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 79 | true_target_(NULL), |
| 80 | false_target_(NULL), |
| 81 | previous_(NULL) { |
| 82 | owner_->set_state(this); |
| 83 | } |
| 84 | |
| 85 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 86 | CodeGenState::CodeGenState(CodeGenerator* owner, |
| 87 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 88 | JumpTarget* true_target, |
| 89 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 90 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 91 | typeof_state_(typeof_state), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 92 | true_target_(true_target), |
| 93 | false_target_(false_target), |
| 94 | previous_(owner->state()) { |
| 95 | owner_->set_state(this); |
| 96 | } |
| 97 | |
| 98 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 99 | CodeGenState::~CodeGenState() { |
| 100 | ASSERT(owner_->state() == this); |
| 101 | owner_->set_state(previous_); |
| 102 | } |
| 103 | |
| 104 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 105 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 106 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 108 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 109 | bool is_eval) |
| 110 | : is_eval_(is_eval), |
| 111 | script_(script), |
| 112 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 114 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 115 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 116 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | cc_reg_(al), |
| 118 | state_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 119 | function_return_is_shadowed_(false), |
| 120 | in_spilled_code_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
| 124 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 125 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 126 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 127 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | // cp: callee's context |
| 129 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 130 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | ZoneList<Statement*>* body = fun->body(); |
| 132 | |
| 133 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 134 | ASSERT(scope_ == NULL); |
| 135 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 136 | ASSERT(allocator_ == NULL); |
| 137 | RegisterAllocator register_allocator(this); |
| 138 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 139 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 140 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 141 | cc_reg_ = al; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 142 | set_in_spilled_code(false); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 143 | { |
| 144 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 146 | // Entry: |
| 147 | // Stack: receiver, arguments |
| 148 | // lr: return address |
| 149 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 151 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 153 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 154 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 155 | // tos: code slot |
| 156 | #ifdef DEBUG |
| 157 | if (strlen(FLAG_stop_at) > 0 && |
| 158 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 159 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 160 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 161 | } |
| 162 | #endif |
| 163 | |
| 164 | // Allocate space for locals and initialize them. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 165 | frame_->AllocateStackSlots(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 166 | // Initialize the function return target after the locals are set |
| 167 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 168 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 169 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 171 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 172 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | // Allocate local context. |
| 174 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 175 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 176 | frame_->EmitPush(r0); |
| 177 | frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 178 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 179 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 180 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 181 | __ cmp(r0, Operand(cp)); |
| 182 | verified_true.Branch(eq); |
| 183 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 184 | verified_true.Bind(); |
| 185 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 187 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | } |
| 189 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 190 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 191 | // 1) only needed if we have a context |
| 192 | // 2) no need to recompute context ptr every single time |
| 193 | // 3) don't copy parameter operand code from SlotOperand! |
| 194 | { |
| 195 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 196 | |
| 197 | // Note that iteration order is relevant here! If we have the same |
| 198 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 199 | // needs to be copied into the context, it must be the last argument |
| 200 | // passed to the parameter that needs to be copied. This is a rare |
| 201 | // case so we don't check for it, instead we rely on the copying |
| 202 | // order: such a parameter is copied repeatedly into the same |
| 203 | // context location and thus the last value is what is seen inside |
| 204 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 205 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 206 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | Slot* slot = par->slot(); |
| 208 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 209 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 210 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 211 | // Loads r2 with context; used below in RecordWrite. |
| 212 | __ str(r1, SlotOperand(slot, r2)); |
| 213 | // Load the offset into r3. |
| 214 | int slot_offset = |
| 215 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 216 | __ mov(r3, Operand(slot_offset)); |
| 217 | __ RecordWrite(r2, r3, r1); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 222 | // Store the arguments object. This must happen after context |
| 223 | // initialization because the arguments object may be stored in the |
| 224 | // context. |
| 225 | if (scope_->arguments() != NULL) { |
| 226 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 227 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 228 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 229 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 230 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 231 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 232 | // The receiver is below the arguments, the return address, |
| 233 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 234 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 235 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 236 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 237 | frame_->Adjust(3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 238 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 239 | frame_->CallStub(&stub, 3); |
| 240 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 241 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 242 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 243 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 245 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | } |
| 247 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 248 | // Generate code to 'execute' declarations and initialize functions |
| 249 | // (source elements). In case of an illegal redeclaration we need to |
| 250 | // handle that instead of processing the declarations. |
| 251 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 252 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 253 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 254 | } else { |
| 255 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 256 | ProcessDeclarations(scope_->declarations()); |
| 257 | // Bail out if a stack-overflow exception occurred when processing |
| 258 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 259 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | } |
| 261 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 262 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 263 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 264 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 265 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 266 | CheckStack(); |
| 267 | |
| 268 | // Compile the body of the function in a vanilla state. Don't |
| 269 | // bother compiling all the code if the scope has an illegal |
| 270 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 271 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | Comment cmnt(masm_, "[ function body"); |
| 273 | #ifdef DEBUG |
| 274 | bool is_builtin = Bootstrapper::IsActive(); |
| 275 | bool should_trace = |
| 276 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 277 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 278 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 279 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 280 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 282 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | } |
| 285 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 286 | // Generate the return sequence if necessary. |
| 287 | if (frame_ != NULL || function_return_.is_linked()) { |
| 288 | // exit |
| 289 | // r0: result |
| 290 | // sp: stack pointer |
| 291 | // fp: frame pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 292 | // cp: callee's context |
| 293 | __ mov(r0, Operand(Factory::undefined_value())); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 294 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 295 | function_return_.Bind(); |
| 296 | if (FLAG_trace) { |
| 297 | // Push the return value on the stack as the parameter. |
| 298 | // Runtime::TraceExit returns the parameter as it is. |
| 299 | frame_->EmitPush(r0); |
| 300 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 301 | } |
| 302 | |
| 303 | // Tear down the frame which will restore the caller's frame pointer and |
| 304 | // the link register. |
| 305 | frame_->Exit(); |
| 306 | |
| 307 | __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 308 | __ Jump(lr); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 309 | } |
| 310 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 312 | ASSERT(!has_cc()); |
| 313 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 314 | ASSERT(!function_return_is_shadowed_); |
| 315 | function_return_.Unuse(); |
| 316 | DeleteFrame(); |
| 317 | |
| 318 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 319 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 320 | ProcessDeferred(); |
| 321 | } |
| 322 | |
| 323 | allocator_ = NULL; |
| 324 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 328 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 329 | // Currently, this assertion will fail if we try to assign to |
| 330 | // a constant variable that is constant because it is read-only |
| 331 | // (such as the variable referring to a named function expression). |
| 332 | // We need to implement assignments to read-only variables. |
| 333 | // Ideally, we should do this during AST generation (by converting |
| 334 | // such assignments into expression statements); however, in general |
| 335 | // we may not be able to make the decision until past AST generation, |
| 336 | // that is when the entire program is known. |
| 337 | ASSERT(slot != NULL); |
| 338 | int index = slot->index(); |
| 339 | switch (slot->type()) { |
| 340 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 341 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 342 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 343 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 344 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 345 | |
| 346 | case Slot::CONTEXT: { |
| 347 | // Follow the context chain if necessary. |
| 348 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 349 | Register context = cp; |
| 350 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 351 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 352 | // Load the closure. |
| 353 | // (All contexts, even 'with' contexts, have a closure, |
| 354 | // and it is the same for all contexts inside a function. |
| 355 | // There is no need to go to the function context first.) |
| 356 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 357 | // Load the function context (which is the incoming, outer context). |
| 358 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 359 | context = tmp; |
| 360 | } |
| 361 | // We may have a 'with' context now. Get the function context. |
| 362 | // (In fact this mov may never be the needed, since the scope analysis |
| 363 | // may not permit a direct context access in this case and thus we are |
| 364 | // always at a function context. However it is safe to dereference be- |
| 365 | // cause the function context of a function context is itself. Before |
| 366 | // deleting this mov we should try to create a counter-example first, |
| 367 | // though...) |
| 368 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 369 | return ContextOperand(tmp, index); |
| 370 | } |
| 371 | |
| 372 | default: |
| 373 | UNREACHABLE(); |
| 374 | return MemOperand(r0, 0); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 379 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 380 | Slot* slot, |
| 381 | Register tmp, |
| 382 | Register tmp2, |
| 383 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 384 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 385 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 386 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 387 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 388 | if (s->num_heap_slots() > 0) { |
| 389 | if (s->calls_eval()) { |
| 390 | // Check that extension is NULL. |
| 391 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 392 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 393 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 394 | } |
| 395 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 396 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 397 | context = tmp; |
| 398 | } |
| 399 | } |
| 400 | // Check that last extension is NULL. |
| 401 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 402 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 403 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 404 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 405 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 409 | void CodeGenerator::LoadConditionAndSpill(Expression* expression, |
| 410 | TypeofState typeof_state, |
| 411 | JumpTarget* true_target, |
| 412 | JumpTarget* false_target, |
| 413 | bool force_control) { |
| 414 | ASSERT(in_spilled_code()); |
| 415 | set_in_spilled_code(false); |
| 416 | LoadCondition(expression, typeof_state, true_target, false_target, |
| 417 | force_control); |
| 418 | if (frame_ != NULL) { |
| 419 | frame_->SpillAll(); |
| 420 | } |
| 421 | set_in_spilled_code(true); |
| 422 | } |
| 423 | |
| 424 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 425 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 426 | // (partially) translated into branches, or it may have set the condition |
| 427 | // code register. If force_cc is set, the value is forced to set the |
| 428 | // condition code register and no value is pushed. If the condition code |
| 429 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 430 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 431 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 432 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 433 | JumpTarget* true_target, |
| 434 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 435 | bool force_cc) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 436 | ASSERT(!in_spilled_code()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 437 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 438 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 439 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 440 | { CodeGenState new_state(this, typeof_state, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 441 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 442 | |
| 443 | // If we hit a stack overflow, we may not have actually visited |
| 444 | // the expression. In that case, we ensure that we have a |
| 445 | // valid-looking frame state because we will continue to generate |
| 446 | // code as we unwind the C++ stack. |
| 447 | // |
| 448 | // It's possible to have both a stack overflow and a valid frame |
| 449 | // state (eg, a subexpression overflowed, visiting it returned |
| 450 | // with a dummied frame state, and visiting this expression |
| 451 | // returned with a normal-looking state). |
| 452 | if (HasStackOverflow() && |
| 453 | has_valid_frame() && |
| 454 | !has_cc() && |
| 455 | frame_->height() == original_height) { |
| 456 | true_target->Jump(); |
| 457 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 458 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 459 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 460 | // Convert the TOS value to a boolean in the condition code register. |
| 461 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 462 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 463 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 464 | ASSERT(!has_valid_frame() || |
| 465 | (has_cc() && frame_->height() == original_height) || |
| 466 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 470 | void CodeGenerator::LoadAndSpill(Expression* expression, |
| 471 | TypeofState typeof_state) { |
| 472 | ASSERT(in_spilled_code()); |
| 473 | set_in_spilled_code(false); |
| 474 | Load(expression, typeof_state); |
| 475 | frame_->SpillAll(); |
| 476 | set_in_spilled_code(true); |
| 477 | } |
| 478 | |
| 479 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 480 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 481 | #ifdef DEBUG |
| 482 | int original_height = frame_->height(); |
| 483 | #endif |
| 484 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 485 | JumpTarget true_target; |
| 486 | JumpTarget false_target; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 487 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 488 | |
| 489 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 490 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 491 | JumpTarget loaded; |
| 492 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 493 | materialize_true.Branch(cc_reg_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 494 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 495 | frame_->EmitPush(r0); |
| 496 | loaded.Jump(); |
| 497 | materialize_true.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 498 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 499 | frame_->EmitPush(r0); |
| 500 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | cc_reg_ = al; |
| 502 | } |
| 503 | |
| 504 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 505 | // We have at least one condition value that has been "translated" |
| 506 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 507 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 508 | if (frame_ != NULL) { |
| 509 | loaded.Jump(); // Don't lose the current TOS. |
| 510 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 511 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 512 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 513 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 514 | true_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 515 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 516 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 517 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 518 | // If both "true" and "false" need to be loaded jump across the code for |
| 519 | // "false". |
| 520 | if (both) { |
| 521 | loaded.Jump(); |
| 522 | } |
| 523 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 524 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 525 | false_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 526 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 527 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 528 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 529 | // A value is loaded on all paths reaching this point. |
| 530 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 531 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 532 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 533 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 534 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 538 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 539 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 540 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 541 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 545 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 546 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 547 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 548 | __ ldr(scratch, |
| 549 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 550 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 554 | // TODO(1241834): Get rid of this function in favor of just using Load, now |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 555 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 556 | // variables w/o reference errors elsewhere. |
| 557 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 558 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 560 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 561 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 562 | // the variable as if through '<global>.<variable>' to make sure we |
| 563 | // do not get reference errors. |
| 564 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 565 | Literal key(variable->name()); |
| 566 | // TODO(1241834): Fetch the position from the variable instead of using |
| 567 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 568 | Property property(&global, &key, RelocInfo::kNoPosition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 569 | LoadAndSpill(&property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 570 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 571 | LoadAndSpill(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 576 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 577 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 578 | cgen->LoadReference(this); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | Reference::~Reference() { |
| 583 | cgen_->UnloadReference(this); |
| 584 | } |
| 585 | |
| 586 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 587 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 588 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 589 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 590 | Expression* e = ref->expression(); |
| 591 | Property* property = e->AsProperty(); |
| 592 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 593 | |
| 594 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 595 | // The expression is either a property or a variable proxy that rewrites |
| 596 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 597 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 598 | // We use a named reference if the key is a literal symbol, unless it is |
| 599 | // a string that can be legally parsed as an integer. This is because |
| 600 | // otherwise we will not get into the slow case code that handles [] on |
| 601 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 602 | Literal* literal = property->key()->AsLiteral(); |
| 603 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 604 | if (literal != NULL && |
| 605 | literal->handle()->IsSymbol() && |
| 606 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | ref->set_type(Reference::NAMED); |
| 608 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 609 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 610 | ref->set_type(Reference::KEYED); |
| 611 | } |
| 612 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 613 | // The expression is a variable proxy that does not rewrite to a |
| 614 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 615 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | LoadGlobal(); |
| 617 | ref->set_type(Reference::NAMED); |
| 618 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 619 | ASSERT(var->slot() != NULL); |
| 620 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 621 | } |
| 622 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 623 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 624 | LoadAndSpill(e); |
| 625 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 630 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 631 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 632 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 633 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 634 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 635 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 636 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 637 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 638 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
| 642 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 643 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 644 | // register to a boolean in the condition code register. The code |
| 645 | // 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] | 646 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 647 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 648 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 649 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 651 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 652 | |
| 653 | // Fast case checks |
| 654 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 655 | // Check if the value is 'false'. |
| 656 | __ cmp(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 657 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 658 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 659 | // Check if the value is 'true'. |
| 660 | __ cmp(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 661 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 662 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 663 | // Check if the value is 'undefined'. |
| 664 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 665 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 666 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 667 | // Check if the value is a smi. |
| 668 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 669 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 670 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 671 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 672 | |
| 673 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 674 | frame_->EmitPush(r0); |
| 675 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 676 | // Convert the result (r0) to a condition code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 677 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | |
| 679 | cc_reg_ = ne; |
| 680 | } |
| 681 | |
| 682 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 683 | class GenericBinaryOpStub : public CodeStub { |
| 684 | public: |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 685 | GenericBinaryOpStub(Token::Value op, |
| 686 | OverwriteMode mode) |
| 687 | : op_(op), mode_(mode) { } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 688 | |
| 689 | private: |
| 690 | Token::Value op_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 691 | OverwriteMode mode_; |
| 692 | |
| 693 | // Minor key encoding in 16 bits. |
| 694 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 695 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 696 | |
| 697 | Major MajorKey() { return GenericBinaryOp; } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 698 | int MinorKey() { |
| 699 | // Encode the parameters in a unique 16 bit value. |
| 700 | return OpBits::encode(op_) |
| 701 | | ModeBits::encode(mode_); |
| 702 | } |
| 703 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 704 | void Generate(MacroAssembler* masm); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 705 | void HandleNonSmiBitwiseOp(MacroAssembler* masm); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 706 | |
| 707 | const char* GetName() { |
| 708 | switch (op_) { |
| 709 | case Token::ADD: return "GenericBinaryOpStub_ADD"; |
| 710 | case Token::SUB: return "GenericBinaryOpStub_SUB"; |
| 711 | case Token::MUL: return "GenericBinaryOpStub_MUL"; |
| 712 | case Token::DIV: return "GenericBinaryOpStub_DIV"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 713 | case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR"; |
| 714 | case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND"; |
| 715 | case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR"; |
| 716 | case Token::SAR: return "GenericBinaryOpStub_SAR"; |
| 717 | case Token::SHL: return "GenericBinaryOpStub_SHL"; |
| 718 | case Token::SHR: return "GenericBinaryOpStub_SHR"; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 719 | default: return "GenericBinaryOpStub"; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | #ifdef DEBUG |
| 724 | void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); } |
| 725 | #endif |
| 726 | }; |
| 727 | |
| 728 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 729 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
| 730 | OverwriteMode overwrite_mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 731 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 732 | // sp[0] : y |
| 733 | // sp[1] : x |
| 734 | // result : r0 |
| 735 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 736 | // Stub is entered with a call: 'return address' is in lr. |
| 737 | switch (op) { |
| 738 | case Token::ADD: // fall through. |
| 739 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 740 | case Token::MUL: |
| 741 | case Token::BIT_OR: |
| 742 | case Token::BIT_AND: |
| 743 | case Token::BIT_XOR: |
| 744 | case Token::SHL: |
| 745 | case Token::SHR: |
| 746 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 747 | frame_->EmitPop(r0); // r0 : y |
| 748 | frame_->EmitPop(r1); // r1 : x |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 749 | GenericBinaryOpStub stub(op, overwrite_mode); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 750 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 751 | break; |
| 752 | } |
| 753 | |
| 754 | case Token::DIV: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 755 | Result arg_count = allocator_->Allocate(r0); |
| 756 | ASSERT(arg_count.is_valid()); |
| 757 | __ mov(arg_count.reg(), Operand(1)); |
| 758 | frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 759 | break; |
| 760 | } |
| 761 | |
| 762 | case Token::MOD: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 763 | Result arg_count = allocator_->Allocate(r0); |
| 764 | ASSERT(arg_count.is_valid()); |
| 765 | __ mov(arg_count.reg(), Operand(1)); |
| 766 | frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 767 | break; |
| 768 | } |
| 769 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 770 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 771 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 772 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 773 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 774 | break; |
| 775 | |
| 776 | default: |
| 777 | // Other cases should have been handled before this point. |
| 778 | UNREACHABLE(); |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 784 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 785 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 786 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 787 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 788 | bool reversed, |
| 789 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 790 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 791 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 792 | reversed_(reversed), |
| 793 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 794 | set_comment("[ DeferredInlinedSmiOperation"); |
| 795 | } |
| 796 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 797 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 798 | |
| 799 | private: |
| 800 | Token::Value op_; |
| 801 | int value_; |
| 802 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 803 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 804 | }; |
| 805 | |
| 806 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 807 | void DeferredInlineSmiOperation::Generate() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 808 | switch (op_) { |
| 809 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 810 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 811 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 812 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 813 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 814 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 815 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 816 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 817 | } |
| 818 | break; |
| 819 | } |
| 820 | |
| 821 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 822 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 823 | if (reversed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 824 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 825 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 826 | } else { |
| 827 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 828 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 829 | } |
| 830 | break; |
| 831 | } |
| 832 | |
| 833 | case Token::BIT_OR: |
| 834 | case Token::BIT_XOR: |
| 835 | case Token::BIT_AND: { |
| 836 | if (reversed_) { |
| 837 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 838 | } else { |
| 839 | __ mov(r1, Operand(r0)); |
| 840 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 841 | } |
| 842 | break; |
| 843 | } |
| 844 | |
| 845 | case Token::SHL: |
| 846 | case Token::SHR: |
| 847 | case Token::SAR: { |
| 848 | if (!reversed_) { |
| 849 | __ mov(r1, Operand(r0)); |
| 850 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 851 | } else { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 852 | UNREACHABLE(); // Should have been handled in SmiOperation. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 853 | } |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 858 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 859 | UNREACHABLE(); |
| 860 | break; |
| 861 | } |
| 862 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 863 | GenericBinaryOpStub stub(op_, overwrite_mode_); |
| 864 | __ CallStub(&stub); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 868 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 869 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 870 | bool reversed, |
| 871 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 872 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 873 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 874 | // some possible smi operations (like + and -) when (at least) one |
| 875 | // of the operands is a literal smi. With this optimization, the |
| 876 | // performance of the system is increased by ~15%, and the generated |
| 877 | // code size is increased by ~1% (measured on a combination of |
| 878 | // different benchmarks). |
| 879 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 880 | // sp[0] : operand |
| 881 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 882 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 883 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 884 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 885 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 886 | |
| 887 | switch (op) { |
| 888 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 889 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 890 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 891 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 892 | __ add(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 893 | deferred->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 894 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 895 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 896 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 897 | break; |
| 898 | } |
| 899 | |
| 900 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 901 | DeferredCode* deferred = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 902 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 903 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 904 | if (reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 905 | __ rsb(r0, r0, Operand(value), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 906 | } else { |
| 907 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 908 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 909 | deferred->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 910 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 911 | deferred->Branch(ne); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 912 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 913 | break; |
| 914 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 915 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 916 | case Token::BIT_OR: |
| 917 | case Token::BIT_XOR: |
| 918 | case Token::BIT_AND: { |
| 919 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 920 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 921 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 922 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 923 | switch (op) { |
| 924 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 925 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 926 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 927 | default: UNREACHABLE(); |
| 928 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 929 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 930 | break; |
| 931 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 932 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 933 | case Token::SHL: |
| 934 | case Token::SHR: |
| 935 | case Token::SAR: { |
| 936 | if (reversed) { |
| 937 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 938 | frame_->EmitPush(ip); |
| 939 | frame_->EmitPush(r0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 940 | GenericBinaryOperation(op, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 941 | |
| 942 | } else { |
| 943 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 944 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 945 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 946 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 947 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 948 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 949 | switch (op) { |
| 950 | case Token::SHL: { |
| 951 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 952 | // check that the *unsigned* result fits in a smi |
| 953 | __ add(r3, r2, Operand(0x40000000), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 954 | deferred->Branch(mi); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 955 | break; |
| 956 | } |
| 957 | case Token::SHR: { |
| 958 | // LSR by immediate 0 means shifting 32 bits. |
| 959 | if (shift_value != 0) { |
| 960 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 961 | } |
| 962 | // check that the *unsigned* result fits in a smi |
| 963 | // neither of the two high-order bits can be set: |
| 964 | // - 0x80000000: high bit would be lost when smi tagging |
| 965 | // - 0x40000000: this number would convert to negative when |
| 966 | // smi tagging these two cases can only happen with shifts |
| 967 | // by 0 or 1 when handed a valid smi |
| 968 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 969 | deferred->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 970 | break; |
| 971 | } |
| 972 | case Token::SAR: { |
| 973 | if (shift_value != 0) { |
| 974 | // ASR by immediate 0 means shifting 32 bits. |
| 975 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 976 | } |
| 977 | break; |
| 978 | } |
| 979 | default: UNREACHABLE(); |
| 980 | } |
| 981 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 982 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 983 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 984 | break; |
| 985 | } |
| 986 | |
| 987 | default: |
| 988 | if (!reversed) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 989 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 990 | __ mov(r0, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 991 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 992 | } else { |
| 993 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 994 | frame_->EmitPush(ip); |
| 995 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 996 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 997 | GenericBinaryOperation(op, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 998 | break; |
| 999 | } |
| 1000 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1001 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1005 | void CodeGenerator::Comparison(Condition cc, bool strict) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1006 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1007 | // sp[0] : y |
| 1008 | // sp[1] : x |
| 1009 | // result : cc register |
| 1010 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1011 | // Strict only makes sense for equality comparisons. |
| 1012 | ASSERT(!strict || cc == eq); |
| 1013 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1014 | JumpTarget exit; |
| 1015 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1016 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1017 | if (cc == gt || cc == le) { |
| 1018 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1019 | frame_->EmitPop(r1); |
| 1020 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1021 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1022 | frame_->EmitPop(r0); |
| 1023 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1024 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1025 | __ orr(r2, r0, Operand(r1)); |
| 1026 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1027 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1028 | |
| 1029 | // Perform non-smi comparison by runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1030 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Figure out which native to call and setup the arguments. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1033 | Builtins::JavaScript native; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1034 | int arg_count = 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1035 | if (cc == eq) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1036 | native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1037 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1038 | native = Builtins::COMPARE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1039 | int ncr; // NaN compare result |
| 1040 | if (cc == lt || cc == le) { |
| 1041 | ncr = GREATER; |
| 1042 | } else { |
| 1043 | ASSERT(cc == gt || cc == ge); // remaining cases |
| 1044 | ncr = LESS; |
| 1045 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1046 | frame_->EmitPush(r0); |
| 1047 | arg_count++; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1048 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 1052 | // tagged as a small integer. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1053 | frame_->EmitPush(r0); |
| 1054 | Result arg_count_register = allocator_->Allocate(r0); |
| 1055 | ASSERT(arg_count_register.is_valid()); |
| 1056 | __ mov(arg_count_register.reg(), Operand(arg_count)); |
| 1057 | Result result = frame_->InvokeBuiltin(native, |
| 1058 | CALL_JS, |
| 1059 | &arg_count_register, |
| 1060 | arg_count + 1); |
| 1061 | __ cmp(result.reg(), Operand(0)); |
| 1062 | result.Unuse(); |
| 1063 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1064 | |
| 1065 | // test smi equality by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1066 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1067 | __ cmp(r1, Operand(r0)); |
| 1068 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1069 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1070 | cc_reg_ = cc; |
| 1071 | } |
| 1072 | |
| 1073 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1074 | class CallFunctionStub: public CodeStub { |
| 1075 | public: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1076 | CallFunctionStub(int argc, InLoopFlag in_loop) |
| 1077 | : argc_(argc), in_loop_(in_loop) {} |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1078 | |
| 1079 | void Generate(MacroAssembler* masm); |
| 1080 | |
| 1081 | private: |
| 1082 | int argc_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1083 | InLoopFlag in_loop_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1084 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1085 | #if defined(DEBUG) |
| 1086 | void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); } |
| 1087 | #endif // defined(DEBUG) |
| 1088 | |
| 1089 | Major MajorKey() { return CallFunction; } |
| 1090 | int MinorKey() { return argc_; } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1091 | InLoopFlag InLoop() { return in_loop_; } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1092 | }; |
| 1093 | |
| 1094 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1095 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1096 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1097 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1098 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1099 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1100 | int arg_count = args->length(); |
| 1101 | for (int i = 0; i < arg_count; i++) { |
| 1102 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1103 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1104 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1105 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1106 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1107 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1108 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1109 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 1110 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1111 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1114 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1115 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1119 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1120 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1121 | ASSERT(has_cc()); |
| 1122 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1123 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1124 | cc_reg_ = al; |
| 1125 | } |
| 1126 | |
| 1127 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1128 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1129 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1130 | if (FLAG_check_stack) { |
| 1131 | Comment cmnt(masm_, "[ check stack"); |
| 1132 | StackCheckStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1133 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1138 | void CodeGenerator::VisitAndSpill(Statement* statement) { |
| 1139 | ASSERT(in_spilled_code()); |
| 1140 | set_in_spilled_code(false); |
| 1141 | Visit(statement); |
| 1142 | if (frame_ != NULL) { |
| 1143 | frame_->SpillAll(); |
| 1144 | } |
| 1145 | set_in_spilled_code(true); |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) { |
| 1150 | ASSERT(in_spilled_code()); |
| 1151 | set_in_spilled_code(false); |
| 1152 | VisitStatements(statements); |
| 1153 | if (frame_ != NULL) { |
| 1154 | frame_->SpillAll(); |
| 1155 | } |
| 1156 | set_in_spilled_code(true); |
| 1157 | } |
| 1158 | |
| 1159 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1160 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1161 | #ifdef DEBUG |
| 1162 | int original_height = frame_->height(); |
| 1163 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1164 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1165 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1166 | VisitAndSpill(statements->at(i)); |
| 1167 | } |
| 1168 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1169 | } |
| 1170 | |
| 1171 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1172 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1173 | #ifdef DEBUG |
| 1174 | int original_height = frame_->height(); |
| 1175 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1176 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1177 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1178 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1179 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1180 | VisitStatementsAndSpill(node->statements()); |
| 1181 | if (node->break_target()->is_linked()) { |
| 1182 | node->break_target()->Bind(); |
| 1183 | } |
| 1184 | node->break_target()->Unuse(); |
| 1185 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1189 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1190 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1191 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1192 | frame_->EmitPush(r0); |
| 1193 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1194 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1195 | frame_->EmitPush(r0); |
| 1196 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1197 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1201 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1202 | #ifdef DEBUG |
| 1203 | int original_height = frame_->height(); |
| 1204 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1205 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1206 | Comment cmnt(masm_, "[ Declaration"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1207 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1208 | Variable* var = node->proxy()->var(); |
| 1209 | ASSERT(var != NULL); // must have been resolved |
| 1210 | Slot* slot = var->slot(); |
| 1211 | |
| 1212 | // If it was not possible to allocate the variable at compile time, |
| 1213 | // we need to "declare" it at runtime to make sure it actually |
| 1214 | // exists in the local context. |
| 1215 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1216 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1217 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1218 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1219 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1220 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1221 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1222 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1223 | // Declaration nodes are always declared in only two modes. |
| 1224 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1225 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1226 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1227 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1228 | // Push initial value, if any. |
| 1229 | // Note: For variables we must not push an initial value (such as |
| 1230 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1231 | // must not destroy the current value. |
| 1232 | if (node->mode() == Variable::CONST) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1233 | __ mov(r0, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1234 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1235 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1236 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1237 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1238 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1239 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1240 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1241 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1242 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1243 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | ASSERT(!var->is_global()); |
| 1248 | |
| 1249 | // If we have a function or a constant, we need to initialize the variable. |
| 1250 | Expression* val = NULL; |
| 1251 | if (node->mode() == Variable::CONST) { |
| 1252 | val = new Literal(Factory::the_hole_value()); |
| 1253 | } else { |
| 1254 | val = node->fun(); // NULL if we don't have a function |
| 1255 | } |
| 1256 | |
| 1257 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1258 | { |
| 1259 | // Set initial value. |
| 1260 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1261 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1262 | target.SetValue(NOT_CONST_INIT); |
| 1263 | // The reference is removed from the stack (preserving TOS) when |
| 1264 | // it goes out of scope. |
| 1265 | } |
| 1266 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1267 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1268 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1269 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1273 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1274 | #ifdef DEBUG |
| 1275 | int original_height = frame_->height(); |
| 1276 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1277 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1278 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1279 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1280 | Expression* expression = node->expression(); |
| 1281 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1282 | LoadAndSpill(expression); |
| 1283 | frame_->Drop(); |
| 1284 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1288 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1289 | #ifdef DEBUG |
| 1290 | int original_height = frame_->height(); |
| 1291 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1292 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1294 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1296 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1300 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1301 | #ifdef DEBUG |
| 1302 | int original_height = frame_->height(); |
| 1303 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1304 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1305 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1306 | // Generate different code depending on which parts of the if statement |
| 1307 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | bool has_then_stm = node->HasThenStatement(); |
| 1309 | bool has_else_stm = node->HasElseStatement(); |
| 1310 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1311 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1312 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1313 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1314 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1315 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1316 | JumpTarget then; |
| 1317 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1318 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1319 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1320 | &then, &else_, true); |
| 1321 | if (frame_ != NULL) { |
| 1322 | Branch(false, &else_); |
| 1323 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1324 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1325 | if (frame_ != NULL || then.is_linked()) { |
| 1326 | then.Bind(); |
| 1327 | VisitAndSpill(node->then_statement()); |
| 1328 | } |
| 1329 | if (frame_ != NULL) { |
| 1330 | exit.Jump(); |
| 1331 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1332 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1333 | if (else_.is_linked()) { |
| 1334 | else_.Bind(); |
| 1335 | VisitAndSpill(node->else_statement()); |
| 1336 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1337 | |
| 1338 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1339 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1340 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1341 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1342 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1343 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1344 | &then, &exit, true); |
| 1345 | if (frame_ != NULL) { |
| 1346 | Branch(false, &exit); |
| 1347 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1348 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1349 | if (frame_ != NULL || then.is_linked()) { |
| 1350 | then.Bind(); |
| 1351 | VisitAndSpill(node->then_statement()); |
| 1352 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1353 | |
| 1354 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1355 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1356 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1357 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1358 | // if (!cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1359 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1360 | &exit, &else_, true); |
| 1361 | if (frame_ != NULL) { |
| 1362 | Branch(true, &exit); |
| 1363 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1364 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1365 | if (frame_ != NULL || else_.is_linked()) { |
| 1366 | else_.Bind(); |
| 1367 | VisitAndSpill(node->else_statement()); |
| 1368 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1369 | |
| 1370 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1371 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1372 | ASSERT(!has_then_stm && !has_else_stm); |
| 1373 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1374 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1375 | &exit, &exit, false); |
| 1376 | if (frame_ != NULL) { |
| 1377 | if (has_cc()) { |
| 1378 | cc_reg_ = al; |
| 1379 | } else { |
| 1380 | frame_->Drop(); |
| 1381 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1386 | if (exit.is_linked()) { |
| 1387 | exit.Bind(); |
| 1388 | } |
| 1389 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1393 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1394 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1395 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1396 | CodeForStatementPosition(node); |
| 1397 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1401 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1402 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1403 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1404 | CodeForStatementPosition(node); |
| 1405 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1409 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1410 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1411 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1412 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1413 | if (function_return_is_shadowed_) { |
| 1414 | CodeForStatementPosition(node); |
| 1415 | LoadAndSpill(node->expression()); |
| 1416 | frame_->EmitPop(r0); |
| 1417 | function_return_.Jump(); |
| 1418 | } else { |
| 1419 | // Load the returned value. |
| 1420 | CodeForStatementPosition(node); |
| 1421 | LoadAndSpill(node->expression()); |
| 1422 | |
| 1423 | // Pop the result from the frame and prepare the frame for |
| 1424 | // returning thus making it easier to merge. |
| 1425 | frame_->EmitPop(r0); |
| 1426 | frame_->PrepareForReturn(); |
| 1427 | |
| 1428 | function_return_.Jump(); |
| 1429 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1433 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1434 | #ifdef DEBUG |
| 1435 | int original_height = frame_->height(); |
| 1436 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1437 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1438 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1439 | CodeForStatementPosition(node); |
| 1440 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1441 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1442 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1443 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1444 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1445 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1446 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1447 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1448 | __ cmp(r0, Operand(cp)); |
| 1449 | verified_true.Branch(eq); |
| 1450 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1451 | verified_true.Bind(); |
| 1452 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1453 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1454 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1455 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1459 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1460 | #ifdef DEBUG |
| 1461 | int original_height = frame_->height(); |
| 1462 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1463 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1464 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1465 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1466 | // Pop context. |
| 1467 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1468 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1469 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1470 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1474 | int CodeGenerator::FastCaseSwitchMaxOverheadFactor() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1475 | return kFastSwitchMaxOverheadFactor; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1478 | int CodeGenerator::FastCaseSwitchMinCaseCount() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1479 | return kFastSwitchMinCaseCount; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1483 | void CodeGenerator::GenerateFastCaseSwitchJumpTable( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1484 | SwitchStatement* node, |
| 1485 | int min_index, |
| 1486 | int range, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1487 | Label* default_label, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1488 | Vector<Label*> case_targets, |
| 1489 | Vector<Label> case_labels) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1490 | VirtualFrame::SpilledScope spilled_scope; |
| 1491 | JumpTarget setup_default; |
| 1492 | JumpTarget is_smi; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1493 | |
| 1494 | // A non-null default label pointer indicates a default case among |
| 1495 | // the case labels. Otherwise we use the break target as a |
| 1496 | // "default" for failure to hit the jump table. |
| 1497 | JumpTarget* default_target = |
| 1498 | (default_label == NULL) ? node->break_target() : &setup_default; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1499 | |
| 1500 | ASSERT(kSmiTag == 0 && kSmiTagSize <= 2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1501 | frame_->EmitPop(r0); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1502 | |
| 1503 | // Test for a Smi value in a HeapNumber. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1504 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1505 | is_smi.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 1506 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1507 | default_target->Branch(ne); |
| 1508 | frame_->EmitPush(r0); |
| 1509 | frame_->CallRuntime(Runtime::kNumberToSmi, 1); |
| 1510 | is_smi.Bind(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1511 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1512 | if (min_index != 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1513 | // Small positive numbers can be immediate operands. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1514 | if (min_index < 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1515 | // If min_index is Smi::kMinValue, -min_index is not a Smi. |
| 1516 | if (Smi::IsValid(-min_index)) { |
| 1517 | __ add(r0, r0, Operand(Smi::FromInt(-min_index))); |
| 1518 | } else { |
| 1519 | __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1))); |
| 1520 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1521 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1522 | } else { |
| 1523 | __ sub(r0, r0, Operand(Smi::FromInt(min_index))); |
| 1524 | } |
| 1525 | } |
| 1526 | __ tst(r0, Operand(0x80000000 | kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1527 | default_target->Branch(ne); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1528 | __ cmp(r0, Operand(Smi::FromInt(range))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1529 | default_target->Branch(ge); |
| 1530 | VirtualFrame* start_frame = new VirtualFrame(frame_); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1531 | __ SmiJumpTable(r0, case_targets); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1532 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1533 | GenerateFastCaseSwitchCases(node, case_labels, start_frame); |
| 1534 | |
| 1535 | // If there was a default case among the case labels, we need to |
| 1536 | // emit code to jump to it from the default target used for failure |
| 1537 | // to hit the jump table. |
| 1538 | if (default_label != NULL) { |
| 1539 | if (has_valid_frame()) { |
| 1540 | node->break_target()->Jump(); |
| 1541 | } |
| 1542 | setup_default.Bind(); |
| 1543 | frame_->MergeTo(start_frame); |
| 1544 | __ b(default_label); |
| 1545 | DeleteFrame(); |
| 1546 | } |
| 1547 | if (node->break_target()->is_linked()) { |
| 1548 | node->break_target()->Bind(); |
| 1549 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1553 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1554 | #ifdef DEBUG |
| 1555 | int original_height = frame_->height(); |
| 1556 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1557 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1558 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1559 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1560 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1561 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1562 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1563 | if (TryGenerateFastCaseSwitchStatement(node)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1564 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1565 | return; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1568 | JumpTarget next_test; |
| 1569 | JumpTarget fall_through; |
| 1570 | JumpTarget default_entry; |
| 1571 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1572 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1573 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1574 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1575 | |
| 1576 | for (int i = 0; i < length; i++) { |
| 1577 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1578 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1579 | // Remember the default clause and compile it at the end. |
| 1580 | default_clause = clause; |
| 1581 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1584 | Comment cmnt(masm_, "[ Case clause"); |
| 1585 | // Compile the test. |
| 1586 | next_test.Bind(); |
| 1587 | next_test.Unuse(); |
| 1588 | // Duplicate TOS. |
| 1589 | __ ldr(r0, frame_->Top()); |
| 1590 | frame_->EmitPush(r0); |
| 1591 | LoadAndSpill(clause->label()); |
| 1592 | Comparison(eq, true); |
| 1593 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1594 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1595 | // Before entering the body from the test, remove the switch value from |
| 1596 | // the stack. |
| 1597 | frame_->Drop(); |
| 1598 | |
| 1599 | // Label the body so that fall through is enabled. |
| 1600 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1601 | default_exit.Bind(); |
| 1602 | } else { |
| 1603 | fall_through.Bind(); |
| 1604 | fall_through.Unuse(); |
| 1605 | } |
| 1606 | VisitStatementsAndSpill(clause->statements()); |
| 1607 | |
| 1608 | // If control flow can fall through from the body, jump to the next body |
| 1609 | // or the end of the statement. |
| 1610 | if (frame_ != NULL) { |
| 1611 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1612 | default_entry.Jump(); |
| 1613 | } else { |
| 1614 | fall_through.Jump(); |
| 1615 | } |
| 1616 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1619 | // The final "test" removes the switch value. |
| 1620 | next_test.Bind(); |
| 1621 | frame_->Drop(); |
| 1622 | |
| 1623 | // If there is a default clause, compile it. |
| 1624 | if (default_clause != NULL) { |
| 1625 | Comment cmnt(masm_, "[ Default clause"); |
| 1626 | default_entry.Bind(); |
| 1627 | VisitStatementsAndSpill(default_clause->statements()); |
| 1628 | // If control flow can fall out of the default and there is a case after |
| 1629 | // it, jup to that case's body. |
| 1630 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1631 | default_exit.Jump(); |
| 1632 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1633 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1634 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1635 | if (fall_through.is_linked()) { |
| 1636 | fall_through.Bind(); |
| 1637 | } |
| 1638 | |
| 1639 | if (node->break_target()->is_linked()) { |
| 1640 | node->break_target()->Bind(); |
| 1641 | } |
| 1642 | node->break_target()->Unuse(); |
| 1643 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1647 | void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1648 | #ifdef DEBUG |
| 1649 | int original_height = frame_->height(); |
| 1650 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1651 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1652 | Comment cmnt(masm_, "[ LoopStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1653 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1654 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1655 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1656 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 1657 | // known result for the test expression, with no side effects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1658 | enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
| 1659 | if (node->cond() == NULL) { |
| 1660 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1661 | info = ALWAYS_TRUE; |
| 1662 | } else { |
| 1663 | Literal* lit = node->cond()->AsLiteral(); |
| 1664 | if (lit != NULL) { |
| 1665 | if (lit->IsTrue()) { |
| 1666 | info = ALWAYS_TRUE; |
| 1667 | } else if (lit->IsFalse()) { |
| 1668 | info = ALWAYS_FALSE; |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1673 | switch (node->type()) { |
| 1674 | case LoopStatement::DO_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1675 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1676 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1677 | // Label the top of the loop for the backward CFG edge. If the test |
| 1678 | // is always true we can use the continue target, and if the test is |
| 1679 | // always false there is no need. |
| 1680 | if (info == ALWAYS_TRUE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1681 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1682 | node->continue_target()->Bind(); |
| 1683 | } else if (info == ALWAYS_FALSE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1684 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1685 | } else { |
| 1686 | ASSERT(info == DONT_KNOW); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1687 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1688 | body.Bind(); |
| 1689 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1690 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1691 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1692 | VisitAndSpill(node->body()); |
| 1693 | |
| 1694 | // Compile the test. |
| 1695 | if (info == ALWAYS_TRUE) { |
| 1696 | if (has_valid_frame()) { |
| 1697 | // If control can fall off the end of the body, jump back to the |
| 1698 | // top. |
| 1699 | node->continue_target()->Jump(); |
| 1700 | } |
| 1701 | } else if (info == ALWAYS_FALSE) { |
| 1702 | // If we have a continue in the body, we only have to bind its jump |
| 1703 | // target. |
| 1704 | if (node->continue_target()->is_linked()) { |
| 1705 | node->continue_target()->Bind(); |
| 1706 | } |
| 1707 | } else { |
| 1708 | ASSERT(info == DONT_KNOW); |
| 1709 | // We have to compile the test expression if it can be reached by |
| 1710 | // control flow falling out of the body or via continue. |
| 1711 | if (node->continue_target()->is_linked()) { |
| 1712 | node->continue_target()->Bind(); |
| 1713 | } |
| 1714 | if (has_valid_frame()) { |
| 1715 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1716 | &body, node->break_target(), true); |
| 1717 | if (has_valid_frame()) { |
| 1718 | // A invalid frame here indicates that control did not |
| 1719 | // fall out of the test expression. |
| 1720 | Branch(true, &body); |
| 1721 | } |
| 1722 | } |
| 1723 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1724 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | case LoopStatement::WHILE_LOOP: { |
| 1728 | // If the test is never true and has no side effects there is no need |
| 1729 | // to compile the test or body. |
| 1730 | if (info == ALWAYS_FALSE) break; |
| 1731 | |
| 1732 | // Label the top of the loop with the continue target for the backward |
| 1733 | // CFG edge. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1734 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1735 | node->continue_target()->Bind(); |
| 1736 | |
| 1737 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1738 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1739 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1740 | &body, node->break_target(), true); |
| 1741 | if (has_valid_frame()) { |
| 1742 | // A NULL frame indicates that control did not fall out of the |
| 1743 | // test expression. |
| 1744 | Branch(false, node->break_target()); |
| 1745 | } |
| 1746 | if (has_valid_frame() || body.is_linked()) { |
| 1747 | body.Bind(); |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | if (has_valid_frame()) { |
| 1752 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1753 | VisitAndSpill(node->body()); |
| 1754 | |
| 1755 | // If control flow can fall out of the body, jump back to the top. |
| 1756 | if (has_valid_frame()) { |
| 1757 | node->continue_target()->Jump(); |
| 1758 | } |
| 1759 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1760 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | case LoopStatement::FOR_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1764 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1765 | |
| 1766 | if (node->init() != NULL) { |
| 1767 | VisitAndSpill(node->init()); |
| 1768 | } |
| 1769 | |
| 1770 | // There is no need to compile the test or body. |
| 1771 | if (info == ALWAYS_FALSE) break; |
| 1772 | |
| 1773 | // If there is no update statement, label the top of the loop with the |
| 1774 | // continue target, otherwise with the loop target. |
| 1775 | if (node->next() == NULL) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1776 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1777 | node->continue_target()->Bind(); |
| 1778 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1779 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1780 | loop.Bind(); |
| 1781 | } |
| 1782 | |
| 1783 | // If the test is always true, there is no need to compile it. |
| 1784 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1785 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1786 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1787 | &body, node->break_target(), true); |
| 1788 | if (has_valid_frame()) { |
| 1789 | Branch(false, node->break_target()); |
| 1790 | } |
| 1791 | if (has_valid_frame() || body.is_linked()) { |
| 1792 | body.Bind(); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | if (has_valid_frame()) { |
| 1797 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1798 | VisitAndSpill(node->body()); |
| 1799 | |
| 1800 | if (node->next() == NULL) { |
| 1801 | // If there is no update statement and control flow can fall out |
| 1802 | // of the loop, jump directly to the continue label. |
| 1803 | if (has_valid_frame()) { |
| 1804 | node->continue_target()->Jump(); |
| 1805 | } |
| 1806 | } else { |
| 1807 | // If there is an update statement and control flow can reach it |
| 1808 | // via falling out of the body of the loop or continuing, we |
| 1809 | // compile the update statement. |
| 1810 | if (node->continue_target()->is_linked()) { |
| 1811 | node->continue_target()->Bind(); |
| 1812 | } |
| 1813 | if (has_valid_frame()) { |
| 1814 | // Record source position of the statement as this code which is |
| 1815 | // after the code for the body actually belongs to the loop |
| 1816 | // statement and not the body. |
| 1817 | CodeForStatementPosition(node); |
| 1818 | VisitAndSpill(node->next()); |
| 1819 | loop.Jump(); |
| 1820 | } |
| 1821 | } |
| 1822 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1823 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1824 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1827 | if (node->break_target()->is_linked()) { |
| 1828 | node->break_target()->Bind(); |
| 1829 | } |
| 1830 | node->continue_target()->Unuse(); |
| 1831 | node->break_target()->Unuse(); |
| 1832 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1836 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1837 | #ifdef DEBUG |
| 1838 | int original_height = frame_->height(); |
| 1839 | #endif |
| 1840 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1841 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1842 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1843 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1844 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1845 | JumpTarget primitive; |
| 1846 | JumpTarget jsobject; |
| 1847 | JumpTarget fixed_array; |
| 1848 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1849 | JumpTarget end_del_check; |
| 1850 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1851 | |
| 1852 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1853 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1854 | |
| 1855 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1856 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1857 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1858 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1859 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1860 | __ cmp(r0, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1861 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1862 | |
| 1863 | // Stack layout in body: |
| 1864 | // [iteration counter (Smi)] |
| 1865 | // [length of array] |
| 1866 | // [FixedArray] |
| 1867 | // [Map or 0] |
| 1868 | // [Object] |
| 1869 | |
| 1870 | // Check if enumerable is already a JSObject |
| 1871 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1872 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 1873 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1874 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1875 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1876 | primitive.Bind(); |
| 1877 | frame_->EmitPush(r0); |
| 1878 | Result arg_count = allocator_->Allocate(r0); |
| 1879 | ASSERT(arg_count.is_valid()); |
| 1880 | __ mov(arg_count.reg(), Operand(0)); |
| 1881 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1882 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1883 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1885 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1886 | frame_->EmitPush(r0); |
| 1887 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1888 | |
| 1889 | // If we got a Map, we can do a fast modification check. |
| 1890 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1891 | __ mov(r2, Operand(r0)); |
| 1892 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1893 | __ cmp(r1, Operand(Factory::meta_map())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1894 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1895 | |
| 1896 | // Get enum cache |
| 1897 | __ mov(r1, Operand(r0)); |
| 1898 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1899 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1900 | __ ldr(r2, |
| 1901 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1902 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1903 | frame_->EmitPush(r0); // map |
| 1904 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1905 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1906 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1907 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1908 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1909 | frame_->EmitPush(r0); |
| 1910 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1911 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1912 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1913 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1914 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1915 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1916 | |
| 1917 | // 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] | 1918 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1919 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1920 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1921 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1922 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1923 | |
| 1924 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1925 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1926 | // sp[0] : index |
| 1927 | // sp[1] : array/enum cache length |
| 1928 | // sp[2] : array or enum cache |
| 1929 | // sp[3] : 0 or map |
| 1930 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1931 | // Grab the current frame's height for the break and continue |
| 1932 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1933 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1934 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1935 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1936 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1937 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1938 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1939 | node->break_target()->Branch(hs); |
| 1940 | |
| 1941 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1942 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1943 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1944 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1945 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1946 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1947 | |
| 1948 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1949 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | // Check if this (still) matches the map of the enumerable. |
| 1951 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1952 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1953 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1954 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1955 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1956 | |
| 1957 | // 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] | 1958 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1959 | frame_->EmitPush(r0); |
| 1960 | frame_->EmitPush(r3); // push entry |
| 1961 | Result arg_count_register = allocator_->Allocate(r0); |
| 1962 | ASSERT(arg_count_register.is_valid()); |
| 1963 | __ mov(arg_count_register.reg(), Operand(1)); |
| 1964 | Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY, |
| 1965 | CALL_JS, |
| 1966 | &arg_count_register, |
| 1967 | 2); |
| 1968 | __ mov(r3, Operand(result.reg())); |
| 1969 | result.Unuse(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1970 | |
| 1971 | // If the property has been removed while iterating, we just skip it. |
| 1972 | __ cmp(r3, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1973 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1974 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1975 | end_del_check.Bind(); |
| 1976 | // Store the entry in the 'each' expression and take another spin in the |
| 1977 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1978 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1979 | { Reference each(this, node->each()); |
| 1980 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1981 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1982 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1983 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1984 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1985 | // If the reference was to a slot we rely on the convenient property |
| 1986 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1987 | // right on top of or right underneath a zero-sized reference. |
| 1988 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1989 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1990 | // It's safe to pop the value lying on top of the reference before |
| 1991 | // unloading the reference itself (which preserves the top of stack, |
| 1992 | // ie, now the topmost value of the non-zero sized reference), since |
| 1993 | // we will discard the top of stack after unloading the reference |
| 1994 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1995 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1996 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1997 | } |
| 1998 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1999 | // Discard the i'th entry pushed above or else the remainder of the |
| 2000 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2001 | frame_->Drop(); |
| 2002 | |
| 2003 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2004 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2005 | VisitAndSpill(node->body()); |
| 2006 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2007 | // Next. Reestablish a spilled frame in case we are coming here via |
| 2008 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2009 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2010 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2011 | frame_->EmitPop(r0); |
| 2012 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 2013 | frame_->EmitPush(r0); |
| 2014 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2015 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2016 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 2017 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2018 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2019 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2020 | |
| 2021 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2022 | exit.Bind(); |
| 2023 | node->continue_target()->Unuse(); |
| 2024 | node->break_target()->Unuse(); |
| 2025 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2029 | void CodeGenerator::VisitTryCatch(TryCatch* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2030 | #ifdef DEBUG |
| 2031 | int original_height = frame_->height(); |
| 2032 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2033 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2034 | Comment cmnt(masm_, "[ TryCatch"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2035 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2036 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2037 | JumpTarget try_block; |
| 2038 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2039 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2040 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2041 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2042 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2043 | |
| 2044 | // Store the caught exception in the catch variable. |
| 2045 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2046 | ASSERT(ref.is_slot()); |
| 2047 | // Here we make use of the convenient property that it doesn't matter |
| 2048 | // whether a value is immediately on top of or underneath a zero-sized |
| 2049 | // reference. |
| 2050 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2054 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2055 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2056 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2057 | if (frame_ != NULL) { |
| 2058 | exit.Jump(); |
| 2059 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2060 | |
| 2061 | |
| 2062 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2063 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2064 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2065 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2066 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2067 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2068 | // Shadow the labels for all escapes from the try block, including |
| 2069 | // returns. During shadowing, the original label is hidden as the |
| 2070 | // LabelShadow and operations on the original actually affect the |
| 2071 | // shadowing label. |
| 2072 | // |
| 2073 | // We should probably try to unify the escaping labels and the return |
| 2074 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2075 | int nof_escapes = node->escaping_targets()->length(); |
| 2076 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2077 | |
| 2078 | // Add the shadow target for the function return. |
| 2079 | static const int kReturnShadowIndex = 0; |
| 2080 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2081 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2082 | function_return_is_shadowed_ = true; |
| 2083 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2084 | |
| 2085 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2086 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2087 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2091 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | |
| 2093 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2094 | // After shadowing stops, the original labels are unshadowed and the |
| 2095 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2096 | bool has_unlinks = false; |
| 2097 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2098 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2099 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2100 | } |
| 2101 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2102 | |
| 2103 | // Get an external reference to the handler address. |
| 2104 | ExternalReference handler_address(Top::k_handler_address); |
| 2105 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2106 | // If we can fall off the end of the try block, unlink from try chain. |
| 2107 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 2108 | // The next handler address is on top of the frame. Unlink from |
| 2109 | // the handler list and drop the rest of this handler from the |
| 2110 | // frame. |
| 2111 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2112 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2113 | __ mov(r3, Operand(handler_address)); |
| 2114 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 2115 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2116 | if (has_unlinks) { |
| 2117 | exit.Jump(); |
| 2118 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2121 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2122 | // jumped to. Deallocate each shadow target. |
| 2123 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2124 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2125 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2126 | shadows[i]->Bind(); |
| 2127 | // Because we can be jumping here (to spilled code) from unspilled |
| 2128 | // code, we need to reestablish a spilled frame at this block. |
| 2129 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2130 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2131 | // Reload sp from the top handler, because some statements that we |
| 2132 | // 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] | 2133 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2134 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 2135 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2136 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 2137 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2138 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2139 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2140 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2141 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2142 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2143 | frame_->PrepareForReturn(); |
| 2144 | } |
| 2145 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2149 | exit.Bind(); |
| 2150 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2154 | void CodeGenerator::VisitTryFinally(TryFinally* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2155 | #ifdef DEBUG |
| 2156 | int original_height = frame_->height(); |
| 2157 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2158 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2159 | Comment cmnt(masm_, "[ TryFinally"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2160 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2161 | |
| 2162 | // State: Used to keep track of reason for entering the finally |
| 2163 | // block. Should probably be extended to hold information for |
| 2164 | // break/continue from within the try block. |
| 2165 | enum { FALLING, THROWING, JUMPING }; |
| 2166 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2167 | JumpTarget try_block; |
| 2168 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2169 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2170 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2171 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2172 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2173 | // In case of thrown exceptions, this is where we continue. |
| 2174 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2175 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2176 | |
| 2177 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2178 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2179 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2180 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2181 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2182 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2183 | // Shadow the labels for all escapes from the try block, including |
| 2184 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2185 | // operations on the original actually affect the shadowing label. |
| 2186 | // |
| 2187 | // We should probably try to unify the escaping labels and the return |
| 2188 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2189 | int nof_escapes = node->escaping_targets()->length(); |
| 2190 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2191 | |
| 2192 | // Add the shadow target for the function return. |
| 2193 | static const int kReturnShadowIndex = 0; |
| 2194 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2195 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2196 | function_return_is_shadowed_ = true; |
| 2197 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2198 | |
| 2199 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2200 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2201 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2205 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2206 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2207 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2208 | // After shadowing stops, the original labels are unshadowed and the |
| 2209 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2210 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2211 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2212 | shadows[i]->StopShadowing(); |
| 2213 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2214 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2215 | function_return_is_shadowed_ = function_return_was_shadowed; |
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 | // Get an external reference to the handler address. |
| 2218 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2219 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2220 | // If we can fall off the end of the try block, unlink from the try |
| 2221 | // chain and set the state on the frame to FALLING. |
| 2222 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 2223 | // The next handler address is on top of the frame. |
| 2224 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 2225 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2226 | __ mov(r3, Operand(handler_address)); |
| 2227 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 2228 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2229 | |
| 2230 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2231 | // state in r2, then jump around the unlink blocks if any. |
| 2232 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2233 | frame_->EmitPush(r0); |
| 2234 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2235 | if (nof_unlinks > 0) { |
| 2236 | finally_block.Jump(); |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | // Generate code to unlink and set the state for the (formerly) |
| 2241 | // shadowing targets that have been jumped to. |
| 2242 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2243 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2244 | // If we have come from the shadowed return, the return value is |
| 2245 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2246 | // until it is pushed. |
| 2247 | // |
| 2248 | // Because we can be jumping here (to spilled code) from |
| 2249 | // unspilled code, we need to reestablish a spilled frame at |
| 2250 | // this block. |
| 2251 | shadows[i]->Bind(); |
| 2252 | frame_->SpillAll(); |
| 2253 | |
| 2254 | // Reload sp from the top handler, because some statements that |
| 2255 | // we break from (eg, for...in) may have left stuff on the |
| 2256 | // stack. |
| 2257 | __ mov(r3, Operand(handler_address)); |
| 2258 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 2259 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2260 | |
| 2261 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 2262 | // handler address is currently on top of the frame. |
| 2263 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2264 | frame_->EmitPop(r1); |
| 2265 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 2266 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2267 | |
| 2268 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2269 | // If this label shadowed the function return, materialize the |
| 2270 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2271 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2272 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2273 | // Fake TOS for targets that shadowed breaks and continues. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2274 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2275 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2276 | } |
| 2277 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2278 | if (--nof_unlinks > 0) { |
| 2279 | // If this is not the last unlink block, jump around the next. |
| 2280 | finally_block.Jump(); |
| 2281 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2282 | } |
| 2283 | } |
| 2284 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2285 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2286 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2287 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2288 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2289 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2290 | |
| 2291 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2292 | // and the state - while evaluating the finally block. |
| 2293 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2294 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2295 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2296 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2297 | if (has_valid_frame()) { |
| 2298 | // Restore state and return value or faked TOS. |
| 2299 | frame_->EmitPop(r2); |
| 2300 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2303 | // Generate code to jump to the right destination for all used |
| 2304 | // formerly shadowing targets. Deallocate each shadow target. |
| 2305 | for (int i = 0; i < shadows.length(); i++) { |
| 2306 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2307 | JumpTarget* original = shadows[i]->other_target(); |
| 2308 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2309 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2310 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2311 | skip.Branch(ne); |
| 2312 | frame_->PrepareForReturn(); |
| 2313 | original->Jump(); |
| 2314 | skip.Bind(); |
| 2315 | } else { |
| 2316 | original->Branch(eq); |
| 2317 | } |
| 2318 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2319 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2320 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2321 | if (has_valid_frame()) { |
| 2322 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2323 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2324 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2325 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2326 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2327 | // Rethrow exception. |
| 2328 | frame_->EmitPush(r0); |
| 2329 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2330 | |
| 2331 | // Done. |
| 2332 | exit.Bind(); |
| 2333 | } |
| 2334 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2338 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2339 | #ifdef DEBUG |
| 2340 | int original_height = frame_->height(); |
| 2341 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2342 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2343 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2344 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2345 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2346 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2347 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2348 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2349 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2353 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2354 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2355 | ASSERT(boilerplate->IsBoilerplate()); |
| 2356 | |
| 2357 | // Push the boilerplate on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2358 | __ mov(r0, Operand(boilerplate)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2359 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2360 | |
| 2361 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2362 | frame_->EmitPush(cp); |
| 2363 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2364 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2368 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2369 | #ifdef DEBUG |
| 2370 | int original_height = frame_->height(); |
| 2371 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2372 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2373 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2374 | |
| 2375 | // Build the function boilerplate and instantiate it. |
| 2376 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2377 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2378 | if (HasStackOverflow()) { |
| 2379 | ASSERT(frame_->height() == original_height); |
| 2380 | return; |
| 2381 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2382 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2383 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2387 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2388 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2389 | #ifdef DEBUG |
| 2390 | int original_height = frame_->height(); |
| 2391 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2392 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2393 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2394 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2395 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2399 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2400 | #ifdef DEBUG |
| 2401 | int original_height = frame_->height(); |
| 2402 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2403 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2404 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2405 | JumpTarget then; |
| 2406 | JumpTarget else_; |
| 2407 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2408 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 2409 | &then, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2410 | Branch(false, &else_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2411 | then.Bind(); |
| 2412 | LoadAndSpill(node->then_expression(), typeof_state()); |
| 2413 | exit.Jump(); |
| 2414 | else_.Bind(); |
| 2415 | LoadAndSpill(node->else_expression(), typeof_state()); |
| 2416 | exit.Bind(); |
| 2417 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2421 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2422 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2423 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2424 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2425 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2426 | JumpTarget slow; |
| 2427 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2428 | |
| 2429 | // Generate fast-case code for variables that might be shadowed by |
| 2430 | // eval-introduced variables. Eval is used a lot without |
| 2431 | // introducing variables. In those cases, we do not want to |
| 2432 | // perform a runtime call for all variables in the scope |
| 2433 | // containing the eval. |
| 2434 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2435 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2436 | // If there was no control flow to slow, we can exit early. |
| 2437 | if (!slow.is_linked()) { |
| 2438 | frame_->EmitPush(r0); |
| 2439 | return; |
| 2440 | } |
| 2441 | |
| 2442 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2443 | |
| 2444 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2445 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2446 | // Only generate the fast case for locals that rewrite to slots. |
| 2447 | // This rules out argument loads. |
| 2448 | if (potential_slot != NULL) { |
| 2449 | __ ldr(r0, |
| 2450 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2451 | r1, |
| 2452 | r2, |
| 2453 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2454 | if (potential_slot->var()->mode() == Variable::CONST) { |
| 2455 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2456 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
| 2457 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2458 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2459 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2460 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2461 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2465 | slow.Bind(); |
| 2466 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2467 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2468 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2469 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2470 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2471 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2472 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2473 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2474 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2475 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2476 | done.Bind(); |
| 2477 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2478 | |
| 2479 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2480 | // Note: We would like to keep the assert below, but it fires because of |
| 2481 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2482 | // ASSERT(!slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2483 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2484 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2485 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2486 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2487 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2488 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2489 | // initialized yet) which needs to be converted into the 'undefined' |
| 2490 | // value. |
| 2491 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2492 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2493 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2494 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2495 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2496 | } |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2501 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2502 | TypeofState typeof_state, |
| 2503 | Register tmp, |
| 2504 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2505 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2506 | // Check that no extension objects have been created by calls to |
| 2507 | // eval from the current scope to the global scope. |
| 2508 | Register context = cp; |
| 2509 | Scope* s = scope(); |
| 2510 | while (s != NULL) { |
| 2511 | if (s->num_heap_slots() > 0) { |
| 2512 | if (s->calls_eval()) { |
| 2513 | // Check that extension is NULL. |
| 2514 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2515 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2516 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2517 | } |
| 2518 | // Load next context in chain. |
| 2519 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2520 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2521 | context = tmp; |
| 2522 | } |
| 2523 | // If no outer scope calls eval, we do not need to check more |
| 2524 | // context extensions. |
| 2525 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2526 | s = s->outer_scope(); |
| 2527 | } |
| 2528 | |
| 2529 | if (s->is_eval_scope()) { |
| 2530 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2531 | if (!context.is(tmp)) { |
| 2532 | __ mov(tmp, Operand(context)); |
| 2533 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2534 | __ bind(&next); |
| 2535 | // Terminate at global context. |
| 2536 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 2537 | __ cmp(tmp2, Operand(Factory::global_context_map())); |
| 2538 | __ b(eq, &fast); |
| 2539 | // Check that extension is NULL. |
| 2540 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2541 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2542 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2543 | // Load next context in chain. |
| 2544 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2545 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2546 | __ b(&next); |
| 2547 | __ bind(&fast); |
| 2548 | } |
| 2549 | |
| 2550 | // All extension objects were empty and it is safe to use a global |
| 2551 | // load IC call. |
| 2552 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2553 | // Load the global object. |
| 2554 | LoadGlobal(); |
| 2555 | // Setup the name register. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2556 | Result name = allocator_->Allocate(r2); |
| 2557 | ASSERT(name.is_valid()); // We are in spilled code. |
| 2558 | __ mov(name.reg(), Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2559 | // Call IC stub. |
| 2560 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2561 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2562 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2563 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2566 | // Drop the global object. The result is in r0. |
| 2567 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2571 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2572 | #ifdef DEBUG |
| 2573 | int original_height = frame_->height(); |
| 2574 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2575 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2576 | Comment cmnt(masm_, "[ Slot"); |
| 2577 | LoadFromSlot(node, typeof_state()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2578 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2579 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2580 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2581 | |
| 2582 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2583 | #ifdef DEBUG |
| 2584 | int original_height = frame_->height(); |
| 2585 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2586 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2587 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2588 | |
| 2589 | Variable* var = node->var(); |
| 2590 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2591 | if (expr != NULL) { |
| 2592 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2593 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2594 | ASSERT(var->is_global()); |
| 2595 | Reference ref(this, node); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2596 | ref.GetValueAndSpill(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2597 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2598 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2602 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2603 | #ifdef DEBUG |
| 2604 | int original_height = frame_->height(); |
| 2605 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2606 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2607 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2608 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2609 | frame_->EmitPush(r0); |
| 2610 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2614 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2615 | #ifdef DEBUG |
| 2616 | int original_height = frame_->height(); |
| 2617 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2618 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2619 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2620 | |
| 2621 | // Retrieve the literal array and check the allocated entry. |
| 2622 | |
| 2623 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2624 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2625 | |
| 2626 | // Load the literals array of the function. |
| 2627 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2628 | |
| 2629 | // Load the literal at the ast saved index. |
| 2630 | int literal_offset = |
| 2631 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2632 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2633 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2634 | JumpTarget done; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2635 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2636 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2637 | |
| 2638 | // If the entry is undefined we call the runtime system to computed |
| 2639 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2640 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2641 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2642 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2643 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2644 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2645 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2646 | frame_->EmitPush(r0); |
| 2647 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2648 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2649 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2650 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2651 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2652 | frame_->EmitPush(r2); |
| 2653 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | |
| 2657 | // This deferred code stub will be used for creating the boilerplate |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2658 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2659 | // Each created boilerplate is stored in the JSFunction and they are |
| 2660 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2661 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2662 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2663 | explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2664 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2665 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2666 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2667 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2668 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2669 | private: |
| 2670 | ObjectLiteral* node_; |
| 2671 | }; |
| 2672 | |
| 2673 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2674 | void DeferredObjectLiteral::Generate() { |
| 2675 | // Argument is passed in r1. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2676 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2677 | // 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] | 2678 | // the literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2679 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2680 | __ push(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2681 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2682 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2683 | __ push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2684 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2685 | __ mov(r0, Operand(node_->constant_properties())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2686 | __ push(r0); |
| 2687 | __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2688 | __ mov(r2, Operand(r0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2689 | // Result is returned in r2. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2693 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2694 | #ifdef DEBUG |
| 2695 | int original_height = frame_->height(); |
| 2696 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2697 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2698 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2699 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2700 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2701 | |
| 2702 | // Retrieve the literal array and check the allocated entry. |
| 2703 | |
| 2704 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2705 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2706 | |
| 2707 | // Load the literals array of the function. |
| 2708 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2709 | |
| 2710 | // Load the literal at the ast saved index. |
| 2711 | int literal_offset = |
| 2712 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2713 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2714 | |
| 2715 | // Check whether we need to materialize the object literal boilerplate. |
| 2716 | // If so, jump to the deferred code. |
| 2717 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2718 | deferred->Branch(eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2719 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2720 | |
| 2721 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2722 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2723 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2724 | // Clone the boilerplate object. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2725 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2726 | if (node->depth() == 1) { |
| 2727 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2728 | } |
| 2729 | frame_->CallRuntime(clone_function_id, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2730 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2731 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2732 | |
| 2733 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2734 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2735 | Literal* key = property->key(); |
| 2736 | Expression* value = property->value(); |
| 2737 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2738 | case ObjectLiteral::Property::CONSTANT: |
| 2739 | break; |
| 2740 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2741 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2742 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2743 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2744 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2745 | frame_->EmitPush(r0); // dup the result |
| 2746 | LoadAndSpill(key); |
| 2747 | LoadAndSpill(value); |
| 2748 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2749 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2750 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2751 | break; |
| 2752 | } |
| 2753 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2754 | frame_->EmitPush(r0); |
| 2755 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2756 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2757 | frame_->EmitPush(r0); |
| 2758 | LoadAndSpill(value); |
| 2759 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2760 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2761 | break; |
| 2762 | } |
| 2763 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2764 | frame_->EmitPush(r0); |
| 2765 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2766 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2767 | frame_->EmitPush(r0); |
| 2768 | LoadAndSpill(value); |
| 2769 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2770 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2771 | break; |
| 2772 | } |
| 2773 | } |
| 2774 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2775 | ASSERT(frame_->height() == original_height + 1); |
| 2776 | } |
| 2777 | |
| 2778 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2779 | // This deferred code stub will be used for creating the boilerplate |
| 2780 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2781 | // Each created boilerplate is stored in the JSFunction and they are |
| 2782 | // therefore context dependent. |
| 2783 | class DeferredArrayLiteral: public DeferredCode { |
| 2784 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2785 | explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2786 | set_comment("[ DeferredArrayLiteral"); |
| 2787 | } |
| 2788 | |
| 2789 | virtual void Generate(); |
| 2790 | |
| 2791 | private: |
| 2792 | ArrayLiteral* node_; |
| 2793 | }; |
| 2794 | |
| 2795 | |
| 2796 | void DeferredArrayLiteral::Generate() { |
| 2797 | // Argument is passed in r1. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2798 | |
| 2799 | // If the entry is undefined we call the runtime system to computed |
| 2800 | // the literal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2801 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2802 | __ push(r1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2803 | // Literal index (1). |
| 2804 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2805 | __ push(r0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2806 | // Constant properties (2). |
| 2807 | __ mov(r0, Operand(node_->literals())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2808 | __ push(r0); |
| 2809 | __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2810 | __ mov(r2, Operand(r0)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2811 | // Result is returned in r2. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2815 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2816 | #ifdef DEBUG |
| 2817 | int original_height = frame_->height(); |
| 2818 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2819 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2820 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2821 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2822 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2823 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2824 | // Retrieve the literal array and check the allocated entry. |
| 2825 | |
| 2826 | // Load the function of this activation. |
| 2827 | __ ldr(r1, frame_->Function()); |
| 2828 | |
| 2829 | // Load the literals array of the function. |
| 2830 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2831 | |
| 2832 | // Load the literal at the ast saved index. |
| 2833 | int literal_offset = |
| 2834 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2835 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2836 | |
| 2837 | // Check whether we need to materialize the object literal boilerplate. |
| 2838 | // If so, jump to the deferred code. |
| 2839 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 2840 | deferred->Branch(eq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2841 | deferred->BindExit(); |
| 2842 | |
| 2843 | // Push the object literal boilerplate. |
| 2844 | frame_->EmitPush(r2); |
| 2845 | |
| 2846 | // Clone the boilerplate object. |
| 2847 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2848 | if (node->depth() == 1) { |
| 2849 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2850 | } |
| 2851 | frame_->CallRuntime(clone_function_id, 1); |
| 2852 | frame_->EmitPush(r0); // save the result |
| 2853 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2854 | |
| 2855 | // Generate code to set the elements in the array that are not |
| 2856 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2857 | for (int i = 0; i < node->values()->length(); i++) { |
| 2858 | Expression* value = node->values()->at(i); |
| 2859 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2860 | // If value is a literal the property value is already set in the |
| 2861 | // boilerplate object. |
| 2862 | if (value->AsLiteral() != NULL) continue; |
| 2863 | // If value is a materialized literal the property value is already set |
| 2864 | // in the boilerplate object if it is simple. |
| 2865 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2866 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2867 | // The property must be set by generated code. |
| 2868 | LoadAndSpill(value); |
| 2869 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2870 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2871 | // Fetch the object literal. |
| 2872 | __ ldr(r1, frame_->Top()); |
| 2873 | // Get the elements array. |
| 2874 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2875 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2876 | // Write to the indexed properties array. |
| 2877 | int offset = i * kPointerSize + Array::kHeaderSize; |
| 2878 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2879 | |
| 2880 | // Update the write barrier for the array address. |
| 2881 | __ mov(r3, Operand(offset)); |
| 2882 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2883 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2884 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2888 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2889 | #ifdef DEBUG |
| 2890 | int original_height = frame_->height(); |
| 2891 | #endif |
| 2892 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2893 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2894 | // Call runtime routine to allocate the catch extension object and |
| 2895 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2896 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2897 | LoadAndSpill(node->key()); |
| 2898 | LoadAndSpill(node->value()); |
| 2899 | Result result = |
| 2900 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2901 | frame_->EmitPush(result.reg()); |
| 2902 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2906 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2907 | #ifdef DEBUG |
| 2908 | int original_height = frame_->height(); |
| 2909 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2910 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2911 | Comment cmnt(masm_, "[ Assignment"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2912 | CodeForStatementPosition(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2913 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2914 | { Reference target(this, node->target()); |
| 2915 | if (target.is_illegal()) { |
| 2916 | // Fool the virtual frame into thinking that we left the assignment's |
| 2917 | // value on the frame. |
| 2918 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2919 | frame_->EmitPush(r0); |
| 2920 | ASSERT(frame_->height() == original_height + 1); |
| 2921 | return; |
| 2922 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2923 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2924 | if (node->op() == Token::ASSIGN || |
| 2925 | node->op() == Token::INIT_VAR || |
| 2926 | node->op() == Token::INIT_CONST) { |
| 2927 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2928 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2929 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2930 | // +=, *= and similar binary assignments. |
| 2931 | // Get the old value of the lhs. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2932 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 2933 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2934 | bool overwrite = |
| 2935 | (node->value()->AsBinaryOperation() != NULL && |
| 2936 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2937 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2938 | SmiOperation(node->binary_op(), |
| 2939 | literal->handle(), |
| 2940 | false, |
| 2941 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2942 | frame_->EmitPush(r0); |
| 2943 | |
| 2944 | } else { |
| 2945 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2946 | GenericBinaryOperation(node->binary_op(), |
| 2947 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2948 | frame_->EmitPush(r0); |
| 2949 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2950 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2951 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2952 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2953 | if (var != NULL && |
| 2954 | (var->mode() == Variable::CONST) && |
| 2955 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2956 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2957 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2958 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2959 | CodeForSourcePosition(node->position()); |
| 2960 | if (node->op() == Token::INIT_CONST) { |
| 2961 | // Dynamic constant initializations must use the function context |
| 2962 | // and initialize the actual constant declared. Dynamic variable |
| 2963 | // initializations are simply assignments and use SetValue. |
| 2964 | target.SetValue(CONST_INIT); |
| 2965 | } else { |
| 2966 | target.SetValue(NOT_CONST_INIT); |
| 2967 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2968 | } |
| 2969 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2970 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2974 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2975 | #ifdef DEBUG |
| 2976 | int original_height = frame_->height(); |
| 2977 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2978 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2979 | Comment cmnt(masm_, "[ Throw"); |
| 2980 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2981 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2982 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2983 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2984 | frame_->EmitPush(r0); |
| 2985 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2989 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2990 | #ifdef DEBUG |
| 2991 | int original_height = frame_->height(); |
| 2992 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2993 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2994 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2995 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2996 | { Reference property(this, node); |
| 2997 | property.GetValueAndSpill(typeof_state()); |
| 2998 | } |
| 2999 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3003 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3004 | #ifdef DEBUG |
| 3005 | int original_height = frame_->height(); |
| 3006 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3007 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3008 | Comment cmnt(masm_, "[ Call"); |
| 3009 | |
| 3010 | ZoneList<Expression*>* args = node->arguments(); |
| 3011 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3012 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3013 | // Standard function call. |
| 3014 | |
| 3015 | // Check if the function is a variable or a property. |
| 3016 | Expression* function = node->expression(); |
| 3017 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 3018 | Property* property = function->AsProperty(); |
| 3019 | |
| 3020 | // ------------------------------------------------------------------------ |
| 3021 | // Fast-case: Use inline caching. |
| 3022 | // --- |
| 3023 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 3024 | // must be resolved after the arguments have been evaluated. The IC code |
| 3025 | // automatically handles this by loading the arguments before the function |
| 3026 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 3027 | // ------------------------------------------------------------------------ |
| 3028 | |
| 3029 | if (var != NULL && !var->is_this() && var->is_global()) { |
| 3030 | // ---------------------------------- |
| 3031 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3032 | // ---------------------------------- |
| 3033 | |
| 3034 | // 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] | 3035 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3036 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3037 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3038 | // Pass the global object as the receiver and let the IC stub |
| 3039 | // patch the stack to use the global proxy as 'this' in the |
| 3040 | // invoked function. |
| 3041 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3042 | |
| 3043 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3044 | int arg_count = args->length(); |
| 3045 | for (int i = 0; i < arg_count; i++) { |
| 3046 | LoadAndSpill(args->at(i)); |
| 3047 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3048 | |
| 3049 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3050 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3051 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3052 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3053 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3054 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3055 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3056 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3057 | frame_->Drop(); |
| 3058 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3059 | |
| 3060 | } else if (var != NULL && var->slot() != NULL && |
| 3061 | var->slot()->type() == Slot::LOOKUP) { |
| 3062 | // ---------------------------------- |
| 3063 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3064 | // ---------------------------------- |
| 3065 | |
| 3066 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3067 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3068 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3069 | frame_->EmitPush(r0); |
| 3070 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3071 | // r0: slot value; r1: receiver |
| 3072 | |
| 3073 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3074 | frame_->EmitPush(r0); // function |
| 3075 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3076 | |
| 3077 | // Call the function. |
| 3078 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3079 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3080 | |
| 3081 | } else if (property != NULL) { |
| 3082 | // Check if the key is a literal string. |
| 3083 | Literal* literal = property->key()->AsLiteral(); |
| 3084 | |
| 3085 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3086 | // ------------------------------------------------------------------ |
| 3087 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3088 | // ------------------------------------------------------------------ |
| 3089 | |
| 3090 | // 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] | 3091 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3092 | frame_->EmitPush(r0); |
| 3093 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3094 | |
| 3095 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3096 | int arg_count = args->length(); |
| 3097 | for (int i = 0; i < arg_count; i++) { |
| 3098 | LoadAndSpill(args->at(i)); |
| 3099 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3100 | |
| 3101 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3102 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3103 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3104 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3105 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3106 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3107 | |
| 3108 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3109 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3110 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3111 | 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] | 3112 | |
| 3113 | } else { |
| 3114 | // ------------------------------------------- |
| 3115 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3116 | // ------------------------------------------- |
| 3117 | |
| 3118 | // Load the function to call from the property through a reference. |
| 3119 | Reference ref(this, property); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3120 | ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3121 | |
| 3122 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3123 | if (property->is_synthetic()) { |
| 3124 | LoadGlobalReceiver(r0); |
| 3125 | } else { |
| 3126 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3127 | frame_->EmitPush(r0); |
| 3128 | } |
| 3129 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3130 | // Call the function. |
| 3131 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3132 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | } else { |
| 3136 | // ---------------------------------- |
| 3137 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3138 | // ---------------------------------- |
| 3139 | |
| 3140 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3141 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3142 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3143 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3144 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3145 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3146 | // Call the function. |
| 3147 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3148 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3149 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3150 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3154 | void CodeGenerator::VisitCallEval(CallEval* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3155 | #ifdef DEBUG |
| 3156 | int original_height = frame_->height(); |
| 3157 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3158 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3159 | Comment cmnt(masm_, "[ CallEval"); |
| 3160 | |
| 3161 | // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve |
| 3162 | // the function we need to call and the receiver of the call. |
| 3163 | // Then we call the resolved function using the given arguments. |
| 3164 | |
| 3165 | ZoneList<Expression*>* args = node->arguments(); |
| 3166 | Expression* function = node->expression(); |
| 3167 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3168 | CodeForStatementPosition(node); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3169 | |
| 3170 | // Prepare stack for call to resolved function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3171 | LoadAndSpill(function); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3172 | __ mov(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3173 | frame_->EmitPush(r2); // Slot for receiver |
| 3174 | int arg_count = args->length(); |
| 3175 | for (int i = 0; i < arg_count; i++) { |
| 3176 | LoadAndSpill(args->at(i)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | // Prepare stack for call to ResolvePossiblyDirectEval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3180 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3181 | frame_->EmitPush(r1); |
| 3182 | if (arg_count > 0) { |
| 3183 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3184 | frame_->EmitPush(r1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3185 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3186 | frame_->EmitPush(r2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | // Resolve the call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3190 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3191 | |
| 3192 | // Touch up stack with the right values for the function and the receiver. |
| 3193 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3194 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3195 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3196 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3197 | |
| 3198 | // Call the function. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3199 | CodeForSourcePosition(node->position()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3200 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3201 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3202 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3203 | frame_->CallStub(&call_function, arg_count + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3204 | |
| 3205 | __ ldr(cp, frame_->Context()); |
| 3206 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3207 | frame_->Drop(); |
| 3208 | frame_->EmitPush(r0); |
| 3209 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3213 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3214 | #ifdef DEBUG |
| 3215 | int original_height = frame_->height(); |
| 3216 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3217 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3218 | Comment cmnt(masm_, "[ CallNew"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3219 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3220 | |
| 3221 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3222 | // expression in new calls must be evaluated before the |
| 3223 | // arguments. This is different from ordinary calls, where the |
| 3224 | // actual function to call is resolved after the arguments have been |
| 3225 | // evaluated. |
| 3226 | |
| 3227 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3228 | // receiver. There is no need to use the global proxy here because |
| 3229 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3230 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3231 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3232 | |
| 3233 | // Push the arguments ("left-to-right") on the stack. |
| 3234 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3235 | int arg_count = args->length(); |
| 3236 | for (int i = 0; i < arg_count; i++) { |
| 3237 | LoadAndSpill(args->at(i)); |
| 3238 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3239 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3240 | // r0: the number of arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3241 | Result num_args = allocator_->Allocate(r0); |
| 3242 | ASSERT(num_args.is_valid()); |
| 3243 | __ mov(num_args.reg(), Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3244 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3245 | // Load the function into r1 as per calling convention. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3246 | Result function = allocator_->Allocate(r1); |
| 3247 | ASSERT(function.is_valid()); |
| 3248 | __ ldr(function.reg(), frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3249 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3250 | // Call the construct call builtin that handles allocation and |
| 3251 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3252 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3253 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
| 3254 | Result result = frame_->CallCodeObject(ic, |
| 3255 | RelocInfo::CONSTRUCT_CALL, |
| 3256 | &num_args, |
| 3257 | &function, |
| 3258 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3259 | |
| 3260 | // 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] | 3261 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3262 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3266 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3267 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3268 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3269 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3270 | LoadAndSpill(args->at(0)); |
| 3271 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3272 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3273 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3274 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 3275 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3276 | __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3277 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3278 | // Load the value. |
| 3279 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3280 | leave.Bind(); |
| 3281 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3285 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3286 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3287 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3288 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3289 | LoadAndSpill(args->at(0)); // Load the object. |
| 3290 | LoadAndSpill(args->at(1)); // Load the value. |
| 3291 | frame_->EmitPop(r0); // r0 contains value |
| 3292 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3293 | // if (object->IsSmi()) return object. |
| 3294 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3295 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 3296 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
| 3297 | __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3298 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3299 | // Store the value. |
| 3300 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3301 | // Update the write barrier. |
| 3302 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3303 | __ RecordWrite(r1, r2, r3); |
| 3304 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3305 | leave.Bind(); |
| 3306 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3310 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3311 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3312 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3313 | LoadAndSpill(args->at(0)); |
| 3314 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3315 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3316 | cc_reg_ = eq; |
| 3317 | } |
| 3318 | |
| 3319 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3320 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3321 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3322 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3323 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3324 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3325 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3326 | LoadAndSpill(args->at(1)); |
| 3327 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3328 | __ CallRuntime(Runtime::kLog, 2); |
| 3329 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3330 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3331 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3332 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3336 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3337 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3338 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3339 | LoadAndSpill(args->at(0)); |
| 3340 | frame_->EmitPop(r0); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3341 | __ tst(r0, Operand(kSmiTagMask | 0x80000000)); |
| 3342 | cc_reg_ = eq; |
| 3343 | } |
| 3344 | |
| 3345 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3346 | // This should generate code that performs a charCodeAt() call or returns |
| 3347 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3348 | // 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] | 3349 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3350 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3351 | ASSERT(args->length() == 2); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3352 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3353 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3357 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3358 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3359 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3360 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3361 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3362 | // We need the CC bits to come out as not_equal in the case where the |
| 3363 | // object is a smi. This can't be done with the usual test opcode so |
| 3364 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3365 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3366 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3367 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3368 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 3369 | // It is a heap object - get the map. Check if the object is a JS array. |
| 3370 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3371 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3372 | cc_reg_ = eq; |
| 3373 | } |
| 3374 | |
| 3375 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3376 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3377 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3378 | ASSERT(args->length() == 0); |
| 3379 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3380 | // Seed the result with the formal parameters count, which will be used |
| 3381 | // 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] | 3382 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3383 | |
| 3384 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3385 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3386 | frame_->CallStub(&stub, 0); |
| 3387 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3391 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3392 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3393 | ASSERT(args->length() == 1); |
| 3394 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3395 | // Satisfy contract with ArgumentsAccessStub: |
| 3396 | // 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] | 3397 | LoadAndSpill(args->at(0)); |
| 3398 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3399 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3400 | |
| 3401 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3402 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3403 | frame_->CallStub(&stub, 0); |
| 3404 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 3408 | void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) { |
| 3409 | VirtualFrame::SpilledScope spilled_scope; |
| 3410 | ASSERT(args->length() == 0); |
| 3411 | __ Call(ExternalReference::random_positive_smi_function().address(), |
| 3412 | RelocInfo::RUNTIME_ENTRY); |
| 3413 | frame_->EmitPush(r0); |
| 3414 | } |
| 3415 | |
| 3416 | |
| 3417 | void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) { |
| 3418 | VirtualFrame::SpilledScope spilled_scope; |
| 3419 | LoadAndSpill(args->at(0)); |
| 3420 | switch (op) { |
| 3421 | case SIN: |
| 3422 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 3423 | break; |
| 3424 | case COS: |
| 3425 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 3426 | break; |
| 3427 | } |
| 3428 | frame_->EmitPush(r0); |
| 3429 | } |
| 3430 | |
| 3431 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3432 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3433 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3434 | ASSERT(args->length() == 2); |
| 3435 | |
| 3436 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3437 | LoadAndSpill(args->at(0)); |
| 3438 | LoadAndSpill(args->at(1)); |
| 3439 | frame_->EmitPop(r0); |
| 3440 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3441 | __ cmp(r0, Operand(r1)); |
| 3442 | cc_reg_ = eq; |
| 3443 | } |
| 3444 | |
| 3445 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3446 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3447 | #ifdef DEBUG |
| 3448 | int original_height = frame_->height(); |
| 3449 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3450 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3451 | if (CheckForInlineRuntimeCall(node)) { |
| 3452 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3453 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3454 | return; |
| 3455 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3456 | |
| 3457 | ZoneList<Expression*>* args = node->arguments(); |
| 3458 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3459 | Runtime::Function* function = node->function(); |
| 3460 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3461 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3462 | // Prepare stack for calling JS runtime function. |
| 3463 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3464 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3465 | // Push the builtins object found in the current global object. |
| 3466 | __ ldr(r1, GlobalObject()); |
| 3467 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3468 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3469 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3470 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3471 | // Push the arguments ("left-to-right"). |
| 3472 | int arg_count = args->length(); |
| 3473 | for (int i = 0; i < arg_count; i++) { |
| 3474 | LoadAndSpill(args->at(i)); |
| 3475 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3476 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3477 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3478 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3479 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3480 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3481 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3482 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3483 | frame_->Drop(); |
| 3484 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3485 | } else { |
| 3486 | // Call the C runtime function. |
| 3487 | frame_->CallRuntime(function, arg_count); |
| 3488 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3489 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3490 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3494 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3495 | #ifdef DEBUG |
| 3496 | int original_height = frame_->height(); |
| 3497 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3498 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3499 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3500 | |
| 3501 | Token::Value op = node->op(); |
| 3502 | |
| 3503 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3504 | LoadConditionAndSpill(node->expression(), |
| 3505 | NOT_INSIDE_TYPEOF, |
| 3506 | false_target(), |
| 3507 | true_target(), |
| 3508 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3509 | cc_reg_ = NegateCondition(cc_reg_); |
| 3510 | |
| 3511 | } else if (op == Token::DELETE) { |
| 3512 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3513 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3514 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3515 | LoadAndSpill(property->obj()); |
| 3516 | LoadAndSpill(property->key()); |
| 3517 | Result arg_count = allocator_->Allocate(r0); |
| 3518 | ASSERT(arg_count.is_valid()); |
| 3519 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3520 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3521 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3522 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3523 | Slot* slot = variable->slot(); |
| 3524 | if (variable->is_global()) { |
| 3525 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3526 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3527 | frame_->EmitPush(r0); |
| 3528 | Result arg_count = allocator_->Allocate(r0); |
| 3529 | ASSERT(arg_count.is_valid()); |
| 3530 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3531 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3532 | |
| 3533 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3534 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3535 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3536 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3537 | frame_->EmitPush(r0); |
| 3538 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3539 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3540 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3541 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3542 | frame_->EmitPush(r0); |
| 3543 | Result arg_count = allocator_->Allocate(r0); |
| 3544 | ASSERT(arg_count.is_valid()); |
| 3545 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3546 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3547 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3548 | } else { |
| 3549 | // Default: Result of deleting non-global, not dynamically |
| 3550 | // introduced variables is false. |
| 3551 | __ mov(r0, Operand(Factory::false_value())); |
| 3552 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3553 | |
| 3554 | } else { |
| 3555 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3556 | LoadAndSpill(node->expression()); // may have side-effects |
| 3557 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3558 | __ mov(r0, Operand(Factory::true_value())); |
| 3559 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3560 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3561 | |
| 3562 | } else if (op == Token::TYPEOF) { |
| 3563 | // Special case for loading the typeof expression; see comment on |
| 3564 | // LoadTypeofExpression(). |
| 3565 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3566 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3567 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3568 | |
| 3569 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3570 | LoadAndSpill(node->expression()); |
| 3571 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3572 | switch (op) { |
| 3573 | case Token::NOT: |
| 3574 | case Token::DELETE: |
| 3575 | case Token::TYPEOF: |
| 3576 | UNREACHABLE(); // handled above |
| 3577 | break; |
| 3578 | |
| 3579 | case Token::SUB: { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 3580 | bool overwrite = |
| 3581 | (node->AsBinaryOperation() != NULL && |
| 3582 | node->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3583 | UnarySubStub stub(overwrite); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3584 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3585 | break; |
| 3586 | } |
| 3587 | |
| 3588 | case Token::BIT_NOT: { |
| 3589 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3590 | JumpTarget smi_label; |
| 3591 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3592 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3593 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3594 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3595 | frame_->EmitPush(r0); |
| 3596 | Result arg_count = allocator_->Allocate(r0); |
| 3597 | ASSERT(arg_count.is_valid()); |
| 3598 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3599 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3600 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3601 | continue_label.Jump(); |
| 3602 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3603 | __ mvn(r0, Operand(r0)); |
| 3604 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3605 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3606 | break; |
| 3607 | } |
| 3608 | |
| 3609 | case Token::VOID: |
| 3610 | // since the stack top is cached in r0, popping and then |
| 3611 | // pushing a value can be done by just writing to r0. |
| 3612 | __ mov(r0, Operand(Factory::undefined_value())); |
| 3613 | break; |
| 3614 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3615 | case Token::ADD: { |
| 3616 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3617 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3618 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3619 | continue_label.Branch(eq); |
| 3620 | frame_->EmitPush(r0); |
| 3621 | Result arg_count = allocator_->Allocate(r0); |
| 3622 | ASSERT(arg_count.is_valid()); |
| 3623 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3624 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3625 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3626 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3627 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3628 | default: |
| 3629 | UNREACHABLE(); |
| 3630 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3631 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3632 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3633 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3634 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3638 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3639 | #ifdef DEBUG |
| 3640 | int original_height = frame_->height(); |
| 3641 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3642 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3643 | Comment cmnt(masm_, "[ CountOperation"); |
| 3644 | |
| 3645 | bool is_postfix = node->is_postfix(); |
| 3646 | bool is_increment = node->op() == Token::INC; |
| 3647 | |
| 3648 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3649 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3650 | |
| 3651 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3652 | if (is_postfix) { |
| 3653 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3654 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3655 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3656 | |
| 3657 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3658 | if (target.is_illegal()) { |
| 3659 | // Spoof the virtual frame to have the expected height (one higher |
| 3660 | // than on entry). |
| 3661 | if (!is_postfix) { |
| 3662 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3663 | frame_->EmitPush(r0); |
| 3664 | } |
| 3665 | ASSERT(frame_->height() == original_height + 1); |
| 3666 | return; |
| 3667 | } |
| 3668 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 3669 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3670 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3671 | JumpTarget slow; |
| 3672 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3673 | |
| 3674 | // Load the value (1) into register r1. |
| 3675 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3676 | |
| 3677 | // Check for smi operand. |
| 3678 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3679 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3680 | |
| 3681 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3682 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3683 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3684 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3685 | |
| 3686 | // Perform optimistic increment/decrement. |
| 3687 | if (is_increment) { |
| 3688 | __ add(r0, r0, Operand(r1), SetCC); |
| 3689 | } else { |
| 3690 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3691 | } |
| 3692 | |
| 3693 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3694 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3695 | |
| 3696 | // Revert optimistic increment/decrement. |
| 3697 | if (is_increment) { |
| 3698 | __ sub(r0, r0, Operand(r1)); |
| 3699 | } else { |
| 3700 | __ add(r0, r0, Operand(r1)); |
| 3701 | } |
| 3702 | |
| 3703 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3704 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3705 | { |
| 3706 | // Convert the operand to a number. |
| 3707 | frame_->EmitPush(r0); |
| 3708 | Result arg_count = allocator_->Allocate(r0); |
| 3709 | ASSERT(arg_count.is_valid()); |
| 3710 | __ mov(arg_count.reg(), Operand(0)); |
| 3711 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3712 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3713 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3714 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3715 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3718 | // Compute the new value. |
| 3719 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3720 | frame_->EmitPush(r0); |
| 3721 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3722 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3723 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3724 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3725 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3729 | exit.Bind(); |
| 3730 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3731 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3735 | if (is_postfix) frame_->EmitPop(r0); |
| 3736 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3740 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3741 | #ifdef DEBUG |
| 3742 | int original_height = frame_->height(); |
| 3743 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3744 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3745 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3746 | Token::Value op = node->op(); |
| 3747 | |
| 3748 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3749 | // operators must yield the result of one of the two expressions |
| 3750 | // before any ToBoolean() conversions. This means that the value |
| 3751 | // produced by a && or || operator is not necessarily a boolean. |
| 3752 | |
| 3753 | // NOTE: If the left hand side produces a materialized value (not in |
| 3754 | // the CC register), we force the right hand side to do the |
| 3755 | // same. This is necessary because we may have to branch to the exit |
| 3756 | // after evaluating the left hand side (due to the shortcut |
| 3757 | // semantics), but the compiler must (statically) know if the result |
| 3758 | // of compiling the binary operation is materialized or not. |
| 3759 | |
| 3760 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3761 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3762 | LoadConditionAndSpill(node->left(), |
| 3763 | NOT_INSIDE_TYPEOF, |
| 3764 | &is_true, |
| 3765 | false_target(), |
| 3766 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3767 | if (has_cc()) { |
| 3768 | Branch(false, false_target()); |
| 3769 | |
| 3770 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3771 | is_true.Bind(); |
| 3772 | LoadConditionAndSpill(node->right(), |
| 3773 | NOT_INSIDE_TYPEOF, |
| 3774 | true_target(), |
| 3775 | false_target(), |
| 3776 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3777 | |
| 3778 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3779 | JumpTarget pop_and_continue; |
| 3780 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3781 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3782 | __ ldr(r0, frame_->Top()); // dup the stack top |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3783 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3784 | // Avoid popping the result if it converts to 'false' using the |
| 3785 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3786 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3787 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3788 | Branch(false, &exit); |
| 3789 | |
| 3790 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3791 | pop_and_continue.Bind(); |
| 3792 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3793 | |
| 3794 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3795 | is_true.Bind(); |
| 3796 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3797 | |
| 3798 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3799 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3803 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3804 | LoadConditionAndSpill(node->left(), |
| 3805 | NOT_INSIDE_TYPEOF, |
| 3806 | true_target(), |
| 3807 | &is_false, |
| 3808 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3809 | if (has_cc()) { |
| 3810 | Branch(true, true_target()); |
| 3811 | |
| 3812 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3813 | is_false.Bind(); |
| 3814 | LoadConditionAndSpill(node->right(), |
| 3815 | NOT_INSIDE_TYPEOF, |
| 3816 | true_target(), |
| 3817 | false_target(), |
| 3818 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3819 | |
| 3820 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3821 | JumpTarget pop_and_continue; |
| 3822 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3823 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3824 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3825 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3826 | // Avoid popping the result if it converts to 'true' using the |
| 3827 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3828 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3829 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3830 | Branch(true, &exit); |
| 3831 | |
| 3832 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3833 | pop_and_continue.Bind(); |
| 3834 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3835 | |
| 3836 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3837 | is_false.Bind(); |
| 3838 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3839 | |
| 3840 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3841 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | } else { |
| 3845 | // Optimize for the case where (at least) one of the expressions |
| 3846 | // is a literal small integer. |
| 3847 | Literal* lliteral = node->left()->AsLiteral(); |
| 3848 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3849 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 3850 | // never return a constant/immutable object. |
| 3851 | bool overwrite_left = |
| 3852 | (node->left()->AsBinaryOperation() != NULL && |
| 3853 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3854 | bool overwrite_right = |
| 3855 | (node->right()->AsBinaryOperation() != NULL && |
| 3856 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3857 | |
| 3858 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3859 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3860 | SmiOperation(node->op(), |
| 3861 | rliteral->handle(), |
| 3862 | false, |
| 3863 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3864 | |
| 3865 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3866 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3867 | SmiOperation(node->op(), |
| 3868 | lliteral->handle(), |
| 3869 | true, |
| 3870 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3871 | |
| 3872 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3873 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 3874 | if (overwrite_left) { |
| 3875 | overwrite_mode = OVERWRITE_LEFT; |
| 3876 | } else if (overwrite_right) { |
| 3877 | overwrite_mode = OVERWRITE_RIGHT; |
| 3878 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3879 | LoadAndSpill(node->left()); |
| 3880 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3881 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3882 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3883 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3884 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3885 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3886 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3890 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3891 | #ifdef DEBUG |
| 3892 | int original_height = frame_->height(); |
| 3893 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3894 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3895 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3896 | frame_->EmitPush(r0); |
| 3897 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3901 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3902 | #ifdef DEBUG |
| 3903 | int original_height = frame_->height(); |
| 3904 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3905 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3906 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3907 | |
| 3908 | // Get the expressions from the node. |
| 3909 | Expression* left = node->left(); |
| 3910 | Expression* right = node->right(); |
| 3911 | Token::Value op = node->op(); |
| 3912 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3913 | // To make null checks efficient, we check if either left or right is the |
| 3914 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3915 | // instead of calling the (very) general runtime routine for checking |
| 3916 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3917 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3918 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3919 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3920 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3921 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 3922 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3923 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3924 | LoadAndSpill(left_is_null ? right : left); |
| 3925 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3926 | __ cmp(r0, Operand(Factory::null_value())); |
| 3927 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3928 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 3929 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3930 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3931 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3932 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3933 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3934 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3935 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3936 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3937 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3938 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3939 | // It can be an undetectable object. |
| 3940 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3941 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 3942 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 3943 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3946 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3947 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3948 | return; |
| 3949 | } |
| 3950 | } |
| 3951 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3952 | // To make typeof testing for natives implemented in JavaScript really |
| 3953 | // efficient, we generate special code for expressions of the form: |
| 3954 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3955 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 3956 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 3957 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 3958 | (right->AsLiteral() != NULL && |
| 3959 | right->AsLiteral()->handle()->IsString())) { |
| 3960 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 3961 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3962 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3963 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3964 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3965 | |
| 3966 | if (check->Equals(Heap::number_symbol())) { |
| 3967 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3968 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3969 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3970 | __ cmp(r1, Operand(Factory::heap_number_map())); |
| 3971 | cc_reg_ = eq; |
| 3972 | |
| 3973 | } else if (check->Equals(Heap::string_symbol())) { |
| 3974 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3975 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3976 | |
| 3977 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3978 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3979 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3980 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3981 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3982 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3983 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3984 | |
| 3985 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3986 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 3987 | cc_reg_ = lt; |
| 3988 | |
| 3989 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 3990 | __ cmp(r1, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3991 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3992 | __ cmp(r1, Operand(Factory::false_value())); |
| 3993 | cc_reg_ = eq; |
| 3994 | |
| 3995 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 3996 | __ cmp(r1, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3997 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3998 | |
| 3999 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4000 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4001 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4002 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4003 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4004 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4005 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4006 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4007 | |
| 4008 | cc_reg_ = eq; |
| 4009 | |
| 4010 | } else if (check->Equals(Heap::function_symbol())) { |
| 4011 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4012 | false_target()->Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4013 | __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4014 | cc_reg_ = eq; |
| 4015 | |
| 4016 | } else if (check->Equals(Heap::object_symbol())) { |
| 4017 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4018 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4019 | |
| 4020 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4021 | __ cmp(r1, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4022 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4023 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4024 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4025 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 4026 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4027 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4028 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4029 | |
| 4030 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4031 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4032 | false_target()->Branch(lt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4033 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 4034 | cc_reg_ = le; |
| 4035 | |
| 4036 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4037 | // Uncommon case: typeof testing against a string literal that is |
| 4038 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4039 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4040 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4041 | ASSERT(!has_valid_frame() || |
| 4042 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4043 | return; |
| 4044 | } |
| 4045 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4046 | LoadAndSpill(left); |
| 4047 | LoadAndSpill(right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4048 | switch (op) { |
| 4049 | case Token::EQ: |
| 4050 | Comparison(eq, false); |
| 4051 | break; |
| 4052 | |
| 4053 | case Token::LT: |
| 4054 | Comparison(lt); |
| 4055 | break; |
| 4056 | |
| 4057 | case Token::GT: |
| 4058 | Comparison(gt); |
| 4059 | break; |
| 4060 | |
| 4061 | case Token::LTE: |
| 4062 | Comparison(le); |
| 4063 | break; |
| 4064 | |
| 4065 | case Token::GTE: |
| 4066 | Comparison(ge); |
| 4067 | break; |
| 4068 | |
| 4069 | case Token::EQ_STRICT: |
| 4070 | Comparison(eq, true); |
| 4071 | break; |
| 4072 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4073 | case Token::IN: { |
| 4074 | Result arg_count = allocator_->Allocate(r0); |
| 4075 | ASSERT(arg_count.is_valid()); |
| 4076 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 4077 | Result result = frame_->InvokeBuiltin(Builtins::IN, |
| 4078 | CALL_JS, |
| 4079 | &arg_count, |
| 4080 | 2); |
| 4081 | frame_->EmitPush(result.reg()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4082 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4083 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4084 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4085 | case Token::INSTANCEOF: { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4086 | InstanceofStub stub; |
| 4087 | Result result = frame_->CallStub(&stub, 2); |
| 4088 | // At this point if instanceof succeeded then r0 == 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4089 | __ tst(result.reg(), Operand(result.reg())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4090 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4091 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4092 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4093 | |
| 4094 | default: |
| 4095 | UNREACHABLE(); |
| 4096 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4097 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4098 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4102 | #ifdef DEBUG |
| 4103 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4104 | #endif |
| 4105 | |
| 4106 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4107 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4108 | #define __ ACCESS_MASM(masm) |
| 4109 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4110 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4111 | Handle<String> Reference::GetName() { |
| 4112 | ASSERT(type_ == NAMED); |
| 4113 | Property* property = expression_->AsProperty(); |
| 4114 | if (property == NULL) { |
| 4115 | // Global variable reference treated as a named property reference. |
| 4116 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4117 | ASSERT(proxy->AsVariable() != NULL); |
| 4118 | ASSERT(proxy->AsVariable()->is_global()); |
| 4119 | return proxy->name(); |
| 4120 | } else { |
| 4121 | Literal* raw_name = property->key()->AsLiteral(); |
| 4122 | ASSERT(raw_name != NULL); |
| 4123 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4124 | } |
| 4125 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4126 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4127 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4128 | void Reference::GetValueAndSpill(TypeofState typeof_state) { |
| 4129 | ASSERT(cgen_->in_spilled_code()); |
| 4130 | cgen_->set_in_spilled_code(false); |
| 4131 | GetValue(typeof_state); |
| 4132 | cgen_->frame()->SpillAll(); |
| 4133 | cgen_->set_in_spilled_code(true); |
| 4134 | } |
| 4135 | |
| 4136 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4137 | void Reference::GetValue(TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4138 | ASSERT(!cgen_->in_spilled_code()); |
| 4139 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4140 | ASSERT(!is_illegal()); |
| 4141 | ASSERT(!cgen_->has_cc()); |
| 4142 | MacroAssembler* masm = cgen_->masm(); |
| 4143 | Property* property = expression_->AsProperty(); |
| 4144 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4145 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
| 4148 | switch (type_) { |
| 4149 | case SLOT: { |
| 4150 | Comment cmnt(masm, "[ Load from Slot"); |
| 4151 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4152 | ASSERT(slot != NULL); |
| 4153 | cgen_->LoadFromSlot(slot, typeof_state); |
| 4154 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4157 | case NAMED: { |
| 4158 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4159 | // distinction between expressions in a typeof and not in a typeof. If |
| 4160 | // there is a chance that reference errors can be thrown below, we |
| 4161 | // must distinguish between the two kinds of loads (typeof expression |
| 4162 | // loads must not throw a reference error). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4163 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4164 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4165 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4166 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4167 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4168 | // Setup the name register. |
| 4169 | Result name_reg = cgen_->allocator()->Allocate(r2); |
| 4170 | ASSERT(name_reg.is_valid()); |
| 4171 | __ mov(name_reg.reg(), Operand(name)); |
| 4172 | ASSERT(var == NULL || var->is_global()); |
| 4173 | RelocInfo::Mode rmode = (var == NULL) |
| 4174 | ? RelocInfo::CODE_TARGET |
| 4175 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4176 | Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4177 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4178 | break; |
| 4179 | } |
| 4180 | |
| 4181 | case KEYED: { |
| 4182 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4183 | // distinction between expressions in a typeof and not in a typeof. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4184 | |
| 4185 | // TODO(181): Implement inlined version of array indexing once |
| 4186 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4187 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4188 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4189 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4190 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4191 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4192 | ASSERT(var == NULL || var->is_global()); |
| 4193 | RelocInfo::Mode rmode = (var == NULL) |
| 4194 | ? RelocInfo::CODE_TARGET |
| 4195 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4196 | Result answer = frame->CallCodeObject(ic, rmode, 0); |
| 4197 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4198 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | default: |
| 4202 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4203 | } |
| 4204 | } |
| 4205 | |
| 4206 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4207 | void Reference::SetValue(InitState init_state) { |
| 4208 | ASSERT(!is_illegal()); |
| 4209 | ASSERT(!cgen_->has_cc()); |
| 4210 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4211 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4212 | Property* property = expression_->AsProperty(); |
| 4213 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4214 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4215 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4216 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4217 | switch (type_) { |
| 4218 | case SLOT: { |
| 4219 | Comment cmnt(masm, "[ Store to Slot"); |
| 4220 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4221 | ASSERT(slot != NULL); |
| 4222 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4223 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4224 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4225 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4226 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4227 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4228 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4229 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4230 | if (init_state == CONST_INIT) { |
| 4231 | // Same as the case for a normal store, but ignores attribute |
| 4232 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4233 | // const properties (introduced via eval("const foo = (some |
| 4234 | // expr);")). Also, uses the current function context instead of |
| 4235 | // the top context. |
| 4236 | // |
| 4237 | // Note that we must declare the foo upon entry of eval(), via a |
| 4238 | // context slot declaration, but we cannot initialize it at the |
| 4239 | // same time, because the const declaration may be at the end of |
| 4240 | // the eval code (sigh...) and the const variable may have been |
| 4241 | // used before (where its value is 'undefined'). Thus, we can only |
| 4242 | // do the initialization when we actually encounter the expression |
| 4243 | // and when the expression operands are defined and valid, and |
| 4244 | // thus we need the split into 2 operations: declaration of the |
| 4245 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4246 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4247 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4248 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4249 | } |
| 4250 | // Storing a variable must keep the (new) value on the expression |
| 4251 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4252 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4253 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4254 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4255 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4256 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4257 | JumpTarget exit; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4258 | if (init_state == CONST_INIT) { |
| 4259 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4260 | // Only the first const initialization must be executed (the slot |
| 4261 | // still contains 'the hole' value). When the assignment is |
| 4262 | // executed, the code is identical to a normal store (see below). |
| 4263 | Comment cmnt(masm, "[ Init const"); |
| 4264 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
| 4265 | __ cmp(r2, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4266 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4267 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4268 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4269 | // We must execute the store. Storing a variable must keep the |
| 4270 | // (new) value on the stack. This is necessary for compiling |
| 4271 | // assignment expressions. |
| 4272 | // |
| 4273 | // Note: We will reach here even with slot->var()->mode() == |
| 4274 | // Variable::CONST because of const declarations which will |
| 4275 | // initialize consts to 'the hole' value and by doing so, end up |
| 4276 | // calling this code. r2 may be loaded with context; used below in |
| 4277 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4278 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4279 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4280 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4281 | if (slot->type() == Slot::CONTEXT) { |
| 4282 | // Skip write barrier if the written value is a smi. |
| 4283 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4284 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4285 | // r2 is loaded with context when calling SlotOperand above. |
| 4286 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4287 | __ mov(r3, Operand(offset)); |
| 4288 | __ RecordWrite(r2, r3, r1); |
| 4289 | } |
| 4290 | // If we definitely did not jump over the assignment, we do not need |
| 4291 | // to bind the exit label. Doing so can defeat peephole |
| 4292 | // optimization. |
| 4293 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4294 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4295 | } |
| 4296 | } |
| 4297 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4300 | case NAMED: { |
| 4301 | Comment cmnt(masm, "[ Store to named Property"); |
| 4302 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4303 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4304 | Handle<String> name(GetName()); |
| 4305 | |
| 4306 | Result value = cgen_->allocator()->Allocate(r0); |
| 4307 | ASSERT(value.is_valid()); |
| 4308 | frame->EmitPop(value.reg()); |
| 4309 | |
| 4310 | // Setup the name register. |
| 4311 | Result property_name = cgen_->allocator()->Allocate(r2); |
| 4312 | ASSERT(property_name.is_valid()); |
| 4313 | __ mov(property_name.reg(), Operand(name)); |
| 4314 | Result answer = frame->CallCodeObject(ic, |
| 4315 | RelocInfo::CODE_TARGET, |
| 4316 | &value, |
| 4317 | &property_name, |
| 4318 | 0); |
| 4319 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4320 | break; |
| 4321 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4322 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4323 | case KEYED: { |
| 4324 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4325 | Property* property = expression_->AsProperty(); |
| 4326 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4327 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4328 | |
| 4329 | // Call IC code. |
| 4330 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4331 | // TODO(1222589): Make the IC grab the values from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4332 | Result value = cgen_->allocator()->Allocate(r0); |
| 4333 | ASSERT(value.is_valid()); |
| 4334 | frame->EmitPop(value.reg()); // value |
| 4335 | Result result = |
| 4336 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4337 | frame->EmitPush(result.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4338 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4339 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4340 | |
| 4341 | default: |
| 4342 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4343 | } |
| 4344 | } |
| 4345 | |
| 4346 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4347 | // Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz |
| 4348 | // instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0 |
| 4349 | // (31 instead of 32). |
| 4350 | static void CountLeadingZeros( |
| 4351 | MacroAssembler* masm, |
| 4352 | Register source, |
| 4353 | Register scratch, |
| 4354 | Register zeros) { |
| 4355 | #ifdef __ARM_ARCH_5__ |
| 4356 | __ clz(zeros, source); // This instruction is only supported after ARM5. |
| 4357 | #else |
| 4358 | __ mov(zeros, Operand(0)); |
| 4359 | __ mov(scratch, source); |
| 4360 | // Top 16. |
| 4361 | __ tst(scratch, Operand(0xffff0000)); |
| 4362 | __ add(zeros, zeros, Operand(16), LeaveCC, eq); |
| 4363 | __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq); |
| 4364 | // Top 8. |
| 4365 | __ tst(scratch, Operand(0xff000000)); |
| 4366 | __ add(zeros, zeros, Operand(8), LeaveCC, eq); |
| 4367 | __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq); |
| 4368 | // Top 4. |
| 4369 | __ tst(scratch, Operand(0xf0000000)); |
| 4370 | __ add(zeros, zeros, Operand(4), LeaveCC, eq); |
| 4371 | __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq); |
| 4372 | // Top 2. |
| 4373 | __ tst(scratch, Operand(0xc0000000)); |
| 4374 | __ add(zeros, zeros, Operand(2), LeaveCC, eq); |
| 4375 | __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq); |
| 4376 | // Top bit. |
| 4377 | __ tst(scratch, Operand(0x80000000)); |
| 4378 | __ add(zeros, zeros, Operand(1), LeaveCC, eq); |
| 4379 | #endif |
| 4380 | } |
| 4381 | |
| 4382 | |
| 4383 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 4384 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 4385 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 4386 | // scratch register. Destroys the source register. No GC occurs during this |
| 4387 | // stub so you don't have to set up the frame. |
| 4388 | class ConvertToDoubleStub : public CodeStub { |
| 4389 | public: |
| 4390 | ConvertToDoubleStub(Register result_reg_1, |
| 4391 | Register result_reg_2, |
| 4392 | Register source_reg, |
| 4393 | Register scratch_reg) |
| 4394 | : result1_(result_reg_1), |
| 4395 | result2_(result_reg_2), |
| 4396 | source_(source_reg), |
| 4397 | zeros_(scratch_reg) { } |
| 4398 | |
| 4399 | private: |
| 4400 | Register result1_; |
| 4401 | Register result2_; |
| 4402 | Register source_; |
| 4403 | Register zeros_; |
| 4404 | |
| 4405 | // Minor key encoding in 16 bits. |
| 4406 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4407 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4408 | |
| 4409 | Major MajorKey() { return ConvertToDouble; } |
| 4410 | int MinorKey() { |
| 4411 | // Encode the parameters in a unique 16 bit value. |
| 4412 | return result1_.code() + |
| 4413 | (result2_.code() << 4) + |
| 4414 | (source_.code() << 8) + |
| 4415 | (zeros_.code() << 12); |
| 4416 | } |
| 4417 | |
| 4418 | void Generate(MacroAssembler* masm); |
| 4419 | |
| 4420 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 4421 | |
| 4422 | #ifdef DEBUG |
| 4423 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 4424 | #endif |
| 4425 | }; |
| 4426 | |
| 4427 | |
| 4428 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 4429 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 4430 | Register exponent = result1_; |
| 4431 | Register mantissa = result2_; |
| 4432 | #else |
| 4433 | Register exponent = result2_; |
| 4434 | Register mantissa = result1_; |
| 4435 | #endif |
| 4436 | Label not_special; |
| 4437 | // Convert from Smi to integer. |
| 4438 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 4439 | // Move sign bit from source to destination. This works because the sign bit |
| 4440 | // in the exponent word of the double has the same position and polarity as |
| 4441 | // the 2's complement sign bit in a Smi. |
| 4442 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4443 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 4444 | // Subtract from 0 if source was negative. |
| 4445 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
| 4446 | __ cmp(source_, Operand(1)); |
| 4447 | __ b(gt, ¬_special); |
| 4448 | |
| 4449 | // We have -1, 0 or 1, which we treat specially. |
| 4450 | __ cmp(source_, Operand(0)); |
| 4451 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 4452 | static const uint32_t exponent_word_for_1 = |
| 4453 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
| 4454 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne); |
| 4455 | // 1, 0 and -1 all have 0 for the second word. |
| 4456 | __ mov(mantissa, Operand(0)); |
| 4457 | __ Ret(); |
| 4458 | |
| 4459 | __ bind(¬_special); |
| 4460 | // Count leading zeros. Uses result2 for a scratch register on pre-ARM5. |
| 4461 | // Gets the wrong answer for 0, but we already checked for that case above. |
| 4462 | CountLeadingZeros(masm, source_, mantissa, zeros_); |
| 4463 | // Compute exponent and or it into the exponent register. |
| 4464 | // We use result2 as a scratch register here. |
| 4465 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias)); |
| 4466 | __ orr(exponent, |
| 4467 | exponent, |
| 4468 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 4469 | // Shift up the source chopping the top bit off. |
| 4470 | __ add(zeros_, zeros_, Operand(1)); |
| 4471 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 4472 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 4473 | // Compute lower part of fraction (last 12 bits). |
| 4474 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 4475 | // And the top (top 20 bits). |
| 4476 | __ orr(exponent, |
| 4477 | exponent, |
| 4478 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 4479 | __ Ret(); |
| 4480 | } |
| 4481 | |
| 4482 | |
| 4483 | // This stub can convert a signed int32 to a heap number (double). It does |
| 4484 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 4485 | // so you don't have to set up the frame. |
| 4486 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 4487 | public: |
| 4488 | WriteInt32ToHeapNumberStub(Register the_int, |
| 4489 | Register the_heap_number, |
| 4490 | Register scratch) |
| 4491 | : the_int_(the_int), |
| 4492 | the_heap_number_(the_heap_number), |
| 4493 | scratch_(scratch) { } |
| 4494 | |
| 4495 | private: |
| 4496 | Register the_int_; |
| 4497 | Register the_heap_number_; |
| 4498 | Register scratch_; |
| 4499 | |
| 4500 | // Minor key encoding in 16 bits. |
| 4501 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 4502 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 4503 | |
| 4504 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 4505 | int MinorKey() { |
| 4506 | // Encode the parameters in a unique 16 bit value. |
| 4507 | return the_int_.code() + |
| 4508 | (the_heap_number_.code() << 4) + |
| 4509 | (scratch_.code() << 8); |
| 4510 | } |
| 4511 | |
| 4512 | void Generate(MacroAssembler* masm); |
| 4513 | |
| 4514 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 4515 | |
| 4516 | #ifdef DEBUG |
| 4517 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 4518 | #endif |
| 4519 | }; |
| 4520 | |
| 4521 | |
| 4522 | // See comment for class. |
| 4523 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) { |
| 4524 | Label max_negative_int; |
| 4525 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 4526 | // We test for the special value that has a different exponent. This test |
| 4527 | // has the neat side effect of setting the flags according to the sign. |
| 4528 | ASSERT(HeapNumber::kSignMask == 0x80000000u); |
| 4529 | __ cmp(the_int_, Operand(0x80000000)); |
| 4530 | __ b(eq, &max_negative_int); |
| 4531 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 4532 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4533 | uint32_t non_smi_exponent = |
| 4534 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4535 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 4536 | // Set the sign bit in scratch_ if the value was negative. |
| 4537 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 4538 | // Subtract from 0 if the value was negative. |
| 4539 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 4540 | // We should be masking the implict first digit of the mantissa away here, |
| 4541 | // but it just ends up combining harmlessly with the last digit of the |
| 4542 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 4543 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 4544 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 4545 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4546 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 4547 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4548 | HeapNumber::kExponentOffset)); |
| 4549 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 4550 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 4551 | HeapNumber::kMantissaOffset)); |
| 4552 | __ Ret(); |
| 4553 | |
| 4554 | __ bind(&max_negative_int); |
| 4555 | // The max negative int32 is stored as a positive number in the mantissa of |
| 4556 | // a double because it uses a sign bit instead of using two's complement. |
| 4557 | // The actual mantissa bits stored are all 0 because the implicit most |
| 4558 | // significant 1 bit is not stored. |
| 4559 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 4560 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 4561 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 4562 | __ mov(ip, Operand(0)); |
| 4563 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 4564 | __ Ret(); |
| 4565 | } |
| 4566 | |
| 4567 | |
| 4568 | // Allocates a heap number or jumps to the label if the young space is full and |
| 4569 | // a scavenge is needed. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4570 | static void AllocateHeapNumber( |
| 4571 | MacroAssembler* masm, |
| 4572 | Label* need_gc, // Jump here if young space is full. |
| 4573 | Register result_reg, // The tagged address of the new heap number. |
| 4574 | Register allocation_top_addr_reg, // A scratch register. |
| 4575 | Register scratch2) { // Another scratch register. |
| 4576 | ExternalReference allocation_top = |
| 4577 | ExternalReference::new_space_allocation_top_address(); |
| 4578 | ExternalReference allocation_limit = |
| 4579 | ExternalReference::new_space_allocation_limit_address(); |
| 4580 | |
| 4581 | // allocat := the address of the allocation top variable. |
| 4582 | __ mov(allocation_top_addr_reg, Operand(allocation_top)); |
| 4583 | // result_reg := the old allocation top. |
| 4584 | __ ldr(result_reg, MemOperand(allocation_top_addr_reg)); |
| 4585 | // scratch2 := the address of the allocation limit. |
| 4586 | __ mov(scratch2, Operand(allocation_limit)); |
| 4587 | // scratch2 := the allocation limit. |
| 4588 | __ ldr(scratch2, MemOperand(scratch2)); |
| 4589 | // result_reg := the new allocation top. |
| 4590 | __ add(result_reg, result_reg, Operand(HeapNumber::kSize)); |
| 4591 | // Compare new new allocation top and limit. |
| 4592 | __ cmp(result_reg, Operand(scratch2)); |
| 4593 | // Branch if out of space in young generation. |
| 4594 | __ b(hi, need_gc); |
| 4595 | // Store new allocation top. |
| 4596 | __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top |
| 4597 | // Tag and adjust back to start of new object. |
| 4598 | __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag)); |
| 4599 | // Get heap number map into scratch2. |
| 4600 | __ mov(scratch2, Operand(Factory::heap_number_map())); |
| 4601 | // Store heap number map in new object. |
| 4602 | __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset)); |
| 4603 | } |
| 4604 | |
| 4605 | |
| 4606 | // We fall into this code if the operands were Smis, but the result was |
| 4607 | // 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^] | 4608 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 4609 | // to call the C-implemented binary fp operation routines we need to end up |
| 4610 | // with the double precision floating point operands in r0 and r1 (for the |
| 4611 | // 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] | 4612 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 4613 | Label* not_smi, |
| 4614 | const Builtins::JavaScript& builtin, |
| 4615 | Token::Value operation, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4616 | OverwriteMode mode) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4617 | Label slow, slow_pop_2_first, do_the_call; |
| 4618 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 4619 | // Smi-smi case (overflow). |
| 4620 | // Since both are Smis there is no heap number to overwrite, so allocate. |
| 4621 | // The new heap number is in r5. r6 and r7 are scratch. |
| 4622 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4623 | // Write Smi from r0 to r3 and r2 in double format. r6 is scratch. |
| 4624 | ConvertToDoubleStub stub1(r3, r2, r0, r6); |
| 4625 | __ push(lr); |
| 4626 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
| 4627 | // Write Smi from r1 to r1 and r0 in double format. r6 is scratch. |
| 4628 | __ mov(r7, Operand(r1)); |
| 4629 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 4630 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 4631 | __ pop(lr); |
| 4632 | __ jmp(&do_the_call); // Tail call. No return. |
| 4633 | |
| 4634 | // We jump to here if something goes wrong (one param is not a number of any |
| 4635 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4636 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4637 | __ push(r1); |
| 4638 | __ push(r0); |
| 4639 | __ mov(r0, Operand(1)); // Set number of arguments. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4640 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4641 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4642 | // 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] | 4643 | __ bind(not_smi); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4644 | if (mode == NO_OVERWRITE) { |
| 4645 | // In the case where there is no chance of an overwritable float we may as |
| 4646 | // well do the allocation immediately while r0 and r1 are untouched. |
| 4647 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4648 | } |
| 4649 | |
| 4650 | // Move r0 to a double in r2-r3. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4651 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4652 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 4653 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4654 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4655 | if (mode == OVERWRITE_RIGHT) { |
| 4656 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
| 4657 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4658 | // Calling convention says that second double is in r2 and r3. |
| 4659 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4660 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4)); |
| 4661 | __ jmp(&finished_loading_r0); |
| 4662 | __ bind(&r0_is_smi); |
| 4663 | if (mode == OVERWRITE_RIGHT) { |
| 4664 | // 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] | 4665 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4666 | } |
| 4667 | // Write Smi from r0 to r3 and r2 in double format. |
| 4668 | __ mov(r7, Operand(r0)); |
| 4669 | ConvertToDoubleStub stub3(r3, r2, r7, r6); |
| 4670 | __ push(lr); |
| 4671 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 4672 | __ pop(lr); |
| 4673 | __ bind(&finished_loading_r0); |
| 4674 | |
| 4675 | // Move r1 to a double in r0-r1. |
| 4676 | __ tst(r1, Operand(kSmiTagMask)); |
| 4677 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 4678 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4679 | __ b(ne, &slow); |
| 4680 | if (mode == OVERWRITE_LEFT) { |
| 4681 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4682 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4683 | // Calling convention says that first double is in r0 and r1. |
| 4684 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4685 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4)); |
| 4686 | __ jmp(&finished_loading_r1); |
| 4687 | __ bind(&r1_is_smi); |
| 4688 | if (mode == OVERWRITE_LEFT) { |
| 4689 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 4690 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4691 | } |
| 4692 | // Write Smi from r1 to r1 and r0 in double format. |
| 4693 | __ mov(r7, Operand(r1)); |
| 4694 | ConvertToDoubleStub stub4(r1, r0, r7, r6); |
| 4695 | __ push(lr); |
| 4696 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 4697 | __ pop(lr); |
| 4698 | __ bind(&finished_loading_r1); |
| 4699 | |
| 4700 | __ bind(&do_the_call); |
| 4701 | // r0: Left value (least significant part of mantissa). |
| 4702 | // r1: Left value (sign, exponent, top of mantissa). |
| 4703 | // r2: Right value (least significant part of mantissa). |
| 4704 | // r3: Right value (sign, exponent, top of mantissa). |
| 4705 | // r5: Address of heap number for result. |
| 4706 | __ push(lr); // For later. |
| 4707 | __ push(r5); // Address of heap number that is answer. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4708 | // Call C routine that may not cause GC or other trouble. |
| 4709 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4710 | __ Call(r5); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4711 | // Store answer in the overwritable heap number. |
| 4712 | __ pop(r4); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4713 | #if !defined(USE_ARM_EABI) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4714 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 4715 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 4716 | // substract the tag from r4. |
| 4717 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 4718 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 4719 | #else |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4720 | // Double returned in registers 0 and 1. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4721 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4722 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4723 | #endif |
| 4724 | __ mov(r0, Operand(r4)); |
| 4725 | // And we are done. |
| 4726 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4730 | // Tries to get a signed int32 out of a double precision floating point heap |
| 4731 | // number. Rounds towards 0. Only succeeds for doubles that are in the ranges |
| 4732 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 4733 | // almost to the range of signed int32 values that are not Smis. Jumps to the |
| 4734 | // label if the double isn't in the range it can cope with. |
| 4735 | static void GetInt32(MacroAssembler* masm, |
| 4736 | Register source, |
| 4737 | Register dest, |
| 4738 | Register scratch, |
| 4739 | Label* slow) { |
| 4740 | Register scratch2 = dest; |
| 4741 | // Get exponent word. |
| 4742 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 4743 | // Get exponent alone in scratch2. |
| 4744 | __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask)); |
| 4745 | // Check whether the exponent matches a 32 bit signed int that is not a Smi. |
| 4746 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 4747 | const uint32_t non_smi_exponent = |
| 4748 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 4749 | __ cmp(scratch2, Operand(non_smi_exponent)); |
| 4750 | // If not, then we go slow. |
| 4751 | __ b(ne, slow); |
| 4752 | // Get the top bits of the mantissa. |
| 4753 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 4754 | // Put back the implicit 1. |
| 4755 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 4756 | // Shift up the mantissa bits to take up the space the exponent used to take. |
| 4757 | // We just orred in the implicit bit so that took care of one and we want to |
| 4758 | // leave the sign bit 0 so we subtract 2 bits from the shift distance. |
| 4759 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 4760 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 4761 | // Put sign in zero flag. |
| 4762 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 4763 | // Get the second half of the double. |
| 4764 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 4765 | // Shift down 22 bits to get the last 10 bits. |
| 4766 | __ orr(dest, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 4767 | // Fix sign if sign bit was set. |
| 4768 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 4769 | } |
| 4770 | |
| 4771 | |
| 4772 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 4773 | // whether both inputs are either Smis or at least heap numbers that can be |
| 4774 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 4775 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 4776 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 4777 | // write the answer into (either by allocating or by overwriting). |
| 4778 | // On entry the operands are in r0 and r1. On exit the answer is in r0. |
| 4779 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) { |
| 4780 | Label slow, result_not_a_smi; |
| 4781 | Label r0_is_smi, r1_is_smi; |
| 4782 | Label done_checking_r0, done_checking_r1; |
| 4783 | |
| 4784 | __ tst(r1, Operand(kSmiTagMask)); |
| 4785 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 4786 | __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE); |
| 4787 | __ b(ne, &slow); |
| 4788 | GetInt32(masm, r1, r3, r4, &slow); |
| 4789 | __ jmp(&done_checking_r1); |
| 4790 | __ bind(&r1_is_smi); |
| 4791 | __ mov(r3, Operand(r1, ASR, 1)); |
| 4792 | __ bind(&done_checking_r1); |
| 4793 | |
| 4794 | __ tst(r0, Operand(kSmiTagMask)); |
| 4795 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 4796 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 4797 | __ b(ne, &slow); |
| 4798 | GetInt32(masm, r0, r2, r4, &slow); |
| 4799 | __ jmp(&done_checking_r0); |
| 4800 | __ bind(&r0_is_smi); |
| 4801 | __ mov(r2, Operand(r0, ASR, 1)); |
| 4802 | __ bind(&done_checking_r0); |
| 4803 | |
| 4804 | // r0 and r1: Original operands (Smi or heap numbers). |
| 4805 | // r2 and r3: Signed int32 operands. |
| 4806 | switch (op_) { |
| 4807 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 4808 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 4809 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 4810 | case Token::SAR: |
| 4811 | // Use only the 5 least significant bits of the shift count. |
| 4812 | __ and_(r2, r2, Operand(0x1f)); |
| 4813 | __ mov(r2, Operand(r3, ASR, r2)); |
| 4814 | break; |
| 4815 | case Token::SHR: |
| 4816 | // Use only the 5 least significant bits of the shift count. |
| 4817 | __ and_(r2, r2, Operand(0x1f)); |
| 4818 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 4819 | // SHR is special because it is required to produce a positive answer. |
| 4820 | // The code below for writing into heap numbers isn't capable of writing |
| 4821 | // the register as an unsigned int so we go to slow case if we hit this |
| 4822 | // case. |
| 4823 | __ b(mi, &slow); |
| 4824 | break; |
| 4825 | case Token::SHL: |
| 4826 | // Use only the 5 least significant bits of the shift count. |
| 4827 | __ and_(r2, r2, Operand(0x1f)); |
| 4828 | __ mov(r2, Operand(r3, LSL, r2)); |
| 4829 | break; |
| 4830 | default: UNREACHABLE(); |
| 4831 | } |
| 4832 | // check that the *signed* result fits in a smi |
| 4833 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 4834 | __ b(mi, &result_not_a_smi); |
| 4835 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 4836 | __ Ret(); |
| 4837 | |
| 4838 | Label have_to_allocate, got_a_heap_number; |
| 4839 | __ bind(&result_not_a_smi); |
| 4840 | switch (mode_) { |
| 4841 | case OVERWRITE_RIGHT: { |
| 4842 | __ tst(r0, Operand(kSmiTagMask)); |
| 4843 | __ b(eq, &have_to_allocate); |
| 4844 | __ mov(r5, Operand(r0)); |
| 4845 | break; |
| 4846 | } |
| 4847 | case OVERWRITE_LEFT: { |
| 4848 | __ tst(r1, Operand(kSmiTagMask)); |
| 4849 | __ b(eq, &have_to_allocate); |
| 4850 | __ mov(r5, Operand(r1)); |
| 4851 | break; |
| 4852 | } |
| 4853 | case NO_OVERWRITE: { |
| 4854 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 4855 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4856 | } |
| 4857 | default: break; |
| 4858 | } |
| 4859 | __ bind(&got_a_heap_number); |
| 4860 | // r2: Answer as signed int32. |
| 4861 | // r5: Heap number to write answer into. |
| 4862 | |
| 4863 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 4864 | // result. |
| 4865 | __ mov(r0, Operand(r5)); |
| 4866 | |
| 4867 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 4868 | // r3 as scratch. r0 is preserved and returned. |
| 4869 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 4870 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 4871 | |
| 4872 | if (mode_ != NO_OVERWRITE) { |
| 4873 | __ bind(&have_to_allocate); |
| 4874 | // Get a new heap number in r5. r6 and r7 are scratch. |
| 4875 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
| 4876 | __ jmp(&got_a_heap_number); |
| 4877 | } |
| 4878 | |
| 4879 | // If all else failed then we go to the runtime system. |
| 4880 | __ bind(&slow); |
| 4881 | __ push(r1); // restore stack |
| 4882 | __ push(r0); |
| 4883 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 4884 | switch (op_) { |
| 4885 | case Token::BIT_OR: |
| 4886 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 4887 | break; |
| 4888 | case Token::BIT_AND: |
| 4889 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 4890 | break; |
| 4891 | case Token::BIT_XOR: |
| 4892 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 4893 | break; |
| 4894 | case Token::SAR: |
| 4895 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 4896 | break; |
| 4897 | case Token::SHR: |
| 4898 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 4899 | break; |
| 4900 | case Token::SHL: |
| 4901 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 4902 | break; |
| 4903 | default: |
| 4904 | UNREACHABLE(); |
| 4905 | } |
| 4906 | } |
| 4907 | |
| 4908 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4909 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 4910 | // r1 : x |
| 4911 | // r0 : y |
| 4912 | // result : r0 |
| 4913 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4914 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 4915 | // tell us that. |
| 4916 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4917 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4918 | switch (op_) { |
| 4919 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4920 | Label not_smi; |
| 4921 | // Fast path. |
| 4922 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4923 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4924 | __ b(ne, ¬_smi); |
| 4925 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 4926 | // Return if no overflow. |
| 4927 | __ Ret(vc); |
| 4928 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 4929 | |
| 4930 | HandleBinaryOpSlowCases(masm, |
| 4931 | ¬_smi, |
| 4932 | Builtins::ADD, |
| 4933 | Token::ADD, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4934 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4935 | break; |
| 4936 | } |
| 4937 | |
| 4938 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4939 | Label not_smi; |
| 4940 | // Fast path. |
| 4941 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4942 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4943 | __ b(ne, ¬_smi); |
| 4944 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 4945 | // Return if no overflow. |
| 4946 | __ Ret(vc); |
| 4947 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 4948 | |
| 4949 | HandleBinaryOpSlowCases(masm, |
| 4950 | ¬_smi, |
| 4951 | Builtins::SUB, |
| 4952 | Token::SUB, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4953 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4954 | break; |
| 4955 | } |
| 4956 | |
| 4957 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4958 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4959 | ASSERT(kSmiTag == 0); // adjust code below |
| 4960 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4961 | __ b(ne, ¬_smi); |
| 4962 | // 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] | 4963 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4964 | // Do multiplication |
| 4965 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 4966 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4967 | __ mov(ip, Operand(r3, ASR, 31)); |
| 4968 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 4969 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4970 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4971 | __ tst(r3, Operand(r3)); |
| 4972 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4973 | __ Ret(ne); |
| 4974 | // Slow case. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4975 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4976 | |
| 4977 | HandleBinaryOpSlowCases(masm, |
| 4978 | ¬_smi, |
| 4979 | Builtins::MUL, |
| 4980 | Token::MUL, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4981 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4982 | break; |
| 4983 | } |
| 4984 | |
| 4985 | case Token::BIT_OR: |
| 4986 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 4987 | case Token::BIT_XOR: |
| 4988 | case Token::SAR: |
| 4989 | case Token::SHR: |
| 4990 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4991 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4992 | ASSERT(kSmiTag == 0); // adjust code below |
| 4993 | __ tst(r2, Operand(kSmiTagMask)); |
| 4994 | __ b(ne, &slow); |
| 4995 | switch (op_) { |
| 4996 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 4997 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 4998 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4999 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5000 | // Remove tags from right operand. |
| 5001 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5002 | // Use only the 5 least significant bits of the shift count. |
| 5003 | __ and_(r2, r2, Operand(0x1f)); |
| 5004 | __ mov(r0, Operand(r1, ASR, r2)); |
| 5005 | // Smi tag result. |
| 5006 | __ and_(r0, r0, Operand(~kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5007 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5008 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5009 | // Remove tags from operands. We can't do this on a 31 bit number |
| 5010 | // because then the 0s get shifted into bit 30 instead of bit 31. |
| 5011 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5012 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5013 | // Use only the 5 least significant bits of the shift count. |
| 5014 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5015 | __ mov(r3, Operand(r3, LSR, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5016 | // Unsigned shift is not allowed to produce a negative number, so |
| 5017 | // check the sign bit and the sign bit after Smi tagging. |
| 5018 | __ tst(r3, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5019 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5020 | // Smi tag result. |
| 5021 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5022 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5023 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5024 | // Remove tags from operands. |
| 5025 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 5026 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 5027 | // Use only the 5 least significant bits of the shift count. |
| 5028 | __ and_(r2, r2, Operand(0x1f)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5029 | __ mov(r3, Operand(r3, LSL, r2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5030 | // Check that the signed result fits in a Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5031 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 5032 | __ b(mi, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5033 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5034 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5035 | default: UNREACHABLE(); |
| 5036 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5037 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5038 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5039 | HandleNonSmiBitwiseOp(masm); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5040 | break; |
| 5041 | } |
| 5042 | |
| 5043 | default: UNREACHABLE(); |
| 5044 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5045 | // This code should be unreachable. |
| 5046 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
| 5049 | |
| 5050 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 5051 | Label within_limit; |
| 5052 | __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit())); |
| 5053 | __ ldr(ip, MemOperand(ip)); |
| 5054 | __ cmp(sp, Operand(ip)); |
| 5055 | __ b(hs, &within_limit); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 5056 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 5057 | // argument, so give it a Smi. |
| 5058 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5059 | __ push(r0); |
| 5060 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1); |
| 5061 | __ bind(&within_limit); |
| 5062 | |
| 5063 | __ StubReturn(1); |
| 5064 | } |
| 5065 | |
| 5066 | |
| 5067 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 5068 | Label undo; |
| 5069 | Label slow; |
| 5070 | Label done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5071 | Label not_smi; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5072 | |
| 5073 | // Enter runtime system if the value is not a smi. |
| 5074 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5075 | __ b(ne, ¬_smi); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5076 | |
| 5077 | // Enter runtime system if the value of the expression is zero |
| 5078 | // to make sure that we switch between 0 and -0. |
| 5079 | __ cmp(r0, Operand(0)); |
| 5080 | __ b(eq, &slow); |
| 5081 | |
| 5082 | // The value of the expression is a smi that is not zero. Try |
| 5083 | // optimistic subtraction '0 - value'. |
| 5084 | __ rsb(r1, r0, Operand(0), SetCC); |
| 5085 | __ b(vs, &slow); |
| 5086 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5087 | __ mov(r0, Operand(r1)); // Set r0 to result. |
| 5088 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5089 | |
| 5090 | // Enter runtime system. |
| 5091 | __ bind(&slow); |
| 5092 | __ push(r0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5093 | __ mov(r0, Operand(0)); // Set number of arguments. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5094 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 5095 | |
| 5096 | __ bind(&done); |
| 5097 | __ StubReturn(1); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5098 | |
| 5099 | __ bind(¬_smi); |
| 5100 | __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE); |
| 5101 | __ b(ne, &slow); |
| 5102 | // r0 is a heap number. Get a new heap number in r1. |
| 5103 | if (overwrite_) { |
| 5104 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5105 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5106 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5107 | } else { |
| 5108 | AllocateHeapNumber(masm, &slow, r1, r2, r3); |
| 5109 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 5110 | __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 5111 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 5112 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 5113 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 5114 | __ mov(r0, Operand(r1)); |
| 5115 | } |
| 5116 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5120 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5121 | // r0 holds the exception. |
| 5122 | |
| 5123 | // Adjust this code if not the case. |
| 5124 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5125 | |
| 5126 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5127 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 5128 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5129 | |
| 5130 | // Restore the next handler and frame pointer, discard handler state. |
| 5131 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5132 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5133 | __ str(r2, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5134 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5135 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 5136 | |
| 5137 | // Before returning we restore the context from the frame pointer if |
| 5138 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5139 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5140 | __ cmp(fp, Operand(0)); |
| 5141 | // Set cp to NULL if fp is NULL. |
| 5142 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5143 | // Restore cp otherwise. |
| 5144 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5145 | #ifdef DEBUG |
| 5146 | if (FLAG_debug_code) { |
| 5147 | __ mov(lr, Operand(pc)); |
| 5148 | } |
| 5149 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5150 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5151 | __ pop(pc); |
| 5152 | } |
| 5153 | |
| 5154 | |
| 5155 | void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5156 | // Adjust this code if not the case. |
| 5157 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 5158 | |
| 5159 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5160 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5161 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5162 | |
| 5163 | // Unwind the handlers until the ENTRY handler is found. |
| 5164 | Label loop, done; |
| 5165 | __ bind(&loop); |
| 5166 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5167 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 5168 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5169 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 5170 | __ b(eq, &done); |
| 5171 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5172 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 5173 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5174 | __ jmp(&loop); |
| 5175 | __ bind(&done); |
| 5176 | |
| 5177 | // 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^] | 5178 | ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 5179 | __ pop(r0); |
| 5180 | __ str(r0, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5181 | |
| 5182 | // Set external caught exception to false. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5183 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5184 | __ mov(r0, Operand(false)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5185 | __ mov(r2, Operand(external_caught)); |
| 5186 | __ str(r0, MemOperand(r2)); |
| 5187 | |
| 5188 | // Set pending exception and r0 to out of memory exception. |
| 5189 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5190 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5191 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 5192 | __ str(r0, MemOperand(r2)); |
| 5193 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5194 | // Stack layout at this point. See also StackHandlerConstants. |
| 5195 | // sp -> state (ENTRY) |
| 5196 | // fp |
| 5197 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5198 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5199 | // Discard handler state (r2 is not used) and restore frame pointer. |
| 5200 | ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
| 5201 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 5202 | // Before returning we restore the context from the frame pointer if |
| 5203 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 5204 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5205 | __ cmp(fp, Operand(0)); |
| 5206 | // Set cp to NULL if fp is NULL. |
| 5207 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 5208 | // Restore cp otherwise. |
| 5209 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5210 | #ifdef DEBUG |
| 5211 | if (FLAG_debug_code) { |
| 5212 | __ mov(lr, Operand(pc)); |
| 5213 | } |
| 5214 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5215 | ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5216 | __ pop(pc); |
| 5217 | } |
| 5218 | |
| 5219 | |
| 5220 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 5221 | Label* throw_normal_exception, |
| 5222 | Label* throw_out_of_memory_exception, |
| 5223 | StackFrame::Type frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5224 | bool do_gc, |
| 5225 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5226 | // r0: result parameter for PerformGC, if any |
| 5227 | // r4: number of arguments including receiver (C callee-saved) |
| 5228 | // r5: pointer to builtin function (C callee-saved) |
| 5229 | // r6: pointer to the first argument (C callee-saved) |
| 5230 | |
| 5231 | if (do_gc) { |
| 5232 | // Passing r0. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5233 | ExternalReference gc_reference = ExternalReference::perform_gc_function(); |
| 5234 | __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5237 | ExternalReference scope_depth = |
| 5238 | ExternalReference::heap_always_allocate_scope_depth(); |
| 5239 | if (always_allocate) { |
| 5240 | __ mov(r0, Operand(scope_depth)); |
| 5241 | __ ldr(r1, MemOperand(r0)); |
| 5242 | __ add(r1, r1, Operand(1)); |
| 5243 | __ str(r1, MemOperand(r0)); |
| 5244 | } |
| 5245 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5246 | // Call C built-in. |
| 5247 | // r0 = argc, r1 = argv |
| 5248 | __ mov(r0, Operand(r4)); |
| 5249 | __ mov(r1, Operand(r6)); |
| 5250 | |
| 5251 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 5252 | // frames, we need to know where the return address is. Right now, |
| 5253 | // we push it on the stack to be able to find it again, but we never |
| 5254 | // restore from it in case of changes, which makes it impossible to |
| 5255 | // support moving the C entry code stub. This should be fixed, but currently |
| 5256 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 5257 | // sequence that it is not moving ever. |
| 5258 | __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 5259 | __ push(lr); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5260 | __ Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5261 | |
| 5262 | if (always_allocate) { |
| 5263 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 5264 | // though (contain the result). |
| 5265 | __ mov(r2, Operand(scope_depth)); |
| 5266 | __ ldr(r3, MemOperand(r2)); |
| 5267 | __ sub(r3, r3, Operand(1)); |
| 5268 | __ str(r3, MemOperand(r2)); |
| 5269 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5270 | |
| 5271 | // check for failure result |
| 5272 | Label failure_returned; |
| 5273 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 5274 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 5275 | __ add(r2, r0, Operand(1)); |
| 5276 | __ tst(r2, Operand(kFailureTagMask)); |
| 5277 | __ b(eq, &failure_returned); |
| 5278 | |
| 5279 | // Exit C frame and return. |
| 5280 | // r0:r1: result |
| 5281 | // sp: stack pointer |
| 5282 | // fp: frame pointer |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5283 | __ LeaveExitFrame(frame_type); |
| 5284 | |
| 5285 | // check if we should retry or throw exception |
| 5286 | Label retry; |
| 5287 | __ bind(&failure_returned); |
| 5288 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 5289 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 5290 | __ b(eq, &retry); |
| 5291 | |
| 5292 | Label continue_exception; |
| 5293 | // If the returned failure is EXCEPTION then promote Top::pending_exception(). |
| 5294 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 5295 | __ b(ne, &continue_exception); |
| 5296 | |
| 5297 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5298 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5299 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5300 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5301 | __ ldr(r0, MemOperand(ip)); |
| 5302 | __ str(r3, MemOperand(ip)); |
| 5303 | |
| 5304 | __ bind(&continue_exception); |
| 5305 | // Special handling of out of memory exception. |
| 5306 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 5307 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 5308 | __ b(eq, throw_out_of_memory_exception); |
| 5309 | |
| 5310 | // Handle normal exception. |
| 5311 | __ jmp(throw_normal_exception); |
| 5312 | |
| 5313 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 5314 | } |
| 5315 | |
| 5316 | |
| 5317 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 5318 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 5319 | // r0: number of arguments including receiver |
| 5320 | // r1: pointer to builtin function |
| 5321 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5322 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5323 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5324 | |
| 5325 | // NOTE: Invocations of builtins may return failure objects |
| 5326 | // instead of a proper result. The builtin entry handles |
| 5327 | // this by performing a garbage collection and retrying the |
| 5328 | // builtin once. |
| 5329 | |
| 5330 | StackFrame::Type frame_type = is_debug_break |
| 5331 | ? StackFrame::EXIT_DEBUG |
| 5332 | : StackFrame::EXIT; |
| 5333 | |
| 5334 | // Enter the exit frame that transitions from JavaScript to C++. |
| 5335 | __ EnterExitFrame(frame_type); |
| 5336 | |
| 5337 | // r4: number of arguments (C callee-saved) |
| 5338 | // r5: pointer to builtin function (C callee-saved) |
| 5339 | // r6: pointer to first argument (C callee-saved) |
| 5340 | |
| 5341 | Label throw_out_of_memory_exception; |
| 5342 | Label throw_normal_exception; |
| 5343 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5344 | // Call into the runtime system. Collect garbage before the call if |
| 5345 | // running with --gc-greedy set. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5346 | if (FLAG_gc_greedy) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5347 | Failure* failure = Failure::RetryAfterGC(0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5348 | __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure))); |
| 5349 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5350 | GenerateCore(masm, &throw_normal_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5351 | &throw_out_of_memory_exception, |
| 5352 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5353 | FLAG_gc_greedy, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5354 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5355 | |
| 5356 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5357 | GenerateCore(masm, |
| 5358 | &throw_normal_exception, |
| 5359 | &throw_out_of_memory_exception, |
| 5360 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 5361 | true, |
| 5362 | false); |
| 5363 | |
| 5364 | // Do full GC and retry runtime call one final time. |
| 5365 | Failure* failure = Failure::InternalError(); |
| 5366 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 5367 | GenerateCore(masm, |
| 5368 | &throw_normal_exception, |
| 5369 | &throw_out_of_memory_exception, |
| 5370 | frame_type, |
| 5371 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5372 | true); |
| 5373 | |
| 5374 | __ bind(&throw_out_of_memory_exception); |
| 5375 | GenerateThrowOutOfMemory(masm); |
| 5376 | // control flow for generated will not return. |
| 5377 | |
| 5378 | __ bind(&throw_normal_exception); |
| 5379 | GenerateThrowTOS(masm); |
| 5380 | } |
| 5381 | |
| 5382 | |
| 5383 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 5384 | // r0: code entry |
| 5385 | // r1: function |
| 5386 | // r2: receiver |
| 5387 | // r3: argc |
| 5388 | // [sp+0]: argv |
| 5389 | |
| 5390 | Label invoke, exit; |
| 5391 | |
| 5392 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 5393 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5394 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5395 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 5396 | |
| 5397 | // Get address of argv, see stm above. |
| 5398 | // r0: code entry |
| 5399 | // r1: function |
| 5400 | // r2: receiver |
| 5401 | // r3: argc |
| 5402 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 5403 | __ ldr(r4, MemOperand(r4)); // argv |
| 5404 | |
| 5405 | // Push a frame with special values setup to mark it as an entry frame. |
| 5406 | // r0: code entry |
| 5407 | // r1: function |
| 5408 | // r2: receiver |
| 5409 | // r3: argc |
| 5410 | // r4: argv |
| 5411 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 5412 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 5413 | __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL)); |
| 5414 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 5415 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5416 | __ ldr(r5, MemOperand(r5)); |
| 5417 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 5418 | |
| 5419 | // Setup frame pointer for the frame to be pushed. |
| 5420 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5421 | |
| 5422 | // Call a faked try-block that does the invoke. |
| 5423 | __ bl(&invoke); |
| 5424 | |
| 5425 | // Caught exception: Store result (exception) in the pending |
| 5426 | // exception field in the JSEnv and return a failure sentinel. |
| 5427 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 5428 | // 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] | 5429 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5430 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5431 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5432 | __ b(&exit); |
| 5433 | |
| 5434 | // Invoke: Link this frame into the handler chain. |
| 5435 | __ bind(&invoke); |
| 5436 | // Must preserve r0-r4, r5-r7 are available. |
| 5437 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5438 | // If an exception not caught by another handler occurs, this handler |
| 5439 | // returns control to the code after the bl(&invoke) above, which |
| 5440 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 5441 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5442 | |
| 5443 | // Clear any pending exceptions. |
| 5444 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 5445 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5446 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5447 | __ str(r5, MemOperand(ip)); |
| 5448 | |
| 5449 | // Invoke the function by calling through JS entry trampoline builtin. |
| 5450 | // Notice that we cannot store a reference to the trampoline code directly in |
| 5451 | // this stub, because runtime stubs are not traversed when doing GC. |
| 5452 | |
| 5453 | // Expected registers by Builtins::JSEntryTrampoline |
| 5454 | // r0: code entry |
| 5455 | // r1: function |
| 5456 | // r2: receiver |
| 5457 | // r3: argc |
| 5458 | // r4: argv |
| 5459 | if (is_construct) { |
| 5460 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 5461 | __ mov(ip, Operand(construct_entry)); |
| 5462 | } else { |
| 5463 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 5464 | __ mov(ip, Operand(entry)); |
| 5465 | } |
| 5466 | __ ldr(ip, MemOperand(ip)); // deref address |
| 5467 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5468 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 5469 | // macro for the add instruction because we don't want the coverage tool |
| 5470 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5471 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5472 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5473 | |
| 5474 | // Unlink this frame from the handler chain. When reading the |
| 5475 | // address of the next handler, there is no need to use the address |
| 5476 | // displacement since the current stack pointer (sp) points directly |
| 5477 | // to the stack handler. |
| 5478 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 5479 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 5480 | __ str(r3, MemOperand(ip)); |
| 5481 | // No need to restore registers |
| 5482 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 5483 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5484 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5485 | __ bind(&exit); // r0 holds result |
| 5486 | // Restore the top frame descriptors from the stack. |
| 5487 | __ pop(r3); |
| 5488 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5489 | __ str(r3, MemOperand(ip)); |
| 5490 | |
| 5491 | // Reset the stack to the callee saved registers. |
| 5492 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5493 | |
| 5494 | // Restore callee-saved registers and return. |
| 5495 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5496 | if (FLAG_debug_code) { |
| 5497 | __ mov(lr, Operand(pc)); |
| 5498 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5499 | #endif |
| 5500 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 5501 | } |
| 5502 | |
| 5503 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5504 | // This stub performs an instanceof, calling the builtin function if |
| 5505 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 5506 | // be an instance of (these are fetched from the stack). |
| 5507 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 5508 | // Get the object - slow case for smis (we may need to throw an exception |
| 5509 | // depending on the rhs). |
| 5510 | Label slow, loop, is_instance, is_not_instance; |
| 5511 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 5512 | __ BranchOnSmi(r0, &slow); |
| 5513 | |
| 5514 | // Check that the left hand is a JS object and put map in r3. |
| 5515 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 5516 | __ b(lt, &slow); |
| 5517 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 5518 | __ b(gt, &slow); |
| 5519 | |
| 5520 | // Get the prototype of the function (r4 is result, r2 is scratch). |
| 5521 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 5522 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 5523 | |
| 5524 | // Check that the function prototype is a JS object. |
| 5525 | __ BranchOnSmi(r4, &slow); |
| 5526 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 5527 | __ b(lt, &slow); |
| 5528 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 5529 | __ b(gt, &slow); |
| 5530 | |
| 5531 | // Register mapping: r3 is object map and r4 is function prototype. |
| 5532 | // Get prototype of object into r2. |
| 5533 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 5534 | |
| 5535 | // Loop through the prototype chain looking for the function prototype. |
| 5536 | __ bind(&loop); |
| 5537 | __ cmp(r2, Operand(r4)); |
| 5538 | __ b(eq, &is_instance); |
| 5539 | __ cmp(r2, Operand(Factory::null_value())); |
| 5540 | __ b(eq, &is_not_instance); |
| 5541 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 5542 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 5543 | __ jmp(&loop); |
| 5544 | |
| 5545 | __ bind(&is_instance); |
| 5546 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 5547 | __ pop(); |
| 5548 | __ pop(); |
| 5549 | __ mov(pc, Operand(lr)); // Return. |
| 5550 | |
| 5551 | __ bind(&is_not_instance); |
| 5552 | __ mov(r0, Operand(Smi::FromInt(1))); |
| 5553 | __ pop(); |
| 5554 | __ pop(); |
| 5555 | __ mov(pc, Operand(lr)); // Return. |
| 5556 | |
| 5557 | // Slow-case. Tail call builtin. |
| 5558 | __ bind(&slow); |
| 5559 | __ mov(r0, Operand(1)); // Arg count without receiver. |
| 5560 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 5561 | } |
| 5562 | |
| 5563 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5564 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5565 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5566 | Label adaptor; |
| 5567 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5568 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5569 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5570 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5571 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5572 | // Nothing to do: The formal number of parameters has already been |
| 5573 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5574 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5575 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5576 | // Arguments adaptor case: Read the arguments length from the |
| 5577 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5578 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5579 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5580 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5584 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 5585 | // The displacement is the offset of the last parameter (if any) |
| 5586 | // relative to the frame pointer. |
| 5587 | static const int kDisplacement = |
| 5588 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5589 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5590 | // Check that the key is a smi. |
| 5591 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5592 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5593 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5594 | // Check if the calling frame is an arguments adaptor frame. |
| 5595 | Label adaptor; |
| 5596 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5597 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5598 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5599 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5600 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5601 | // Check index against formal parameters count limit passed in |
| 5602 | // through register eax. Use unsigned comparison to get negative |
| 5603 | // check for free. |
| 5604 | __ cmp(r1, r0); |
| 5605 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5606 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5607 | // Read the argument from the stack and return it. |
| 5608 | __ sub(r3, r0, r1); |
| 5609 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5610 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5611 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5612 | |
| 5613 | // Arguments adaptor case: Check index against actual arguments |
| 5614 | // limit found in the arguments adaptor frame. Use unsigned |
| 5615 | // comparison to get negative check for free. |
| 5616 | __ bind(&adaptor); |
| 5617 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5618 | __ cmp(r1, r0); |
| 5619 | __ b(cs, &slow); |
| 5620 | |
| 5621 | // Read the argument from the adaptor frame and return it. |
| 5622 | __ sub(r3, r0, r1); |
| 5623 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5624 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5625 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5626 | |
| 5627 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 5628 | // by calling the runtime system. |
| 5629 | __ bind(&slow); |
| 5630 | __ push(r1); |
| 5631 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1); |
| 5632 | } |
| 5633 | |
| 5634 | |
| 5635 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 5636 | // Check if the calling frame is an arguments adaptor frame. |
| 5637 | Label runtime; |
| 5638 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5639 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5640 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5641 | __ b(ne, &runtime); |
| 5642 | |
| 5643 | // Patch the arguments.length and the parameters pointer. |
| 5644 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5645 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 5646 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5647 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 5648 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 5649 | |
| 5650 | // Do the runtime call to allocate the arguments object. |
| 5651 | __ bind(&runtime); |
| 5652 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5653 | } |
| 5654 | |
| 5655 | |
| 5656 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 5657 | Label slow; |
| 5658 | // Get the function to call from the stack. |
| 5659 | // function, receiver [, arguments] |
| 5660 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 5661 | |
| 5662 | // Check that the function is really a JavaScript function. |
| 5663 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5664 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5665 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame^] | 5666 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5667 | __ b(ne, &slow); |
| 5668 | |
| 5669 | // Fast-case: Invoke the function now. |
| 5670 | // r1: pushed function |
| 5671 | ParameterCount actual(argc_); |
| 5672 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 5673 | |
| 5674 | // Slow-case: Non-function called. |
| 5675 | __ bind(&slow); |
| 5676 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5677 | __ mov(r2, Operand(0)); |
| 5678 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 5679 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 5680 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5681 | } |
| 5682 | |
| 5683 | |
| 5684 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5685 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5686 | } } // namespace v8::internal |