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 |
| 292 | // pp: parameter pointer |
| 293 | // cp: callee's context |
| 294 | __ mov(r0, Operand(Factory::undefined_value())); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 295 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 296 | function_return_.Bind(); |
| 297 | if (FLAG_trace) { |
| 298 | // Push the return value on the stack as the parameter. |
| 299 | // Runtime::TraceExit returns the parameter as it is. |
| 300 | frame_->EmitPush(r0); |
| 301 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 302 | } |
| 303 | |
| 304 | // Tear down the frame which will restore the caller's frame pointer and |
| 305 | // the link register. |
| 306 | frame_->Exit(); |
| 307 | |
| 308 | __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 309 | __ Jump(lr); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 310 | } |
| 311 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 312 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 313 | ASSERT(!has_cc()); |
| 314 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 315 | ASSERT(!function_return_is_shadowed_); |
| 316 | function_return_.Unuse(); |
| 317 | DeleteFrame(); |
| 318 | |
| 319 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 320 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 321 | ProcessDeferred(); |
| 322 | } |
| 323 | |
| 324 | allocator_ = NULL; |
| 325 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 329 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 330 | // Currently, this assertion will fail if we try to assign to |
| 331 | // a constant variable that is constant because it is read-only |
| 332 | // (such as the variable referring to a named function expression). |
| 333 | // We need to implement assignments to read-only variables. |
| 334 | // Ideally, we should do this during AST generation (by converting |
| 335 | // such assignments into expression statements); however, in general |
| 336 | // we may not be able to make the decision until past AST generation, |
| 337 | // that is when the entire program is known. |
| 338 | ASSERT(slot != NULL); |
| 339 | int index = slot->index(); |
| 340 | switch (slot->type()) { |
| 341 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 342 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 343 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 344 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 345 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 346 | |
| 347 | case Slot::CONTEXT: { |
| 348 | // Follow the context chain if necessary. |
| 349 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 350 | Register context = cp; |
| 351 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 352 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 353 | // Load the closure. |
| 354 | // (All contexts, even 'with' contexts, have a closure, |
| 355 | // and it is the same for all contexts inside a function. |
| 356 | // There is no need to go to the function context first.) |
| 357 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 358 | // Load the function context (which is the incoming, outer context). |
| 359 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 360 | context = tmp; |
| 361 | } |
| 362 | // We may have a 'with' context now. Get the function context. |
| 363 | // (In fact this mov may never be the needed, since the scope analysis |
| 364 | // may not permit a direct context access in this case and thus we are |
| 365 | // always at a function context. However it is safe to dereference be- |
| 366 | // cause the function context of a function context is itself. Before |
| 367 | // deleting this mov we should try to create a counter-example first, |
| 368 | // though...) |
| 369 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 370 | return ContextOperand(tmp, index); |
| 371 | } |
| 372 | |
| 373 | default: |
| 374 | UNREACHABLE(); |
| 375 | return MemOperand(r0, 0); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 380 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 381 | Slot* slot, |
| 382 | Register tmp, |
| 383 | Register tmp2, |
| 384 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 385 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 386 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 387 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 388 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 389 | if (s->num_heap_slots() > 0) { |
| 390 | if (s->calls_eval()) { |
| 391 | // Check that extension is NULL. |
| 392 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 393 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 394 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 395 | } |
| 396 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 397 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 398 | context = tmp; |
| 399 | } |
| 400 | } |
| 401 | // Check that last extension is NULL. |
| 402 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 403 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 404 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 405 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 406 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 410 | void CodeGenerator::LoadConditionAndSpill(Expression* expression, |
| 411 | TypeofState typeof_state, |
| 412 | JumpTarget* true_target, |
| 413 | JumpTarget* false_target, |
| 414 | bool force_control) { |
| 415 | ASSERT(in_spilled_code()); |
| 416 | set_in_spilled_code(false); |
| 417 | LoadCondition(expression, typeof_state, true_target, false_target, |
| 418 | force_control); |
| 419 | if (frame_ != NULL) { |
| 420 | frame_->SpillAll(); |
| 421 | } |
| 422 | set_in_spilled_code(true); |
| 423 | } |
| 424 | |
| 425 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 426 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 427 | // (partially) translated into branches, or it may have set the condition |
| 428 | // code register. If force_cc is set, the value is forced to set the |
| 429 | // condition code register and no value is pushed. If the condition code |
| 430 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 431 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 432 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 433 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 434 | JumpTarget* true_target, |
| 435 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 436 | bool force_cc) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 437 | ASSERT(!in_spilled_code()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 438 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 439 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 440 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 441 | { CodeGenState new_state(this, typeof_state, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 442 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 443 | |
| 444 | // If we hit a stack overflow, we may not have actually visited |
| 445 | // the expression. In that case, we ensure that we have a |
| 446 | // valid-looking frame state because we will continue to generate |
| 447 | // code as we unwind the C++ stack. |
| 448 | // |
| 449 | // It's possible to have both a stack overflow and a valid frame |
| 450 | // state (eg, a subexpression overflowed, visiting it returned |
| 451 | // with a dummied frame state, and visiting this expression |
| 452 | // returned with a normal-looking state). |
| 453 | if (HasStackOverflow() && |
| 454 | has_valid_frame() && |
| 455 | !has_cc() && |
| 456 | frame_->height() == original_height) { |
| 457 | true_target->Jump(); |
| 458 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 459 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 460 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 461 | // Convert the TOS value to a boolean in the condition code register. |
| 462 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 464 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 465 | ASSERT(!has_valid_frame() || |
| 466 | (has_cc() && frame_->height() == original_height) || |
| 467 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 471 | void CodeGenerator::LoadAndSpill(Expression* expression, |
| 472 | TypeofState typeof_state) { |
| 473 | ASSERT(in_spilled_code()); |
| 474 | set_in_spilled_code(false); |
| 475 | Load(expression, typeof_state); |
| 476 | frame_->SpillAll(); |
| 477 | set_in_spilled_code(true); |
| 478 | } |
| 479 | |
| 480 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 481 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 482 | #ifdef DEBUG |
| 483 | int original_height = frame_->height(); |
| 484 | #endif |
| 485 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 486 | JumpTarget true_target; |
| 487 | JumpTarget false_target; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 488 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 489 | |
| 490 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 491 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 492 | JumpTarget loaded; |
| 493 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 494 | materialize_true.Branch(cc_reg_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 495 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 496 | frame_->EmitPush(r0); |
| 497 | loaded.Jump(); |
| 498 | materialize_true.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 499 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 500 | frame_->EmitPush(r0); |
| 501 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 502 | cc_reg_ = al; |
| 503 | } |
| 504 | |
| 505 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 506 | // We have at least one condition value that has been "translated" |
| 507 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 508 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 509 | if (frame_ != NULL) { |
| 510 | loaded.Jump(); // Don't lose the current TOS. |
| 511 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 512 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 513 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 515 | true_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 516 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 517 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 519 | // If both "true" and "false" need to be loaded jump across the code for |
| 520 | // "false". |
| 521 | if (both) { |
| 522 | loaded.Jump(); |
| 523 | } |
| 524 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 525 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 526 | false_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 527 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 528 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 529 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 530 | // A value is loaded on all paths reaching this point. |
| 531 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 533 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 534 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 535 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 539 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 540 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 541 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 542 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 546 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 547 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 548 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 549 | __ ldr(scratch, |
| 550 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 551 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 555 | // 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] | 556 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 557 | // variables w/o reference errors elsewhere. |
| 558 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 559 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 561 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 562 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 563 | // the variable as if through '<global>.<variable>' to make sure we |
| 564 | // do not get reference errors. |
| 565 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 566 | Literal key(variable->name()); |
| 567 | // TODO(1241834): Fetch the position from the variable instead of using |
| 568 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 569 | Property property(&global, &key, RelocInfo::kNoPosition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 570 | LoadAndSpill(&property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 572 | LoadAndSpill(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 577 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 578 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 579 | cgen->LoadReference(this); |
| 580 | } |
| 581 | |
| 582 | |
| 583 | Reference::~Reference() { |
| 584 | cgen_->UnloadReference(this); |
| 585 | } |
| 586 | |
| 587 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 588 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 589 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 590 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 591 | Expression* e = ref->expression(); |
| 592 | Property* property = e->AsProperty(); |
| 593 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 594 | |
| 595 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 596 | // The expression is either a property or a variable proxy that rewrites |
| 597 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 598 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 599 | // We use a named reference if the key is a literal symbol, unless it is |
| 600 | // a string that can be legally parsed as an integer. This is because |
| 601 | // otherwise we will not get into the slow case code that handles [] on |
| 602 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | Literal* literal = property->key()->AsLiteral(); |
| 604 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 605 | if (literal != NULL && |
| 606 | literal->handle()->IsSymbol() && |
| 607 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 608 | ref->set_type(Reference::NAMED); |
| 609 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 610 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 611 | ref->set_type(Reference::KEYED); |
| 612 | } |
| 613 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 614 | // The expression is a variable proxy that does not rewrite to a |
| 615 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 617 | LoadGlobal(); |
| 618 | ref->set_type(Reference::NAMED); |
| 619 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 620 | ASSERT(var->slot() != NULL); |
| 621 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 622 | } |
| 623 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 624 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 625 | LoadAndSpill(e); |
| 626 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
| 630 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 631 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 632 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 633 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 634 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 635 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 636 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 637 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 638 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 639 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 644 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 645 | // register to a boolean in the condition code register. The code |
| 646 | // 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] | 647 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 648 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 649 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 650 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 651 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 652 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 653 | |
| 654 | // Fast case checks |
| 655 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 656 | // Check if the value is 'false'. |
| 657 | __ cmp(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 658 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 659 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 660 | // Check if the value is 'true'. |
| 661 | __ cmp(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 662 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 663 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 664 | // Check if the value is 'undefined'. |
| 665 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 666 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 667 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 668 | // Check if the value is a smi. |
| 669 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 670 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 671 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 672 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 673 | |
| 674 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 675 | frame_->EmitPush(r0); |
| 676 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 677 | // Convert the result (r0) to a condition code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 679 | |
| 680 | cc_reg_ = ne; |
| 681 | } |
| 682 | |
| 683 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 684 | class GenericBinaryOpStub : public CodeStub { |
| 685 | public: |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 686 | GenericBinaryOpStub(Token::Value op, |
| 687 | OverwriteMode mode) |
| 688 | : op_(op), mode_(mode) { } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 689 | |
| 690 | private: |
| 691 | Token::Value op_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 692 | OverwriteMode mode_; |
| 693 | |
| 694 | // Minor key encoding in 16 bits. |
| 695 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 696 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 697 | |
| 698 | Major MajorKey() { return GenericBinaryOp; } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 699 | int MinorKey() { |
| 700 | // Encode the parameters in a unique 16 bit value. |
| 701 | return OpBits::encode(op_) |
| 702 | | ModeBits::encode(mode_); |
| 703 | } |
| 704 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 705 | void Generate(MacroAssembler* masm); |
| 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 | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1506 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1507 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1508 | __ cmp(r1, Operand(HEAP_NUMBER_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1509 | default_target->Branch(ne); |
| 1510 | frame_->EmitPush(r0); |
| 1511 | frame_->CallRuntime(Runtime::kNumberToSmi, 1); |
| 1512 | is_smi.Bind(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1513 | |
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 | // Small positive numbers can be immediate operands. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1516 | if (min_index < 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1517 | // If min_index is Smi::kMinValue, -min_index is not a Smi. |
| 1518 | if (Smi::IsValid(-min_index)) { |
| 1519 | __ add(r0, r0, Operand(Smi::FromInt(-min_index))); |
| 1520 | } else { |
| 1521 | __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1))); |
| 1522 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1523 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1524 | } else { |
| 1525 | __ sub(r0, r0, Operand(Smi::FromInt(min_index))); |
| 1526 | } |
| 1527 | } |
| 1528 | __ tst(r0, Operand(0x80000000 | kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1529 | default_target->Branch(ne); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1530 | __ cmp(r0, Operand(Smi::FromInt(range))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1531 | default_target->Branch(ge); |
| 1532 | VirtualFrame* start_frame = new VirtualFrame(frame_); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1533 | __ SmiJumpTable(r0, case_targets); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1534 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1535 | GenerateFastCaseSwitchCases(node, case_labels, start_frame); |
| 1536 | |
| 1537 | // If there was a default case among the case labels, we need to |
| 1538 | // emit code to jump to it from the default target used for failure |
| 1539 | // to hit the jump table. |
| 1540 | if (default_label != NULL) { |
| 1541 | if (has_valid_frame()) { |
| 1542 | node->break_target()->Jump(); |
| 1543 | } |
| 1544 | setup_default.Bind(); |
| 1545 | frame_->MergeTo(start_frame); |
| 1546 | __ b(default_label); |
| 1547 | DeleteFrame(); |
| 1548 | } |
| 1549 | if (node->break_target()->is_linked()) { |
| 1550 | node->break_target()->Bind(); |
| 1551 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1555 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1556 | #ifdef DEBUG |
| 1557 | int original_height = frame_->height(); |
| 1558 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1559 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1560 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1561 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1562 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1563 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1564 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1565 | if (TryGenerateFastCaseSwitchStatement(node)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1566 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1567 | return; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1570 | JumpTarget next_test; |
| 1571 | JumpTarget fall_through; |
| 1572 | JumpTarget default_entry; |
| 1573 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1574 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1575 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1576 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1577 | |
| 1578 | for (int i = 0; i < length; i++) { |
| 1579 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1580 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1581 | // Remember the default clause and compile it at the end. |
| 1582 | default_clause = clause; |
| 1583 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1586 | Comment cmnt(masm_, "[ Case clause"); |
| 1587 | // Compile the test. |
| 1588 | next_test.Bind(); |
| 1589 | next_test.Unuse(); |
| 1590 | // Duplicate TOS. |
| 1591 | __ ldr(r0, frame_->Top()); |
| 1592 | frame_->EmitPush(r0); |
| 1593 | LoadAndSpill(clause->label()); |
| 1594 | Comparison(eq, true); |
| 1595 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1596 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1597 | // Before entering the body from the test, remove the switch value from |
| 1598 | // the stack. |
| 1599 | frame_->Drop(); |
| 1600 | |
| 1601 | // Label the body so that fall through is enabled. |
| 1602 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1603 | default_exit.Bind(); |
| 1604 | } else { |
| 1605 | fall_through.Bind(); |
| 1606 | fall_through.Unuse(); |
| 1607 | } |
| 1608 | VisitStatementsAndSpill(clause->statements()); |
| 1609 | |
| 1610 | // If control flow can fall through from the body, jump to the next body |
| 1611 | // or the end of the statement. |
| 1612 | if (frame_ != NULL) { |
| 1613 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1614 | default_entry.Jump(); |
| 1615 | } else { |
| 1616 | fall_through.Jump(); |
| 1617 | } |
| 1618 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1621 | // The final "test" removes the switch value. |
| 1622 | next_test.Bind(); |
| 1623 | frame_->Drop(); |
| 1624 | |
| 1625 | // If there is a default clause, compile it. |
| 1626 | if (default_clause != NULL) { |
| 1627 | Comment cmnt(masm_, "[ Default clause"); |
| 1628 | default_entry.Bind(); |
| 1629 | VisitStatementsAndSpill(default_clause->statements()); |
| 1630 | // If control flow can fall out of the default and there is a case after |
| 1631 | // it, jup to that case's body. |
| 1632 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1633 | default_exit.Jump(); |
| 1634 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1635 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1636 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1637 | if (fall_through.is_linked()) { |
| 1638 | fall_through.Bind(); |
| 1639 | } |
| 1640 | |
| 1641 | if (node->break_target()->is_linked()) { |
| 1642 | node->break_target()->Bind(); |
| 1643 | } |
| 1644 | node->break_target()->Unuse(); |
| 1645 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1649 | void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1650 | #ifdef DEBUG |
| 1651 | int original_height = frame_->height(); |
| 1652 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1653 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1654 | Comment cmnt(masm_, "[ LoopStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1655 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1656 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1657 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1658 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 1659 | // known result for the test expression, with no side effects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1660 | enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
| 1661 | if (node->cond() == NULL) { |
| 1662 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1663 | info = ALWAYS_TRUE; |
| 1664 | } else { |
| 1665 | Literal* lit = node->cond()->AsLiteral(); |
| 1666 | if (lit != NULL) { |
| 1667 | if (lit->IsTrue()) { |
| 1668 | info = ALWAYS_TRUE; |
| 1669 | } else if (lit->IsFalse()) { |
| 1670 | info = ALWAYS_FALSE; |
| 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1675 | switch (node->type()) { |
| 1676 | case LoopStatement::DO_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1677 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1678 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1679 | // Label the top of the loop for the backward CFG edge. If the test |
| 1680 | // is always true we can use the continue target, and if the test is |
| 1681 | // always false there is no need. |
| 1682 | if (info == ALWAYS_TRUE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1683 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1684 | node->continue_target()->Bind(); |
| 1685 | } else if (info == ALWAYS_FALSE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1686 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1687 | } else { |
| 1688 | ASSERT(info == DONT_KNOW); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1689 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1690 | body.Bind(); |
| 1691 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1692 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1693 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1694 | VisitAndSpill(node->body()); |
| 1695 | |
| 1696 | // Compile the test. |
| 1697 | if (info == ALWAYS_TRUE) { |
| 1698 | if (has_valid_frame()) { |
| 1699 | // If control can fall off the end of the body, jump back to the |
| 1700 | // top. |
| 1701 | node->continue_target()->Jump(); |
| 1702 | } |
| 1703 | } else if (info == ALWAYS_FALSE) { |
| 1704 | // If we have a continue in the body, we only have to bind its jump |
| 1705 | // target. |
| 1706 | if (node->continue_target()->is_linked()) { |
| 1707 | node->continue_target()->Bind(); |
| 1708 | } |
| 1709 | } else { |
| 1710 | ASSERT(info == DONT_KNOW); |
| 1711 | // We have to compile the test expression if it can be reached by |
| 1712 | // control flow falling out of the body or via continue. |
| 1713 | if (node->continue_target()->is_linked()) { |
| 1714 | node->continue_target()->Bind(); |
| 1715 | } |
| 1716 | if (has_valid_frame()) { |
| 1717 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1718 | &body, node->break_target(), true); |
| 1719 | if (has_valid_frame()) { |
| 1720 | // A invalid frame here indicates that control did not |
| 1721 | // fall out of the test expression. |
| 1722 | Branch(true, &body); |
| 1723 | } |
| 1724 | } |
| 1725 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1726 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | case LoopStatement::WHILE_LOOP: { |
| 1730 | // If the test is never true and has no side effects there is no need |
| 1731 | // to compile the test or body. |
| 1732 | if (info == ALWAYS_FALSE) break; |
| 1733 | |
| 1734 | // Label the top of the loop with the continue target for the backward |
| 1735 | // CFG edge. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1736 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1737 | node->continue_target()->Bind(); |
| 1738 | |
| 1739 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1740 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1741 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1742 | &body, node->break_target(), true); |
| 1743 | if (has_valid_frame()) { |
| 1744 | // A NULL frame indicates that control did not fall out of the |
| 1745 | // test expression. |
| 1746 | Branch(false, node->break_target()); |
| 1747 | } |
| 1748 | if (has_valid_frame() || body.is_linked()) { |
| 1749 | body.Bind(); |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | if (has_valid_frame()) { |
| 1754 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1755 | VisitAndSpill(node->body()); |
| 1756 | |
| 1757 | // If control flow can fall out of the body, jump back to the top. |
| 1758 | if (has_valid_frame()) { |
| 1759 | node->continue_target()->Jump(); |
| 1760 | } |
| 1761 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1762 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | case LoopStatement::FOR_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1766 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1767 | |
| 1768 | if (node->init() != NULL) { |
| 1769 | VisitAndSpill(node->init()); |
| 1770 | } |
| 1771 | |
| 1772 | // There is no need to compile the test or body. |
| 1773 | if (info == ALWAYS_FALSE) break; |
| 1774 | |
| 1775 | // If there is no update statement, label the top of the loop with the |
| 1776 | // continue target, otherwise with the loop target. |
| 1777 | if (node->next() == NULL) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1778 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1779 | node->continue_target()->Bind(); |
| 1780 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1781 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1782 | loop.Bind(); |
| 1783 | } |
| 1784 | |
| 1785 | // If the test is always true, there is no need to compile it. |
| 1786 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1787 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1788 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1789 | &body, node->break_target(), true); |
| 1790 | if (has_valid_frame()) { |
| 1791 | Branch(false, node->break_target()); |
| 1792 | } |
| 1793 | if (has_valid_frame() || body.is_linked()) { |
| 1794 | body.Bind(); |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | if (has_valid_frame()) { |
| 1799 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1800 | VisitAndSpill(node->body()); |
| 1801 | |
| 1802 | if (node->next() == NULL) { |
| 1803 | // If there is no update statement and control flow can fall out |
| 1804 | // of the loop, jump directly to the continue label. |
| 1805 | if (has_valid_frame()) { |
| 1806 | node->continue_target()->Jump(); |
| 1807 | } |
| 1808 | } else { |
| 1809 | // If there is an update statement and control flow can reach it |
| 1810 | // via falling out of the body of the loop or continuing, we |
| 1811 | // compile the update statement. |
| 1812 | if (node->continue_target()->is_linked()) { |
| 1813 | node->continue_target()->Bind(); |
| 1814 | } |
| 1815 | if (has_valid_frame()) { |
| 1816 | // Record source position of the statement as this code which is |
| 1817 | // after the code for the body actually belongs to the loop |
| 1818 | // statement and not the body. |
| 1819 | CodeForStatementPosition(node); |
| 1820 | VisitAndSpill(node->next()); |
| 1821 | loop.Jump(); |
| 1822 | } |
| 1823 | } |
| 1824 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1825 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1826 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1829 | if (node->break_target()->is_linked()) { |
| 1830 | node->break_target()->Bind(); |
| 1831 | } |
| 1832 | node->continue_target()->Unuse(); |
| 1833 | node->break_target()->Unuse(); |
| 1834 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1838 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1839 | #ifdef DEBUG |
| 1840 | int original_height = frame_->height(); |
| 1841 | #endif |
| 1842 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1843 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1844 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1845 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1846 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1847 | JumpTarget primitive; |
| 1848 | JumpTarget jsobject; |
| 1849 | JumpTarget fixed_array; |
| 1850 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1851 | JumpTarget end_del_check; |
| 1852 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1853 | |
| 1854 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1855 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1856 | |
| 1857 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1858 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1859 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1860 | __ cmp(r0, Operand(Factory::undefined_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 | __ cmp(r0, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1863 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1864 | |
| 1865 | // Stack layout in body: |
| 1866 | // [iteration counter (Smi)] |
| 1867 | // [length of array] |
| 1868 | // [FixedArray] |
| 1869 | // [Map or 0] |
| 1870 | // [Object] |
| 1871 | |
| 1872 | // Check if enumerable is already a JSObject |
| 1873 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1874 | primitive.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1875 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1876 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 1877 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1878 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1879 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1880 | primitive.Bind(); |
| 1881 | frame_->EmitPush(r0); |
| 1882 | Result arg_count = allocator_->Allocate(r0); |
| 1883 | ASSERT(arg_count.is_valid()); |
| 1884 | __ mov(arg_count.reg(), Operand(0)); |
| 1885 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1886 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1887 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1888 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1889 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1890 | frame_->EmitPush(r0); |
| 1891 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1892 | |
| 1893 | // If we got a Map, we can do a fast modification check. |
| 1894 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1895 | __ mov(r2, Operand(r0)); |
| 1896 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1897 | __ cmp(r1, Operand(Factory::meta_map())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1898 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1899 | |
| 1900 | // Get enum cache |
| 1901 | __ mov(r1, Operand(r0)); |
| 1902 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1903 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1904 | __ ldr(r2, |
| 1905 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1906 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1907 | frame_->EmitPush(r0); // map |
| 1908 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1909 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1910 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1911 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1912 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1913 | frame_->EmitPush(r0); |
| 1914 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1915 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1916 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1917 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1918 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1919 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1920 | |
| 1921 | // 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] | 1922 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1923 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1924 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1925 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1926 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1927 | |
| 1928 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1929 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1930 | // sp[0] : index |
| 1931 | // sp[1] : array/enum cache length |
| 1932 | // sp[2] : array or enum cache |
| 1933 | // sp[3] : 0 or map |
| 1934 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1935 | // Grab the current frame's height for the break and continue |
| 1936 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1937 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1938 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1939 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1940 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1941 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1942 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1943 | node->break_target()->Branch(hs); |
| 1944 | |
| 1945 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1946 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1947 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1948 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1949 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1950 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1951 | |
| 1952 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1953 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1954 | // Check if this (still) matches the map of the enumerable. |
| 1955 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1956 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1957 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1958 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1959 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1960 | |
| 1961 | // 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] | 1962 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1963 | frame_->EmitPush(r0); |
| 1964 | frame_->EmitPush(r3); // push entry |
| 1965 | Result arg_count_register = allocator_->Allocate(r0); |
| 1966 | ASSERT(arg_count_register.is_valid()); |
| 1967 | __ mov(arg_count_register.reg(), Operand(1)); |
| 1968 | Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY, |
| 1969 | CALL_JS, |
| 1970 | &arg_count_register, |
| 1971 | 2); |
| 1972 | __ mov(r3, Operand(result.reg())); |
| 1973 | result.Unuse(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1974 | |
| 1975 | // If the property has been removed while iterating, we just skip it. |
| 1976 | __ cmp(r3, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1977 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1978 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1979 | end_del_check.Bind(); |
| 1980 | // Store the entry in the 'each' expression and take another spin in the |
| 1981 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1982 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1983 | { Reference each(this, node->each()); |
| 1984 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1985 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1986 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1987 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1988 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1989 | // If the reference was to a slot we rely on the convenient property |
| 1990 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1991 | // right on top of or right underneath a zero-sized reference. |
| 1992 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1993 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1994 | // It's safe to pop the value lying on top of the reference before |
| 1995 | // unloading the reference itself (which preserves the top of stack, |
| 1996 | // ie, now the topmost value of the non-zero sized reference), since |
| 1997 | // we will discard the top of stack after unloading the reference |
| 1998 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1999 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2000 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2001 | } |
| 2002 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2003 | // Discard the i'th entry pushed above or else the remainder of the |
| 2004 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2005 | frame_->Drop(); |
| 2006 | |
| 2007 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2008 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2009 | VisitAndSpill(node->body()); |
| 2010 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2011 | // Next. Reestablish a spilled frame in case we are coming here via |
| 2012 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2013 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2014 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2015 | frame_->EmitPop(r0); |
| 2016 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 2017 | frame_->EmitPush(r0); |
| 2018 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2019 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2020 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 2021 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2022 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2023 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2024 | |
| 2025 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2026 | exit.Bind(); |
| 2027 | node->continue_target()->Unuse(); |
| 2028 | node->break_target()->Unuse(); |
| 2029 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2033 | void CodeGenerator::VisitTryCatch(TryCatch* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2034 | #ifdef DEBUG |
| 2035 | int original_height = frame_->height(); |
| 2036 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2037 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2038 | Comment cmnt(masm_, "[ TryCatch"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2039 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2040 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2041 | JumpTarget try_block; |
| 2042 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2043 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2044 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2046 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2047 | |
| 2048 | // Store the caught exception in the catch variable. |
| 2049 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2050 | ASSERT(ref.is_slot()); |
| 2051 | // Here we make use of the convenient property that it doesn't matter |
| 2052 | // whether a value is immediately on top of or underneath a zero-sized |
| 2053 | // reference. |
| 2054 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2058 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2059 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2060 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2061 | if (frame_ != NULL) { |
| 2062 | exit.Jump(); |
| 2063 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2064 | |
| 2065 | |
| 2066 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2067 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2068 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2069 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2070 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2071 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2072 | // Shadow the labels for all escapes from the try block, including |
| 2073 | // returns. During shadowing, the original label is hidden as the |
| 2074 | // LabelShadow and operations on the original actually affect the |
| 2075 | // shadowing label. |
| 2076 | // |
| 2077 | // We should probably try to unify the escaping labels and the return |
| 2078 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2079 | int nof_escapes = node->escaping_targets()->length(); |
| 2080 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2081 | |
| 2082 | // Add the shadow target for the function return. |
| 2083 | static const int kReturnShadowIndex = 0; |
| 2084 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2085 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2086 | function_return_is_shadowed_ = true; |
| 2087 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2088 | |
| 2089 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2090 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2091 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2095 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2096 | |
| 2097 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2098 | // After shadowing stops, the original labels are unshadowed and the |
| 2099 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2100 | bool has_unlinks = false; |
| 2101 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2102 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2103 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2104 | } |
| 2105 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2106 | |
| 2107 | // Get an external reference to the handler address. |
| 2108 | ExternalReference handler_address(Top::k_handler_address); |
| 2109 | |
| 2110 | // The next handler address is at kNextIndex in the stack. |
| 2111 | const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize; |
| 2112 | // If we can fall off the end of the try block, unlink from try chain. |
| 2113 | if (has_valid_frame()) { |
| 2114 | __ ldr(r1, frame_->ElementAt(kNextIndex)); |
| 2115 | __ mov(r3, Operand(handler_address)); |
| 2116 | __ str(r1, MemOperand(r3)); |
| 2117 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize); |
| 2118 | if (has_unlinks) { |
| 2119 | exit.Jump(); |
| 2120 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2123 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2124 | // jumped to. Deallocate each shadow target. |
| 2125 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2126 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2127 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2128 | shadows[i]->Bind(); |
| 2129 | // Because we can be jumping here (to spilled code) from unspilled |
| 2130 | // code, we need to reestablish a spilled frame at this block. |
| 2131 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2132 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2133 | // Reload sp from the top handler, because some statements that we |
| 2134 | // 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] | 2135 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2136 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2137 | // The stack pointer was restored to just below the code slot |
| 2138 | // (the topmost slot) in the handler. |
| 2139 | frame_->Forget(frame_->height() - handler_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2140 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2141 | // kNextIndex is off by one because the code slot has already |
| 2142 | // been dropped. |
| 2143 | __ ldr(r1, frame_->ElementAt(kNextIndex - 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2144 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2145 | // The code slot has already been dropped from the handler. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2146 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2147 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2148 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2149 | frame_->PrepareForReturn(); |
| 2150 | } |
| 2151 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2155 | exit.Bind(); |
| 2156 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2160 | void CodeGenerator::VisitTryFinally(TryFinally* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2161 | #ifdef DEBUG |
| 2162 | int original_height = frame_->height(); |
| 2163 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2164 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2165 | Comment cmnt(masm_, "[ TryFinally"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2166 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2167 | |
| 2168 | // State: Used to keep track of reason for entering the finally |
| 2169 | // block. Should probably be extended to hold information for |
| 2170 | // break/continue from within the try block. |
| 2171 | enum { FALLING, THROWING, JUMPING }; |
| 2172 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2173 | JumpTarget try_block; |
| 2174 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2175 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2176 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2177 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2178 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2179 | // In case of thrown exceptions, this is where we continue. |
| 2180 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2181 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2182 | |
| 2183 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2184 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2185 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2186 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2187 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2188 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2189 | // Shadow the labels for all escapes from the try block, including |
| 2190 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2191 | // operations on the original actually affect the shadowing label. |
| 2192 | // |
| 2193 | // We should probably try to unify the escaping labels and the return |
| 2194 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2195 | int nof_escapes = node->escaping_targets()->length(); |
| 2196 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2197 | |
| 2198 | // Add the shadow target for the function return. |
| 2199 | static const int kReturnShadowIndex = 0; |
| 2200 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2201 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2202 | function_return_is_shadowed_ = true; |
| 2203 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2204 | |
| 2205 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2206 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2207 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2211 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2212 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2213 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2214 | // After shadowing stops, the original labels are unshadowed and the |
| 2215 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2216 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2217 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2218 | shadows[i]->StopShadowing(); |
| 2219 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2220 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2221 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2222 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2223 | // Get an external reference to the handler address. |
| 2224 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2225 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2226 | // The next handler address is at kNextIndex in the stack. |
| 2227 | const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize; |
| 2228 | // If we can fall off the end of the try block, unlink from the try |
| 2229 | // chain and set the state on the frame to FALLING. |
| 2230 | if (has_valid_frame()) { |
| 2231 | __ ldr(r1, frame_->ElementAt(kNextIndex)); |
| 2232 | __ mov(r3, Operand(handler_address)); |
| 2233 | __ str(r1, MemOperand(r3)); |
| 2234 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize); |
| 2235 | |
| 2236 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2237 | // state in r2, then jump around the unlink blocks if any. |
| 2238 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2239 | frame_->EmitPush(r0); |
| 2240 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2241 | if (nof_unlinks > 0) { |
| 2242 | finally_block.Jump(); |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | // Generate code to unlink and set the state for the (formerly) |
| 2247 | // shadowing targets that have been jumped to. |
| 2248 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2249 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2250 | // If we have come from the shadowed return, the return value is |
| 2251 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2252 | // until it is pushed. |
| 2253 | // |
| 2254 | // Because we can be jumping here (to spilled code) from |
| 2255 | // unspilled code, we need to reestablish a spilled frame at |
| 2256 | // this block. |
| 2257 | shadows[i]->Bind(); |
| 2258 | frame_->SpillAll(); |
| 2259 | |
| 2260 | // Reload sp from the top handler, because some statements that |
| 2261 | // we break from (eg, for...in) may have left stuff on the |
| 2262 | // stack. |
| 2263 | __ mov(r3, Operand(handler_address)); |
| 2264 | __ ldr(sp, MemOperand(r3)); |
| 2265 | // The stack pointer was restored to the address slot in the handler. |
| 2266 | ASSERT(StackHandlerConstants::kNextOffset == 1 * kPointerSize); |
| 2267 | frame_->Forget(frame_->height() - handler_height + 1); |
| 2268 | |
| 2269 | // Unlink this handler and drop it from the frame. The next |
| 2270 | // handler address is now on top of the frame. |
| 2271 | frame_->EmitPop(r1); |
| 2272 | __ str(r1, MemOperand(r3)); |
| 2273 | // The top (code) and the second (handler) slot have both been |
| 2274 | // dropped already. |
| 2275 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 2); |
| 2276 | |
| 2277 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2278 | // If this label shadowed the function return, materialize the |
| 2279 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2280 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2281 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2282 | // Fake TOS for targets that shadowed breaks and continues. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2283 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2284 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2285 | } |
| 2286 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2287 | if (--nof_unlinks > 0) { |
| 2288 | // If this is not the last unlink block, jump around the next. |
| 2289 | finally_block.Jump(); |
| 2290 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2291 | } |
| 2292 | } |
| 2293 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2294 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2295 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2296 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2297 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2298 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2299 | |
| 2300 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2301 | // and the state - while evaluating the finally block. |
| 2302 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2303 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2304 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2305 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2306 | if (has_valid_frame()) { |
| 2307 | // Restore state and return value or faked TOS. |
| 2308 | frame_->EmitPop(r2); |
| 2309 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2312 | // Generate code to jump to the right destination for all used |
| 2313 | // formerly shadowing targets. Deallocate each shadow target. |
| 2314 | for (int i = 0; i < shadows.length(); i++) { |
| 2315 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2316 | JumpTarget* original = shadows[i]->other_target(); |
| 2317 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2318 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2319 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2320 | skip.Branch(ne); |
| 2321 | frame_->PrepareForReturn(); |
| 2322 | original->Jump(); |
| 2323 | skip.Bind(); |
| 2324 | } else { |
| 2325 | original->Branch(eq); |
| 2326 | } |
| 2327 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2328 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2329 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2330 | if (has_valid_frame()) { |
| 2331 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2332 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2333 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2334 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2335 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2336 | // Rethrow exception. |
| 2337 | frame_->EmitPush(r0); |
| 2338 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2339 | |
| 2340 | // Done. |
| 2341 | exit.Bind(); |
| 2342 | } |
| 2343 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2347 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2348 | #ifdef DEBUG |
| 2349 | int original_height = frame_->height(); |
| 2350 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2351 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2352 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2353 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2354 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2355 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2356 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2357 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2358 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2362 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2363 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2364 | ASSERT(boilerplate->IsBoilerplate()); |
| 2365 | |
| 2366 | // Push the boilerplate on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2367 | __ mov(r0, Operand(boilerplate)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2368 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2369 | |
| 2370 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2371 | frame_->EmitPush(cp); |
| 2372 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2373 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2377 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2378 | #ifdef DEBUG |
| 2379 | int original_height = frame_->height(); |
| 2380 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2381 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2382 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2383 | |
| 2384 | // Build the function boilerplate and instantiate it. |
| 2385 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2386 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2387 | if (HasStackOverflow()) { |
| 2388 | ASSERT(frame_->height() == original_height); |
| 2389 | return; |
| 2390 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2391 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2392 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2396 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2397 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2398 | #ifdef DEBUG |
| 2399 | int original_height = frame_->height(); |
| 2400 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2401 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2402 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2403 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2404 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2408 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2409 | #ifdef DEBUG |
| 2410 | int original_height = frame_->height(); |
| 2411 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2412 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2413 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2414 | JumpTarget then; |
| 2415 | JumpTarget else_; |
| 2416 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2417 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 2418 | &then, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2419 | Branch(false, &else_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2420 | then.Bind(); |
| 2421 | LoadAndSpill(node->then_expression(), typeof_state()); |
| 2422 | exit.Jump(); |
| 2423 | else_.Bind(); |
| 2424 | LoadAndSpill(node->else_expression(), typeof_state()); |
| 2425 | exit.Bind(); |
| 2426 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2430 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2431 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2432 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2433 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2434 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2435 | JumpTarget slow; |
| 2436 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2437 | |
| 2438 | // Generate fast-case code for variables that might be shadowed by |
| 2439 | // eval-introduced variables. Eval is used a lot without |
| 2440 | // introducing variables. In those cases, we do not want to |
| 2441 | // perform a runtime call for all variables in the scope |
| 2442 | // containing the eval. |
| 2443 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2444 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2445 | // If there was no control flow to slow, we can exit early. |
| 2446 | if (!slow.is_linked()) { |
| 2447 | frame_->EmitPush(r0); |
| 2448 | return; |
| 2449 | } |
| 2450 | |
| 2451 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2452 | |
| 2453 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2454 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2455 | // Only generate the fast case for locals that rewrite to slots. |
| 2456 | // This rules out argument loads. |
| 2457 | if (potential_slot != NULL) { |
| 2458 | __ ldr(r0, |
| 2459 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2460 | r1, |
| 2461 | r2, |
| 2462 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2463 | if (potential_slot->var()->mode() == Variable::CONST) { |
| 2464 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2465 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
| 2466 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2467 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2468 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2469 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2470 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2471 | } |
| 2472 | } |
| 2473 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2474 | slow.Bind(); |
| 2475 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2476 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2477 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2478 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2479 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2480 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2481 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2482 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2483 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2484 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2485 | done.Bind(); |
| 2486 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2487 | |
| 2488 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2489 | // Note: We would like to keep the assert below, but it fires because of |
| 2490 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2491 | // ASSERT(!slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2492 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2493 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2494 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2495 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2496 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2497 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2498 | // initialized yet) which needs to be converted into the 'undefined' |
| 2499 | // value. |
| 2500 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2501 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2502 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2503 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2504 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2505 | } |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2510 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2511 | TypeofState typeof_state, |
| 2512 | Register tmp, |
| 2513 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2514 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2515 | // Check that no extension objects have been created by calls to |
| 2516 | // eval from the current scope to the global scope. |
| 2517 | Register context = cp; |
| 2518 | Scope* s = scope(); |
| 2519 | while (s != NULL) { |
| 2520 | if (s->num_heap_slots() > 0) { |
| 2521 | if (s->calls_eval()) { |
| 2522 | // Check that extension is NULL. |
| 2523 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2524 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2525 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2526 | } |
| 2527 | // Load next context in chain. |
| 2528 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2529 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2530 | context = tmp; |
| 2531 | } |
| 2532 | // If no outer scope calls eval, we do not need to check more |
| 2533 | // context extensions. |
| 2534 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2535 | s = s->outer_scope(); |
| 2536 | } |
| 2537 | |
| 2538 | if (s->is_eval_scope()) { |
| 2539 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2540 | if (!context.is(tmp)) { |
| 2541 | __ mov(tmp, Operand(context)); |
| 2542 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2543 | __ bind(&next); |
| 2544 | // Terminate at global context. |
| 2545 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 2546 | __ cmp(tmp2, Operand(Factory::global_context_map())); |
| 2547 | __ b(eq, &fast); |
| 2548 | // Check that extension is NULL. |
| 2549 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2550 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2551 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2552 | // Load next context in chain. |
| 2553 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2554 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2555 | __ b(&next); |
| 2556 | __ bind(&fast); |
| 2557 | } |
| 2558 | |
| 2559 | // All extension objects were empty and it is safe to use a global |
| 2560 | // load IC call. |
| 2561 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2562 | // Load the global object. |
| 2563 | LoadGlobal(); |
| 2564 | // Setup the name register. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2565 | Result name = allocator_->Allocate(r2); |
| 2566 | ASSERT(name.is_valid()); // We are in spilled code. |
| 2567 | __ mov(name.reg(), Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2568 | // Call IC stub. |
| 2569 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2570 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2571 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2572 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2575 | // Drop the global object. The result is in r0. |
| 2576 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2580 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2581 | #ifdef DEBUG |
| 2582 | int original_height = frame_->height(); |
| 2583 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2584 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2585 | Comment cmnt(masm_, "[ Slot"); |
| 2586 | LoadFromSlot(node, typeof_state()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2587 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2588 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2589 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2590 | |
| 2591 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2592 | #ifdef DEBUG |
| 2593 | int original_height = frame_->height(); |
| 2594 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2595 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2596 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2597 | |
| 2598 | Variable* var = node->var(); |
| 2599 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2600 | if (expr != NULL) { |
| 2601 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2602 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2603 | ASSERT(var->is_global()); |
| 2604 | Reference ref(this, node); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2605 | ref.GetValueAndSpill(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2606 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2607 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2611 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2612 | #ifdef DEBUG |
| 2613 | int original_height = frame_->height(); |
| 2614 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2615 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2616 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2617 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2618 | frame_->EmitPush(r0); |
| 2619 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2623 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2624 | #ifdef DEBUG |
| 2625 | int original_height = frame_->height(); |
| 2626 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2627 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2628 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2629 | |
| 2630 | // Retrieve the literal array and check the allocated entry. |
| 2631 | |
| 2632 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2633 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2634 | |
| 2635 | // Load the literals array of the function. |
| 2636 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2637 | |
| 2638 | // Load the literal at the ast saved index. |
| 2639 | int literal_offset = |
| 2640 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2641 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2642 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2643 | JumpTarget done; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2644 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2645 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2646 | |
| 2647 | // If the entry is undefined we call the runtime system to computed |
| 2648 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2649 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2650 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2651 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2652 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2653 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2654 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2655 | frame_->EmitPush(r0); |
| 2656 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2657 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2658 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2659 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2660 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2661 | frame_->EmitPush(r2); |
| 2662 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | |
| 2666 | // This deferred code stub will be used for creating the boilerplate |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2667 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2668 | // Each created boilerplate is stored in the JSFunction and they are |
| 2669 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2670 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2671 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2672 | explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2673 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2674 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2675 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2676 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2677 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2678 | private: |
| 2679 | ObjectLiteral* node_; |
| 2680 | }; |
| 2681 | |
| 2682 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2683 | void DeferredObjectLiteral::Generate() { |
| 2684 | // Argument is passed in r1. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2685 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2686 | // 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] | 2687 | // the literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2688 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 2689 | __ push(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2690 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2691 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 2692 | __ push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2693 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2694 | __ mov(r0, Operand(node_->constant_properties())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 2695 | __ push(r0); |
| 2696 | __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2697 | __ mov(r2, Operand(r0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2698 | // Result is returned in r2. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2702 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2703 | #ifdef DEBUG |
| 2704 | int original_height = frame_->height(); |
| 2705 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2706 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2707 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2708 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2709 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2710 | |
| 2711 | // Retrieve the literal array and check the allocated entry. |
| 2712 | |
| 2713 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2714 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2715 | |
| 2716 | // Load the literals array of the function. |
| 2717 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2718 | |
| 2719 | // Load the literal at the ast saved index. |
| 2720 | int literal_offset = |
| 2721 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2722 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2723 | |
| 2724 | // Check whether we need to materialize the object literal boilerplate. |
| 2725 | // If so, jump to the deferred code. |
| 2726 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 2727 | deferred->Branch(eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2728 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2731 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2732 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2733 | // Clone the boilerplate object. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2734 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2735 | if (node->depth() == 1) { |
| 2736 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2737 | } |
| 2738 | frame_->CallRuntime(clone_function_id, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2739 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2740 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2741 | |
| 2742 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2743 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2744 | Literal* key = property->key(); |
| 2745 | Expression* value = property->value(); |
| 2746 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2747 | case ObjectLiteral::Property::CONSTANT: |
| 2748 | break; |
| 2749 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2750 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2751 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2752 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2753 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2754 | frame_->EmitPush(r0); // dup the result |
| 2755 | LoadAndSpill(key); |
| 2756 | LoadAndSpill(value); |
| 2757 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2758 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2759 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2760 | break; |
| 2761 | } |
| 2762 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2763 | frame_->EmitPush(r0); |
| 2764 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2765 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2766 | frame_->EmitPush(r0); |
| 2767 | LoadAndSpill(value); |
| 2768 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2769 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2770 | break; |
| 2771 | } |
| 2772 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2773 | frame_->EmitPush(r0); |
| 2774 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2775 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2776 | frame_->EmitPush(r0); |
| 2777 | LoadAndSpill(value); |
| 2778 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2779 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2780 | break; |
| 2781 | } |
| 2782 | } |
| 2783 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2784 | ASSERT(frame_->height() == original_height + 1); |
| 2785 | } |
| 2786 | |
| 2787 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2788 | // This deferred code stub will be used for creating the boilerplate |
| 2789 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2790 | // Each created boilerplate is stored in the JSFunction and they are |
| 2791 | // therefore context dependent. |
| 2792 | class DeferredArrayLiteral: public DeferredCode { |
| 2793 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2794 | explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2795 | set_comment("[ DeferredArrayLiteral"); |
| 2796 | } |
| 2797 | |
| 2798 | virtual void Generate(); |
| 2799 | |
| 2800 | private: |
| 2801 | ArrayLiteral* node_; |
| 2802 | }; |
| 2803 | |
| 2804 | |
| 2805 | void DeferredArrayLiteral::Generate() { |
| 2806 | // Argument is passed in r1. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2807 | |
| 2808 | // If the entry is undefined we call the runtime system to computed |
| 2809 | // the literal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2810 | // Literal array (0). |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 2811 | __ push(r1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2812 | // Literal index (1). |
| 2813 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 2814 | __ push(r0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2815 | // Constant properties (2). |
| 2816 | __ mov(r0, Operand(node_->literals())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 2817 | __ push(r0); |
| 2818 | __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2819 | __ mov(r2, Operand(r0)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2820 | // Result is returned in r2. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2824 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2825 | #ifdef DEBUG |
| 2826 | int original_height = frame_->height(); |
| 2827 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2828 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2829 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2830 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 2831 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2832 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2833 | // Retrieve the literal array and check the allocated entry. |
| 2834 | |
| 2835 | // Load the function of this activation. |
| 2836 | __ ldr(r1, frame_->Function()); |
| 2837 | |
| 2838 | // Load the literals array of the function. |
| 2839 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2840 | |
| 2841 | // Load the literal at the ast saved index. |
| 2842 | int literal_offset = |
| 2843 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2844 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2845 | |
| 2846 | // Check whether we need to materialize the object literal boilerplate. |
| 2847 | // If so, jump to the deferred code. |
| 2848 | __ cmp(r2, Operand(Factory::undefined_value())); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 2849 | deferred->Branch(eq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2850 | deferred->BindExit(); |
| 2851 | |
| 2852 | // Push the object literal boilerplate. |
| 2853 | frame_->EmitPush(r2); |
| 2854 | |
| 2855 | // Clone the boilerplate object. |
| 2856 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2857 | if (node->depth() == 1) { |
| 2858 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2859 | } |
| 2860 | frame_->CallRuntime(clone_function_id, 1); |
| 2861 | frame_->EmitPush(r0); // save the result |
| 2862 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2863 | |
| 2864 | // Generate code to set the elements in the array that are not |
| 2865 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2866 | for (int i = 0; i < node->values()->length(); i++) { |
| 2867 | Expression* value = node->values()->at(i); |
| 2868 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2869 | // If value is a literal the property value is already set in the |
| 2870 | // boilerplate object. |
| 2871 | if (value->AsLiteral() != NULL) continue; |
| 2872 | // If value is a materialized literal the property value is already set |
| 2873 | // in the boilerplate object if it is simple. |
| 2874 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
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 | // The property must be set by generated code. |
| 2877 | LoadAndSpill(value); |
| 2878 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2879 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2880 | // Fetch the object literal. |
| 2881 | __ ldr(r1, frame_->Top()); |
| 2882 | // Get the elements array. |
| 2883 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2884 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2885 | // Write to the indexed properties array. |
| 2886 | int offset = i * kPointerSize + Array::kHeaderSize; |
| 2887 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2888 | |
| 2889 | // Update the write barrier for the array address. |
| 2890 | __ mov(r3, Operand(offset)); |
| 2891 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2892 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2893 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2897 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2898 | #ifdef DEBUG |
| 2899 | int original_height = frame_->height(); |
| 2900 | #endif |
| 2901 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2902 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2903 | // Call runtime routine to allocate the catch extension object and |
| 2904 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2905 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2906 | LoadAndSpill(node->key()); |
| 2907 | LoadAndSpill(node->value()); |
| 2908 | Result result = |
| 2909 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2910 | frame_->EmitPush(result.reg()); |
| 2911 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2915 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2916 | #ifdef DEBUG |
| 2917 | int original_height = frame_->height(); |
| 2918 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2919 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2920 | Comment cmnt(masm_, "[ Assignment"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2921 | CodeForStatementPosition(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2922 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2923 | { Reference target(this, node->target()); |
| 2924 | if (target.is_illegal()) { |
| 2925 | // Fool the virtual frame into thinking that we left the assignment's |
| 2926 | // value on the frame. |
| 2927 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2928 | frame_->EmitPush(r0); |
| 2929 | ASSERT(frame_->height() == original_height + 1); |
| 2930 | return; |
| 2931 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2932 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2933 | if (node->op() == Token::ASSIGN || |
| 2934 | node->op() == Token::INIT_VAR || |
| 2935 | node->op() == Token::INIT_CONST) { |
| 2936 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2937 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2938 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2939 | // +=, *= and similar binary assignments. |
| 2940 | // Get the old value of the lhs. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2941 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 2942 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2943 | bool overwrite = |
| 2944 | (node->value()->AsBinaryOperation() != NULL && |
| 2945 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2946 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2947 | SmiOperation(node->binary_op(), |
| 2948 | literal->handle(), |
| 2949 | false, |
| 2950 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2951 | frame_->EmitPush(r0); |
| 2952 | |
| 2953 | } else { |
| 2954 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2955 | GenericBinaryOperation(node->binary_op(), |
| 2956 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2957 | frame_->EmitPush(r0); |
| 2958 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2959 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2960 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2961 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2962 | if (var != NULL && |
| 2963 | (var->mode() == Variable::CONST) && |
| 2964 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2965 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2966 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2967 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2968 | CodeForSourcePosition(node->position()); |
| 2969 | if (node->op() == Token::INIT_CONST) { |
| 2970 | // Dynamic constant initializations must use the function context |
| 2971 | // and initialize the actual constant declared. Dynamic variable |
| 2972 | // initializations are simply assignments and use SetValue. |
| 2973 | target.SetValue(CONST_INIT); |
| 2974 | } else { |
| 2975 | target.SetValue(NOT_CONST_INIT); |
| 2976 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2977 | } |
| 2978 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2979 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2983 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2984 | #ifdef DEBUG |
| 2985 | int original_height = frame_->height(); |
| 2986 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2987 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2988 | Comment cmnt(masm_, "[ Throw"); |
| 2989 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2990 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2991 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2992 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2993 | frame_->EmitPush(r0); |
| 2994 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2998 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2999 | #ifdef DEBUG |
| 3000 | int original_height = frame_->height(); |
| 3001 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3002 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3003 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3004 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3005 | { Reference property(this, node); |
| 3006 | property.GetValueAndSpill(typeof_state()); |
| 3007 | } |
| 3008 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3012 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3013 | #ifdef DEBUG |
| 3014 | int original_height = frame_->height(); |
| 3015 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3016 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3017 | Comment cmnt(masm_, "[ Call"); |
| 3018 | |
| 3019 | ZoneList<Expression*>* args = node->arguments(); |
| 3020 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3021 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3022 | // Standard function call. |
| 3023 | |
| 3024 | // Check if the function is a variable or a property. |
| 3025 | Expression* function = node->expression(); |
| 3026 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 3027 | Property* property = function->AsProperty(); |
| 3028 | |
| 3029 | // ------------------------------------------------------------------------ |
| 3030 | // Fast-case: Use inline caching. |
| 3031 | // --- |
| 3032 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 3033 | // must be resolved after the arguments have been evaluated. The IC code |
| 3034 | // automatically handles this by loading the arguments before the function |
| 3035 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 3036 | // ------------------------------------------------------------------------ |
| 3037 | |
| 3038 | if (var != NULL && !var->is_this() && var->is_global()) { |
| 3039 | // ---------------------------------- |
| 3040 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3041 | // ---------------------------------- |
| 3042 | |
| 3043 | // 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] | 3044 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3045 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3046 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3047 | // Pass the global object as the receiver and let the IC stub |
| 3048 | // patch the stack to use the global proxy as 'this' in the |
| 3049 | // invoked function. |
| 3050 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3051 | |
| 3052 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3053 | int arg_count = args->length(); |
| 3054 | for (int i = 0; i < arg_count; i++) { |
| 3055 | LoadAndSpill(args->at(i)); |
| 3056 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3057 | |
| 3058 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3059 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3060 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3061 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3062 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3063 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3064 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3065 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3066 | frame_->Drop(); |
| 3067 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3068 | |
| 3069 | } else if (var != NULL && var->slot() != NULL && |
| 3070 | var->slot()->type() == Slot::LOOKUP) { |
| 3071 | // ---------------------------------- |
| 3072 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3073 | // ---------------------------------- |
| 3074 | |
| 3075 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3076 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3077 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3078 | frame_->EmitPush(r0); |
| 3079 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3080 | // r0: slot value; r1: receiver |
| 3081 | |
| 3082 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3083 | frame_->EmitPush(r0); // function |
| 3084 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3085 | |
| 3086 | // Call the function. |
| 3087 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3088 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3089 | |
| 3090 | } else if (property != NULL) { |
| 3091 | // Check if the key is a literal string. |
| 3092 | Literal* literal = property->key()->AsLiteral(); |
| 3093 | |
| 3094 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3095 | // ------------------------------------------------------------------ |
| 3096 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3097 | // ------------------------------------------------------------------ |
| 3098 | |
| 3099 | // 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] | 3100 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3101 | frame_->EmitPush(r0); |
| 3102 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3103 | |
| 3104 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3105 | int arg_count = args->length(); |
| 3106 | for (int i = 0; i < arg_count; i++) { |
| 3107 | LoadAndSpill(args->at(i)); |
| 3108 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3109 | |
| 3110 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3111 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3112 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3113 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3114 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3115 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3116 | |
| 3117 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3118 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3119 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3120 | 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] | 3121 | |
| 3122 | } else { |
| 3123 | // ------------------------------------------- |
| 3124 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3125 | // ------------------------------------------- |
| 3126 | |
| 3127 | // Load the function to call from the property through a reference. |
| 3128 | Reference ref(this, property); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3129 | ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3130 | |
| 3131 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3132 | if (property->is_synthetic()) { |
| 3133 | LoadGlobalReceiver(r0); |
| 3134 | } else { |
| 3135 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3136 | frame_->EmitPush(r0); |
| 3137 | } |
| 3138 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3139 | // Call the function. |
| 3140 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3141 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | } else { |
| 3145 | // ---------------------------------- |
| 3146 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3147 | // ---------------------------------- |
| 3148 | |
| 3149 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3150 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3151 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3152 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3153 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3154 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3155 | // Call the function. |
| 3156 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3157 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3158 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3159 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3163 | void CodeGenerator::VisitCallEval(CallEval* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3164 | #ifdef DEBUG |
| 3165 | int original_height = frame_->height(); |
| 3166 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3167 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3168 | Comment cmnt(masm_, "[ CallEval"); |
| 3169 | |
| 3170 | // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve |
| 3171 | // the function we need to call and the receiver of the call. |
| 3172 | // Then we call the resolved function using the given arguments. |
| 3173 | |
| 3174 | ZoneList<Expression*>* args = node->arguments(); |
| 3175 | Expression* function = node->expression(); |
| 3176 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3177 | CodeForStatementPosition(node); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3178 | |
| 3179 | // Prepare stack for call to resolved function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3180 | LoadAndSpill(function); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3181 | __ mov(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3182 | frame_->EmitPush(r2); // Slot for receiver |
| 3183 | int arg_count = args->length(); |
| 3184 | for (int i = 0; i < arg_count; i++) { |
| 3185 | LoadAndSpill(args->at(i)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | // Prepare stack for call to ResolvePossiblyDirectEval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3189 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3190 | frame_->EmitPush(r1); |
| 3191 | if (arg_count > 0) { |
| 3192 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3193 | frame_->EmitPush(r1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3194 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3195 | frame_->EmitPush(r2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | // Resolve the call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3199 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3200 | |
| 3201 | // Touch up stack with the right values for the function and the receiver. |
| 3202 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3203 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3204 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3205 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3206 | |
| 3207 | // Call the function. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3208 | CodeForSourcePosition(node->position()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3209 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3210 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3211 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3212 | frame_->CallStub(&call_function, arg_count + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3213 | |
| 3214 | __ ldr(cp, frame_->Context()); |
| 3215 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3216 | frame_->Drop(); |
| 3217 | frame_->EmitPush(r0); |
| 3218 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3222 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3223 | #ifdef DEBUG |
| 3224 | int original_height = frame_->height(); |
| 3225 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3226 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3227 | Comment cmnt(masm_, "[ CallNew"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3228 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3229 | |
| 3230 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3231 | // expression in new calls must be evaluated before the |
| 3232 | // arguments. This is different from ordinary calls, where the |
| 3233 | // actual function to call is resolved after the arguments have been |
| 3234 | // evaluated. |
| 3235 | |
| 3236 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3237 | // receiver. There is no need to use the global proxy here because |
| 3238 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3239 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3240 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3241 | |
| 3242 | // Push the arguments ("left-to-right") on the stack. |
| 3243 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3244 | int arg_count = args->length(); |
| 3245 | for (int i = 0; i < arg_count; i++) { |
| 3246 | LoadAndSpill(args->at(i)); |
| 3247 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3248 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3249 | // r0: the number of arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3250 | Result num_args = allocator_->Allocate(r0); |
| 3251 | ASSERT(num_args.is_valid()); |
| 3252 | __ mov(num_args.reg(), Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3253 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3254 | // Load the function into r1 as per calling convention. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3255 | Result function = allocator_->Allocate(r1); |
| 3256 | ASSERT(function.is_valid()); |
| 3257 | __ ldr(function.reg(), frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3258 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3259 | // Call the construct call builtin that handles allocation and |
| 3260 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3261 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3262 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
| 3263 | Result result = frame_->CallCodeObject(ic, |
| 3264 | RelocInfo::CONSTRUCT_CALL, |
| 3265 | &num_args, |
| 3266 | &function, |
| 3267 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3268 | |
| 3269 | // 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] | 3270 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3271 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3275 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3276 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3277 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3278 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3279 | LoadAndSpill(args->at(0)); |
| 3280 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3281 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3282 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3283 | leave.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3284 | // It is a heap object - get map. |
| 3285 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3286 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3287 | // if (!object->IsJSValue()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3288 | __ cmp(r1, Operand(JS_VALUE_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3289 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3290 | // Load the value. |
| 3291 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3292 | leave.Bind(); |
| 3293 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3297 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3298 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3299 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3300 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3301 | LoadAndSpill(args->at(0)); // Load the object. |
| 3302 | LoadAndSpill(args->at(1)); // Load the value. |
| 3303 | frame_->EmitPop(r0); // r0 contains value |
| 3304 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3305 | // if (object->IsSmi()) return object. |
| 3306 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3307 | leave.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3308 | // It is a heap object - get map. |
| 3309 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3310 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3311 | // if (!object->IsJSValue()) return object. |
| 3312 | __ cmp(r2, Operand(JS_VALUE_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3313 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3314 | // Store the value. |
| 3315 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3316 | // Update the write barrier. |
| 3317 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3318 | __ RecordWrite(r1, r2, r3); |
| 3319 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3320 | leave.Bind(); |
| 3321 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3325 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3326 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3327 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3328 | LoadAndSpill(args->at(0)); |
| 3329 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3330 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3331 | cc_reg_ = eq; |
| 3332 | } |
| 3333 | |
| 3334 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3335 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3336 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3337 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3338 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3339 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3340 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3341 | LoadAndSpill(args->at(1)); |
| 3342 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3343 | __ CallRuntime(Runtime::kLog, 2); |
| 3344 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3345 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3346 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3347 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3351 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3352 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3353 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3354 | LoadAndSpill(args->at(0)); |
| 3355 | frame_->EmitPop(r0); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3356 | __ tst(r0, Operand(kSmiTagMask | 0x80000000)); |
| 3357 | cc_reg_ = eq; |
| 3358 | } |
| 3359 | |
| 3360 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3361 | // This should generate code that performs a charCodeAt() call or returns |
| 3362 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3363 | // 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] | 3364 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3365 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3366 | ASSERT(args->length() == 2); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3367 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3368 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3372 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3373 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3374 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3375 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3376 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3377 | // We need the CC bits to come out as not_equal in the case where the |
| 3378 | // object is a smi. This can't be done with the usual test opcode so |
| 3379 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3380 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3381 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3382 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3383 | answer.Branch(ne); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3384 | // It is a heap object - get the map. |
| 3385 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3386 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3387 | // Check if the object is a JS array or not. |
| 3388 | __ cmp(r1, Operand(JS_ARRAY_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3389 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3390 | cc_reg_ = eq; |
| 3391 | } |
| 3392 | |
| 3393 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3394 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3395 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3396 | ASSERT(args->length() == 0); |
| 3397 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3398 | // Seed the result with the formal parameters count, which will be used |
| 3399 | // 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] | 3400 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3401 | |
| 3402 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3403 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3404 | frame_->CallStub(&stub, 0); |
| 3405 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3409 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3410 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3411 | ASSERT(args->length() == 1); |
| 3412 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3413 | // Satisfy contract with ArgumentsAccessStub: |
| 3414 | // 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] | 3415 | LoadAndSpill(args->at(0)); |
| 3416 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3417 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3418 | |
| 3419 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3420 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3421 | frame_->CallStub(&stub, 0); |
| 3422 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3426 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3427 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3428 | ASSERT(args->length() == 2); |
| 3429 | |
| 3430 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3431 | LoadAndSpill(args->at(0)); |
| 3432 | LoadAndSpill(args->at(1)); |
| 3433 | frame_->EmitPop(r0); |
| 3434 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3435 | __ cmp(r0, Operand(r1)); |
| 3436 | cc_reg_ = eq; |
| 3437 | } |
| 3438 | |
| 3439 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3440 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3441 | #ifdef DEBUG |
| 3442 | int original_height = frame_->height(); |
| 3443 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3444 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3445 | if (CheckForInlineRuntimeCall(node)) { |
| 3446 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3447 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3448 | return; |
| 3449 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3450 | |
| 3451 | ZoneList<Expression*>* args = node->arguments(); |
| 3452 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3453 | Runtime::Function* function = node->function(); |
| 3454 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3455 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3456 | // Prepare stack for calling JS runtime function. |
| 3457 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3458 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3459 | // Push the builtins object found in the current global object. |
| 3460 | __ ldr(r1, GlobalObject()); |
| 3461 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3462 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3463 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3464 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3465 | // Push the arguments ("left-to-right"). |
| 3466 | int arg_count = args->length(); |
| 3467 | for (int i = 0; i < arg_count; i++) { |
| 3468 | LoadAndSpill(args->at(i)); |
| 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 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3472 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3473 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3474 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3475 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3476 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3477 | frame_->Drop(); |
| 3478 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3479 | } else { |
| 3480 | // Call the C runtime function. |
| 3481 | frame_->CallRuntime(function, arg_count); |
| 3482 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3483 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3484 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3488 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3489 | #ifdef DEBUG |
| 3490 | int original_height = frame_->height(); |
| 3491 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3492 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3493 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3494 | |
| 3495 | Token::Value op = node->op(); |
| 3496 | |
| 3497 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3498 | LoadConditionAndSpill(node->expression(), |
| 3499 | NOT_INSIDE_TYPEOF, |
| 3500 | false_target(), |
| 3501 | true_target(), |
| 3502 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3503 | cc_reg_ = NegateCondition(cc_reg_); |
| 3504 | |
| 3505 | } else if (op == Token::DELETE) { |
| 3506 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3507 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3508 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3509 | LoadAndSpill(property->obj()); |
| 3510 | LoadAndSpill(property->key()); |
| 3511 | Result arg_count = allocator_->Allocate(r0); |
| 3512 | ASSERT(arg_count.is_valid()); |
| 3513 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3514 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3515 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3516 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3517 | Slot* slot = variable->slot(); |
| 3518 | if (variable->is_global()) { |
| 3519 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3520 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3521 | frame_->EmitPush(r0); |
| 3522 | Result arg_count = allocator_->Allocate(r0); |
| 3523 | ASSERT(arg_count.is_valid()); |
| 3524 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3525 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3526 | |
| 3527 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3528 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3529 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3530 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3531 | frame_->EmitPush(r0); |
| 3532 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3533 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3534 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3535 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3536 | frame_->EmitPush(r0); |
| 3537 | Result arg_count = allocator_->Allocate(r0); |
| 3538 | ASSERT(arg_count.is_valid()); |
| 3539 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3540 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3541 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3542 | } else { |
| 3543 | // Default: Result of deleting non-global, not dynamically |
| 3544 | // introduced variables is false. |
| 3545 | __ mov(r0, Operand(Factory::false_value())); |
| 3546 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3547 | |
| 3548 | } else { |
| 3549 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3550 | LoadAndSpill(node->expression()); // may have side-effects |
| 3551 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3552 | __ mov(r0, Operand(Factory::true_value())); |
| 3553 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3554 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3555 | |
| 3556 | } else if (op == Token::TYPEOF) { |
| 3557 | // Special case for loading the typeof expression; see comment on |
| 3558 | // LoadTypeofExpression(). |
| 3559 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3560 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3561 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3562 | |
| 3563 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3564 | LoadAndSpill(node->expression()); |
| 3565 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3566 | switch (op) { |
| 3567 | case Token::NOT: |
| 3568 | case Token::DELETE: |
| 3569 | case Token::TYPEOF: |
| 3570 | UNREACHABLE(); // handled above |
| 3571 | break; |
| 3572 | |
| 3573 | case Token::SUB: { |
| 3574 | UnarySubStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3575 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3576 | break; |
| 3577 | } |
| 3578 | |
| 3579 | case Token::BIT_NOT: { |
| 3580 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3581 | JumpTarget smi_label; |
| 3582 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3583 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3584 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3585 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3586 | frame_->EmitPush(r0); |
| 3587 | Result arg_count = allocator_->Allocate(r0); |
| 3588 | ASSERT(arg_count.is_valid()); |
| 3589 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3590 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3591 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3592 | continue_label.Jump(); |
| 3593 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3594 | __ mvn(r0, Operand(r0)); |
| 3595 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3596 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3597 | break; |
| 3598 | } |
| 3599 | |
| 3600 | case Token::VOID: |
| 3601 | // since the stack top is cached in r0, popping and then |
| 3602 | // pushing a value can be done by just writing to r0. |
| 3603 | __ mov(r0, Operand(Factory::undefined_value())); |
| 3604 | break; |
| 3605 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3606 | case Token::ADD: { |
| 3607 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3608 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3609 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3610 | continue_label.Branch(eq); |
| 3611 | frame_->EmitPush(r0); |
| 3612 | Result arg_count = allocator_->Allocate(r0); |
| 3613 | ASSERT(arg_count.is_valid()); |
| 3614 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3615 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3616 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3617 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3618 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3619 | default: |
| 3620 | UNREACHABLE(); |
| 3621 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3622 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3623 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3624 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3625 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
| 3628 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3629 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3630 | #ifdef DEBUG |
| 3631 | int original_height = frame_->height(); |
| 3632 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3633 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3634 | Comment cmnt(masm_, "[ CountOperation"); |
| 3635 | |
| 3636 | bool is_postfix = node->is_postfix(); |
| 3637 | bool is_increment = node->op() == Token::INC; |
| 3638 | |
| 3639 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3640 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3641 | |
| 3642 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3643 | if (is_postfix) { |
| 3644 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3645 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3646 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3647 | |
| 3648 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3649 | if (target.is_illegal()) { |
| 3650 | // Spoof the virtual frame to have the expected height (one higher |
| 3651 | // than on entry). |
| 3652 | if (!is_postfix) { |
| 3653 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3654 | frame_->EmitPush(r0); |
| 3655 | } |
| 3656 | ASSERT(frame_->height() == original_height + 1); |
| 3657 | return; |
| 3658 | } |
| 3659 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 3660 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3661 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3662 | JumpTarget slow; |
| 3663 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3664 | |
| 3665 | // Load the value (1) into register r1. |
| 3666 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3667 | |
| 3668 | // Check for smi operand. |
| 3669 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3670 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3671 | |
| 3672 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3673 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3674 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3675 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3676 | |
| 3677 | // Perform optimistic increment/decrement. |
| 3678 | if (is_increment) { |
| 3679 | __ add(r0, r0, Operand(r1), SetCC); |
| 3680 | } else { |
| 3681 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3682 | } |
| 3683 | |
| 3684 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3685 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3686 | |
| 3687 | // Revert optimistic increment/decrement. |
| 3688 | if (is_increment) { |
| 3689 | __ sub(r0, r0, Operand(r1)); |
| 3690 | } else { |
| 3691 | __ add(r0, r0, Operand(r1)); |
| 3692 | } |
| 3693 | |
| 3694 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3695 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3696 | { |
| 3697 | // Convert the operand to a number. |
| 3698 | frame_->EmitPush(r0); |
| 3699 | Result arg_count = allocator_->Allocate(r0); |
| 3700 | ASSERT(arg_count.is_valid()); |
| 3701 | __ mov(arg_count.reg(), Operand(0)); |
| 3702 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3703 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3704 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3705 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3706 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3709 | // Compute the new value. |
| 3710 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3711 | frame_->EmitPush(r0); |
| 3712 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3713 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3714 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3715 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3716 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3720 | exit.Bind(); |
| 3721 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3722 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3726 | if (is_postfix) frame_->EmitPop(r0); |
| 3727 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3731 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3732 | #ifdef DEBUG |
| 3733 | int original_height = frame_->height(); |
| 3734 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3735 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3736 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3737 | Token::Value op = node->op(); |
| 3738 | |
| 3739 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3740 | // operators must yield the result of one of the two expressions |
| 3741 | // before any ToBoolean() conversions. This means that the value |
| 3742 | // produced by a && or || operator is not necessarily a boolean. |
| 3743 | |
| 3744 | // NOTE: If the left hand side produces a materialized value (not in |
| 3745 | // the CC register), we force the right hand side to do the |
| 3746 | // same. This is necessary because we may have to branch to the exit |
| 3747 | // after evaluating the left hand side (due to the shortcut |
| 3748 | // semantics), but the compiler must (statically) know if the result |
| 3749 | // of compiling the binary operation is materialized or not. |
| 3750 | |
| 3751 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3752 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3753 | LoadConditionAndSpill(node->left(), |
| 3754 | NOT_INSIDE_TYPEOF, |
| 3755 | &is_true, |
| 3756 | false_target(), |
| 3757 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3758 | if (has_cc()) { |
| 3759 | Branch(false, false_target()); |
| 3760 | |
| 3761 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3762 | is_true.Bind(); |
| 3763 | LoadConditionAndSpill(node->right(), |
| 3764 | NOT_INSIDE_TYPEOF, |
| 3765 | true_target(), |
| 3766 | false_target(), |
| 3767 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3768 | |
| 3769 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3770 | JumpTarget pop_and_continue; |
| 3771 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3772 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3773 | __ ldr(r0, frame_->Top()); // dup the stack top |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3774 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3775 | // Avoid popping the result if it converts to 'false' using the |
| 3776 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3777 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3778 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3779 | Branch(false, &exit); |
| 3780 | |
| 3781 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3782 | pop_and_continue.Bind(); |
| 3783 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3784 | |
| 3785 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3786 | is_true.Bind(); |
| 3787 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3788 | |
| 3789 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3790 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3794 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3795 | LoadConditionAndSpill(node->left(), |
| 3796 | NOT_INSIDE_TYPEOF, |
| 3797 | true_target(), |
| 3798 | &is_false, |
| 3799 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3800 | if (has_cc()) { |
| 3801 | Branch(true, true_target()); |
| 3802 | |
| 3803 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3804 | is_false.Bind(); |
| 3805 | LoadConditionAndSpill(node->right(), |
| 3806 | NOT_INSIDE_TYPEOF, |
| 3807 | true_target(), |
| 3808 | false_target(), |
| 3809 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3810 | |
| 3811 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3812 | JumpTarget pop_and_continue; |
| 3813 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3814 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3815 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3816 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3817 | // Avoid popping the result if it converts to 'true' using the |
| 3818 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3819 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3820 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3821 | Branch(true, &exit); |
| 3822 | |
| 3823 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3824 | pop_and_continue.Bind(); |
| 3825 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3826 | |
| 3827 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3828 | is_false.Bind(); |
| 3829 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3830 | |
| 3831 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3832 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | } else { |
| 3836 | // Optimize for the case where (at least) one of the expressions |
| 3837 | // is a literal small integer. |
| 3838 | Literal* lliteral = node->left()->AsLiteral(); |
| 3839 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3840 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 3841 | // never return a constant/immutable object. |
| 3842 | bool overwrite_left = |
| 3843 | (node->left()->AsBinaryOperation() != NULL && |
| 3844 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3845 | bool overwrite_right = |
| 3846 | (node->right()->AsBinaryOperation() != NULL && |
| 3847 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3848 | |
| 3849 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3850 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3851 | SmiOperation(node->op(), |
| 3852 | rliteral->handle(), |
| 3853 | false, |
| 3854 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3855 | |
| 3856 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3857 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3858 | SmiOperation(node->op(), |
| 3859 | lliteral->handle(), |
| 3860 | true, |
| 3861 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3862 | |
| 3863 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3864 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 3865 | if (overwrite_left) { |
| 3866 | overwrite_mode = OVERWRITE_LEFT; |
| 3867 | } else if (overwrite_right) { |
| 3868 | overwrite_mode = OVERWRITE_RIGHT; |
| 3869 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3870 | LoadAndSpill(node->left()); |
| 3871 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3872 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3873 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3874 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3875 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3876 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3877 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3881 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3882 | #ifdef DEBUG |
| 3883 | int original_height = frame_->height(); |
| 3884 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3885 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3886 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3887 | frame_->EmitPush(r0); |
| 3888 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3892 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3893 | #ifdef DEBUG |
| 3894 | int original_height = frame_->height(); |
| 3895 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3896 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3897 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3898 | |
| 3899 | // Get the expressions from the node. |
| 3900 | Expression* left = node->left(); |
| 3901 | Expression* right = node->right(); |
| 3902 | Token::Value op = node->op(); |
| 3903 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3904 | // To make null checks efficient, we check if either left or right is the |
| 3905 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3906 | // instead of calling the (very) general runtime routine for checking |
| 3907 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3908 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3909 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3910 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3911 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3912 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 3913 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3914 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3915 | LoadAndSpill(left_is_null ? right : left); |
| 3916 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3917 | __ cmp(r0, Operand(Factory::null_value())); |
| 3918 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3919 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 3920 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3921 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3922 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3923 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3924 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3925 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3926 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3927 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3928 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3929 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3930 | // It can be an undetectable object. |
| 3931 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3932 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 3933 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 3934 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3937 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3938 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3939 | return; |
| 3940 | } |
| 3941 | } |
| 3942 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3943 | // To make typeof testing for natives implemented in JavaScript really |
| 3944 | // efficient, we generate special code for expressions of the form: |
| 3945 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3946 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 3947 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 3948 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 3949 | (right->AsLiteral() != NULL && |
| 3950 | right->AsLiteral()->handle()->IsString())) { |
| 3951 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 3952 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3953 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3954 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3955 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3956 | |
| 3957 | if (check->Equals(Heap::number_symbol())) { |
| 3958 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3959 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3960 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3961 | __ cmp(r1, Operand(Factory::heap_number_map())); |
| 3962 | cc_reg_ = eq; |
| 3963 | |
| 3964 | } else if (check->Equals(Heap::string_symbol())) { |
| 3965 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3966 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3967 | |
| 3968 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3969 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3970 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3971 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3972 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3973 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3974 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3975 | |
| 3976 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3977 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 3978 | cc_reg_ = lt; |
| 3979 | |
| 3980 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 3981 | __ cmp(r1, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3982 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3983 | __ cmp(r1, Operand(Factory::false_value())); |
| 3984 | cc_reg_ = eq; |
| 3985 | |
| 3986 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 3987 | __ cmp(r1, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3988 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3989 | |
| 3990 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3991 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3992 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3993 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3994 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3995 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3996 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3997 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 3998 | |
| 3999 | cc_reg_ = eq; |
| 4000 | |
| 4001 | } else if (check->Equals(Heap::function_symbol())) { |
| 4002 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4003 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4004 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4005 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 4006 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 4007 | cc_reg_ = eq; |
| 4008 | |
| 4009 | } else if (check->Equals(Heap::object_symbol())) { |
| 4010 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4011 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4012 | |
| 4013 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4014 | __ cmp(r1, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4015 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4016 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4017 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4018 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 4019 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4020 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4021 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4022 | |
| 4023 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4024 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4025 | false_target()->Branch(lt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4026 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 4027 | cc_reg_ = le; |
| 4028 | |
| 4029 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4030 | // Uncommon case: typeof testing against a string literal that is |
| 4031 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4032 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4033 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4034 | ASSERT(!has_valid_frame() || |
| 4035 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4036 | return; |
| 4037 | } |
| 4038 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4039 | LoadAndSpill(left); |
| 4040 | LoadAndSpill(right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4041 | switch (op) { |
| 4042 | case Token::EQ: |
| 4043 | Comparison(eq, false); |
| 4044 | break; |
| 4045 | |
| 4046 | case Token::LT: |
| 4047 | Comparison(lt); |
| 4048 | break; |
| 4049 | |
| 4050 | case Token::GT: |
| 4051 | Comparison(gt); |
| 4052 | break; |
| 4053 | |
| 4054 | case Token::LTE: |
| 4055 | Comparison(le); |
| 4056 | break; |
| 4057 | |
| 4058 | case Token::GTE: |
| 4059 | Comparison(ge); |
| 4060 | break; |
| 4061 | |
| 4062 | case Token::EQ_STRICT: |
| 4063 | Comparison(eq, true); |
| 4064 | break; |
| 4065 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4066 | case Token::IN: { |
| 4067 | Result arg_count = allocator_->Allocate(r0); |
| 4068 | ASSERT(arg_count.is_valid()); |
| 4069 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 4070 | Result result = frame_->InvokeBuiltin(Builtins::IN, |
| 4071 | CALL_JS, |
| 4072 | &arg_count, |
| 4073 | 2); |
| 4074 | frame_->EmitPush(result.reg()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4075 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4076 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4077 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4078 | case Token::INSTANCEOF: { |
| 4079 | Result arg_count = allocator_->Allocate(r0); |
| 4080 | ASSERT(arg_count.is_valid()); |
| 4081 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 4082 | Result result = frame_->InvokeBuiltin(Builtins::INSTANCE_OF, |
| 4083 | CALL_JS, |
| 4084 | &arg_count, |
| 4085 | 2); |
| 4086 | __ tst(result.reg(), Operand(result.reg())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4087 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4088 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4089 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4090 | |
| 4091 | default: |
| 4092 | UNREACHABLE(); |
| 4093 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4094 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4095 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4099 | #ifdef DEBUG |
| 4100 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4101 | #endif |
| 4102 | |
| 4103 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4104 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4105 | #define __ ACCESS_MASM(masm) |
| 4106 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4107 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4108 | Handle<String> Reference::GetName() { |
| 4109 | ASSERT(type_ == NAMED); |
| 4110 | Property* property = expression_->AsProperty(); |
| 4111 | if (property == NULL) { |
| 4112 | // Global variable reference treated as a named property reference. |
| 4113 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4114 | ASSERT(proxy->AsVariable() != NULL); |
| 4115 | ASSERT(proxy->AsVariable()->is_global()); |
| 4116 | return proxy->name(); |
| 4117 | } else { |
| 4118 | Literal* raw_name = property->key()->AsLiteral(); |
| 4119 | ASSERT(raw_name != NULL); |
| 4120 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4121 | } |
| 4122 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4123 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4124 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4125 | void Reference::GetValueAndSpill(TypeofState typeof_state) { |
| 4126 | ASSERT(cgen_->in_spilled_code()); |
| 4127 | cgen_->set_in_spilled_code(false); |
| 4128 | GetValue(typeof_state); |
| 4129 | cgen_->frame()->SpillAll(); |
| 4130 | cgen_->set_in_spilled_code(true); |
| 4131 | } |
| 4132 | |
| 4133 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4134 | void Reference::GetValue(TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4135 | ASSERT(!cgen_->in_spilled_code()); |
| 4136 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4137 | ASSERT(!is_illegal()); |
| 4138 | ASSERT(!cgen_->has_cc()); |
| 4139 | MacroAssembler* masm = cgen_->masm(); |
| 4140 | Property* property = expression_->AsProperty(); |
| 4141 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4142 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | switch (type_) { |
| 4146 | case SLOT: { |
| 4147 | Comment cmnt(masm, "[ Load from Slot"); |
| 4148 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4149 | ASSERT(slot != NULL); |
| 4150 | cgen_->LoadFromSlot(slot, typeof_state); |
| 4151 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4154 | case NAMED: { |
| 4155 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4156 | // distinction between expressions in a typeof and not in a typeof. If |
| 4157 | // there is a chance that reference errors can be thrown below, we |
| 4158 | // must distinguish between the two kinds of loads (typeof expression |
| 4159 | // loads must not throw a reference error). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4160 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4161 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4162 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4163 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4164 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4165 | // Setup the name register. |
| 4166 | Result name_reg = cgen_->allocator()->Allocate(r2); |
| 4167 | ASSERT(name_reg.is_valid()); |
| 4168 | __ mov(name_reg.reg(), Operand(name)); |
| 4169 | ASSERT(var == NULL || var->is_global()); |
| 4170 | RelocInfo::Mode rmode = (var == NULL) |
| 4171 | ? RelocInfo::CODE_TARGET |
| 4172 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4173 | Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4174 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4175 | break; |
| 4176 | } |
| 4177 | |
| 4178 | case KEYED: { |
| 4179 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4180 | // 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] | 4181 | |
| 4182 | // TODO(181): Implement inlined version of array indexing once |
| 4183 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4184 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4185 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4186 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4187 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4188 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4189 | ASSERT(var == NULL || var->is_global()); |
| 4190 | RelocInfo::Mode rmode = (var == NULL) |
| 4191 | ? RelocInfo::CODE_TARGET |
| 4192 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4193 | Result answer = frame->CallCodeObject(ic, rmode, 0); |
| 4194 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4195 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | default: |
| 4199 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4204 | void Reference::SetValue(InitState init_state) { |
| 4205 | ASSERT(!is_illegal()); |
| 4206 | ASSERT(!cgen_->has_cc()); |
| 4207 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4208 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4209 | Property* property = expression_->AsProperty(); |
| 4210 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4211 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4212 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4213 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4214 | switch (type_) { |
| 4215 | case SLOT: { |
| 4216 | Comment cmnt(masm, "[ Store to Slot"); |
| 4217 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4218 | ASSERT(slot != NULL); |
| 4219 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4220 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4221 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4222 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4223 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4224 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4225 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4226 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4227 | if (init_state == CONST_INIT) { |
| 4228 | // Same as the case for a normal store, but ignores attribute |
| 4229 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4230 | // const properties (introduced via eval("const foo = (some |
| 4231 | // expr);")). Also, uses the current function context instead of |
| 4232 | // the top context. |
| 4233 | // |
| 4234 | // Note that we must declare the foo upon entry of eval(), via a |
| 4235 | // context slot declaration, but we cannot initialize it at the |
| 4236 | // same time, because the const declaration may be at the end of |
| 4237 | // the eval code (sigh...) and the const variable may have been |
| 4238 | // used before (where its value is 'undefined'). Thus, we can only |
| 4239 | // do the initialization when we actually encounter the expression |
| 4240 | // and when the expression operands are defined and valid, and |
| 4241 | // thus we need the split into 2 operations: declaration of the |
| 4242 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4243 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4244 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4245 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4246 | } |
| 4247 | // Storing a variable must keep the (new) value on the expression |
| 4248 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4249 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4250 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4251 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4252 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4253 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4254 | JumpTarget exit; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4255 | if (init_state == CONST_INIT) { |
| 4256 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4257 | // Only the first const initialization must be executed (the slot |
| 4258 | // still contains 'the hole' value). When the assignment is |
| 4259 | // executed, the code is identical to a normal store (see below). |
| 4260 | Comment cmnt(masm, "[ Init const"); |
| 4261 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
| 4262 | __ cmp(r2, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4263 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4264 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4265 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4266 | // We must execute the store. Storing a variable must keep the |
| 4267 | // (new) value on the stack. This is necessary for compiling |
| 4268 | // assignment expressions. |
| 4269 | // |
| 4270 | // Note: We will reach here even with slot->var()->mode() == |
| 4271 | // Variable::CONST because of const declarations which will |
| 4272 | // initialize consts to 'the hole' value and by doing so, end up |
| 4273 | // calling this code. r2 may be loaded with context; used below in |
| 4274 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4275 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4276 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4277 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4278 | if (slot->type() == Slot::CONTEXT) { |
| 4279 | // Skip write barrier if the written value is a smi. |
| 4280 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4281 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4282 | // r2 is loaded with context when calling SlotOperand above. |
| 4283 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4284 | __ mov(r3, Operand(offset)); |
| 4285 | __ RecordWrite(r2, r3, r1); |
| 4286 | } |
| 4287 | // If we definitely did not jump over the assignment, we do not need |
| 4288 | // to bind the exit label. Doing so can defeat peephole |
| 4289 | // optimization. |
| 4290 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4291 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4292 | } |
| 4293 | } |
| 4294 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4297 | case NAMED: { |
| 4298 | Comment cmnt(masm, "[ Store to named Property"); |
| 4299 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4300 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4301 | Handle<String> name(GetName()); |
| 4302 | |
| 4303 | Result value = cgen_->allocator()->Allocate(r0); |
| 4304 | ASSERT(value.is_valid()); |
| 4305 | frame->EmitPop(value.reg()); |
| 4306 | |
| 4307 | // Setup the name register. |
| 4308 | Result property_name = cgen_->allocator()->Allocate(r2); |
| 4309 | ASSERT(property_name.is_valid()); |
| 4310 | __ mov(property_name.reg(), Operand(name)); |
| 4311 | Result answer = frame->CallCodeObject(ic, |
| 4312 | RelocInfo::CODE_TARGET, |
| 4313 | &value, |
| 4314 | &property_name, |
| 4315 | 0); |
| 4316 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4317 | break; |
| 4318 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4319 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4320 | case KEYED: { |
| 4321 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4322 | Property* property = expression_->AsProperty(); |
| 4323 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4324 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4325 | |
| 4326 | // Call IC code. |
| 4327 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4328 | // TODO(1222589): Make the IC grab the values from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4329 | Result value = cgen_->allocator()->Allocate(r0); |
| 4330 | ASSERT(value.is_valid()); |
| 4331 | frame->EmitPop(value.reg()); // value |
| 4332 | Result result = |
| 4333 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4334 | frame->EmitPush(result.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4335 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4336 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4337 | |
| 4338 | default: |
| 4339 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4340 | } |
| 4341 | } |
| 4342 | |
| 4343 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4344 | static void AllocateHeapNumber( |
| 4345 | MacroAssembler* masm, |
| 4346 | Label* need_gc, // Jump here if young space is full. |
| 4347 | Register result_reg, // The tagged address of the new heap number. |
| 4348 | Register allocation_top_addr_reg, // A scratch register. |
| 4349 | Register scratch2) { // Another scratch register. |
| 4350 | ExternalReference allocation_top = |
| 4351 | ExternalReference::new_space_allocation_top_address(); |
| 4352 | ExternalReference allocation_limit = |
| 4353 | ExternalReference::new_space_allocation_limit_address(); |
| 4354 | |
| 4355 | // allocat := the address of the allocation top variable. |
| 4356 | __ mov(allocation_top_addr_reg, Operand(allocation_top)); |
| 4357 | // result_reg := the old allocation top. |
| 4358 | __ ldr(result_reg, MemOperand(allocation_top_addr_reg)); |
| 4359 | // scratch2 := the address of the allocation limit. |
| 4360 | __ mov(scratch2, Operand(allocation_limit)); |
| 4361 | // scratch2 := the allocation limit. |
| 4362 | __ ldr(scratch2, MemOperand(scratch2)); |
| 4363 | // result_reg := the new allocation top. |
| 4364 | __ add(result_reg, result_reg, Operand(HeapNumber::kSize)); |
| 4365 | // Compare new new allocation top and limit. |
| 4366 | __ cmp(result_reg, Operand(scratch2)); |
| 4367 | // Branch if out of space in young generation. |
| 4368 | __ b(hi, need_gc); |
| 4369 | // Store new allocation top. |
| 4370 | __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top |
| 4371 | // Tag and adjust back to start of new object. |
| 4372 | __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag)); |
| 4373 | // Get heap number map into scratch2. |
| 4374 | __ mov(scratch2, Operand(Factory::heap_number_map())); |
| 4375 | // Store heap number map in new object. |
| 4376 | __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset)); |
| 4377 | } |
| 4378 | |
| 4379 | |
| 4380 | // We fall into this code if the operands were Smis, but the result was |
| 4381 | // not (eg. overflow). We branch into this code (to the not_smi label) if |
| 4382 | // the operands were not both Smi. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4383 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 4384 | Label* not_smi, |
| 4385 | const Builtins::JavaScript& builtin, |
| 4386 | Token::Value operation, |
| 4387 | int swi_number, |
| 4388 | OverwriteMode mode) { |
| 4389 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4390 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4391 | __ push(r1); |
| 4392 | __ push(r0); |
| 4393 | __ mov(r0, Operand(1)); // Set number of arguments. |
| 4394 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4395 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4396 | __ bind(not_smi); |
| 4397 | __ tst(r0, Operand(kSmiTagMask)); |
| 4398 | __ b(eq, &slow); // We can't handle a Smi-double combination yet. |
| 4399 | __ tst(r1, Operand(kSmiTagMask)); |
| 4400 | __ b(eq, &slow); // We can't handle a Smi-double combination yet. |
| 4401 | // Get map of r0 into r2. |
| 4402 | __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4403 | // Get type of r0 into r3. |
| 4404 | __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4405 | __ cmp(r3, Operand(HEAP_NUMBER_TYPE)); |
| 4406 | __ b(ne, &slow); |
| 4407 | // Get type of r1 into r3. |
| 4408 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4409 | // Check they are both the same map (heap number map). |
| 4410 | __ cmp(r2, r3); |
| 4411 | __ b(ne, &slow); |
| 4412 | // Both are doubles. |
| 4413 | // Calling convention says that second double is in r2 and r3. |
| 4414 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4415 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4416 | |
| 4417 | if (mode == NO_OVERWRITE) { |
| 4418 | // Get address of new heap number into r5. |
| 4419 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4420 | __ push(lr); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4421 | __ push(r5); |
| 4422 | } else if (mode == OVERWRITE_LEFT) { |
| 4423 | __ push(lr); |
| 4424 | __ push(r1); |
| 4425 | } else { |
| 4426 | ASSERT(mode == OVERWRITE_RIGHT); |
| 4427 | __ push(lr); |
| 4428 | __ push(r0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4429 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4430 | // Calling convention says that first double is in r0 and r1. |
| 4431 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4432 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4433 | // Call C routine that may not cause GC or other trouble. |
| 4434 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
| 4435 | #if !defined(__arm__) |
| 4436 | // Notify the simulator that we are calling an add routine in C. |
| 4437 | __ swi(swi_number); |
| 4438 | #else |
| 4439 | // Actually call the add routine written in C. |
| 4440 | __ Call(r5); |
| 4441 | #endif |
| 4442 | // Store answer in the overwritable heap number. |
| 4443 | __ pop(r4); |
| 4444 | #if !defined(__ARM_EABI__) && defined(__arm__) |
| 4445 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 4446 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 4447 | // substract the tag from r4. |
| 4448 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 4449 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 4450 | #else |
| 4451 | // Double returned in fp coprocessor register 0 and 1. |
| 4452 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
| 4453 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + kPointerSize)); |
| 4454 | #endif |
| 4455 | __ mov(r0, Operand(r4)); |
| 4456 | // And we are done. |
| 4457 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | |
| 4461 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 4462 | // r1 : x |
| 4463 | // r0 : y |
| 4464 | // result : r0 |
| 4465 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4466 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 4467 | // tell us that. |
| 4468 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4469 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4470 | switch (op_) { |
| 4471 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4472 | Label not_smi; |
| 4473 | // Fast path. |
| 4474 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4475 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4476 | __ b(ne, ¬_smi); |
| 4477 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 4478 | // Return if no overflow. |
| 4479 | __ Ret(vc); |
| 4480 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 4481 | |
| 4482 | HandleBinaryOpSlowCases(masm, |
| 4483 | ¬_smi, |
| 4484 | Builtins::ADD, |
| 4485 | Token::ADD, |
| 4486 | assembler::arm::simulator_fp_add, |
| 4487 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4488 | break; |
| 4489 | } |
| 4490 | |
| 4491 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4492 | Label not_smi; |
| 4493 | // Fast path. |
| 4494 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4495 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4496 | __ b(ne, ¬_smi); |
| 4497 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 4498 | // Return if no overflow. |
| 4499 | __ Ret(vc); |
| 4500 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 4501 | |
| 4502 | HandleBinaryOpSlowCases(masm, |
| 4503 | ¬_smi, |
| 4504 | Builtins::SUB, |
| 4505 | Token::SUB, |
| 4506 | assembler::arm::simulator_fp_sub, |
| 4507 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4508 | break; |
| 4509 | } |
| 4510 | |
| 4511 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4512 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4513 | ASSERT(kSmiTag == 0); // adjust code below |
| 4514 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4515 | __ b(ne, ¬_smi); |
| 4516 | // 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] | 4517 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4518 | // Do multiplication |
| 4519 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 4520 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4521 | __ mov(ip, Operand(r3, ASR, 31)); |
| 4522 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 4523 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4524 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4525 | __ tst(r3, Operand(r3)); |
| 4526 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4527 | __ Ret(ne); |
| 4528 | // Slow case. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4529 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4530 | |
| 4531 | HandleBinaryOpSlowCases(masm, |
| 4532 | ¬_smi, |
| 4533 | Builtins::MUL, |
| 4534 | Token::MUL, |
| 4535 | assembler::arm::simulator_fp_mul, |
| 4536 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4537 | break; |
| 4538 | } |
| 4539 | |
| 4540 | case Token::BIT_OR: |
| 4541 | case Token::BIT_AND: |
| 4542 | case Token::BIT_XOR: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4543 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4544 | ASSERT(kSmiTag == 0); // adjust code below |
| 4545 | __ tst(r2, Operand(kSmiTagMask)); |
| 4546 | __ b(ne, &slow); |
| 4547 | switch (op_) { |
| 4548 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 4549 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 4550 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
| 4551 | default: UNREACHABLE(); |
| 4552 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4553 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4554 | __ bind(&slow); |
| 4555 | __ push(r1); // restore stack |
| 4556 | __ push(r0); |
| 4557 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 4558 | switch (op_) { |
| 4559 | case Token::BIT_OR: |
| 4560 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 4561 | break; |
| 4562 | case Token::BIT_AND: |
| 4563 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 4564 | break; |
| 4565 | case Token::BIT_XOR: |
| 4566 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 4567 | break; |
| 4568 | default: |
| 4569 | UNREACHABLE(); |
| 4570 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4571 | break; |
| 4572 | } |
| 4573 | |
| 4574 | case Token::SHL: |
| 4575 | case Token::SHR: |
| 4576 | case Token::SAR: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4577 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4578 | ASSERT(kSmiTag == 0); // adjust code below |
| 4579 | __ tst(r2, Operand(kSmiTagMask)); |
| 4580 | __ b(ne, &slow); |
| 4581 | // remove tags from operands (but keep sign) |
| 4582 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 4583 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 4584 | // use only the 5 least significant bits of the shift count |
| 4585 | __ and_(r2, r2, Operand(0x1f)); |
| 4586 | // perform operation |
| 4587 | switch (op_) { |
| 4588 | case Token::SAR: |
| 4589 | __ mov(r3, Operand(r3, ASR, r2)); |
| 4590 | // no checks of result necessary |
| 4591 | break; |
| 4592 | |
| 4593 | case Token::SHR: |
| 4594 | __ mov(r3, Operand(r3, LSR, r2)); |
| 4595 | // check that the *unsigned* result fits in a smi |
| 4596 | // neither of the two high-order bits can be set: |
| 4597 | // - 0x80000000: high bit would be lost when smi tagging |
| 4598 | // - 0x40000000: this number would convert to negative when |
| 4599 | // smi tagging these two cases can only happen with shifts |
| 4600 | // by 0 or 1 when handed a valid smi |
| 4601 | __ and_(r2, r3, Operand(0xc0000000), SetCC); |
| 4602 | __ b(ne, &slow); |
| 4603 | break; |
| 4604 | |
| 4605 | case Token::SHL: |
| 4606 | __ mov(r3, Operand(r3, LSL, r2)); |
| 4607 | // check that the *signed* result fits in a smi |
| 4608 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 4609 | __ b(mi, &slow); |
| 4610 | break; |
| 4611 | |
| 4612 | default: UNREACHABLE(); |
| 4613 | } |
| 4614 | // tag result and store it in r0 |
| 4615 | ASSERT(kSmiTag == 0); // adjust code below |
| 4616 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4617 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4618 | // slow case |
| 4619 | __ bind(&slow); |
| 4620 | __ push(r1); // restore stack |
| 4621 | __ push(r0); |
| 4622 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 4623 | switch (op_) { |
| 4624 | case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break; |
| 4625 | case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break; |
| 4626 | case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break; |
| 4627 | default: UNREACHABLE(); |
| 4628 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4629 | break; |
| 4630 | } |
| 4631 | |
| 4632 | default: UNREACHABLE(); |
| 4633 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4634 | // This code should be unreachable. |
| 4635 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | |
| 4639 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 4640 | Label within_limit; |
| 4641 | __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit())); |
| 4642 | __ ldr(ip, MemOperand(ip)); |
| 4643 | __ cmp(sp, Operand(ip)); |
| 4644 | __ b(hs, &within_limit); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4645 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 4646 | // argument, so give it a Smi. |
| 4647 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4648 | __ push(r0); |
| 4649 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1); |
| 4650 | __ bind(&within_limit); |
| 4651 | |
| 4652 | __ StubReturn(1); |
| 4653 | } |
| 4654 | |
| 4655 | |
| 4656 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 4657 | Label undo; |
| 4658 | Label slow; |
| 4659 | Label done; |
| 4660 | |
| 4661 | // Enter runtime system if the value is not a smi. |
| 4662 | __ tst(r0, Operand(kSmiTagMask)); |
| 4663 | __ b(ne, &slow); |
| 4664 | |
| 4665 | // Enter runtime system if the value of the expression is zero |
| 4666 | // to make sure that we switch between 0 and -0. |
| 4667 | __ cmp(r0, Operand(0)); |
| 4668 | __ b(eq, &slow); |
| 4669 | |
| 4670 | // The value of the expression is a smi that is not zero. Try |
| 4671 | // optimistic subtraction '0 - value'. |
| 4672 | __ rsb(r1, r0, Operand(0), SetCC); |
| 4673 | __ b(vs, &slow); |
| 4674 | |
| 4675 | // If result is a smi we are done. |
| 4676 | __ tst(r1, Operand(kSmiTagMask)); |
| 4677 | __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result |
| 4678 | __ b(eq, &done); |
| 4679 | |
| 4680 | // Enter runtime system. |
| 4681 | __ bind(&slow); |
| 4682 | __ push(r0); |
| 4683 | __ mov(r0, Operand(0)); // set number of arguments |
| 4684 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 4685 | |
| 4686 | __ bind(&done); |
| 4687 | __ StubReturn(1); |
| 4688 | } |
| 4689 | |
| 4690 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4691 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
| 4692 | // r0 holds exception |
| 4693 | ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code |
| 4694 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 4695 | __ ldr(sp, MemOperand(r3)); |
| 4696 | __ pop(r2); // pop next in chain |
| 4697 | __ str(r2, MemOperand(r3)); |
| 4698 | // restore parameter- and frame-pointer and pop state. |
| 4699 | __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit()); |
| 4700 | // Before returning we restore the context from the frame pointer if not NULL. |
| 4701 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 4702 | __ cmp(fp, Operand(0)); |
| 4703 | // Set cp to NULL if fp is NULL. |
| 4704 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 4705 | // Restore cp otherwise. |
| 4706 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4707 | #ifdef DEBUG |
| 4708 | if (FLAG_debug_code) { |
| 4709 | __ mov(lr, Operand(pc)); |
| 4710 | } |
| 4711 | #endif |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4712 | __ pop(pc); |
| 4713 | } |
| 4714 | |
| 4715 | |
| 4716 | void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) { |
| 4717 | // Fetch top stack handler. |
| 4718 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 4719 | __ ldr(r3, MemOperand(r3)); |
| 4720 | |
| 4721 | // Unwind the handlers until the ENTRY handler is found. |
| 4722 | Label loop, done; |
| 4723 | __ bind(&loop); |
| 4724 | // Load the type of the current stack handler. |
| 4725 | const int kStateOffset = StackHandlerConstants::kAddressDisplacement + |
| 4726 | StackHandlerConstants::kStateOffset; |
| 4727 | __ ldr(r2, MemOperand(r3, kStateOffset)); |
| 4728 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 4729 | __ b(eq, &done); |
| 4730 | // Fetch the next handler in the list. |
| 4731 | const int kNextOffset = StackHandlerConstants::kAddressDisplacement + |
| 4732 | StackHandlerConstants::kNextOffset; |
| 4733 | __ ldr(r3, MemOperand(r3, kNextOffset)); |
| 4734 | __ jmp(&loop); |
| 4735 | __ bind(&done); |
| 4736 | |
| 4737 | // Set the top handler address to next handler past the current ENTRY handler. |
| 4738 | __ ldr(r0, MemOperand(r3, kNextOffset)); |
| 4739 | __ mov(r2, Operand(ExternalReference(Top::k_handler_address))); |
| 4740 | __ str(r0, MemOperand(r2)); |
| 4741 | |
| 4742 | // Set external caught exception to false. |
| 4743 | __ mov(r0, Operand(false)); |
| 4744 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 4745 | __ mov(r2, Operand(external_caught)); |
| 4746 | __ str(r0, MemOperand(r2)); |
| 4747 | |
| 4748 | // Set pending exception and r0 to out of memory exception. |
| 4749 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 4750 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 4751 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 4752 | __ str(r0, MemOperand(r2)); |
| 4753 | |
| 4754 | // Restore the stack to the address of the ENTRY handler |
| 4755 | __ mov(sp, Operand(r3)); |
| 4756 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4757 | // Stack layout at this point. See also PushTryHandler |
| 4758 | // r3, sp -> next handler |
| 4759 | // state (ENTRY) |
| 4760 | // pp |
| 4761 | // fp |
| 4762 | // lr |
| 4763 | |
| 4764 | // Discard ENTRY state (r2 is not used), and restore parameter- |
| 4765 | // and frame-pointer and pop state. |
| 4766 | __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4767 | // Before returning we restore the context from the frame pointer if not NULL. |
| 4768 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 4769 | __ cmp(fp, Operand(0)); |
| 4770 | // Set cp to NULL if fp is NULL. |
| 4771 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 4772 | // Restore cp otherwise. |
| 4773 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4774 | #ifdef DEBUG |
| 4775 | if (FLAG_debug_code) { |
| 4776 | __ mov(lr, Operand(pc)); |
| 4777 | } |
| 4778 | #endif |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4779 | __ pop(pc); |
| 4780 | } |
| 4781 | |
| 4782 | |
| 4783 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 4784 | Label* throw_normal_exception, |
| 4785 | Label* throw_out_of_memory_exception, |
| 4786 | StackFrame::Type frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4787 | bool do_gc, |
| 4788 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4789 | // r0: result parameter for PerformGC, if any |
| 4790 | // r4: number of arguments including receiver (C callee-saved) |
| 4791 | // r5: pointer to builtin function (C callee-saved) |
| 4792 | // r6: pointer to the first argument (C callee-saved) |
| 4793 | |
| 4794 | if (do_gc) { |
| 4795 | // Passing r0. |
| 4796 | __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY); |
| 4797 | } |
| 4798 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4799 | ExternalReference scope_depth = |
| 4800 | ExternalReference::heap_always_allocate_scope_depth(); |
| 4801 | if (always_allocate) { |
| 4802 | __ mov(r0, Operand(scope_depth)); |
| 4803 | __ ldr(r1, MemOperand(r0)); |
| 4804 | __ add(r1, r1, Operand(1)); |
| 4805 | __ str(r1, MemOperand(r0)); |
| 4806 | } |
| 4807 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4808 | // Call C built-in. |
| 4809 | // r0 = argc, r1 = argv |
| 4810 | __ mov(r0, Operand(r4)); |
| 4811 | __ mov(r1, Operand(r6)); |
| 4812 | |
| 4813 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 4814 | // frames, we need to know where the return address is. Right now, |
| 4815 | // we push it on the stack to be able to find it again, but we never |
| 4816 | // restore from it in case of changes, which makes it impossible to |
| 4817 | // support moving the C entry code stub. This should be fixed, but currently |
| 4818 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 4819 | // sequence that it is not moving ever. |
| 4820 | __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 4821 | __ push(lr); |
| 4822 | #if !defined(__arm__) |
| 4823 | // Notify the simulator of the transition to C code. |
| 4824 | __ swi(assembler::arm::call_rt_r5); |
| 4825 | #else /* !defined(__arm__) */ |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4826 | __ Jump(r5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4827 | #endif /* !defined(__arm__) */ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4828 | |
| 4829 | if (always_allocate) { |
| 4830 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 4831 | // though (contain the result). |
| 4832 | __ mov(r2, Operand(scope_depth)); |
| 4833 | __ ldr(r3, MemOperand(r2)); |
| 4834 | __ sub(r3, r3, Operand(1)); |
| 4835 | __ str(r3, MemOperand(r2)); |
| 4836 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4837 | |
| 4838 | // check for failure result |
| 4839 | Label failure_returned; |
| 4840 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 4841 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 4842 | __ add(r2, r0, Operand(1)); |
| 4843 | __ tst(r2, Operand(kFailureTagMask)); |
| 4844 | __ b(eq, &failure_returned); |
| 4845 | |
| 4846 | // Exit C frame and return. |
| 4847 | // r0:r1: result |
| 4848 | // sp: stack pointer |
| 4849 | // fp: frame pointer |
| 4850 | // pp: caller's parameter pointer pp (restored as C callee-saved) |
| 4851 | __ LeaveExitFrame(frame_type); |
| 4852 | |
| 4853 | // check if we should retry or throw exception |
| 4854 | Label retry; |
| 4855 | __ bind(&failure_returned); |
| 4856 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 4857 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 4858 | __ b(eq, &retry); |
| 4859 | |
| 4860 | Label continue_exception; |
| 4861 | // If the returned failure is EXCEPTION then promote Top::pending_exception(). |
| 4862 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 4863 | __ b(ne, &continue_exception); |
| 4864 | |
| 4865 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4866 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4867 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4868 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4869 | __ ldr(r0, MemOperand(ip)); |
| 4870 | __ str(r3, MemOperand(ip)); |
| 4871 | |
| 4872 | __ bind(&continue_exception); |
| 4873 | // Special handling of out of memory exception. |
| 4874 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 4875 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 4876 | __ b(eq, throw_out_of_memory_exception); |
| 4877 | |
| 4878 | // Handle normal exception. |
| 4879 | __ jmp(throw_normal_exception); |
| 4880 | |
| 4881 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 4882 | } |
| 4883 | |
| 4884 | |
| 4885 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 4886 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 4887 | // r0: number of arguments including receiver |
| 4888 | // r1: pointer to builtin function |
| 4889 | // fp: frame pointer (restored after C call) |
| 4890 | // sp: stack pointer (restored as callee's pp after C call) |
| 4891 | // cp: current context (C callee-saved) |
| 4892 | // pp: caller's parameter pointer pp (C callee-saved) |
| 4893 | |
| 4894 | // NOTE: Invocations of builtins may return failure objects |
| 4895 | // instead of a proper result. The builtin entry handles |
| 4896 | // this by performing a garbage collection and retrying the |
| 4897 | // builtin once. |
| 4898 | |
| 4899 | StackFrame::Type frame_type = is_debug_break |
| 4900 | ? StackFrame::EXIT_DEBUG |
| 4901 | : StackFrame::EXIT; |
| 4902 | |
| 4903 | // Enter the exit frame that transitions from JavaScript to C++. |
| 4904 | __ EnterExitFrame(frame_type); |
| 4905 | |
| 4906 | // r4: number of arguments (C callee-saved) |
| 4907 | // r5: pointer to builtin function (C callee-saved) |
| 4908 | // r6: pointer to first argument (C callee-saved) |
| 4909 | |
| 4910 | Label throw_out_of_memory_exception; |
| 4911 | Label throw_normal_exception; |
| 4912 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4913 | // Call into the runtime system. Collect garbage before the call if |
| 4914 | // running with --gc-greedy set. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4915 | if (FLAG_gc_greedy) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4916 | Failure* failure = Failure::RetryAfterGC(0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4917 | __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure))); |
| 4918 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4919 | GenerateCore(masm, &throw_normal_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4920 | &throw_out_of_memory_exception, |
| 4921 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4922 | FLAG_gc_greedy, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4923 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4924 | |
| 4925 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4926 | GenerateCore(masm, |
| 4927 | &throw_normal_exception, |
| 4928 | &throw_out_of_memory_exception, |
| 4929 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4930 | true, |
| 4931 | false); |
| 4932 | |
| 4933 | // Do full GC and retry runtime call one final time. |
| 4934 | Failure* failure = Failure::InternalError(); |
| 4935 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 4936 | GenerateCore(masm, |
| 4937 | &throw_normal_exception, |
| 4938 | &throw_out_of_memory_exception, |
| 4939 | frame_type, |
| 4940 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4941 | true); |
| 4942 | |
| 4943 | __ bind(&throw_out_of_memory_exception); |
| 4944 | GenerateThrowOutOfMemory(masm); |
| 4945 | // control flow for generated will not return. |
| 4946 | |
| 4947 | __ bind(&throw_normal_exception); |
| 4948 | GenerateThrowTOS(masm); |
| 4949 | } |
| 4950 | |
| 4951 | |
| 4952 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 4953 | // r0: code entry |
| 4954 | // r1: function |
| 4955 | // r2: receiver |
| 4956 | // r3: argc |
| 4957 | // [sp+0]: argv |
| 4958 | |
| 4959 | Label invoke, exit; |
| 4960 | |
| 4961 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 4962 | // No need to save register-passed args |
| 4963 | // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr |
| 4964 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 4965 | |
| 4966 | // Get address of argv, see stm above. |
| 4967 | // r0: code entry |
| 4968 | // r1: function |
| 4969 | // r2: receiver |
| 4970 | // r3: argc |
| 4971 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 4972 | __ ldr(r4, MemOperand(r4)); // argv |
| 4973 | |
| 4974 | // Push a frame with special values setup to mark it as an entry frame. |
| 4975 | // r0: code entry |
| 4976 | // r1: function |
| 4977 | // r2: receiver |
| 4978 | // r3: argc |
| 4979 | // r4: argv |
| 4980 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 4981 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 4982 | __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL)); |
| 4983 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 4984 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 4985 | __ ldr(r5, MemOperand(r5)); |
| 4986 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 4987 | |
| 4988 | // Setup frame pointer for the frame to be pushed. |
| 4989 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 4990 | |
| 4991 | // Call a faked try-block that does the invoke. |
| 4992 | __ bl(&invoke); |
| 4993 | |
| 4994 | // Caught exception: Store result (exception) in the pending |
| 4995 | // exception field in the JSEnv and return a failure sentinel. |
| 4996 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 4997 | // 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] | 4998 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4999 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5000 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5001 | __ b(&exit); |
| 5002 | |
| 5003 | // Invoke: Link this frame into the handler chain. |
| 5004 | __ bind(&invoke); |
| 5005 | // Must preserve r0-r4, r5-r7 are available. |
| 5006 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
| 5007 | // If an exception not caught by another handler occurs, this handler returns |
| 5008 | // control to the code after the bl(&invoke) above, which restores all |
| 5009 | // kCalleeSaved registers (including cp, pp and fp) to their saved values |
| 5010 | // before returning a failure to C. |
| 5011 | |
| 5012 | // Clear any pending exceptions. |
| 5013 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 5014 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5015 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5016 | __ str(r5, MemOperand(ip)); |
| 5017 | |
| 5018 | // Invoke the function by calling through JS entry trampoline builtin. |
| 5019 | // Notice that we cannot store a reference to the trampoline code directly in |
| 5020 | // this stub, because runtime stubs are not traversed when doing GC. |
| 5021 | |
| 5022 | // Expected registers by Builtins::JSEntryTrampoline |
| 5023 | // r0: code entry |
| 5024 | // r1: function |
| 5025 | // r2: receiver |
| 5026 | // r3: argc |
| 5027 | // r4: argv |
| 5028 | if (is_construct) { |
| 5029 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 5030 | __ mov(ip, Operand(construct_entry)); |
| 5031 | } else { |
| 5032 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 5033 | __ mov(ip, Operand(entry)); |
| 5034 | } |
| 5035 | __ ldr(ip, MemOperand(ip)); // deref address |
| 5036 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5037 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 5038 | // macro for the add instruction because we don't want the coverage tool |
| 5039 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5040 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5041 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5042 | |
| 5043 | // Unlink this frame from the handler chain. When reading the |
| 5044 | // address of the next handler, there is no need to use the address |
| 5045 | // displacement since the current stack pointer (sp) points directly |
| 5046 | // to the stack handler. |
| 5047 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 5048 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 5049 | __ str(r3, MemOperand(ip)); |
| 5050 | // No need to restore registers |
| 5051 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 5052 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5053 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5054 | __ bind(&exit); // r0 holds result |
| 5055 | // Restore the top frame descriptors from the stack. |
| 5056 | __ pop(r3); |
| 5057 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5058 | __ str(r3, MemOperand(ip)); |
| 5059 | |
| 5060 | // Reset the stack to the callee saved registers. |
| 5061 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5062 | |
| 5063 | // Restore callee-saved registers and return. |
| 5064 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5065 | if (FLAG_debug_code) { |
| 5066 | __ mov(lr, Operand(pc)); |
| 5067 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5068 | #endif |
| 5069 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 5070 | } |
| 5071 | |
| 5072 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5073 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5074 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5075 | Label adaptor; |
| 5076 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5077 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5078 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5079 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5080 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5081 | // Nothing to do: The formal number of parameters has already been |
| 5082 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5083 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5084 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5085 | // Arguments adaptor case: Read the arguments length from the |
| 5086 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5087 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5088 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5089 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5093 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 5094 | // The displacement is the offset of the last parameter (if any) |
| 5095 | // relative to the frame pointer. |
| 5096 | static const int kDisplacement = |
| 5097 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5098 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5099 | // Check that the key is a smi. |
| 5100 | Label slow; |
| 5101 | __ tst(r1, Operand(kSmiTagMask)); |
| 5102 | __ b(ne, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5103 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5104 | // Check if the calling frame is an arguments adaptor frame. |
| 5105 | Label adaptor; |
| 5106 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5107 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5108 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5109 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5110 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5111 | // Check index against formal parameters count limit passed in |
| 5112 | // through register eax. Use unsigned comparison to get negative |
| 5113 | // check for free. |
| 5114 | __ cmp(r1, r0); |
| 5115 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5116 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5117 | // Read the argument from the stack and return it. |
| 5118 | __ sub(r3, r0, r1); |
| 5119 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5120 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5121 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5122 | |
| 5123 | // Arguments adaptor case: Check index against actual arguments |
| 5124 | // limit found in the arguments adaptor frame. Use unsigned |
| 5125 | // comparison to get negative check for free. |
| 5126 | __ bind(&adaptor); |
| 5127 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5128 | __ cmp(r1, r0); |
| 5129 | __ b(cs, &slow); |
| 5130 | |
| 5131 | // Read the argument from the adaptor frame and return it. |
| 5132 | __ sub(r3, r0, r1); |
| 5133 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5134 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5135 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5136 | |
| 5137 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 5138 | // by calling the runtime system. |
| 5139 | __ bind(&slow); |
| 5140 | __ push(r1); |
| 5141 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1); |
| 5142 | } |
| 5143 | |
| 5144 | |
| 5145 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 5146 | // Check if the calling frame is an arguments adaptor frame. |
| 5147 | Label runtime; |
| 5148 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5149 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5150 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5151 | __ b(ne, &runtime); |
| 5152 | |
| 5153 | // Patch the arguments.length and the parameters pointer. |
| 5154 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5155 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 5156 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5157 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 5158 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 5159 | |
| 5160 | // Do the runtime call to allocate the arguments object. |
| 5161 | __ bind(&runtime); |
| 5162 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
| 5165 | |
| 5166 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 5167 | Label slow; |
| 5168 | // Get the function to call from the stack. |
| 5169 | // function, receiver [, arguments] |
| 5170 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 5171 | |
| 5172 | // Check that the function is really a JavaScript function. |
| 5173 | // r1: pushed function (to be verified) |
| 5174 | __ tst(r1, Operand(kSmiTagMask)); |
| 5175 | __ b(eq, &slow); |
| 5176 | // Get the map of the function object. |
| 5177 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5178 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 5179 | __ cmp(r2, Operand(JS_FUNCTION_TYPE)); |
| 5180 | __ b(ne, &slow); |
| 5181 | |
| 5182 | // Fast-case: Invoke the function now. |
| 5183 | // r1: pushed function |
| 5184 | ParameterCount actual(argc_); |
| 5185 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 5186 | |
| 5187 | // Slow-case: Non-function called. |
| 5188 | __ bind(&slow); |
| 5189 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5190 | __ mov(r2, Operand(0)); |
| 5191 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 5192 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 5193 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | |
| 5197 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5198 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5199 | } } // namespace v8::internal |