ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "scopes.h" |
| 34 | #include "runtime.h" |
| 35 | |
| 36 | namespace v8 { namespace internal { |
| 37 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 38 | #define __ masm_-> |
| 39 | |
| 40 | // ------------------------------------------------------------------------- |
| 41 | // VirtualFrame implementation. |
| 42 | |
| 43 | VirtualFrame::VirtualFrame(CodeGenerator* cgen) { |
| 44 | ASSERT(cgen->scope() != NULL); |
| 45 | |
| 46 | masm_ = cgen->masm(); |
| 47 | frame_local_count_ = cgen->scope()->num_stack_slots(); |
| 48 | parameter_count_ = cgen->scope()->num_parameters(); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | void VirtualFrame::Enter() { |
| 53 | Comment cmnt(masm_, "[ Enter JS frame"); |
| 54 | #ifdef DEBUG |
| 55 | { Label done, fail; |
| 56 | __ tst(r1, Operand(kSmiTagMask)); |
| 57 | __ b(eq, &fail); |
| 58 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 59 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 60 | __ cmp(r2, Operand(JS_FUNCTION_TYPE)); |
| 61 | __ b(eq, &done); |
| 62 | __ bind(&fail); |
| 63 | __ stop("CodeGenerator::EnterJSFrame - r1 not a function"); |
| 64 | __ bind(&done); |
| 65 | } |
| 66 | #endif // DEBUG |
| 67 | |
| 68 | __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit()); |
| 69 | // Adjust FP to point to saved FP. |
| 70 | __ add(fp, sp, Operand(2 * kPointerSize)); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void VirtualFrame::Exit() { |
| 75 | Comment cmnt(masm_, "[ Exit JS frame"); |
| 76 | // Drop the execution stack down to the frame pointer and restore the caller |
| 77 | // frame pointer and return address. |
| 78 | __ mov(sp, fp); |
| 79 | __ ldm(ia_w, sp, fp.bit() | lr.bit()); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void VirtualFrame::AllocateLocals() { |
| 84 | if (frame_local_count_ > 0) { |
| 85 | Comment cmnt(masm_, "[ Allocate space for locals"); |
| 86 | // Initialize stack slots with 'undefined' value. |
| 87 | __ mov(ip, Operand(Factory::undefined_value())); |
| 88 | for (int i = 0; i < frame_local_count_; i++) { |
| 89 | __ push(ip); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void VirtualFrame::Drop(int count) { |
| 96 | ASSERT(count >= 0); |
| 97 | if (count > 0) { |
| 98 | __ add(sp, sp, Operand(count * kPointerSize)); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void VirtualFrame::Pop() { Drop(1); } |
| 104 | |
| 105 | |
| 106 | void VirtualFrame::Pop(Register reg) { |
| 107 | __ pop(reg); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void VirtualFrame::Push(Register reg) { |
| 112 | __ push(reg); |
| 113 | } |
| 114 | |
| 115 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 116 | // ------------------------------------------------------------------------- |
| 117 | // CodeGenState implementation. |
| 118 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 119 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 120 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 121 | typeof_state_(NOT_INSIDE_TYPEOF), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 122 | true_target_(NULL), |
| 123 | false_target_(NULL), |
| 124 | previous_(NULL) { |
| 125 | owner_->set_state(this); |
| 126 | } |
| 127 | |
| 128 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 129 | CodeGenState::CodeGenState(CodeGenerator* owner, |
| 130 | TypeofState typeof_state, |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 131 | Label* true_target, |
| 132 | Label* false_target) |
| 133 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 134 | typeof_state_(typeof_state), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 135 | true_target_(true_target), |
| 136 | false_target_(false_target), |
| 137 | previous_(owner->state()) { |
| 138 | owner_->set_state(this); |
| 139 | } |
| 140 | |
| 141 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 142 | CodeGenState::~CodeGenState() { |
| 143 | ASSERT(owner_->state() == this); |
| 144 | owner_->set_state(previous_); |
| 145 | } |
| 146 | |
| 147 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 148 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 149 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 151 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 152 | bool is_eval) |
| 153 | : is_eval_(is_eval), |
| 154 | script_(script), |
| 155 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 156 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 157 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 158 | frame_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 159 | cc_reg_(al), |
| 160 | state_(NULL), |
| 161 | break_stack_height_(0) { |
| 162 | } |
| 163 | |
| 164 | |
| 165 | // Calling conventions: |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 166 | // r0: the number of arguments |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | // fp: frame pointer |
| 168 | // sp: stack pointer |
| 169 | // pp: caller's parameter pointer |
| 170 | // cp: callee's context |
| 171 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 172 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | ZoneList<Statement*>* body = fun->body(); |
| 174 | |
| 175 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 176 | ASSERT(scope_ == NULL); |
| 177 | scope_ = fun->scope(); |
| 178 | ASSERT(frame_ == NULL); |
| 179 | VirtualFrame virtual_frame(this); |
| 180 | frame_ = &virtual_frame; |
| 181 | cc_reg_ = al; |
| 182 | { |
| 183 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 184 | |
| 185 | // Entry |
| 186 | // stack: function, receiver, arguments, return address |
| 187 | // r0: number of arguments |
| 188 | // sp: stack pointer |
| 189 | // fp: frame pointer |
| 190 | // pp: caller's parameter pointer |
| 191 | // cp: callee's context |
| 192 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 193 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 194 | // tos: code slot |
| 195 | #ifdef DEBUG |
| 196 | if (strlen(FLAG_stop_at) > 0 && |
| 197 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 198 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 199 | } |
| 200 | #endif |
| 201 | |
| 202 | // Allocate space for locals and initialize them. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 203 | frame_->AllocateLocals(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 205 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 206 | // Allocate local context. |
| 207 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 208 | __ ldr(r0, frame_->Function()); |
| 209 | frame_->Push(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 210 | __ CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
| 211 | |
| 212 | if (kDebug) { |
| 213 | Label verified_true; |
| 214 | __ cmp(r0, Operand(cp)); |
| 215 | __ b(eq, &verified_true); |
| 216 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 217 | __ bind(&verified_true); |
| 218 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 220 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | } |
| 222 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 223 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | // 1) only needed if we have a context |
| 225 | // 2) no need to recompute context ptr every single time |
| 226 | // 3) don't copy parameter operand code from SlotOperand! |
| 227 | { |
| 228 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 229 | |
| 230 | // Note that iteration order is relevant here! If we have the same |
| 231 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 232 | // needs to be copied into the context, it must be the last argument |
| 233 | // passed to the parameter that needs to be copied. This is a rare |
| 234 | // case so we don't check for it, instead we rely on the copying |
| 235 | // order: such a parameter is copied repeatedly into the same |
| 236 | // context location and thus the last value is what is seen inside |
| 237 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 238 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 239 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 240 | Slot* slot = par->slot(); |
| 241 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 242 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
| 243 | __ ldr(r1, frame_->Parameter(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | // Loads r2 with context; used below in RecordWrite. |
| 245 | __ str(r1, SlotOperand(slot, r2)); |
| 246 | // Load the offset into r3. |
| 247 | int slot_offset = |
| 248 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 249 | __ mov(r3, Operand(slot_offset)); |
| 250 | __ RecordWrite(r2, r3, r1); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 255 | // Store the arguments object. This must happen after context |
| 256 | // initialization because the arguments object may be stored in the |
| 257 | // context. |
| 258 | if (scope_->arguments() != NULL) { |
| 259 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 261 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 262 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 263 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 264 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 265 | // The receiver is below the arguments, the return address, |
| 266 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 267 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 268 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 269 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 270 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
| 271 | __ CallStub(&stub); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 272 | frame_->Push(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 273 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 274 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 275 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 277 | frame_->Pop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | } |
| 279 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 280 | // Generate code to 'execute' declarations and initialize functions |
| 281 | // (source elements). In case of an illegal redeclaration we need to |
| 282 | // handle that instead of processing the declarations. |
| 283 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 285 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 286 | } else { |
| 287 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 288 | ProcessDeclarations(scope_->declarations()); |
| 289 | // Bail out if a stack-overflow exception occurred when processing |
| 290 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 291 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 292 | } |
| 293 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 294 | if (FLAG_trace) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 295 | __ CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 296 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 297 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | CheckStack(); |
| 299 | |
| 300 | // Compile the body of the function in a vanilla state. Don't |
| 301 | // bother compiling all the code if the scope has an illegal |
| 302 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 303 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | Comment cmnt(masm_, "[ function body"); |
| 305 | #ifdef DEBUG |
| 306 | bool is_builtin = Bootstrapper::IsActive(); |
| 307 | bool should_trace = |
| 308 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 309 | if (should_trace) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 310 | __ CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 311 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 312 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 313 | #endif |
| 314 | VisitStatements(body); |
| 315 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // exit |
| 319 | // r0: result |
| 320 | // sp: stack pointer |
| 321 | // fp: frame pointer |
| 322 | // pp: parameter pointer |
| 323 | // cp: callee's context |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 324 | __ mov(r0, Operand(Factory::undefined_value())); |
| 325 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | __ bind(&function_return_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 327 | if (FLAG_trace) { |
| 328 | // Push the return value on the stack as the parameter. |
| 329 | // Runtime::TraceExit returns the parameter as it is. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 330 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 331 | __ CallRuntime(Runtime::kTraceExit, 1); |
| 332 | } |
| 333 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 334 | // Tear down the frame which will restore the caller's frame pointer and the |
| 335 | // link register. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 336 | frame_->Exit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 337 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 338 | __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
| 339 | __ mov(pc, lr); |
| 340 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | // Code generation state must be reset. |
| 342 | scope_ = NULL; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 343 | frame_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 344 | ASSERT(!has_cc()); |
| 345 | ASSERT(state_ == NULL); |
| 346 | } |
| 347 | |
| 348 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 349 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 350 | // Currently, this assertion will fail if we try to assign to |
| 351 | // a constant variable that is constant because it is read-only |
| 352 | // (such as the variable referring to a named function expression). |
| 353 | // We need to implement assignments to read-only variables. |
| 354 | // Ideally, we should do this during AST generation (by converting |
| 355 | // such assignments into expression statements); however, in general |
| 356 | // we may not be able to make the decision until past AST generation, |
| 357 | // that is when the entire program is known. |
| 358 | ASSERT(slot != NULL); |
| 359 | int index = slot->index(); |
| 360 | switch (slot->type()) { |
| 361 | case Slot::PARAMETER: |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 362 | return frame_->Parameter(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 363 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 364 | case Slot::LOCAL: |
| 365 | return frame_->Local(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 366 | |
| 367 | case Slot::CONTEXT: { |
| 368 | // Follow the context chain if necessary. |
| 369 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 370 | Register context = cp; |
| 371 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
| 372 | for (int i = chain_length; i-- > 0;) { |
| 373 | // Load the closure. |
| 374 | // (All contexts, even 'with' contexts, have a closure, |
| 375 | // and it is the same for all contexts inside a function. |
| 376 | // There is no need to go to the function context first.) |
| 377 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 378 | // Load the function context (which is the incoming, outer context). |
| 379 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 380 | context = tmp; |
| 381 | } |
| 382 | // We may have a 'with' context now. Get the function context. |
| 383 | // (In fact this mov may never be the needed, since the scope analysis |
| 384 | // may not permit a direct context access in this case and thus we are |
| 385 | // always at a function context. However it is safe to dereference be- |
| 386 | // cause the function context of a function context is itself. Before |
| 387 | // deleting this mov we should try to create a counter-example first, |
| 388 | // though...) |
| 389 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 390 | return ContextOperand(tmp, index); |
| 391 | } |
| 392 | |
| 393 | default: |
| 394 | UNREACHABLE(); |
| 395 | return MemOperand(r0, 0); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 400 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 401 | // (partially) translated into branches, or it may have set the condition |
| 402 | // code register. If force_cc is set, the value is forced to set the |
| 403 | // condition code register and no value is pushed. If the condition code |
| 404 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 405 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 406 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 407 | TypeofState typeof_state, |
| 408 | Label* true_target, |
| 409 | Label* false_target, |
| 410 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 411 | ASSERT(!has_cc()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 412 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 413 | { CodeGenState new_state(this, typeof_state, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 414 | Visit(x); |
| 415 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 416 | if (force_cc && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 417 | // Convert the TOS value to a boolean in the condition code register. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 418 | // Visiting an expression may possibly choose neither (a) to leave a |
| 419 | // value in the condition code register nor (b) to leave a value in TOS |
| 420 | // (eg, by compiling to only jumps to the targets). In that case the |
| 421 | // code generated by ToBoolean is wrong because it assumes the value of |
| 422 | // the expression in TOS. So long as there is always a value in TOS or |
| 423 | // the condition code register when control falls through to here (there |
| 424 | // is), the code generated by ToBoolean is dead and therefore safe. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 425 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 426 | } |
| 427 | ASSERT(has_cc() || !force_cc); |
| 428 | } |
| 429 | |
| 430 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 431 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | Label true_target; |
| 433 | Label false_target; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 434 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 435 | |
| 436 | if (has_cc()) { |
| 437 | // convert cc_reg_ into a bool |
| 438 | Label loaded, materialize_true; |
| 439 | __ b(cc_reg_, &materialize_true); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 440 | __ mov(r0, Operand(Factory::false_value())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 441 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 442 | __ b(&loaded); |
| 443 | __ bind(&materialize_true); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 444 | __ mov(r0, Operand(Factory::true_value())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 445 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 446 | __ bind(&loaded); |
| 447 | cc_reg_ = al; |
| 448 | } |
| 449 | |
| 450 | if (true_target.is_linked() || false_target.is_linked()) { |
| 451 | // we have at least one condition value |
| 452 | // that has been "translated" into a branch, |
| 453 | // thus it needs to be loaded explicitly again |
| 454 | Label loaded; |
| 455 | __ b(&loaded); // don't lose current TOS |
| 456 | bool both = true_target.is_linked() && false_target.is_linked(); |
| 457 | // reincarnate "true", if necessary |
| 458 | if (true_target.is_linked()) { |
| 459 | __ bind(&true_target); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 460 | __ mov(r0, Operand(Factory::true_value())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 461 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 462 | } |
| 463 | // if both "true" and "false" need to be reincarnated, |
| 464 | // jump across code for "false" |
| 465 | if (both) |
| 466 | __ b(&loaded); |
| 467 | // reincarnate "false", if necessary |
| 468 | if (false_target.is_linked()) { |
| 469 | __ bind(&false_target); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 470 | __ mov(r0, Operand(Factory::false_value())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 471 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 472 | } |
| 473 | // everything is loaded at this point |
| 474 | __ bind(&loaded); |
| 475 | } |
| 476 | ASSERT(!has_cc()); |
| 477 | } |
| 478 | |
| 479 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 480 | void CodeGenerator::LoadGlobal() { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 481 | __ ldr(r0, GlobalObject()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 482 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 486 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
| 487 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 488 | __ ldr(scratch, |
| 489 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
| 490 | frame_->Push(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 494 | // 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] | 495 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 496 | // variables w/o reference errors elsewhere. |
| 497 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 498 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 499 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 500 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 501 | // the variable as if through '<global>.<variable>' to make sure we |
| 502 | // do not get reference errors. |
| 503 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 504 | Literal key(variable->name()); |
| 505 | // TODO(1241834): Fetch the position from the variable instead of using |
| 506 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 507 | Property property(&global, &key, RelocInfo::kNoPosition); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 508 | Load(&property); |
| 509 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 510 | Load(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 515 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 516 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 517 | cgen->LoadReference(this); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | Reference::~Reference() { |
| 522 | cgen_->UnloadReference(this); |
| 523 | } |
| 524 | |
| 525 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 526 | void CodeGenerator::LoadReference(Reference* ref) { |
| 527 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 528 | Expression* e = ref->expression(); |
| 529 | Property* property = e->AsProperty(); |
| 530 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 531 | |
| 532 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 533 | // The expression is either a property or a variable proxy that rewrites |
| 534 | // to a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 535 | Load(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 536 | // We use a named reference if the key is a literal symbol, unless it is |
| 537 | // a string that can be legally parsed as an integer. This is because |
| 538 | // otherwise we will not get into the slow case code that handles [] on |
| 539 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | Literal* literal = property->key()->AsLiteral(); |
| 541 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 542 | if (literal != NULL && |
| 543 | literal->handle()->IsSymbol() && |
| 544 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | ref->set_type(Reference::NAMED); |
| 546 | } else { |
| 547 | Load(property->key()); |
| 548 | ref->set_type(Reference::KEYED); |
| 549 | } |
| 550 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 551 | // The expression is a variable proxy that does not rewrite to a |
| 552 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 554 | LoadGlobal(); |
| 555 | ref->set_type(Reference::NAMED); |
| 556 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 557 | ASSERT(var->slot() != NULL); |
| 558 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | } |
| 560 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 561 | // Anything else is a runtime error. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 562 | Load(e); |
| 563 | __ CallRuntime(Runtime::kThrowReferenceError, 1); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 568 | void CodeGenerator::UnloadReference(Reference* ref) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 569 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 570 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 572 | if (size > 0) { |
| 573 | frame_->Pop(r0); |
| 574 | frame_->Drop(size); |
| 575 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 580 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 581 | // register to a boolean in the condition code register. The code |
| 582 | // may jump to 'false_target' in case the register converts to 'false'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 583 | void CodeGenerator::ToBoolean(Label* true_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 584 | Label* false_target) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 585 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 586 | // Only the condition code should be set. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 587 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | |
| 589 | // Fast case checks |
| 590 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 591 | // Check if the value is 'false'. |
| 592 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 593 | __ b(eq, false_target); |
| 594 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 595 | // Check if the value is 'true'. |
| 596 | __ cmp(r0, Operand(Factory::true_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | __ b(eq, true_target); |
| 598 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 599 | // Check if the value is 'undefined'. |
| 600 | __ cmp(r0, Operand(Factory::undefined_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 601 | __ b(eq, false_target); |
| 602 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 603 | // Check if the value is a smi. |
| 604 | __ cmp(r0, Operand(Smi::FromInt(0))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 605 | __ b(eq, false_target); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 606 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | __ b(eq, true_target); |
| 608 | |
| 609 | // Slow case: call the runtime. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 610 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 611 | __ CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 612 | // Convert the result (r0) to a condition code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 613 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 614 | |
| 615 | cc_reg_ = ne; |
| 616 | } |
| 617 | |
| 618 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 619 | class GetPropertyStub : public CodeStub { |
| 620 | public: |
| 621 | GetPropertyStub() { } |
| 622 | |
| 623 | private: |
| 624 | Major MajorKey() { return GetProperty; } |
| 625 | int MinorKey() { return 0; } |
| 626 | void Generate(MacroAssembler* masm); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | }; |
| 628 | |
| 629 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 630 | class SetPropertyStub : public CodeStub { |
| 631 | public: |
| 632 | SetPropertyStub() { } |
| 633 | |
| 634 | private: |
| 635 | Major MajorKey() { return SetProperty; } |
| 636 | int MinorKey() { return 0; } |
| 637 | void Generate(MacroAssembler* masm); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 638 | }; |
| 639 | |
| 640 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 641 | class GenericBinaryOpStub : public CodeStub { |
| 642 | public: |
| 643 | explicit GenericBinaryOpStub(Token::Value op) : op_(op) { } |
| 644 | |
| 645 | private: |
| 646 | Token::Value op_; |
| 647 | |
| 648 | Major MajorKey() { return GenericBinaryOp; } |
| 649 | int MinorKey() { return static_cast<int>(op_); } |
| 650 | void Generate(MacroAssembler* masm); |
| 651 | |
| 652 | const char* GetName() { |
| 653 | switch (op_) { |
| 654 | case Token::ADD: return "GenericBinaryOpStub_ADD"; |
| 655 | case Token::SUB: return "GenericBinaryOpStub_SUB"; |
| 656 | case Token::MUL: return "GenericBinaryOpStub_MUL"; |
| 657 | case Token::DIV: return "GenericBinaryOpStub_DIV"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 658 | case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR"; |
| 659 | case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND"; |
| 660 | case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR"; |
| 661 | case Token::SAR: return "GenericBinaryOpStub_SAR"; |
| 662 | case Token::SHL: return "GenericBinaryOpStub_SHL"; |
| 663 | case Token::SHR: return "GenericBinaryOpStub_SHR"; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 664 | default: return "GenericBinaryOpStub"; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | #ifdef DEBUG |
| 669 | void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); } |
| 670 | #endif |
| 671 | }; |
| 672 | |
| 673 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 674 | class InvokeBuiltinStub : public CodeStub { |
| 675 | public: |
| 676 | enum Kind { Inc, Dec, ToNumber }; |
| 677 | InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { } |
| 678 | |
| 679 | private: |
| 680 | Kind kind_; |
| 681 | int argc_; |
| 682 | |
| 683 | Major MajorKey() { return InvokeBuiltin; } |
| 684 | int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); } |
| 685 | void Generate(MacroAssembler* masm); |
| 686 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 687 | #ifdef DEBUG |
| 688 | void Print() { |
| 689 | PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n", |
| 690 | static_cast<int>(kind_), |
| 691 | argc_); |
| 692 | } |
| 693 | #endif |
| 694 | }; |
| 695 | |
| 696 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 697 | void CodeGenerator::GenericBinaryOperation(Token::Value op) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 698 | // sp[0] : y |
| 699 | // sp[1] : x |
| 700 | // result : r0 |
| 701 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | // Stub is entered with a call: 'return address' is in lr. |
| 703 | switch (op) { |
| 704 | case Token::ADD: // fall through. |
| 705 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 706 | case Token::MUL: |
| 707 | case Token::BIT_OR: |
| 708 | case Token::BIT_AND: |
| 709 | case Token::BIT_XOR: |
| 710 | case Token::SHL: |
| 711 | case Token::SHR: |
| 712 | case Token::SAR: { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 713 | frame_->Pop(r0); // r0 : y |
| 714 | frame_->Pop(r1); // r1 : x |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 715 | GenericBinaryOpStub stub(op); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 716 | __ CallStub(&stub); |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | case Token::DIV: { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 721 | __ mov(r0, Operand(1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 722 | __ InvokeBuiltin(Builtins::DIV, CALL_JS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | |
| 726 | case Token::MOD: { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 727 | __ mov(r0, Operand(1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 728 | __ InvokeBuiltin(Builtins::MOD, CALL_JS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 729 | break; |
| 730 | } |
| 731 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 732 | case Token::COMMA: |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 733 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 734 | // simply discard left value |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 735 | frame_->Pop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 736 | break; |
| 737 | |
| 738 | default: |
| 739 | // Other cases should have been handled before this point. |
| 740 | UNREACHABLE(); |
| 741 | break; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 746 | class DeferredInlinedSmiOperation: public DeferredCode { |
| 747 | public: |
| 748 | DeferredInlinedSmiOperation(CodeGenerator* generator, Token::Value op, |
| 749 | int value, bool reversed) : |
| 750 | DeferredCode(generator), op_(op), value_(value), reversed_(reversed) { |
| 751 | set_comment("[ DeferredInlinedSmiOperation"); |
| 752 | } |
| 753 | |
| 754 | virtual void Generate() { |
| 755 | switch (op_) { |
| 756 | case Token::ADD: { |
| 757 | if (reversed_) { |
| 758 | // revert optimistic add |
| 759 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 760 | __ mov(r1, Operand(Smi::FromInt(value_))); // x |
| 761 | } else { |
| 762 | // revert optimistic add |
| 763 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 764 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 765 | } |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | case Token::SUB: { |
| 770 | if (reversed_) { |
| 771 | // revert optimistic sub |
| 772 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 773 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 774 | } else { |
| 775 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 776 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 777 | } |
| 778 | break; |
| 779 | } |
| 780 | |
| 781 | case Token::BIT_OR: |
| 782 | case Token::BIT_XOR: |
| 783 | case Token::BIT_AND: { |
| 784 | if (reversed_) { |
| 785 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 786 | } else { |
| 787 | __ mov(r1, Operand(r0)); |
| 788 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 789 | } |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | case Token::SHL: |
| 794 | case Token::SHR: |
| 795 | case Token::SAR: { |
| 796 | if (!reversed_) { |
| 797 | __ mov(r1, Operand(r0)); |
| 798 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 799 | } else { |
| 800 | UNREACHABLE(); // should have been handled in SmiOperation |
| 801 | } |
| 802 | break; |
| 803 | } |
| 804 | |
| 805 | default: |
| 806 | // other cases should have been handled before this point. |
| 807 | UNREACHABLE(); |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | GenericBinaryOpStub igostub(op_); |
| 812 | __ CallStub(&igostub); |
| 813 | } |
| 814 | |
| 815 | private: |
| 816 | Token::Value op_; |
| 817 | int value_; |
| 818 | bool reversed_; |
| 819 | }; |
| 820 | |
| 821 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 822 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 823 | Handle<Object> value, |
| 824 | bool reversed) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 825 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 826 | // some possible smi operations (like + and -) when (at least) one |
| 827 | // of the operands is a literal smi. With this optimization, the |
| 828 | // performance of the system is increased by ~15%, and the generated |
| 829 | // code size is increased by ~1% (measured on a combination of |
| 830 | // different benchmarks). |
| 831 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 832 | // sp[0] : operand |
| 833 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 834 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 835 | |
| 836 | Label exit; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 837 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 838 | |
| 839 | switch (op) { |
| 840 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 841 | DeferredCode* deferred = |
| 842 | new DeferredInlinedSmiOperation(this, op, int_value, reversed); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 843 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 844 | __ add(r0, r0, Operand(value), SetCC); |
| 845 | __ b(vs, deferred->enter()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 846 | __ tst(r0, Operand(kSmiTagMask)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 847 | __ b(ne, deferred->enter()); |
| 848 | __ bind(deferred->exit()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 849 | break; |
| 850 | } |
| 851 | |
| 852 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 853 | DeferredCode* deferred = |
| 854 | new DeferredInlinedSmiOperation(this, op, int_value, reversed); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 855 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 856 | if (!reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 857 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 858 | } else { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 859 | __ rsb(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 860 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 861 | __ b(vs, deferred->enter()); |
| 862 | __ tst(r0, Operand(kSmiTagMask)); |
| 863 | __ b(ne, deferred->enter()); |
| 864 | __ bind(deferred->exit()); |
| 865 | break; |
| 866 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 867 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 868 | case Token::BIT_OR: |
| 869 | case Token::BIT_XOR: |
| 870 | case Token::BIT_AND: { |
| 871 | DeferredCode* deferred = |
| 872 | new DeferredInlinedSmiOperation(this, op, int_value, reversed); |
| 873 | __ tst(r0, Operand(kSmiTagMask)); |
| 874 | __ b(ne, deferred->enter()); |
| 875 | switch (op) { |
| 876 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 877 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 878 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 879 | default: UNREACHABLE(); |
| 880 | } |
| 881 | __ bind(deferred->exit()); |
| 882 | break; |
| 883 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 884 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 885 | case Token::SHL: |
| 886 | case Token::SHR: |
| 887 | case Token::SAR: { |
| 888 | if (reversed) { |
| 889 | __ mov(ip, Operand(value)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 890 | frame_->Push(ip); |
| 891 | frame_->Push(r0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 892 | GenericBinaryOperation(op); |
| 893 | |
| 894 | } else { |
| 895 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 896 | DeferredCode* deferred = |
| 897 | new DeferredInlinedSmiOperation(this, op, shift_value, false); |
| 898 | __ tst(r0, Operand(kSmiTagMask)); |
| 899 | __ b(ne, deferred->enter()); |
| 900 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 901 | switch (op) { |
| 902 | case Token::SHL: { |
| 903 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 904 | // check that the *unsigned* result fits in a smi |
| 905 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 906 | __ b(mi, deferred->enter()); |
| 907 | break; |
| 908 | } |
| 909 | case Token::SHR: { |
| 910 | // LSR by immediate 0 means shifting 32 bits. |
| 911 | if (shift_value != 0) { |
| 912 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 913 | } |
| 914 | // check that the *unsigned* result fits in a smi |
| 915 | // neither of the two high-order bits can be set: |
| 916 | // - 0x80000000: high bit would be lost when smi tagging |
| 917 | // - 0x40000000: this number would convert to negative when |
| 918 | // smi tagging these two cases can only happen with shifts |
| 919 | // by 0 or 1 when handed a valid smi |
| 920 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
| 921 | __ b(ne, deferred->enter()); |
| 922 | break; |
| 923 | } |
| 924 | case Token::SAR: { |
| 925 | if (shift_value != 0) { |
| 926 | // ASR by immediate 0 means shifting 32 bits. |
| 927 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 928 | } |
| 929 | break; |
| 930 | } |
| 931 | default: UNREACHABLE(); |
| 932 | } |
| 933 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 934 | __ bind(deferred->exit()); |
| 935 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 936 | break; |
| 937 | } |
| 938 | |
| 939 | default: |
| 940 | if (!reversed) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 941 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 942 | __ mov(r0, Operand(value)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 943 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 944 | } else { |
| 945 | __ mov(ip, Operand(value)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 946 | frame_->Push(ip); |
| 947 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 948 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 949 | GenericBinaryOperation(op); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 950 | break; |
| 951 | } |
| 952 | |
| 953 | __ bind(&exit); |
| 954 | } |
| 955 | |
| 956 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 957 | void CodeGenerator::Comparison(Condition cc, bool strict) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 958 | // sp[0] : y |
| 959 | // sp[1] : x |
| 960 | // result : cc register |
| 961 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 962 | // Strict only makes sense for equality comparisons. |
| 963 | ASSERT(!strict || cc == eq); |
| 964 | |
| 965 | Label exit, smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 966 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 967 | if (cc == gt || cc == le) { |
| 968 | cc = ReverseCondition(cc); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 969 | frame_->Pop(r1); |
| 970 | frame_->Pop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 971 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 972 | frame_->Pop(r0); |
| 973 | frame_->Pop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 974 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 975 | __ orr(r2, r0, Operand(r1)); |
| 976 | __ tst(r2, Operand(kSmiTagMask)); |
| 977 | __ b(eq, &smi); |
| 978 | |
| 979 | // Perform non-smi comparison by runtime call. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 980 | frame_->Push(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 981 | |
| 982 | // Figure out which native to call and setup the arguments. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 983 | Builtins::JavaScript native; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 984 | int argc; |
| 985 | if (cc == eq) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 986 | native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 987 | argc = 1; |
| 988 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 989 | native = Builtins::COMPARE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 990 | int ncr; // NaN compare result |
| 991 | if (cc == lt || cc == le) { |
| 992 | ncr = GREATER; |
| 993 | } else { |
| 994 | ASSERT(cc == gt || cc == ge); // remaining cases |
| 995 | ncr = LESS; |
| 996 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 997 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 998 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 999 | argc = 2; |
| 1000 | } |
| 1001 | |
| 1002 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 1003 | // tagged as a small integer. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1004 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1005 | __ mov(r0, Operand(argc)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1006 | __ InvokeBuiltin(native, CALL_JS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1007 | __ cmp(r0, Operand(0)); |
| 1008 | __ b(&exit); |
| 1009 | |
| 1010 | // test smi equality by pointer comparison. |
| 1011 | __ bind(&smi); |
| 1012 | __ cmp(r1, Operand(r0)); |
| 1013 | |
| 1014 | __ bind(&exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1015 | cc_reg_ = cc; |
| 1016 | } |
| 1017 | |
| 1018 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1019 | class CallFunctionStub: public CodeStub { |
| 1020 | public: |
| 1021 | explicit CallFunctionStub(int argc) : argc_(argc) {} |
| 1022 | |
| 1023 | void Generate(MacroAssembler* masm); |
| 1024 | |
| 1025 | private: |
| 1026 | int argc_; |
| 1027 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1028 | #if defined(DEBUG) |
| 1029 | void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); } |
| 1030 | #endif // defined(DEBUG) |
| 1031 | |
| 1032 | Major MajorKey() { return CallFunction; } |
| 1033 | int MinorKey() { return argc_; } |
| 1034 | }; |
| 1035 | |
| 1036 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1037 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1038 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1039 | int position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1040 | // Push the arguments ("left-to-right") on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1041 | for (int i = 0; i < args->length(); i++) { |
| 1042 | Load(args->at(i)); |
| 1043 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1044 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1045 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1046 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1047 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1048 | // Use the shared code stub to call the function. |
| 1049 | CallFunctionStub call_function(args->length()); |
| 1050 | __ CallStub(&call_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1051 | |
| 1052 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1053 | __ ldr(cp, frame_->Context()); |
| 1054 | frame_->Pop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1058 | void CodeGenerator::Branch(bool if_true, Label* L) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1059 | ASSERT(has_cc()); |
| 1060 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
| 1061 | __ b(cc, L); |
| 1062 | cc_reg_ = al; |
| 1063 | } |
| 1064 | |
| 1065 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1066 | void CodeGenerator::CheckStack() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1067 | if (FLAG_check_stack) { |
| 1068 | Comment cmnt(masm_, "[ check stack"); |
| 1069 | StackCheckStub stub; |
| 1070 | __ CallStub(&stub); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1075 | void CodeGenerator::VisitBlock(Block* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1076 | Comment cmnt(masm_, "[ Block"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1077 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1078 | node->set_break_stack_height(break_stack_height_); |
| 1079 | VisitStatements(node->statements()); |
| 1080 | __ bind(node->break_target()); |
| 1081 | } |
| 1082 | |
| 1083 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1084 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1085 | __ mov(r0, Operand(pairs)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1086 | frame_->Push(r0); |
| 1087 | frame_->Push(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1088 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1089 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1090 | __ CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1091 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1095 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1096 | Comment cmnt(masm_, "[ Declaration"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1097 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | Variable* var = node->proxy()->var(); |
| 1099 | ASSERT(var != NULL); // must have been resolved |
| 1100 | Slot* slot = var->slot(); |
| 1101 | |
| 1102 | // If it was not possible to allocate the variable at compile time, |
| 1103 | // we need to "declare" it at runtime to make sure it actually |
| 1104 | // exists in the local context. |
| 1105 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1106 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1107 | // during variable resolution and must have mode DYNAMIC. |
| 1108 | ASSERT(var->mode() == Variable::DYNAMIC); |
| 1109 | // For now, just do a runtime call. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1110 | frame_->Push(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1111 | __ mov(r0, Operand(var->name())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1112 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1113 | // Declaration nodes are always declared in only two modes. |
| 1114 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1115 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1116 | __ mov(r0, Operand(Smi::FromInt(attr))); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1117 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1118 | // Push initial value, if any. |
| 1119 | // Note: For variables we must not push an initial value (such as |
| 1120 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1121 | // must not destroy the current value. |
| 1122 | if (node->mode() == Variable::CONST) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1123 | __ mov(r0, Operand(Factory::the_hole_value())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1124 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1125 | } else if (node->fun() != NULL) { |
| 1126 | Load(node->fun()); |
| 1127 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1128 | __ mov(r0, Operand(0)); // no initial value! |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1129 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1130 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1131 | __ CallRuntime(Runtime::kDeclareContextSlot, 4); |
| 1132 | // Ignore the return value (declarations are statements). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | ASSERT(!var->is_global()); |
| 1137 | |
| 1138 | // If we have a function or a constant, we need to initialize the variable. |
| 1139 | Expression* val = NULL; |
| 1140 | if (node->mode() == Variable::CONST) { |
| 1141 | val = new Literal(Factory::the_hole_value()); |
| 1142 | } else { |
| 1143 | val = node->fun(); // NULL if we don't have a function |
| 1144 | } |
| 1145 | |
| 1146 | if (val != NULL) { |
| 1147 | // Set initial value. |
| 1148 | Reference target(this, node->proxy()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1149 | ASSERT(target.is_slot()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1150 | Load(val); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1151 | target.SetValue(NOT_CONST_INIT); |
| 1152 | // Get rid of the assigned value (declarations are statements). It's |
| 1153 | // safe to pop the value lying on top of the reference before unloading |
| 1154 | // the reference itself (which preserves the top of stack) because we |
| 1155 | // know it is a zero-sized reference. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1156 | frame_->Pop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1161 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1162 | Comment cmnt(masm_, "[ ExpressionStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1163 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1164 | Expression* expression = node->expression(); |
| 1165 | expression->MarkAsStatement(); |
| 1166 | Load(expression); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1167 | frame_->Pop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1171 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1172 | Comment cmnt(masm_, "// EmptyStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1173 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1174 | // nothing to do |
| 1175 | } |
| 1176 | |
| 1177 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1178 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1179 | Comment cmnt(masm_, "[ IfStatement"); |
| 1180 | // Generate different code depending on which |
| 1181 | // parts of the if statement are present or not. |
| 1182 | bool has_then_stm = node->HasThenStatement(); |
| 1183 | bool has_else_stm = node->HasElseStatement(); |
| 1184 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1185 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1186 | |
| 1187 | Label exit; |
| 1188 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1189 | Comment cmnt(masm_, "[ IfThenElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1190 | Label then; |
| 1191 | Label else_; |
| 1192 | // if (cond) |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1193 | LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1194 | Branch(false, &else_); |
| 1195 | // then |
| 1196 | __ bind(&then); |
| 1197 | Visit(node->then_statement()); |
| 1198 | __ b(&exit); |
| 1199 | // else |
| 1200 | __ bind(&else_); |
| 1201 | Visit(node->else_statement()); |
| 1202 | |
| 1203 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1204 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1205 | ASSERT(!has_else_stm); |
| 1206 | Label then; |
| 1207 | // if (cond) |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1208 | LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &exit, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1209 | Branch(false, &exit); |
| 1210 | // then |
| 1211 | __ bind(&then); |
| 1212 | Visit(node->then_statement()); |
| 1213 | |
| 1214 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1215 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1216 | ASSERT(!has_then_stm); |
| 1217 | Label else_; |
| 1218 | // if (!cond) |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1219 | LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1220 | Branch(true, &exit); |
| 1221 | // else |
| 1222 | __ bind(&else_); |
| 1223 | Visit(node->else_statement()); |
| 1224 | |
| 1225 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1226 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1227 | ASSERT(!has_then_stm && !has_else_stm); |
| 1228 | // if (cond) |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1229 | LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &exit, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1230 | if (has_cc()) { |
| 1231 | cc_reg_ = al; |
| 1232 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1233 | frame_->Pop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | // end |
| 1238 | __ bind(&exit); |
| 1239 | } |
| 1240 | |
| 1241 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1242 | void CodeGenerator::CleanStack(int num_bytes) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1243 | ASSERT(num_bytes % kPointerSize == 0); |
| 1244 | frame_->Drop(num_bytes / kPointerSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1248 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1249 | Comment cmnt(masm_, "[ ContinueStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1250 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1251 | CleanStack(break_stack_height_ - node->target()->break_stack_height()); |
| 1252 | __ b(node->target()->continue_target()); |
| 1253 | } |
| 1254 | |
| 1255 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1256 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1257 | Comment cmnt(masm_, "[ BreakStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1258 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1259 | CleanStack(break_stack_height_ - node->target()->break_stack_height()); |
| 1260 | __ b(node->target()->break_target()); |
| 1261 | } |
| 1262 | |
| 1263 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1264 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1265 | Comment cmnt(masm_, "[ ReturnStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1266 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1267 | Load(node->expression()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1268 | // Move the function result into r0. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1269 | frame_->Pop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1270 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1271 | __ b(&function_return_); |
| 1272 | } |
| 1273 | |
| 1274 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1275 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1276 | Comment cmnt(masm_, "[ WithEnterStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1277 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1278 | Load(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1279 | if (node->is_catch_block()) { |
| 1280 | __ CallRuntime(Runtime::kPushCatchContext, 1); |
| 1281 | } else { |
| 1282 | __ CallRuntime(Runtime::kPushContext, 1); |
| 1283 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1284 | if (kDebug) { |
| 1285 | Label verified_true; |
| 1286 | __ cmp(r0, Operand(cp)); |
| 1287 | __ b(eq, &verified_true); |
| 1288 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1289 | __ bind(&verified_true); |
| 1290 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1291 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1292 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1296 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1297 | Comment cmnt(masm_, "[ WithExitStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1298 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1299 | // Pop context. |
| 1300 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1301 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1302 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1306 | int CodeGenerator::FastCaseSwitchMaxOverheadFactor() { |
| 1307 | return kFastSwitchMaxOverheadFactor; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1310 | int CodeGenerator::FastCaseSwitchMinCaseCount() { |
| 1311 | return kFastSwitchMinCaseCount; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1315 | void CodeGenerator::GenerateFastCaseSwitchJumpTable( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1316 | SwitchStatement* node, |
| 1317 | int min_index, |
| 1318 | int range, |
| 1319 | Label* fail_label, |
| 1320 | Vector<Label*> case_targets, |
| 1321 | Vector<Label> case_labels) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1322 | |
| 1323 | ASSERT(kSmiTag == 0 && kSmiTagSize <= 2); |
| 1324 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1325 | frame_->Pop(r0); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1326 | |
| 1327 | // Test for a Smi value in a HeapNumber. |
| 1328 | Label is_smi; |
| 1329 | __ tst(r0, Operand(kSmiTagMask)); |
| 1330 | __ b(eq, &is_smi); |
| 1331 | __ ldr(r1, MemOperand(r0, HeapObject::kMapOffset - kHeapObjectTag)); |
| 1332 | __ ldrb(r1, MemOperand(r1, Map::kInstanceTypeOffset - kHeapObjectTag)); |
| 1333 | __ cmp(r1, Operand(HEAP_NUMBER_TYPE)); |
| 1334 | __ b(ne, fail_label); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1335 | frame_->Push(r0); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1336 | __ CallRuntime(Runtime::kNumberToSmi, 1); |
| 1337 | __ bind(&is_smi); |
| 1338 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1339 | if (min_index != 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1340 | // Small positive numbers can be immediate operands. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1341 | if (min_index < 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1342 | // If min_index is Smi::kMinValue, -min_index is not a Smi. |
| 1343 | if (Smi::IsValid(-min_index)) { |
| 1344 | __ add(r0, r0, Operand(Smi::FromInt(-min_index))); |
| 1345 | } else { |
| 1346 | __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1))); |
| 1347 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1348 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1349 | } else { |
| 1350 | __ sub(r0, r0, Operand(Smi::FromInt(min_index))); |
| 1351 | } |
| 1352 | } |
| 1353 | __ tst(r0, Operand(0x80000000 | kSmiTagMask)); |
| 1354 | __ b(ne, fail_label); |
| 1355 | __ cmp(r0, Operand(Smi::FromInt(range))); |
| 1356 | __ b(ge, fail_label); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1357 | __ SmiJumpTable(r0, case_targets); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1358 | |
| 1359 | GenerateFastCaseSwitchCases(node, case_labels); |
| 1360 | } |
| 1361 | |
| 1362 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1363 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1364 | Comment cmnt(masm_, "[ SwitchStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1365 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1366 | node->set_break_stack_height(break_stack_height_); |
| 1367 | |
| 1368 | Load(node->tag()); |
| 1369 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1370 | if (TryGenerateFastCaseSwitchStatement(node)) { |
| 1371 | return; |
| 1372 | } |
| 1373 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1374 | Label next, fall_through, default_case; |
| 1375 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1376 | int length = cases->length(); |
| 1377 | |
| 1378 | for (int i = 0; i < length; i++) { |
| 1379 | CaseClause* clause = cases->at(i); |
| 1380 | |
| 1381 | Comment cmnt(masm_, "[ case clause"); |
| 1382 | |
| 1383 | if (clause->is_default()) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1384 | // Continue matching cases. The program will execute the default case's |
| 1385 | // statements if it does not match any of the cases. |
| 1386 | __ b(&next); |
| 1387 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1388 | // Bind the default case label, so we can branch to it when we |
| 1389 | // have compared against all other cases. |
| 1390 | ASSERT(default_case.is_unused()); // at most one default clause |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1391 | __ bind(&default_case); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | } else { |
| 1393 | __ bind(&next); |
| 1394 | next.Unuse(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1395 | __ ldr(r0, frame_->Top()); |
| 1396 | frame_->Push(r0); // duplicate TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1397 | Load(clause->label()); |
| 1398 | Comparison(eq, true); |
| 1399 | Branch(false, &next); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1402 | // Entering the case statement for the first time. Remove the switch value |
| 1403 | // from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1404 | frame_->Pop(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1405 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1406 | // Generate code for the body. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1407 | // This is also the target for the fall through from the previous case's |
| 1408 | // statements which has to skip over the matching code and the popping of |
| 1409 | // the switch value. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1410 | __ bind(&fall_through); |
| 1411 | fall_through.Unuse(); |
| 1412 | VisitStatements(clause->statements()); |
| 1413 | __ b(&fall_through); |
| 1414 | } |
| 1415 | |
| 1416 | __ bind(&next); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1417 | // Reached the end of the case statements without matching any of the cases. |
| 1418 | if (default_case.is_bound()) { |
| 1419 | // A default case exists -> execute its statements. |
| 1420 | __ b(&default_case); |
| 1421 | } else { |
| 1422 | // Remove the switch value from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1423 | frame_->Pop(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1424 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1425 | |
| 1426 | __ bind(&fall_through); |
| 1427 | __ bind(node->break_target()); |
| 1428 | } |
| 1429 | |
| 1430 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1431 | void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1432 | Comment cmnt(masm_, "[ LoopStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1433 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1434 | node->set_break_stack_height(break_stack_height_); |
| 1435 | |
| 1436 | // simple condition analysis |
| 1437 | enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
| 1438 | if (node->cond() == NULL) { |
| 1439 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1440 | info = ALWAYS_TRUE; |
| 1441 | } else { |
| 1442 | Literal* lit = node->cond()->AsLiteral(); |
| 1443 | if (lit != NULL) { |
| 1444 | if (lit->IsTrue()) { |
| 1445 | info = ALWAYS_TRUE; |
| 1446 | } else if (lit->IsFalse()) { |
| 1447 | info = ALWAYS_FALSE; |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | Label loop, entry; |
| 1453 | |
| 1454 | // init |
| 1455 | if (node->init() != NULL) { |
| 1456 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1457 | Visit(node->init()); |
| 1458 | } |
| 1459 | if (node->type() != LoopStatement::DO_LOOP && info != ALWAYS_TRUE) { |
| 1460 | __ b(&entry); |
| 1461 | } |
| 1462 | |
| 1463 | // body |
| 1464 | __ bind(&loop); |
| 1465 | Visit(node->body()); |
| 1466 | |
| 1467 | // next |
| 1468 | __ bind(node->continue_target()); |
| 1469 | if (node->next() != NULL) { |
| 1470 | // Record source position of the statement as this code which is after the |
| 1471 | // code for the body actually belongs to the loop statement and not the |
| 1472 | // body. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1473 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1474 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1475 | Visit(node->next()); |
| 1476 | } |
| 1477 | |
| 1478 | // cond |
| 1479 | __ bind(&entry); |
| 1480 | switch (info) { |
| 1481 | case ALWAYS_TRUE: |
| 1482 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1483 | __ b(&loop); |
| 1484 | break; |
| 1485 | case ALWAYS_FALSE: |
| 1486 | break; |
| 1487 | case DONT_KNOW: |
| 1488 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1489 | LoadCondition(node->cond(), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1490 | NOT_INSIDE_TYPEOF, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1491 | &loop, |
| 1492 | node->break_target(), |
| 1493 | true); |
| 1494 | Branch(true, &loop); |
| 1495 | break; |
| 1496 | } |
| 1497 | |
| 1498 | // exit |
| 1499 | __ bind(node->break_target()); |
| 1500 | } |
| 1501 | |
| 1502 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1503 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1504 | Comment cmnt(masm_, "[ ForInStatement"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1505 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1506 | |
| 1507 | // We keep stuff on the stack while the body is executing. |
| 1508 | // Record it, so that a break/continue crossing this statement |
| 1509 | // can restore the stack. |
| 1510 | const int kForInStackSize = 5 * kPointerSize; |
| 1511 | break_stack_height_ += kForInStackSize; |
| 1512 | node->set_break_stack_height(break_stack_height_); |
| 1513 | |
| 1514 | Label loop, next, entry, cleanup, exit, primitive, jsobject; |
| 1515 | Label filter_key, end_del_check, fixed_array, non_string; |
| 1516 | |
| 1517 | // Get the object to enumerate over (converted to JSObject). |
| 1518 | Load(node->enumerable()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1519 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1520 | |
| 1521 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1522 | // to the specification. 12.6.4 mandates a call to ToObject. |
| 1523 | __ cmp(r0, Operand(Factory::undefined_value())); |
| 1524 | __ b(eq, &exit); |
| 1525 | __ cmp(r0, Operand(Factory::null_value())); |
| 1526 | __ b(eq, &exit); |
| 1527 | |
| 1528 | // Stack layout in body: |
| 1529 | // [iteration counter (Smi)] |
| 1530 | // [length of array] |
| 1531 | // [FixedArray] |
| 1532 | // [Map or 0] |
| 1533 | // [Object] |
| 1534 | |
| 1535 | // Check if enumerable is already a JSObject |
| 1536 | __ tst(r0, Operand(kSmiTagMask)); |
| 1537 | __ b(eq, &primitive); |
| 1538 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1539 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 1540 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1541 | __ b(hs, &jsobject); |
| 1542 | |
| 1543 | __ bind(&primitive); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1544 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1545 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1546 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1547 | |
| 1548 | |
| 1549 | __ bind(&jsobject); |
| 1550 | |
| 1551 | // Get the set of properties (as a FixedArray or Map). |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1552 | frame_->Push(r0); // duplicate the object being enumerated |
| 1553 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1554 | __ CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
| 1555 | |
| 1556 | // If we got a Map, we can do a fast modification check. |
| 1557 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1558 | __ mov(r2, Operand(r0)); |
| 1559 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1560 | __ cmp(r1, Operand(Factory::meta_map())); |
| 1561 | __ b(ne, &fixed_array); |
| 1562 | |
| 1563 | // Get enum cache |
| 1564 | __ mov(r1, Operand(r0)); |
| 1565 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1566 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1567 | __ ldr(r2, |
| 1568 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1569 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1570 | frame_->Push(r0); // map |
| 1571 | frame_->Push(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1572 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1573 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1574 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1575 | __ mov(r0, Operand(Smi::FromInt(0))); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1576 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1577 | __ b(&entry); |
| 1578 | |
| 1579 | |
| 1580 | __ bind(&fixed_array); |
| 1581 | |
| 1582 | __ mov(r1, Operand(Smi::FromInt(0))); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1583 | frame_->Push(r1); // insert 0 in place of Map |
| 1584 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1585 | |
| 1586 | // 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] | 1587 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1588 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1589 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1590 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1591 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1592 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1593 | __ b(&entry); |
| 1594 | |
| 1595 | // Body. |
| 1596 | __ bind(&loop); |
| 1597 | Visit(node->body()); |
| 1598 | |
| 1599 | // Next. |
| 1600 | __ bind(node->continue_target()); |
| 1601 | __ bind(&next); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1602 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1603 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1604 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1605 | |
| 1606 | // Condition. |
| 1607 | __ bind(&entry); |
| 1608 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1609 | // sp[0] : index |
| 1610 | // sp[1] : array/enum cache length |
| 1611 | // sp[2] : array or enum cache |
| 1612 | // sp[3] : 0 or map |
| 1613 | // sp[4] : enumerable |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1614 | __ ldr(r0, frame_->Element(0)); // load the current count |
| 1615 | __ ldr(r1, frame_->Element(1)); // load the length |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1616 | __ cmp(r0, Operand(r1)); // compare to the array length |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1617 | __ b(hs, &cleanup); |
| 1618 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1619 | __ ldr(r0, frame_->Element(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1620 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1621 | // Get the i'th entry of the array. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1622 | __ ldr(r2, frame_->Element(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1623 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1624 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1625 | |
| 1626 | // Get Map or 0. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1627 | __ ldr(r2, frame_->Element(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1628 | // Check if this (still) matches the map of the enumerable. |
| 1629 | // If not, we have to filter the key. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1630 | __ ldr(r1, frame_->Element(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1631 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1632 | __ cmp(r1, Operand(r2)); |
| 1633 | __ b(eq, &end_del_check); |
| 1634 | |
| 1635 | // Convert the entry to a string (or null if it isn't a property anymore). |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1636 | __ ldr(r0, frame_->Element(4)); // push enumerable |
| 1637 | frame_->Push(r0); |
| 1638 | frame_->Push(r3); // push entry |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1639 | __ mov(r0, Operand(1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1640 | __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1641 | __ mov(r3, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1642 | |
| 1643 | // If the property has been removed while iterating, we just skip it. |
| 1644 | __ cmp(r3, Operand(Factory::null_value())); |
| 1645 | __ b(eq, &next); |
| 1646 | |
| 1647 | |
| 1648 | __ bind(&end_del_check); |
| 1649 | |
| 1650 | // Store the entry in the 'each' expression and take another spin in the loop. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1651 | // r3: i'th entry of the enum cache (or string there of) |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1652 | frame_->Push(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1653 | { Reference each(this, node->each()); |
| 1654 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1655 | if (each.size() > 0) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1656 | __ ldr(r0, frame_->Element(each.size())); |
| 1657 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1658 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1659 | // If the reference was to a slot we rely on the convenient property |
| 1660 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1661 | // right on top of or right underneath a zero-sized reference. |
| 1662 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1663 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1664 | // It's safe to pop the value lying on top of the reference before |
| 1665 | // unloading the reference itself (which preserves the top of stack, |
| 1666 | // ie, now the topmost value of the non-zero sized reference), since |
| 1667 | // we will discard the top of stack after unloading the reference |
| 1668 | // anyway. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1669 | frame_->Pop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1670 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1671 | } |
| 1672 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1673 | // Discard the i'th entry pushed above or else the remainder of the |
| 1674 | // reference, whichever is currently on top of the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1675 | frame_->Pop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1676 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1677 | __ jmp(&loop); |
| 1678 | |
| 1679 | // Cleanup. |
| 1680 | __ bind(&cleanup); |
| 1681 | __ bind(node->break_target()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1682 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1683 | |
| 1684 | // Exit. |
| 1685 | __ bind(&exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1686 | |
| 1687 | break_stack_height_ -= kForInStackSize; |
| 1688 | } |
| 1689 | |
| 1690 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1691 | void CodeGenerator::VisitTryCatch(TryCatch* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1692 | Comment cmnt(masm_, "[ TryCatch"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1693 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1694 | |
| 1695 | Label try_block, exit; |
| 1696 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1697 | __ bl(&try_block); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1698 | // --- Catch block --- |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1699 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1700 | |
| 1701 | // Store the caught exception in the catch variable. |
| 1702 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1703 | ASSERT(ref.is_slot()); |
| 1704 | // Here we make use of the convenient property that it doesn't matter |
| 1705 | // whether a value is immediately on top of or underneath a zero-sized |
| 1706 | // reference. |
| 1707 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | // Remove the exception from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1711 | frame_->Pop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1712 | |
| 1713 | VisitStatements(node->catch_block()->statements()); |
| 1714 | __ b(&exit); |
| 1715 | |
| 1716 | |
| 1717 | // --- Try block --- |
| 1718 | __ bind(&try_block); |
| 1719 | |
| 1720 | __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER); |
| 1721 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1722 | // Shadow the labels for all escapes from the try block, including |
| 1723 | // returns. During shadowing, the original label is hidden as the |
| 1724 | // LabelShadow and operations on the original actually affect the |
| 1725 | // shadowing label. |
| 1726 | // |
| 1727 | // We should probably try to unify the escaping labels and the return |
| 1728 | // label. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1729 | int nof_escapes = node->escaping_labels()->length(); |
| 1730 | List<LabelShadow*> shadows(1 + nof_escapes); |
| 1731 | shadows.Add(new LabelShadow(&function_return_)); |
| 1732 | for (int i = 0; i < nof_escapes; i++) { |
| 1733 | shadows.Add(new LabelShadow(node->escaping_labels()->at(i))); |
| 1734 | } |
| 1735 | |
| 1736 | // Generate code for the statements in the try block. |
| 1737 | VisitStatements(node->try_block()->statements()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1738 | frame_->Pop(); // Discard the result. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1739 | |
| 1740 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1741 | // After shadowing stops, the original labels are unshadowed and the |
| 1742 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1743 | int nof_unlinks = 0; |
| 1744 | for (int i = 0; i <= nof_escapes; i++) { |
| 1745 | shadows[i]->StopShadowing(); |
| 1746 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 1747 | } |
| 1748 | |
| 1749 | // Unlink from try chain. |
| 1750 | // TOS contains code slot |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1751 | const int kNextIndex = (StackHandlerConstants::kNextOffset |
| 1752 | + StackHandlerConstants::kAddressDisplacement) |
| 1753 | / kPointerSize; |
| 1754 | __ ldr(r1, frame_->Element(kNextIndex)); // read next_sp |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1755 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 1756 | __ str(r1, MemOperand(r3)); |
| 1757 | ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1758 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1759 | // Code slot popped. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1760 | if (nof_unlinks > 0) __ b(&exit); |
| 1761 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1762 | // Generate unlink code for the (formerly) shadowing labels that have been |
| 1763 | // jumped to. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1764 | for (int i = 0; i <= nof_escapes; i++) { |
| 1765 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1766 | // Unlink from try chain; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1767 | __ bind(shadows[i]); |
| 1768 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1769 | // Reload sp from the top handler, because some statements that we |
| 1770 | // break from (eg, for...in) may have left stuff on the stack. |
| 1771 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 1772 | __ ldr(sp, MemOperand(r3)); |
| 1773 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1774 | __ ldr(r1, frame_->Element(kNextIndex)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1775 | __ str(r1, MemOperand(r3)); |
| 1776 | ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1777 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1778 | // Code slot popped. |
| 1779 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1780 | __ b(shadows[i]->original_label()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | __ bind(&exit); |
| 1785 | } |
| 1786 | |
| 1787 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1788 | void CodeGenerator::VisitTryFinally(TryFinally* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1789 | Comment cmnt(masm_, "[ TryFinally"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1790 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1791 | |
| 1792 | // State: Used to keep track of reason for entering the finally |
| 1793 | // block. Should probably be extended to hold information for |
| 1794 | // break/continue from within the try block. |
| 1795 | enum { FALLING, THROWING, JUMPING }; |
| 1796 | |
| 1797 | Label exit, unlink, try_block, finally_block; |
| 1798 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1799 | __ bl(&try_block); |
| 1800 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1801 | frame_->Push(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1802 | // In case of thrown exceptions, this is where we continue. |
| 1803 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
| 1804 | __ b(&finally_block); |
| 1805 | |
| 1806 | |
| 1807 | // --- Try block --- |
| 1808 | __ bind(&try_block); |
| 1809 | |
| 1810 | __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER); |
| 1811 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1812 | // Shadow the labels for all escapes from the try block, including |
| 1813 | // returns. Shadowing hides the original label as the LabelShadow and |
| 1814 | // operations on the original actually affect the shadowing label. |
| 1815 | // |
| 1816 | // We should probably try to unify the escaping labels and the return |
| 1817 | // label. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1818 | int nof_escapes = node->escaping_labels()->length(); |
| 1819 | List<LabelShadow*> shadows(1 + nof_escapes); |
| 1820 | shadows.Add(new LabelShadow(&function_return_)); |
| 1821 | for (int i = 0; i < nof_escapes; i++) { |
| 1822 | shadows.Add(new LabelShadow(node->escaping_labels()->at(i))); |
| 1823 | } |
| 1824 | |
| 1825 | // Generate code for the statements in the try block. |
| 1826 | VisitStatements(node->try_block()->statements()); |
| 1827 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1828 | // Stop the introduced shadowing and count the number of required unlinks. |
| 1829 | // After shadowing stops, the original labels are unshadowed and the |
| 1830 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1831 | int nof_unlinks = 0; |
| 1832 | for (int i = 0; i <= nof_escapes; i++) { |
| 1833 | shadows[i]->StopShadowing(); |
| 1834 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 1835 | } |
| 1836 | |
| 1837 | // Set the state on the stack to FALLING. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1838 | __ mov(r0, Operand(Factory::undefined_value())); // fake TOS |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1839 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1840 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 1841 | if (nof_unlinks > 0) __ b(&unlink); |
| 1842 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1843 | // Generate code to set the state for the (formerly) shadowing labels that |
| 1844 | // have been jumped to. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1845 | for (int i = 0; i <= nof_escapes; i++) { |
| 1846 | if (shadows[i]->is_linked()) { |
| 1847 | __ bind(shadows[i]); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1848 | if (shadows[i]->original_label() == &function_return_) { |
| 1849 | // If this label shadowed the function return, materialize the |
| 1850 | // return value on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1851 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1852 | } else { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1853 | // Fake TOS for labels that shadowed breaks and continues. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1854 | __ mov(r0, Operand(Factory::undefined_value())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1855 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1856 | } |
| 1857 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 1858 | __ b(&unlink); |
| 1859 | } |
| 1860 | } |
| 1861 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1862 | // Unlink from try chain; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1863 | __ bind(&unlink); |
| 1864 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1865 | frame_->Pop(r0); // Store TOS in r0 across stack manipulation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1866 | // Reload sp from the top handler, because some statements that we |
| 1867 | // break from (eg, for...in) may have left stuff on the stack. |
| 1868 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 1869 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1870 | const int kNextIndex = (StackHandlerConstants::kNextOffset |
| 1871 | + StackHandlerConstants::kAddressDisplacement) |
| 1872 | / kPointerSize; |
| 1873 | __ ldr(r1, frame_->Element(kNextIndex)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1874 | __ str(r1, MemOperand(r3)); |
| 1875 | ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1876 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1877 | // Code slot popped. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1878 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1879 | |
| 1880 | // --- Finally block --- |
| 1881 | __ bind(&finally_block); |
| 1882 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1883 | // Push the state on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1884 | frame_->Push(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1885 | |
| 1886 | // We keep two elements on the stack - the (possibly faked) result |
| 1887 | // and the state - while evaluating the finally block. Record it, so |
| 1888 | // that a break/continue crossing this statement can restore the |
| 1889 | // stack. |
| 1890 | const int kFinallyStackSize = 2 * kPointerSize; |
| 1891 | break_stack_height_ += kFinallyStackSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1892 | |
| 1893 | // Generate code for the statements in the finally block. |
| 1894 | VisitStatements(node->finally_block()->statements()); |
| 1895 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1896 | // Restore state and return value or faked TOS. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1897 | frame_->Pop(r2); |
| 1898 | frame_->Pop(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1899 | break_stack_height_ -= kFinallyStackSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1900 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1901 | // Generate code to jump to the right destination for all used (formerly) |
| 1902 | // shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1903 | for (int i = 0; i <= nof_escapes; i++) { |
| 1904 | if (shadows[i]->is_bound()) { |
| 1905 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1906 | if (shadows[i]->original_label() != &function_return_) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1907 | Label next; |
| 1908 | __ b(ne, &next); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1909 | __ b(shadows[i]->original_label()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1910 | __ bind(&next); |
| 1911 | } else { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1912 | __ b(eq, shadows[i]->original_label()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | // Check if we need to rethrow the exception. |
| 1918 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 1919 | __ b(ne, &exit); |
| 1920 | |
| 1921 | // Rethrow exception. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1922 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1923 | __ CallRuntime(Runtime::kReThrow, 1); |
| 1924 | |
| 1925 | // Done. |
| 1926 | __ bind(&exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1930 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1931 | Comment cmnt(masm_, "[ DebuggerStatament"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1932 | CodeForStatement(node); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1933 | __ CallRuntime(Runtime::kDebugBreak, 0); |
| 1934 | // Ignore the return value. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1938 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1939 | ASSERT(boilerplate->IsBoilerplate()); |
| 1940 | |
| 1941 | // Push the boilerplate on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1942 | __ mov(r0, Operand(boilerplate)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1943 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1944 | |
| 1945 | // Create a new closure. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1946 | frame_->Push(cp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1947 | __ CallRuntime(Runtime::kNewClosure, 2); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1948 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1952 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1953 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 1954 | |
| 1955 | // Build the function boilerplate and instantiate it. |
| 1956 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1957 | // Check for stack-overflow exception. |
| 1958 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1959 | InstantiateBoilerplate(boilerplate); |
| 1960 | } |
| 1961 | |
| 1962 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1963 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1964 | FunctionBoilerplateLiteral* node) { |
| 1965 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 1966 | InstantiateBoilerplate(node->boilerplate()); |
| 1967 | } |
| 1968 | |
| 1969 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1970 | void CodeGenerator::VisitConditional(Conditional* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1971 | Comment cmnt(masm_, "[ Conditional"); |
| 1972 | Label then, else_, exit; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1973 | LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1974 | Branch(false, &else_); |
| 1975 | __ bind(&then); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1976 | Load(node->then_expression(), typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1977 | __ b(&exit); |
| 1978 | __ bind(&else_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1979 | Load(node->else_expression(), typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1980 | __ bind(&exit); |
| 1981 | } |
| 1982 | |
| 1983 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1984 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
| 1985 | if (slot->type() == Slot::LOOKUP) { |
| 1986 | ASSERT(slot->var()->mode() == Variable::DYNAMIC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1987 | |
| 1988 | // For now, just do a runtime call. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1989 | frame_->Push(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1990 | __ mov(r0, Operand(slot->var()->name())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1991 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1992 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1993 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1994 | __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1995 | } else { |
| 1996 | __ CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1997 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1998 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1999 | |
| 2000 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2001 | // Note: We would like to keep the assert below, but it fires because of |
| 2002 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2003 | // ASSERT(slot->var()->mode() != Variable::DYNAMIC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2004 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2005 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2006 | __ ldr(r0, SlotOperand(slot, r2)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2007 | frame_->Push(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2008 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2009 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2010 | // initialized yet) which needs to be converted into the 'undefined' |
| 2011 | // value. |
| 2012 | Comment cmnt(masm_, "[ Unhole const"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2013 | frame_->Pop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2014 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2015 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2016 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2022 | void CodeGenerator::VisitSlot(Slot* node) { |
| 2023 | Comment cmnt(masm_, "[ Slot"); |
| 2024 | LoadFromSlot(node, typeof_state()); |
| 2025 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2026 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2027 | |
| 2028 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
| 2029 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2030 | |
| 2031 | Variable* var = node->var(); |
| 2032 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2033 | if (expr != NULL) { |
| 2034 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2035 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2036 | ASSERT(var->is_global()); |
| 2037 | Reference ref(this, node); |
| 2038 | ref.GetValue(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2043 | void CodeGenerator::VisitLiteral(Literal* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2044 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2045 | __ mov(r0, Operand(node->handle())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2046 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2050 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2052 | |
| 2053 | // Retrieve the literal array and check the allocated entry. |
| 2054 | |
| 2055 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2056 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Load the literals array of the function. |
| 2059 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2060 | |
| 2061 | // Load the literal at the ast saved index. |
| 2062 | int literal_offset = |
| 2063 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2064 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2065 | |
| 2066 | Label done; |
| 2067 | __ cmp(r2, Operand(Factory::undefined_value())); |
| 2068 | __ b(ne, &done); |
| 2069 | |
| 2070 | // If the entry is undefined we call the runtime system to computed |
| 2071 | // the literal. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2072 | frame_->Push(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2073 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2074 | frame_->Push(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2075 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2076 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2077 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2078 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2079 | __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2080 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2081 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2082 | __ bind(&done); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2083 | // Push the literal. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2084 | frame_->Push(r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | |
| 2088 | // This deferred code stub will be used for creating the boilerplate |
| 2089 | // by calling Runtime_CreateObjectLiteral. |
| 2090 | // Each created boilerplate is stored in the JSFunction and they are |
| 2091 | // therefore context dependent. |
| 2092 | class ObjectLiteralDeferred: public DeferredCode { |
| 2093 | public: |
| 2094 | ObjectLiteralDeferred(CodeGenerator* generator, ObjectLiteral* node) |
| 2095 | : DeferredCode(generator), node_(node) { |
| 2096 | set_comment("[ ObjectLiteralDeferred"); |
| 2097 | } |
| 2098 | virtual void Generate(); |
| 2099 | private: |
| 2100 | ObjectLiteral* node_; |
| 2101 | }; |
| 2102 | |
| 2103 | |
| 2104 | void ObjectLiteralDeferred::Generate() { |
| 2105 | // If the entry is undefined we call the runtime system to computed |
| 2106 | // the literal. |
| 2107 | |
| 2108 | // Literal array (0). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2109 | __ push(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2110 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2111 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
| 2112 | __ push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2113 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2114 | __ mov(r0, Operand(node_->constant_properties())); |
| 2115 | __ push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2116 | __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2117 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2121 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2122 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2123 | |
| 2124 | ObjectLiteralDeferred* deferred = new ObjectLiteralDeferred(this, node); |
| 2125 | |
| 2126 | // Retrieve the literal array and check the allocated entry. |
| 2127 | |
| 2128 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2129 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2130 | |
| 2131 | // Load the literals array of the function. |
| 2132 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2133 | |
| 2134 | // Load the literal at the ast saved index. |
| 2135 | int literal_offset = |
| 2136 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2137 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2138 | |
| 2139 | // Check whether we need to materialize the object literal boilerplate. |
| 2140 | // If so, jump to the deferred code. |
| 2141 | __ cmp(r2, Operand(Factory::undefined_value())); |
| 2142 | __ b(eq, deferred->enter()); |
| 2143 | __ bind(deferred->exit()); |
| 2144 | |
| 2145 | // Push the object literal boilerplate. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2146 | frame_->Push(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2147 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2148 | // Clone the boilerplate object. |
| 2149 | __ CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2150 | frame_->Push(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2151 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2152 | |
| 2153 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2154 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2155 | Literal* key = property->key(); |
| 2156 | Expression* value = property->value(); |
| 2157 | switch (property->kind()) { |
| 2158 | case ObjectLiteral::Property::CONSTANT: break; |
| 2159 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2160 | case ObjectLiteral::Property::PROTOTYPE: { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2161 | frame_->Push(r0); // dup the result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2162 | Load(key); |
| 2163 | Load(value); |
| 2164 | __ CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2165 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2166 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2167 | break; |
| 2168 | } |
| 2169 | case ObjectLiteral::Property::SETTER: { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2170 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2171 | Load(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2172 | __ mov(r0, Operand(Smi::FromInt(1))); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2173 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2174 | Load(value); |
| 2175 | __ CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2176 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2177 | break; |
| 2178 | } |
| 2179 | case ObjectLiteral::Property::GETTER: { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2180 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2181 | Load(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2182 | __ mov(r0, Operand(Smi::FromInt(0))); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2183 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2184 | Load(value); |
| 2185 | __ CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2186 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2187 | break; |
| 2188 | } |
| 2189 | } |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2194 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2195 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2196 | |
| 2197 | // Call runtime to create the array literal. |
| 2198 | __ mov(r0, Operand(node->literals())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2199 | frame_->Push(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2200 | // Load the function of this frame. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2201 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2202 | __ ldr(r0, FieldMemOperand(r0, JSFunction::kLiteralsOffset)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2203 | frame_->Push(r0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2204 | __ CallRuntime(Runtime::kCreateArrayLiteral, 2); |
| 2205 | |
| 2206 | // Push the resulting array literal on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2207 | frame_->Push(r0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2208 | |
| 2209 | // Generate code to set the elements in the array that are not |
| 2210 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2211 | for (int i = 0; i < node->values()->length(); i++) { |
| 2212 | Expression* value = node->values()->at(i); |
| 2213 | |
| 2214 | // If value is literal the property value is already |
| 2215 | // set in the boilerplate object. |
| 2216 | if (value->AsLiteral() == NULL) { |
| 2217 | // The property must be set by generated code. |
| 2218 | Load(value); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2219 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2220 | |
| 2221 | // Fetch the object literal |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2222 | __ ldr(r1, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2223 | // Get the elements array. |
| 2224 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 2225 | |
| 2226 | // Write to the indexed properties array. |
| 2227 | int offset = i * kPointerSize + Array::kHeaderSize; |
| 2228 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2229 | |
| 2230 | // Update the write barrier for the array address. |
| 2231 | __ mov(r3, Operand(offset)); |
| 2232 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame^] | 2238 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
| 2239 | // Call runtime routine to allocate the catch extension object and |
| 2240 | // assign the exception value to the catch variable. |
| 2241 | Comment cmnt(masm_, "[CatchExtensionObject "); |
| 2242 | Load(node->key()); |
| 2243 | Load(node->value()); |
| 2244 | __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2245 | frame_->Push(r0); |
| 2246 | } |
| 2247 | |
| 2248 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2249 | void CodeGenerator::VisitAssignment(Assignment* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2250 | Comment cmnt(masm_, "[ Assignment"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2251 | CodeForStatement(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2252 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2253 | Reference target(this, node->target()); |
| 2254 | if (target.is_illegal()) return; |
| 2255 | |
| 2256 | if (node->op() == Token::ASSIGN || |
| 2257 | node->op() == Token::INIT_VAR || |
| 2258 | node->op() == Token::INIT_CONST) { |
| 2259 | Load(node->value()); |
| 2260 | |
| 2261 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2262 | target.GetValue(NOT_INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2263 | Literal* literal = node->value()->AsLiteral(); |
| 2264 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 2265 | SmiOperation(node->binary_op(), literal->handle(), false); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2266 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2267 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2268 | } else { |
| 2269 | Load(node->value()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2270 | GenericBinaryOperation(node->binary_op()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2271 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2276 | if (var != NULL && |
| 2277 | (var->mode() == Variable::CONST) && |
| 2278 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2279 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2280 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2281 | } else { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2282 | CodeForSourcePosition(node->position()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2283 | if (node->op() == Token::INIT_CONST) { |
| 2284 | // Dynamic constant initializations must use the function context |
| 2285 | // and initialize the actual constant declared. Dynamic variable |
| 2286 | // initializations are simply assignments and use SetValue. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2287 | target.SetValue(CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2288 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2289 | target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2295 | void CodeGenerator::VisitThrow(Throw* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2296 | Comment cmnt(masm_, "[ Throw"); |
| 2297 | |
| 2298 | Load(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2299 | CodeForSourcePosition(node->position()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2300 | __ CallRuntime(Runtime::kThrow, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2301 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2305 | void CodeGenerator::VisitProperty(Property* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2306 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2307 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2308 | Reference property(this, node); |
| 2309 | property.GetValue(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2313 | void CodeGenerator::VisitCall(Call* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2314 | Comment cmnt(masm_, "[ Call"); |
| 2315 | |
| 2316 | ZoneList<Expression*>* args = node->arguments(); |
| 2317 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2318 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2319 | // Standard function call. |
| 2320 | |
| 2321 | // Check if the function is a variable or a property. |
| 2322 | Expression* function = node->expression(); |
| 2323 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2324 | Property* property = function->AsProperty(); |
| 2325 | |
| 2326 | // ------------------------------------------------------------------------ |
| 2327 | // Fast-case: Use inline caching. |
| 2328 | // --- |
| 2329 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2330 | // must be resolved after the arguments have been evaluated. The IC code |
| 2331 | // automatically handles this by loading the arguments before the function |
| 2332 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2333 | // ------------------------------------------------------------------------ |
| 2334 | |
| 2335 | if (var != NULL && !var->is_this() && var->is_global()) { |
| 2336 | // ---------------------------------- |
| 2337 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 2338 | // ---------------------------------- |
| 2339 | |
| 2340 | // 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] | 2341 | __ mov(r0, Operand(var->name())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2342 | frame_->Push(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2343 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2344 | // Pass the global object as the receiver and let the IC stub |
| 2345 | // patch the stack to use the global proxy as 'this' in the |
| 2346 | // invoked function. |
| 2347 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2348 | |
| 2349 | // Load the arguments. |
| 2350 | for (int i = 0; i < args->length(); i++) Load(args->at(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2351 | |
| 2352 | // Setup the receiver register and call the IC initialization code. |
| 2353 | Handle<Code> stub = ComputeCallInitialize(args->length()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2354 | CodeForSourcePosition(node->position()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2355 | __ Call(stub, RelocInfo::CODE_TARGET_CONTEXT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2356 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2357 | // Remove the function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2358 | frame_->Pop(); |
| 2359 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2360 | |
| 2361 | } else if (var != NULL && var->slot() != NULL && |
| 2362 | var->slot()->type() == Slot::LOOKUP) { |
| 2363 | // ---------------------------------- |
| 2364 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 2365 | // ---------------------------------- |
| 2366 | |
| 2367 | // Load the function |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2368 | frame_->Push(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2369 | __ mov(r0, Operand(var->name())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2370 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2371 | __ CallRuntime(Runtime::kLoadContextSlot, 2); |
| 2372 | // r0: slot value; r1: receiver |
| 2373 | |
| 2374 | // Load the receiver. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2375 | frame_->Push(r0); // function |
| 2376 | frame_->Push(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2377 | |
| 2378 | // Call the function. |
| 2379 | CallWithArguments(args, node->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2380 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2381 | |
| 2382 | } else if (property != NULL) { |
| 2383 | // Check if the key is a literal string. |
| 2384 | Literal* literal = property->key()->AsLiteral(); |
| 2385 | |
| 2386 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 2387 | // ------------------------------------------------------------------ |
| 2388 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 2389 | // ------------------------------------------------------------------ |
| 2390 | |
| 2391 | // 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] | 2392 | __ mov(r0, Operand(literal->handle())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2393 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2394 | Load(property->obj()); |
| 2395 | |
| 2396 | // Load the arguments. |
| 2397 | for (int i = 0; i < args->length(); i++) Load(args->at(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2398 | |
| 2399 | // Set the receiver register and call the IC initialization code. |
| 2400 | Handle<Code> stub = ComputeCallInitialize(args->length()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2401 | CodeForSourcePosition(node->position()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2402 | __ Call(stub, RelocInfo::CODE_TARGET); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2403 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2404 | |
| 2405 | // Remove the function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2406 | frame_->Pop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2407 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2408 | frame_->Push(r0); // push after get rid of function from the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2409 | |
| 2410 | } else { |
| 2411 | // ------------------------------------------- |
| 2412 | // JavaScript example: 'array[index](1, 2, 3)' |
| 2413 | // ------------------------------------------- |
| 2414 | |
| 2415 | // Load the function to call from the property through a reference. |
| 2416 | Reference ref(this, property); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2417 | ref.GetValue(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2418 | |
| 2419 | // Pass receiver to called function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2420 | __ ldr(r0, frame_->Element(ref.size())); |
| 2421 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2422 | // Call the function. |
| 2423 | CallWithArguments(args, node->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2424 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | } else { |
| 2428 | // ---------------------------------- |
| 2429 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 2430 | // ---------------------------------- |
| 2431 | |
| 2432 | // Load the function. |
| 2433 | Load(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2434 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2435 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2436 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2437 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2438 | // Call the function. |
| 2439 | CallWithArguments(args, node->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2440 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2445 | void CodeGenerator::VisitCallEval(CallEval* node) { |
| 2446 | Comment cmnt(masm_, "[ CallEval"); |
| 2447 | |
| 2448 | // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve |
| 2449 | // the function we need to call and the receiver of the call. |
| 2450 | // Then we call the resolved function using the given arguments. |
| 2451 | |
| 2452 | ZoneList<Expression*>* args = node->arguments(); |
| 2453 | Expression* function = node->expression(); |
| 2454 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2455 | CodeForStatement(node); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2456 | |
| 2457 | // Prepare stack for call to resolved function. |
| 2458 | Load(function); |
| 2459 | __ mov(r2, Operand(Factory::undefined_value())); |
| 2460 | __ push(r2); // Slot for receiver |
| 2461 | for (int i = 0; i < args->length(); i++) { |
| 2462 | Load(args->at(i)); |
| 2463 | } |
| 2464 | |
| 2465 | // Prepare stack for call to ResolvePossiblyDirectEval. |
| 2466 | __ ldr(r1, MemOperand(sp, args->length() * kPointerSize + kPointerSize)); |
| 2467 | __ push(r1); |
| 2468 | if (args->length() > 0) { |
| 2469 | __ ldr(r1, MemOperand(sp, args->length() * kPointerSize)); |
| 2470 | __ push(r1); |
| 2471 | } else { |
| 2472 | __ push(r2); |
| 2473 | } |
| 2474 | |
| 2475 | // Resolve the call. |
| 2476 | __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
| 2477 | |
| 2478 | // Touch up stack with the right values for the function and the receiver. |
| 2479 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
| 2480 | __ str(r1, MemOperand(sp, (args->length() + 1) * kPointerSize)); |
| 2481 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
| 2482 | __ str(r1, MemOperand(sp, args->length() * kPointerSize)); |
| 2483 | |
| 2484 | // Call the function. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2485 | CodeForSourcePosition(node->position()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2486 | |
| 2487 | CallFunctionStub call_function(args->length()); |
| 2488 | __ CallStub(&call_function); |
| 2489 | |
| 2490 | __ ldr(cp, frame_->Context()); |
| 2491 | // Remove the function from the stack. |
| 2492 | frame_->Pop(); |
| 2493 | frame_->Push(r0); |
| 2494 | } |
| 2495 | |
| 2496 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2497 | void CodeGenerator::VisitCallNew(CallNew* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2498 | Comment cmnt(masm_, "[ CallNew"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2499 | CodeForStatement(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2500 | |
| 2501 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 2502 | // expression in new calls must be evaluated before the |
| 2503 | // arguments. This is different from ordinary calls, where the |
| 2504 | // actual function to call is resolved after the arguments have been |
| 2505 | // evaluated. |
| 2506 | |
| 2507 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2508 | // receiver. There is no need to use the global proxy here because |
| 2509 | // it will always be replaced with a newly allocated object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2510 | Load(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2511 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2512 | |
| 2513 | // Push the arguments ("left-to-right") on the stack. |
| 2514 | ZoneList<Expression*>* args = node->arguments(); |
| 2515 | for (int i = 0; i < args->length(); i++) Load(args->at(i)); |
| 2516 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2517 | // r0: the number of arguments. |
| 2518 | __ mov(r0, Operand(args->length())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2519 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2520 | // Load the function into r1 as per calling convention. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2521 | __ ldr(r1, frame_->Element(args->length() + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2522 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2523 | // Call the construct call builtin that handles allocation and |
| 2524 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2525 | CodeForSourcePosition(node->position()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2526 | __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)), |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2527 | RelocInfo::CONSTRUCT_CALL); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2528 | |
| 2529 | // 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] | 2530 | __ str(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2534 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2535 | ASSERT(args->length() == 1); |
| 2536 | Label leave; |
| 2537 | Load(args->at(0)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2538 | frame_->Pop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2539 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2540 | __ tst(r0, Operand(kSmiTagMask)); |
| 2541 | __ b(eq, &leave); |
| 2542 | // It is a heap object - get map. |
| 2543 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 2544 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2545 | // if (!object->IsJSValue()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2546 | __ cmp(r1, Operand(JS_VALUE_TYPE)); |
| 2547 | __ b(ne, &leave); |
| 2548 | // Load the value. |
| 2549 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
| 2550 | __ bind(&leave); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2551 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2555 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2556 | ASSERT(args->length() == 2); |
| 2557 | Label leave; |
| 2558 | Load(args->at(0)); // Load the object. |
| 2559 | Load(args->at(1)); // Load the value. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2560 | frame_->Pop(r0); // r0 contains value |
| 2561 | frame_->Pop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2562 | // if (object->IsSmi()) return object. |
| 2563 | __ tst(r1, Operand(kSmiTagMask)); |
| 2564 | __ b(eq, &leave); |
| 2565 | // It is a heap object - get map. |
| 2566 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 2567 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 2568 | // if (!object->IsJSValue()) return object. |
| 2569 | __ cmp(r2, Operand(JS_VALUE_TYPE)); |
| 2570 | __ b(ne, &leave); |
| 2571 | // Store the value. |
| 2572 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 2573 | // Update the write barrier. |
| 2574 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 2575 | __ RecordWrite(r1, r2, r3); |
| 2576 | // Leave. |
| 2577 | __ bind(&leave); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2578 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2582 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2583 | ASSERT(args->length() == 1); |
| 2584 | Load(args->at(0)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2585 | frame_->Pop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2586 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2587 | cc_reg_ = eq; |
| 2588 | } |
| 2589 | |
| 2590 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2591 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
| 2592 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 2593 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 2594 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2595 | if (ShouldGenerateLog(args->at(0))) { |
| 2596 | Load(args->at(1)); |
| 2597 | Load(args->at(2)); |
| 2598 | __ CallRuntime(Runtime::kLog, 2); |
| 2599 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 2600 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2601 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2602 | frame_->Push(r0); |
| 2603 | } |
| 2604 | |
| 2605 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2606 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 2607 | ASSERT(args->length() == 1); |
| 2608 | Load(args->at(0)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2609 | frame_->Pop(r0); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 2610 | __ tst(r0, Operand(kSmiTagMask | 0x80000000)); |
| 2611 | cc_reg_ = eq; |
| 2612 | } |
| 2613 | |
| 2614 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2615 | // This should generate code that performs a charCodeAt() call or returns |
| 2616 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 2617 | // 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] | 2618 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2619 | ASSERT(args->length() == 2); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2620 | __ mov(r0, Operand(Factory::undefined_value())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2621 | frame_->Push(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2625 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2626 | ASSERT(args->length() == 1); |
| 2627 | Load(args->at(0)); |
| 2628 | Label answer; |
| 2629 | // We need the CC bits to come out as not_equal in the case where the |
| 2630 | // object is a smi. This can't be done with the usual test opcode so |
| 2631 | // we use XOR to get the right CC bits. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2632 | frame_->Pop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2633 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 2634 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
| 2635 | __ b(ne, &answer); |
| 2636 | // It is a heap object - get the map. |
| 2637 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 2638 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 2639 | // Check if the object is a JS array or not. |
| 2640 | __ cmp(r1, Operand(JS_ARRAY_TYPE)); |
| 2641 | __ bind(&answer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2642 | cc_reg_ = eq; |
| 2643 | } |
| 2644 | |
| 2645 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2646 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2647 | ASSERT(args->length() == 0); |
| 2648 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2649 | // Seed the result with the formal parameters count, which will be used |
| 2650 | // 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] | 2651 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 2652 | |
| 2653 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2654 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2655 | __ CallStub(&stub); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2656 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2660 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2661 | ASSERT(args->length() == 1); |
| 2662 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2663 | // Satisfy contract with ArgumentsAccessStub: |
| 2664 | // Load the key into r1 and the formal parameters count into r0. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2665 | Load(args->at(0)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2666 | frame_->Pop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2667 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2668 | |
| 2669 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2670 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2671 | __ CallStub(&stub); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2672 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2676 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2677 | ASSERT(args->length() == 2); |
| 2678 | |
| 2679 | // Load the two objects into registers and perform the comparison. |
| 2680 | Load(args->at(0)); |
| 2681 | Load(args->at(1)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2682 | frame_->Pop(r0); |
| 2683 | frame_->Pop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2684 | __ cmp(r0, Operand(r1)); |
| 2685 | cc_reg_ = eq; |
| 2686 | } |
| 2687 | |
| 2688 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2689 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2690 | if (CheckForInlineRuntimeCall(node)) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2691 | |
| 2692 | ZoneList<Expression*>* args = node->arguments(); |
| 2693 | Comment cmnt(masm_, "[ CallRuntime"); |
| 2694 | Runtime::Function* function = node->function(); |
| 2695 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2696 | if (function != NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2697 | // Push the arguments ("left-to-right"). |
| 2698 | for (int i = 0; i < args->length(); i++) Load(args->at(i)); |
| 2699 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2700 | // Call the C runtime function. |
| 2701 | __ CallRuntime(function, args->length()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2702 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2703 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2704 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2705 | // Prepare stack for calling JS runtime function. |
| 2706 | __ mov(r0, Operand(node->name())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2707 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2708 | // Push the builtins object found in the current global object. |
| 2709 | __ ldr(r1, GlobalObject()); |
| 2710 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2711 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2712 | |
| 2713 | for (int i = 0; i < args->length(); i++) Load(args->at(i)); |
| 2714 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2715 | // Call the JS runtime function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2716 | Handle<Code> stub = ComputeCallInitialize(args->length()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2717 | __ Call(stub, RelocInfo::CODE_TARGET); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2718 | __ ldr(cp, frame_->Context()); |
| 2719 | frame_->Pop(); |
| 2720 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2725 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2726 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 2727 | |
| 2728 | Token::Value op = node->op(); |
| 2729 | |
| 2730 | if (op == Token::NOT) { |
| 2731 | LoadCondition(node->expression(), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2732 | NOT_INSIDE_TYPEOF, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2733 | false_target(), |
| 2734 | true_target(), |
| 2735 | true); |
| 2736 | cc_reg_ = NegateCondition(cc_reg_); |
| 2737 | |
| 2738 | } else if (op == Token::DELETE) { |
| 2739 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2740 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2741 | if (property != NULL) { |
| 2742 | Load(property->obj()); |
| 2743 | Load(property->key()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2744 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2745 | __ InvokeBuiltin(Builtins::DELETE, CALL_JS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2746 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2747 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2748 | Slot* slot = variable->slot(); |
| 2749 | if (variable->is_global()) { |
| 2750 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2751 | __ mov(r0, Operand(variable->name())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2752 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2753 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2754 | __ InvokeBuiltin(Builtins::DELETE, CALL_JS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2755 | |
| 2756 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 2757 | // lookup the context holding the named variable |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2758 | frame_->Push(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2759 | __ mov(r0, Operand(variable->name())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2760 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2761 | __ CallRuntime(Runtime::kLookupContext, 2); |
| 2762 | // r0: context |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2763 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2764 | __ mov(r0, Operand(variable->name())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2765 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2766 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2767 | __ InvokeBuiltin(Builtins::DELETE, CALL_JS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2768 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2769 | } else { |
| 2770 | // Default: Result of deleting non-global, not dynamically |
| 2771 | // introduced variables is false. |
| 2772 | __ mov(r0, Operand(Factory::false_value())); |
| 2773 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2774 | |
| 2775 | } else { |
| 2776 | // Default: Result of deleting expressions is true. |
| 2777 | Load(node->expression()); // may have side-effects |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2778 | frame_->Pop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2779 | __ mov(r0, Operand(Factory::true_value())); |
| 2780 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2781 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2782 | |
| 2783 | } else if (op == Token::TYPEOF) { |
| 2784 | // Special case for loading the typeof expression; see comment on |
| 2785 | // LoadTypeofExpression(). |
| 2786 | LoadTypeofExpression(node->expression()); |
| 2787 | __ CallRuntime(Runtime::kTypeof, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2788 | frame_->Push(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2789 | |
| 2790 | } else { |
| 2791 | Load(node->expression()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2792 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2793 | switch (op) { |
| 2794 | case Token::NOT: |
| 2795 | case Token::DELETE: |
| 2796 | case Token::TYPEOF: |
| 2797 | UNREACHABLE(); // handled above |
| 2798 | break; |
| 2799 | |
| 2800 | case Token::SUB: { |
| 2801 | UnarySubStub stub; |
| 2802 | __ CallStub(&stub); |
| 2803 | break; |
| 2804 | } |
| 2805 | |
| 2806 | case Token::BIT_NOT: { |
| 2807 | // smi check |
| 2808 | Label smi_label; |
| 2809 | Label continue_label; |
| 2810 | __ tst(r0, Operand(kSmiTagMask)); |
| 2811 | __ b(eq, &smi_label); |
| 2812 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2813 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2814 | __ mov(r0, Operand(0)); // not counting receiver |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2815 | __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2816 | |
| 2817 | __ b(&continue_label); |
| 2818 | __ bind(&smi_label); |
| 2819 | __ mvn(r0, Operand(r0)); |
| 2820 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
| 2821 | __ bind(&continue_label); |
| 2822 | break; |
| 2823 | } |
| 2824 | |
| 2825 | case Token::VOID: |
| 2826 | // since the stack top is cached in r0, popping and then |
| 2827 | // pushing a value can be done by just writing to r0. |
| 2828 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2829 | break; |
| 2830 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2831 | case Token::ADD: { |
| 2832 | // Smi check. |
| 2833 | Label continue_label; |
| 2834 | __ tst(r0, Operand(kSmiTagMask)); |
| 2835 | __ b(eq, &continue_label); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2836 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2837 | __ mov(r0, Operand(0)); // not counting receiver |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2838 | __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2839 | __ bind(&continue_label); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2840 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2841 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2842 | default: |
| 2843 | UNREACHABLE(); |
| 2844 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2845 | frame_->Push(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2850 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2851 | Comment cmnt(masm_, "[ CountOperation"); |
| 2852 | |
| 2853 | bool is_postfix = node->is_postfix(); |
| 2854 | bool is_increment = node->op() == Token::INC; |
| 2855 | |
| 2856 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 2857 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 2858 | |
| 2859 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2860 | if (is_postfix) { |
| 2861 | __ mov(r0, Operand(0)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2862 | frame_->Push(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2863 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2864 | |
| 2865 | { Reference target(this, node->expression()); |
| 2866 | if (target.is_illegal()) return; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2867 | target.GetValue(NOT_INSIDE_TYPEOF); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2868 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2869 | |
| 2870 | Label slow, exit; |
| 2871 | |
| 2872 | // Load the value (1) into register r1. |
| 2873 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 2874 | |
| 2875 | // Check for smi operand. |
| 2876 | __ tst(r0, Operand(kSmiTagMask)); |
| 2877 | __ b(ne, &slow); |
| 2878 | |
| 2879 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2880 | if (is_postfix) { |
| 2881 | __ str(r0, frame_->Element(target.size())); |
| 2882 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2883 | |
| 2884 | // Perform optimistic increment/decrement. |
| 2885 | if (is_increment) { |
| 2886 | __ add(r0, r0, Operand(r1), SetCC); |
| 2887 | } else { |
| 2888 | __ sub(r0, r0, Operand(r1), SetCC); |
| 2889 | } |
| 2890 | |
| 2891 | // If the increment/decrement didn't overflow, we're done. |
| 2892 | __ b(vc, &exit); |
| 2893 | |
| 2894 | // Revert optimistic increment/decrement. |
| 2895 | if (is_increment) { |
| 2896 | __ sub(r0, r0, Operand(r1)); |
| 2897 | } else { |
| 2898 | __ add(r0, r0, Operand(r1)); |
| 2899 | } |
| 2900 | |
| 2901 | // Slow case: Convert to number. |
| 2902 | __ bind(&slow); |
| 2903 | |
| 2904 | // Postfix: Convert the operand to a number and store it as the result. |
| 2905 | if (is_postfix) { |
| 2906 | InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2); |
| 2907 | __ CallStub(&stub); |
| 2908 | // Store to result (on the stack). |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2909 | __ str(r0, frame_->Element(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | // Compute the new value by calling the right JavaScript native. |
| 2913 | if (is_increment) { |
| 2914 | InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1); |
| 2915 | __ CallStub(&stub); |
| 2916 | } else { |
| 2917 | InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1); |
| 2918 | __ CallStub(&stub); |
| 2919 | } |
| 2920 | |
| 2921 | // Store the new value in the target if not const. |
| 2922 | __ bind(&exit); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2923 | frame_->Push(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2924 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | // Postfix: Discard the new value and use the old. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2928 | if (is_postfix) frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2932 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2933 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 2934 | Token::Value op = node->op(); |
| 2935 | |
| 2936 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 2937 | // operators must yield the result of one of the two expressions |
| 2938 | // before any ToBoolean() conversions. This means that the value |
| 2939 | // produced by a && or || operator is not necessarily a boolean. |
| 2940 | |
| 2941 | // NOTE: If the left hand side produces a materialized value (not in |
| 2942 | // the CC register), we force the right hand side to do the |
| 2943 | // same. This is necessary because we may have to branch to the exit |
| 2944 | // after evaluating the left hand side (due to the shortcut |
| 2945 | // semantics), but the compiler must (statically) know if the result |
| 2946 | // of compiling the binary operation is materialized or not. |
| 2947 | |
| 2948 | if (op == Token::AND) { |
| 2949 | Label is_true; |
| 2950 | LoadCondition(node->left(), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2951 | NOT_INSIDE_TYPEOF, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2952 | &is_true, |
| 2953 | false_target(), |
| 2954 | false); |
| 2955 | if (has_cc()) { |
| 2956 | Branch(false, false_target()); |
| 2957 | |
| 2958 | // Evaluate right side expression. |
| 2959 | __ bind(&is_true); |
| 2960 | LoadCondition(node->right(), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2961 | NOT_INSIDE_TYPEOF, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2962 | true_target(), |
| 2963 | false_target(), |
| 2964 | false); |
| 2965 | |
| 2966 | } else { |
| 2967 | Label pop_and_continue, exit; |
| 2968 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2969 | __ ldr(r0, frame_->Top()); // dup the stack top |
| 2970 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2971 | // Avoid popping the result if it converts to 'false' using the |
| 2972 | // standard ToBoolean() conversion as described in ECMA-262, |
| 2973 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2974 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2975 | Branch(false, &exit); |
| 2976 | |
| 2977 | // Pop the result of evaluating the first part. |
| 2978 | __ bind(&pop_and_continue); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2979 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2980 | |
| 2981 | // Evaluate right side expression. |
| 2982 | __ bind(&is_true); |
| 2983 | Load(node->right()); |
| 2984 | |
| 2985 | // Exit (always with a materialized value). |
| 2986 | __ bind(&exit); |
| 2987 | } |
| 2988 | |
| 2989 | } else if (op == Token::OR) { |
| 2990 | Label is_false; |
| 2991 | LoadCondition(node->left(), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2992 | NOT_INSIDE_TYPEOF, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2993 | true_target(), |
| 2994 | &is_false, |
| 2995 | false); |
| 2996 | if (has_cc()) { |
| 2997 | Branch(true, true_target()); |
| 2998 | |
| 2999 | // Evaluate right side expression. |
| 3000 | __ bind(&is_false); |
| 3001 | LoadCondition(node->right(), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3002 | NOT_INSIDE_TYPEOF, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3003 | true_target(), |
| 3004 | false_target(), |
| 3005 | false); |
| 3006 | |
| 3007 | } else { |
| 3008 | Label pop_and_continue, exit; |
| 3009 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3010 | __ ldr(r0, frame_->Top()); |
| 3011 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3012 | // Avoid popping the result if it converts to 'true' using the |
| 3013 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3014 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3015 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3016 | Branch(true, &exit); |
| 3017 | |
| 3018 | // Pop the result of evaluating the first part. |
| 3019 | __ bind(&pop_and_continue); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3020 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3021 | |
| 3022 | // Evaluate right side expression. |
| 3023 | __ bind(&is_false); |
| 3024 | Load(node->right()); |
| 3025 | |
| 3026 | // Exit (always with a materialized value). |
| 3027 | __ bind(&exit); |
| 3028 | } |
| 3029 | |
| 3030 | } else { |
| 3031 | // Optimize for the case where (at least) one of the expressions |
| 3032 | // is a literal small integer. |
| 3033 | Literal* lliteral = node->left()->AsLiteral(); |
| 3034 | Literal* rliteral = node->right()->AsLiteral(); |
| 3035 | |
| 3036 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
| 3037 | Load(node->left()); |
| 3038 | SmiOperation(node->op(), rliteral->handle(), false); |
| 3039 | |
| 3040 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
| 3041 | Load(node->right()); |
| 3042 | SmiOperation(node->op(), lliteral->handle(), true); |
| 3043 | |
| 3044 | } else { |
| 3045 | Load(node->left()); |
| 3046 | Load(node->right()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3047 | GenericBinaryOperation(node->op()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3048 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3049 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3054 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3055 | __ ldr(r0, frame_->Function()); |
| 3056 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3060 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3061 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3062 | |
| 3063 | // Get the expressions from the node. |
| 3064 | Expression* left = node->left(); |
| 3065 | Expression* right = node->right(); |
| 3066 | Token::Value op = node->op(); |
| 3067 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3068 | // To make null checks efficient, we check if either left or right is the |
| 3069 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3070 | // instead of calling the (very) general runtime routine for checking |
| 3071 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3072 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3073 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3074 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3075 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3076 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 3077 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3078 | if (left_is_null || right_is_null) { |
| 3079 | Load(left_is_null ? right : left); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3080 | frame_->Pop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3081 | __ cmp(r0, Operand(Factory::null_value())); |
| 3082 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3083 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 3084 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3085 | if (op != Token::EQ_STRICT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3086 | __ b(eq, true_target()); |
| 3087 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3088 | __ cmp(r0, Operand(Factory::undefined_value())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3089 | __ b(eq, true_target()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3090 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3091 | __ tst(r0, Operand(kSmiTagMask)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3092 | __ b(eq, false_target()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3093 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3094 | // It can be an undetectable object. |
| 3095 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3096 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 3097 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 3098 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3101 | cc_reg_ = eq; |
| 3102 | return; |
| 3103 | } |
| 3104 | } |
| 3105 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3106 | // To make typeof testing for natives implemented in JavaScript really |
| 3107 | // efficient, we generate special code for expressions of the form: |
| 3108 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3109 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 3110 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 3111 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 3112 | (right->AsLiteral() != NULL && |
| 3113 | right->AsLiteral()->handle()->IsString())) { |
| 3114 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 3115 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3116 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3117 | LoadTypeofExpression(operation->expression()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3118 | frame_->Pop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3119 | |
| 3120 | if (check->Equals(Heap::number_symbol())) { |
| 3121 | __ tst(r1, Operand(kSmiTagMask)); |
| 3122 | __ b(eq, true_target()); |
| 3123 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3124 | __ cmp(r1, Operand(Factory::heap_number_map())); |
| 3125 | cc_reg_ = eq; |
| 3126 | |
| 3127 | } else if (check->Equals(Heap::string_symbol())) { |
| 3128 | __ tst(r1, Operand(kSmiTagMask)); |
| 3129 | __ b(eq, false_target()); |
| 3130 | |
| 3131 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3132 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3133 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3134 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3135 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3136 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 3137 | __ b(eq, false_target()); |
| 3138 | |
| 3139 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3140 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 3141 | cc_reg_ = lt; |
| 3142 | |
| 3143 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 3144 | __ cmp(r1, Operand(Factory::true_value())); |
| 3145 | __ b(eq, true_target()); |
| 3146 | __ cmp(r1, Operand(Factory::false_value())); |
| 3147 | cc_reg_ = eq; |
| 3148 | |
| 3149 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 3150 | __ cmp(r1, Operand(Factory::undefined_value())); |
| 3151 | __ b(eq, true_target()); |
| 3152 | |
| 3153 | __ tst(r1, Operand(kSmiTagMask)); |
| 3154 | __ b(eq, false_target()); |
| 3155 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3156 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3157 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3158 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3159 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3160 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 3161 | |
| 3162 | cc_reg_ = eq; |
| 3163 | |
| 3164 | } else if (check->Equals(Heap::function_symbol())) { |
| 3165 | __ tst(r1, Operand(kSmiTagMask)); |
| 3166 | __ b(eq, false_target()); |
| 3167 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3168 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3169 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3170 | cc_reg_ = eq; |
| 3171 | |
| 3172 | } else if (check->Equals(Heap::object_symbol())) { |
| 3173 | __ tst(r1, Operand(kSmiTagMask)); |
| 3174 | __ b(eq, false_target()); |
| 3175 | |
| 3176 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3177 | __ cmp(r1, Operand(Factory::null_value())); |
| 3178 | __ b(eq, true_target()); |
| 3179 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3180 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3181 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 3182 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3183 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
| 3184 | __ b(eq, false_target()); |
| 3185 | |
| 3186 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3187 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3188 | __ b(lt, false_target()); |
| 3189 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 3190 | cc_reg_ = le; |
| 3191 | |
| 3192 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3193 | // Uncommon case: typeof testing against a string literal that is |
| 3194 | // never returned from the typeof operator. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3195 | __ b(false_target()); |
| 3196 | } |
| 3197 | return; |
| 3198 | } |
| 3199 | |
| 3200 | Load(left); |
| 3201 | Load(right); |
| 3202 | switch (op) { |
| 3203 | case Token::EQ: |
| 3204 | Comparison(eq, false); |
| 3205 | break; |
| 3206 | |
| 3207 | case Token::LT: |
| 3208 | Comparison(lt); |
| 3209 | break; |
| 3210 | |
| 3211 | case Token::GT: |
| 3212 | Comparison(gt); |
| 3213 | break; |
| 3214 | |
| 3215 | case Token::LTE: |
| 3216 | Comparison(le); |
| 3217 | break; |
| 3218 | |
| 3219 | case Token::GTE: |
| 3220 | Comparison(ge); |
| 3221 | break; |
| 3222 | |
| 3223 | case Token::EQ_STRICT: |
| 3224 | Comparison(eq, true); |
| 3225 | break; |
| 3226 | |
| 3227 | case Token::IN: |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3228 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3229 | __ InvokeBuiltin(Builtins::IN, CALL_JS); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3230 | frame_->Push(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3231 | break; |
| 3232 | |
| 3233 | case Token::INSTANCEOF: |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3234 | __ mov(r0, Operand(1)); // not counting receiver |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3235 | __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3236 | __ tst(r0, Operand(r0)); |
| 3237 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3238 | break; |
| 3239 | |
| 3240 | default: |
| 3241 | UNREACHABLE(); |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3246 | #undef __ |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3247 | #define __ masm-> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3248 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3249 | Handle<String> Reference::GetName() { |
| 3250 | ASSERT(type_ == NAMED); |
| 3251 | Property* property = expression_->AsProperty(); |
| 3252 | if (property == NULL) { |
| 3253 | // Global variable reference treated as a named property reference. |
| 3254 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 3255 | ASSERT(proxy->AsVariable() != NULL); |
| 3256 | ASSERT(proxy->AsVariable()->is_global()); |
| 3257 | return proxy->name(); |
| 3258 | } else { |
| 3259 | Literal* raw_name = property->key()->AsLiteral(); |
| 3260 | ASSERT(raw_name != NULL); |
| 3261 | return Handle<String>(String::cast(*raw_name->handle())); |
| 3262 | } |
| 3263 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3264 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3265 | |
| 3266 | void Reference::GetValue(TypeofState typeof_state) { |
| 3267 | ASSERT(!is_illegal()); |
| 3268 | ASSERT(!cgen_->has_cc()); |
| 3269 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3270 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3271 | Property* property = expression_->AsProperty(); |
| 3272 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3273 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | switch (type_) { |
| 3277 | case SLOT: { |
| 3278 | Comment cmnt(masm, "[ Load from Slot"); |
| 3279 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 3280 | ASSERT(slot != NULL); |
| 3281 | cgen_->LoadFromSlot(slot, typeof_state); |
| 3282 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3285 | case NAMED: { |
| 3286 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 3287 | // distinction between expressions in a typeof and not in a typeof. If |
| 3288 | // there is a chance that reference errors can be thrown below, we |
| 3289 | // must distinguish between the two kinds of loads (typeof expression |
| 3290 | // loads must not throw a reference error). |
| 3291 | Comment cmnt(masm, "[ Load from named Property"); |
| 3292 | // Setup the name register. |
| 3293 | Handle<String> name(GetName()); |
| 3294 | __ mov(r2, Operand(name)); |
| 3295 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 3296 | |
| 3297 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 3298 | if (var != NULL) { |
| 3299 | ASSERT(var->is_global()); |
| 3300 | __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT); |
| 3301 | } else { |
| 3302 | __ Call(ic, RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3303 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3304 | frame->Push(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3305 | break; |
| 3306 | } |
| 3307 | |
| 3308 | case KEYED: { |
| 3309 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 3310 | // 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] | 3311 | |
| 3312 | // TODO(181): Implement inlined version of array indexing once |
| 3313 | // loop nesting is properly tracked on ARM. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3314 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 3315 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3316 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 3317 | |
| 3318 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
| 3319 | if (var != NULL) { |
| 3320 | ASSERT(var->is_global()); |
| 3321 | __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT); |
| 3322 | } else { |
| 3323 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 3324 | } |
| 3325 | frame->Push(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3326 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | default: |
| 3330 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3331 | } |
| 3332 | } |
| 3333 | |
| 3334 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3335 | void Reference::SetValue(InitState init_state) { |
| 3336 | ASSERT(!is_illegal()); |
| 3337 | ASSERT(!cgen_->has_cc()); |
| 3338 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3339 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3340 | Property* property = expression_->AsProperty(); |
| 3341 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3342 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3343 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3344 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3345 | switch (type_) { |
| 3346 | case SLOT: { |
| 3347 | Comment cmnt(masm, "[ Store to Slot"); |
| 3348 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 3349 | ASSERT(slot != NULL); |
| 3350 | if (slot->type() == Slot::LOOKUP) { |
| 3351 | ASSERT(slot->var()->mode() == Variable::DYNAMIC); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3352 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3353 | // For now, just do a runtime call. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3354 | frame->Push(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3355 | __ mov(r0, Operand(slot->var()->name())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3356 | frame->Push(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3357 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3358 | if (init_state == CONST_INIT) { |
| 3359 | // Same as the case for a normal store, but ignores attribute |
| 3360 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 3361 | // const properties (introduced via eval("const foo = (some |
| 3362 | // expr);")). Also, uses the current function context instead of |
| 3363 | // the top context. |
| 3364 | // |
| 3365 | // Note that we must declare the foo upon entry of eval(), via a |
| 3366 | // context slot declaration, but we cannot initialize it at the |
| 3367 | // same time, because the const declaration may be at the end of |
| 3368 | // the eval code (sigh...) and the const variable may have been |
| 3369 | // used before (where its value is 'undefined'). Thus, we can only |
| 3370 | // do the initialization when we actually encounter the expression |
| 3371 | // and when the expression operands are defined and valid, and |
| 3372 | // thus we need the split into 2 operations: declaration of the |
| 3373 | // context slot followed by initialization. |
| 3374 | __ CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 3375 | } else { |
| 3376 | __ CallRuntime(Runtime::kStoreContextSlot, 3); |
| 3377 | } |
| 3378 | // Storing a variable must keep the (new) value on the expression |
| 3379 | // stack. This is necessary for compiling assignment expressions. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3380 | frame->Push(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3381 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3382 | } else { |
| 3383 | ASSERT(slot->var()->mode() != Variable::DYNAMIC); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3384 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3385 | Label exit; |
| 3386 | if (init_state == CONST_INIT) { |
| 3387 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 3388 | // Only the first const initialization must be executed (the slot |
| 3389 | // still contains 'the hole' value). When the assignment is |
| 3390 | // executed, the code is identical to a normal store (see below). |
| 3391 | Comment cmnt(masm, "[ Init const"); |
| 3392 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
| 3393 | __ cmp(r2, Operand(Factory::the_hole_value())); |
| 3394 | __ b(ne, &exit); |
| 3395 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3396 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3397 | // We must execute the store. Storing a variable must keep the |
| 3398 | // (new) value on the stack. This is necessary for compiling |
| 3399 | // assignment expressions. |
| 3400 | // |
| 3401 | // Note: We will reach here even with slot->var()->mode() == |
| 3402 | // Variable::CONST because of const declarations which will |
| 3403 | // initialize consts to 'the hole' value and by doing so, end up |
| 3404 | // calling this code. r2 may be loaded with context; used below in |
| 3405 | // RecordWrite. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3406 | frame->Pop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3407 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3408 | frame->Push(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3409 | if (slot->type() == Slot::CONTEXT) { |
| 3410 | // Skip write barrier if the written value is a smi. |
| 3411 | __ tst(r0, Operand(kSmiTagMask)); |
| 3412 | __ b(eq, &exit); |
| 3413 | // r2 is loaded with context when calling SlotOperand above. |
| 3414 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 3415 | __ mov(r3, Operand(offset)); |
| 3416 | __ RecordWrite(r2, r3, r1); |
| 3417 | } |
| 3418 | // If we definitely did not jump over the assignment, we do not need |
| 3419 | // to bind the exit label. Doing so can defeat peephole |
| 3420 | // optimization. |
| 3421 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
| 3422 | __ bind(&exit); |
| 3423 | } |
| 3424 | } |
| 3425 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3428 | case NAMED: { |
| 3429 | Comment cmnt(masm, "[ Store to named Property"); |
| 3430 | // Call the appropriate IC code. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3431 | frame->Pop(r0); // value |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3432 | // Setup the name register. |
| 3433 | Handle<String> name(GetName()); |
| 3434 | __ mov(r2, Operand(name)); |
| 3435 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 3436 | __ Call(ic, RelocInfo::CODE_TARGET); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3437 | frame->Push(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3438 | break; |
| 3439 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3440 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3441 | case KEYED: { |
| 3442 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 3443 | Property* property = expression_->AsProperty(); |
| 3444 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3445 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3446 | |
| 3447 | // Call IC code. |
| 3448 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 3449 | // TODO(1222589): Make the IC grab the values from the stack. |
| 3450 | frame->Pop(r0); // value |
| 3451 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 3452 | frame->Push(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3453 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3454 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3455 | |
| 3456 | default: |
| 3457 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3458 | } |
| 3459 | } |
| 3460 | |
| 3461 | |
| 3462 | void GetPropertyStub::Generate(MacroAssembler* masm) { |
| 3463 | // sp[0]: key |
| 3464 | // sp[1]: receiver |
| 3465 | Label slow, fast; |
| 3466 | // Get the key and receiver object from the stack. |
| 3467 | __ ldm(ia, sp, r0.bit() | r1.bit()); |
| 3468 | // Check that the key is a smi. |
| 3469 | __ tst(r0, Operand(kSmiTagMask)); |
| 3470 | __ b(ne, &slow); |
| 3471 | __ mov(r0, Operand(r0, ASR, kSmiTagSize)); |
| 3472 | // Check that the object isn't a smi. |
| 3473 | __ tst(r1, Operand(kSmiTagMask)); |
| 3474 | __ b(eq, &slow); |
| 3475 | |
| 3476 | // Check that the object is some kind of JS object EXCEPT JS Value type. |
| 3477 | // In the case that the object is a value-wrapper object, |
| 3478 | // we enter the runtime system to make sure that indexing into string |
| 3479 | // objects work as intended. |
| 3480 | ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE); |
| 3481 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3482 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3483 | __ cmp(r2, Operand(JS_OBJECT_TYPE)); |
| 3484 | __ b(lt, &slow); |
| 3485 | |
| 3486 | // Get the elements array of the object. |
| 3487 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 3488 | // Check that the object is in fast mode (not dictionary). |
| 3489 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3490 | __ cmp(r3, Operand(Factory::hash_table_map())); |
| 3491 | __ b(eq, &slow); |
| 3492 | // Check that the key (index) is within bounds. |
| 3493 | __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset)); |
| 3494 | __ cmp(r0, Operand(r3)); |
| 3495 | __ b(lo, &fast); |
| 3496 | |
| 3497 | // Slow case: Push extra copies of the arguments (2). |
| 3498 | __ bind(&slow); |
| 3499 | __ ldm(ia, sp, r0.bit() | r1.bit()); |
| 3500 | __ stm(db_w, sp, r0.bit() | r1.bit()); |
| 3501 | // Do tail-call to runtime routine. |
| 3502 | __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2); |
| 3503 | |
| 3504 | // Fast case: Do the load. |
| 3505 | __ bind(&fast); |
| 3506 | __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag)); |
| 3507 | __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2)); |
| 3508 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 3509 | // In case the loaded value is the_hole we have to consult GetProperty |
| 3510 | // to ensure the prototype chain is searched. |
| 3511 | __ b(eq, &slow); |
| 3512 | |
| 3513 | __ StubReturn(1); |
| 3514 | } |
| 3515 | |
| 3516 | |
| 3517 | void SetPropertyStub::Generate(MacroAssembler* masm) { |
| 3518 | // r0 : value |
| 3519 | // sp[0] : key |
| 3520 | // sp[1] : receiver |
| 3521 | |
| 3522 | Label slow, fast, array, extra, exit; |
| 3523 | // Get the key and the object from the stack. |
| 3524 | __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver |
| 3525 | // Check that the key is a smi. |
| 3526 | __ tst(r1, Operand(kSmiTagMask)); |
| 3527 | __ b(ne, &slow); |
| 3528 | // Check that the object isn't a smi. |
| 3529 | __ tst(r3, Operand(kSmiTagMask)); |
| 3530 | __ b(eq, &slow); |
| 3531 | // Get the type of the object from its map. |
| 3532 | __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 3533 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3534 | // Check if the object is a JS array or not. |
| 3535 | __ cmp(r2, Operand(JS_ARRAY_TYPE)); |
| 3536 | __ b(eq, &array); |
| 3537 | // Check that the object is some kind of JS object. |
| 3538 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
| 3539 | __ b(lt, &slow); |
| 3540 | |
| 3541 | |
| 3542 | // Object case: Check key against length in the elements array. |
| 3543 | __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset)); |
| 3544 | // Check that the object is in fast mode (not dictionary). |
| 3545 | __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 3546 | __ cmp(r2, Operand(Factory::hash_table_map())); |
| 3547 | __ b(eq, &slow); |
| 3548 | // Untag the key (for checking against untagged length in the fixed array). |
| 3549 | __ mov(r1, Operand(r1, ASR, kSmiTagSize)); |
| 3550 | // Compute address to store into and check array bounds. |
| 3551 | __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag)); |
| 3552 | __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2)); |
| 3553 | __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset)); |
| 3554 | __ cmp(r1, Operand(ip)); |
| 3555 | __ b(lo, &fast); |
| 3556 | |
| 3557 | |
| 3558 | // Slow case: Push extra copies of the arguments (3). |
| 3559 | __ bind(&slow); |
| 3560 | __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object |
| 3561 | __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit()); |
| 3562 | // Do tail-call to runtime routine. |
| 3563 | __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3); |
| 3564 | |
| 3565 | |
| 3566 | // Extra capacity case: Check if there is extra capacity to |
| 3567 | // perform the store and update the length. Used for adding one |
| 3568 | // element to the array by writing to array[array.length]. |
| 3569 | // r0 == value, r1 == key, r2 == elements, r3 == object |
| 3570 | __ bind(&extra); |
| 3571 | __ b(ne, &slow); // do not leave holes in the array |
| 3572 | __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag |
| 3573 | __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset)); |
| 3574 | __ cmp(r1, Operand(ip)); |
| 3575 | __ b(hs, &slow); |
| 3576 | __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag |
| 3577 | __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment |
| 3578 | __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset)); |
| 3579 | __ mov(r3, Operand(r2)); |
| 3580 | // NOTE: Computing the address to store into must take the fact |
| 3581 | // that the key has been incremented into account. |
| 3582 | int displacement = Array::kHeaderSize - kHeapObjectTag - |
| 3583 | ((1 << kSmiTagSize) * 2); |
| 3584 | __ add(r2, r2, Operand(displacement)); |
| 3585 | __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 3586 | __ b(&fast); |
| 3587 | |
| 3588 | |
| 3589 | // Array case: Get the length and the elements array from the JS |
| 3590 | // array. Check that the array is in fast mode; if it is the |
| 3591 | // length is always a smi. |
| 3592 | // r0 == value, r3 == object |
| 3593 | __ bind(&array); |
| 3594 | __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset)); |
| 3595 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 3596 | __ cmp(r1, Operand(Factory::hash_table_map())); |
| 3597 | __ b(eq, &slow); |
| 3598 | |
| 3599 | // Check the key against the length in the array, compute the |
| 3600 | // address to store into and fall through to fast case. |
| 3601 | __ ldr(r1, MemOperand(sp)); |
| 3602 | // r0 == value, r1 == key, r2 == elements, r3 == object. |
| 3603 | __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset)); |
| 3604 | __ cmp(r1, Operand(ip)); |
| 3605 | __ b(hs, &extra); |
| 3606 | __ mov(r3, Operand(r2)); |
| 3607 | __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag)); |
| 3608 | __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 3609 | |
| 3610 | |
| 3611 | // Fast case: Do the store. |
| 3612 | // r0 == value, r2 == address to store into, r3 == elements |
| 3613 | __ bind(&fast); |
| 3614 | __ str(r0, MemOperand(r2)); |
| 3615 | // Skip write barrier if the written value is a smi. |
| 3616 | __ tst(r0, Operand(kSmiTagMask)); |
| 3617 | __ b(eq, &exit); |
| 3618 | // Update write barrier for the elements array address. |
| 3619 | __ sub(r1, r2, Operand(r3)); |
| 3620 | __ RecordWrite(r3, r1, r2); |
| 3621 | __ bind(&exit); |
| 3622 | __ StubReturn(1); |
| 3623 | } |
| 3624 | |
| 3625 | |
| 3626 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 3627 | // r1 : x |
| 3628 | // r0 : y |
| 3629 | // result : r0 |
| 3630 | |
| 3631 | switch (op_) { |
| 3632 | case Token::ADD: { |
| 3633 | Label slow, exit; |
| 3634 | // fast path |
| 3635 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 3636 | __ add(r0, r1, Operand(r0), SetCC); // add y optimistically |
| 3637 | // go slow-path in case of overflow |
| 3638 | __ b(vs, &slow); |
| 3639 | // go slow-path in case of non-smi operands |
| 3640 | ASSERT(kSmiTag == 0); // adjust code below |
| 3641 | __ tst(r2, Operand(kSmiTagMask)); |
| 3642 | __ b(eq, &exit); |
| 3643 | // slow path |
| 3644 | __ bind(&slow); |
| 3645 | __ sub(r0, r0, Operand(r1)); // revert optimistic add |
| 3646 | __ push(r1); |
| 3647 | __ push(r0); |
| 3648 | __ mov(r0, Operand(1)); // set number of arguments |
| 3649 | __ InvokeBuiltin(Builtins::ADD, JUMP_JS); |
| 3650 | // done |
| 3651 | __ bind(&exit); |
| 3652 | break; |
| 3653 | } |
| 3654 | |
| 3655 | case Token::SUB: { |
| 3656 | Label slow, exit; |
| 3657 | // fast path |
| 3658 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 3659 | __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically |
| 3660 | // go slow-path in case of overflow |
| 3661 | __ b(vs, &slow); |
| 3662 | // go slow-path in case of non-smi operands |
| 3663 | ASSERT(kSmiTag == 0); // adjust code below |
| 3664 | __ tst(r2, Operand(kSmiTagMask)); |
| 3665 | __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result |
| 3666 | __ b(eq, &exit); |
| 3667 | // slow path |
| 3668 | __ bind(&slow); |
| 3669 | __ push(r1); |
| 3670 | __ push(r0); |
| 3671 | __ mov(r0, Operand(1)); // set number of arguments |
| 3672 | __ InvokeBuiltin(Builtins::SUB, JUMP_JS); |
| 3673 | // done |
| 3674 | __ bind(&exit); |
| 3675 | break; |
| 3676 | } |
| 3677 | |
| 3678 | case Token::MUL: { |
| 3679 | Label slow, exit; |
| 3680 | // tag check |
| 3681 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 3682 | ASSERT(kSmiTag == 0); // adjust code below |
| 3683 | __ tst(r2, Operand(kSmiTagMask)); |
| 3684 | __ b(ne, &slow); |
| 3685 | // remove tag from one operand (but keep sign), so that result is smi |
| 3686 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
| 3687 | // do multiplication |
| 3688 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1 |
| 3689 | // go slow on overflows (overflow bit is not set) |
| 3690 | __ mov(ip, Operand(r3, ASR, 31)); |
| 3691 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 3692 | __ b(ne, &slow); |
| 3693 | // go slow on zero result to handle -0 |
| 3694 | __ tst(r3, Operand(r3)); |
| 3695 | __ mov(r0, Operand(r3), LeaveCC, ne); |
| 3696 | __ b(ne, &exit); |
| 3697 | // slow case |
| 3698 | __ bind(&slow); |
| 3699 | __ push(r1); |
| 3700 | __ push(r0); |
| 3701 | __ mov(r0, Operand(1)); // set number of arguments |
| 3702 | __ InvokeBuiltin(Builtins::MUL, JUMP_JS); |
| 3703 | // done |
| 3704 | __ bind(&exit); |
| 3705 | break; |
| 3706 | } |
| 3707 | |
| 3708 | case Token::BIT_OR: |
| 3709 | case Token::BIT_AND: |
| 3710 | case Token::BIT_XOR: { |
| 3711 | Label slow, exit; |
| 3712 | // tag check |
| 3713 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 3714 | ASSERT(kSmiTag == 0); // adjust code below |
| 3715 | __ tst(r2, Operand(kSmiTagMask)); |
| 3716 | __ b(ne, &slow); |
| 3717 | switch (op_) { |
| 3718 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 3719 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 3720 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
| 3721 | default: UNREACHABLE(); |
| 3722 | } |
| 3723 | __ b(&exit); |
| 3724 | __ bind(&slow); |
| 3725 | __ push(r1); // restore stack |
| 3726 | __ push(r0); |
| 3727 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 3728 | switch (op_) { |
| 3729 | case Token::BIT_OR: |
| 3730 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 3731 | break; |
| 3732 | case Token::BIT_AND: |
| 3733 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 3734 | break; |
| 3735 | case Token::BIT_XOR: |
| 3736 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 3737 | break; |
| 3738 | default: |
| 3739 | UNREACHABLE(); |
| 3740 | } |
| 3741 | __ bind(&exit); |
| 3742 | break; |
| 3743 | } |
| 3744 | |
| 3745 | case Token::SHL: |
| 3746 | case Token::SHR: |
| 3747 | case Token::SAR: { |
| 3748 | Label slow, exit; |
| 3749 | // tag check |
| 3750 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 3751 | ASSERT(kSmiTag == 0); // adjust code below |
| 3752 | __ tst(r2, Operand(kSmiTagMask)); |
| 3753 | __ b(ne, &slow); |
| 3754 | // remove tags from operands (but keep sign) |
| 3755 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 3756 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 3757 | // use only the 5 least significant bits of the shift count |
| 3758 | __ and_(r2, r2, Operand(0x1f)); |
| 3759 | // perform operation |
| 3760 | switch (op_) { |
| 3761 | case Token::SAR: |
| 3762 | __ mov(r3, Operand(r3, ASR, r2)); |
| 3763 | // no checks of result necessary |
| 3764 | break; |
| 3765 | |
| 3766 | case Token::SHR: |
| 3767 | __ mov(r3, Operand(r3, LSR, r2)); |
| 3768 | // check that the *unsigned* result fits in a smi |
| 3769 | // neither of the two high-order bits can be set: |
| 3770 | // - 0x80000000: high bit would be lost when smi tagging |
| 3771 | // - 0x40000000: this number would convert to negative when |
| 3772 | // smi tagging these two cases can only happen with shifts |
| 3773 | // by 0 or 1 when handed a valid smi |
| 3774 | __ and_(r2, r3, Operand(0xc0000000), SetCC); |
| 3775 | __ b(ne, &slow); |
| 3776 | break; |
| 3777 | |
| 3778 | case Token::SHL: |
| 3779 | __ mov(r3, Operand(r3, LSL, r2)); |
| 3780 | // check that the *signed* result fits in a smi |
| 3781 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 3782 | __ b(mi, &slow); |
| 3783 | break; |
| 3784 | |
| 3785 | default: UNREACHABLE(); |
| 3786 | } |
| 3787 | // tag result and store it in r0 |
| 3788 | ASSERT(kSmiTag == 0); // adjust code below |
| 3789 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
| 3790 | __ b(&exit); |
| 3791 | // slow case |
| 3792 | __ bind(&slow); |
| 3793 | __ push(r1); // restore stack |
| 3794 | __ push(r0); |
| 3795 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 3796 | switch (op_) { |
| 3797 | case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break; |
| 3798 | case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break; |
| 3799 | case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break; |
| 3800 | default: UNREACHABLE(); |
| 3801 | } |
| 3802 | __ bind(&exit); |
| 3803 | break; |
| 3804 | } |
| 3805 | |
| 3806 | default: UNREACHABLE(); |
| 3807 | } |
| 3808 | __ Ret(); |
| 3809 | } |
| 3810 | |
| 3811 | |
| 3812 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 3813 | Label within_limit; |
| 3814 | __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit())); |
| 3815 | __ ldr(ip, MemOperand(ip)); |
| 3816 | __ cmp(sp, Operand(ip)); |
| 3817 | __ b(hs, &within_limit); |
| 3818 | // Do tail-call to runtime routine. |
| 3819 | __ push(r0); |
| 3820 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1); |
| 3821 | __ bind(&within_limit); |
| 3822 | |
| 3823 | __ StubReturn(1); |
| 3824 | } |
| 3825 | |
| 3826 | |
| 3827 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 3828 | Label undo; |
| 3829 | Label slow; |
| 3830 | Label done; |
| 3831 | |
| 3832 | // Enter runtime system if the value is not a smi. |
| 3833 | __ tst(r0, Operand(kSmiTagMask)); |
| 3834 | __ b(ne, &slow); |
| 3835 | |
| 3836 | // Enter runtime system if the value of the expression is zero |
| 3837 | // to make sure that we switch between 0 and -0. |
| 3838 | __ cmp(r0, Operand(0)); |
| 3839 | __ b(eq, &slow); |
| 3840 | |
| 3841 | // The value of the expression is a smi that is not zero. Try |
| 3842 | // optimistic subtraction '0 - value'. |
| 3843 | __ rsb(r1, r0, Operand(0), SetCC); |
| 3844 | __ b(vs, &slow); |
| 3845 | |
| 3846 | // If result is a smi we are done. |
| 3847 | __ tst(r1, Operand(kSmiTagMask)); |
| 3848 | __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result |
| 3849 | __ b(eq, &done); |
| 3850 | |
| 3851 | // Enter runtime system. |
| 3852 | __ bind(&slow); |
| 3853 | __ push(r0); |
| 3854 | __ mov(r0, Operand(0)); // set number of arguments |
| 3855 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 3856 | |
| 3857 | __ bind(&done); |
| 3858 | __ StubReturn(1); |
| 3859 | } |
| 3860 | |
| 3861 | |
| 3862 | void InvokeBuiltinStub::Generate(MacroAssembler* masm) { |
| 3863 | __ push(r0); |
| 3864 | __ mov(r0, Operand(0)); // set number of arguments |
| 3865 | switch (kind_) { |
| 3866 | case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break; |
| 3867 | case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break; |
| 3868 | case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break; |
| 3869 | default: UNREACHABLE(); |
| 3870 | } |
| 3871 | __ StubReturn(argc_); |
| 3872 | } |
| 3873 | |
| 3874 | |
| 3875 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
| 3876 | // r0 holds exception |
| 3877 | ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code |
| 3878 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 3879 | __ ldr(sp, MemOperand(r3)); |
| 3880 | __ pop(r2); // pop next in chain |
| 3881 | __ str(r2, MemOperand(r3)); |
| 3882 | // restore parameter- and frame-pointer and pop state. |
| 3883 | __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit()); |
| 3884 | // Before returning we restore the context from the frame pointer if not NULL. |
| 3885 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 3886 | __ cmp(fp, Operand(0)); |
| 3887 | // Set cp to NULL if fp is NULL. |
| 3888 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 3889 | // Restore cp otherwise. |
| 3890 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 3891 | if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc)); |
| 3892 | __ pop(pc); |
| 3893 | } |
| 3894 | |
| 3895 | |
| 3896 | void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) { |
| 3897 | // Fetch top stack handler. |
| 3898 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 3899 | __ ldr(r3, MemOperand(r3)); |
| 3900 | |
| 3901 | // Unwind the handlers until the ENTRY handler is found. |
| 3902 | Label loop, done; |
| 3903 | __ bind(&loop); |
| 3904 | // Load the type of the current stack handler. |
| 3905 | const int kStateOffset = StackHandlerConstants::kAddressDisplacement + |
| 3906 | StackHandlerConstants::kStateOffset; |
| 3907 | __ ldr(r2, MemOperand(r3, kStateOffset)); |
| 3908 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 3909 | __ b(eq, &done); |
| 3910 | // Fetch the next handler in the list. |
| 3911 | const int kNextOffset = StackHandlerConstants::kAddressDisplacement + |
| 3912 | StackHandlerConstants::kNextOffset; |
| 3913 | __ ldr(r3, MemOperand(r3, kNextOffset)); |
| 3914 | __ jmp(&loop); |
| 3915 | __ bind(&done); |
| 3916 | |
| 3917 | // Set the top handler address to next handler past the current ENTRY handler. |
| 3918 | __ ldr(r0, MemOperand(r3, kNextOffset)); |
| 3919 | __ mov(r2, Operand(ExternalReference(Top::k_handler_address))); |
| 3920 | __ str(r0, MemOperand(r2)); |
| 3921 | |
| 3922 | // Set external caught exception to false. |
| 3923 | __ mov(r0, Operand(false)); |
| 3924 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 3925 | __ mov(r2, Operand(external_caught)); |
| 3926 | __ str(r0, MemOperand(r2)); |
| 3927 | |
| 3928 | // Set pending exception and r0 to out of memory exception. |
| 3929 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 3930 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 3931 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 3932 | __ str(r0, MemOperand(r2)); |
| 3933 | |
| 3934 | // Restore the stack to the address of the ENTRY handler |
| 3935 | __ mov(sp, Operand(r3)); |
| 3936 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3937 | // Stack layout at this point. See also PushTryHandler |
| 3938 | // r3, sp -> next handler |
| 3939 | // state (ENTRY) |
| 3940 | // pp |
| 3941 | // fp |
| 3942 | // lr |
| 3943 | |
| 3944 | // Discard ENTRY state (r2 is not used), and restore parameter- |
| 3945 | // and frame-pointer and pop state. |
| 3946 | __ 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] | 3947 | // Before returning we restore the context from the frame pointer if not NULL. |
| 3948 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 3949 | __ cmp(fp, Operand(0)); |
| 3950 | // Set cp to NULL if fp is NULL. |
| 3951 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 3952 | // Restore cp otherwise. |
| 3953 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 3954 | if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc)); |
| 3955 | __ pop(pc); |
| 3956 | } |
| 3957 | |
| 3958 | |
| 3959 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 3960 | Label* throw_normal_exception, |
| 3961 | Label* throw_out_of_memory_exception, |
| 3962 | StackFrame::Type frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3963 | bool do_gc, |
| 3964 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3965 | // r0: result parameter for PerformGC, if any |
| 3966 | // r4: number of arguments including receiver (C callee-saved) |
| 3967 | // r5: pointer to builtin function (C callee-saved) |
| 3968 | // r6: pointer to the first argument (C callee-saved) |
| 3969 | |
| 3970 | if (do_gc) { |
| 3971 | // Passing r0. |
| 3972 | __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY); |
| 3973 | } |
| 3974 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3975 | ExternalReference scope_depth = |
| 3976 | ExternalReference::heap_always_allocate_scope_depth(); |
| 3977 | if (always_allocate) { |
| 3978 | __ mov(r0, Operand(scope_depth)); |
| 3979 | __ ldr(r1, MemOperand(r0)); |
| 3980 | __ add(r1, r1, Operand(1)); |
| 3981 | __ str(r1, MemOperand(r0)); |
| 3982 | } |
| 3983 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3984 | // Call C built-in. |
| 3985 | // r0 = argc, r1 = argv |
| 3986 | __ mov(r0, Operand(r4)); |
| 3987 | __ mov(r1, Operand(r6)); |
| 3988 | |
| 3989 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 3990 | // frames, we need to know where the return address is. Right now, |
| 3991 | // we push it on the stack to be able to find it again, but we never |
| 3992 | // restore from it in case of changes, which makes it impossible to |
| 3993 | // support moving the C entry code stub. This should be fixed, but currently |
| 3994 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 3995 | // sequence that it is not moving ever. |
| 3996 | __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 3997 | __ push(lr); |
| 3998 | #if !defined(__arm__) |
| 3999 | // Notify the simulator of the transition to C code. |
| 4000 | __ swi(assembler::arm::call_rt_r5); |
| 4001 | #else /* !defined(__arm__) */ |
| 4002 | __ mov(pc, Operand(r5)); |
| 4003 | #endif /* !defined(__arm__) */ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4004 | |
| 4005 | if (always_allocate) { |
| 4006 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 4007 | // though (contain the result). |
| 4008 | __ mov(r2, Operand(scope_depth)); |
| 4009 | __ ldr(r3, MemOperand(r2)); |
| 4010 | __ sub(r3, r3, Operand(1)); |
| 4011 | __ str(r3, MemOperand(r2)); |
| 4012 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4013 | |
| 4014 | // check for failure result |
| 4015 | Label failure_returned; |
| 4016 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 4017 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 4018 | __ add(r2, r0, Operand(1)); |
| 4019 | __ tst(r2, Operand(kFailureTagMask)); |
| 4020 | __ b(eq, &failure_returned); |
| 4021 | |
| 4022 | // Exit C frame and return. |
| 4023 | // r0:r1: result |
| 4024 | // sp: stack pointer |
| 4025 | // fp: frame pointer |
| 4026 | // pp: caller's parameter pointer pp (restored as C callee-saved) |
| 4027 | __ LeaveExitFrame(frame_type); |
| 4028 | |
| 4029 | // check if we should retry or throw exception |
| 4030 | Label retry; |
| 4031 | __ bind(&failure_returned); |
| 4032 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 4033 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 4034 | __ b(eq, &retry); |
| 4035 | |
| 4036 | Label continue_exception; |
| 4037 | // If the returned failure is EXCEPTION then promote Top::pending_exception(). |
| 4038 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 4039 | __ b(ne, &continue_exception); |
| 4040 | |
| 4041 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame^] | 4042 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4043 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame^] | 4044 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4045 | __ ldr(r0, MemOperand(ip)); |
| 4046 | __ str(r3, MemOperand(ip)); |
| 4047 | |
| 4048 | __ bind(&continue_exception); |
| 4049 | // Special handling of out of memory exception. |
| 4050 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 4051 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 4052 | __ b(eq, throw_out_of_memory_exception); |
| 4053 | |
| 4054 | // Handle normal exception. |
| 4055 | __ jmp(throw_normal_exception); |
| 4056 | |
| 4057 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 4058 | } |
| 4059 | |
| 4060 | |
| 4061 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 4062 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 4063 | // r0: number of arguments including receiver |
| 4064 | // r1: pointer to builtin function |
| 4065 | // fp: frame pointer (restored after C call) |
| 4066 | // sp: stack pointer (restored as callee's pp after C call) |
| 4067 | // cp: current context (C callee-saved) |
| 4068 | // pp: caller's parameter pointer pp (C callee-saved) |
| 4069 | |
| 4070 | // NOTE: Invocations of builtins may return failure objects |
| 4071 | // instead of a proper result. The builtin entry handles |
| 4072 | // this by performing a garbage collection and retrying the |
| 4073 | // builtin once. |
| 4074 | |
| 4075 | StackFrame::Type frame_type = is_debug_break |
| 4076 | ? StackFrame::EXIT_DEBUG |
| 4077 | : StackFrame::EXIT; |
| 4078 | |
| 4079 | // Enter the exit frame that transitions from JavaScript to C++. |
| 4080 | __ EnterExitFrame(frame_type); |
| 4081 | |
| 4082 | // r4: number of arguments (C callee-saved) |
| 4083 | // r5: pointer to builtin function (C callee-saved) |
| 4084 | // r6: pointer to first argument (C callee-saved) |
| 4085 | |
| 4086 | Label throw_out_of_memory_exception; |
| 4087 | Label throw_normal_exception; |
| 4088 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4089 | // Call into the runtime system. Collect garbage before the call if |
| 4090 | // running with --gc-greedy set. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4091 | if (FLAG_gc_greedy) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4092 | Failure* failure = Failure::RetryAfterGC(0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4093 | __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure))); |
| 4094 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4095 | GenerateCore(masm, &throw_normal_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4096 | &throw_out_of_memory_exception, |
| 4097 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4098 | FLAG_gc_greedy, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4099 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4100 | |
| 4101 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4102 | GenerateCore(masm, |
| 4103 | &throw_normal_exception, |
| 4104 | &throw_out_of_memory_exception, |
| 4105 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4106 | true, |
| 4107 | false); |
| 4108 | |
| 4109 | // Do full GC and retry runtime call one final time. |
| 4110 | Failure* failure = Failure::InternalError(); |
| 4111 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 4112 | GenerateCore(masm, |
| 4113 | &throw_normal_exception, |
| 4114 | &throw_out_of_memory_exception, |
| 4115 | frame_type, |
| 4116 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4117 | true); |
| 4118 | |
| 4119 | __ bind(&throw_out_of_memory_exception); |
| 4120 | GenerateThrowOutOfMemory(masm); |
| 4121 | // control flow for generated will not return. |
| 4122 | |
| 4123 | __ bind(&throw_normal_exception); |
| 4124 | GenerateThrowTOS(masm); |
| 4125 | } |
| 4126 | |
| 4127 | |
| 4128 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 4129 | // r0: code entry |
| 4130 | // r1: function |
| 4131 | // r2: receiver |
| 4132 | // r3: argc |
| 4133 | // [sp+0]: argv |
| 4134 | |
| 4135 | Label invoke, exit; |
| 4136 | |
| 4137 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 4138 | // No need to save register-passed args |
| 4139 | // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr |
| 4140 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 4141 | |
| 4142 | // Get address of argv, see stm above. |
| 4143 | // r0: code entry |
| 4144 | // r1: function |
| 4145 | // r2: receiver |
| 4146 | // r3: argc |
| 4147 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 4148 | __ ldr(r4, MemOperand(r4)); // argv |
| 4149 | |
| 4150 | // Push a frame with special values setup to mark it as an entry frame. |
| 4151 | // r0: code entry |
| 4152 | // r1: function |
| 4153 | // r2: receiver |
| 4154 | // r3: argc |
| 4155 | // r4: argv |
| 4156 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 4157 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 4158 | __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL)); |
| 4159 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 4160 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 4161 | __ ldr(r5, MemOperand(r5)); |
| 4162 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 4163 | |
| 4164 | // Setup frame pointer for the frame to be pushed. |
| 4165 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 4166 | |
| 4167 | // Call a faked try-block that does the invoke. |
| 4168 | __ bl(&invoke); |
| 4169 | |
| 4170 | // Caught exception: Store result (exception) in the pending |
| 4171 | // exception field in the JSEnv and return a failure sentinel. |
| 4172 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 4173 | // 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^] | 4174 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4175 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4176 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4177 | __ b(&exit); |
| 4178 | |
| 4179 | // Invoke: Link this frame into the handler chain. |
| 4180 | __ bind(&invoke); |
| 4181 | // Must preserve r0-r4, r5-r7 are available. |
| 4182 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
| 4183 | // If an exception not caught by another handler occurs, this handler returns |
| 4184 | // control to the code after the bl(&invoke) above, which restores all |
| 4185 | // kCalleeSaved registers (including cp, pp and fp) to their saved values |
| 4186 | // before returning a failure to C. |
| 4187 | |
| 4188 | // Clear any pending exceptions. |
| 4189 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 4190 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame^] | 4191 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4192 | __ str(r5, MemOperand(ip)); |
| 4193 | |
| 4194 | // Invoke the function by calling through JS entry trampoline builtin. |
| 4195 | // Notice that we cannot store a reference to the trampoline code directly in |
| 4196 | // this stub, because runtime stubs are not traversed when doing GC. |
| 4197 | |
| 4198 | // Expected registers by Builtins::JSEntryTrampoline |
| 4199 | // r0: code entry |
| 4200 | // r1: function |
| 4201 | // r2: receiver |
| 4202 | // r3: argc |
| 4203 | // r4: argv |
| 4204 | if (is_construct) { |
| 4205 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 4206 | __ mov(ip, Operand(construct_entry)); |
| 4207 | } else { |
| 4208 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 4209 | __ mov(ip, Operand(entry)); |
| 4210 | } |
| 4211 | __ ldr(ip, MemOperand(ip)); // deref address |
| 4212 | |
| 4213 | // Branch and link to JSEntryTrampoline |
| 4214 | __ mov(lr, Operand(pc)); |
| 4215 | __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 4216 | |
| 4217 | // Unlink this frame from the handler chain. When reading the |
| 4218 | // address of the next handler, there is no need to use the address |
| 4219 | // displacement since the current stack pointer (sp) points directly |
| 4220 | // to the stack handler. |
| 4221 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 4222 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 4223 | __ str(r3, MemOperand(ip)); |
| 4224 | // No need to restore registers |
| 4225 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 4226 | |
| 4227 | __ bind(&exit); // r0 holds result |
| 4228 | // Restore the top frame descriptors from the stack. |
| 4229 | __ pop(r3); |
| 4230 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 4231 | __ str(r3, MemOperand(ip)); |
| 4232 | |
| 4233 | // Reset the stack to the callee saved registers. |
| 4234 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 4235 | |
| 4236 | // Restore callee-saved registers and return. |
| 4237 | #ifdef DEBUG |
| 4238 | if (FLAG_debug_code) __ mov(lr, Operand(pc)); |
| 4239 | #endif |
| 4240 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 4241 | } |
| 4242 | |
| 4243 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4244 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4245 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4246 | Label adaptor; |
| 4247 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 4248 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 4249 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4250 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4251 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4252 | // Nothing to do: The formal number of parameters has already been |
| 4253 | // passed in register r0 by calling function. Just return it. |
| 4254 | __ mov(pc, lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4255 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4256 | // Arguments adaptor case: Read the arguments length from the |
| 4257 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4258 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4259 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4260 | __ mov(pc, lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
| 4263 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4264 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 4265 | // The displacement is the offset of the last parameter (if any) |
| 4266 | // relative to the frame pointer. |
| 4267 | static const int kDisplacement = |
| 4268 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4269 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4270 | // Check that the key is a smi. |
| 4271 | Label slow; |
| 4272 | __ tst(r1, Operand(kSmiTagMask)); |
| 4273 | __ b(ne, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4274 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4275 | // Check if the calling frame is an arguments adaptor frame. |
| 4276 | Label adaptor; |
| 4277 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 4278 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 4279 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 4280 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4281 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4282 | // Check index against formal parameters count limit passed in |
| 4283 | // through register eax. Use unsigned comparison to get negative |
| 4284 | // check for free. |
| 4285 | __ cmp(r1, r0); |
| 4286 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4287 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4288 | // Read the argument from the stack and return it. |
| 4289 | __ sub(r3, r0, r1); |
| 4290 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4291 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 4292 | __ mov(pc, lr); |
| 4293 | |
| 4294 | // Arguments adaptor case: Check index against actual arguments |
| 4295 | // limit found in the arguments adaptor frame. Use unsigned |
| 4296 | // comparison to get negative check for free. |
| 4297 | __ bind(&adaptor); |
| 4298 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 4299 | __ cmp(r1, r0); |
| 4300 | __ b(cs, &slow); |
| 4301 | |
| 4302 | // Read the argument from the adaptor frame and return it. |
| 4303 | __ sub(r3, r0, r1); |
| 4304 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4305 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 4306 | __ mov(pc, lr); |
| 4307 | |
| 4308 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 4309 | // by calling the runtime system. |
| 4310 | __ bind(&slow); |
| 4311 | __ push(r1); |
| 4312 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1); |
| 4313 | } |
| 4314 | |
| 4315 | |
| 4316 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 4317 | // Check if the calling frame is an arguments adaptor frame. |
| 4318 | Label runtime; |
| 4319 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 4320 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 4321 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 4322 | __ b(ne, &runtime); |
| 4323 | |
| 4324 | // Patch the arguments.length and the parameters pointer. |
| 4325 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 4326 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 4327 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4328 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 4329 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 4330 | |
| 4331 | // Do the runtime call to allocate the arguments object. |
| 4332 | __ bind(&runtime); |
| 4333 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | |
| 4337 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 4338 | Label slow; |
| 4339 | // Get the function to call from the stack. |
| 4340 | // function, receiver [, arguments] |
| 4341 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 4342 | |
| 4343 | // Check that the function is really a JavaScript function. |
| 4344 | // r1: pushed function (to be verified) |
| 4345 | __ tst(r1, Operand(kSmiTagMask)); |
| 4346 | __ b(eq, &slow); |
| 4347 | // Get the map of the function object. |
| 4348 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4349 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4350 | __ cmp(r2, Operand(JS_FUNCTION_TYPE)); |
| 4351 | __ b(ne, &slow); |
| 4352 | |
| 4353 | // Fast-case: Invoke the function now. |
| 4354 | // r1: pushed function |
| 4355 | ParameterCount actual(argc_); |
| 4356 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 4357 | |
| 4358 | // Slow-case: Non-function called. |
| 4359 | __ bind(&slow); |
| 4360 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4361 | __ mov(r2, Operand(0)); |
| 4362 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 4363 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 4364 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | |
| 4368 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4369 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4370 | } } // namespace v8::internal |